OSDN Git Service

* config/arm/arm.c (arm_print_operand): Deal with HIGH.
[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   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1617      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1618      will ever exist.  GCC makes no attempt to support this combination.  */
1619   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1620     sorry ("iWMMXt and hardware floating point");
1621
1622   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1623   if (TARGET_THUMB2 && TARGET_IWMMXT)
1624     sorry ("Thumb-2 iWMMXt");
1625
1626   /* __fp16 support currently assumes the core has ldrh.  */
1627   if (!arm_arch4 && arm_fp16_format != ARM_FP16_FORMAT_NONE)
1628     sorry ("__fp16 and no ldrh");
1629
1630   /* If soft-float is specified then don't use FPU.  */
1631   if (TARGET_SOFT_FLOAT)
1632     arm_fpu_arch = FPUTYPE_NONE;
1633
1634   /* For arm2/3 there is no need to do any scheduling if there is only
1635      a floating point emulator, or we are doing software floating-point.  */
1636   if ((TARGET_SOFT_FLOAT
1637        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1638        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1639       && (tune_flags & FL_MODE32) == 0)
1640     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1641
1642   if (target_thread_switch)
1643     {
1644       if (strcmp (target_thread_switch, "soft") == 0)
1645         target_thread_pointer = TP_SOFT;
1646       else if (strcmp (target_thread_switch, "auto") == 0)
1647         target_thread_pointer = TP_AUTO;
1648       else if (strcmp (target_thread_switch, "cp15") == 0)
1649         target_thread_pointer = TP_CP15;
1650       else
1651         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1652     }
1653
1654   /* Use the cp15 method if it is available.  */
1655   if (target_thread_pointer == TP_AUTO)
1656     {
1657       if (arm_arch6k && !TARGET_THUMB)
1658         target_thread_pointer = TP_CP15;
1659       else
1660         target_thread_pointer = TP_SOFT;
1661     }
1662
1663   if (TARGET_HARD_TP && TARGET_THUMB1)
1664     error ("can not use -mtp=cp15 with 16-bit Thumb");
1665
1666   /* Override the default structure alignment for AAPCS ABI.  */
1667   if (TARGET_AAPCS_BASED)
1668     arm_structure_size_boundary = 8;
1669
1670   if (structure_size_string != NULL)
1671     {
1672       int size = strtol (structure_size_string, NULL, 0);
1673
1674       if (size == 8 || size == 32
1675           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1676         arm_structure_size_boundary = size;
1677       else
1678         warning (0, "structure size boundary can only be set to %s",
1679                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1680     }
1681
1682   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1683     {
1684       error ("RTP PIC is incompatible with Thumb");
1685       flag_pic = 0;
1686     }
1687
1688   /* If stack checking is disabled, we can use r10 as the PIC register,
1689      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1690   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1691     {
1692       if (TARGET_VXWORKS_RTP)
1693         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1694       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1695     }
1696
1697   if (flag_pic && TARGET_VXWORKS_RTP)
1698     arm_pic_register = 9;
1699
1700   if (arm_pic_register_string != NULL)
1701     {
1702       int pic_register = decode_reg_name (arm_pic_register_string);
1703
1704       if (!flag_pic)
1705         warning (0, "-mpic-register= is useless without -fpic");
1706
1707       /* Prevent the user from choosing an obviously stupid PIC register.  */
1708       else if (pic_register < 0 || call_used_regs[pic_register]
1709                || pic_register == HARD_FRAME_POINTER_REGNUM
1710                || pic_register == STACK_POINTER_REGNUM
1711                || pic_register >= PC_REGNUM
1712                || (TARGET_VXWORKS_RTP
1713                    && (unsigned int) pic_register != arm_pic_register))
1714         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1715       else
1716         arm_pic_register = pic_register;
1717     }
1718
1719   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1720   if (fix_cm3_ldrd == 2)
1721     {
1722       if (selected_cpu == cortexm3)
1723         fix_cm3_ldrd = 1;
1724       else
1725         fix_cm3_ldrd = 0;
1726     }
1727
1728   /* ??? We might want scheduling for thumb2.  */
1729   if (TARGET_THUMB && flag_schedule_insns)
1730     {
1731       /* Don't warn since it's on by default in -O2.  */
1732       flag_schedule_insns = 0;
1733     }
1734
1735   if (optimize_size)
1736     {
1737       arm_constant_limit = 1;
1738
1739       /* If optimizing for size, bump the number of instructions that we
1740          are prepared to conditionally execute (even on a StrongARM).  */
1741       max_insns_skipped = 6;
1742     }
1743   else
1744     {
1745       /* For processors with load scheduling, it never costs more than
1746          2 cycles to load a constant, and the load scheduler may well
1747          reduce that to 1.  */
1748       if (arm_ld_sched)
1749         arm_constant_limit = 1;
1750
1751       /* On XScale the longer latency of a load makes it more difficult
1752          to achieve a good schedule, so it's faster to synthesize
1753          constants that can be done in two insns.  */
1754       if (arm_tune_xscale)
1755         arm_constant_limit = 2;
1756
1757       /* StrongARM has early execution of branches, so a sequence
1758          that is worth skipping is shorter.  */
1759       if (arm_tune_strongarm)
1760         max_insns_skipped = 3;
1761     }
1762
1763   /* Register global variables with the garbage collector.  */
1764   arm_add_gc_roots ();
1765 }
1766
1767 static void
1768 arm_add_gc_roots (void)
1769 {
1770   gcc_obstack_init(&minipool_obstack);
1771   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1772 }
1773 \f
1774 /* A table of known ARM exception types.
1775    For use with the interrupt function attribute.  */
1776
1777 typedef struct
1778 {
1779   const char *const arg;
1780   const unsigned long return_value;
1781 }
1782 isr_attribute_arg;
1783
1784 static const isr_attribute_arg isr_attribute_args [] =
1785 {
1786   { "IRQ",   ARM_FT_ISR },
1787   { "irq",   ARM_FT_ISR },
1788   { "FIQ",   ARM_FT_FIQ },
1789   { "fiq",   ARM_FT_FIQ },
1790   { "ABORT", ARM_FT_ISR },
1791   { "abort", ARM_FT_ISR },
1792   { "ABORT", ARM_FT_ISR },
1793   { "abort", ARM_FT_ISR },
1794   { "UNDEF", ARM_FT_EXCEPTION },
1795   { "undef", ARM_FT_EXCEPTION },
1796   { "SWI",   ARM_FT_EXCEPTION },
1797   { "swi",   ARM_FT_EXCEPTION },
1798   { NULL,    ARM_FT_NORMAL }
1799 };
1800
1801 /* Returns the (interrupt) function type of the current
1802    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1803
1804 static unsigned long
1805 arm_isr_value (tree argument)
1806 {
1807   const isr_attribute_arg * ptr;
1808   const char *              arg;
1809
1810   if (!arm_arch_notm)
1811     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1812
1813   /* No argument - default to IRQ.  */
1814   if (argument == NULL_TREE)
1815     return ARM_FT_ISR;
1816
1817   /* Get the value of the argument.  */
1818   if (TREE_VALUE (argument) == NULL_TREE
1819       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1820     return ARM_FT_UNKNOWN;
1821
1822   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1823
1824   /* Check it against the list of known arguments.  */
1825   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1826     if (streq (arg, ptr->arg))
1827       return ptr->return_value;
1828
1829   /* An unrecognized interrupt type.  */
1830   return ARM_FT_UNKNOWN;
1831 }
1832
1833 /* Computes the type of the current function.  */
1834
1835 static unsigned long
1836 arm_compute_func_type (void)
1837 {
1838   unsigned long type = ARM_FT_UNKNOWN;
1839   tree a;
1840   tree attr;
1841
1842   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1843
1844   /* Decide if the current function is volatile.  Such functions
1845      never return, and many memory cycles can be saved by not storing
1846      register values that will never be needed again.  This optimization
1847      was added to speed up context switching in a kernel application.  */
1848   if (optimize > 0
1849       && (TREE_NOTHROW (current_function_decl)
1850           || !(flag_unwind_tables
1851                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1852       && TREE_THIS_VOLATILE (current_function_decl))
1853     type |= ARM_FT_VOLATILE;
1854
1855   if (cfun->static_chain_decl != NULL)
1856     type |= ARM_FT_NESTED;
1857
1858   attr = DECL_ATTRIBUTES (current_function_decl);
1859
1860   a = lookup_attribute ("naked", attr);
1861   if (a != NULL_TREE)
1862     type |= ARM_FT_NAKED;
1863
1864   a = lookup_attribute ("isr", attr);
1865   if (a == NULL_TREE)
1866     a = lookup_attribute ("interrupt", attr);
1867
1868   if (a == NULL_TREE)
1869     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1870   else
1871     type |= arm_isr_value (TREE_VALUE (a));
1872
1873   return type;
1874 }
1875
1876 /* Returns the type of the current function.  */
1877
1878 unsigned long
1879 arm_current_func_type (void)
1880 {
1881   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1882     cfun->machine->func_type = arm_compute_func_type ();
1883
1884   return cfun->machine->func_type;
1885 }
1886
1887 bool
1888 arm_allocate_stack_slots_for_args (void)
1889 {
1890   /* Naked functions should not allocate stack slots for arguments.  */
1891   return !IS_NAKED (arm_current_func_type ());
1892 }
1893
1894 \f
1895 /* Return 1 if it is possible to return using a single instruction.
1896    If SIBLING is non-null, this is a test for a return before a sibling
1897    call.  SIBLING is the call insn, so we can examine its register usage.  */
1898
1899 int
1900 use_return_insn (int iscond, rtx sibling)
1901 {
1902   int regno;
1903   unsigned int func_type;
1904   unsigned long saved_int_regs;
1905   unsigned HOST_WIDE_INT stack_adjust;
1906   arm_stack_offsets *offsets;
1907
1908   /* Never use a return instruction before reload has run.  */
1909   if (!reload_completed)
1910     return 0;
1911
1912   func_type = arm_current_func_type ();
1913
1914   /* Naked, volatile and stack alignment functions need special
1915      consideration.  */
1916   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1917     return 0;
1918
1919   /* So do interrupt functions that use the frame pointer and Thumb
1920      interrupt functions.  */
1921   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1922     return 0;
1923
1924   offsets = arm_get_frame_offsets ();
1925   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1926
1927   /* As do variadic functions.  */
1928   if (crtl->args.pretend_args_size
1929       || cfun->machine->uses_anonymous_args
1930       /* Or if the function calls __builtin_eh_return () */
1931       || crtl->calls_eh_return
1932       /* Or if the function calls alloca */
1933       || cfun->calls_alloca
1934       /* Or if there is a stack adjustment.  However, if the stack pointer
1935          is saved on the stack, we can use a pre-incrementing stack load.  */
1936       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
1937                                  && stack_adjust == 4)))
1938     return 0;
1939
1940   saved_int_regs = offsets->saved_regs_mask;
1941
1942   /* Unfortunately, the insn
1943
1944        ldmib sp, {..., sp, ...}
1945
1946      triggers a bug on most SA-110 based devices, such that the stack
1947      pointer won't be correctly restored if the instruction takes a
1948      page fault.  We work around this problem by popping r3 along with
1949      the other registers, since that is never slower than executing
1950      another instruction.
1951
1952      We test for !arm_arch5 here, because code for any architecture
1953      less than this could potentially be run on one of the buggy
1954      chips.  */
1955   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1956     {
1957       /* Validate that r3 is a call-clobbered register (always true in
1958          the default abi) ...  */
1959       if (!call_used_regs[3])
1960         return 0;
1961
1962       /* ... that it isn't being used for a return value ... */
1963       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1964         return 0;
1965
1966       /* ... or for a tail-call argument ...  */
1967       if (sibling)
1968         {
1969           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1970
1971           if (find_regno_fusage (sibling, USE, 3))
1972             return 0;
1973         }
1974
1975       /* ... and that there are no call-saved registers in r0-r2
1976          (always true in the default ABI).  */
1977       if (saved_int_regs & 0x7)
1978         return 0;
1979     }
1980
1981   /* Can't be done if interworking with Thumb, and any registers have been
1982      stacked.  */
1983   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1984     return 0;
1985
1986   /* On StrongARM, conditional returns are expensive if they aren't
1987      taken and multiple registers have been stacked.  */
1988   if (iscond && arm_tune_strongarm)
1989     {
1990       /* Conditional return when just the LR is stored is a simple
1991          conditional-load instruction, that's not expensive.  */
1992       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1993         return 0;
1994
1995       if (flag_pic 
1996           && arm_pic_register != INVALID_REGNUM
1997           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1998         return 0;
1999     }
2000
2001   /* If there are saved registers but the LR isn't saved, then we need
2002      two instructions for the return.  */
2003   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
2004     return 0;
2005
2006   /* Can't be done if any of the FPA regs are pushed,
2007      since this also requires an insn.  */
2008   if (TARGET_HARD_FLOAT && TARGET_FPA)
2009     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
2010       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2011         return 0;
2012
2013   /* Likewise VFP regs.  */
2014   if (TARGET_HARD_FLOAT && TARGET_VFP)
2015     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
2016       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
2017         return 0;
2018
2019   if (TARGET_REALLY_IWMMXT)
2020     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
2021       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
2022         return 0;
2023
2024   return 1;
2025 }
2026
2027 /* Return TRUE if int I is a valid immediate ARM constant.  */
2028
2029 int
2030 const_ok_for_arm (HOST_WIDE_INT i)
2031 {
2032   int lowbit;
2033
2034   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
2035      be all zero, or all one.  */
2036   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
2037       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
2038           != ((~(unsigned HOST_WIDE_INT) 0)
2039               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
2040     return FALSE;
2041
2042   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
2043
2044   /* Fast return for 0 and small values.  We must do this for zero, since
2045      the code below can't handle that one case.  */
2046   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
2047     return TRUE;
2048
2049   /* Get the number of trailing zeros.  */
2050   lowbit = ffs((int) i) - 1;
2051   
2052   /* Only even shifts are allowed in ARM mode so round down to the
2053      nearest even number.  */
2054   if (TARGET_ARM)
2055     lowbit &= ~1;
2056
2057   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
2058     return TRUE;
2059
2060   if (TARGET_ARM)
2061     {
2062       /* Allow rotated constants in ARM mode.  */
2063       if (lowbit <= 4
2064            && ((i & ~0xc000003f) == 0
2065                || (i & ~0xf000000f) == 0
2066                || (i & ~0xfc000003) == 0))
2067         return TRUE;
2068     }
2069   else
2070     {
2071       HOST_WIDE_INT v;
2072
2073       /* Allow repeated pattern.  */
2074       v = i & 0xff;
2075       v |= v << 16;
2076       if (i == v || i == (v | (v << 8)))
2077         return TRUE;
2078     }
2079
2080   return FALSE;
2081 }
2082
2083 /* Return true if I is a valid constant for the operation CODE.  */
2084 static int
2085 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
2086 {
2087   if (const_ok_for_arm (i))
2088     return 1;
2089
2090   switch (code)
2091     {
2092     case PLUS:
2093     case COMPARE:
2094     case EQ:
2095     case NE:
2096     case GT:
2097     case LE:
2098     case LT:
2099     case GE:
2100     case GEU:
2101     case LTU:
2102     case GTU:
2103     case LEU:
2104     case UNORDERED:
2105     case ORDERED:
2106     case UNEQ:
2107     case UNGE:
2108     case UNLT:
2109     case UNGT:
2110     case UNLE:
2111       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
2112
2113     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
2114     case XOR:
2115       return 0;
2116
2117     case IOR:
2118       if (TARGET_THUMB2)
2119         return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2120       return 0;
2121
2122     case AND:
2123       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
2124
2125     default:
2126       gcc_unreachable ();
2127     }
2128 }
2129
2130 /* Emit a sequence of insns to handle a large constant.
2131    CODE is the code of the operation required, it can be any of SET, PLUS,
2132    IOR, AND, XOR, MINUS;
2133    MODE is the mode in which the operation is being performed;
2134    VAL is the integer to operate on;
2135    SOURCE is the other operand (a register, or a null-pointer for SET);
2136    SUBTARGETS means it is safe to create scratch registers if that will
2137    either produce a simpler sequence, or we will want to cse the values.
2138    Return value is the number of insns emitted.  */
2139
2140 /* ??? Tweak this for thumb2.  */
2141 int
2142 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
2143                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
2144 {
2145   rtx cond;
2146
2147   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
2148     cond = COND_EXEC_TEST (PATTERN (insn));
2149   else
2150     cond = NULL_RTX;
2151
2152   if (subtargets || code == SET
2153       || (GET_CODE (target) == REG && GET_CODE (source) == REG
2154           && REGNO (target) != REGNO (source)))
2155     {
2156       /* After arm_reorg has been called, we can't fix up expensive
2157          constants by pushing them into memory so we must synthesize
2158          them in-line, regardless of the cost.  This is only likely to
2159          be more costly on chips that have load delay slots and we are
2160          compiling without running the scheduler (so no splitting
2161          occurred before the final instruction emission).
2162
2163          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
2164       */
2165       if (!after_arm_reorg
2166           && !cond
2167           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
2168                                 1, 0)
2169               > arm_constant_limit + (code != SET)))
2170         {
2171           if (code == SET)
2172             {
2173               /* Currently SET is the only monadic value for CODE, all
2174                  the rest are diadic.  */
2175               if (TARGET_USE_MOVT)
2176                 arm_emit_movpair (target, GEN_INT (val));
2177               else
2178                 emit_set_insn (target, GEN_INT (val));
2179
2180               return 1;
2181             }
2182           else
2183             {
2184               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
2185
2186               if (TARGET_USE_MOVT)
2187                 arm_emit_movpair (temp, GEN_INT (val));
2188               else
2189                 emit_set_insn (temp, GEN_INT (val));
2190
2191               /* For MINUS, the value is subtracted from, since we never
2192                  have subtraction of a constant.  */
2193               if (code == MINUS)
2194                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
2195               else
2196                 emit_set_insn (target,
2197                                gen_rtx_fmt_ee (code, mode, source, temp));
2198               return 2;
2199             }
2200         }
2201     }
2202
2203   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
2204                            1);
2205 }
2206
2207 /* Return the number of ARM instructions required to synthesize the given
2208    constant.  */
2209 static int
2210 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2211 {
2212   HOST_WIDE_INT temp1;
2213   int num_insns = 0;
2214   do
2215     {
2216       int end;
2217
2218       if (i <= 0)
2219         i += 32;
2220       if (remainder & (3 << (i - 2)))
2221         {
2222           end = i - 8;
2223           if (end < 0)
2224             end += 32;
2225           temp1 = remainder & ((0x0ff << end)
2226                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2227           remainder &= ~temp1;
2228           num_insns++;
2229           i -= 6;
2230         }
2231       i -= 2;
2232     } while (remainder);
2233   return num_insns;
2234 }
2235
2236 /* Emit an instruction with the indicated PATTERN.  If COND is
2237    non-NULL, conditionalize the execution of the instruction on COND
2238    being true.  */
2239
2240 static void
2241 emit_constant_insn (rtx cond, rtx pattern)
2242 {
2243   if (cond)
2244     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2245   emit_insn (pattern);
2246 }
2247
2248 /* As above, but extra parameter GENERATE which, if clear, suppresses
2249    RTL generation.  */
2250 /* ??? This needs more work for thumb2.  */
2251
2252 static int
2253 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2254                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2255                   int generate)
2256 {
2257   int can_invert = 0;
2258   int can_negate = 0;
2259   int can_negate_initial = 0;
2260   int can_shift = 0;
2261   int i;
2262   int num_bits_set = 0;
2263   int set_sign_bit_copies = 0;
2264   int clear_sign_bit_copies = 0;
2265   int clear_zero_bit_copies = 0;
2266   int set_zero_bit_copies = 0;
2267   int insns = 0;
2268   unsigned HOST_WIDE_INT temp1, temp2;
2269   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2270
2271   /* Find out which operations are safe for a given CODE.  Also do a quick
2272      check for degenerate cases; these can occur when DImode operations
2273      are split.  */
2274   switch (code)
2275     {
2276     case SET:
2277       can_invert = 1;
2278       can_shift = 1;
2279       can_negate = 1;
2280       break;
2281
2282     case PLUS:
2283       can_negate = 1;
2284       can_negate_initial = 1;
2285       break;
2286
2287     case IOR:
2288       if (remainder == 0xffffffff)
2289         {
2290           if (generate)
2291             emit_constant_insn (cond,
2292                                 gen_rtx_SET (VOIDmode, target,
2293                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2294           return 1;
2295         }
2296
2297       if (remainder == 0)
2298         {
2299           if (reload_completed && rtx_equal_p (target, source))
2300             return 0;
2301
2302           if (generate)
2303             emit_constant_insn (cond,
2304                                 gen_rtx_SET (VOIDmode, target, source));
2305           return 1;
2306         }
2307
2308       if (TARGET_THUMB2)
2309         can_invert = 1;
2310       break;
2311
2312     case AND:
2313       if (remainder == 0)
2314         {
2315           if (generate)
2316             emit_constant_insn (cond,
2317                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2318           return 1;
2319         }
2320       if (remainder == 0xffffffff)
2321         {
2322           if (reload_completed && rtx_equal_p (target, source))
2323             return 0;
2324           if (generate)
2325             emit_constant_insn (cond,
2326                                 gen_rtx_SET (VOIDmode, target, source));
2327           return 1;
2328         }
2329       can_invert = 1;
2330       break;
2331
2332     case XOR:
2333       if (remainder == 0)
2334         {
2335           if (reload_completed && rtx_equal_p (target, source))
2336             return 0;
2337           if (generate)
2338             emit_constant_insn (cond,
2339                                 gen_rtx_SET (VOIDmode, target, source));
2340           return 1;
2341         }
2342
2343       /* We don't know how to handle other cases yet.  */
2344       gcc_assert (remainder == 0xffffffff);
2345
2346       if (generate)
2347         emit_constant_insn (cond,
2348                             gen_rtx_SET (VOIDmode, target,
2349                                          gen_rtx_NOT (mode, source)));
2350       return 1;
2351
2352     case MINUS:
2353       /* We treat MINUS as (val - source), since (source - val) is always
2354          passed as (source + (-val)).  */
2355       if (remainder == 0)
2356         {
2357           if (generate)
2358             emit_constant_insn (cond,
2359                                 gen_rtx_SET (VOIDmode, target,
2360                                              gen_rtx_NEG (mode, source)));
2361           return 1;
2362         }
2363       if (const_ok_for_arm (val))
2364         {
2365           if (generate)
2366             emit_constant_insn (cond,
2367                                 gen_rtx_SET (VOIDmode, target,
2368                                              gen_rtx_MINUS (mode, GEN_INT (val),
2369                                                             source)));
2370           return 1;
2371         }
2372       can_negate = 1;
2373
2374       break;
2375
2376     default:
2377       gcc_unreachable ();
2378     }
2379
2380   /* If we can do it in one insn get out quickly.  */
2381   if (const_ok_for_arm (val)
2382       || (can_negate_initial && const_ok_for_arm (-val))
2383       || (can_invert && const_ok_for_arm (~val)))
2384     {
2385       if (generate)
2386         emit_constant_insn (cond,
2387                             gen_rtx_SET (VOIDmode, target,
2388                                          (source
2389                                           ? gen_rtx_fmt_ee (code, mode, source,
2390                                                             GEN_INT (val))
2391                                           : GEN_INT (val))));
2392       return 1;
2393     }
2394
2395   /* Calculate a few attributes that may be useful for specific
2396      optimizations.  */
2397   /* Count number of leading zeros.  */
2398   for (i = 31; i >= 0; i--)
2399     {
2400       if ((remainder & (1 << i)) == 0)
2401         clear_sign_bit_copies++;
2402       else
2403         break;
2404     }
2405
2406   /* Count number of leading 1's.  */
2407   for (i = 31; i >= 0; i--)
2408     {
2409       if ((remainder & (1 << i)) != 0)
2410         set_sign_bit_copies++;
2411       else
2412         break;
2413     }
2414
2415   /* Count number of trailing zero's.  */
2416   for (i = 0; i <= 31; i++)
2417     {
2418       if ((remainder & (1 << i)) == 0)
2419         clear_zero_bit_copies++;
2420       else
2421         break;
2422     }
2423
2424   /* Count number of trailing 1's.  */
2425   for (i = 0; i <= 31; i++)
2426     {
2427       if ((remainder & (1 << i)) != 0)
2428         set_zero_bit_copies++;
2429       else
2430         break;
2431     }
2432
2433   switch (code)
2434     {
2435     case SET:
2436       /* See if we can use movw.  */
2437       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2438         {
2439           if (generate)
2440             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2441                                                    GEN_INT (val)));
2442           return 1;
2443         }
2444
2445       /* See if we can do this by sign_extending a constant that is known
2446          to be negative.  This is a good, way of doing it, since the shift
2447          may well merge into a subsequent insn.  */
2448       if (set_sign_bit_copies > 1)
2449         {
2450           if (const_ok_for_arm
2451               (temp1 = ARM_SIGN_EXTEND (remainder
2452                                         << (set_sign_bit_copies - 1))))
2453             {
2454               if (generate)
2455                 {
2456                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2457                   emit_constant_insn (cond,
2458                                       gen_rtx_SET (VOIDmode, new_src,
2459                                                    GEN_INT (temp1)));
2460                   emit_constant_insn (cond,
2461                                       gen_ashrsi3 (target, new_src,
2462                                                    GEN_INT (set_sign_bit_copies - 1)));
2463                 }
2464               return 2;
2465             }
2466           /* For an inverted constant, we will need to set the low bits,
2467              these will be shifted out of harm's way.  */
2468           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2469           if (const_ok_for_arm (~temp1))
2470             {
2471               if (generate)
2472                 {
2473                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2474                   emit_constant_insn (cond,
2475                                       gen_rtx_SET (VOIDmode, new_src,
2476                                                    GEN_INT (temp1)));
2477                   emit_constant_insn (cond,
2478                                       gen_ashrsi3 (target, new_src,
2479                                                    GEN_INT (set_sign_bit_copies - 1)));
2480                 }
2481               return 2;
2482             }
2483         }
2484
2485       /* See if we can calculate the value as the difference between two
2486          valid immediates.  */
2487       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2488         {
2489           int topshift = clear_sign_bit_copies & ~1;
2490
2491           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2492                                    & (0xff000000 >> topshift));
2493
2494           /* If temp1 is zero, then that means the 9 most significant
2495              bits of remainder were 1 and we've caused it to overflow.
2496              When topshift is 0 we don't need to do anything since we
2497              can borrow from 'bit 32'.  */
2498           if (temp1 == 0 && topshift != 0)
2499             temp1 = 0x80000000 >> (topshift - 1);
2500
2501           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2502
2503           if (const_ok_for_arm (temp2))
2504             {
2505               if (generate)
2506                 {
2507                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2508                   emit_constant_insn (cond,
2509                                       gen_rtx_SET (VOIDmode, new_src,
2510                                                    GEN_INT (temp1)));
2511                   emit_constant_insn (cond,
2512                                       gen_addsi3 (target, new_src,
2513                                                   GEN_INT (-temp2)));
2514                 }
2515
2516               return 2;
2517             }
2518         }
2519
2520       /* See if we can generate this by setting the bottom (or the top)
2521          16 bits, and then shifting these into the other half of the
2522          word.  We only look for the simplest cases, to do more would cost
2523          too much.  Be careful, however, not to generate this when the
2524          alternative would take fewer insns.  */
2525       if (val & 0xffff0000)
2526         {
2527           temp1 = remainder & 0xffff0000;
2528           temp2 = remainder & 0x0000ffff;
2529
2530           /* Overlaps outside this range are best done using other methods.  */
2531           for (i = 9; i < 24; i++)
2532             {
2533               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2534                   && !const_ok_for_arm (temp2))
2535                 {
2536                   rtx new_src = (subtargets
2537                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2538                                  : target);
2539                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2540                                             source, subtargets, generate);
2541                   source = new_src;
2542                   if (generate)
2543                     emit_constant_insn
2544                       (cond,
2545                        gen_rtx_SET
2546                        (VOIDmode, target,
2547                         gen_rtx_IOR (mode,
2548                                      gen_rtx_ASHIFT (mode, source,
2549                                                      GEN_INT (i)),
2550                                      source)));
2551                   return insns + 1;
2552                 }
2553             }
2554
2555           /* Don't duplicate cases already considered.  */
2556           for (i = 17; i < 24; i++)
2557             {
2558               if (((temp1 | (temp1 >> i)) == remainder)
2559                   && !const_ok_for_arm (temp1))
2560                 {
2561                   rtx new_src = (subtargets
2562                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2563                                  : target);
2564                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2565                                             source, subtargets, generate);
2566                   source = new_src;
2567                   if (generate)
2568                     emit_constant_insn
2569                       (cond,
2570                        gen_rtx_SET (VOIDmode, target,
2571                                     gen_rtx_IOR
2572                                     (mode,
2573                                      gen_rtx_LSHIFTRT (mode, source,
2574                                                        GEN_INT (i)),
2575                                      source)));
2576                   return insns + 1;
2577                 }
2578             }
2579         }
2580       break;
2581
2582     case IOR:
2583     case XOR:
2584       /* If we have IOR or XOR, and the constant can be loaded in a
2585          single instruction, and we can find a temporary to put it in,
2586          then this can be done in two instructions instead of 3-4.  */
2587       if (subtargets
2588           /* TARGET can't be NULL if SUBTARGETS is 0 */
2589           || (reload_completed && !reg_mentioned_p (target, source)))
2590         {
2591           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2592             {
2593               if (generate)
2594                 {
2595                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2596
2597                   emit_constant_insn (cond,
2598                                       gen_rtx_SET (VOIDmode, sub,
2599                                                    GEN_INT (val)));
2600                   emit_constant_insn (cond,
2601                                       gen_rtx_SET (VOIDmode, target,
2602                                                    gen_rtx_fmt_ee (code, mode,
2603                                                                    source, sub)));
2604                 }
2605               return 2;
2606             }
2607         }
2608
2609       if (code == XOR)
2610         break;
2611
2612       /*  Convert.
2613           x = y | constant ( which is composed of set_sign_bit_copies of leading 1s
2614                              and the remainder 0s for e.g. 0xfff00000)
2615           x = ~(~(y ashift set_sign_bit_copies) lshiftrt set_sign_bit_copies)
2616
2617           This can be done in 2 instructions by using shifts with mov or mvn.
2618           e.g. for
2619           x = x | 0xfff00000;
2620           we generate.
2621           mvn   r0, r0, asl #12
2622           mvn   r0, r0, lsr #12  */
2623       if (set_sign_bit_copies > 8
2624           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2625         {
2626           if (generate)
2627             {
2628               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2629               rtx shift = GEN_INT (set_sign_bit_copies);
2630
2631               emit_constant_insn
2632                 (cond,
2633                  gen_rtx_SET (VOIDmode, sub,
2634                               gen_rtx_NOT (mode,
2635                                            gen_rtx_ASHIFT (mode,
2636                                                            source,
2637                                                            shift))));
2638               emit_constant_insn
2639                 (cond,
2640                  gen_rtx_SET (VOIDmode, target,
2641                               gen_rtx_NOT (mode,
2642                                            gen_rtx_LSHIFTRT (mode, sub,
2643                                                              shift))));
2644             }
2645           return 2;
2646         }
2647
2648       /* Convert
2649           x = y | constant (which has set_zero_bit_copies number of trailing ones).
2650            to
2651           x = ~((~y lshiftrt set_zero_bit_copies) ashift set_zero_bit_copies).
2652
2653           For eg. r0 = r0 | 0xfff
2654                mvn      r0, r0, lsr #12
2655                mvn      r0, r0, asl #12
2656
2657       */
2658       if (set_zero_bit_copies > 8
2659           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2660         {
2661           if (generate)
2662             {
2663               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2664               rtx shift = GEN_INT (set_zero_bit_copies);
2665
2666               emit_constant_insn
2667                 (cond,
2668                  gen_rtx_SET (VOIDmode, sub,
2669                               gen_rtx_NOT (mode,
2670                                            gen_rtx_LSHIFTRT (mode,
2671                                                              source,
2672                                                              shift))));
2673               emit_constant_insn
2674                 (cond,
2675                  gen_rtx_SET (VOIDmode, target,
2676                               gen_rtx_NOT (mode,
2677                                            gen_rtx_ASHIFT (mode, sub,
2678                                                            shift))));
2679             }
2680           return 2;
2681         }
2682
2683       /* This will never be reached for Thumb2 because orn is a valid
2684          instruction. This is for Thumb1 and the ARM 32 bit cases.
2685
2686          x = y | constant (such that ~constant is a valid constant)
2687          Transform this to
2688          x = ~(~y & ~constant).
2689       */
2690       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2691         {
2692           if (generate)
2693             {
2694               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2695               emit_constant_insn (cond,
2696                                   gen_rtx_SET (VOIDmode, sub,
2697                                                gen_rtx_NOT (mode, source)));
2698               source = sub;
2699               if (subtargets)
2700                 sub = gen_reg_rtx (mode);
2701               emit_constant_insn (cond,
2702                                   gen_rtx_SET (VOIDmode, sub,
2703                                                gen_rtx_AND (mode, source,
2704                                                             GEN_INT (temp1))));
2705               emit_constant_insn (cond,
2706                                   gen_rtx_SET (VOIDmode, target,
2707                                                gen_rtx_NOT (mode, sub)));
2708             }
2709           return 3;
2710         }
2711       break;
2712
2713     case AND:
2714       /* See if two shifts will do 2 or more insn's worth of work.  */
2715       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2716         {
2717           HOST_WIDE_INT shift_mask = ((0xffffffff
2718                                        << (32 - clear_sign_bit_copies))
2719                                       & 0xffffffff);
2720
2721           if ((remainder | shift_mask) != 0xffffffff)
2722             {
2723               if (generate)
2724                 {
2725                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2726                   insns = arm_gen_constant (AND, mode, cond,
2727                                             remainder | shift_mask,
2728                                             new_src, source, subtargets, 1);
2729                   source = new_src;
2730                 }
2731               else
2732                 {
2733                   rtx targ = subtargets ? NULL_RTX : target;
2734                   insns = arm_gen_constant (AND, mode, cond,
2735                                             remainder | shift_mask,
2736                                             targ, source, subtargets, 0);
2737                 }
2738             }
2739
2740           if (generate)
2741             {
2742               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2743               rtx shift = GEN_INT (clear_sign_bit_copies);
2744
2745               emit_insn (gen_ashlsi3 (new_src, source, shift));
2746               emit_insn (gen_lshrsi3 (target, new_src, shift));
2747             }
2748
2749           return insns + 2;
2750         }
2751
2752       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2753         {
2754           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2755
2756           if ((remainder | shift_mask) != 0xffffffff)
2757             {
2758               if (generate)
2759                 {
2760                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2761
2762                   insns = arm_gen_constant (AND, mode, cond,
2763                                             remainder | shift_mask,
2764                                             new_src, source, subtargets, 1);
2765                   source = new_src;
2766                 }
2767               else
2768                 {
2769                   rtx targ = subtargets ? NULL_RTX : target;
2770
2771                   insns = arm_gen_constant (AND, mode, cond,
2772                                             remainder | shift_mask,
2773                                             targ, source, subtargets, 0);
2774                 }
2775             }
2776
2777           if (generate)
2778             {
2779               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2780               rtx shift = GEN_INT (clear_zero_bit_copies);
2781
2782               emit_insn (gen_lshrsi3 (new_src, source, shift));
2783               emit_insn (gen_ashlsi3 (target, new_src, shift));
2784             }
2785
2786           return insns + 2;
2787         }
2788
2789       break;
2790
2791     default:
2792       break;
2793     }
2794
2795   for (i = 0; i < 32; i++)
2796     if (remainder & (1 << i))
2797       num_bits_set++;
2798
2799   if ((code == AND)
2800       || (code != IOR && can_invert && num_bits_set > 16))
2801     remainder = (~remainder) & 0xffffffff;
2802   else if (code == PLUS && num_bits_set > 16)
2803     remainder = (-remainder) & 0xffffffff;
2804   else
2805     {
2806       can_invert = 0;
2807       can_negate = 0;
2808     }
2809
2810   /* Now try and find a way of doing the job in either two or three
2811      instructions.
2812      We start by looking for the largest block of zeros that are aligned on
2813      a 2-bit boundary, we then fill up the temps, wrapping around to the
2814      top of the word when we drop off the bottom.
2815      In the worst case this code should produce no more than four insns.
2816      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2817      best place to start.  */
2818
2819   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2820      the same.  */
2821   {
2822     int best_start = 0;
2823     if (!TARGET_THUMB2)
2824       {
2825         int best_consecutive_zeros = 0;
2826
2827         for (i = 0; i < 32; i += 2)
2828           {
2829             int consecutive_zeros = 0;
2830
2831             if (!(remainder & (3 << i)))
2832               {
2833                 while ((i < 32) && !(remainder & (3 << i)))
2834                   {
2835                     consecutive_zeros += 2;
2836                     i += 2;
2837                   }
2838                 if (consecutive_zeros > best_consecutive_zeros)
2839                   {
2840                     best_consecutive_zeros = consecutive_zeros;
2841                     best_start = i - consecutive_zeros;
2842                   }
2843                 i -= 2;
2844               }
2845           }
2846
2847         /* So long as it won't require any more insns to do so, it's
2848            desirable to emit a small constant (in bits 0...9) in the last
2849            insn.  This way there is more chance that it can be combined with
2850            a later addressing insn to form a pre-indexed load or store
2851            operation.  Consider:
2852
2853                    *((volatile int *)0xe0000100) = 1;
2854                    *((volatile int *)0xe0000110) = 2;
2855
2856            We want this to wind up as:
2857
2858                     mov rA, #0xe0000000
2859                     mov rB, #1
2860                     str rB, [rA, #0x100]
2861                     mov rB, #2
2862                     str rB, [rA, #0x110]
2863
2864            rather than having to synthesize both large constants from scratch.
2865
2866            Therefore, we calculate how many insns would be required to emit
2867            the constant starting from `best_start', and also starting from
2868            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2869            yield a shorter sequence, we may as well use zero.  */
2870         if (best_start != 0
2871             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2872             && (count_insns_for_constant (remainder, 0) <=
2873                 count_insns_for_constant (remainder, best_start)))
2874           best_start = 0;
2875       }
2876
2877     /* Now start emitting the insns.  */
2878     i = best_start;
2879     do
2880       {
2881         int end;
2882
2883         if (i <= 0)
2884           i += 32;
2885         if (remainder & (3 << (i - 2)))
2886           {
2887             end = i - 8;
2888             if (end < 0)
2889               end += 32;
2890             temp1 = remainder & ((0x0ff << end)
2891                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2892             remainder &= ~temp1;
2893
2894             if (generate)
2895               {
2896                 rtx new_src, temp1_rtx;
2897
2898                 if (code == SET || code == MINUS)
2899                   {
2900                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2901                     if (can_invert && code != MINUS)
2902                       temp1 = ~temp1;
2903                   }
2904                 else
2905                   {
2906                     if (remainder && subtargets)
2907                       new_src = gen_reg_rtx (mode);
2908                     else
2909                       new_src = target;
2910                     if (can_invert)
2911                       temp1 = ~temp1;
2912                     else if (can_negate)
2913                       temp1 = -temp1;
2914                   }
2915
2916                 temp1 = trunc_int_for_mode (temp1, mode);
2917                 temp1_rtx = GEN_INT (temp1);
2918
2919                 if (code == SET)
2920                   ;
2921                 else if (code == MINUS)
2922                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2923                 else
2924                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2925
2926                 emit_constant_insn (cond,
2927                                     gen_rtx_SET (VOIDmode, new_src,
2928                                                  temp1_rtx));
2929                 source = new_src;
2930               }
2931
2932             if (code == SET)
2933               {
2934                 can_invert = 0;
2935                 code = PLUS;
2936               }
2937             else if (code == MINUS)
2938               code = PLUS;
2939
2940             insns++;
2941             if (TARGET_ARM)
2942               i -= 6;
2943             else
2944               i -= 7;
2945           }
2946         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2947            shifts.  */
2948         if (TARGET_ARM)
2949           i -= 2;
2950         else
2951           i--;
2952       }
2953     while (remainder);
2954   }
2955
2956   return insns;
2957 }
2958
2959 /* Canonicalize a comparison so that we are more likely to recognize it.
2960    This can be done for a few constant compares, where we can make the
2961    immediate value easier to load.  */
2962
2963 enum rtx_code
2964 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2965                              rtx * op1)
2966 {
2967   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2968   unsigned HOST_WIDE_INT maxval;
2969   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2970
2971   switch (code)
2972     {
2973     case EQ:
2974     case NE:
2975       return code;
2976
2977     case GT:
2978     case LE:
2979       if (i != maxval
2980           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2981         {
2982           *op1 = GEN_INT (i + 1);
2983           return code == GT ? GE : LT;
2984         }
2985       break;
2986
2987     case GE:
2988     case LT:
2989       if (i != ~maxval
2990           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2991         {
2992           *op1 = GEN_INT (i - 1);
2993           return code == GE ? GT : LE;
2994         }
2995       break;
2996
2997     case GTU:
2998     case LEU:
2999       if (i != ~((unsigned HOST_WIDE_INT) 0)
3000           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
3001         {
3002           *op1 = GEN_INT (i + 1);
3003           return code == GTU ? GEU : LTU;
3004         }
3005       break;
3006
3007     case GEU:
3008     case LTU:
3009       if (i != 0
3010           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
3011         {
3012           *op1 = GEN_INT (i - 1);
3013           return code == GEU ? GTU : LEU;
3014         }
3015       break;
3016
3017     default:
3018       gcc_unreachable ();
3019     }
3020
3021   return code;
3022 }
3023
3024
3025 /* Define how to find the value returned by a function.  */
3026
3027 rtx
3028 arm_function_value(const_tree type, const_tree func ATTRIBUTE_UNUSED)
3029 {
3030   enum machine_mode mode;
3031   int unsignedp ATTRIBUTE_UNUSED;
3032   rtx r ATTRIBUTE_UNUSED;
3033
3034   mode = TYPE_MODE (type);
3035   /* Promote integer types.  */
3036   if (INTEGRAL_TYPE_P (type))
3037     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
3038
3039   /* Promotes small structs returned in a register to full-word size
3040      for big-endian AAPCS.  */
3041   if (arm_return_in_msb (type))
3042     {
3043       HOST_WIDE_INT size = int_size_in_bytes (type);
3044       if (size % UNITS_PER_WORD != 0)
3045         {
3046           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
3047           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
3048         }
3049     }
3050
3051   return LIBCALL_VALUE(mode);
3052 }
3053
3054 /* Determine the amount of memory needed to store the possible return
3055    registers of an untyped call.  */
3056 int
3057 arm_apply_result_size (void)
3058 {
3059   int size = 16;
3060
3061   if (TARGET_ARM)
3062     {
3063       if (TARGET_HARD_FLOAT_ABI)
3064         {
3065           if (TARGET_FPA)
3066             size += 12;
3067           if (TARGET_MAVERICK)
3068             size += 8;
3069         }
3070       if (TARGET_IWMMXT_ABI)
3071         size += 8;
3072     }
3073
3074   return size;
3075 }
3076
3077 /* Decide whether a type should be returned in memory (true)
3078    or in a register (false).  This is called as the target hook
3079    TARGET_RETURN_IN_MEMORY.  */
3080 static bool
3081 arm_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
3082 {
3083   HOST_WIDE_INT size;
3084
3085   size = int_size_in_bytes (type);
3086
3087   /* Vector values should be returned using ARM registers, not memory (unless
3088      they're over 16 bytes, which will break since we only have four
3089      call-clobbered registers to play with).  */
3090   if (TREE_CODE (type) == VECTOR_TYPE)
3091     return (size < 0 || size > (4 * UNITS_PER_WORD));
3092
3093   if (!AGGREGATE_TYPE_P (type) &&
3094       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
3095     /* All simple types are returned in registers.
3096        For AAPCS, complex types are treated the same as aggregates.  */
3097     return 0;
3098
3099   if (arm_abi != ARM_ABI_APCS)
3100     {
3101       /* ATPCS and later return aggregate types in memory only if they are
3102          larger than a word (or are variable size).  */
3103       return (size < 0 || size > UNITS_PER_WORD);
3104     }
3105
3106   /* For the arm-wince targets we choose to be compatible with Microsoft's
3107      ARM and Thumb compilers, which always return aggregates in memory.  */
3108 #ifndef ARM_WINCE
3109   /* All structures/unions bigger than one word are returned in memory.
3110      Also catch the case where int_size_in_bytes returns -1.  In this case
3111      the aggregate is either huge or of variable size, and in either case
3112      we will want to return it via memory and not in a register.  */
3113   if (size < 0 || size > UNITS_PER_WORD)
3114     return 1;
3115
3116   if (TREE_CODE (type) == RECORD_TYPE)
3117     {
3118       tree field;
3119
3120       /* For a struct the APCS says that we only return in a register
3121          if the type is 'integer like' and every addressable element
3122          has an offset of zero.  For practical purposes this means
3123          that the structure can have at most one non bit-field element
3124          and that this element must be the first one in the structure.  */
3125
3126       /* Find the first field, ignoring non FIELD_DECL things which will
3127          have been created by C++.  */
3128       for (field = TYPE_FIELDS (type);
3129            field && TREE_CODE (field) != FIELD_DECL;
3130            field = TREE_CHAIN (field))
3131         continue;
3132
3133       if (field == NULL)
3134         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
3135
3136       /* Check that the first field is valid for returning in a register.  */
3137
3138       /* ... Floats are not allowed */
3139       if (FLOAT_TYPE_P (TREE_TYPE (field)))
3140         return 1;
3141
3142       /* ... Aggregates that are not themselves valid for returning in
3143          a register are not allowed.  */
3144       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3145         return 1;
3146
3147       /* Now check the remaining fields, if any.  Only bitfields are allowed,
3148          since they are not addressable.  */
3149       for (field = TREE_CHAIN (field);
3150            field;
3151            field = TREE_CHAIN (field))
3152         {
3153           if (TREE_CODE (field) != FIELD_DECL)
3154             continue;
3155
3156           if (!DECL_BIT_FIELD_TYPE (field))
3157             return 1;
3158         }
3159
3160       return 0;
3161     }
3162
3163   if (TREE_CODE (type) == UNION_TYPE)
3164     {
3165       tree field;
3166
3167       /* Unions can be returned in registers if every element is
3168          integral, or can be returned in an integer register.  */
3169       for (field = TYPE_FIELDS (type);
3170            field;
3171            field = TREE_CHAIN (field))
3172         {
3173           if (TREE_CODE (field) != FIELD_DECL)
3174             continue;
3175
3176           if (FLOAT_TYPE_P (TREE_TYPE (field)))
3177             return 1;
3178
3179           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
3180             return 1;
3181         }
3182
3183       return 0;
3184     }
3185 #endif /* not ARM_WINCE */
3186
3187   /* Return all other types in memory.  */
3188   return 1;
3189 }
3190
3191 /* Indicate whether or not words of a double are in big-endian order.  */
3192
3193 int
3194 arm_float_words_big_endian (void)
3195 {
3196   if (TARGET_MAVERICK)
3197     return 0;
3198
3199   /* For FPA, float words are always big-endian.  For VFP, floats words
3200      follow the memory system mode.  */
3201
3202   if (TARGET_FPA)
3203     {
3204       return 1;
3205     }
3206
3207   if (TARGET_VFP)
3208     return (TARGET_BIG_END ? 1 : 0);
3209
3210   return 1;
3211 }
3212
3213 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3214    for a call to a function whose data type is FNTYPE.
3215    For a library call, FNTYPE is NULL.  */
3216 void
3217 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
3218                           rtx libname  ATTRIBUTE_UNUSED,
3219                           tree fndecl ATTRIBUTE_UNUSED)
3220 {
3221   /* On the ARM, the offset starts at 0.  */
3222   pcum->nregs = 0;
3223   pcum->iwmmxt_nregs = 0;
3224   pcum->can_split = true;
3225
3226   /* Varargs vectors are treated the same as long long.
3227      named_count avoids having to change the way arm handles 'named' */
3228   pcum->named_count = 0;
3229   pcum->nargs = 0;
3230
3231   if (TARGET_REALLY_IWMMXT && fntype)
3232     {
3233       tree fn_arg;
3234
3235       for (fn_arg = TYPE_ARG_TYPES (fntype);
3236            fn_arg;
3237            fn_arg = TREE_CHAIN (fn_arg))
3238         pcum->named_count += 1;
3239
3240       if (! pcum->named_count)
3241         pcum->named_count = INT_MAX;
3242     }
3243 }
3244
3245
3246 /* Return true if mode/type need doubleword alignment.  */
3247 bool
3248 arm_needs_doubleword_align (enum machine_mode mode, tree type)
3249 {
3250   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
3251           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
3252 }
3253
3254
3255 /* Determine where to put an argument to a function.
3256    Value is zero to push the argument on the stack,
3257    or a hard register in which to store the argument.
3258
3259    MODE is the argument's machine mode.
3260    TYPE is the data type of the argument (as a tree).
3261     This is null for libcalls where that information may
3262     not be available.
3263    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3264     the preceding args and about the function being called.
3265    NAMED is nonzero if this argument is a named parameter
3266     (otherwise it is an extra parameter matching an ellipsis).  */
3267
3268 rtx
3269 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3270                   tree type, int named)
3271 {
3272   int nregs;
3273
3274   /* Varargs vectors are treated the same as long long.
3275      named_count avoids having to change the way arm handles 'named' */
3276   if (TARGET_IWMMXT_ABI
3277       && arm_vector_mode_supported_p (mode)
3278       && pcum->named_count > pcum->nargs + 1)
3279     {
3280       if (pcum->iwmmxt_nregs <= 9)
3281         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
3282       else
3283         {
3284           pcum->can_split = false;
3285           return NULL_RTX;
3286         }
3287     }
3288
3289   /* Put doubleword aligned quantities in even register pairs.  */
3290   if (pcum->nregs & 1
3291       && ARM_DOUBLEWORD_ALIGN
3292       && arm_needs_doubleword_align (mode, type))
3293     pcum->nregs++;
3294
3295   if (mode == VOIDmode)
3296     /* Pick an arbitrary value for operand 2 of the call insn.  */
3297     return const0_rtx;
3298
3299   /* Only allow splitting an arg between regs and memory if all preceding
3300      args were allocated to regs.  For args passed by reference we only count
3301      the reference pointer.  */
3302   if (pcum->can_split)
3303     nregs = 1;
3304   else
3305     nregs = ARM_NUM_REGS2 (mode, type);
3306
3307   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
3308     return NULL_RTX;
3309
3310   return gen_rtx_REG (mode, pcum->nregs);
3311 }
3312
3313 static int
3314 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3315                        tree type, bool named ATTRIBUTE_UNUSED)
3316 {
3317   int nregs = pcum->nregs;
3318
3319   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
3320     return 0;
3321
3322   if (NUM_ARG_REGS > nregs
3323       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
3324       && pcum->can_split)
3325     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
3326
3327   return 0;
3328 }
3329
3330 /* Variable sized types are passed by reference.  This is a GCC
3331    extension to the ARM ABI.  */
3332
3333 static bool
3334 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3335                        enum machine_mode mode ATTRIBUTE_UNUSED,
3336                        const_tree type, bool named ATTRIBUTE_UNUSED)
3337 {
3338   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3339 }
3340 \f
3341 /* Encode the current state of the #pragma [no_]long_calls.  */
3342 typedef enum
3343 {
3344   OFF,          /* No #pragma [no_]long_calls is in effect.  */
3345   LONG,         /* #pragma long_calls is in effect.  */
3346   SHORT         /* #pragma no_long_calls is in effect.  */
3347 } arm_pragma_enum;
3348
3349 static arm_pragma_enum arm_pragma_long_calls = OFF;
3350
3351 void
3352 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3353 {
3354   arm_pragma_long_calls = LONG;
3355 }
3356
3357 void
3358 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3359 {
3360   arm_pragma_long_calls = SHORT;
3361 }
3362
3363 void
3364 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3365 {
3366   arm_pragma_long_calls = OFF;
3367 }
3368 \f
3369 /* Table of machine attributes.  */
3370 const struct attribute_spec arm_attribute_table[] =
3371 {
3372   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3373   /* Function calls made to this symbol must be done indirectly, because
3374      it may lie outside of the 26 bit addressing range of a normal function
3375      call.  */
3376   { "long_call",    0, 0, false, true,  true,  NULL },
3377   /* Whereas these functions are always known to reside within the 26 bit
3378      addressing range.  */
3379   { "short_call",   0, 0, false, true,  true,  NULL },
3380   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
3381   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
3382   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
3383   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
3384 #ifdef ARM_PE
3385   /* ARM/PE has three new attributes:
3386      interfacearm - ?
3387      dllexport - for exporting a function/variable that will live in a dll
3388      dllimport - for importing a function/variable from a dll
3389
3390      Microsoft allows multiple declspecs in one __declspec, separating
3391      them with spaces.  We do NOT support this.  Instead, use __declspec
3392      multiple times.
3393   */
3394   { "dllimport",    0, 0, true,  false, false, NULL },
3395   { "dllexport",    0, 0, true,  false, false, NULL },
3396   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
3397 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3398   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
3399   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
3400   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
3401 #endif
3402   { NULL,           0, 0, false, false, false, NULL }
3403 };
3404
3405 /* Handle an attribute requiring a FUNCTION_DECL;
3406    arguments as in struct attribute_spec.handler.  */
3407 static tree
3408 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3409                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3410 {
3411   if (TREE_CODE (*node) != FUNCTION_DECL)
3412     {
3413       warning (OPT_Wattributes, "%qE attribute only applies to functions",
3414                name);
3415       *no_add_attrs = true;
3416     }
3417
3418   return NULL_TREE;
3419 }
3420
3421 /* Handle an "interrupt" or "isr" attribute;
3422    arguments as in struct attribute_spec.handler.  */
3423 static tree
3424 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3425                           bool *no_add_attrs)
3426 {
3427   if (DECL_P (*node))
3428     {
3429       if (TREE_CODE (*node) != FUNCTION_DECL)
3430         {
3431           warning (OPT_Wattributes, "%qE attribute only applies to functions",
3432                    name);
3433           *no_add_attrs = true;
3434         }
3435       /* FIXME: the argument if any is checked for type attributes;
3436          should it be checked for decl ones?  */
3437     }
3438   else
3439     {
3440       if (TREE_CODE (*node) == FUNCTION_TYPE
3441           || TREE_CODE (*node) == METHOD_TYPE)
3442         {
3443           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3444             {
3445               warning (OPT_Wattributes, "%qE attribute ignored",
3446                        name);
3447               *no_add_attrs = true;
3448             }
3449         }
3450       else if (TREE_CODE (*node) == POINTER_TYPE
3451                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3452                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3453                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3454         {
3455           *node = build_variant_type_copy (*node);
3456           TREE_TYPE (*node) = build_type_attribute_variant
3457             (TREE_TYPE (*node),
3458              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3459           *no_add_attrs = true;
3460         }
3461       else
3462         {
3463           /* Possibly pass this attribute on from the type to a decl.  */
3464           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3465                        | (int) ATTR_FLAG_FUNCTION_NEXT
3466                        | (int) ATTR_FLAG_ARRAY_NEXT))
3467             {
3468               *no_add_attrs = true;
3469               return tree_cons (name, args, NULL_TREE);
3470             }
3471           else
3472             {
3473               warning (OPT_Wattributes, "%qE attribute ignored",
3474                        name);
3475             }
3476         }
3477     }
3478
3479   return NULL_TREE;
3480 }
3481
3482 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3483 /* Handle the "notshared" attribute.  This attribute is another way of
3484    requesting hidden visibility.  ARM's compiler supports
3485    "__declspec(notshared)"; we support the same thing via an
3486    attribute.  */
3487
3488 static tree
3489 arm_handle_notshared_attribute (tree *node,
3490                                 tree name ATTRIBUTE_UNUSED,
3491                                 tree args ATTRIBUTE_UNUSED,
3492                                 int flags ATTRIBUTE_UNUSED,
3493                                 bool *no_add_attrs)
3494 {
3495   tree decl = TYPE_NAME (*node);
3496
3497   if (decl)
3498     {
3499       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3500       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3501       *no_add_attrs = false;
3502     }
3503   return NULL_TREE;
3504 }
3505 #endif
3506
3507 /* Return 0 if the attributes for two types are incompatible, 1 if they
3508    are compatible, and 2 if they are nearly compatible (which causes a
3509    warning to be generated).  */
3510 static int
3511 arm_comp_type_attributes (const_tree type1, const_tree type2)
3512 {
3513   int l1, l2, s1, s2;
3514
3515   /* Check for mismatch of non-default calling convention.  */
3516   if (TREE_CODE (type1) != FUNCTION_TYPE)
3517     return 1;
3518
3519   /* Check for mismatched call attributes.  */
3520   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3521   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3522   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3523   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3524
3525   /* Only bother to check if an attribute is defined.  */
3526   if (l1 | l2 | s1 | s2)
3527     {
3528       /* If one type has an attribute, the other must have the same attribute.  */
3529       if ((l1 != l2) || (s1 != s2))
3530         return 0;
3531
3532       /* Disallow mixed attributes.  */
3533       if ((l1 & s2) || (l2 & s1))
3534         return 0;
3535     }
3536
3537   /* Check for mismatched ISR attribute.  */
3538   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3539   if (! l1)
3540     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3541   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3542   if (! l2)
3543     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3544   if (l1 != l2)
3545     return 0;
3546
3547   return 1;
3548 }
3549
3550 /*  Assigns default attributes to newly defined type.  This is used to
3551     set short_call/long_call attributes for function types of
3552     functions defined inside corresponding #pragma scopes.  */
3553 static void
3554 arm_set_default_type_attributes (tree type)
3555 {
3556   /* Add __attribute__ ((long_call)) to all functions, when
3557      inside #pragma long_calls or __attribute__ ((short_call)),
3558      when inside #pragma no_long_calls.  */
3559   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3560     {
3561       tree type_attr_list, attr_name;
3562       type_attr_list = TYPE_ATTRIBUTES (type);
3563
3564       if (arm_pragma_long_calls == LONG)
3565         attr_name = get_identifier ("long_call");
3566       else if (arm_pragma_long_calls == SHORT)
3567         attr_name = get_identifier ("short_call");
3568       else
3569         return;
3570
3571       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3572       TYPE_ATTRIBUTES (type) = type_attr_list;
3573     }
3574 }
3575 \f
3576 /* Return true if DECL is known to be linked into section SECTION.  */
3577
3578 static bool
3579 arm_function_in_section_p (tree decl, section *section)
3580 {
3581   /* We can only be certain about functions defined in the same
3582      compilation unit.  */
3583   if (!TREE_STATIC (decl))
3584     return false;
3585
3586   /* Make sure that SYMBOL always binds to the definition in this
3587      compilation unit.  */
3588   if (!targetm.binds_local_p (decl))
3589     return false;
3590
3591   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3592   if (!DECL_SECTION_NAME (decl))
3593     {
3594       /* Make sure that we will not create a unique section for DECL.  */
3595       if (flag_function_sections || DECL_ONE_ONLY (decl))
3596         return false;
3597     }
3598
3599   return function_section (decl) == section;
3600 }
3601
3602 /* Return nonzero if a 32-bit "long_call" should be generated for
3603    a call from the current function to DECL.  We generate a long_call
3604    if the function:
3605
3606         a.  has an __attribute__((long call))
3607      or b.  is within the scope of a #pragma long_calls
3608      or c.  the -mlong-calls command line switch has been specified
3609
3610    However we do not generate a long call if the function:
3611
3612         d.  has an __attribute__ ((short_call))
3613      or e.  is inside the scope of a #pragma no_long_calls
3614      or f.  is defined in the same section as the current function.  */
3615
3616 bool
3617 arm_is_long_call_p (tree decl)
3618 {
3619   tree attrs;
3620
3621   if (!decl)
3622     return TARGET_LONG_CALLS;
3623
3624   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3625   if (lookup_attribute ("short_call", attrs))
3626     return false;
3627
3628   /* For "f", be conservative, and only cater for cases in which the
3629      whole of the current function is placed in the same section.  */
3630   if (!flag_reorder_blocks_and_partition
3631       && TREE_CODE (decl) == FUNCTION_DECL
3632       && arm_function_in_section_p (decl, current_function_section ()))
3633     return false;
3634
3635   if (lookup_attribute ("long_call", attrs))
3636     return true;
3637
3638   return TARGET_LONG_CALLS;
3639 }
3640
3641 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3642 static bool
3643 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3644 {
3645   unsigned long func_type;
3646
3647   if (cfun->machine->sibcall_blocked)
3648     return false;
3649
3650   /* Never tailcall something for which we have no decl, or if we
3651      are in Thumb mode.  */
3652   if (decl == NULL || TARGET_THUMB)
3653     return false;
3654
3655   /* The PIC register is live on entry to VxWorks PLT entries, so we
3656      must make the call before restoring the PIC register.  */
3657   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3658     return false;
3659
3660   /* Cannot tail-call to long calls, since these are out of range of
3661      a branch instruction.  */
3662   if (arm_is_long_call_p (decl))
3663     return false;
3664
3665   /* If we are interworking and the function is not declared static
3666      then we can't tail-call it unless we know that it exists in this
3667      compilation unit (since it might be a Thumb routine).  */
3668   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3669     return false;
3670
3671   func_type = arm_current_func_type ();
3672   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3673   if (IS_INTERRUPT (func_type))
3674     return false;
3675
3676   /* Never tailcall if function may be called with a misaligned SP.  */
3677   if (IS_STACKALIGN (func_type))
3678     return false;
3679
3680   /* Everything else is ok.  */
3681   return true;
3682 }
3683
3684 \f
3685 /* Addressing mode support functions.  */
3686
3687 /* Return nonzero if X is a legitimate immediate operand when compiling
3688    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3689 int
3690 legitimate_pic_operand_p (rtx x)
3691 {
3692   if (GET_CODE (x) == SYMBOL_REF
3693       || (GET_CODE (x) == CONST
3694           && GET_CODE (XEXP (x, 0)) == PLUS
3695           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3696     return 0;
3697
3698   return 1;
3699 }
3700
3701 /* Record that the current function needs a PIC register.  Initialize
3702    cfun->machine->pic_reg if we have not already done so.  */
3703
3704 static void
3705 require_pic_register (void)
3706 {
3707   /* A lot of the logic here is made obscure by the fact that this
3708      routine gets called as part of the rtx cost estimation process.
3709      We don't want those calls to affect any assumptions about the real
3710      function; and further, we can't call entry_of_function() until we
3711      start the real expansion process.  */
3712   if (!crtl->uses_pic_offset_table)
3713     {
3714       gcc_assert (can_create_pseudo_p ());
3715       if (arm_pic_register != INVALID_REGNUM)
3716         {
3717           if (!cfun->machine->pic_reg)
3718             cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3719
3720           /* Play games to avoid marking the function as needing pic
3721              if we are being called as part of the cost-estimation
3722              process.  */
3723           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
3724             crtl->uses_pic_offset_table = 1;
3725         }
3726       else
3727         {
3728           rtx seq;
3729
3730           if (!cfun->machine->pic_reg)
3731             cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3732
3733           /* Play games to avoid marking the function as needing pic
3734              if we are being called as part of the cost-estimation
3735              process.  */
3736           if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
3737             {
3738               crtl->uses_pic_offset_table = 1;
3739               start_sequence ();
3740
3741               arm_load_pic_register (0UL);
3742
3743               seq = get_insns ();
3744               end_sequence ();
3745               /* We can be called during expansion of PHI nodes, where
3746                  we can't yet emit instructions directly in the final
3747                  insn stream.  Queue the insns on the entry edge, they will
3748                  be committed after everything else is expanded.  */
3749               insert_insn_on_edge (seq, single_succ_edge (ENTRY_BLOCK_PTR));
3750             }
3751         }
3752     }
3753 }
3754
3755 rtx
3756 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3757 {
3758   if (GET_CODE (orig) == SYMBOL_REF
3759       || GET_CODE (orig) == LABEL_REF)
3760     {
3761       rtx pic_ref, address;
3762       rtx insn;
3763       int subregs = 0;
3764
3765       /* If this function doesn't have a pic register, create one now.  */
3766       require_pic_register ();
3767
3768       if (reg == 0)
3769         {
3770           gcc_assert (can_create_pseudo_p ());
3771           reg = gen_reg_rtx (Pmode);
3772
3773           subregs = 1;
3774         }
3775
3776       if (subregs)
3777         address = gen_reg_rtx (Pmode);
3778       else
3779         address = reg;
3780
3781       if (TARGET_ARM)
3782         emit_insn (gen_pic_load_addr_arm (address, orig));
3783       else if (TARGET_THUMB2)
3784         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3785       else /* TARGET_THUMB1 */
3786         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3787
3788       /* VxWorks does not impose a fixed gap between segments; the run-time
3789          gap can be different from the object-file gap.  We therefore can't
3790          use GOTOFF unless we are absolutely sure that the symbol is in the
3791          same segment as the GOT.  Unfortunately, the flexibility of linker
3792          scripts means that we can't be sure of that in general, so assume
3793          that GOTOFF is never valid on VxWorks.  */
3794       if ((GET_CODE (orig) == LABEL_REF
3795            || (GET_CODE (orig) == SYMBOL_REF &&
3796                SYMBOL_REF_LOCAL_P (orig)))
3797           && NEED_GOT_RELOC
3798           && !TARGET_VXWORKS_RTP)
3799         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3800       else
3801         {
3802           pic_ref = gen_const_mem (Pmode,
3803                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3804                                                  address));
3805         }
3806
3807       insn = emit_move_insn (reg, pic_ref);
3808
3809       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3810          by loop.  */
3811       set_unique_reg_note (insn, REG_EQUAL, orig);
3812
3813       return reg;
3814     }
3815   else if (GET_CODE (orig) == CONST)
3816     {
3817       rtx base, offset;
3818
3819       if (GET_CODE (XEXP (orig, 0)) == PLUS
3820           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3821         return orig;
3822
3823       /* Handle the case where we have: const (UNSPEC_TLS).  */
3824       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3825           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3826         return orig;
3827
3828       /* Handle the case where we have:
3829          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
3830          CONST_INT.  */
3831       if (GET_CODE (XEXP (orig, 0)) == PLUS
3832           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
3833           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
3834         {
3835           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
3836           return orig;
3837         }
3838
3839       if (reg == 0)
3840         {
3841           gcc_assert (can_create_pseudo_p ());
3842           reg = gen_reg_rtx (Pmode);
3843         }
3844
3845       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3846
3847       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3848       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3849                                        base == reg ? 0 : reg);
3850
3851       if (GET_CODE (offset) == CONST_INT)
3852         {
3853           /* The base register doesn't really matter, we only want to
3854              test the index for the appropriate mode.  */
3855           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3856             {
3857               gcc_assert (can_create_pseudo_p ());
3858               offset = force_reg (Pmode, offset);
3859             }
3860
3861           if (GET_CODE (offset) == CONST_INT)
3862             return plus_constant (base, INTVAL (offset));
3863         }
3864
3865       if (GET_MODE_SIZE (mode) > 4
3866           && (GET_MODE_CLASS (mode) == MODE_INT
3867               || TARGET_SOFT_FLOAT))
3868         {
3869           emit_insn (gen_addsi3 (reg, base, offset));
3870           return reg;
3871         }
3872
3873       return gen_rtx_PLUS (Pmode, base, offset);
3874     }
3875
3876   return orig;
3877 }
3878
3879
3880 /* Find a spare register to use during the prolog of a function.  */
3881
3882 static int
3883 thumb_find_work_register (unsigned long pushed_regs_mask)
3884 {
3885   int reg;
3886
3887   /* Check the argument registers first as these are call-used.  The
3888      register allocation order means that sometimes r3 might be used
3889      but earlier argument registers might not, so check them all.  */
3890   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3891     if (!df_regs_ever_live_p (reg))
3892       return reg;
3893
3894   /* Before going on to check the call-saved registers we can try a couple
3895      more ways of deducing that r3 is available.  The first is when we are
3896      pushing anonymous arguments onto the stack and we have less than 4
3897      registers worth of fixed arguments(*).  In this case r3 will be part of
3898      the variable argument list and so we can be sure that it will be
3899      pushed right at the start of the function.  Hence it will be available
3900      for the rest of the prologue.
3901      (*): ie crtl->args.pretend_args_size is greater than 0.  */
3902   if (cfun->machine->uses_anonymous_args
3903       && crtl->args.pretend_args_size > 0)
3904     return LAST_ARG_REGNUM;
3905
3906   /* The other case is when we have fixed arguments but less than 4 registers
3907      worth.  In this case r3 might be used in the body of the function, but
3908      it is not being used to convey an argument into the function.  In theory
3909      we could just check crtl->args.size to see how many bytes are
3910      being passed in argument registers, but it seems that it is unreliable.
3911      Sometimes it will have the value 0 when in fact arguments are being
3912      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3913      check the args_info.nregs field as well.  The problem with this field is
3914      that it makes no allowances for arguments that are passed to the
3915      function but which are not used.  Hence we could miss an opportunity
3916      when a function has an unused argument in r3.  But it is better to be
3917      safe than to be sorry.  */
3918   if (! cfun->machine->uses_anonymous_args
3919       && crtl->args.size >= 0
3920       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3921       && crtl->args.info.nregs < 4)
3922     return LAST_ARG_REGNUM;
3923
3924   /* Otherwise look for a call-saved register that is going to be pushed.  */
3925   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3926     if (pushed_regs_mask & (1 << reg))
3927       return reg;
3928
3929   if (TARGET_THUMB2)
3930     {
3931       /* Thumb-2 can use high regs.  */
3932       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3933         if (pushed_regs_mask & (1 << reg))
3934           return reg;
3935     }
3936   /* Something went wrong - thumb_compute_save_reg_mask()
3937      should have arranged for a suitable register to be pushed.  */
3938   gcc_unreachable ();
3939 }
3940
3941 static GTY(()) int pic_labelno;
3942
3943 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3944    low register.  */
3945
3946 void
3947 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3948 {
3949   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
3950
3951   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3952     return;
3953
3954   gcc_assert (flag_pic);
3955
3956   pic_reg = cfun->machine->pic_reg;
3957   if (TARGET_VXWORKS_RTP)
3958     {
3959       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3960       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3961       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3962
3963       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
3964
3965       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3966       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
3967     }
3968   else
3969     {
3970       /* We use an UNSPEC rather than a LABEL_REF because this label
3971          never appears in the code stream.  */
3972
3973       labelno = GEN_INT (pic_labelno++);
3974       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3975       l1 = gen_rtx_CONST (VOIDmode, l1);
3976
3977       /* On the ARM the PC register contains 'dot + 8' at the time of the
3978          addition, on the Thumb it is 'dot + 4'.  */
3979       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
3980       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
3981                                 UNSPEC_GOTSYM_OFF);
3982       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3983
3984       if (TARGET_ARM)
3985         {
3986           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3987           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
3988         }
3989       else if (TARGET_THUMB2)
3990         {
3991           /* Thumb-2 only allows very limited access to the PC.  Calculate the
3992              address in a temporary register.  */
3993           if (arm_pic_register != INVALID_REGNUM)
3994             {
3995               pic_tmp = gen_rtx_REG (SImode,
3996                                      thumb_find_work_register (saved_regs));
3997             }
3998           else
3999             {
4000               gcc_assert (can_create_pseudo_p ());
4001               pic_tmp = gen_reg_rtx (Pmode);
4002             }
4003
4004           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
4005           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
4006           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
4007         }
4008       else /* TARGET_THUMB1 */
4009         {
4010           if (arm_pic_register != INVALID_REGNUM
4011               && REGNO (pic_reg) > LAST_LO_REGNUM)
4012             {
4013               /* We will have pushed the pic register, so we should always be
4014                  able to find a work register.  */
4015               pic_tmp = gen_rtx_REG (SImode,
4016                                      thumb_find_work_register (saved_regs));
4017               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
4018               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
4019             }
4020           else
4021             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
4022           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
4023         }
4024     }
4025
4026   /* Need to emit this whether or not we obey regdecls,
4027      since setjmp/longjmp can cause life info to screw up.  */
4028   emit_use (pic_reg);
4029 }
4030
4031
4032 /* Return nonzero if X is valid as an ARM state addressing register.  */
4033 static int
4034 arm_address_register_rtx_p (rtx x, int strict_p)
4035 {
4036   int regno;
4037
4038   if (GET_CODE (x) != REG)
4039     return 0;
4040
4041   regno = REGNO (x);
4042
4043   if (strict_p)
4044     return ARM_REGNO_OK_FOR_BASE_P (regno);
4045
4046   return (regno <= LAST_ARM_REGNUM
4047           || regno >= FIRST_PSEUDO_REGISTER
4048           || regno == FRAME_POINTER_REGNUM
4049           || regno == ARG_POINTER_REGNUM);
4050 }
4051
4052 /* Return TRUE if this rtx is the difference of a symbol and a label,
4053    and will reduce to a PC-relative relocation in the object file.
4054    Expressions like this can be left alone when generating PIC, rather
4055    than forced through the GOT.  */
4056 static int
4057 pcrel_constant_p (rtx x)
4058 {
4059   if (GET_CODE (x) == MINUS)
4060     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
4061
4062   return FALSE;
4063 }
4064
4065 /* Return nonzero if X is a valid ARM state address operand.  */
4066 int
4067 arm_legitimate_address_outer_p (enum machine_mode mode, rtx x, RTX_CODE outer,
4068                                 int strict_p)
4069 {
4070   bool use_ldrd;
4071   enum rtx_code code = GET_CODE (x);
4072
4073   if (arm_address_register_rtx_p (x, strict_p))
4074     return 1;
4075
4076   use_ldrd = (TARGET_LDRD
4077               && (mode == DImode
4078                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
4079
4080   if (code == POST_INC || code == PRE_DEC
4081       || ((code == PRE_INC || code == POST_DEC)
4082           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
4083     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
4084
4085   else if ((code == POST_MODIFY || code == PRE_MODIFY)
4086            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
4087            && GET_CODE (XEXP (x, 1)) == PLUS
4088            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4089     {
4090       rtx addend = XEXP (XEXP (x, 1), 1);
4091
4092       /* Don't allow ldrd post increment by register because it's hard
4093          to fixup invalid register choices.  */
4094       if (use_ldrd
4095           && GET_CODE (x) == POST_MODIFY
4096           && GET_CODE (addend) == REG)
4097         return 0;
4098
4099       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
4100               && arm_legitimate_index_p (mode, addend, outer, strict_p));
4101     }
4102
4103   /* After reload constants split into minipools will have addresses
4104      from a LABEL_REF.  */
4105   else if (reload_completed
4106            && (code == LABEL_REF
4107                || (code == CONST
4108                    && GET_CODE (XEXP (x, 0)) == PLUS
4109                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4110                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4111     return 1;
4112
4113   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
4114     return 0;
4115
4116   else if (code == PLUS)
4117     {
4118       rtx xop0 = XEXP (x, 0);
4119       rtx xop1 = XEXP (x, 1);
4120
4121       return ((arm_address_register_rtx_p (xop0, strict_p)
4122                && GET_CODE(xop1) == CONST_INT
4123                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
4124               || (arm_address_register_rtx_p (xop1, strict_p)
4125                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
4126     }
4127
4128 #if 0
4129   /* Reload currently can't handle MINUS, so disable this for now */
4130   else if (GET_CODE (x) == MINUS)
4131     {
4132       rtx xop0 = XEXP (x, 0);
4133       rtx xop1 = XEXP (x, 1);
4134
4135       return (arm_address_register_rtx_p (xop0, strict_p)
4136               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
4137     }
4138 #endif
4139
4140   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4141            && code == SYMBOL_REF
4142            && CONSTANT_POOL_ADDRESS_P (x)
4143            && ! (flag_pic
4144                  && symbol_mentioned_p (get_pool_constant (x))
4145                  && ! pcrel_constant_p (get_pool_constant (x))))
4146     return 1;
4147
4148   return 0;
4149 }
4150
4151 /* Return nonzero if X is a valid Thumb-2 address operand.  */
4152 static int
4153 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4154 {
4155   bool use_ldrd;
4156   enum rtx_code code = GET_CODE (x);
4157   
4158   if (arm_address_register_rtx_p (x, strict_p))
4159     return 1;
4160
4161   use_ldrd = (TARGET_LDRD
4162               && (mode == DImode
4163                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
4164
4165   if (code == POST_INC || code == PRE_DEC
4166       || ((code == PRE_INC || code == POST_DEC)
4167           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
4168     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
4169
4170   else if ((code == POST_MODIFY || code == PRE_MODIFY)
4171            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
4172            && GET_CODE (XEXP (x, 1)) == PLUS
4173            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4174     {
4175       /* Thumb-2 only has autoincrement by constant.  */
4176       rtx addend = XEXP (XEXP (x, 1), 1);
4177       HOST_WIDE_INT offset;
4178
4179       if (GET_CODE (addend) != CONST_INT)
4180         return 0;
4181
4182       offset = INTVAL(addend);
4183       if (GET_MODE_SIZE (mode) <= 4)
4184         return (offset > -256 && offset < 256);
4185       
4186       return (use_ldrd && offset > -1024 && offset < 1024
4187               && (offset & 3) == 0);
4188     }
4189
4190   /* After reload constants split into minipools will have addresses
4191      from a LABEL_REF.  */
4192   else if (reload_completed
4193            && (code == LABEL_REF
4194                || (code == CONST
4195                    && GET_CODE (XEXP (x, 0)) == PLUS
4196                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4197                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4198     return 1;
4199
4200   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
4201     return 0;
4202
4203   else if (code == PLUS)
4204     {
4205       rtx xop0 = XEXP (x, 0);
4206       rtx xop1 = XEXP (x, 1);
4207
4208       return ((arm_address_register_rtx_p (xop0, strict_p)
4209                && thumb2_legitimate_index_p (mode, xop1, strict_p))
4210               || (arm_address_register_rtx_p (xop1, strict_p)
4211                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
4212     }
4213
4214   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4215            && code == SYMBOL_REF
4216            && CONSTANT_POOL_ADDRESS_P (x)
4217            && ! (flag_pic
4218                  && symbol_mentioned_p (get_pool_constant (x))
4219                  && ! pcrel_constant_p (get_pool_constant (x))))
4220     return 1;
4221
4222   return 0;
4223 }
4224
4225 /* Return nonzero if INDEX is valid for an address index operand in
4226    ARM state.  */
4227 static int
4228 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
4229                         int strict_p)
4230 {
4231   HOST_WIDE_INT range;
4232   enum rtx_code code = GET_CODE (index);
4233
4234   /* Standard coprocessor addressing modes.  */
4235   if (TARGET_HARD_FLOAT
4236       && (TARGET_FPA || TARGET_MAVERICK)
4237       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4238           || (TARGET_MAVERICK && mode == DImode)))
4239     return (code == CONST_INT && INTVAL (index) < 1024
4240             && INTVAL (index) > -1024
4241             && (INTVAL (index) & 3) == 0);
4242
4243   if (TARGET_NEON
4244       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4245     return (code == CONST_INT
4246             && INTVAL (index) < 1016
4247             && INTVAL (index) > -1024
4248             && (INTVAL (index) & 3) == 0);
4249
4250   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4251     return (code == CONST_INT
4252             && INTVAL (index) < 1024
4253             && INTVAL (index) > -1024
4254             && (INTVAL (index) & 3) == 0);
4255
4256   if (arm_address_register_rtx_p (index, strict_p)
4257       && (GET_MODE_SIZE (mode) <= 4))
4258     return 1;
4259
4260   if (mode == DImode || mode == DFmode)
4261     {
4262       if (code == CONST_INT)
4263         {
4264           HOST_WIDE_INT val = INTVAL (index);
4265
4266           if (TARGET_LDRD)
4267             return val > -256 && val < 256;
4268           else
4269             return val > -4096 && val < 4092;
4270         }
4271
4272       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
4273     }
4274
4275   if (GET_MODE_SIZE (mode) <= 4
4276       && ! (arm_arch4
4277             && (mode == HImode
4278                 || mode == HFmode
4279                 || (mode == QImode && outer == SIGN_EXTEND))))
4280     {
4281       if (code == MULT)
4282         {
4283           rtx xiop0 = XEXP (index, 0);
4284           rtx xiop1 = XEXP (index, 1);
4285
4286           return ((arm_address_register_rtx_p (xiop0, strict_p)
4287                    && power_of_two_operand (xiop1, SImode))
4288                   || (arm_address_register_rtx_p (xiop1, strict_p)
4289                       && power_of_two_operand (xiop0, SImode)));
4290         }
4291       else if (code == LSHIFTRT || code == ASHIFTRT
4292                || code == ASHIFT || code == ROTATERT)
4293         {
4294           rtx op = XEXP (index, 1);
4295
4296           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4297                   && GET_CODE (op) == CONST_INT
4298                   && INTVAL (op) > 0
4299                   && INTVAL (op) <= 31);
4300         }
4301     }
4302
4303   /* For ARM v4 we may be doing a sign-extend operation during the
4304      load.  */
4305   if (arm_arch4)
4306     {
4307       if (mode == HImode
4308           || mode == HFmode
4309           || (outer == SIGN_EXTEND && mode == QImode))
4310         range = 256;
4311       else
4312         range = 4096;
4313     }
4314   else
4315     range = (mode == HImode || mode == HFmode) ? 4095 : 4096;
4316
4317   return (code == CONST_INT
4318           && INTVAL (index) < range
4319           && INTVAL (index) > -range);
4320 }
4321
4322 /* Return true if OP is a valid index scaling factor for Thumb-2 address
4323    index operand.  i.e. 1, 2, 4 or 8.  */
4324 static bool
4325 thumb2_index_mul_operand (rtx op)
4326 {
4327   HOST_WIDE_INT val;
4328   
4329   if (GET_CODE(op) != CONST_INT)
4330     return false;
4331
4332   val = INTVAL(op);
4333   return (val == 1 || val == 2 || val == 4 || val == 8);
4334 }
4335   
4336 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
4337 static int
4338 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4339 {
4340   enum rtx_code code = GET_CODE (index);
4341
4342   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
4343   /* Standard coprocessor addressing modes.  */
4344   if (TARGET_HARD_FLOAT
4345       && (TARGET_FPA || TARGET_MAVERICK)
4346       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4347           || (TARGET_MAVERICK && mode == DImode)))
4348     return (code == CONST_INT && INTVAL (index) < 1024
4349             && INTVAL (index) > -1024
4350             && (INTVAL (index) & 3) == 0);
4351
4352   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4353     {
4354       /* For DImode assume values will usually live in core regs
4355          and only allow LDRD addressing modes.  */
4356       if (!TARGET_LDRD || mode != DImode)
4357         return (code == CONST_INT
4358                 && INTVAL (index) < 1024
4359                 && INTVAL (index) > -1024
4360                 && (INTVAL (index) & 3) == 0);
4361     }
4362
4363   if (TARGET_NEON
4364       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4365     return (code == CONST_INT
4366             && INTVAL (index) < 1016
4367             && INTVAL (index) > -1024
4368             && (INTVAL (index) & 3) == 0);
4369
4370   if (arm_address_register_rtx_p (index, strict_p)
4371       && (GET_MODE_SIZE (mode) <= 4))
4372     return 1;
4373
4374   if (mode == DImode || mode == DFmode)
4375     {
4376       if (code == CONST_INT)
4377         {
4378           HOST_WIDE_INT val = INTVAL (index);
4379           /* ??? Can we assume ldrd for thumb2?  */
4380           /* Thumb-2 ldrd only has reg+const addressing modes.  */
4381           /* ldrd supports offsets of +-1020.
4382              However the ldr fallback does not.  */
4383           return val > -256 && val < 256 && (val & 3) == 0;
4384         }
4385       else
4386         return 0;
4387     }
4388
4389   if (code == MULT)
4390     {
4391       rtx xiop0 = XEXP (index, 0);
4392       rtx xiop1 = XEXP (index, 1);
4393
4394       return ((arm_address_register_rtx_p (xiop0, strict_p)
4395                && thumb2_index_mul_operand (xiop1))
4396               || (arm_address_register_rtx_p (xiop1, strict_p)
4397                   && thumb2_index_mul_operand (xiop0)));
4398     }
4399   else if (code == ASHIFT)
4400     {
4401       rtx op = XEXP (index, 1);
4402
4403       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4404               && GET_CODE (op) == CONST_INT
4405               && INTVAL (op) > 0
4406               && INTVAL (op) <= 3);
4407     }
4408
4409   return (code == CONST_INT
4410           && INTVAL (index) < 4096
4411           && INTVAL (index) > -256);
4412 }
4413
4414 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
4415 static int
4416 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
4417 {
4418   int regno;
4419
4420   if (GET_CODE (x) != REG)
4421     return 0;
4422
4423   regno = REGNO (x);
4424
4425   if (strict_p)
4426     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
4427
4428   return (regno <= LAST_LO_REGNUM
4429           || regno > LAST_VIRTUAL_REGISTER
4430           || regno == FRAME_POINTER_REGNUM
4431           || (GET_MODE_SIZE (mode) >= 4
4432               && (regno == STACK_POINTER_REGNUM
4433                   || regno >= FIRST_PSEUDO_REGISTER
4434                   || x == hard_frame_pointer_rtx
4435                   || x == arg_pointer_rtx)));
4436 }
4437
4438 /* Return nonzero if x is a legitimate index register.  This is the case
4439    for any base register that can access a QImode object.  */
4440 inline static int
4441 thumb1_index_register_rtx_p (rtx x, int strict_p)
4442 {
4443   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4444 }
4445
4446 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4447
4448    The AP may be eliminated to either the SP or the FP, so we use the
4449    least common denominator, e.g. SImode, and offsets from 0 to 64.
4450
4451    ??? Verify whether the above is the right approach.
4452
4453    ??? Also, the FP may be eliminated to the SP, so perhaps that
4454    needs special handling also.
4455
4456    ??? Look at how the mips16 port solves this problem.  It probably uses
4457    better ways to solve some of these problems.
4458
4459    Although it is not incorrect, we don't accept QImode and HImode
4460    addresses based on the frame pointer or arg pointer until the
4461    reload pass starts.  This is so that eliminating such addresses
4462    into stack based ones won't produce impossible code.  */
4463 static int
4464 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4465 {
4466   /* ??? Not clear if this is right.  Experiment.  */
4467   if (GET_MODE_SIZE (mode) < 4
4468       && !(reload_in_progress || reload_completed)
4469       && (reg_mentioned_p (frame_pointer_rtx, x)
4470           || reg_mentioned_p (arg_pointer_rtx, x)
4471           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4472           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4473           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4474           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4475     return 0;
4476
4477   /* Accept any base register.  SP only in SImode or larger.  */
4478   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4479     return 1;
4480
4481   /* This is PC relative data before arm_reorg runs.  */
4482   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4483            && GET_CODE (x) == SYMBOL_REF
4484            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4485     return 1;
4486
4487   /* This is PC relative data after arm_reorg runs.  */
4488   else if ((GET_MODE_SIZE (mode) >= 4 || mode == HFmode)
4489            && reload_completed
4490            && (GET_CODE (x) == LABEL_REF
4491                || (GET_CODE (x) == CONST
4492                    && GET_CODE (XEXP (x, 0)) == PLUS
4493                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4494                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4495     return 1;
4496
4497   /* Post-inc indexing only supported for SImode and larger.  */
4498   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4499            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4500     return 1;
4501
4502   else if (GET_CODE (x) == PLUS)
4503     {
4504       /* REG+REG address can be any two index registers.  */
4505       /* We disallow FRAME+REG addressing since we know that FRAME
4506          will be replaced with STACK, and SP relative addressing only
4507          permits SP+OFFSET.  */
4508       if (GET_MODE_SIZE (mode) <= 4
4509           && XEXP (x, 0) != frame_pointer_rtx
4510           && XEXP (x, 1) != frame_pointer_rtx
4511           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4512           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4513         return 1;
4514
4515       /* REG+const has 5-7 bit offset for non-SP registers.  */
4516       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4517                 || XEXP (x, 0) == arg_pointer_rtx)
4518                && GET_CODE (XEXP (x, 1)) == CONST_INT
4519                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4520         return 1;
4521
4522       /* REG+const has 10-bit offset for SP, but only SImode and
4523          larger is supported.  */
4524       /* ??? Should probably check for DI/DFmode overflow here
4525          just like GO_IF_LEGITIMATE_OFFSET does.  */
4526       else if (GET_CODE (XEXP (x, 0)) == REG
4527                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4528                && GET_MODE_SIZE (mode) >= 4
4529                && GET_CODE (XEXP (x, 1)) == CONST_INT
4530                && INTVAL (XEXP (x, 1)) >= 0
4531                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4532                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4533         return 1;
4534
4535       else if (GET_CODE (XEXP (x, 0)) == REG
4536                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4537                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4538                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4539                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4540                && GET_MODE_SIZE (mode) >= 4
4541                && GET_CODE (XEXP (x, 1)) == CONST_INT
4542                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4543         return 1;
4544     }
4545
4546   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4547            && GET_MODE_SIZE (mode) == 4
4548            && GET_CODE (x) == SYMBOL_REF
4549            && CONSTANT_POOL_ADDRESS_P (x)
4550            && ! (flag_pic
4551                  && symbol_mentioned_p (get_pool_constant (x))
4552                  && ! pcrel_constant_p (get_pool_constant (x))))
4553     return 1;
4554
4555   return 0;
4556 }
4557
4558 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4559    instruction of mode MODE.  */
4560 int
4561 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4562 {
4563   switch (GET_MODE_SIZE (mode))
4564     {
4565     case 1:
4566       return val >= 0 && val < 32;
4567
4568     case 2:
4569       return val >= 0 && val < 64 && (val & 1) == 0;
4570
4571     default:
4572       return (val >= 0
4573               && (val + GET_MODE_SIZE (mode)) <= 128
4574               && (val & 3) == 0);
4575     }
4576 }
4577
4578 bool
4579 arm_legitimate_address_p (enum machine_mode mode, rtx x, bool strict_p)
4580 {
4581   if (TARGET_ARM)
4582     return arm_legitimate_address_outer_p (mode, x, SET, strict_p);
4583   else if (TARGET_THUMB2)
4584     return thumb2_legitimate_address_p (mode, x, strict_p);
4585   else /* if (TARGET_THUMB1) */
4586     return thumb1_legitimate_address_p (mode, x, strict_p);
4587 }
4588
4589 /* Build the SYMBOL_REF for __tls_get_addr.  */
4590
4591 static GTY(()) rtx tls_get_addr_libfunc;
4592
4593 static rtx
4594 get_tls_get_addr (void)
4595 {
4596   if (!tls_get_addr_libfunc)
4597     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4598   return tls_get_addr_libfunc;
4599 }
4600
4601 static rtx
4602 arm_load_tp (rtx target)
4603 {
4604   if (!target)
4605     target = gen_reg_rtx (SImode);
4606
4607   if (TARGET_HARD_TP)
4608     {
4609       /* Can return in any reg.  */
4610       emit_insn (gen_load_tp_hard (target));
4611     }
4612   else
4613     {
4614       /* Always returned in r0.  Immediately copy the result into a pseudo,
4615          otherwise other uses of r0 (e.g. setting up function arguments) may
4616          clobber the value.  */
4617
4618       rtx tmp;
4619
4620       emit_insn (gen_load_tp_soft ());
4621
4622       tmp = gen_rtx_REG (SImode, 0);
4623       emit_move_insn (target, tmp);
4624     }
4625   return target;
4626 }
4627
4628 static rtx
4629 load_tls_operand (rtx x, rtx reg)
4630 {
4631   rtx tmp;
4632
4633   if (reg == NULL_RTX)
4634     reg = gen_reg_rtx (SImode);
4635
4636   tmp = gen_rtx_CONST (SImode, x);
4637
4638   emit_move_insn (reg, tmp);
4639
4640   return reg;
4641 }
4642
4643 static rtx
4644 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4645 {
4646   rtx insns, label, labelno, sum;
4647
4648   start_sequence ();
4649
4650   labelno = GEN_INT (pic_labelno++);
4651   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4652   label = gen_rtx_CONST (VOIDmode, label);
4653
4654   sum = gen_rtx_UNSPEC (Pmode,
4655                         gen_rtvec (4, x, GEN_INT (reloc), label,
4656                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4657                         UNSPEC_TLS);
4658   reg = load_tls_operand (sum, reg);
4659
4660   if (TARGET_ARM)
4661     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4662   else if (TARGET_THUMB2)
4663     {
4664       rtx tmp;
4665       /* Thumb-2 only allows very limited access to the PC.  Calculate
4666          the address in a temporary register.  */
4667       tmp = gen_reg_rtx (SImode);
4668       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4669       emit_insn (gen_addsi3(reg, reg, tmp));
4670     }
4671   else /* TARGET_THUMB1 */
4672     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4673
4674   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4675                                      Pmode, 1, reg, Pmode);
4676
4677   insns = get_insns ();
4678   end_sequence ();
4679
4680   return insns;
4681 }
4682
4683 rtx
4684 legitimize_tls_address (rtx x, rtx reg)
4685 {
4686   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4687   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4688
4689   switch (model)
4690     {
4691     case TLS_MODEL_GLOBAL_DYNAMIC:
4692       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4693       dest = gen_reg_rtx (Pmode);
4694       emit_libcall_block (insns, dest, ret, x);
4695       return dest;
4696
4697     case TLS_MODEL_LOCAL_DYNAMIC:
4698       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4699
4700       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4701          share the LDM result with other LD model accesses.  */
4702       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4703                             UNSPEC_TLS);
4704       dest = gen_reg_rtx (Pmode);
4705       emit_libcall_block (insns, dest, ret, eqv);
4706
4707       /* Load the addend.  */
4708       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4709                                UNSPEC_TLS);
4710       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4711       return gen_rtx_PLUS (Pmode, dest, addend);
4712
4713     case TLS_MODEL_INITIAL_EXEC:
4714       labelno = GEN_INT (pic_labelno++);
4715       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4716       label = gen_rtx_CONST (VOIDmode, label);
4717       sum = gen_rtx_UNSPEC (Pmode,
4718                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4719                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4720                             UNSPEC_TLS);
4721       reg = load_tls_operand (sum, reg);
4722
4723       if (TARGET_ARM)
4724         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4725       else if (TARGET_THUMB2)
4726         {
4727           rtx tmp;
4728           /* Thumb-2 only allows very limited access to the PC.  Calculate
4729              the address in a temporary register.  */
4730           tmp = gen_reg_rtx (SImode);
4731           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4732           emit_insn (gen_addsi3(reg, reg, tmp));
4733           emit_move_insn (reg, gen_const_mem (SImode, reg));
4734         }
4735       else
4736         {
4737           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4738           emit_move_insn (reg, gen_const_mem (SImode, reg));
4739         }
4740
4741       tp = arm_load_tp (NULL_RTX);
4742
4743       return gen_rtx_PLUS (Pmode, tp, reg);
4744
4745     case TLS_MODEL_LOCAL_EXEC:
4746       tp = arm_load_tp (NULL_RTX);
4747
4748       reg = gen_rtx_UNSPEC (Pmode,
4749                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4750                             UNSPEC_TLS);
4751       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4752
4753       return gen_rtx_PLUS (Pmode, tp, reg);
4754
4755     default:
4756       abort ();
4757     }
4758 }
4759
4760 /* Try machine-dependent ways of modifying an illegitimate address
4761    to be legitimate.  If we find one, return the new, valid address.  */
4762 rtx
4763 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4764 {
4765   if (!TARGET_ARM)
4766     {
4767       /* TODO: legitimize_address for Thumb2.  */
4768       if (TARGET_THUMB2)
4769         return x;
4770       return thumb_legitimize_address (x, orig_x, mode);
4771     }
4772
4773   if (arm_tls_symbol_p (x))
4774     return legitimize_tls_address (x, NULL_RTX);
4775
4776   if (GET_CODE (x) == PLUS)
4777     {
4778       rtx xop0 = XEXP (x, 0);
4779       rtx xop1 = XEXP (x, 1);
4780
4781       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4782         xop0 = force_reg (SImode, xop0);
4783
4784       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4785         xop1 = force_reg (SImode, xop1);
4786
4787       if (ARM_BASE_REGISTER_RTX_P (xop0)
4788           && GET_CODE (xop1) == CONST_INT)
4789         {
4790           HOST_WIDE_INT n, low_n;
4791           rtx base_reg, val;
4792           n = INTVAL (xop1);
4793
4794           /* VFP addressing modes actually allow greater offsets, but for
4795              now we just stick with the lowest common denominator.  */
4796           if (mode == DImode
4797               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4798             {
4799               low_n = n & 0x0f;
4800               n &= ~0x0f;
4801               if (low_n > 4)
4802                 {
4803                   n += 16;
4804                   low_n -= 16;
4805                 }
4806             }
4807           else
4808             {
4809               low_n = ((mode) == TImode ? 0
4810                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4811               n -= low_n;
4812             }
4813
4814           base_reg = gen_reg_rtx (SImode);
4815           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4816           emit_move_insn (base_reg, val);
4817           x = plus_constant (base_reg, low_n);
4818         }
4819       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4820         x = gen_rtx_PLUS (SImode, xop0, xop1);
4821     }
4822
4823   /* XXX We don't allow MINUS any more -- see comment in
4824      arm_legitimate_address_outer_p ().  */
4825   else if (GET_CODE (x) == MINUS)
4826     {
4827       rtx xop0 = XEXP (x, 0);
4828       rtx xop1 = XEXP (x, 1);
4829
4830       if (CONSTANT_P (xop0))
4831         xop0 = force_reg (SImode, xop0);
4832
4833       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4834         xop1 = force_reg (SImode, xop1);
4835
4836       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4837         x = gen_rtx_MINUS (SImode, xop0, xop1);
4838     }
4839
4840   /* Make sure to take full advantage of the pre-indexed addressing mode
4841      with absolute addresses which often allows for the base register to
4842      be factorized for multiple adjacent memory references, and it might
4843      even allows for the mini pool to be avoided entirely. */
4844   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4845     {
4846       unsigned int bits;
4847       HOST_WIDE_INT mask, base, index;
4848       rtx base_reg;
4849
4850       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4851          use a 8-bit index. So let's use a 12-bit index for SImode only and
4852          hope that arm_gen_constant will enable ldrb to use more bits. */
4853       bits = (mode == SImode) ? 12 : 8;
4854       mask = (1 << bits) - 1;
4855       base = INTVAL (x) & ~mask;
4856       index = INTVAL (x) & mask;
4857       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4858         {
4859           /* It'll most probably be more efficient to generate the base
4860              with more bits set and use a negative index instead. */
4861           base |= mask;
4862           index -= mask;
4863         }
4864       base_reg = force_reg (SImode, GEN_INT (base));
4865       x = plus_constant (base_reg, index);
4866     }
4867
4868   if (flag_pic)
4869     {
4870       /* We need to find and carefully transform any SYMBOL and LABEL
4871          references; so go back to the original address expression.  */
4872       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4873
4874       if (new_x != orig_x)
4875         x = new_x;
4876     }
4877
4878   return x;
4879 }
4880
4881
4882 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4883    to be legitimate.  If we find one, return the new, valid address.  */
4884 rtx
4885 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4886 {
4887   if (arm_tls_symbol_p (x))
4888     return legitimize_tls_address (x, NULL_RTX);
4889
4890   if (GET_CODE (x) == PLUS
4891       && GET_CODE (XEXP (x, 1)) == CONST_INT
4892       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4893           || INTVAL (XEXP (x, 1)) < 0))
4894     {
4895       rtx xop0 = XEXP (x, 0);
4896       rtx xop1 = XEXP (x, 1);
4897       HOST_WIDE_INT offset = INTVAL (xop1);
4898
4899       /* Try and fold the offset into a biasing of the base register and
4900          then offsetting that.  Don't do this when optimizing for space
4901          since it can cause too many CSEs.  */
4902       if (optimize_size && offset >= 0
4903           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4904         {
4905           HOST_WIDE_INT delta;
4906
4907           if (offset >= 256)
4908             delta = offset - (256 - GET_MODE_SIZE (mode));
4909           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4910             delta = 31 * GET_MODE_SIZE (mode);
4911           else
4912             delta = offset & (~31 * GET_MODE_SIZE (mode));
4913
4914           xop0 = force_operand (plus_constant (xop0, offset - delta),
4915                                 NULL_RTX);
4916           x = plus_constant (xop0, delta);
4917         }
4918       else if (offset < 0 && offset > -256)
4919         /* Small negative offsets are best done with a subtract before the
4920            dereference, forcing these into a register normally takes two
4921            instructions.  */
4922         x = force_operand (x, NULL_RTX);
4923       else
4924         {
4925           /* For the remaining cases, force the constant into a register.  */
4926           xop1 = force_reg (SImode, xop1);
4927           x = gen_rtx_PLUS (SImode, xop0, xop1);
4928         }
4929     }
4930   else if (GET_CODE (x) == PLUS
4931            && s_register_operand (XEXP (x, 1), SImode)
4932            && !s_register_operand (XEXP (x, 0), SImode))
4933     {
4934       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4935
4936       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4937     }
4938
4939   if (flag_pic)
4940     {
4941       /* We need to find and carefully transform any SYMBOL and LABEL
4942          references; so go back to the original address expression.  */
4943       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4944
4945       if (new_x != orig_x)
4946         x = new_x;
4947     }
4948
4949   return x;
4950 }
4951
4952 rtx
4953 thumb_legitimize_reload_address (rtx *x_p,
4954                                  enum machine_mode mode,
4955                                  int opnum, int type,
4956                                  int ind_levels ATTRIBUTE_UNUSED)
4957 {
4958   rtx x = *x_p;
4959
4960   if (GET_CODE (x) == PLUS
4961       && GET_MODE_SIZE (mode) < 4
4962       && REG_P (XEXP (x, 0))
4963       && XEXP (x, 0) == stack_pointer_rtx
4964       && GET_CODE (XEXP (x, 1)) == CONST_INT
4965       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4966     {
4967       rtx orig_x = x;
4968
4969       x = copy_rtx (x);
4970       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4971                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
4972       return x;
4973     }
4974
4975   /* If both registers are hi-regs, then it's better to reload the
4976      entire expression rather than each register individually.  That
4977      only requires one reload register rather than two.  */
4978   if (GET_CODE (x) == PLUS
4979       && REG_P (XEXP (x, 0))
4980       && REG_P (XEXP (x, 1))
4981       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4982       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4983     {
4984       rtx orig_x = x;
4985
4986       x = copy_rtx (x);
4987       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4988                    Pmode, VOIDmode, 0, 0, opnum, (enum reload_type) type);
4989       return x;
4990     }
4991
4992   return NULL;
4993 }
4994
4995 /* Test for various thread-local symbols.  */
4996
4997 /* Return TRUE if X is a thread-local symbol.  */
4998
4999 static bool
5000 arm_tls_symbol_p (rtx x)
5001 {
5002   if (! TARGET_HAVE_TLS)
5003     return false;
5004
5005   if (GET_CODE (x) != SYMBOL_REF)
5006     return false;
5007
5008   return SYMBOL_REF_TLS_MODEL (x) != 0;
5009 }
5010
5011 /* Helper for arm_tls_referenced_p.  */
5012
5013 static int
5014 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
5015 {
5016   if (GET_CODE (*x) == SYMBOL_REF)
5017     return SYMBOL_REF_TLS_MODEL (*x) != 0;
5018
5019   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
5020      TLS offsets, not real symbol references.  */
5021   if (GET_CODE (*x) == UNSPEC
5022       && XINT (*x, 1) == UNSPEC_TLS)
5023     return -1;
5024
5025   return 0;
5026 }
5027
5028 /* Return TRUE if X contains any TLS symbol references.  */
5029
5030 bool
5031 arm_tls_referenced_p (rtx x)
5032 {
5033   if (! TARGET_HAVE_TLS)
5034     return false;
5035
5036   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
5037 }
5038
5039 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
5040
5041 bool
5042 arm_cannot_force_const_mem (rtx x)
5043 {
5044   rtx base, offset;
5045
5046   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
5047     {
5048       split_const (x, &base, &offset);
5049       if (GET_CODE (base) == SYMBOL_REF
5050           && !offset_within_block_p (base, INTVAL (offset)))
5051         return true;
5052     }
5053   return arm_tls_referenced_p (x);
5054 }
5055 \f
5056 #define REG_OR_SUBREG_REG(X)                                            \
5057   (GET_CODE (X) == REG                                                  \
5058    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
5059
5060 #define REG_OR_SUBREG_RTX(X)                    \
5061    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
5062
5063 #ifndef COSTS_N_INSNS
5064 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
5065 #endif
5066 static inline int
5067 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
5068 {
5069   enum machine_mode mode = GET_MODE (x);
5070
5071   switch (code)
5072     {
5073     case ASHIFT:
5074     case ASHIFTRT:
5075     case LSHIFTRT:
5076     case ROTATERT:
5077     case PLUS:
5078     case MINUS:
5079     case COMPARE:
5080     case NEG:
5081     case NOT:
5082       return COSTS_N_INSNS (1);
5083
5084     case MULT:
5085       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5086         {
5087           int cycles = 0;
5088           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
5089
5090           while (i)
5091             {
5092               i >>= 2;
5093               cycles++;
5094             }
5095           return COSTS_N_INSNS (2) + cycles;
5096         }
5097       return COSTS_N_INSNS (1) + 16;
5098
5099     case SET:
5100       return (COSTS_N_INSNS (1)
5101               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
5102                      + GET_CODE (SET_DEST (x)) == MEM));
5103
5104     case CONST_INT:
5105       if (outer == SET)
5106         {
5107           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
5108             return 0;
5109           if (thumb_shiftable_const (INTVAL (x)))
5110             return COSTS_N_INSNS (2);
5111           return COSTS_N_INSNS (3);
5112         }
5113       else if ((outer == PLUS || outer == COMPARE)
5114                && INTVAL (x) < 256 && INTVAL (x) > -256)
5115         return 0;
5116       else if (outer == AND
5117                && INTVAL (x) < 256 && INTVAL (x) >= -256)
5118         return COSTS_N_INSNS (1);
5119       else if (outer == ASHIFT || outer == ASHIFTRT
5120                || outer == LSHIFTRT)
5121         return 0;
5122       return COSTS_N_INSNS (2);
5123
5124     case CONST:
5125     case CONST_DOUBLE:
5126     case LABEL_REF:
5127     case SYMBOL_REF:
5128       return COSTS_N_INSNS (3);
5129
5130     case UDIV:
5131     case UMOD:
5132     case DIV:
5133     case MOD:
5134       return 100;
5135
5136     case TRUNCATE:
5137       return 99;
5138
5139     case AND:
5140     case XOR:
5141     case IOR:
5142       /* XXX guess.  */
5143       return 8;
5144
5145     case MEM:
5146       /* XXX another guess.  */
5147       /* Memory costs quite a lot for the first word, but subsequent words
5148          load at the equivalent of a single insn each.  */
5149       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
5150               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
5151                  ? 4 : 0));
5152
5153     case IF_THEN_ELSE:
5154       /* XXX a guess.  */
5155       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5156         return 14;
5157       return 2;
5158
5159     case ZERO_EXTEND:
5160       /* XXX still guessing.  */
5161       switch (GET_MODE (XEXP (x, 0)))
5162         {
5163         case QImode:
5164           return (1 + (mode == DImode ? 4 : 0)
5165                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5166
5167         case HImode:
5168           return (4 + (mode == DImode ? 4 : 0)
5169                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5170
5171         case SImode:
5172           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5173
5174         default:
5175           return 99;
5176         }
5177
5178     default:
5179       return 99;
5180     }
5181 }
5182
5183 static inline bool
5184 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
5185 {
5186   enum machine_mode mode = GET_MODE (x);
5187   enum rtx_code subcode;
5188   rtx operand;
5189   enum rtx_code code = GET_CODE (x);
5190   int extra_cost;
5191   *total = 0;
5192
5193   switch (code)
5194     {
5195     case MEM:
5196       /* Memory costs quite a lot for the first word, but subsequent words
5197          load at the equivalent of a single insn each.  */
5198       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
5199       return true;
5200
5201     case DIV:
5202     case MOD:
5203     case UDIV:
5204     case UMOD:
5205       if (TARGET_HARD_FLOAT && mode == SFmode)
5206         *total = COSTS_N_INSNS (2);
5207       else if (TARGET_HARD_FLOAT && mode == DFmode)
5208         *total = COSTS_N_INSNS (4);
5209       else
5210         *total = COSTS_N_INSNS (20);
5211       return false;
5212
5213     case ROTATE:
5214       if (GET_CODE (XEXP (x, 1)) == REG)
5215         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
5216       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5217         *total = rtx_cost (XEXP (x, 1), code, speed);
5218
5219       /* Fall through */
5220     case ROTATERT:
5221       if (mode != SImode)
5222         {
5223           *total += COSTS_N_INSNS (4);
5224           return true;
5225         }
5226
5227       /* Fall through */
5228     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
5229       *total += rtx_cost (XEXP (x, 0), code, speed);
5230       if (mode == DImode)
5231         {
5232           *total += COSTS_N_INSNS (3);
5233           return true;
5234         }
5235
5236       *total += COSTS_N_INSNS (1);
5237       /* Increase the cost of complex shifts because they aren't any faster,
5238          and reduce dual issue opportunities.  */
5239       if (arm_tune_cortex_a9
5240           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
5241         ++*total;
5242
5243       return true;
5244
5245     case MINUS:
5246       if (TARGET_THUMB2)
5247         {
5248           if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5249             {
5250               if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5251                 *total = COSTS_N_INSNS (1);
5252               else
5253                 *total = COSTS_N_INSNS (20);
5254             }
5255           else
5256             *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5257           /* Thumb2 does not have RSB, so all arguments must be
5258              registers (subtracting a constant is canonicalized as
5259              addition of the negated constant).  */
5260           return false;
5261         }
5262
5263       if (mode == DImode)
5264         {
5265           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5266           if (GET_CODE (XEXP (x, 0)) == CONST_INT
5267               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
5268             {
5269               *total += rtx_cost (XEXP (x, 1), code, speed);
5270               return true;
5271             }
5272
5273           if (GET_CODE (XEXP (x, 1)) == CONST_INT
5274               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
5275             {
5276               *total += rtx_cost (XEXP (x, 0), code, speed);
5277               return true;
5278             }
5279
5280           return false;
5281         }
5282
5283       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5284         {
5285           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5286             {
5287               *total = COSTS_N_INSNS (1);
5288               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
5289                   && arm_const_double_rtx (XEXP (x, 0)))
5290                 {
5291                   *total += rtx_cost (XEXP (x, 1), code, speed);
5292                   return true;
5293                 }
5294
5295               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5296                   && arm_const_double_rtx (XEXP (x, 1)))
5297                 {
5298                   *total += rtx_cost (XEXP (x, 0), code, speed);
5299                   return true;
5300                 }
5301
5302               return false;
5303             }
5304           *total = COSTS_N_INSNS (20);
5305           return false;
5306         }
5307
5308       *total = COSTS_N_INSNS (1);
5309       if (GET_CODE (XEXP (x, 0)) == CONST_INT
5310           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
5311         {
5312           *total += rtx_cost (XEXP (x, 1), code, speed);
5313           return true;
5314         }
5315
5316       subcode = GET_CODE (XEXP (x, 1));
5317       if (subcode == ASHIFT || subcode == ASHIFTRT
5318           || subcode == LSHIFTRT
5319           || subcode == ROTATE || subcode == ROTATERT)
5320         {
5321           *total += rtx_cost (XEXP (x, 0), code, speed);
5322           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
5323           return true;
5324         }
5325
5326       /* A shift as a part of RSB costs no more than RSB itself.  */
5327       if (GET_CODE (XEXP (x, 0)) == MULT
5328           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5329         {
5330           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, speed);
5331           *total += rtx_cost (XEXP (x, 1), code, speed);
5332           return true;
5333         }
5334
5335       if (subcode == MULT
5336           && power_of_two_operand (XEXP (XEXP (x, 1), 1), SImode))
5337         {
5338           *total += rtx_cost (XEXP (x, 0), code, speed);
5339           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
5340           return true;
5341         }
5342
5343       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
5344           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
5345         {
5346           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5347           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
5348               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
5349             *total += COSTS_N_INSNS (1);
5350
5351           return true;
5352         }
5353
5354       /* Fall through */
5355
5356     case PLUS:
5357       if (code == PLUS && arm_arch6 && mode == SImode
5358           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5359               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5360         {
5361           *total = COSTS_N_INSNS (1);
5362           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
5363                               speed);
5364           *total += rtx_cost (XEXP (x, 1), code, speed);
5365           return true;
5366         }
5367
5368       /* MLA: All arguments must be registers.  We filter out
5369          multiplication by a power of two, so that we fall down into
5370          the code below.  */
5371       if (GET_CODE (XEXP (x, 0)) == MULT
5372           && !power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5373         {
5374           /* The cost comes from the cost of the multiply.  */
5375           return false;
5376         }
5377
5378       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5379         {
5380           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5381             {
5382               *total = COSTS_N_INSNS (1);
5383               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5384                   && arm_const_double_rtx (XEXP (x, 1)))
5385                 {
5386                   *total += rtx_cost (XEXP (x, 0), code, speed);
5387                   return true;
5388                 }
5389
5390               return false;
5391             }
5392
5393           *total = COSTS_N_INSNS (20);
5394           return false;
5395         }
5396
5397       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
5398           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
5399         {
5400           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
5401           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
5402               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
5403             *total += COSTS_N_INSNS (1);
5404           return true;
5405         }
5406
5407       /* Fall through */
5408
5409     case AND: case XOR: case IOR:
5410       extra_cost = 0;
5411
5412       /* Normally the frame registers will be spilt into reg+const during
5413          reload, so it is a bad idea to combine them with other instructions,
5414          since then they might not be moved outside of loops.  As a compromise
5415          we allow integration with ops that have a constant as their second
5416          operand.  */
5417       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
5418            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
5419            && GET_CODE (XEXP (x, 1)) != CONST_INT)
5420           || (REG_OR_SUBREG_REG (XEXP (x, 0))
5421               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
5422         *total = 4;
5423
5424       if (mode == DImode)
5425         {
5426           *total += COSTS_N_INSNS (2);
5427           if (GET_CODE (XEXP (x, 1)) == CONST_INT
5428               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5429             {
5430               *total += rtx_cost (XEXP (x, 0), code, speed);
5431               return true;
5432             }
5433
5434           return false;
5435         }
5436
5437       *total += COSTS_N_INSNS (1);
5438       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5439           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5440         {
5441           *total += rtx_cost (XEXP (x, 0), code, speed);
5442           return true;
5443         }
5444       subcode = GET_CODE (XEXP (x, 0));
5445       if (subcode == ASHIFT || subcode == ASHIFTRT
5446           || subcode == LSHIFTRT
5447           || subcode == ROTATE || subcode == ROTATERT)
5448         {
5449           *total += rtx_cost (XEXP (x, 1), code, speed);
5450           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5451           return true;
5452         }
5453
5454       if (subcode == MULT
5455           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5456         {
5457           *total += rtx_cost (XEXP (x, 1), code, speed);
5458           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5459           return true;
5460         }
5461
5462       if (subcode == UMIN || subcode == UMAX
5463           || subcode == SMIN || subcode == SMAX)
5464         {
5465           *total = COSTS_N_INSNS (3);
5466           return true;
5467         }
5468
5469       return false;
5470
5471     case MULT:
5472       /* This should have been handled by the CPU specific routines.  */
5473       gcc_unreachable ();
5474
5475     case TRUNCATE:
5476       if (arm_arch3m && mode == SImode
5477           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5478           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5479           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
5480               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5481           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
5482               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
5483         {
5484           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
5485           return true;
5486         }
5487       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
5488       return false;
5489
5490     case NEG:
5491       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5492         {
5493           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5494             {
5495               *total = COSTS_N_INSNS (1);
5496               return false;
5497             }
5498           *total = COSTS_N_INSNS (2);
5499           return false;
5500         }
5501
5502       /* Fall through */
5503     case NOT:
5504       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
5505       if (mode == SImode && code == NOT)
5506         {
5507           subcode = GET_CODE (XEXP (x, 0));
5508           if (subcode == ASHIFT || subcode == ASHIFTRT
5509               || subcode == LSHIFTRT
5510               || subcode == ROTATE || subcode == ROTATERT
5511               || (subcode == MULT
5512                   && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode)))
5513             {
5514               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5515               /* Register shifts cost an extra cycle.  */
5516               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
5517                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
5518                                                         subcode, speed);
5519               return true;
5520             }
5521         }
5522
5523       return false;
5524
5525     case IF_THEN_ELSE:
5526       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5527         {
5528           *total = COSTS_N_INSNS (4);
5529           return true;
5530         }
5531
5532       operand = XEXP (x, 0);
5533
5534       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
5535              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
5536             && GET_CODE (XEXP (operand, 0)) == REG
5537             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
5538         *total += COSTS_N_INSNS (1);
5539       *total += (rtx_cost (XEXP (x, 1), code, speed)
5540                  + rtx_cost (XEXP (x, 2), code, speed));
5541       return true;
5542
5543     case NE:
5544       if (mode == SImode && XEXP (x, 1) == const0_rtx)
5545         {
5546           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5547           return true;
5548         }
5549       goto scc_insn;
5550
5551     case GE:
5552       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
5553           && 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 LT:
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 (1) + rtx_cost (XEXP (x, 0), code, speed);
5565           return true;
5566         }
5567       goto scc_insn;
5568
5569     case EQ:
5570     case GT:
5571     case LE:
5572     case GEU:
5573     case LTU:
5574     case GTU:
5575     case LEU:
5576     case UNORDERED:
5577     case ORDERED:
5578     case UNEQ:
5579     case UNGE:
5580     case UNLT:
5581     case UNGT:
5582     case UNLE:
5583     scc_insn:
5584       /* SCC insns.  In the case where the comparison has already been
5585          performed, then they cost 2 instructions.  Otherwise they need
5586          an additional comparison before them.  */
5587       *total = COSTS_N_INSNS (2);
5588       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
5589         {
5590           return true;
5591         }
5592
5593       /* Fall through */
5594     case COMPARE:
5595       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
5596         {
5597           *total = 0;
5598           return true;
5599         }
5600
5601       *total += COSTS_N_INSNS (1);
5602       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5603           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5604         {
5605           *total += rtx_cost (XEXP (x, 0), code, speed);
5606           return true;
5607         }
5608
5609       subcode = GET_CODE (XEXP (x, 0));
5610       if (subcode == ASHIFT || subcode == ASHIFTRT
5611           || subcode == LSHIFTRT
5612           || subcode == ROTATE || subcode == ROTATERT)
5613         {
5614           *total += rtx_cost (XEXP (x, 1), code, speed);
5615           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5616           return true;
5617         }
5618
5619       if (subcode == MULT
5620           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5621         {
5622           *total += rtx_cost (XEXP (x, 1), code, speed);
5623           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5624           return true;
5625         }
5626       
5627       return false;
5628
5629     case UMIN:
5630     case UMAX:
5631     case SMIN:
5632     case SMAX:
5633       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5634       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5635           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
5636         *total += rtx_cost (XEXP (x, 1), code, speed);
5637       return true;
5638
5639     case ABS:
5640       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5641         {
5642           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5643             {
5644               *total = COSTS_N_INSNS (1);
5645               return false;
5646             }
5647           *total = COSTS_N_INSNS (20);
5648           return false;
5649         }
5650       *total = COSTS_N_INSNS (1);
5651       if (mode == DImode)
5652         *total += COSTS_N_INSNS (3);
5653       return false;
5654
5655     case SIGN_EXTEND:
5656       if (GET_MODE_CLASS (mode) == MODE_INT)
5657         {
5658           *total = 0;
5659           if (mode == DImode)
5660             *total += COSTS_N_INSNS (1);
5661
5662           if (GET_MODE (XEXP (x, 0)) != SImode)
5663             {
5664               if (arm_arch6)
5665                 {
5666                   if (GET_CODE (XEXP (x, 0)) != MEM)
5667                     *total += COSTS_N_INSNS (1);
5668                 }
5669               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
5670                 *total += COSTS_N_INSNS (2);
5671             }
5672
5673           return false;
5674         }
5675
5676       /* Fall through */
5677     case ZERO_EXTEND:
5678       *total = 0;
5679       if (GET_MODE_CLASS (mode) == MODE_INT)
5680         {
5681           if (mode == DImode)
5682             *total += COSTS_N_INSNS (1);
5683
5684           if (GET_MODE (XEXP (x, 0)) != SImode)
5685             {
5686               if (arm_arch6)
5687                 {
5688                   if (GET_CODE (XEXP (x, 0)) != MEM)
5689                     *total += COSTS_N_INSNS (1);
5690                 }
5691               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
5692                 *total += COSTS_N_INSNS (GET_MODE (XEXP (x, 0)) == QImode ?
5693                                          1 : 2);
5694             }
5695
5696           return false;
5697         }
5698
5699       switch (GET_MODE (XEXP (x, 0)))
5700         {
5701         case V8QImode:
5702         case V4HImode:
5703         case V2SImode:
5704         case V4QImode:
5705         case V2HImode:
5706           *total = COSTS_N_INSNS (1);
5707           return false;
5708
5709         default:
5710           gcc_unreachable ();
5711         }
5712       gcc_unreachable ();
5713
5714     case ZERO_EXTRACT:
5715     case SIGN_EXTRACT:
5716       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5717       return true;
5718
5719     case CONST_INT:
5720       if (const_ok_for_arm (INTVAL (x))
5721           || const_ok_for_arm (~INTVAL (x)))
5722         *total = COSTS_N_INSNS (1);
5723       else
5724         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
5725                                                   INTVAL (x), NULL_RTX,
5726                                                   NULL_RTX, 0, 0));
5727       return true;
5728
5729     case CONST:
5730     case LABEL_REF:
5731     case SYMBOL_REF:
5732       *total = COSTS_N_INSNS (3);
5733       return true;
5734
5735     case HIGH:
5736       *total = COSTS_N_INSNS (1);
5737       return true;
5738
5739     case LO_SUM:
5740       *total = COSTS_N_INSNS (1);
5741       *total += rtx_cost (XEXP (x, 0), code, speed);
5742       return true;
5743
5744     case CONST_DOUBLE:
5745       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x))
5746         *total = COSTS_N_INSNS (1);
5747       else
5748         *total = COSTS_N_INSNS (4);
5749       return true;
5750
5751     default:
5752       *total = COSTS_N_INSNS (4);
5753       return false;
5754     }
5755 }
5756
5757 /* RTX costs when optimizing for size.  */
5758 static bool
5759 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
5760                     int *total)
5761 {
5762   enum machine_mode mode = GET_MODE (x);
5763   if (TARGET_THUMB1)
5764     {
5765       /* XXX TBD.  For now, use the standard costs.  */
5766       *total = thumb1_rtx_costs (x, code, outer_code);
5767       return true;
5768     }
5769
5770   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
5771   switch (code)
5772     {
5773     case MEM:
5774       /* A memory access costs 1 insn if the mode is small, or the address is
5775          a single register, otherwise it costs one insn per word.  */
5776       if (REG_P (XEXP (x, 0)))
5777         *total = COSTS_N_INSNS (1);
5778       else
5779         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5780       return true;
5781
5782     case DIV:
5783     case MOD:
5784     case UDIV:
5785     case UMOD:
5786       /* Needs a libcall, so it costs about this.  */
5787       *total = COSTS_N_INSNS (2);
5788       return false;
5789
5790     case ROTATE:
5791       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5792         {
5793           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
5794           return true;
5795         }
5796       /* Fall through */
5797     case ROTATERT:
5798     case ASHIFT:
5799     case LSHIFTRT:
5800     case ASHIFTRT:
5801       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5802         {
5803           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
5804           return true;
5805         }
5806       else if (mode == SImode)
5807         {
5808           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
5809           /* Slightly disparage register shifts, but not by much.  */
5810           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5811             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
5812           return true;
5813         }
5814
5815       /* Needs a libcall.  */
5816       *total = COSTS_N_INSNS (2);
5817       return false;
5818
5819     case MINUS:
5820       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5821         {
5822           *total = COSTS_N_INSNS (1);
5823           return false;
5824         }
5825
5826       if (mode == SImode)
5827         {
5828           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5829           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5830
5831           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5832               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5833               || subcode1 == ROTATE || subcode1 == ROTATERT
5834               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5835               || subcode1 == ASHIFTRT)
5836             {
5837               /* It's just the cost of the two operands.  */
5838               *total = 0;
5839               return false;
5840             }
5841
5842           *total = COSTS_N_INSNS (1);
5843           return false;
5844         }
5845
5846       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5847       return false;
5848
5849     case PLUS:
5850       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5851         {
5852           *total = COSTS_N_INSNS (1);
5853           return false;
5854         }
5855
5856       /* A shift as a part of ADD costs nothing.  */
5857       if (GET_CODE (XEXP (x, 0)) == MULT
5858           && power_of_two_operand (XEXP (XEXP (x, 0), 1), SImode))
5859         {
5860           *total = COSTS_N_INSNS (TARGET_THUMB2 ? 2 : 1);
5861           *total += rtx_cost (XEXP (XEXP (x, 0), 0), code, false);
5862           *total += rtx_cost (XEXP (x, 1), code, false);
5863           return true;
5864         }
5865
5866       /* Fall through */
5867     case AND: case XOR: case IOR:
5868       if (mode == SImode)
5869         {
5870           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5871
5872           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5873               || subcode == LSHIFTRT || subcode == ASHIFTRT
5874               || (code == AND && subcode == NOT))
5875             {
5876               /* It's just the cost of the two operands.  */
5877               *total = 0;
5878               return false;
5879             }
5880         }
5881
5882       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5883       return false;
5884
5885     case MULT:
5886       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5887       return false;
5888
5889     case NEG:
5890       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5891         {
5892           *total = COSTS_N_INSNS (1);
5893           return false;
5894         }
5895
5896       /* Fall through */
5897     case NOT:
5898       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5899
5900       return false;
5901
5902     case IF_THEN_ELSE:
5903       *total = 0;
5904       return false;
5905
5906     case COMPARE:
5907       if (cc_register (XEXP (x, 0), VOIDmode))
5908         * total = 0;
5909       else
5910         *total = COSTS_N_INSNS (1);
5911       return false;
5912
5913     case ABS:
5914       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5915         *total = COSTS_N_INSNS (1);
5916       else
5917         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5918       return false;
5919
5920     case SIGN_EXTEND:
5921       *total = 0;
5922       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5923         {
5924           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5925             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5926         }
5927       if (mode == DImode)
5928         *total += COSTS_N_INSNS (1);
5929       return false;
5930
5931     case ZERO_EXTEND:
5932       *total = 0;
5933       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5934         {
5935           switch (GET_MODE (XEXP (x, 0)))
5936             {
5937             case QImode:
5938               *total += COSTS_N_INSNS (1);
5939               break;
5940
5941             case HImode:
5942               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5943
5944             case SImode:
5945               break;
5946
5947             default:
5948               *total += COSTS_N_INSNS (2);
5949             }
5950         }
5951
5952       if (mode == DImode)
5953         *total += COSTS_N_INSNS (1);
5954
5955       return false;
5956
5957     case CONST_INT:
5958       if (const_ok_for_arm (INTVAL (x)))
5959         /* A multiplication by a constant requires another instruction
5960            to load the constant to a register.  */
5961         *total = COSTS_N_INSNS ((outer_code == SET || outer_code == MULT)
5962                                 ? 1 : 0);
5963       else if (const_ok_for_arm (~INTVAL (x)))
5964         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5965       else if (const_ok_for_arm (-INTVAL (x)))
5966         {
5967           if (outer_code == COMPARE || outer_code == PLUS
5968               || outer_code == MINUS)
5969             *total = 0;
5970           else
5971             *total = COSTS_N_INSNS (1);
5972         }
5973       else
5974         *total = COSTS_N_INSNS (2);
5975       return true;
5976
5977     case CONST:
5978     case LABEL_REF:
5979     case SYMBOL_REF:
5980       *total = COSTS_N_INSNS (2);
5981       return true;
5982
5983     case CONST_DOUBLE:
5984       *total = COSTS_N_INSNS (4);
5985       return true;
5986
5987     case HIGH:
5988     case LO_SUM:
5989       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
5990          cost of these slightly.  */
5991       *total = COSTS_N_INSNS (1) + 1;
5992       return true;
5993
5994     default:
5995       if (mode != VOIDmode)
5996         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5997       else
5998         *total = COSTS_N_INSNS (4); /* How knows?  */
5999       return false;
6000     }
6001 }
6002
6003 /* RTX costs when optimizing for size.  */
6004 static bool
6005 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
6006                bool speed)
6007 {
6008   if (!speed)
6009     return arm_size_rtx_costs (x, (enum rtx_code) code,
6010                                (enum rtx_code) outer_code, total);
6011   else
6012     return all_cores[(int)arm_tune].rtx_costs (x, (enum rtx_code) code,
6013                                                (enum rtx_code) outer_code,
6014                                                total, speed);
6015 }
6016
6017 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
6018    supported on any "slowmul" cores, so it can be ignored.  */
6019
6020 static bool
6021 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6022                        int *total, bool speed)
6023 {
6024   enum machine_mode mode = GET_MODE (x);
6025
6026   if (TARGET_THUMB)
6027     {
6028       *total = thumb1_rtx_costs (x, code, outer_code);
6029       return true;
6030     }
6031
6032   switch (code)
6033     {
6034     case MULT:
6035       if (GET_MODE_CLASS (mode) == MODE_FLOAT
6036           || mode == DImode)
6037         {
6038           *total = COSTS_N_INSNS (20);
6039           return false;
6040         }
6041
6042       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6043         {
6044           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
6045                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
6046           int cost, const_ok = const_ok_for_arm (i);
6047           int j, booth_unit_size;
6048
6049           /* Tune as appropriate.  */
6050           cost = const_ok ? 4 : 8;
6051           booth_unit_size = 2;
6052           for (j = 0; i && j < 32; j += booth_unit_size)
6053             {
6054               i >>= booth_unit_size;
6055               cost++;
6056             }
6057
6058           *total = COSTS_N_INSNS (cost);
6059           *total += rtx_cost (XEXP (x, 0), code, speed);
6060           return true;
6061         }
6062
6063       *total = COSTS_N_INSNS (20);
6064       return false;
6065
6066     default:
6067       return arm_rtx_costs_1 (x, outer_code, total, speed);;
6068     }
6069 }
6070
6071
6072 /* RTX cost for cores with a fast multiply unit (M variants).  */
6073
6074 static bool
6075 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6076                        int *total, bool speed)
6077 {
6078   enum machine_mode mode = GET_MODE (x);
6079
6080   if (TARGET_THUMB1)
6081     {
6082       *total = thumb1_rtx_costs (x, code, outer_code);
6083       return true;
6084     }
6085
6086   /* ??? should thumb2 use different costs?  */
6087   switch (code)
6088     {
6089     case MULT:
6090       /* There is no point basing this on the tuning, since it is always the
6091          fast variant if it exists at all.  */
6092       if (mode == DImode
6093           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
6094           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6095               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6096         {
6097           *total = COSTS_N_INSNS(2);
6098           return false;
6099         }
6100
6101
6102       if (mode == DImode)
6103         {
6104           *total = COSTS_N_INSNS (5);
6105           return false;
6106         }
6107
6108       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6109         {
6110           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
6111                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
6112           int cost, const_ok = const_ok_for_arm (i);
6113           int j, booth_unit_size;
6114
6115           /* Tune as appropriate.  */
6116           cost = const_ok ? 4 : 8;
6117           booth_unit_size = 8;
6118           for (j = 0; i && j < 32; j += booth_unit_size)
6119             {
6120               i >>= booth_unit_size;
6121               cost++;
6122             }
6123
6124           *total = COSTS_N_INSNS(cost);
6125           return false;
6126         }
6127
6128       if (mode == SImode)
6129         {
6130           *total = COSTS_N_INSNS (4);
6131           return false;
6132         }
6133
6134       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6135         {
6136           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6137             {
6138               *total = COSTS_N_INSNS (1);
6139               return false;
6140             }
6141         }
6142
6143       /* Requires a lib call */
6144       *total = COSTS_N_INSNS (20);
6145       return false;
6146
6147     default:
6148       return arm_rtx_costs_1 (x, outer_code, total, speed);
6149     }
6150 }
6151
6152
6153 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
6154    so it can be ignored.  */
6155
6156 static bool
6157 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code, int *total, bool speed)
6158 {
6159   enum machine_mode mode = GET_MODE (x);
6160
6161   if (TARGET_THUMB)
6162     {
6163       *total = thumb1_rtx_costs (x, code, outer_code);
6164       return true;
6165     }
6166
6167   switch (code)
6168     {
6169     case COMPARE:
6170       if (GET_CODE (XEXP (x, 0)) != MULT)
6171         return arm_rtx_costs_1 (x, outer_code, total, speed);
6172
6173       /* A COMPARE of a MULT is slow on XScale; the muls instruction
6174          will stall until the multiplication is complete.  */
6175       *total = COSTS_N_INSNS (3);
6176       return false;
6177
6178     case MULT:
6179       /* There is no point basing this on the tuning, since it is always the
6180          fast variant if it exists at all.  */
6181       if (mode == DImode
6182           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
6183           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6184               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6185         {
6186           *total = COSTS_N_INSNS (2);
6187           return false;
6188         }
6189
6190
6191       if (mode == DImode)
6192         {
6193           *total = COSTS_N_INSNS (5);
6194           return false;
6195         }
6196
6197       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6198         {
6199           /* If operand 1 is a constant we can more accurately
6200              calculate the cost of the multiply.  The multiplier can
6201              retire 15 bits on the first cycle and a further 12 on the
6202              second.  We do, of course, have to load the constant into
6203              a register first.  */
6204           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
6205           /* There's a general overhead of one cycle.  */
6206           int cost = 1;
6207           unsigned HOST_WIDE_INT masked_const;
6208
6209           if (i & 0x80000000)
6210             i = ~i;
6211
6212           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
6213
6214           masked_const = i & 0xffff8000;
6215           if (masked_const != 0)
6216             {
6217               cost++;
6218               masked_const = i & 0xf8000000;
6219               if (masked_const != 0)
6220                 cost++;
6221             }
6222           *total = COSTS_N_INSNS (cost);
6223           return false;
6224         }
6225
6226       if (mode == SImode)
6227         {
6228           *total = COSTS_N_INSNS (3);
6229           return false;
6230         }
6231
6232       /* Requires a lib call */
6233       *total = COSTS_N_INSNS (20);
6234       return false;
6235
6236     default:
6237       return arm_rtx_costs_1 (x, outer_code, total, speed);
6238     }
6239 }
6240
6241
6242 /* RTX costs for 9e (and later) cores.  */
6243
6244 static bool
6245 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
6246                   int *total, bool speed)
6247 {
6248   enum machine_mode mode = GET_MODE (x);
6249
6250   if (TARGET_THUMB1)
6251     {
6252       switch (code)
6253         {
6254         case MULT:
6255           *total = COSTS_N_INSNS (3);
6256           return true;
6257
6258         default:
6259           *total = thumb1_rtx_costs (x, code, outer_code);
6260           return true;
6261         }
6262     }
6263
6264   switch (code)
6265     {
6266     case MULT:
6267       /* There is no point basing this on the tuning, since it is always the
6268          fast variant if it exists at all.  */
6269       if (mode == DImode
6270           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
6271           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
6272               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
6273         {
6274           *total = COSTS_N_INSNS (2);
6275           return false;
6276         }
6277
6278
6279       if (mode == DImode)
6280         {
6281           *total = COSTS_N_INSNS (5);
6282           return false;
6283         }
6284
6285       if (mode == SImode)
6286         {
6287           *total = COSTS_N_INSNS (2);
6288           return false;
6289         }
6290
6291       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
6292         {
6293           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6294             {
6295               *total = COSTS_N_INSNS (1);
6296               return false;
6297             }
6298         }
6299
6300       *total = COSTS_N_INSNS (20);
6301       return false;
6302
6303     default:
6304       return arm_rtx_costs_1 (x, outer_code, total, speed);
6305     }
6306 }
6307 /* All address computations that can be done are free, but rtx cost returns
6308    the same for practically all of them.  So we weight the different types
6309    of address here in the order (most pref first):
6310    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
6311 static inline int
6312 arm_arm_address_cost (rtx x)
6313 {
6314   enum rtx_code c  = GET_CODE (x);
6315
6316   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
6317     return 0;
6318   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
6319     return 10;
6320
6321   if (c == PLUS || c == MINUS)
6322     {
6323       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6324         return 2;
6325
6326       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
6327         return 3;
6328
6329       return 4;
6330     }
6331
6332   return 6;
6333 }
6334
6335 static inline int
6336 arm_thumb_address_cost (rtx x)
6337 {
6338   enum rtx_code c  = GET_CODE (x);
6339
6340   if (c == REG)
6341     return 1;
6342   if (c == PLUS
6343       && GET_CODE (XEXP (x, 0)) == REG
6344       && GET_CODE (XEXP (x, 1)) == CONST_INT)
6345     return 1;
6346
6347   return 2;
6348 }
6349
6350 static int
6351 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
6352 {
6353   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
6354 }
6355
6356 static int
6357 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
6358 {
6359   rtx i_pat, d_pat;
6360
6361   /* Some true dependencies can have a higher cost depending
6362      on precisely how certain input operands are used.  */
6363   if (arm_tune_xscale
6364       && REG_NOTE_KIND (link) == 0
6365       && recog_memoized (insn) >= 0
6366       && recog_memoized (dep) >= 0)
6367     {
6368       int shift_opnum = get_attr_shift (insn);
6369       enum attr_type attr_type = get_attr_type (dep);
6370
6371       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
6372          operand for INSN.  If we have a shifted input operand and the
6373          instruction we depend on is another ALU instruction, then we may
6374          have to account for an additional stall.  */
6375       if (shift_opnum != 0
6376           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
6377         {
6378           rtx shifted_operand;
6379           int opno;
6380
6381           /* Get the shifted operand.  */
6382           extract_insn (insn);
6383           shifted_operand = recog_data.operand[shift_opnum];
6384
6385           /* Iterate over all the operands in DEP.  If we write an operand
6386              that overlaps with SHIFTED_OPERAND, then we have increase the
6387              cost of this dependency.  */
6388           extract_insn (dep);
6389           preprocess_constraints ();
6390           for (opno = 0; opno < recog_data.n_operands; opno++)
6391             {
6392               /* We can ignore strict inputs.  */
6393               if (recog_data.operand_type[opno] == OP_IN)
6394                 continue;
6395
6396               if (reg_overlap_mentioned_p (recog_data.operand[opno],
6397                                            shifted_operand))
6398                 return 2;
6399             }
6400         }
6401     }
6402
6403   /* XXX This is not strictly true for the FPA.  */
6404   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
6405       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
6406     return 0;
6407
6408   /* Call insns don't incur a stall, even if they follow a load.  */
6409   if (REG_NOTE_KIND (link) == 0
6410       && GET_CODE (insn) == CALL_INSN)
6411     return 1;
6412
6413   if ((i_pat = single_set (insn)) != NULL
6414       && GET_CODE (SET_SRC (i_pat)) == MEM
6415       && (d_pat = single_set (dep)) != NULL
6416       && GET_CODE (SET_DEST (d_pat)) == MEM)
6417     {
6418       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
6419       /* This is a load after a store, there is no conflict if the load reads
6420          from a cached area.  Assume that loads from the stack, and from the
6421          constant pool are cached, and that others will miss.  This is a
6422          hack.  */
6423
6424       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
6425           || reg_mentioned_p (stack_pointer_rtx, src_mem)
6426           || reg_mentioned_p (frame_pointer_rtx, src_mem)
6427           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
6428         return 1;
6429     }
6430
6431   return cost;
6432 }
6433
6434 static int fp_consts_inited = 0;
6435
6436 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
6437 static const char * const strings_fp[8] =
6438 {
6439   "0",   "1",   "2",   "3",
6440   "4",   "5",   "0.5", "10"
6441 };
6442
6443 static REAL_VALUE_TYPE values_fp[8];
6444
6445 static void
6446 init_fp_table (void)
6447 {
6448   int i;
6449   REAL_VALUE_TYPE r;
6450
6451   if (TARGET_VFP)
6452     fp_consts_inited = 1;
6453   else
6454     fp_consts_inited = 8;
6455
6456   for (i = 0; i < fp_consts_inited; i++)
6457     {
6458       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
6459       values_fp[i] = r;
6460     }
6461 }
6462
6463 /* Return TRUE if rtx X is a valid immediate FP constant.  */
6464 int
6465 arm_const_double_rtx (rtx x)
6466 {
6467   REAL_VALUE_TYPE r;
6468   int i;
6469
6470   if (!fp_consts_inited)
6471     init_fp_table ();
6472
6473   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6474   if (REAL_VALUE_MINUS_ZERO (r))
6475     return 0;
6476
6477   for (i = 0; i < fp_consts_inited; i++)
6478     if (REAL_VALUES_EQUAL (r, values_fp[i]))
6479       return 1;
6480
6481   return 0;
6482 }
6483
6484 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
6485 int
6486 neg_const_double_rtx_ok_for_fpa (rtx x)
6487 {
6488   REAL_VALUE_TYPE r;
6489   int i;
6490
6491   if (!fp_consts_inited)
6492     init_fp_table ();
6493
6494   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6495   r = REAL_VALUE_NEGATE (r);
6496   if (REAL_VALUE_MINUS_ZERO (r))
6497     return 0;
6498
6499   for (i = 0; i < 8; i++)
6500     if (REAL_VALUES_EQUAL (r, values_fp[i]))
6501       return 1;
6502
6503   return 0;
6504 }
6505
6506
6507 /* VFPv3 has a fairly wide range of representable immediates, formed from
6508    "quarter-precision" floating-point values. These can be evaluated using this
6509    formula (with ^ for exponentiation):
6510
6511      -1^s * n * 2^-r
6512
6513    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
6514    16 <= n <= 31 and 0 <= r <= 7.
6515
6516    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
6517
6518      - A (most-significant) is the sign bit.
6519      - BCD are the exponent (encoded as r XOR 3).
6520      - EFGH are the mantissa (encoded as n - 16).
6521 */
6522
6523 /* Return an integer index for a VFPv3 immediate operand X suitable for the
6524    fconst[sd] instruction, or -1 if X isn't suitable.  */
6525 static int
6526 vfp3_const_double_index (rtx x)
6527 {
6528   REAL_VALUE_TYPE r, m;
6529   int sign, exponent;
6530   unsigned HOST_WIDE_INT mantissa, mant_hi;
6531   unsigned HOST_WIDE_INT mask;
6532   HOST_WIDE_INT m1, m2;
6533   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
6534
6535   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
6536     return -1;
6537
6538   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6539
6540   /* We can't represent these things, so detect them first.  */
6541   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
6542     return -1;
6543
6544   /* Extract sign, exponent and mantissa.  */
6545   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
6546   r = REAL_VALUE_ABS (r);
6547   exponent = REAL_EXP (&r);
6548   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
6549      highest (sign) bit, with a fixed binary point at bit point_pos.
6550      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
6551      bits for the mantissa, this may fail (low bits would be lost).  */
6552   real_ldexp (&m, &r, point_pos - exponent);
6553   REAL_VALUE_TO_INT (&m1, &m2, m);
6554   mantissa = m1;
6555   mant_hi = m2;
6556
6557   /* If there are bits set in the low part of the mantissa, we can't
6558      represent this value.  */
6559   if (mantissa != 0)
6560     return -1;
6561
6562   /* Now make it so that mantissa contains the most-significant bits, and move
6563      the point_pos to indicate that the least-significant bits have been
6564      discarded.  */
6565   point_pos -= HOST_BITS_PER_WIDE_INT;
6566   mantissa = mant_hi;
6567
6568   /* We can permit four significant bits of mantissa only, plus a high bit
6569      which is always 1.  */
6570   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
6571   if ((mantissa & mask) != 0)
6572     return -1;
6573
6574   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
6575   mantissa >>= point_pos - 5;
6576
6577   /* The mantissa may be zero. Disallow that case. (It's possible to load the
6578      floating-point immediate zero with Neon using an integer-zero load, but
6579      that case is handled elsewhere.)  */
6580   if (mantissa == 0)
6581     return -1;
6582
6583   gcc_assert (mantissa >= 16 && mantissa <= 31);
6584
6585   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
6586      normalized significands are in the range [1, 2). (Our mantissa is shifted
6587      left 4 places at this point relative to normalized IEEE754 values).  GCC
6588      internally uses [0.5, 1) (see real.c), so the exponent returned from
6589      REAL_EXP must be altered.  */
6590   exponent = 5 - exponent;
6591
6592   if (exponent < 0 || exponent > 7)
6593     return -1;
6594
6595   /* Sign, mantissa and exponent are now in the correct form to plug into the
6596      formula described in the comment above.  */
6597   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
6598 }
6599
6600 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
6601 int
6602 vfp3_const_double_rtx (rtx x)
6603 {
6604   if (!TARGET_VFP3)
6605     return 0;
6606
6607   return vfp3_const_double_index (x) != -1;
6608 }
6609
6610 /* Recognize immediates which can be used in various Neon instructions. Legal
6611    immediates are described by the following table (for VMVN variants, the
6612    bitwise inverse of the constant shown is recognized. In either case, VMOV
6613    is output and the correct instruction to use for a given constant is chosen
6614    by the assembler). The constant shown is replicated across all elements of
6615    the destination vector.
6616
6617    insn elems variant constant (binary)
6618    ---- ----- ------- -----------------
6619    vmov  i32     0    00000000 00000000 00000000 abcdefgh
6620    vmov  i32     1    00000000 00000000 abcdefgh 00000000
6621    vmov  i32     2    00000000 abcdefgh 00000000 00000000
6622    vmov  i32     3    abcdefgh 00000000 00000000 00000000
6623    vmov  i16     4    00000000 abcdefgh
6624    vmov  i16     5    abcdefgh 00000000
6625    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
6626    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
6627    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
6628    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
6629    vmvn  i16    10    00000000 abcdefgh
6630    vmvn  i16    11    abcdefgh 00000000
6631    vmov  i32    12    00000000 00000000 abcdefgh 11111111
6632    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
6633    vmov  i32    14    00000000 abcdefgh 11111111 11111111
6634    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
6635    vmov   i8    16    abcdefgh
6636    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
6637                       eeeeeeee ffffffff gggggggg hhhhhhhh
6638    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
6639
6640    For case 18, B = !b. Representable values are exactly those accepted by
6641    vfp3_const_double_index, but are output as floating-point numbers rather
6642    than indices.
6643
6644    Variants 0-5 (inclusive) may also be used as immediates for the second
6645    operand of VORR/VBIC instructions.
6646
6647    The INVERSE argument causes the bitwise inverse of the given operand to be
6648    recognized instead (used for recognizing legal immediates for the VAND/VORN
6649    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
6650    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
6651    output, rather than the real insns vbic/vorr).
6652
6653    INVERSE makes no difference to the recognition of float vectors.
6654
6655    The return value is the variant of immediate as shown in the above table, or
6656    -1 if the given value doesn't match any of the listed patterns.
6657 */
6658 static int
6659 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
6660                       rtx *modconst, int *elementwidth)
6661 {
6662 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
6663   matches = 1;                                  \
6664   for (i = 0; i < idx; i += (STRIDE))           \
6665     if (!(TEST))                                \
6666       matches = 0;                              \
6667   if (matches)                                  \
6668     {                                           \
6669       immtype = (CLASS);                        \
6670       elsize = (ELSIZE);                        \
6671       break;                                    \
6672     }
6673
6674   unsigned int i, elsize = 0, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
6675   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
6676   unsigned char bytes[16];
6677   int immtype = -1, matches;
6678   unsigned int invmask = inverse ? 0xff : 0;
6679
6680   /* Vectors of float constants.  */
6681   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6682     {
6683       rtx el0 = CONST_VECTOR_ELT (op, 0);
6684       REAL_VALUE_TYPE r0;
6685
6686       if (!vfp3_const_double_rtx (el0))
6687         return -1;
6688
6689       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
6690
6691       for (i = 1; i < n_elts; i++)
6692         {
6693           rtx elt = CONST_VECTOR_ELT (op, i);
6694           REAL_VALUE_TYPE re;
6695
6696           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
6697
6698           if (!REAL_VALUES_EQUAL (r0, re))
6699             return -1;
6700         }
6701
6702       if (modconst)
6703         *modconst = CONST_VECTOR_ELT (op, 0);
6704
6705       if (elementwidth)
6706         *elementwidth = 0;
6707
6708       return 18;
6709     }
6710
6711   /* Splat vector constant out into a byte vector.  */
6712   for (i = 0; i < n_elts; i++)
6713     {
6714       rtx el = CONST_VECTOR_ELT (op, i);
6715       unsigned HOST_WIDE_INT elpart;
6716       unsigned int part, parts;
6717
6718       if (GET_CODE (el) == CONST_INT)
6719         {
6720           elpart = INTVAL (el);
6721           parts = 1;
6722         }
6723       else if (GET_CODE (el) == CONST_DOUBLE)
6724         {
6725           elpart = CONST_DOUBLE_LOW (el);
6726           parts = 2;
6727         }
6728       else
6729         gcc_unreachable ();
6730
6731       for (part = 0; part < parts; part++)
6732         {
6733           unsigned int byte;
6734           for (byte = 0; byte < innersize; byte++)
6735             {
6736               bytes[idx++] = (elpart & 0xff) ^ invmask;
6737               elpart >>= BITS_PER_UNIT;
6738             }
6739           if (GET_CODE (el) == CONST_DOUBLE)
6740             elpart = CONST_DOUBLE_HIGH (el);
6741         }
6742     }
6743
6744   /* Sanity check.  */
6745   gcc_assert (idx == GET_MODE_SIZE (mode));
6746
6747   do
6748     {
6749       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
6750                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6751
6752       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6753                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6754
6755       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
6756                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6757
6758       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
6759                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
6760
6761       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
6762
6763       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
6764
6765       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
6766                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6767
6768       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6769                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6770
6771       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
6772                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6773
6774       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
6775                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
6776
6777       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
6778
6779       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
6780
6781       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6782                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6783
6784       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6785                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6786
6787       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
6788                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6789
6790       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
6791                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6792
6793       CHECK (1, 8, 16, bytes[i] == bytes[0]);
6794
6795       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
6796                         && bytes[i] == bytes[(i + 8) % idx]);
6797     }
6798   while (0);
6799
6800   if (immtype == -1)
6801     return -1;
6802
6803   if (elementwidth)
6804     *elementwidth = elsize;
6805
6806   if (modconst)
6807     {
6808       unsigned HOST_WIDE_INT imm = 0;
6809
6810       /* Un-invert bytes of recognized vector, if necessary.  */
6811       if (invmask != 0)
6812         for (i = 0; i < idx; i++)
6813           bytes[i] ^= invmask;
6814
6815       if (immtype == 17)
6816         {
6817           /* FIXME: Broken on 32-bit H_W_I hosts.  */
6818           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
6819
6820           for (i = 0; i < 8; i++)
6821             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
6822                    << (i * BITS_PER_UNIT);
6823
6824           *modconst = GEN_INT (imm);
6825         }
6826       else
6827         {
6828           unsigned HOST_WIDE_INT imm = 0;
6829
6830           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
6831             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
6832
6833           *modconst = GEN_INT (imm);
6834         }
6835     }
6836
6837   return immtype;
6838 #undef CHECK
6839 }
6840
6841 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
6842    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
6843    float elements), and a modified constant (whatever should be output for a
6844    VMOV) in *MODCONST.  */
6845
6846 int
6847 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
6848                                rtx *modconst, int *elementwidth)
6849 {
6850   rtx tmpconst;
6851   int tmpwidth;
6852   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
6853
6854   if (retval == -1)
6855     return 0;
6856
6857   if (modconst)
6858     *modconst = tmpconst;
6859
6860   if (elementwidth)
6861     *elementwidth = tmpwidth;
6862
6863   return 1;
6864 }
6865
6866 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
6867    the immediate is valid, write a constant suitable for using as an operand
6868    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
6869    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
6870
6871 int
6872 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
6873                                 rtx *modconst, int *elementwidth)
6874 {
6875   rtx tmpconst;
6876   int tmpwidth;
6877   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
6878
6879   if (retval < 0 || retval > 5)
6880     return 0;
6881
6882   if (modconst)
6883     *modconst = tmpconst;
6884
6885   if (elementwidth)
6886     *elementwidth = tmpwidth;
6887
6888   return 1;
6889 }
6890
6891 /* Return a string suitable for output of Neon immediate logic operation
6892    MNEM.  */
6893
6894 char *
6895 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
6896                              int inverse, int quad)
6897 {
6898   int width, is_valid;
6899   static char templ[40];
6900
6901   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
6902
6903   gcc_assert (is_valid != 0);
6904
6905   if (quad)
6906     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
6907   else
6908     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
6909
6910   return templ;
6911 }
6912
6913 /* Output a sequence of pairwise operations to implement a reduction.
6914    NOTE: We do "too much work" here, because pairwise operations work on two
6915    registers-worth of operands in one go. Unfortunately we can't exploit those
6916    extra calculations to do the full operation in fewer steps, I don't think.
6917    Although all vector elements of the result but the first are ignored, we
6918    actually calculate the same result in each of the elements. An alternative
6919    such as initially loading a vector with zero to use as each of the second
6920    operands would use up an additional register and take an extra instruction,
6921    for no particular gain.  */
6922
6923 void
6924 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
6925                       rtx (*reduc) (rtx, rtx, rtx))
6926 {
6927   enum machine_mode inner = GET_MODE_INNER (mode);
6928   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
6929   rtx tmpsum = op1;
6930
6931   for (i = parts / 2; i >= 1; i /= 2)
6932     {
6933       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
6934       emit_insn (reduc (dest, tmpsum, tmpsum));
6935       tmpsum = dest;
6936     }
6937 }
6938
6939 /* Initialize a vector with non-constant elements.  FIXME: We can do better
6940    than the current implementation (building a vector on the stack and then
6941    loading it) in many cases.  See rs6000.c.  */
6942
6943 void
6944 neon_expand_vector_init (rtx target, rtx vals)
6945 {
6946   enum machine_mode mode = GET_MODE (target);
6947   enum machine_mode inner = GET_MODE_INNER (mode);
6948   unsigned int i, n_elts = GET_MODE_NUNITS (mode);
6949   rtx mem;
6950
6951   gcc_assert (VECTOR_MODE_P (mode));
6952
6953   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
6954   for (i = 0; i < n_elts; i++)
6955     emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
6956                    XVECEXP (vals, 0, i));
6957
6958   emit_move_insn (target, mem);
6959 }
6960
6961 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
6962    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
6963    reported source locations are bogus.  */
6964
6965 static void
6966 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
6967               const char *err)
6968 {
6969   HOST_WIDE_INT lane;
6970
6971   gcc_assert (GET_CODE (operand) == CONST_INT);
6972
6973   lane = INTVAL (operand);
6974
6975   if (lane < low || lane >= high)
6976     error (err);
6977 }
6978
6979 /* Bounds-check lanes.  */
6980
6981 void
6982 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6983 {
6984   bounds_check (operand, low, high, "lane out of range");
6985 }
6986
6987 /* Bounds-check constants.  */
6988
6989 void
6990 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6991 {
6992   bounds_check (operand, low, high, "constant out of range");
6993 }
6994
6995 HOST_WIDE_INT
6996 neon_element_bits (enum machine_mode mode)
6997 {
6998   if (mode == DImode)
6999     return GET_MODE_BITSIZE (mode);
7000   else
7001     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
7002 }
7003
7004 \f
7005 /* Predicates for `match_operand' and `match_operator'.  */
7006
7007 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
7008 int
7009 cirrus_memory_offset (rtx op)
7010 {
7011   /* Reject eliminable registers.  */
7012   if (! (reload_in_progress || reload_completed)
7013       && (   reg_mentioned_p (frame_pointer_rtx, op)
7014           || reg_mentioned_p (arg_pointer_rtx, op)
7015           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7016           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7017           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7018           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7019     return 0;
7020
7021   if (GET_CODE (op) == MEM)
7022     {
7023       rtx ind;
7024
7025       ind = XEXP (op, 0);
7026
7027       /* Match: (mem (reg)).  */
7028       if (GET_CODE (ind) == REG)
7029         return 1;
7030
7031       /* Match:
7032          (mem (plus (reg)
7033                     (const))).  */
7034       if (GET_CODE (ind) == PLUS
7035           && GET_CODE (XEXP (ind, 0)) == REG
7036           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
7037           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
7038         return 1;
7039     }
7040
7041   return 0;
7042 }
7043
7044 /* Return TRUE if OP is a valid coprocessor memory address pattern.
7045    WB is true if full writeback address modes are allowed and is false
7046    if limited writeback address modes (POST_INC and PRE_DEC) are
7047    allowed.  */
7048
7049 int
7050 arm_coproc_mem_operand (rtx op, bool wb)
7051 {
7052   rtx ind;
7053
7054   /* Reject eliminable registers.  */
7055   if (! (reload_in_progress || reload_completed)
7056       && (   reg_mentioned_p (frame_pointer_rtx, op)
7057           || reg_mentioned_p (arg_pointer_rtx, op)
7058           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7059           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7060           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7061           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7062     return FALSE;
7063
7064   /* Constants are converted into offsets from labels.  */
7065   if (GET_CODE (op) != MEM)
7066     return FALSE;
7067
7068   ind = XEXP (op, 0);
7069
7070   if (reload_completed
7071       && (GET_CODE (ind) == LABEL_REF
7072           || (GET_CODE (ind) == CONST
7073               && GET_CODE (XEXP (ind, 0)) == PLUS
7074               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
7075               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
7076     return TRUE;
7077
7078   /* Match: (mem (reg)).  */
7079   if (GET_CODE (ind) == REG)
7080     return arm_address_register_rtx_p (ind, 0);
7081
7082   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
7083      acceptable in any case (subject to verification by
7084      arm_address_register_rtx_p).  We need WB to be true to accept
7085      PRE_INC and POST_DEC.  */
7086   if (GET_CODE (ind) == POST_INC
7087       || GET_CODE (ind) == PRE_DEC
7088       || (wb
7089           && (GET_CODE (ind) == PRE_INC
7090               || GET_CODE (ind) == POST_DEC)))
7091     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
7092
7093   if (wb
7094       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
7095       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
7096       && GET_CODE (XEXP (ind, 1)) == PLUS
7097       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
7098     ind = XEXP (ind, 1);
7099
7100   /* Match:
7101      (plus (reg)
7102            (const)).  */
7103   if (GET_CODE (ind) == PLUS
7104       && GET_CODE (XEXP (ind, 0)) == REG
7105       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
7106       && GET_CODE (XEXP (ind, 1)) == CONST_INT
7107       && INTVAL (XEXP (ind, 1)) > -1024
7108       && INTVAL (XEXP (ind, 1)) <  1024
7109       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
7110     return TRUE;
7111
7112   return FALSE;
7113 }
7114
7115 /* Return TRUE if OP is a memory operand which we can load or store a vector
7116    to/from. TYPE is one of the following values:
7117     0 - Vector load/stor (vldr)
7118     1 - Core registers (ldm)
7119     2 - Element/structure loads (vld1)
7120  */
7121 int
7122 neon_vector_mem_operand (rtx op, int type)
7123 {
7124   rtx ind;
7125
7126   /* Reject eliminable registers.  */
7127   if (! (reload_in_progress || reload_completed)
7128       && (   reg_mentioned_p (frame_pointer_rtx, op)
7129           || reg_mentioned_p (arg_pointer_rtx, op)
7130           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7131           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7132           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7133           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7134     return FALSE;
7135
7136   /* Constants are converted into offsets from labels.  */
7137   if (GET_CODE (op) != MEM)
7138     return FALSE;
7139
7140   ind = XEXP (op, 0);
7141
7142   if (reload_completed
7143       && (GET_CODE (ind) == LABEL_REF
7144           || (GET_CODE (ind) == CONST
7145               && GET_CODE (XEXP (ind, 0)) == PLUS
7146               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
7147               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
7148     return TRUE;
7149
7150   /* Match: (mem (reg)).  */
7151   if (GET_CODE (ind) == REG)
7152     return arm_address_register_rtx_p (ind, 0);
7153
7154   /* Allow post-increment with Neon registers.  */
7155   if (type != 1 && (GET_CODE (ind) == POST_INC || GET_CODE (ind) == PRE_DEC))
7156     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
7157
7158   /* FIXME: vld1 allows register post-modify.  */
7159
7160   /* Match:
7161      (plus (reg)
7162           (const)).  */
7163   if (type == 0
7164       && GET_CODE (ind) == PLUS
7165       && GET_CODE (XEXP (ind, 0)) == REG
7166       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
7167       && GET_CODE (XEXP (ind, 1)) == CONST_INT
7168       && INTVAL (XEXP (ind, 1)) > -1024
7169       && INTVAL (XEXP (ind, 1)) < 1016
7170       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
7171     return TRUE;
7172
7173   return FALSE;
7174 }
7175
7176 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
7177    type.  */
7178 int
7179 neon_struct_mem_operand (rtx op)
7180 {
7181   rtx ind;
7182
7183   /* Reject eliminable registers.  */
7184   if (! (reload_in_progress || reload_completed)
7185       && (   reg_mentioned_p (frame_pointer_rtx, op)
7186           || reg_mentioned_p (arg_pointer_rtx, op)
7187           || reg_mentioned_p (virtual_incoming_args_rtx, op)
7188           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
7189           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
7190           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
7191     return FALSE;
7192
7193   /* Constants are converted into offsets from labels.  */
7194   if (GET_CODE (op) != MEM)
7195     return FALSE;
7196
7197   ind = XEXP (op, 0);
7198
7199   if (reload_completed
7200       && (GET_CODE (ind) == LABEL_REF
7201           || (GET_CODE (ind) == CONST
7202               && GET_CODE (XEXP (ind, 0)) == PLUS
7203               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
7204               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
7205     return TRUE;
7206
7207   /* Match: (mem (reg)).  */
7208   if (GET_CODE (ind) == REG)
7209     return arm_address_register_rtx_p (ind, 0);
7210
7211   return FALSE;
7212 }
7213
7214 /* Return true if X is a register that will be eliminated later on.  */
7215 int
7216 arm_eliminable_register (rtx x)
7217 {
7218   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
7219                        || REGNO (x) == ARG_POINTER_REGNUM
7220                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7221                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
7222 }
7223
7224 /* Return GENERAL_REGS if a scratch register required to reload x to/from
7225    coprocessor registers.  Otherwise return NO_REGS.  */
7226
7227 enum reg_class
7228 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
7229 {
7230   if (mode == HFmode)
7231     {
7232       if (s_register_operand (x, mode) || neon_vector_mem_operand (x, 2))
7233         return NO_REGS;
7234       return GENERAL_REGS;
7235     }
7236
7237   if (TARGET_NEON
7238       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
7239           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
7240       && neon_vector_mem_operand (x, 0))
7241      return NO_REGS;
7242
7243   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
7244     return NO_REGS;
7245
7246   return GENERAL_REGS;
7247 }
7248
7249 /* Values which must be returned in the most-significant end of the return
7250    register.  */
7251
7252 static bool
7253 arm_return_in_msb (const_tree valtype)
7254 {
7255   return (TARGET_AAPCS_BASED
7256           && BYTES_BIG_ENDIAN
7257           && (AGGREGATE_TYPE_P (valtype)
7258               || TREE_CODE (valtype) == COMPLEX_TYPE));
7259 }
7260
7261 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
7262    Use by the Cirrus Maverick code which has to workaround
7263    a hardware bug triggered by such instructions.  */
7264 static bool
7265 arm_memory_load_p (rtx insn)
7266 {
7267   rtx body, lhs, rhs;;
7268
7269   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
7270     return false;
7271
7272   body = PATTERN (insn);
7273
7274   if (GET_CODE (body) != SET)
7275     return false;
7276
7277   lhs = XEXP (body, 0);
7278   rhs = XEXP (body, 1);
7279
7280   lhs = REG_OR_SUBREG_RTX (lhs);
7281
7282   /* If the destination is not a general purpose
7283      register we do not have to worry.  */
7284   if (GET_CODE (lhs) != REG
7285       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
7286     return false;
7287
7288   /* As well as loads from memory we also have to react
7289      to loads of invalid constants which will be turned
7290      into loads from the minipool.  */
7291   return (GET_CODE (rhs) == MEM
7292           || GET_CODE (rhs) == SYMBOL_REF
7293           || note_invalid_constants (insn, -1, false));
7294 }
7295
7296 /* Return TRUE if INSN is a Cirrus instruction.  */
7297 static bool
7298 arm_cirrus_insn_p (rtx insn)
7299 {
7300   enum attr_cirrus attr;
7301
7302   /* get_attr cannot accept USE or CLOBBER.  */
7303   if (!insn
7304       || GET_CODE (insn) != INSN
7305       || GET_CODE (PATTERN (insn)) == USE
7306       || GET_CODE (PATTERN (insn)) == CLOBBER)
7307     return 0;
7308
7309   attr = get_attr_cirrus (insn);
7310
7311   return attr != CIRRUS_NOT;
7312 }
7313
7314 /* Cirrus reorg for invalid instruction combinations.  */
7315 static void
7316 cirrus_reorg (rtx first)
7317 {
7318   enum attr_cirrus attr;
7319   rtx body = PATTERN (first);
7320   rtx t;
7321   int nops;
7322
7323   /* Any branch must be followed by 2 non Cirrus instructions.  */
7324   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
7325     {
7326       nops = 0;
7327       t = next_nonnote_insn (first);
7328
7329       if (arm_cirrus_insn_p (t))
7330         ++ nops;
7331
7332       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
7333         ++ nops;
7334
7335       while (nops --)
7336         emit_insn_after (gen_nop (), first);
7337
7338       return;
7339     }
7340
7341   /* (float (blah)) is in parallel with a clobber.  */
7342   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
7343     body = XVECEXP (body, 0, 0);
7344
7345   if (GET_CODE (body) == SET)
7346     {
7347       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
7348
7349       /* cfldrd, cfldr64, cfstrd, cfstr64 must
7350          be followed by a non Cirrus insn.  */
7351       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
7352         {
7353           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
7354             emit_insn_after (gen_nop (), first);
7355
7356           return;
7357         }
7358       else if (arm_memory_load_p (first))
7359         {
7360           unsigned int arm_regno;
7361
7362           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
7363              ldr/cfmv64hr combination where the Rd field is the same
7364              in both instructions must be split with a non Cirrus
7365              insn.  Example:
7366
7367              ldr r0, blah
7368              nop
7369              cfmvsr mvf0, r0.  */
7370
7371           /* Get Arm register number for ldr insn.  */
7372           if (GET_CODE (lhs) == REG)
7373             arm_regno = REGNO (lhs);
7374           else
7375             {
7376               gcc_assert (GET_CODE (rhs) == REG);
7377               arm_regno = REGNO (rhs);
7378             }
7379
7380           /* Next insn.  */
7381           first = next_nonnote_insn (first);
7382
7383           if (! arm_cirrus_insn_p (first))
7384             return;
7385
7386           body = PATTERN (first);
7387
7388           /* (float (blah)) is in parallel with a clobber.  */
7389           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
7390             body = XVECEXP (body, 0, 0);
7391
7392           if (GET_CODE (body) == FLOAT)
7393             body = XEXP (body, 0);
7394
7395           if (get_attr_cirrus (first) == CIRRUS_MOVE
7396               && GET_CODE (XEXP (body, 1)) == REG
7397               && arm_regno == REGNO (XEXP (body, 1)))
7398             emit_insn_after (gen_nop (), first);
7399
7400           return;
7401         }
7402     }
7403
7404   /* get_attr cannot accept USE or CLOBBER.  */
7405   if (!first
7406       || GET_CODE (first) != INSN
7407       || GET_CODE (PATTERN (first)) == USE
7408       || GET_CODE (PATTERN (first)) == CLOBBER)
7409     return;
7410
7411   attr = get_attr_cirrus (first);
7412
7413   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
7414      must be followed by a non-coprocessor instruction.  */
7415   if (attr == CIRRUS_COMPARE)
7416     {
7417       nops = 0;
7418
7419       t = next_nonnote_insn (first);
7420
7421       if (arm_cirrus_insn_p (t))
7422         ++ nops;
7423
7424       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
7425         ++ nops;
7426
7427       while (nops --)
7428         emit_insn_after (gen_nop (), first);
7429
7430       return;
7431     }
7432 }
7433
7434 /* Return TRUE if X references a SYMBOL_REF.  */
7435 int
7436 symbol_mentioned_p (rtx x)
7437 {
7438   const char * fmt;
7439   int i;
7440
7441   if (GET_CODE (x) == SYMBOL_REF)
7442     return 1;
7443
7444   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
7445      are constant offsets, not symbols.  */
7446   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
7447     return 0;
7448
7449   fmt = GET_RTX_FORMAT (GET_CODE (x));
7450
7451   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7452     {
7453       if (fmt[i] == 'E')
7454         {
7455           int j;
7456
7457           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7458             if (symbol_mentioned_p (XVECEXP (x, i, j)))
7459               return 1;
7460         }
7461       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
7462         return 1;
7463     }
7464
7465   return 0;
7466 }
7467
7468 /* Return TRUE if X references a LABEL_REF.  */
7469 int
7470 label_mentioned_p (rtx x)
7471 {
7472   const char * fmt;
7473   int i;
7474
7475   if (GET_CODE (x) == LABEL_REF)
7476     return 1;
7477
7478   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
7479      instruction, but they are constant offsets, not symbols.  */
7480   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
7481     return 0;
7482
7483   fmt = GET_RTX_FORMAT (GET_CODE (x));
7484   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7485     {
7486       if (fmt[i] == 'E')
7487         {
7488           int j;
7489
7490           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7491             if (label_mentioned_p (XVECEXP (x, i, j)))
7492               return 1;
7493         }
7494       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
7495         return 1;
7496     }
7497
7498   return 0;
7499 }
7500
7501 int
7502 tls_mentioned_p (rtx x)
7503 {
7504   switch (GET_CODE (x))
7505     {
7506     case CONST:
7507       return tls_mentioned_p (XEXP (x, 0));
7508
7509     case UNSPEC:
7510       if (XINT (x, 1) == UNSPEC_TLS)
7511         return 1;
7512
7513     default:
7514       return 0;
7515     }
7516 }
7517
7518 /* Must not copy a SET whose source operand is PC-relative.  */
7519
7520 static bool
7521 arm_cannot_copy_insn_p (rtx insn)
7522 {
7523   rtx pat = PATTERN (insn);
7524
7525   if (GET_CODE (pat) == SET)
7526     {
7527       rtx rhs = SET_SRC (pat);
7528
7529       if (GET_CODE (rhs) == UNSPEC
7530           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
7531         return TRUE;
7532
7533       if (GET_CODE (rhs) == MEM
7534           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
7535           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
7536         return TRUE;
7537     }
7538
7539   return FALSE;
7540 }
7541
7542 enum rtx_code
7543 minmax_code (rtx x)
7544 {
7545   enum rtx_code code = GET_CODE (x);
7546
7547   switch (code)
7548     {
7549     case SMAX:
7550       return GE;
7551     case SMIN:
7552       return LE;
7553     case UMIN:
7554       return LEU;
7555     case UMAX:
7556       return GEU;
7557     default:
7558       gcc_unreachable ();
7559     }
7560 }
7561
7562 /* Return 1 if memory locations are adjacent.  */
7563 int
7564 adjacent_mem_locations (rtx a, rtx b)
7565 {
7566   /* We don't guarantee to preserve the order of these memory refs.  */
7567   if (volatile_refs_p (a) || volatile_refs_p (b))
7568     return 0;
7569
7570   if ((GET_CODE (XEXP (a, 0)) == REG
7571        || (GET_CODE (XEXP (a, 0)) == PLUS
7572            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
7573       && (GET_CODE (XEXP (b, 0)) == REG
7574           || (GET_CODE (XEXP (b, 0)) == PLUS
7575               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
7576     {
7577       HOST_WIDE_INT val0 = 0, val1 = 0;
7578       rtx reg0, reg1;
7579       int val_diff;
7580
7581       if (GET_CODE (XEXP (a, 0)) == PLUS)
7582         {
7583           reg0 = XEXP (XEXP (a, 0), 0);
7584           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
7585         }
7586       else
7587         reg0 = XEXP (a, 0);
7588
7589       if (GET_CODE (XEXP (b, 0)) == PLUS)
7590         {
7591           reg1 = XEXP (XEXP (b, 0), 0);
7592           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
7593         }
7594       else
7595         reg1 = XEXP (b, 0);
7596
7597       /* Don't accept any offset that will require multiple
7598          instructions to handle, since this would cause the
7599          arith_adjacentmem pattern to output an overlong sequence.  */
7600       if (!const_ok_for_op (val0, PLUS) || !const_ok_for_op (val1, PLUS))
7601         return 0;
7602
7603       /* Don't allow an eliminable register: register elimination can make
7604          the offset too large.  */
7605       if (arm_eliminable_register (reg0))
7606         return 0;
7607
7608       val_diff = val1 - val0;
7609
7610       if (arm_ld_sched)
7611         {
7612           /* If the target has load delay slots, then there's no benefit
7613              to using an ldm instruction unless the offset is zero and
7614              we are optimizing for size.  */
7615           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
7616                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
7617                   && (val_diff == 4 || val_diff == -4));
7618         }
7619
7620       return ((REGNO (reg0) == REGNO (reg1))
7621               && (val_diff == 4 || val_diff == -4));
7622     }
7623
7624   return 0;
7625 }
7626
7627 int
7628 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7629                         HOST_WIDE_INT *load_offset)
7630 {
7631   int unsorted_regs[4];
7632   HOST_WIDE_INT unsorted_offsets[4];
7633   int order[4];
7634   int base_reg = -1;
7635   int i;
7636
7637   /* Can only handle 2, 3, or 4 insns at present,
7638      though could be easily extended if required.  */
7639   gcc_assert (nops >= 2 && nops <= 4);
7640
7641   memset (order, 0, 4 * sizeof (int));
7642
7643   /* Loop over the operands and check that the memory references are
7644      suitable (i.e. immediate offsets from the same base register).  At
7645      the same time, extract the target register, and the memory
7646      offsets.  */
7647   for (i = 0; i < nops; i++)
7648     {
7649       rtx reg;
7650       rtx offset;
7651
7652       /* Convert a subreg of a mem into the mem itself.  */
7653       if (GET_CODE (operands[nops + i]) == SUBREG)
7654         operands[nops + i] = alter_subreg (operands + (nops + i));
7655
7656       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7657
7658       /* Don't reorder volatile memory references; it doesn't seem worth
7659          looking for the case where the order is ok anyway.  */
7660       if (MEM_VOLATILE_P (operands[nops + i]))
7661         return 0;
7662
7663       offset = const0_rtx;
7664
7665       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7666            || (GET_CODE (reg) == SUBREG
7667                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7668           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7669               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7670                    == REG)
7671                   || (GET_CODE (reg) == SUBREG
7672                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7673               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7674                   == CONST_INT)))
7675         {
7676           if (i == 0)
7677             {
7678               base_reg = REGNO (reg);
7679               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7680                                   ? REGNO (operands[i])
7681                                   : REGNO (SUBREG_REG (operands[i])));
7682               order[0] = 0;
7683             }
7684           else
7685             {
7686               if (base_reg != (int) REGNO (reg))
7687                 /* Not addressed from the same base register.  */
7688                 return 0;
7689
7690               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7691                                   ? REGNO (operands[i])
7692                                   : REGNO (SUBREG_REG (operands[i])));
7693               if (unsorted_regs[i] < unsorted_regs[order[0]])
7694                 order[0] = i;
7695             }
7696
7697           /* If it isn't an integer register, or if it overwrites the
7698              base register but isn't the last insn in the list, then
7699              we can't do this.  */
7700           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
7701               || (i != nops - 1 && unsorted_regs[i] == base_reg))
7702             return 0;
7703
7704           unsorted_offsets[i] = INTVAL (offset);
7705         }
7706       else
7707         /* Not a suitable memory address.  */
7708         return 0;
7709     }
7710
7711   /* All the useful information has now been extracted from the
7712      operands into unsorted_regs and unsorted_offsets; additionally,
7713      order[0] has been set to the lowest numbered register in the
7714      list.  Sort the registers into order, and check that the memory
7715      offsets are ascending and adjacent.  */
7716
7717   for (i = 1; i < nops; i++)
7718     {
7719       int j;
7720
7721       order[i] = order[i - 1];
7722       for (j = 0; j < nops; j++)
7723         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7724             && (order[i] == order[i - 1]
7725                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7726           order[i] = j;
7727
7728       /* Have we found a suitable register? if not, one must be used more
7729          than once.  */
7730       if (order[i] == order[i - 1])
7731         return 0;
7732
7733       /* Is the memory address adjacent and ascending? */
7734       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7735         return 0;
7736     }
7737
7738   if (base)
7739     {
7740       *base = base_reg;
7741
7742       for (i = 0; i < nops; i++)
7743         regs[i] = unsorted_regs[order[i]];
7744
7745       *load_offset = unsorted_offsets[order[0]];
7746     }
7747
7748   if (unsorted_offsets[order[0]] == 0)
7749     return 1; /* ldmia */
7750
7751   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
7752     return 2; /* ldmib */
7753
7754   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
7755     return 3; /* ldmda */
7756
7757   if (unsorted_offsets[order[nops - 1]] == -4)
7758     return 4; /* ldmdb */
7759
7760   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
7761      if the offset isn't small enough.  The reason 2 ldrs are faster
7762      is because these ARMs are able to do more than one cache access
7763      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
7764      whilst the ARM8 has a double bandwidth cache.  This means that
7765      these cores can do both an instruction fetch and a data fetch in
7766      a single cycle, so the trick of calculating the address into a
7767      scratch register (one of the result regs) and then doing a load
7768      multiple actually becomes slower (and no smaller in code size).
7769      That is the transformation
7770
7771         ldr     rd1, [rbase + offset]
7772         ldr     rd2, [rbase + offset + 4]
7773
7774      to
7775
7776         add     rd1, rbase, offset
7777         ldmia   rd1, {rd1, rd2}
7778
7779      produces worse code -- '3 cycles + any stalls on rd2' instead of
7780      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
7781      access per cycle, the first sequence could never complete in less
7782      than 6 cycles, whereas the ldm sequence would only take 5 and
7783      would make better use of sequential accesses if not hitting the
7784      cache.
7785
7786      We cheat here and test 'arm_ld_sched' which we currently know to
7787      only be true for the ARM8, ARM9 and StrongARM.  If this ever
7788      changes, then the test below needs to be reworked.  */
7789   if (nops == 2 && arm_ld_sched)
7790     return 0;
7791
7792   /* Can't do it without setting up the offset, only do this if it takes
7793      no more than one insn.  */
7794   return (const_ok_for_arm (unsorted_offsets[order[0]])
7795           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
7796 }
7797
7798 const char *
7799 emit_ldm_seq (rtx *operands, int nops)
7800 {
7801   int regs[4];
7802   int base_reg;
7803   HOST_WIDE_INT offset;
7804   char buf[100];
7805   int i;
7806
7807   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7808     {
7809     case 1:
7810       strcpy (buf, "ldm%(ia%)\t");
7811       break;
7812
7813     case 2:
7814       strcpy (buf, "ldm%(ib%)\t");
7815       break;
7816
7817     case 3:
7818       strcpy (buf, "ldm%(da%)\t");
7819       break;
7820
7821     case 4:
7822       strcpy (buf, "ldm%(db%)\t");
7823       break;
7824
7825     case 5:
7826       if (offset >= 0)
7827         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7828                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7829                  (long) offset);
7830       else
7831         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7832                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7833                  (long) -offset);
7834       output_asm_insn (buf, operands);
7835       base_reg = regs[0];
7836       strcpy (buf, "ldm%(ia%)\t");
7837       break;
7838
7839     default:
7840       gcc_unreachable ();
7841     }
7842
7843   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7844            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7845
7846   for (i = 1; i < nops; i++)
7847     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7848              reg_names[regs[i]]);
7849
7850   strcat (buf, "}\t%@ phole ldm");
7851
7852   output_asm_insn (buf, operands);
7853   return "";
7854 }
7855
7856 int
7857 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7858                          HOST_WIDE_INT * load_offset)
7859 {
7860   int unsorted_regs[4];
7861   HOST_WIDE_INT unsorted_offsets[4];
7862   int order[4];
7863   int base_reg = -1;
7864   int i;
7865
7866   /* Can only handle 2, 3, or 4 insns at present, though could be easily
7867      extended if required.  */
7868   gcc_assert (nops >= 2 && nops <= 4);
7869
7870   memset (order, 0, 4 * sizeof (int));
7871
7872   /* Loop over the operands and check that the memory references are
7873      suitable (i.e. immediate offsets from the same base register).  At
7874      the same time, extract the target register, and the memory
7875      offsets.  */
7876   for (i = 0; i < nops; i++)
7877     {
7878       rtx reg;
7879       rtx offset;
7880
7881       /* Convert a subreg of a mem into the mem itself.  */
7882       if (GET_CODE (operands[nops + i]) == SUBREG)
7883         operands[nops + i] = alter_subreg (operands + (nops + i));
7884
7885       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7886
7887       /* Don't reorder volatile memory references; it doesn't seem worth
7888          looking for the case where the order is ok anyway.  */
7889       if (MEM_VOLATILE_P (operands[nops + i]))
7890         return 0;
7891
7892       offset = const0_rtx;
7893
7894       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7895            || (GET_CODE (reg) == SUBREG
7896                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7897           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7898               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7899                    == REG)
7900                   || (GET_CODE (reg) == SUBREG
7901                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7902               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7903                   == CONST_INT)))
7904         {
7905           if (i == 0)
7906             {
7907               base_reg = REGNO (reg);
7908               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7909                                   ? REGNO (operands[i])
7910                                   : REGNO (SUBREG_REG (operands[i])));
7911               order[0] = 0;
7912             }
7913           else
7914             {
7915               if (base_reg != (int) REGNO (reg))
7916                 /* Not addressed from the same base register.  */
7917                 return 0;
7918
7919               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7920                                   ? REGNO (operands[i])
7921                                   : REGNO (SUBREG_REG (operands[i])));
7922               if (unsorted_regs[i] < unsorted_regs[order[0]])
7923                 order[0] = i;
7924             }
7925
7926           /* If it isn't an integer register, then we can't do this.  */
7927           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
7928             return 0;
7929
7930           unsorted_offsets[i] = INTVAL (offset);
7931         }
7932       else
7933         /* Not a suitable memory address.  */
7934         return 0;
7935     }
7936
7937   /* All the useful information has now been extracted from the
7938      operands into unsorted_regs and unsorted_offsets; additionally,
7939      order[0] has been set to the lowest numbered register in the
7940      list.  Sort the registers into order, and check that the memory
7941      offsets are ascending and adjacent.  */
7942
7943   for (i = 1; i < nops; i++)
7944     {
7945       int j;
7946
7947       order[i] = order[i - 1];
7948       for (j = 0; j < nops; j++)
7949         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7950             && (order[i] == order[i - 1]
7951                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7952           order[i] = j;
7953
7954       /* Have we found a suitable register? if not, one must be used more
7955          than once.  */
7956       if (order[i] == order[i - 1])
7957         return 0;
7958
7959       /* Is the memory address adjacent and ascending? */
7960       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7961         return 0;
7962     }
7963
7964   if (base)
7965     {
7966       *base = base_reg;
7967
7968       for (i = 0; i < nops; i++)
7969         regs[i] = unsorted_regs[order[i]];
7970
7971       *load_offset = unsorted_offsets[order[0]];
7972     }
7973
7974   if (unsorted_offsets[order[0]] == 0)
7975     return 1; /* stmia */
7976
7977   if (unsorted_offsets[order[0]] == 4)
7978     return 2; /* stmib */
7979
7980   if (unsorted_offsets[order[nops - 1]] == 0)
7981     return 3; /* stmda */
7982
7983   if (unsorted_offsets[order[nops - 1]] == -4)
7984     return 4; /* stmdb */
7985
7986   return 0;
7987 }
7988
7989 const char *
7990 emit_stm_seq (rtx *operands, int nops)
7991 {
7992   int regs[4];
7993   int base_reg;
7994   HOST_WIDE_INT offset;
7995   char buf[100];
7996   int i;
7997
7998   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7999     {
8000     case 1:
8001       strcpy (buf, "stm%(ia%)\t");
8002       break;
8003
8004     case 2:
8005       strcpy (buf, "stm%(ib%)\t");
8006       break;
8007
8008     case 3:
8009       strcpy (buf, "stm%(da%)\t");
8010       break;
8011
8012     case 4:
8013       strcpy (buf, "stm%(db%)\t");
8014       break;
8015
8016     default:
8017       gcc_unreachable ();
8018     }
8019
8020   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
8021            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
8022
8023   for (i = 1; i < nops; i++)
8024     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
8025              reg_names[regs[i]]);
8026
8027   strcat (buf, "}\t%@ phole stm");
8028
8029   output_asm_insn (buf, operands);
8030   return "";
8031 }
8032 \f
8033 /* Routines for use in generating RTL.  */
8034
8035 rtx
8036 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
8037                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
8038 {
8039   HOST_WIDE_INT offset = *offsetp;
8040   int i = 0, j;
8041   rtx result;
8042   int sign = up ? 1 : -1;
8043   rtx mem, addr;
8044
8045   /* XScale has load-store double instructions, but they have stricter
8046      alignment requirements than load-store multiple, so we cannot
8047      use them.
8048
8049      For XScale ldm requires 2 + NREGS cycles to complete and blocks
8050      the pipeline until completion.
8051
8052         NREGS           CYCLES
8053           1               3
8054           2               4
8055           3               5
8056           4               6
8057
8058      An ldr instruction takes 1-3 cycles, but does not block the
8059      pipeline.
8060
8061         NREGS           CYCLES
8062           1              1-3
8063           2              2-6
8064           3              3-9
8065           4              4-12
8066
8067      Best case ldr will always win.  However, the more ldr instructions
8068      we issue, the less likely we are to be able to schedule them well.
8069      Using ldr instructions also increases code size.
8070
8071      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
8072      for counts of 3 or 4 regs.  */
8073   if (arm_tune_xscale && count <= 2 && ! optimize_size)
8074     {
8075       rtx seq;
8076
8077       start_sequence ();
8078
8079       for (i = 0; i < count; i++)
8080         {
8081           addr = plus_constant (from, i * 4 * sign);
8082           mem = adjust_automodify_address (basemem, SImode, addr, offset);
8083           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
8084           offset += 4 * sign;
8085         }
8086
8087       if (write_back)
8088         {
8089           emit_move_insn (from, plus_constant (from, count * 4 * sign));
8090           *offsetp = offset;
8091         }
8092
8093       seq = get_insns ();
8094       end_sequence ();
8095
8096       return seq;
8097     }
8098
8099   result = gen_rtx_PARALLEL (VOIDmode,
8100                              rtvec_alloc (count + (write_back ? 1 : 0)));
8101   if (write_back)
8102     {
8103       XVECEXP (result, 0, 0)
8104         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
8105       i = 1;
8106       count++;
8107     }
8108
8109   for (j = 0; i < count; i++, j++)
8110     {
8111       addr = plus_constant (from, j * 4 * sign);
8112       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
8113       XVECEXP (result, 0, i)
8114         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
8115       offset += 4 * sign;
8116     }
8117
8118   if (write_back)
8119     *offsetp = offset;
8120
8121   return result;
8122 }
8123
8124 rtx
8125 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
8126                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
8127 {
8128   HOST_WIDE_INT offset = *offsetp;
8129   int i = 0, j;
8130   rtx result;
8131   int sign = up ? 1 : -1;
8132   rtx mem, addr;
8133
8134   /* See arm_gen_load_multiple for discussion of
8135      the pros/cons of ldm/stm usage for XScale.  */
8136   if (arm_tune_xscale && count <= 2 && ! optimize_size)
8137     {
8138       rtx seq;
8139
8140       start_sequence ();
8141
8142       for (i = 0; i < count; i++)
8143         {
8144           addr = plus_constant (to, i * 4 * sign);
8145           mem = adjust_automodify_address (basemem, SImode, addr, offset);
8146           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
8147           offset += 4 * sign;
8148         }
8149
8150       if (write_back)
8151         {
8152           emit_move_insn (to, plus_constant (to, count * 4 * sign));
8153           *offsetp = offset;
8154         }
8155
8156       seq = get_insns ();
8157       end_sequence ();
8158
8159       return seq;
8160     }
8161
8162   result = gen_rtx_PARALLEL (VOIDmode,
8163                              rtvec_alloc (count + (write_back ? 1 : 0)));
8164   if (write_back)
8165     {
8166       XVECEXP (result, 0, 0)
8167         = gen_rtx_SET (VOIDmode, to,
8168                        plus_constant (to, count * 4 * sign));
8169       i = 1;
8170       count++;
8171     }
8172
8173   for (j = 0; i < count; i++, j++)
8174     {
8175       addr = plus_constant (to, j * 4 * sign);
8176       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
8177       XVECEXP (result, 0, i)
8178         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
8179       offset += 4 * sign;
8180     }
8181
8182   if (write_back)
8183     *offsetp = offset;
8184
8185   return result;
8186 }
8187
8188 int
8189 arm_gen_movmemqi (rtx *operands)
8190 {
8191   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
8192   HOST_WIDE_INT srcoffset, dstoffset;
8193   int i;
8194   rtx src, dst, srcbase, dstbase;
8195   rtx part_bytes_reg = NULL;
8196   rtx mem;
8197
8198   if (GET_CODE (operands[2]) != CONST_INT
8199       || GET_CODE (operands[3]) != CONST_INT
8200       || INTVAL (operands[2]) > 64
8201       || INTVAL (operands[3]) & 3)
8202     return 0;
8203
8204   dstbase = operands[0];
8205   srcbase = operands[1];
8206
8207   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
8208   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
8209
8210   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
8211   out_words_to_go = INTVAL (operands[2]) / 4;
8212   last_bytes = INTVAL (operands[2]) & 3;
8213   dstoffset = srcoffset = 0;
8214
8215   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
8216     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
8217
8218   for (i = 0; in_words_to_go >= 2; i+=4)
8219     {
8220       if (in_words_to_go > 4)
8221         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
8222                                           srcbase, &srcoffset));
8223       else
8224         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
8225                                           FALSE, srcbase, &srcoffset));
8226
8227       if (out_words_to_go)
8228         {
8229           if (out_words_to_go > 4)
8230             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
8231                                                dstbase, &dstoffset));
8232           else if (out_words_to_go != 1)
8233             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
8234                                                dst, TRUE,
8235                                                (last_bytes == 0
8236                                                 ? FALSE : TRUE),
8237                                                dstbase, &dstoffset));
8238           else
8239             {
8240               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
8241               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
8242               if (last_bytes != 0)
8243                 {
8244                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
8245                   dstoffset += 4;
8246                 }
8247             }
8248         }
8249
8250       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
8251       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
8252     }
8253
8254   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
8255   if (out_words_to_go)
8256     {
8257       rtx sreg;
8258
8259       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
8260       sreg = copy_to_reg (mem);
8261
8262       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
8263       emit_move_insn (mem, sreg);
8264       in_words_to_go--;
8265
8266       gcc_assert (!in_words_to_go);     /* Sanity check */
8267     }
8268
8269   if (in_words_to_go)
8270     {
8271       gcc_assert (in_words_to_go > 0);
8272
8273       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
8274       part_bytes_reg = copy_to_mode_reg (SImode, mem);
8275     }
8276
8277   gcc_assert (!last_bytes || part_bytes_reg);
8278
8279   if (BYTES_BIG_ENDIAN && last_bytes)
8280     {
8281       rtx tmp = gen_reg_rtx (SImode);
8282
8283       /* The bytes we want are in the top end of the word.  */
8284       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
8285                               GEN_INT (8 * (4 - last_bytes))));
8286       part_bytes_reg = tmp;
8287
8288       while (last_bytes)
8289         {
8290           mem = adjust_automodify_address (dstbase, QImode,
8291                                            plus_constant (dst, last_bytes - 1),
8292                                            dstoffset + last_bytes - 1);
8293           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
8294
8295           if (--last_bytes)
8296             {
8297               tmp = gen_reg_rtx (SImode);
8298               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
8299               part_bytes_reg = tmp;
8300             }
8301         }
8302
8303     }
8304   else
8305     {
8306       if (last_bytes > 1)
8307         {
8308           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
8309           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
8310           last_bytes -= 2;
8311           if (last_bytes)
8312             {
8313               rtx tmp = gen_reg_rtx (SImode);
8314               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
8315               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
8316               part_bytes_reg = tmp;
8317               dstoffset += 2;
8318             }
8319         }
8320
8321       if (last_bytes)
8322         {
8323           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
8324           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
8325         }
8326     }
8327
8328   return 1;
8329 }
8330
8331 /* Select a dominance comparison mode if possible for a test of the general
8332    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
8333    COND_OR == DOM_CC_X_AND_Y => (X && Y)
8334    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
8335    COND_OR == DOM_CC_X_OR_Y => (X || Y)
8336    In all cases OP will be either EQ or NE, but we don't need to know which
8337    here.  If we are unable to support a dominance comparison we return
8338    CC mode.  This will then fail to match for the RTL expressions that
8339    generate this call.  */
8340 enum machine_mode
8341 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
8342 {
8343   enum rtx_code cond1, cond2;
8344   int swapped = 0;
8345
8346   /* Currently we will probably get the wrong result if the individual
8347      comparisons are not simple.  This also ensures that it is safe to
8348      reverse a comparison if necessary.  */
8349   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
8350        != CCmode)
8351       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
8352           != CCmode))
8353     return CCmode;
8354
8355   /* The if_then_else variant of this tests the second condition if the
8356      first passes, but is true if the first fails.  Reverse the first
8357      condition to get a true "inclusive-or" expression.  */
8358   if (cond_or == DOM_CC_NX_OR_Y)
8359     cond1 = reverse_condition (cond1);
8360
8361   /* If the comparisons are not equal, and one doesn't dominate the other,
8362      then we can't do this.  */
8363   if (cond1 != cond2
8364       && !comparison_dominates_p (cond1, cond2)
8365       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
8366     return CCmode;
8367
8368   if (swapped)
8369     {
8370       enum rtx_code temp = cond1;
8371       cond1 = cond2;
8372       cond2 = temp;
8373     }
8374
8375   switch (cond1)
8376     {
8377     case EQ:
8378       if (cond_or == DOM_CC_X_AND_Y)
8379         return CC_DEQmode;
8380
8381       switch (cond2)
8382         {
8383         case EQ: return CC_DEQmode;
8384         case LE: return CC_DLEmode;
8385         case LEU: return CC_DLEUmode;
8386         case GE: return CC_DGEmode;
8387         case GEU: return CC_DGEUmode;
8388         default: gcc_unreachable ();
8389         }
8390
8391     case LT:
8392       if (cond_or == DOM_CC_X_AND_Y)
8393         return CC_DLTmode;
8394
8395       switch (cond2)
8396         {
8397         case  LT:
8398             return CC_DLTmode;
8399         case LE:
8400           return CC_DLEmode;
8401         case NE:
8402           return CC_DNEmode;
8403         default:
8404           gcc_unreachable ();
8405         }
8406
8407     case GT:
8408       if (cond_or == DOM_CC_X_AND_Y)
8409         return CC_DGTmode;
8410
8411       switch (cond2)
8412         {
8413         case GT:
8414           return CC_DGTmode;
8415         case GE:
8416           return CC_DGEmode;
8417         case NE:
8418           return CC_DNEmode;
8419         default:
8420           gcc_unreachable ();
8421         }
8422
8423     case LTU:
8424       if (cond_or == DOM_CC_X_AND_Y)
8425         return CC_DLTUmode;
8426
8427       switch (cond2)
8428         {
8429         case LTU:
8430           return CC_DLTUmode;
8431         case LEU:
8432           return CC_DLEUmode;
8433         case NE:
8434           return CC_DNEmode;
8435         default:
8436           gcc_unreachable ();
8437         }
8438
8439     case GTU:
8440       if (cond_or == DOM_CC_X_AND_Y)
8441         return CC_DGTUmode;
8442
8443       switch (cond2)
8444         {
8445         case GTU:
8446           return CC_DGTUmode;
8447         case GEU:
8448           return CC_DGEUmode;
8449         case NE:
8450           return CC_DNEmode;
8451         default:
8452           gcc_unreachable ();
8453         }
8454
8455     /* The remaining cases only occur when both comparisons are the
8456        same.  */
8457     case NE:
8458       gcc_assert (cond1 == cond2);
8459       return CC_DNEmode;
8460
8461     case LE:
8462       gcc_assert (cond1 == cond2);
8463       return CC_DLEmode;
8464
8465     case GE:
8466       gcc_assert (cond1 == cond2);
8467       return CC_DGEmode;
8468
8469     case LEU:
8470       gcc_assert (cond1 == cond2);
8471       return CC_DLEUmode;
8472
8473     case GEU:
8474       gcc_assert (cond1 == cond2);
8475       return CC_DGEUmode;
8476
8477     default:
8478       gcc_unreachable ();
8479     }
8480 }
8481
8482 enum machine_mode
8483 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
8484 {
8485   /* All floating point compares return CCFP if it is an equality
8486      comparison, and CCFPE otherwise.  */
8487   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
8488     {
8489       switch (op)
8490         {
8491         case EQ:
8492         case NE:
8493         case UNORDERED:
8494         case ORDERED:
8495         case UNLT:
8496         case UNLE:
8497         case UNGT:
8498         case UNGE:
8499         case UNEQ:
8500         case LTGT:
8501           return CCFPmode;
8502
8503         case LT:
8504         case LE:
8505         case GT:
8506         case GE:
8507           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
8508             return CCFPmode;
8509           return CCFPEmode;
8510
8511         default:
8512           gcc_unreachable ();
8513         }
8514     }
8515
8516   /* A compare with a shifted operand.  Because of canonicalization, the
8517      comparison will have to be swapped when we emit the assembler.  */
8518   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
8519       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
8520           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
8521           || GET_CODE (x) == ROTATERT))
8522     return CC_SWPmode;
8523
8524   /* This operation is performed swapped, but since we only rely on the Z
8525      flag we don't need an additional mode.  */
8526   if (GET_MODE (y) == SImode && REG_P (y)
8527       && GET_CODE (x) == NEG
8528       && (op == EQ || op == NE))
8529     return CC_Zmode;
8530
8531   /* This is a special case that is used by combine to allow a
8532      comparison of a shifted byte load to be split into a zero-extend
8533      followed by a comparison of the shifted integer (only valid for
8534      equalities and unsigned inequalities).  */
8535   if (GET_MODE (x) == SImode
8536       && GET_CODE (x) == ASHIFT
8537       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
8538       && GET_CODE (XEXP (x, 0)) == SUBREG
8539       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
8540       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
8541       && (op == EQ || op == NE
8542           || op == GEU || op == GTU || op == LTU || op == LEU)
8543       && GET_CODE (y) == CONST_INT)
8544     return CC_Zmode;
8545
8546   /* A construct for a conditional compare, if the false arm contains
8547      0, then both conditions must be true, otherwise either condition
8548      must be true.  Not all conditions are possible, so CCmode is
8549      returned if it can't be done.  */
8550   if (GET_CODE (x) == IF_THEN_ELSE
8551       && (XEXP (x, 2) == const0_rtx
8552           || XEXP (x, 2) == const1_rtx)
8553       && COMPARISON_P (XEXP (x, 0))
8554       && COMPARISON_P (XEXP (x, 1)))
8555     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8556                                          INTVAL (XEXP (x, 2)));
8557
8558   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
8559   if (GET_CODE (x) == AND
8560       && COMPARISON_P (XEXP (x, 0))
8561       && COMPARISON_P (XEXP (x, 1)))
8562     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8563                                          DOM_CC_X_AND_Y);
8564
8565   if (GET_CODE (x) == IOR
8566       && COMPARISON_P (XEXP (x, 0))
8567       && COMPARISON_P (XEXP (x, 1)))
8568     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8569                                          DOM_CC_X_OR_Y);
8570
8571   /* An operation (on Thumb) where we want to test for a single bit.
8572      This is done by shifting that bit up into the top bit of a
8573      scratch register; we can then branch on the sign bit.  */
8574   if (TARGET_THUMB1
8575       && GET_MODE (x) == SImode
8576       && (op == EQ || op == NE)
8577       && GET_CODE (x) == ZERO_EXTRACT
8578       && XEXP (x, 1) == const1_rtx)
8579     return CC_Nmode;
8580
8581   /* An operation that sets the condition codes as a side-effect, the
8582      V flag is not set correctly, so we can only use comparisons where
8583      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
8584      instead.)  */
8585   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
8586   if (GET_MODE (x) == SImode
8587       && y == const0_rtx
8588       && (op == EQ || op == NE || op == LT || op == GE)
8589       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
8590           || GET_CODE (x) == AND || GET_CODE (x) == IOR
8591           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
8592           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
8593           || GET_CODE (x) == LSHIFTRT
8594           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
8595           || GET_CODE (x) == ROTATERT
8596           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
8597     return CC_NOOVmode;
8598
8599   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
8600     return CC_Zmode;
8601
8602   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
8603       && GET_CODE (x) == PLUS
8604       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
8605     return CC_Cmode;
8606
8607   return CCmode;
8608 }
8609
8610 /* X and Y are two things to compare using CODE.  Emit the compare insn and
8611    return the rtx for register 0 in the proper mode.  FP means this is a
8612    floating point compare: I don't think that it is needed on the arm.  */
8613 rtx
8614 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
8615 {
8616   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
8617   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
8618
8619   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
8620
8621   return cc_reg;
8622 }
8623
8624 /* Generate a sequence of insns that will generate the correct return
8625    address mask depending on the physical architecture that the program
8626    is running on.  */
8627 rtx
8628 arm_gen_return_addr_mask (void)
8629 {
8630   rtx reg = gen_reg_rtx (Pmode);
8631
8632   emit_insn (gen_return_addr_mask (reg));
8633   return reg;
8634 }
8635
8636 void
8637 arm_reload_in_hi (rtx *operands)
8638 {
8639   rtx ref = operands[1];
8640   rtx base, scratch;
8641   HOST_WIDE_INT offset = 0;
8642
8643   if (GET_CODE (ref) == SUBREG)
8644     {
8645       offset = SUBREG_BYTE (ref);
8646       ref = SUBREG_REG (ref);
8647     }
8648
8649   if (GET_CODE (ref) == REG)
8650     {
8651       /* We have a pseudo which has been spilt onto the stack; there
8652          are two cases here: the first where there is a simple
8653          stack-slot replacement and a second where the stack-slot is
8654          out of range, or is used as a subreg.  */
8655       if (reg_equiv_mem[REGNO (ref)])
8656         {
8657           ref = reg_equiv_mem[REGNO (ref)];
8658           base = find_replacement (&XEXP (ref, 0));
8659         }
8660       else
8661         /* The slot is out of range, or was dressed up in a SUBREG.  */
8662         base = reg_equiv_address[REGNO (ref)];
8663     }
8664   else
8665     base = find_replacement (&XEXP (ref, 0));
8666
8667   /* Handle the case where the address is too complex to be offset by 1.  */
8668   if (GET_CODE (base) == MINUS
8669       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8670     {
8671       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8672
8673       emit_set_insn (base_plus, base);
8674       base = base_plus;
8675     }
8676   else if (GET_CODE (base) == PLUS)
8677     {
8678       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8679       HOST_WIDE_INT hi, lo;
8680
8681       offset += INTVAL (XEXP (base, 1));
8682       base = XEXP (base, 0);
8683
8684       /* Rework the address into a legal sequence of insns.  */
8685       /* Valid range for lo is -4095 -> 4095 */
8686       lo = (offset >= 0
8687             ? (offset & 0xfff)
8688             : -((-offset) & 0xfff));
8689
8690       /* Corner case, if lo is the max offset then we would be out of range
8691          once we have added the additional 1 below, so bump the msb into the
8692          pre-loading insn(s).  */
8693       if (lo == 4095)
8694         lo &= 0x7ff;
8695
8696       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8697              ^ (HOST_WIDE_INT) 0x80000000)
8698             - (HOST_WIDE_INT) 0x80000000);
8699
8700       gcc_assert (hi + lo == offset);
8701
8702       if (hi != 0)
8703         {
8704           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8705
8706           /* Get the base address; addsi3 knows how to handle constants
8707              that require more than one insn.  */
8708           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8709           base = base_plus;
8710           offset = lo;
8711         }
8712     }
8713
8714   /* Operands[2] may overlap operands[0] (though it won't overlap
8715      operands[1]), that's why we asked for a DImode reg -- so we can
8716      use the bit that does not overlap.  */
8717   if (REGNO (operands[2]) == REGNO (operands[0]))
8718     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8719   else
8720     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8721
8722   emit_insn (gen_zero_extendqisi2 (scratch,
8723                                    gen_rtx_MEM (QImode,
8724                                                 plus_constant (base,
8725                                                                offset))));
8726   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
8727                                    gen_rtx_MEM (QImode,
8728                                                 plus_constant (base,
8729                                                                offset + 1))));
8730   if (!BYTES_BIG_ENDIAN)
8731     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8732                    gen_rtx_IOR (SImode,
8733                                 gen_rtx_ASHIFT
8734                                 (SImode,
8735                                  gen_rtx_SUBREG (SImode, operands[0], 0),
8736                                  GEN_INT (8)),
8737                                 scratch));
8738   else
8739     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8740                    gen_rtx_IOR (SImode,
8741                                 gen_rtx_ASHIFT (SImode, scratch,
8742                                                 GEN_INT (8)),
8743                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
8744 }
8745
8746 /* Handle storing a half-word to memory during reload by synthesizing as two
8747    byte stores.  Take care not to clobber the input values until after we
8748    have moved them somewhere safe.  This code assumes that if the DImode
8749    scratch in operands[2] overlaps either the input value or output address
8750    in some way, then that value must die in this insn (we absolutely need
8751    two scratch registers for some corner cases).  */
8752 void
8753 arm_reload_out_hi (rtx *operands)
8754 {
8755   rtx ref = operands[0];
8756   rtx outval = operands[1];
8757   rtx base, scratch;
8758   HOST_WIDE_INT offset = 0;
8759
8760   if (GET_CODE (ref) == SUBREG)
8761     {
8762       offset = SUBREG_BYTE (ref);
8763       ref = SUBREG_REG (ref);
8764     }
8765
8766   if (GET_CODE (ref) == REG)
8767     {
8768       /* We have a pseudo which has been spilt onto the stack; there
8769          are two cases here: the first where there is a simple
8770          stack-slot replacement and a second where the stack-slot is
8771          out of range, or is used as a subreg.  */
8772       if (reg_equiv_mem[REGNO (ref)])
8773         {
8774           ref = reg_equiv_mem[REGNO (ref)];
8775           base = find_replacement (&XEXP (ref, 0));
8776         }
8777       else
8778         /* The slot is out of range, or was dressed up in a SUBREG.  */
8779         base = reg_equiv_address[REGNO (ref)];
8780     }
8781   else
8782     base = find_replacement (&XEXP (ref, 0));
8783
8784   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8785
8786   /* Handle the case where the address is too complex to be offset by 1.  */
8787   if (GET_CODE (base) == MINUS
8788       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8789     {
8790       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8791
8792       /* Be careful not to destroy OUTVAL.  */
8793       if (reg_overlap_mentioned_p (base_plus, outval))
8794         {
8795           /* Updating base_plus might destroy outval, see if we can
8796              swap the scratch and base_plus.  */
8797           if (!reg_overlap_mentioned_p (scratch, outval))
8798             {
8799               rtx tmp = scratch;
8800               scratch = base_plus;
8801               base_plus = tmp;
8802             }
8803           else
8804             {
8805               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8806
8807               /* Be conservative and copy OUTVAL into the scratch now,
8808                  this should only be necessary if outval is a subreg
8809                  of something larger than a word.  */
8810               /* XXX Might this clobber base?  I can't see how it can,
8811                  since scratch is known to overlap with OUTVAL, and
8812                  must be wider than a word.  */
8813               emit_insn (gen_movhi (scratch_hi, outval));
8814               outval = scratch_hi;
8815             }
8816         }
8817
8818       emit_set_insn (base_plus, base);
8819       base = base_plus;
8820     }
8821   else if (GET_CODE (base) == PLUS)
8822     {
8823       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8824       HOST_WIDE_INT hi, lo;
8825
8826       offset += INTVAL (XEXP (base, 1));
8827       base = XEXP (base, 0);
8828
8829       /* Rework the address into a legal sequence of insns.  */
8830       /* Valid range for lo is -4095 -> 4095 */
8831       lo = (offset >= 0
8832             ? (offset & 0xfff)
8833             : -((-offset) & 0xfff));
8834
8835       /* Corner case, if lo is the max offset then we would be out of range
8836          once we have added the additional 1 below, so bump the msb into the
8837          pre-loading insn(s).  */
8838       if (lo == 4095)
8839         lo &= 0x7ff;
8840
8841       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8842              ^ (HOST_WIDE_INT) 0x80000000)
8843             - (HOST_WIDE_INT) 0x80000000);
8844
8845       gcc_assert (hi + lo == offset);
8846
8847       if (hi != 0)
8848         {
8849           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8850
8851           /* Be careful not to destroy OUTVAL.  */
8852           if (reg_overlap_mentioned_p (base_plus, outval))
8853             {
8854               /* Updating base_plus might destroy outval, see if we
8855                  can swap the scratch and base_plus.  */
8856               if (!reg_overlap_mentioned_p (scratch, outval))
8857                 {
8858                   rtx tmp = scratch;
8859                   scratch = base_plus;
8860                   base_plus = tmp;
8861                 }
8862               else
8863                 {
8864                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8865
8866                   /* Be conservative and copy outval into scratch now,
8867                      this should only be necessary if outval is a
8868                      subreg of something larger than a word.  */
8869                   /* XXX Might this clobber base?  I can't see how it
8870                      can, since scratch is known to overlap with
8871                      outval.  */
8872                   emit_insn (gen_movhi (scratch_hi, outval));
8873                   outval = scratch_hi;
8874                 }
8875             }
8876
8877           /* Get the base address; addsi3 knows how to handle constants
8878              that require more than one insn.  */
8879           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8880           base = base_plus;
8881           offset = lo;
8882         }
8883     }
8884
8885   if (BYTES_BIG_ENDIAN)
8886     {
8887       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8888                                          plus_constant (base, offset + 1)),
8889                             gen_lowpart (QImode, outval)));
8890       emit_insn (gen_lshrsi3 (scratch,
8891                               gen_rtx_SUBREG (SImode, outval, 0),
8892                               GEN_INT (8)));
8893       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8894                             gen_lowpart (QImode, scratch)));
8895     }
8896   else
8897     {
8898       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8899                             gen_lowpart (QImode, outval)));
8900       emit_insn (gen_lshrsi3 (scratch,
8901                               gen_rtx_SUBREG (SImode, outval, 0),
8902                               GEN_INT (8)));
8903       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8904                                          plus_constant (base, offset + 1)),
8905                             gen_lowpart (QImode, scratch)));
8906     }
8907 }
8908
8909 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
8910    (padded to the size of a word) should be passed in a register.  */
8911
8912 static bool
8913 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
8914 {
8915   if (TARGET_AAPCS_BASED)
8916     return must_pass_in_stack_var_size (mode, type);
8917   else
8918     return must_pass_in_stack_var_size_or_pad (mode, type);
8919 }
8920
8921
8922 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
8923    Return true if an argument passed on the stack should be padded upwards,
8924    i.e. if the least-significant byte has useful data.
8925    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
8926    aggregate types are placed in the lowest memory address.  */
8927
8928 bool
8929 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
8930 {
8931   if (!TARGET_AAPCS_BASED)
8932     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
8933
8934   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
8935     return false;
8936
8937   return true;
8938 }
8939
8940
8941 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
8942    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
8943    byte of the register has useful data, and return the opposite if the
8944    most significant byte does.
8945    For AAPCS, small aggregates and small complex types are always padded
8946    upwards.  */
8947
8948 bool
8949 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
8950                     tree type, int first ATTRIBUTE_UNUSED)
8951 {
8952   if (TARGET_AAPCS_BASED
8953       && BYTES_BIG_ENDIAN
8954       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
8955       && int_size_in_bytes (type) <= 4)
8956     return true;
8957
8958   /* Otherwise, use default padding.  */
8959   return !BYTES_BIG_ENDIAN;
8960 }
8961
8962 \f
8963 /* Print a symbolic form of X to the debug file, F.  */
8964 static void
8965 arm_print_value (FILE *f, rtx x)
8966 {
8967   switch (GET_CODE (x))
8968     {
8969     case CONST_INT:
8970       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
8971       return;
8972
8973     case CONST_DOUBLE:
8974       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
8975       return;
8976
8977     case CONST_VECTOR:
8978       {
8979         int i;
8980
8981         fprintf (f, "<");
8982         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
8983           {
8984             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
8985             if (i < (CONST_VECTOR_NUNITS (x) - 1))
8986               fputc (',', f);
8987           }
8988         fprintf (f, ">");
8989       }
8990       return;
8991
8992     case CONST_STRING:
8993       fprintf (f, "\"%s\"", XSTR (x, 0));
8994       return;
8995
8996     case SYMBOL_REF:
8997       fprintf (f, "`%s'", XSTR (x, 0));
8998       return;
8999
9000     case LABEL_REF:
9001       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
9002       return;
9003
9004     case CONST:
9005       arm_print_value (f, XEXP (x, 0));
9006       return;
9007
9008     case PLUS:
9009       arm_print_value (f, XEXP (x, 0));
9010       fprintf (f, "+");
9011       arm_print_value (f, XEXP (x, 1));
9012       return;
9013
9014     case PC:
9015       fprintf (f, "pc");
9016       return;
9017
9018     default:
9019       fprintf (f, "????");
9020       return;
9021     }
9022 }
9023 \f
9024 /* Routines for manipulation of the constant pool.  */
9025
9026 /* Arm instructions cannot load a large constant directly into a
9027    register; they have to come from a pc relative load.  The constant
9028    must therefore be placed in the addressable range of the pc
9029    relative load.  Depending on the precise pc relative load
9030    instruction the range is somewhere between 256 bytes and 4k.  This
9031    means that we often have to dump a constant inside a function, and
9032    generate code to branch around it.
9033
9034    It is important to minimize this, since the branches will slow
9035    things down and make the code larger.
9036
9037    Normally we can hide the table after an existing unconditional
9038    branch so that there is no interruption of the flow, but in the
9039    worst case the code looks like this:
9040
9041         ldr     rn, L1
9042         ...
9043         b       L2
9044         align
9045         L1:     .long value
9046         L2:
9047         ...
9048
9049         ldr     rn, L3
9050         ...
9051         b       L4
9052         align
9053         L3:     .long value
9054         L4:
9055         ...
9056
9057    We fix this by performing a scan after scheduling, which notices
9058    which instructions need to have their operands fetched from the
9059    constant table and builds the table.
9060
9061    The algorithm starts by building a table of all the constants that
9062    need fixing up and all the natural barriers in the function (places
9063    where a constant table can be dropped without breaking the flow).
9064    For each fixup we note how far the pc-relative replacement will be
9065    able to reach and the offset of the instruction into the function.
9066
9067    Having built the table we then group the fixes together to form
9068    tables that are as large as possible (subject to addressing
9069    constraints) and emit each table of constants after the last
9070    barrier that is within range of all the instructions in the group.
9071    If a group does not contain a barrier, then we forcibly create one
9072    by inserting a jump instruction into the flow.  Once the table has
9073    been inserted, the insns are then modified to reference the
9074    relevant entry in the pool.
9075
9076    Possible enhancements to the algorithm (not implemented) are:
9077
9078    1) For some processors and object formats, there may be benefit in
9079    aligning the pools to the start of cache lines; this alignment
9080    would need to be taken into account when calculating addressability
9081    of a pool.  */
9082
9083 /* These typedefs are located at the start of this file, so that
9084    they can be used in the prototypes there.  This comment is to
9085    remind readers of that fact so that the following structures
9086    can be understood more easily.
9087
9088      typedef struct minipool_node    Mnode;
9089      typedef struct minipool_fixup   Mfix;  */
9090
9091 struct minipool_node
9092 {
9093   /* Doubly linked chain of entries.  */
9094   Mnode * next;
9095   Mnode * prev;
9096   /* The maximum offset into the code that this entry can be placed.  While
9097      pushing fixes for forward references, all entries are sorted in order
9098      of increasing max_address.  */
9099   HOST_WIDE_INT max_address;
9100   /* Similarly for an entry inserted for a backwards ref.  */
9101   HOST_WIDE_INT min_address;
9102   /* The number of fixes referencing this entry.  This can become zero
9103      if we "unpush" an entry.  In this case we ignore the entry when we
9104      come to emit the code.  */
9105   int refcount;
9106   /* The offset from the start of the minipool.  */
9107   HOST_WIDE_INT offset;
9108   /* The value in table.  */
9109   rtx value;
9110   /* The mode of value.  */
9111   enum machine_mode mode;
9112   /* The size of the value.  With iWMMXt enabled
9113      sizes > 4 also imply an alignment of 8-bytes.  */
9114   int fix_size;
9115 };
9116
9117 struct minipool_fixup
9118 {
9119   Mfix *            next;
9120   rtx               insn;
9121   HOST_WIDE_INT     address;
9122   rtx *             loc;
9123   enum machine_mode mode;
9124   int               fix_size;
9125   rtx               value;
9126   Mnode *           minipool;
9127   HOST_WIDE_INT     forwards;
9128   HOST_WIDE_INT     backwards;
9129 };
9130
9131 /* Fixes less than a word need padding out to a word boundary.  */
9132 #define MINIPOOL_FIX_SIZE(mode) \
9133   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
9134
9135 static Mnode *  minipool_vector_head;
9136 static Mnode *  minipool_vector_tail;
9137 static rtx      minipool_vector_label;
9138 static int      minipool_pad;
9139
9140 /* The linked list of all minipool fixes required for this function.  */
9141 Mfix *          minipool_fix_head;
9142 Mfix *          minipool_fix_tail;
9143 /* The fix entry for the current minipool, once it has been placed.  */
9144 Mfix *          minipool_barrier;
9145
9146 /* Determines if INSN is the start of a jump table.  Returns the end
9147    of the TABLE or NULL_RTX.  */
9148 static rtx
9149 is_jump_table (rtx insn)
9150 {
9151   rtx table;
9152
9153   if (GET_CODE (insn) == JUMP_INSN
9154       && JUMP_LABEL (insn) != NULL
9155       && ((table = next_real_insn (JUMP_LABEL (insn)))
9156           == next_real_insn (insn))
9157       && table != NULL
9158       && GET_CODE (table) == JUMP_INSN
9159       && (GET_CODE (PATTERN (table)) == ADDR_VEC
9160           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
9161     return table;
9162
9163   return NULL_RTX;
9164 }
9165
9166 #ifndef JUMP_TABLES_IN_TEXT_SECTION
9167 #define JUMP_TABLES_IN_TEXT_SECTION 0
9168 #endif
9169
9170 static HOST_WIDE_INT
9171 get_jump_table_size (rtx insn)
9172 {
9173   /* ADDR_VECs only take room if read-only data does into the text
9174      section.  */
9175   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
9176     {
9177       rtx body = PATTERN (insn);
9178       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
9179       HOST_WIDE_INT size;
9180       HOST_WIDE_INT modesize;
9181
9182       modesize = GET_MODE_SIZE (GET_MODE (body));
9183       size = modesize * XVECLEN (body, elt);
9184       switch (modesize)
9185         {
9186         case 1:
9187           /* Round up size  of TBB table to a halfword boundary.  */
9188           size = (size + 1) & ~(HOST_WIDE_INT)1;
9189           break;
9190         case 2:
9191           /* No padding necessary for TBH.  */
9192           break;
9193         case 4:
9194           /* Add two bytes for alignment on Thumb.  */
9195           if (TARGET_THUMB)
9196             size += 2;
9197           break;
9198         default:
9199           gcc_unreachable ();
9200         }
9201       return size;
9202     }
9203
9204   return 0;
9205 }
9206
9207 /* Move a minipool fix MP from its current location to before MAX_MP.
9208    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
9209    constraints may need updating.  */
9210 static Mnode *
9211 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
9212                                HOST_WIDE_INT max_address)
9213 {
9214   /* The code below assumes these are different.  */
9215   gcc_assert (mp != max_mp);
9216
9217   if (max_mp == NULL)
9218     {
9219       if (max_address < mp->max_address)
9220         mp->max_address = max_address;
9221     }
9222   else
9223     {
9224       if (max_address > max_mp->max_address - mp->fix_size)
9225         mp->max_address = max_mp->max_address - mp->fix_size;
9226       else
9227         mp->max_address = max_address;
9228
9229       /* Unlink MP from its current position.  Since max_mp is non-null,
9230        mp->prev must be non-null.  */
9231       mp->prev->next = mp->next;
9232       if (mp->next != NULL)
9233         mp->next->prev = mp->prev;
9234       else
9235         minipool_vector_tail = mp->prev;
9236
9237       /* Re-insert it before MAX_MP.  */
9238       mp->next = max_mp;
9239       mp->prev = max_mp->prev;
9240       max_mp->prev = mp;
9241
9242       if (mp->prev != NULL)
9243         mp->prev->next = mp;
9244       else
9245         minipool_vector_head = mp;
9246     }
9247
9248   /* Save the new entry.  */
9249   max_mp = mp;
9250
9251   /* Scan over the preceding entries and adjust their addresses as
9252      required.  */
9253   while (mp->prev != NULL
9254          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
9255     {
9256       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
9257       mp = mp->prev;
9258     }
9259
9260   return max_mp;
9261 }
9262
9263 /* Add a constant to the minipool for a forward reference.  Returns the
9264    node added or NULL if the constant will not fit in this pool.  */
9265 static Mnode *
9266 add_minipool_forward_ref (Mfix *fix)
9267 {
9268   /* If set, max_mp is the first pool_entry that has a lower
9269      constraint than the one we are trying to add.  */
9270   Mnode *       max_mp = NULL;
9271   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
9272   Mnode *       mp;
9273
9274   /* If the minipool starts before the end of FIX->INSN then this FIX
9275      can not be placed into the current pool.  Furthermore, adding the
9276      new constant pool entry may cause the pool to start FIX_SIZE bytes
9277      earlier.  */
9278   if (minipool_vector_head &&
9279       (fix->address + get_attr_length (fix->insn)
9280        >= minipool_vector_head->max_address - fix->fix_size))
9281     return NULL;
9282
9283   /* Scan the pool to see if a constant with the same value has
9284      already been added.  While we are doing this, also note the
9285      location where we must insert the constant if it doesn't already
9286      exist.  */
9287   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9288     {
9289       if (GET_CODE (fix->value) == GET_CODE (mp->value)
9290           && fix->mode == mp->mode
9291           && (GET_CODE (fix->value) != CODE_LABEL
9292               || (CODE_LABEL_NUMBER (fix->value)
9293                   == CODE_LABEL_NUMBER (mp->value)))
9294           && rtx_equal_p (fix->value, mp->value))
9295         {
9296           /* More than one fix references this entry.  */
9297           mp->refcount++;
9298           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
9299         }
9300
9301       /* Note the insertion point if necessary.  */
9302       if (max_mp == NULL
9303           && mp->max_address > max_address)
9304         max_mp = mp;
9305
9306       /* If we are inserting an 8-bytes aligned quantity and
9307          we have not already found an insertion point, then
9308          make sure that all such 8-byte aligned quantities are
9309          placed at the start of the pool.  */
9310       if (ARM_DOUBLEWORD_ALIGN
9311           && max_mp == NULL
9312           && fix->fix_size >= 8
9313           && mp->fix_size < 8)
9314         {
9315           max_mp = mp;
9316           max_address = mp->max_address;
9317         }
9318     }
9319
9320   /* The value is not currently in the minipool, so we need to create
9321      a new entry for it.  If MAX_MP is NULL, the entry will be put on
9322      the end of the list since the placement is less constrained than
9323      any existing entry.  Otherwise, we insert the new fix before
9324      MAX_MP and, if necessary, adjust the constraints on the other
9325      entries.  */
9326   mp = XNEW (Mnode);
9327   mp->fix_size = fix->fix_size;
9328   mp->mode = fix->mode;
9329   mp->value = fix->value;
9330   mp->refcount = 1;
9331   /* Not yet required for a backwards ref.  */
9332   mp->min_address = -65536;
9333
9334   if (max_mp == NULL)
9335     {
9336       mp->max_address = max_address;
9337       mp->next = NULL;
9338       mp->prev = minipool_vector_tail;
9339
9340       if (mp->prev == NULL)
9341         {
9342           minipool_vector_head = mp;
9343           minipool_vector_label = gen_label_rtx ();
9344         }
9345       else
9346         mp->prev->next = mp;
9347
9348       minipool_vector_tail = mp;
9349     }
9350   else
9351     {
9352       if (max_address > max_mp->max_address - mp->fix_size)
9353         mp->max_address = max_mp->max_address - mp->fix_size;
9354       else
9355         mp->max_address = max_address;
9356
9357       mp->next = max_mp;
9358       mp->prev = max_mp->prev;
9359       max_mp->prev = mp;
9360       if (mp->prev != NULL)
9361         mp->prev->next = mp;
9362       else
9363         minipool_vector_head = mp;
9364     }
9365
9366   /* Save the new entry.  */
9367   max_mp = mp;
9368
9369   /* Scan over the preceding entries and adjust their addresses as
9370      required.  */
9371   while (mp->prev != NULL
9372          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
9373     {
9374       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
9375       mp = mp->prev;
9376     }
9377
9378   return max_mp;
9379 }
9380
9381 static Mnode *
9382 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
9383                                 HOST_WIDE_INT  min_address)
9384 {
9385   HOST_WIDE_INT offset;
9386
9387   /* The code below assumes these are different.  */
9388   gcc_assert (mp != min_mp);
9389
9390   if (min_mp == NULL)
9391     {
9392       if (min_address > mp->min_address)
9393         mp->min_address = min_address;
9394     }
9395   else
9396     {
9397       /* We will adjust this below if it is too loose.  */
9398       mp->min_address = min_address;
9399
9400       /* Unlink MP from its current position.  Since min_mp is non-null,
9401          mp->next must be non-null.  */
9402       mp->next->prev = mp->prev;
9403       if (mp->prev != NULL)
9404         mp->prev->next = mp->next;
9405       else
9406         minipool_vector_head = mp->next;
9407
9408       /* Reinsert it after MIN_MP.  */
9409       mp->prev = min_mp;
9410       mp->next = min_mp->next;
9411       min_mp->next = mp;
9412       if (mp->next != NULL)
9413         mp->next->prev = mp;
9414       else
9415         minipool_vector_tail = mp;
9416     }
9417
9418   min_mp = mp;
9419
9420   offset = 0;
9421   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9422     {
9423       mp->offset = offset;
9424       if (mp->refcount > 0)
9425         offset += mp->fix_size;
9426
9427       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
9428         mp->next->min_address = mp->min_address + mp->fix_size;
9429     }
9430
9431   return min_mp;
9432 }
9433
9434 /* Add a constant to the minipool for a backward reference.  Returns the
9435    node added or NULL if the constant will not fit in this pool.
9436
9437    Note that the code for insertion for a backwards reference can be
9438    somewhat confusing because the calculated offsets for each fix do
9439    not take into account the size of the pool (which is still under
9440    construction.  */
9441 static Mnode *
9442 add_minipool_backward_ref (Mfix *fix)
9443 {
9444   /* If set, min_mp is the last pool_entry that has a lower constraint
9445      than the one we are trying to add.  */
9446   Mnode *min_mp = NULL;
9447   /* This can be negative, since it is only a constraint.  */
9448   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
9449   Mnode *mp;
9450
9451   /* If we can't reach the current pool from this insn, or if we can't
9452      insert this entry at the end of the pool without pushing other
9453      fixes out of range, then we don't try.  This ensures that we
9454      can't fail later on.  */
9455   if (min_address >= minipool_barrier->address
9456       || (minipool_vector_tail->min_address + fix->fix_size
9457           >= minipool_barrier->address))
9458     return NULL;
9459
9460   /* Scan the pool to see if a constant with the same value has
9461      already been added.  While we are doing this, also note the
9462      location where we must insert the constant if it doesn't already
9463      exist.  */
9464   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
9465     {
9466       if (GET_CODE (fix->value) == GET_CODE (mp->value)
9467           && fix->mode == mp->mode
9468           && (GET_CODE (fix->value) != CODE_LABEL
9469               || (CODE_LABEL_NUMBER (fix->value)
9470                   == CODE_LABEL_NUMBER (mp->value)))
9471           && rtx_equal_p (fix->value, mp->value)
9472           /* Check that there is enough slack to move this entry to the
9473              end of the table (this is conservative).  */
9474           && (mp->max_address
9475               > (minipool_barrier->address
9476                  + minipool_vector_tail->offset
9477                  + minipool_vector_tail->fix_size)))
9478         {
9479           mp->refcount++;
9480           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
9481         }
9482
9483       if (min_mp != NULL)
9484         mp->min_address += fix->fix_size;
9485       else
9486         {
9487           /* Note the insertion point if necessary.  */
9488           if (mp->min_address < min_address)
9489             {
9490               /* For now, we do not allow the insertion of 8-byte alignment
9491                  requiring nodes anywhere but at the start of the pool.  */
9492               if (ARM_DOUBLEWORD_ALIGN
9493                   && fix->fix_size >= 8 && mp->fix_size < 8)
9494                 return NULL;
9495               else
9496                 min_mp = mp;
9497             }
9498           else if (mp->max_address
9499                    < minipool_barrier->address + mp->offset + fix->fix_size)
9500             {
9501               /* Inserting before this entry would push the fix beyond
9502                  its maximum address (which can happen if we have
9503                  re-located a forwards fix); force the new fix to come
9504                  after it.  */
9505               if (ARM_DOUBLEWORD_ALIGN
9506                   && fix->fix_size >= 8 && mp->fix_size < 8)
9507                 return NULL;
9508               else
9509                 {
9510                   min_mp = mp;
9511                   min_address = mp->min_address + fix->fix_size;
9512                 }
9513             }
9514           /* Do not insert a non-8-byte aligned quantity before 8-byte
9515              aligned quantities.  */
9516           else if (ARM_DOUBLEWORD_ALIGN
9517                    && fix->fix_size < 8
9518                    && mp->fix_size >= 8)
9519             {
9520               min_mp = mp;
9521               min_address = mp->min_address + fix->fix_size;
9522             }
9523         }
9524     }
9525
9526   /* We need to create a new entry.  */
9527   mp = XNEW (Mnode);
9528   mp->fix_size = fix->fix_size;
9529   mp->mode = fix->mode;
9530   mp->value = fix->value;
9531   mp->refcount = 1;
9532   mp->max_address = minipool_barrier->address + 65536;
9533
9534   mp->min_address = min_address;
9535
9536   if (min_mp == NULL)
9537     {
9538       mp->prev = NULL;
9539       mp->next = minipool_vector_head;
9540
9541       if (mp->next == NULL)
9542         {
9543           minipool_vector_tail = mp;
9544           minipool_vector_label = gen_label_rtx ();
9545         }
9546       else
9547         mp->next->prev = mp;
9548
9549       minipool_vector_head = mp;
9550     }
9551   else
9552     {
9553       mp->next = min_mp->next;
9554       mp->prev = min_mp;
9555       min_mp->next = mp;
9556
9557       if (mp->next != NULL)
9558         mp->next->prev = mp;
9559       else
9560         minipool_vector_tail = mp;
9561     }
9562
9563   /* Save the new entry.  */
9564   min_mp = mp;
9565
9566   if (mp->prev)
9567     mp = mp->prev;
9568   else
9569     mp->offset = 0;
9570
9571   /* Scan over the following entries and adjust their offsets.  */
9572   while (mp->next != NULL)
9573     {
9574       if (mp->next->min_address < mp->min_address + mp->fix_size)
9575         mp->next->min_address = mp->min_address + mp->fix_size;
9576
9577       if (mp->refcount)
9578         mp->next->offset = mp->offset + mp->fix_size;
9579       else
9580         mp->next->offset = mp->offset;
9581
9582       mp = mp->next;
9583     }
9584
9585   return min_mp;
9586 }
9587
9588 static void
9589 assign_minipool_offsets (Mfix *barrier)
9590 {
9591   HOST_WIDE_INT offset = 0;
9592   Mnode *mp;
9593
9594   minipool_barrier = barrier;
9595
9596   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9597     {
9598       mp->offset = offset;
9599
9600       if (mp->refcount > 0)
9601         offset += mp->fix_size;
9602     }
9603 }
9604
9605 /* Output the literal table */
9606 static void
9607 dump_minipool (rtx scan)
9608 {
9609   Mnode * mp;
9610   Mnode * nmp;
9611   int align64 = 0;
9612
9613   if (ARM_DOUBLEWORD_ALIGN)
9614     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9615       if (mp->refcount > 0 && mp->fix_size >= 8)
9616         {
9617           align64 = 1;
9618           break;
9619         }
9620
9621   if (dump_file)
9622     fprintf (dump_file,
9623              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
9624              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
9625
9626   scan = emit_label_after (gen_label_rtx (), scan);
9627   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
9628   scan = emit_label_after (minipool_vector_label, scan);
9629
9630   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
9631     {
9632       if (mp->refcount > 0)
9633         {
9634           if (dump_file)
9635             {
9636               fprintf (dump_file,
9637                        ";;  Offset %u, min %ld, max %ld ",
9638                        (unsigned) mp->offset, (unsigned long) mp->min_address,
9639                        (unsigned long) mp->max_address);
9640               arm_print_value (dump_file, mp->value);
9641               fputc ('\n', dump_file);
9642             }
9643
9644           switch (mp->fix_size)
9645             {
9646 #ifdef HAVE_consttable_1
9647             case 1:
9648               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
9649               break;
9650
9651 #endif
9652 #ifdef HAVE_consttable_2
9653             case 2:
9654               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
9655               break;
9656
9657 #endif
9658 #ifdef HAVE_consttable_4
9659             case 4:
9660               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
9661               break;
9662
9663 #endif
9664 #ifdef HAVE_consttable_8
9665             case 8:
9666               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
9667               break;
9668
9669 #endif
9670 #ifdef HAVE_consttable_16
9671             case 16:
9672               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
9673               break;
9674
9675 #endif
9676             default:
9677               gcc_unreachable ();
9678             }
9679         }
9680
9681       nmp = mp->next;
9682       free (mp);
9683     }
9684
9685   minipool_vector_head = minipool_vector_tail = NULL;
9686   scan = emit_insn_after (gen_consttable_end (), scan);
9687   scan = emit_barrier_after (scan);
9688 }
9689
9690 /* Return the cost of forcibly inserting a barrier after INSN.  */
9691 static int
9692 arm_barrier_cost (rtx insn)
9693 {
9694   /* Basing the location of the pool on the loop depth is preferable,
9695      but at the moment, the basic block information seems to be
9696      corrupt by this stage of the compilation.  */
9697   int base_cost = 50;
9698   rtx next = next_nonnote_insn (insn);
9699
9700   if (next != NULL && GET_CODE (next) == CODE_LABEL)
9701     base_cost -= 20;
9702
9703   switch (GET_CODE (insn))
9704     {
9705     case CODE_LABEL:
9706       /* It will always be better to place the table before the label, rather
9707          than after it.  */
9708       return 50;
9709
9710     case INSN:
9711     case CALL_INSN:
9712       return base_cost;
9713
9714     case JUMP_INSN:
9715       return base_cost - 10;
9716
9717     default:
9718       return base_cost + 10;
9719     }
9720 }
9721
9722 /* Find the best place in the insn stream in the range
9723    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
9724    Create the barrier by inserting a jump and add a new fix entry for
9725    it.  */
9726 static Mfix *
9727 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
9728 {
9729   HOST_WIDE_INT count = 0;
9730   rtx barrier;
9731   rtx from = fix->insn;
9732   /* The instruction after which we will insert the jump.  */
9733   rtx selected = NULL;
9734   int selected_cost;
9735   /* The address at which the jump instruction will be placed.  */
9736   HOST_WIDE_INT selected_address;
9737   Mfix * new_fix;
9738   HOST_WIDE_INT max_count = max_address - fix->address;
9739   rtx label = gen_label_rtx ();
9740
9741   selected_cost = arm_barrier_cost (from);
9742   selected_address = fix->address;
9743
9744   while (from && count < max_count)
9745     {
9746       rtx tmp;
9747       int new_cost;
9748
9749       /* This code shouldn't have been called if there was a natural barrier
9750          within range.  */
9751       gcc_assert (GET_CODE (from) != BARRIER);
9752
9753       /* Count the length of this insn.  */
9754       count += get_attr_length (from);
9755
9756       /* If there is a jump table, add its length.  */
9757       tmp = is_jump_table (from);
9758       if (tmp != NULL)
9759         {
9760           count += get_jump_table_size (tmp);
9761
9762           /* Jump tables aren't in a basic block, so base the cost on
9763              the dispatch insn.  If we select this location, we will
9764              still put the pool after the table.  */
9765           new_cost = arm_barrier_cost (from);
9766
9767           if (count < max_count 
9768               && (!selected || new_cost <= selected_cost))
9769             {
9770               selected = tmp;
9771               selected_cost = new_cost;
9772               selected_address = fix->address + count;
9773             }
9774
9775           /* Continue after the dispatch table.  */
9776           from = NEXT_INSN (tmp);
9777           continue;
9778         }
9779
9780       new_cost = arm_barrier_cost (from);
9781
9782       if (count < max_count
9783           && (!selected || new_cost <= selected_cost))
9784         {
9785           selected = from;
9786           selected_cost = new_cost;
9787           selected_address = fix->address + count;
9788         }
9789
9790       from = NEXT_INSN (from);
9791     }
9792
9793   /* Make sure that we found a place to insert the jump.  */
9794   gcc_assert (selected);
9795
9796   /* Create a new JUMP_INSN that branches around a barrier.  */
9797   from = emit_jump_insn_after (gen_jump (label), selected);
9798   JUMP_LABEL (from) = label;
9799   barrier = emit_barrier_after (from);
9800   emit_label_after (label, barrier);
9801
9802   /* Create a minipool barrier entry for the new barrier.  */
9803   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
9804   new_fix->insn = barrier;
9805   new_fix->address = selected_address;
9806   new_fix->next = fix->next;
9807   fix->next = new_fix;
9808
9809   return new_fix;
9810 }
9811
9812 /* Record that there is a natural barrier in the insn stream at
9813    ADDRESS.  */
9814 static void
9815 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
9816 {
9817   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9818
9819   fix->insn = insn;
9820   fix->address = address;
9821
9822   fix->next = NULL;
9823   if (minipool_fix_head != NULL)
9824     minipool_fix_tail->next = fix;
9825   else
9826     minipool_fix_head = fix;
9827
9828   minipool_fix_tail = fix;
9829 }
9830
9831 /* Record INSN, which will need fixing up to load a value from the
9832    minipool.  ADDRESS is the offset of the insn since the start of the
9833    function; LOC is a pointer to the part of the insn which requires
9834    fixing; VALUE is the constant that must be loaded, which is of type
9835    MODE.  */
9836 static void
9837 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
9838                    enum machine_mode mode, rtx value)
9839 {
9840   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9841
9842   fix->insn = insn;
9843   fix->address = address;
9844   fix->loc = loc;
9845   fix->mode = mode;
9846   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
9847   fix->value = value;
9848   fix->forwards = get_attr_pool_range (insn);
9849   fix->backwards = get_attr_neg_pool_range (insn);
9850   fix->minipool = NULL;
9851
9852   /* If an insn doesn't have a range defined for it, then it isn't
9853      expecting to be reworked by this code.  Better to stop now than
9854      to generate duff assembly code.  */
9855   gcc_assert (fix->forwards || fix->backwards);
9856
9857   /* If an entry requires 8-byte alignment then assume all constant pools
9858      require 4 bytes of padding.  Trying to do this later on a per-pool
9859      basis is awkward because existing pool entries have to be modified.  */
9860   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
9861     minipool_pad = 4;
9862
9863   if (dump_file)
9864     {
9865       fprintf (dump_file,
9866                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
9867                GET_MODE_NAME (mode),
9868                INSN_UID (insn), (unsigned long) address,
9869                -1 * (long)fix->backwards, (long)fix->forwards);
9870       arm_print_value (dump_file, fix->value);
9871       fprintf (dump_file, "\n");
9872     }
9873
9874   /* Add it to the chain of fixes.  */
9875   fix->next = NULL;
9876
9877   if (minipool_fix_head != NULL)
9878     minipool_fix_tail->next = fix;
9879   else
9880     minipool_fix_head = fix;
9881
9882   minipool_fix_tail = fix;
9883 }
9884
9885 /* Return the cost of synthesizing a 64-bit constant VAL inline.
9886    Returns the number of insns needed, or 99 if we don't know how to
9887    do it.  */
9888 int
9889 arm_const_double_inline_cost (rtx val)
9890 {
9891   rtx lowpart, highpart;
9892   enum machine_mode mode;
9893
9894   mode = GET_MODE (val);
9895
9896   if (mode == VOIDmode)
9897     mode = DImode;
9898
9899   gcc_assert (GET_MODE_SIZE (mode) == 8);
9900
9901   lowpart = gen_lowpart (SImode, val);
9902   highpart = gen_highpart_mode (SImode, mode, val);
9903
9904   gcc_assert (GET_CODE (lowpart) == CONST_INT);
9905   gcc_assert (GET_CODE (highpart) == CONST_INT);
9906
9907   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
9908                             NULL_RTX, NULL_RTX, 0, 0)
9909           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
9910                               NULL_RTX, NULL_RTX, 0, 0));
9911 }
9912
9913 /* Return true if it is worthwhile to split a 64-bit constant into two
9914    32-bit operations.  This is the case if optimizing for size, or
9915    if we have load delay slots, or if one 32-bit part can be done with
9916    a single data operation.  */
9917 bool
9918 arm_const_double_by_parts (rtx val)
9919 {
9920   enum machine_mode mode = GET_MODE (val);
9921   rtx part;
9922
9923   if (optimize_size || arm_ld_sched)
9924     return true;
9925
9926   if (mode == VOIDmode)
9927     mode = DImode;
9928
9929   part = gen_highpart_mode (SImode, mode, val);
9930
9931   gcc_assert (GET_CODE (part) == CONST_INT);
9932
9933   if (const_ok_for_arm (INTVAL (part))
9934       || const_ok_for_arm (~INTVAL (part)))
9935     return true;
9936
9937   part = gen_lowpart (SImode, val);
9938
9939   gcc_assert (GET_CODE (part) == CONST_INT);
9940
9941   if (const_ok_for_arm (INTVAL (part))
9942       || const_ok_for_arm (~INTVAL (part)))
9943     return true;
9944
9945   return false;
9946 }
9947
9948 /* Scan INSN and note any of its operands that need fixing.
9949    If DO_PUSHES is false we do not actually push any of the fixups
9950    needed.  The function returns TRUE if any fixups were needed/pushed.
9951    This is used by arm_memory_load_p() which needs to know about loads
9952    of constants that will be converted into minipool loads.  */
9953 static bool
9954 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
9955 {
9956   bool result = false;
9957   int opno;
9958
9959   extract_insn (insn);
9960
9961   if (!constrain_operands (1))
9962     fatal_insn_not_found (insn);
9963
9964   if (recog_data.n_alternatives == 0)
9965     return false;
9966
9967   /* Fill in recog_op_alt with information about the constraints of
9968      this insn.  */
9969   preprocess_constraints ();
9970
9971   for (opno = 0; opno < recog_data.n_operands; opno++)
9972     {
9973       /* Things we need to fix can only occur in inputs.  */
9974       if (recog_data.operand_type[opno] != OP_IN)
9975         continue;
9976
9977       /* If this alternative is a memory reference, then any mention
9978          of constants in this alternative is really to fool reload
9979          into allowing us to accept one there.  We need to fix them up
9980          now so that we output the right code.  */
9981       if (recog_op_alt[opno][which_alternative].memory_ok)
9982         {
9983           rtx op = recog_data.operand[opno];
9984
9985           if (CONSTANT_P (op))
9986             {
9987               if (do_pushes)
9988                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
9989                                    recog_data.operand_mode[opno], op);
9990               result = true;
9991             }
9992           else if (GET_CODE (op) == MEM
9993                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
9994                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
9995             {
9996               if (do_pushes)
9997                 {
9998                   rtx cop = avoid_constant_pool_reference (op);
9999
10000                   /* Casting the address of something to a mode narrower
10001                      than a word can cause avoid_constant_pool_reference()
10002                      to return the pool reference itself.  That's no good to
10003                      us here.  Lets just hope that we can use the
10004                      constant pool value directly.  */
10005                   if (op == cop)
10006                     cop = get_pool_constant (XEXP (op, 0));
10007
10008                   push_minipool_fix (insn, address,
10009                                      recog_data.operand_loc[opno],
10010                                      recog_data.operand_mode[opno], cop);
10011                 }
10012
10013               result = true;
10014             }
10015         }
10016     }
10017
10018   return result;
10019 }
10020
10021 /* Gcc puts the pool in the wrong place for ARM, since we can only
10022    load addresses a limited distance around the pc.  We do some
10023    special munging to move the constant pool values to the correct
10024    point in the code.  */
10025 static void
10026 arm_reorg (void)
10027 {
10028   rtx insn;
10029   HOST_WIDE_INT address = 0;
10030   Mfix * fix;
10031
10032   minipool_fix_head = minipool_fix_tail = NULL;
10033
10034   /* The first insn must always be a note, or the code below won't
10035      scan it properly.  */
10036   insn = get_insns ();
10037   gcc_assert (GET_CODE (insn) == NOTE);
10038   minipool_pad = 0;
10039
10040   /* Scan all the insns and record the operands that will need fixing.  */
10041   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
10042     {
10043       if (TARGET_CIRRUS_FIX_INVALID_INSNS
10044           && (arm_cirrus_insn_p (insn)
10045               || GET_CODE (insn) == JUMP_INSN
10046               || arm_memory_load_p (insn)))
10047         cirrus_reorg (insn);
10048
10049       if (GET_CODE (insn) == BARRIER)
10050         push_minipool_barrier (insn, address);
10051       else if (INSN_P (insn))
10052         {
10053           rtx table;
10054
10055           note_invalid_constants (insn, address, true);
10056           address += get_attr_length (insn);
10057
10058           /* If the insn is a vector jump, add the size of the table
10059              and skip the table.  */
10060           if ((table = is_jump_table (insn)) != NULL)
10061             {
10062               address += get_jump_table_size (table);
10063               insn = table;
10064             }
10065         }
10066     }
10067
10068   fix = minipool_fix_head;
10069
10070   /* Now scan the fixups and perform the required changes.  */
10071   while (fix)
10072     {
10073       Mfix * ftmp;
10074       Mfix * fdel;
10075       Mfix *  last_added_fix;
10076       Mfix * last_barrier = NULL;
10077       Mfix * this_fix;
10078
10079       /* Skip any further barriers before the next fix.  */
10080       while (fix && GET_CODE (fix->insn) == BARRIER)
10081         fix = fix->next;
10082
10083       /* No more fixes.  */
10084       if (fix == NULL)
10085         break;
10086
10087       last_added_fix = NULL;
10088
10089       for (ftmp = fix; ftmp; ftmp = ftmp->next)
10090         {
10091           if (GET_CODE (ftmp->insn) == BARRIER)
10092             {
10093               if (ftmp->address >= minipool_vector_head->max_address)
10094                 break;
10095
10096               last_barrier = ftmp;
10097             }
10098           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
10099             break;
10100
10101           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
10102         }
10103
10104       /* If we found a barrier, drop back to that; any fixes that we
10105          could have reached but come after the barrier will now go in
10106          the next mini-pool.  */
10107       if (last_barrier != NULL)
10108         {
10109           /* Reduce the refcount for those fixes that won't go into this
10110              pool after all.  */
10111           for (fdel = last_barrier->next;
10112                fdel && fdel != ftmp;
10113                fdel = fdel->next)
10114             {
10115               fdel->minipool->refcount--;
10116               fdel->minipool = NULL;
10117             }
10118
10119           ftmp = last_barrier;
10120         }
10121       else
10122         {
10123           /* ftmp is first fix that we can't fit into this pool and
10124              there no natural barriers that we could use.  Insert a
10125              new barrier in the code somewhere between the previous
10126              fix and this one, and arrange to jump around it.  */
10127           HOST_WIDE_INT max_address;
10128
10129           /* The last item on the list of fixes must be a barrier, so
10130              we can never run off the end of the list of fixes without
10131              last_barrier being set.  */
10132           gcc_assert (ftmp);
10133
10134           max_address = minipool_vector_head->max_address;
10135           /* Check that there isn't another fix that is in range that
10136              we couldn't fit into this pool because the pool was
10137              already too large: we need to put the pool before such an
10138              instruction.  The pool itself may come just after the
10139              fix because create_fix_barrier also allows space for a
10140              jump instruction.  */
10141           if (ftmp->address < max_address)
10142             max_address = ftmp->address + 1;
10143
10144           last_barrier = create_fix_barrier (last_added_fix, max_address);
10145         }
10146
10147       assign_minipool_offsets (last_barrier);
10148
10149       while (ftmp)
10150         {
10151           if (GET_CODE (ftmp->insn) != BARRIER
10152               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
10153                   == NULL))
10154             break;
10155
10156           ftmp = ftmp->next;
10157         }
10158
10159       /* Scan over the fixes we have identified for this pool, fixing them
10160          up and adding the constants to the pool itself.  */
10161       for (this_fix = fix; this_fix && ftmp != this_fix;
10162            this_fix = this_fix->next)
10163         if (GET_CODE (this_fix->insn) != BARRIER)
10164           {
10165             rtx addr
10166               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
10167                                                   minipool_vector_label),
10168                                this_fix->minipool->offset);
10169             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
10170           }
10171
10172       dump_minipool (last_barrier->insn);
10173       fix = ftmp;
10174     }
10175
10176   /* From now on we must synthesize any constants that we can't handle
10177      directly.  This can happen if the RTL gets split during final
10178      instruction generation.  */
10179   after_arm_reorg = 1;
10180
10181   /* Free the minipool memory.  */
10182   obstack_free (&minipool_obstack, minipool_startobj);
10183 }
10184 \f
10185 /* Routines to output assembly language.  */
10186
10187 /* If the rtx is the correct value then return the string of the number.
10188    In this way we can ensure that valid double constants are generated even
10189    when cross compiling.  */
10190 const char *
10191 fp_immediate_constant (rtx x)
10192 {
10193   REAL_VALUE_TYPE r;
10194   int i;
10195
10196   if (!fp_consts_inited)
10197     init_fp_table ();
10198
10199   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10200   for (i = 0; i < 8; i++)
10201     if (REAL_VALUES_EQUAL (r, values_fp[i]))
10202       return strings_fp[i];
10203
10204   gcc_unreachable ();
10205 }
10206
10207 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
10208 static const char *
10209 fp_const_from_val (REAL_VALUE_TYPE *r)
10210 {
10211   int i;
10212
10213   if (!fp_consts_inited)
10214     init_fp_table ();
10215
10216   for (i = 0; i < 8; i++)
10217     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
10218       return strings_fp[i];
10219
10220   gcc_unreachable ();
10221 }
10222
10223 /* Output the operands of a LDM/STM instruction to STREAM.
10224    MASK is the ARM register set mask of which only bits 0-15 are important.
10225    REG is the base register, either the frame pointer or the stack pointer,
10226    INSTR is the possibly suffixed load or store instruction.
10227    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
10228
10229 static void
10230 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
10231                  unsigned long mask, int rfe)
10232 {
10233   unsigned i;
10234   bool not_first = FALSE;
10235
10236   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
10237   fputc ('\t', stream);
10238   asm_fprintf (stream, instr, reg);
10239   fputc ('{', stream);
10240
10241   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10242     if (mask & (1 << i))
10243       {
10244         if (not_first)
10245           fprintf (stream, ", ");
10246
10247         asm_fprintf (stream, "%r", i);
10248         not_first = TRUE;
10249       }
10250
10251   if (rfe)
10252     fprintf (stream, "}^\n");
10253   else
10254     fprintf (stream, "}\n");
10255 }
10256
10257
10258 /* Output a FLDMD instruction to STREAM.
10259    BASE if the register containing the address.
10260    REG and COUNT specify the register range.
10261    Extra registers may be added to avoid hardware bugs.
10262
10263    We output FLDMD even for ARMv5 VFP implementations.  Although
10264    FLDMD is technically not supported until ARMv6, it is believed
10265    that all VFP implementations support its use in this context.  */
10266
10267 static void
10268 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
10269 {
10270   int i;
10271
10272   /* Workaround ARM10 VFPr1 bug.  */
10273   if (count == 2 && !arm_arch6)
10274     {
10275       if (reg == 15)
10276         reg--;
10277       count++;
10278     }
10279
10280   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
10281      load into multiple parts if we have to handle more than 16 registers.  */
10282   if (count > 16)
10283     {
10284       vfp_output_fldmd (stream, base, reg, 16);
10285       vfp_output_fldmd (stream, base, reg + 16, count - 16);
10286       return;
10287     }
10288
10289   fputc ('\t', stream);
10290   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
10291
10292   for (i = reg; i < reg + count; i++)
10293     {
10294       if (i > reg)
10295         fputs (", ", stream);
10296       asm_fprintf (stream, "d%d", i);
10297     }
10298   fputs ("}\n", stream);
10299
10300 }
10301
10302
10303 /* Output the assembly for a store multiple.  */
10304
10305 const char *
10306 vfp_output_fstmd (rtx * operands)
10307 {
10308   char pattern[100];
10309   int p;
10310   int base;
10311   int i;
10312
10313   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
10314   p = strlen (pattern);
10315
10316   gcc_assert (GET_CODE (operands[1]) == REG);
10317
10318   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
10319   for (i = 1; i < XVECLEN (operands[2], 0); i++)
10320     {
10321       p += sprintf (&pattern[p], ", d%d", base + i);
10322     }
10323   strcpy (&pattern[p], "}");
10324
10325   output_asm_insn (pattern, operands);
10326   return "";
10327 }
10328
10329
10330 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
10331    number of bytes pushed.  */
10332
10333 static int
10334 vfp_emit_fstmd (int base_reg, int count)
10335 {
10336   rtx par;
10337   rtx dwarf;
10338   rtx tmp, reg;
10339   int i;
10340
10341   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
10342      register pairs are stored by a store multiple insn.  We avoid this
10343      by pushing an extra pair.  */
10344   if (count == 2 && !arm_arch6)
10345     {
10346       if (base_reg == LAST_VFP_REGNUM - 3)
10347         base_reg -= 2;
10348       count++;
10349     }
10350
10351   /* FSTMD may not store more than 16 doubleword registers at once.  Split
10352      larger stores into multiple parts (up to a maximum of two, in
10353      practice).  */
10354   if (count > 16)
10355     {
10356       int saved;
10357       /* NOTE: base_reg is an internal register number, so each D register
10358          counts as 2.  */
10359       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
10360       saved += vfp_emit_fstmd (base_reg, 16);
10361       return saved;
10362     }
10363
10364   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10365   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10366
10367   reg = gen_rtx_REG (DFmode, base_reg);
10368   base_reg += 2;
10369
10370   XVECEXP (par, 0, 0)
10371     = gen_rtx_SET (VOIDmode,
10372                    gen_frame_mem (BLKmode,
10373                                   gen_rtx_PRE_DEC (BLKmode,
10374                                                    stack_pointer_rtx)),
10375                    gen_rtx_UNSPEC (BLKmode,
10376                                    gen_rtvec (1, reg),
10377                                    UNSPEC_PUSH_MULT));
10378
10379   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10380                      plus_constant (stack_pointer_rtx, -(count * 8)));
10381   RTX_FRAME_RELATED_P (tmp) = 1;
10382   XVECEXP (dwarf, 0, 0) = tmp;
10383
10384   tmp = gen_rtx_SET (VOIDmode,
10385                      gen_frame_mem (DFmode, stack_pointer_rtx),
10386                      reg);
10387   RTX_FRAME_RELATED_P (tmp) = 1;
10388   XVECEXP (dwarf, 0, 1) = tmp;
10389
10390   for (i = 1; i < count; i++)
10391     {
10392       reg = gen_rtx_REG (DFmode, base_reg);
10393       base_reg += 2;
10394       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10395
10396       tmp = gen_rtx_SET (VOIDmode,
10397                          gen_frame_mem (DFmode,
10398                                         plus_constant (stack_pointer_rtx,
10399                                                        i * 8)),
10400                          reg);
10401       RTX_FRAME_RELATED_P (tmp) = 1;
10402       XVECEXP (dwarf, 0, i + 1) = tmp;
10403     }
10404
10405   par = emit_insn (par);
10406   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
10407   RTX_FRAME_RELATED_P (par) = 1;
10408
10409   return count * 8;
10410 }
10411
10412 /* Emit a call instruction with pattern PAT.  ADDR is the address of
10413    the call target.  */
10414
10415 void
10416 arm_emit_call_insn (rtx pat, rtx addr)
10417 {
10418   rtx insn;
10419
10420   insn = emit_call_insn (pat);
10421
10422   /* The PIC register is live on entry to VxWorks PIC PLT entries.
10423      If the call might use such an entry, add a use of the PIC register
10424      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
10425   if (TARGET_VXWORKS_RTP
10426       && flag_pic
10427       && GET_CODE (addr) == SYMBOL_REF
10428       && (SYMBOL_REF_DECL (addr)
10429           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
10430           : !SYMBOL_REF_LOCAL_P (addr)))
10431     {
10432       require_pic_register ();
10433       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
10434     }
10435 }
10436
10437 /* Output a 'call' insn.  */
10438 const char *
10439 output_call (rtx *operands)
10440 {
10441   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
10442
10443   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
10444   if (REGNO (operands[0]) == LR_REGNUM)
10445     {
10446       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
10447       output_asm_insn ("mov%?\t%0, %|lr", operands);
10448     }
10449
10450   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10451
10452   if (TARGET_INTERWORK || arm_arch4t)
10453     output_asm_insn ("bx%?\t%0", operands);
10454   else
10455     output_asm_insn ("mov%?\t%|pc, %0", operands);
10456
10457   return "";
10458 }
10459
10460 /* Output a 'call' insn that is a reference in memory.  */
10461 const char *
10462 output_call_mem (rtx *operands)
10463 {
10464   if (TARGET_INTERWORK && !arm_arch5)
10465     {
10466       output_asm_insn ("ldr%?\t%|ip, %0", operands);
10467       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10468       output_asm_insn ("bx%?\t%|ip", operands);
10469     }
10470   else if (regno_use_in (LR_REGNUM, operands[0]))
10471     {
10472       /* LR is used in the memory address.  We load the address in the
10473          first instruction.  It's safe to use IP as the target of the
10474          load since the call will kill it anyway.  */
10475       output_asm_insn ("ldr%?\t%|ip, %0", operands);
10476       if (arm_arch5)
10477         output_asm_insn ("blx%?\t%|ip", operands);
10478       else
10479         {
10480           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10481           if (arm_arch4t)
10482             output_asm_insn ("bx%?\t%|ip", operands);
10483           else
10484             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
10485         }
10486     }
10487   else
10488     {
10489       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10490       output_asm_insn ("ldr%?\t%|pc, %0", operands);
10491     }
10492
10493   return "";
10494 }
10495
10496
10497 /* Output a move from arm registers to an fpa registers.
10498    OPERANDS[0] is an fpa register.
10499    OPERANDS[1] is the first registers of an arm register pair.  */
10500 const char *
10501 output_mov_long_double_fpa_from_arm (rtx *operands)
10502 {
10503   int arm_reg0 = REGNO (operands[1]);
10504   rtx ops[3];
10505
10506   gcc_assert (arm_reg0 != IP_REGNUM);
10507
10508   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10509   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10510   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
10511
10512   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
10513   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
10514
10515   return "";
10516 }
10517
10518 /* Output a move from an fpa register to arm registers.
10519    OPERANDS[0] is the first registers of an arm register pair.
10520    OPERANDS[1] is an fpa register.  */
10521 const char *
10522 output_mov_long_double_arm_from_fpa (rtx *operands)
10523 {
10524   int arm_reg0 = REGNO (operands[0]);
10525   rtx ops[3];
10526
10527   gcc_assert (arm_reg0 != IP_REGNUM);
10528
10529   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10530   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10531   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
10532
10533   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
10534   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
10535   return "";
10536 }
10537
10538 /* Output a move from arm registers to arm registers of a long double
10539    OPERANDS[0] is the destination.
10540    OPERANDS[1] is the source.  */
10541 const char *
10542 output_mov_long_double_arm_from_arm (rtx *operands)
10543 {
10544   /* We have to be careful here because the two might overlap.  */
10545   int dest_start = REGNO (operands[0]);
10546   int src_start = REGNO (operands[1]);
10547   rtx ops[2];
10548   int i;
10549
10550   if (dest_start < src_start)
10551     {
10552       for (i = 0; i < 3; i++)
10553         {
10554           ops[0] = gen_rtx_REG (SImode, dest_start + i);
10555           ops[1] = gen_rtx_REG (SImode, src_start + i);
10556           output_asm_insn ("mov%?\t%0, %1", ops);
10557         }
10558     }
10559   else
10560     {
10561       for (i = 2; i >= 0; 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
10569   return "";
10570 }
10571
10572
10573 /* Emit a MOVW/MOVT pair.  */
10574 void arm_emit_movpair (rtx dest, rtx src)
10575 {
10576   emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
10577   emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
10578 }
10579
10580
10581 /* Output a move from arm registers to an fpa registers.
10582    OPERANDS[0] is an fpa register.
10583    OPERANDS[1] is the first registers of an arm register pair.  */
10584 const char *
10585 output_mov_double_fpa_from_arm (rtx *operands)
10586 {
10587   int arm_reg0 = REGNO (operands[1]);
10588   rtx ops[2];
10589
10590   gcc_assert (arm_reg0 != IP_REGNUM);
10591
10592   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10593   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10594   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
10595   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
10596   return "";
10597 }
10598
10599 /* Output a move from an fpa register to arm registers.
10600    OPERANDS[0] is the first registers of an arm register pair.
10601    OPERANDS[1] is an fpa register.  */
10602 const char *
10603 output_mov_double_arm_from_fpa (rtx *operands)
10604 {
10605   int arm_reg0 = REGNO (operands[0]);
10606   rtx ops[2];
10607
10608   gcc_assert (arm_reg0 != IP_REGNUM);
10609
10610   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10611   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10612   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
10613   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
10614   return "";
10615 }
10616
10617 /* Output a move between double words.
10618    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
10619    or MEM<-REG and all MEMs must be offsettable addresses.  */
10620 const char *
10621 output_move_double (rtx *operands)
10622 {
10623   enum rtx_code code0 = GET_CODE (operands[0]);
10624   enum rtx_code code1 = GET_CODE (operands[1]);
10625   rtx otherops[3];
10626
10627   if (code0 == REG)
10628     {
10629       unsigned int reg0 = REGNO (operands[0]);
10630
10631       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10632
10633       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
10634
10635       switch (GET_CODE (XEXP (operands[1], 0)))
10636         {
10637         case REG:
10638           if (TARGET_LDRD
10639               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
10640             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
10641           else
10642             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10643           break;
10644
10645         case PRE_INC:
10646           gcc_assert (TARGET_LDRD);
10647           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
10648           break;
10649
10650         case PRE_DEC:
10651           if (TARGET_LDRD)
10652             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
10653           else
10654             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
10655           break;
10656
10657         case POST_INC:
10658           if (TARGET_LDRD)
10659             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
10660           else
10661             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
10662           break;
10663
10664         case POST_DEC:
10665           gcc_assert (TARGET_LDRD);
10666           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
10667           break;
10668
10669         case PRE_MODIFY:
10670         case POST_MODIFY:
10671           /* Autoicrement addressing modes should never have overlapping
10672              base and destination registers, and overlapping index registers
10673              are already prohibited, so this doesn't need to worry about
10674              fix_cm3_ldrd.  */
10675           otherops[0] = operands[0];
10676           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
10677           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
10678
10679           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
10680             {
10681               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
10682                 {
10683                   /* Registers overlap so split out the increment.  */
10684                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
10685                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
10686                 }
10687               else
10688                 {
10689                   /* Use a single insn if we can.
10690                      FIXME: IWMMXT allows offsets larger than ldrd can
10691                      handle, fix these up with a pair of ldr.  */
10692                   if (TARGET_THUMB2
10693                       || GET_CODE (otherops[2]) != CONST_INT
10694                       || (INTVAL (otherops[2]) > -256
10695                           && INTVAL (otherops[2]) < 256))
10696                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
10697                   else
10698                     {
10699                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10700                       output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10701                     }
10702                 }
10703             }
10704           else
10705             {
10706               /* Use a single insn if we can.
10707                  FIXME: IWMMXT allows offsets larger than ldrd can handle,
10708                  fix these up with a pair of ldr.  */
10709               if (TARGET_THUMB2
10710                   || GET_CODE (otherops[2]) != CONST_INT
10711                   || (INTVAL (otherops[2]) > -256
10712                       && INTVAL (otherops[2]) < 256))
10713                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
10714               else
10715                 {
10716                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10717                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10718                 }
10719             }
10720           break;
10721
10722         case LABEL_REF:
10723         case CONST:
10724           /* We might be able to use ldrd %0, %1 here.  However the range is
10725              different to ldr/adr, and it is broken on some ARMv7-M
10726              implementations.  */
10727           /* Use the second register of the pair to avoid problematic
10728              overlap.  */
10729           otherops[1] = operands[1];
10730           output_asm_insn ("adr%?\t%0, %1", otherops);
10731           operands[1] = otherops[0];
10732           if (TARGET_LDRD)
10733             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10734           else
10735             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
10736           break;
10737
10738           /* ??? This needs checking for thumb2.  */
10739         default:
10740           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
10741                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
10742             {
10743               otherops[0] = operands[0];
10744               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
10745               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
10746
10747               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
10748                 {
10749                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10750                     {
10751                       switch ((int) INTVAL (otherops[2]))
10752                         {
10753                         case -8:
10754                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
10755                           return "";
10756                         case -4:
10757                           if (TARGET_THUMB2)
10758                             break;
10759                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
10760                           return "";
10761                         case 4:
10762                           if (TARGET_THUMB2)
10763                             break;
10764                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
10765                           return "";
10766                         }
10767                     }
10768                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
10769                   operands[1] = otherops[0];
10770                   if (TARGET_LDRD
10771                       && (GET_CODE (otherops[2]) == REG
10772                           || TARGET_THUMB2
10773                           || (GET_CODE (otherops[2]) == CONST_INT
10774                               && INTVAL (otherops[2]) > -256
10775                               && INTVAL (otherops[2]) < 256)))
10776                     {
10777                       if (reg_overlap_mentioned_p (operands[0],
10778                                                    otherops[2]))
10779                         {
10780                           rtx tmp;
10781                           /* Swap base and index registers over to
10782                              avoid a conflict.  */
10783                           tmp = otherops[1];
10784                           otherops[1] = otherops[2];
10785                           otherops[2] = tmp;
10786                         }
10787                       /* If both registers conflict, it will usually
10788                          have been fixed by a splitter.  */
10789                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
10790                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
10791                         {
10792                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
10793                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10794                         }
10795                       else
10796                         {
10797                           otherops[0] = operands[0];
10798                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
10799                         }
10800                       return "";
10801                     }
10802
10803                   if (GET_CODE (otherops[2]) == CONST_INT)
10804                     {
10805                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
10806                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
10807                       else
10808                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
10809                     }
10810                   else
10811                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
10812                 }
10813               else
10814                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
10815
10816               if (TARGET_LDRD)
10817                 return "ldr%(d%)\t%0, [%1]";
10818
10819               return "ldm%(ia%)\t%1, %M0";
10820             }
10821           else
10822             {
10823               otherops[1] = adjust_address (operands[1], SImode, 4);
10824               /* Take care of overlapping base/data reg.  */
10825               if (reg_mentioned_p (operands[0], operands[1]))
10826                 {
10827                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10828                   output_asm_insn ("ldr%?\t%0, %1", operands);
10829                 }
10830               else
10831                 {
10832                   output_asm_insn ("ldr%?\t%0, %1", operands);
10833                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10834                 }
10835             }
10836         }
10837     }
10838   else
10839     {
10840       /* Constraints should ensure this.  */
10841       gcc_assert (code0 == MEM && code1 == REG);
10842       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
10843
10844       switch (GET_CODE (XEXP (operands[0], 0)))
10845         {
10846         case REG:
10847           if (TARGET_LDRD)
10848             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
10849           else
10850             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10851           break;
10852
10853         case PRE_INC:
10854           gcc_assert (TARGET_LDRD);
10855           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
10856           break;
10857
10858         case PRE_DEC:
10859           if (TARGET_LDRD)
10860             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
10861           else
10862             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
10863           break;
10864
10865         case POST_INC:
10866           if (TARGET_LDRD)
10867             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
10868           else
10869             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
10870           break;
10871
10872         case POST_DEC:
10873           gcc_assert (TARGET_LDRD);
10874           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
10875           break;
10876
10877         case PRE_MODIFY:
10878         case POST_MODIFY:
10879           otherops[0] = operands[1];
10880           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
10881           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
10882
10883           /* IWMMXT allows offsets larger than ldrd can handle,
10884              fix these up with a pair of ldr.  */
10885           if (!TARGET_THUMB2
10886               && GET_CODE (otherops[2]) == CONST_INT
10887               && (INTVAL(otherops[2]) <= -256
10888                   || INTVAL(otherops[2]) >= 256))
10889             {
10890               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10891                 {
10892                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10893                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10894                 }
10895               else
10896                 {
10897                   output_asm_insn ("ldr%?\t%H0, [%1, #4]", otherops);
10898                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10899                 }
10900             }
10901           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10902             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
10903           else
10904             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
10905           break;
10906
10907         case PLUS:
10908           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
10909           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10910             {
10911               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
10912                 {
10913                 case -8:
10914                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
10915                   return "";
10916
10917                 case -4:
10918                   if (TARGET_THUMB2)
10919                     break;
10920                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
10921                   return "";
10922
10923                 case 4:
10924                   if (TARGET_THUMB2)
10925                     break;
10926                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
10927                   return "";
10928                 }
10929             }
10930           if (TARGET_LDRD
10931               && (GET_CODE (otherops[2]) == REG
10932                   || TARGET_THUMB2
10933                   || (GET_CODE (otherops[2]) == CONST_INT
10934                       && INTVAL (otherops[2]) > -256
10935                       && INTVAL (otherops[2]) < 256)))
10936             {
10937               otherops[0] = operands[1];
10938               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
10939               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
10940               return "";
10941             }
10942           /* Fall through */
10943
10944         default:
10945           otherops[0] = adjust_address (operands[0], SImode, 4);
10946           otherops[1] = operands[1];
10947           output_asm_insn ("str%?\t%1, %0", operands);
10948           output_asm_insn ("str%?\t%H1, %0", otherops);
10949         }
10950     }
10951
10952   return "";
10953 }
10954
10955 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
10956    handles MEMs accepted by neon_vector_mem_operand with TYPE=1.  */
10957
10958 const char *
10959 output_move_quad (rtx *operands)
10960 {
10961   if (REG_P (operands[0]))
10962     {
10963       /* Load, or reg->reg move.  */
10964
10965       if (MEM_P (operands[1]))
10966         {
10967           switch (GET_CODE (XEXP (operands[1], 0)))
10968             {
10969             case REG:
10970               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10971               break;
10972
10973             case LABEL_REF:
10974             case CONST:
10975               output_asm_insn ("adr%?\t%0, %1", operands);
10976               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
10977               break;
10978
10979             default:
10980               gcc_unreachable ();
10981             }
10982         }
10983       else
10984         {
10985           rtx ops[2];
10986           int dest, src, i;
10987
10988           gcc_assert (REG_P (operands[1]));
10989
10990           dest = REGNO (operands[0]);
10991           src = REGNO (operands[1]);
10992
10993           /* This seems pretty dumb, but hopefully GCC won't try to do it
10994              very often.  */
10995           if (dest < src)
10996             for (i = 0; i < 4; i++)
10997               {
10998                 ops[0] = gen_rtx_REG (SImode, dest + i);
10999                 ops[1] = gen_rtx_REG (SImode, src + i);
11000                 output_asm_insn ("mov%?\t%0, %1", ops);
11001               }
11002           else
11003             for (i = 3; i >= 0; i--)
11004               {
11005                 ops[0] = gen_rtx_REG (SImode, dest + i);
11006                 ops[1] = gen_rtx_REG (SImode, src + i);
11007                 output_asm_insn ("mov%?\t%0, %1", ops);
11008               }
11009         }
11010     }
11011   else
11012     {
11013       gcc_assert (MEM_P (operands[0]));
11014       gcc_assert (REG_P (operands[1]));
11015       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
11016
11017       switch (GET_CODE (XEXP (operands[0], 0)))
11018         {
11019         case REG:
11020           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
11021           break;
11022
11023         default:
11024           gcc_unreachable ();
11025         }
11026     }
11027
11028   return "";
11029 }
11030
11031 /* Output a VFP load or store instruction.  */
11032
11033 const char *
11034 output_move_vfp (rtx *operands)
11035 {
11036   rtx reg, mem, addr, ops[2];
11037   int load = REG_P (operands[0]);
11038   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
11039   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
11040   const char *templ;
11041   char buff[50];
11042   enum machine_mode mode;
11043
11044   reg = operands[!load];
11045   mem = operands[load];
11046
11047   mode = GET_MODE (reg);
11048
11049   gcc_assert (REG_P (reg));
11050   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
11051   gcc_assert (mode == SFmode
11052               || mode == DFmode
11053               || mode == SImode
11054               || mode == DImode
11055               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
11056   gcc_assert (MEM_P (mem));
11057
11058   addr = XEXP (mem, 0);
11059
11060   switch (GET_CODE (addr))
11061     {
11062     case PRE_DEC:
11063       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
11064       ops[0] = XEXP (addr, 0);
11065       ops[1] = reg;
11066       break;
11067
11068     case POST_INC:
11069       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
11070       ops[0] = XEXP (addr, 0);
11071       ops[1] = reg;
11072       break;
11073
11074     default:
11075       templ = "f%s%c%%?\t%%%s0, %%1%s";
11076       ops[0] = reg;
11077       ops[1] = mem;
11078       break;
11079     }
11080
11081   sprintf (buff, templ,
11082            load ? "ld" : "st",
11083            dp ? 'd' : 's',
11084            dp ? "P" : "",
11085            integer_p ? "\t%@ int" : "");
11086   output_asm_insn (buff, ops);
11087
11088   return "";
11089 }
11090
11091 /* Output a Neon quad-word load or store, or a load or store for
11092    larger structure modes.
11093
11094    WARNING: The ordering of elements is weird in big-endian mode,
11095    because we use VSTM, as required by the EABI.  GCC RTL defines
11096    element ordering based on in-memory order.  This can be differ
11097    from the architectural ordering of elements within a NEON register.
11098    The intrinsics defined in arm_neon.h use the NEON register element
11099    ordering, not the GCC RTL element ordering.
11100
11101    For example, the in-memory ordering of a big-endian a quadword
11102    vector with 16-bit elements when stored from register pair {d0,d1}
11103    will be (lowest address first, d0[N] is NEON register element N):
11104
11105      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
11106
11107    When necessary, quadword registers (dN, dN+1) are moved to ARM
11108    registers from rN in the order:
11109
11110      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
11111
11112    So that STM/LDM can be used on vectors in ARM registers, and the
11113    same memory layout will result as if VSTM/VLDM were used.  */
11114
11115 const char *
11116 output_move_neon (rtx *operands)
11117 {
11118   rtx reg, mem, addr, ops[2];
11119   int regno, load = REG_P (operands[0]);
11120   const char *templ;
11121   char buff[50];
11122   enum machine_mode mode;
11123
11124   reg = operands[!load];
11125   mem = operands[load];
11126
11127   mode = GET_MODE (reg);
11128
11129   gcc_assert (REG_P (reg));
11130   regno = REGNO (reg);
11131   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
11132               || NEON_REGNO_OK_FOR_QUAD (regno));
11133   gcc_assert (VALID_NEON_DREG_MODE (mode)
11134               || VALID_NEON_QREG_MODE (mode)
11135               || VALID_NEON_STRUCT_MODE (mode));
11136   gcc_assert (MEM_P (mem));
11137
11138   addr = XEXP (mem, 0);
11139
11140   /* Strip off const from addresses like (const (plus (...))).  */
11141   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
11142     addr = XEXP (addr, 0);
11143
11144   switch (GET_CODE (addr))
11145     {
11146     case POST_INC:
11147       templ = "v%smia%%?\t%%0!, %%h1";
11148       ops[0] = XEXP (addr, 0);
11149       ops[1] = reg;
11150       break;
11151
11152     case PRE_DEC:
11153       /* FIXME: We should be using vld1/vst1 here in BE mode?  */
11154       templ = "v%smdb%%?\t%%0!, %%h1";
11155       ops[0] = XEXP (addr, 0);
11156       ops[1] = reg;
11157       break;
11158     
11159     case POST_MODIFY:
11160       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
11161       gcc_unreachable ();
11162
11163     case LABEL_REF:
11164     case PLUS:
11165       {
11166         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
11167         int i;
11168         int overlap = -1;
11169         for (i = 0; i < nregs; i++)
11170           {
11171             /* We're only using DImode here because it's a convenient size.  */
11172             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
11173             ops[1] = adjust_address (mem, SImode, 8 * i);
11174             if (reg_overlap_mentioned_p (ops[0], mem))
11175               {
11176                 gcc_assert (overlap == -1);
11177                 overlap = i;
11178               }
11179             else
11180               {
11181                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
11182                 output_asm_insn (buff, ops);
11183               }
11184           }
11185         if (overlap != -1)
11186           {
11187             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
11188             ops[1] = adjust_address (mem, SImode, 8 * overlap);
11189             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
11190             output_asm_insn (buff, ops);
11191           }
11192
11193         return "";
11194       }
11195
11196     default:
11197       templ = "v%smia%%?\t%%m0, %%h1";
11198       ops[0] = mem;
11199       ops[1] = reg;
11200     }
11201
11202   sprintf (buff, templ, load ? "ld" : "st");
11203   output_asm_insn (buff, ops);
11204
11205   return "";
11206 }
11207
11208 /* Output an ADD r, s, #n where n may be too big for one instruction.
11209    If adding zero to one register, output nothing.  */
11210 const char *
11211 output_add_immediate (rtx *operands)
11212 {
11213   HOST_WIDE_INT n = INTVAL (operands[2]);
11214
11215   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
11216     {
11217       if (n < 0)
11218         output_multi_immediate (operands,
11219                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
11220                                 -n);
11221       else
11222         output_multi_immediate (operands,
11223                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
11224                                 n);
11225     }
11226
11227   return "";
11228 }
11229
11230 /* Output a multiple immediate operation.
11231    OPERANDS is the vector of operands referred to in the output patterns.
11232    INSTR1 is the output pattern to use for the first constant.
11233    INSTR2 is the output pattern to use for subsequent constants.
11234    IMMED_OP is the index of the constant slot in OPERANDS.
11235    N is the constant value.  */
11236 static const char *
11237 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
11238                         int immed_op, HOST_WIDE_INT n)
11239 {
11240 #if HOST_BITS_PER_WIDE_INT > 32
11241   n &= 0xffffffff;
11242 #endif
11243
11244   if (n == 0)
11245     {
11246       /* Quick and easy output.  */
11247       operands[immed_op] = const0_rtx;
11248       output_asm_insn (instr1, operands);
11249     }
11250   else
11251     {
11252       int i;
11253       const char * instr = instr1;
11254
11255       /* Note that n is never zero here (which would give no output).  */
11256       for (i = 0; i < 32; i += 2)
11257         {
11258           if (n & (3 << i))
11259             {
11260               operands[immed_op] = GEN_INT (n & (255 << i));
11261               output_asm_insn (instr, operands);
11262               instr = instr2;
11263               i += 6;
11264             }
11265         }
11266     }
11267
11268   return "";
11269 }
11270
11271 /* Return the name of a shifter operation.  */
11272 static const char *
11273 arm_shift_nmem(enum rtx_code code)
11274 {
11275   switch (code)
11276     {
11277     case ASHIFT:
11278       return ARM_LSL_NAME;
11279
11280     case ASHIFTRT:
11281       return "asr";
11282
11283     case LSHIFTRT:
11284       return "lsr";
11285
11286     case ROTATERT:
11287       return "ror";
11288
11289     default:
11290       abort();
11291     }
11292 }
11293
11294 /* Return the appropriate ARM instruction for the operation code.
11295    The returned result should not be overwritten.  OP is the rtx of the
11296    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
11297    was shifted.  */
11298 const char *
11299 arithmetic_instr (rtx op, int shift_first_arg)
11300 {
11301   switch (GET_CODE (op))
11302     {
11303     case PLUS:
11304       return "add";
11305
11306     case MINUS:
11307       return shift_first_arg ? "rsb" : "sub";
11308
11309     case IOR:
11310       return "orr";
11311
11312     case XOR:
11313       return "eor";
11314
11315     case AND:
11316       return "and";
11317
11318     case ASHIFT:
11319     case ASHIFTRT:
11320     case LSHIFTRT:
11321     case ROTATERT:
11322       return arm_shift_nmem(GET_CODE(op));
11323
11324     default:
11325       gcc_unreachable ();
11326     }
11327 }
11328
11329 /* Ensure valid constant shifts and return the appropriate shift mnemonic
11330    for the operation code.  The returned result should not be overwritten.
11331    OP is the rtx code of the shift.
11332    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
11333    shift.  */
11334 static const char *
11335 shift_op (rtx op, HOST_WIDE_INT *amountp)
11336 {
11337   const char * mnem;
11338   enum rtx_code code = GET_CODE (op);
11339
11340   switch (GET_CODE (XEXP (op, 1)))
11341     {
11342     case REG:
11343     case SUBREG:
11344       *amountp = -1;
11345       break;
11346
11347     case CONST_INT:
11348       *amountp = INTVAL (XEXP (op, 1));
11349       break;
11350
11351     default:
11352       gcc_unreachable ();
11353     }
11354
11355   switch (code)
11356     {
11357     case ROTATE:
11358       gcc_assert (*amountp != -1);
11359       *amountp = 32 - *amountp;
11360       code = ROTATERT;
11361
11362       /* Fall through.  */
11363
11364     case ASHIFT:
11365     case ASHIFTRT:
11366     case LSHIFTRT:
11367     case ROTATERT:
11368       mnem = arm_shift_nmem(code);
11369       break;
11370
11371     case MULT:
11372       /* We never have to worry about the amount being other than a
11373          power of 2, since this case can never be reloaded from a reg.  */
11374       gcc_assert (*amountp != -1);
11375       *amountp = int_log2 (*amountp);
11376       return ARM_LSL_NAME;
11377
11378     default:
11379       gcc_unreachable ();
11380     }
11381
11382   if (*amountp != -1)
11383     {
11384       /* This is not 100% correct, but follows from the desire to merge
11385          multiplication by a power of 2 with the recognizer for a
11386          shift.  >=32 is not a valid shift for "lsl", so we must try and
11387          output a shift that produces the correct arithmetical result.
11388          Using lsr #32 is identical except for the fact that the carry bit
11389          is not set correctly if we set the flags; but we never use the
11390          carry bit from such an operation, so we can ignore that.  */
11391       if (code == ROTATERT)
11392         /* Rotate is just modulo 32.  */
11393         *amountp &= 31;
11394       else if (*amountp != (*amountp & 31))
11395         {
11396           if (code == ASHIFT)
11397             mnem = "lsr";
11398           *amountp = 32;
11399         }
11400
11401       /* Shifts of 0 are no-ops.  */
11402       if (*amountp == 0)
11403         return NULL;
11404     }
11405
11406   return mnem;
11407 }
11408
11409 /* Obtain the shift from the POWER of two.  */
11410
11411 static HOST_WIDE_INT
11412 int_log2 (HOST_WIDE_INT power)
11413 {
11414   HOST_WIDE_INT shift = 0;
11415
11416   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
11417     {
11418       gcc_assert (shift <= 31);
11419       shift++;
11420     }
11421
11422   return shift;
11423 }
11424
11425 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
11426    because /bin/as is horribly restrictive.  The judgement about
11427    whether or not each character is 'printable' (and can be output as
11428    is) or not (and must be printed with an octal escape) must be made
11429    with reference to the *host* character set -- the situation is
11430    similar to that discussed in the comments above pp_c_char in
11431    c-pretty-print.c.  */
11432
11433 #define MAX_ASCII_LEN 51
11434
11435 void
11436 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
11437 {
11438   int i;
11439   int len_so_far = 0;
11440
11441   fputs ("\t.ascii\t\"", stream);
11442
11443   for (i = 0; i < len; i++)
11444     {
11445       int c = p[i];
11446
11447       if (len_so_far >= MAX_ASCII_LEN)
11448         {
11449           fputs ("\"\n\t.ascii\t\"", stream);
11450           len_so_far = 0;
11451         }
11452
11453       if (ISPRINT (c))
11454         {
11455           if (c == '\\' || c == '\"')
11456             {
11457               putc ('\\', stream);
11458               len_so_far++;
11459             }
11460           putc (c, stream);
11461           len_so_far++;
11462         }
11463       else
11464         {
11465           fprintf (stream, "\\%03o", c);
11466           len_so_far += 4;
11467         }
11468     }
11469
11470   fputs ("\"\n", stream);
11471 }
11472 \f
11473 /* Compute the register save mask for registers 0 through 12
11474    inclusive.  This code is used by arm_compute_save_reg_mask.  */
11475
11476 static unsigned long
11477 arm_compute_save_reg0_reg12_mask (void)
11478 {
11479   unsigned long func_type = arm_current_func_type ();
11480   unsigned long save_reg_mask = 0;
11481   unsigned int reg;
11482
11483   if (IS_INTERRUPT (func_type))
11484     {
11485       unsigned int max_reg;
11486       /* Interrupt functions must not corrupt any registers,
11487          even call clobbered ones.  If this is a leaf function
11488          we can just examine the registers used by the RTL, but
11489          otherwise we have to assume that whatever function is
11490          called might clobber anything, and so we have to save
11491          all the call-clobbered registers as well.  */
11492       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
11493         /* FIQ handlers have registers r8 - r12 banked, so
11494            we only need to check r0 - r7, Normal ISRs only
11495            bank r14 and r15, so we must check up to r12.
11496            r13 is the stack pointer which is always preserved,
11497            so we do not need to consider it here.  */
11498         max_reg = 7;
11499       else
11500         max_reg = 12;
11501
11502       for (reg = 0; reg <= max_reg; reg++)
11503         if (df_regs_ever_live_p (reg)
11504             || (! current_function_is_leaf && call_used_regs[reg]))
11505           save_reg_mask |= (1 << reg);
11506
11507       /* Also save the pic base register if necessary.  */
11508       if (flag_pic
11509           && !TARGET_SINGLE_PIC_BASE
11510           && arm_pic_register != INVALID_REGNUM
11511           && crtl->uses_pic_offset_table)
11512         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11513     }
11514   else
11515     {
11516       /* In the normal case we only need to save those registers
11517          which are call saved and which are used by this function.  */
11518       for (reg = 0; reg <= 11; reg++)
11519         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
11520           save_reg_mask |= (1 << reg);
11521
11522       /* Handle the frame pointer as a special case.  */
11523       if (frame_pointer_needed)
11524         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
11525
11526       /* If we aren't loading the PIC register,
11527          don't stack it even though it may be live.  */
11528       if (flag_pic
11529           && !TARGET_SINGLE_PIC_BASE
11530           && arm_pic_register != INVALID_REGNUM
11531           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
11532               || crtl->uses_pic_offset_table))
11533         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11534
11535       /* The prologue will copy SP into R0, so save it.  */
11536       if (IS_STACKALIGN (func_type))
11537         save_reg_mask |= 1;
11538     }
11539
11540   /* Save registers so the exception handler can modify them.  */
11541   if (crtl->calls_eh_return)
11542     {
11543       unsigned int i;
11544
11545       for (i = 0; ; i++)
11546         {
11547           reg = EH_RETURN_DATA_REGNO (i);
11548           if (reg == INVALID_REGNUM)
11549             break;
11550           save_reg_mask |= 1 << reg;
11551         }
11552     }
11553
11554   return save_reg_mask;
11555 }
11556
11557
11558 /* Compute the number of bytes used to store the static chain register on the 
11559    stack, above the stack frame. We need to know this accurately to get the
11560    alignment of the rest of the stack frame correct. */
11561
11562 static int arm_compute_static_chain_stack_bytes (void)
11563 {
11564   unsigned long func_type = arm_current_func_type ();
11565   int static_chain_stack_bytes = 0;
11566
11567   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
11568       IS_NESTED (func_type) &&
11569       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
11570     static_chain_stack_bytes = 4;
11571
11572   return static_chain_stack_bytes;
11573 }
11574
11575
11576 /* Compute a bit mask of which registers need to be
11577    saved on the stack for the current function.
11578    This is used by arm_get_frame_offsets, which may add extra registers.  */
11579
11580 static unsigned long
11581 arm_compute_save_reg_mask (void)
11582 {
11583   unsigned int save_reg_mask = 0;
11584   unsigned long func_type = arm_current_func_type ();
11585   unsigned int reg;
11586
11587   if (IS_NAKED (func_type))
11588     /* This should never really happen.  */
11589     return 0;
11590
11591   /* If we are creating a stack frame, then we must save the frame pointer,
11592      IP (which will hold the old stack pointer), LR and the PC.  */
11593   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
11594     save_reg_mask |=
11595       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
11596       | (1 << IP_REGNUM)
11597       | (1 << LR_REGNUM)
11598       | (1 << PC_REGNUM);
11599
11600   /* Volatile functions do not return, so there
11601      is no need to save any other registers.  */
11602   if (IS_VOLATILE (func_type))
11603     return save_reg_mask;
11604
11605   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
11606
11607   /* Decide if we need to save the link register.
11608      Interrupt routines have their own banked link register,
11609      so they never need to save it.
11610      Otherwise if we do not use the link register we do not need to save
11611      it.  If we are pushing other registers onto the stack however, we
11612      can save an instruction in the epilogue by pushing the link register
11613      now and then popping it back into the PC.  This incurs extra memory
11614      accesses though, so we only do it when optimizing for size, and only
11615      if we know that we will not need a fancy return sequence.  */
11616   if (df_regs_ever_live_p (LR_REGNUM)
11617       || (save_reg_mask
11618           && optimize_size
11619           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
11620           && !crtl->calls_eh_return))
11621     save_reg_mask |= 1 << LR_REGNUM;
11622
11623   if (cfun->machine->lr_save_eliminated)
11624     save_reg_mask &= ~ (1 << LR_REGNUM);
11625
11626   if (TARGET_REALLY_IWMMXT
11627       && ((bit_count (save_reg_mask)
11628            + ARM_NUM_INTS (crtl->args.pretend_args_size +
11629                            arm_compute_static_chain_stack_bytes())
11630            ) % 2) != 0)
11631     {
11632       /* The total number of registers that are going to be pushed
11633          onto the stack is odd.  We need to ensure that the stack
11634          is 64-bit aligned before we start to save iWMMXt registers,
11635          and also before we start to create locals.  (A local variable
11636          might be a double or long long which we will load/store using
11637          an iWMMXt instruction).  Therefore we need to push another
11638          ARM register, so that the stack will be 64-bit aligned.  We
11639          try to avoid using the arg registers (r0 -r3) as they might be
11640          used to pass values in a tail call.  */
11641       for (reg = 4; reg <= 12; reg++)
11642         if ((save_reg_mask & (1 << reg)) == 0)
11643           break;
11644
11645       if (reg <= 12)
11646         save_reg_mask |= (1 << reg);
11647       else
11648         {
11649           cfun->machine->sibcall_blocked = 1;
11650           save_reg_mask |= (1 << 3);
11651         }
11652     }
11653
11654   /* We may need to push an additional register for use initializing the
11655      PIC base register.  */
11656   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
11657       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
11658     {
11659       reg = thumb_find_work_register (1 << 4);
11660       if (!call_used_regs[reg])
11661         save_reg_mask |= (1 << reg);
11662     }
11663
11664   return save_reg_mask;
11665 }
11666
11667
11668 /* Compute a bit mask of which registers need to be
11669    saved on the stack for the current function.  */
11670 static unsigned long
11671 thumb1_compute_save_reg_mask (void)
11672 {
11673   unsigned long mask;
11674   unsigned reg;
11675
11676   mask = 0;
11677   for (reg = 0; reg < 12; reg ++)
11678     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11679       mask |= 1 << reg;
11680
11681   if (flag_pic
11682       && !TARGET_SINGLE_PIC_BASE
11683       && arm_pic_register != INVALID_REGNUM
11684       && crtl->uses_pic_offset_table)
11685     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11686
11687   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
11688   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
11689     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
11690
11691   /* LR will also be pushed if any lo regs are pushed.  */
11692   if (mask & 0xff || thumb_force_lr_save ())
11693     mask |= (1 << LR_REGNUM);
11694
11695   /* Make sure we have a low work register if we need one.
11696      We will need one if we are going to push a high register,
11697      but we are not currently intending to push a low register.  */
11698   if ((mask & 0xff) == 0
11699       && ((mask & 0x0f00) || TARGET_BACKTRACE))
11700     {
11701       /* Use thumb_find_work_register to choose which register
11702          we will use.  If the register is live then we will
11703          have to push it.  Use LAST_LO_REGNUM as our fallback
11704          choice for the register to select.  */
11705       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
11706       /* Make sure the register returned by thumb_find_work_register is
11707          not part of the return value.  */
11708       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
11709         reg = LAST_LO_REGNUM;
11710
11711       if (! call_used_regs[reg])
11712         mask |= 1 << reg;
11713     }
11714
11715   /* The 504 below is 8 bytes less than 512 because there are two possible
11716      alignment words.  We can't tell here if they will be present or not so we
11717      have to play it safe and assume that they are. */
11718   if ((CALLER_INTERWORKING_SLOT_SIZE +
11719        ROUND_UP_WORD (get_frame_size ()) +
11720        crtl->outgoing_args_size) >= 504)
11721     {
11722       /* This is the same as the code in thumb1_expand_prologue() which
11723          determines which register to use for stack decrement. */
11724       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
11725         if (mask & (1 << reg))
11726           break;
11727
11728       if (reg > LAST_LO_REGNUM)
11729         {
11730           /* Make sure we have a register available for stack decrement. */
11731           mask |= 1 << LAST_LO_REGNUM;
11732         }
11733     }
11734
11735   return mask;
11736 }
11737
11738
11739 /* Return the number of bytes required to save VFP registers.  */
11740 static int
11741 arm_get_vfp_saved_size (void)
11742 {
11743   unsigned int regno;
11744   int count;
11745   int saved;
11746
11747   saved = 0;
11748   /* Space for saved VFP registers.  */
11749   if (TARGET_HARD_FLOAT && TARGET_VFP)
11750     {
11751       count = 0;
11752       for (regno = FIRST_VFP_REGNUM;
11753            regno < LAST_VFP_REGNUM;
11754            regno += 2)
11755         {
11756           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
11757               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
11758             {
11759               if (count > 0)
11760                 {
11761                   /* Workaround ARM10 VFPr1 bug.  */
11762                   if (count == 2 && !arm_arch6)
11763                     count++;
11764                   saved += count * 8;
11765                 }
11766               count = 0;
11767             }
11768           else
11769             count++;
11770         }
11771       if (count > 0)
11772         {
11773           if (count == 2 && !arm_arch6)
11774             count++;
11775           saved += count * 8;
11776         }
11777     }
11778   return saved;
11779 }
11780
11781
11782 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
11783    everything bar the final return instruction.  */
11784 const char *
11785 output_return_instruction (rtx operand, int really_return, int reverse)
11786 {
11787   char conditional[10];
11788   char instr[100];
11789   unsigned reg;
11790   unsigned long live_regs_mask;
11791   unsigned long func_type;
11792   arm_stack_offsets *offsets;
11793
11794   func_type = arm_current_func_type ();
11795
11796   if (IS_NAKED (func_type))
11797     return "";
11798
11799   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11800     {
11801       /* If this function was declared non-returning, and we have
11802          found a tail call, then we have to trust that the called
11803          function won't return.  */
11804       if (really_return)
11805         {
11806           rtx ops[2];
11807
11808           /* Otherwise, trap an attempted return by aborting.  */
11809           ops[0] = operand;
11810           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
11811                                        : "abort");
11812           assemble_external_libcall (ops[1]);
11813           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
11814         }
11815
11816       return "";
11817     }
11818
11819   gcc_assert (!cfun->calls_alloca || really_return);
11820
11821   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
11822
11823   cfun->machine->return_used_this_function = 1;
11824
11825   offsets = arm_get_frame_offsets ();
11826   live_regs_mask = offsets->saved_regs_mask;
11827
11828   if (live_regs_mask)
11829     {
11830       const char * return_reg;
11831
11832       /* If we do not have any special requirements for function exit
11833          (e.g. interworking) then we can load the return address
11834          directly into the PC.  Otherwise we must load it into LR.  */
11835       if (really_return
11836           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
11837         return_reg = reg_names[PC_REGNUM];
11838       else
11839         return_reg = reg_names[LR_REGNUM];
11840
11841       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
11842         {
11843           /* There are three possible reasons for the IP register
11844              being saved.  1) a stack frame was created, in which case
11845              IP contains the old stack pointer, or 2) an ISR routine
11846              corrupted it, or 3) it was saved to align the stack on
11847              iWMMXt.  In case 1, restore IP into SP, otherwise just
11848              restore IP.  */
11849           if (frame_pointer_needed)
11850             {
11851               live_regs_mask &= ~ (1 << IP_REGNUM);
11852               live_regs_mask |=   (1 << SP_REGNUM);
11853             }
11854           else
11855             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
11856         }
11857
11858       /* On some ARM architectures it is faster to use LDR rather than
11859          LDM to load a single register.  On other architectures, the
11860          cost is the same.  In 26 bit mode, or for exception handlers,
11861          we have to use LDM to load the PC so that the CPSR is also
11862          restored.  */
11863       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11864         if (live_regs_mask == (1U << reg))
11865           break;
11866
11867       if (reg <= LAST_ARM_REGNUM
11868           && (reg != LR_REGNUM
11869               || ! really_return
11870               || ! IS_INTERRUPT (func_type)))
11871         {
11872           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
11873                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
11874         }
11875       else
11876         {
11877           char *p;
11878           int first = 1;
11879
11880           /* Generate the load multiple instruction to restore the
11881              registers.  Note we can get here, even if
11882              frame_pointer_needed is true, but only if sp already
11883              points to the base of the saved core registers.  */
11884           if (live_regs_mask & (1 << SP_REGNUM))
11885             {
11886               unsigned HOST_WIDE_INT stack_adjust;
11887
11888               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
11889               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
11890
11891               if (stack_adjust && arm_arch5 && TARGET_ARM)
11892                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
11893               else
11894                 {
11895                   /* If we can't use ldmib (SA110 bug),
11896                      then try to pop r3 instead.  */
11897                   if (stack_adjust)
11898                     live_regs_mask |= 1 << 3;
11899                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
11900                 }
11901             }
11902           else
11903             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
11904
11905           p = instr + strlen (instr);
11906
11907           for (reg = 0; reg <= SP_REGNUM; reg++)
11908             if (live_regs_mask & (1 << reg))
11909               {
11910                 int l = strlen (reg_names[reg]);
11911
11912                 if (first)
11913                   first = 0;
11914                 else
11915                   {
11916                     memcpy (p, ", ", 2);
11917                     p += 2;
11918                   }
11919
11920                 memcpy (p, "%|", 2);
11921                 memcpy (p + 2, reg_names[reg], l);
11922                 p += l + 2;
11923               }
11924
11925           if (live_regs_mask & (1 << LR_REGNUM))
11926             {
11927               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
11928               /* If returning from an interrupt, restore the CPSR.  */
11929               if (IS_INTERRUPT (func_type))
11930                 strcat (p, "^");
11931             }
11932           else
11933             strcpy (p, "}");
11934         }
11935
11936       output_asm_insn (instr, & operand);
11937
11938       /* See if we need to generate an extra instruction to
11939          perform the actual function return.  */
11940       if (really_return
11941           && func_type != ARM_FT_INTERWORKED
11942           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
11943         {
11944           /* The return has already been handled
11945              by loading the LR into the PC.  */
11946           really_return = 0;
11947         }
11948     }
11949
11950   if (really_return)
11951     {
11952       switch ((int) ARM_FUNC_TYPE (func_type))
11953         {
11954         case ARM_FT_ISR:
11955         case ARM_FT_FIQ:
11956           /* ??? This is wrong for unified assembly syntax.  */
11957           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
11958           break;
11959
11960         case ARM_FT_INTERWORKED:
11961           sprintf (instr, "bx%s\t%%|lr", conditional);
11962           break;
11963
11964         case ARM_FT_EXCEPTION:
11965           /* ??? This is wrong for unified assembly syntax.  */
11966           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
11967           break;
11968
11969         default:
11970           /* Use bx if it's available.  */
11971           if (arm_arch5 || arm_arch4t)
11972             sprintf (instr, "bx%s\t%%|lr", conditional);
11973           else
11974             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
11975           break;
11976         }
11977
11978       output_asm_insn (instr, & operand);
11979     }
11980
11981   return "";
11982 }
11983
11984 /* Write the function name into the code section, directly preceding
11985    the function prologue.
11986
11987    Code will be output similar to this:
11988      t0
11989          .ascii "arm_poke_function_name", 0
11990          .align
11991      t1
11992          .word 0xff000000 + (t1 - t0)
11993      arm_poke_function_name
11994          mov     ip, sp
11995          stmfd   sp!, {fp, ip, lr, pc}
11996          sub     fp, ip, #4
11997
11998    When performing a stack backtrace, code can inspect the value
11999    of 'pc' stored at 'fp' + 0.  If the trace function then looks
12000    at location pc - 12 and the top 8 bits are set, then we know
12001    that there is a function name embedded immediately preceding this
12002    location and has length ((pc[-3]) & 0xff000000).
12003
12004    We assume that pc is declared as a pointer to an unsigned long.
12005
12006    It is of no benefit to output the function name if we are assembling
12007    a leaf function.  These function types will not contain a stack
12008    backtrace structure, therefore it is not possible to determine the
12009    function name.  */
12010 void
12011 arm_poke_function_name (FILE *stream, const char *name)
12012 {
12013   unsigned long alignlength;
12014   unsigned long length;
12015   rtx           x;
12016
12017   length      = strlen (name) + 1;
12018   alignlength = ROUND_UP_WORD (length);
12019
12020   ASM_OUTPUT_ASCII (stream, name, length);
12021   ASM_OUTPUT_ALIGN (stream, 2);
12022   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
12023   assemble_aligned_integer (UNITS_PER_WORD, x);
12024 }
12025
12026 /* Place some comments into the assembler stream
12027    describing the current function.  */
12028 static void
12029 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
12030 {
12031   unsigned long func_type;
12032
12033   if (TARGET_THUMB1)
12034     {
12035       thumb1_output_function_prologue (f, frame_size);
12036       return;
12037     }
12038
12039   /* Sanity check.  */
12040   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
12041
12042   func_type = arm_current_func_type ();
12043
12044   switch ((int) ARM_FUNC_TYPE (func_type))
12045     {
12046     default:
12047     case ARM_FT_NORMAL:
12048       break;
12049     case ARM_FT_INTERWORKED:
12050       asm_fprintf (f, "\t%@ Function supports interworking.\n");
12051       break;
12052     case ARM_FT_ISR:
12053       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
12054       break;
12055     case ARM_FT_FIQ:
12056       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
12057       break;
12058     case ARM_FT_EXCEPTION:
12059       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
12060       break;
12061     }
12062
12063   if (IS_NAKED (func_type))
12064     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
12065
12066   if (IS_VOLATILE (func_type))
12067     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
12068
12069   if (IS_NESTED (func_type))
12070     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
12071   if (IS_STACKALIGN (func_type))
12072     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
12073
12074   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
12075                crtl->args.size,
12076                crtl->args.pretend_args_size, frame_size);
12077
12078   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
12079                frame_pointer_needed,
12080                cfun->machine->uses_anonymous_args);
12081
12082   if (cfun->machine->lr_save_eliminated)
12083     asm_fprintf (f, "\t%@ link register save eliminated.\n");
12084
12085   if (crtl->calls_eh_return)
12086     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
12087
12088 }
12089
12090 const char *
12091 arm_output_epilogue (rtx sibling)
12092 {
12093   int reg;
12094   unsigned long saved_regs_mask;
12095   unsigned long func_type;
12096   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
12097      frame that is $fp + 4 for a non-variadic function.  */
12098   int floats_offset = 0;
12099   rtx operands[3];
12100   FILE * f = asm_out_file;
12101   unsigned int lrm_count = 0;
12102   int really_return = (sibling == NULL);
12103   int start_reg;
12104   arm_stack_offsets *offsets;
12105
12106   /* If we have already generated the return instruction
12107      then it is futile to generate anything else.  */
12108   if (use_return_insn (FALSE, sibling) && 
12109       (cfun->machine->return_used_this_function != 0))
12110     return "";
12111
12112   func_type = arm_current_func_type ();
12113
12114   if (IS_NAKED (func_type))
12115     /* Naked functions don't have epilogues.  */
12116     return "";
12117
12118   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
12119     {
12120       rtx op;
12121
12122       /* A volatile function should never return.  Call abort.  */
12123       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
12124       assemble_external_libcall (op);
12125       output_asm_insn ("bl\t%a0", &op);
12126
12127       return "";
12128     }
12129
12130   /* If we are throwing an exception, then we really must be doing a
12131      return, so we can't tail-call.  */
12132   gcc_assert (!crtl->calls_eh_return || really_return);
12133
12134   offsets = arm_get_frame_offsets ();
12135   saved_regs_mask = offsets->saved_regs_mask;
12136
12137   if (TARGET_IWMMXT)
12138     lrm_count = bit_count (saved_regs_mask);
12139
12140   floats_offset = offsets->saved_args;
12141   /* Compute how far away the floats will be.  */
12142   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
12143     if (saved_regs_mask & (1 << reg))
12144       floats_offset += 4;
12145
12146   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
12147     {
12148       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
12149       int vfp_offset = offsets->frame;
12150
12151       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12152         {
12153           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12154             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12155               {
12156                 floats_offset += 12;
12157                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
12158                              reg, FP_REGNUM, floats_offset - vfp_offset);
12159               }
12160         }
12161       else
12162         {
12163           start_reg = LAST_FPA_REGNUM;
12164
12165           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12166             {
12167               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12168                 {
12169                   floats_offset += 12;
12170
12171                   /* We can't unstack more than four registers at once.  */
12172                   if (start_reg - reg == 3)
12173                     {
12174                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
12175                                    reg, FP_REGNUM, floats_offset - vfp_offset);
12176                       start_reg = reg - 1;
12177                     }
12178                 }
12179               else
12180                 {
12181                   if (reg != start_reg)
12182                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
12183                                  reg + 1, start_reg - reg,
12184                                  FP_REGNUM, floats_offset - vfp_offset);
12185                   start_reg = reg - 1;
12186                 }
12187             }
12188
12189           /* Just in case the last register checked also needs unstacking.  */
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         }
12195
12196       if (TARGET_HARD_FLOAT && TARGET_VFP)
12197         {
12198           int saved_size;
12199
12200           /* The fldmd insns do not have base+offset addressing
12201              modes, so we use IP to hold the address.  */
12202           saved_size = arm_get_vfp_saved_size ();
12203
12204           if (saved_size > 0)
12205             {
12206               floats_offset += saved_size;
12207               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
12208                            FP_REGNUM, floats_offset - vfp_offset);
12209             }
12210           start_reg = FIRST_VFP_REGNUM;
12211           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12212             {
12213               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12214                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12215                 {
12216                   if (start_reg != reg)
12217                     vfp_output_fldmd (f, IP_REGNUM,
12218                                       (start_reg - FIRST_VFP_REGNUM) / 2,
12219                                       (reg - start_reg) / 2);
12220                   start_reg = reg + 2;
12221                 }
12222             }
12223           if (start_reg != reg)
12224             vfp_output_fldmd (f, IP_REGNUM,
12225                               (start_reg - FIRST_VFP_REGNUM) / 2,
12226                               (reg - start_reg) / 2);
12227         }
12228
12229       if (TARGET_IWMMXT)
12230         {
12231           /* The frame pointer is guaranteed to be non-double-word aligned.
12232              This is because it is set to (old_stack_pointer - 4) and the
12233              old_stack_pointer was double word aligned.  Thus the offset to
12234              the iWMMXt registers to be loaded must also be non-double-word
12235              sized, so that the resultant address *is* double-word aligned.
12236              We can ignore floats_offset since that was already included in
12237              the live_regs_mask.  */
12238           lrm_count += (lrm_count % 2 ? 2 : 1);
12239
12240           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
12241             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12242               {
12243                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
12244                              reg, FP_REGNUM, lrm_count * 4);
12245                 lrm_count += 2;
12246               }
12247         }
12248
12249       /* saved_regs_mask should contain the IP, which at the time of stack
12250          frame generation actually contains the old stack pointer.  So a
12251          quick way to unwind the stack is just pop the IP register directly
12252          into the stack pointer.  */
12253       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
12254       saved_regs_mask &= ~ (1 << IP_REGNUM);
12255       saved_regs_mask |=   (1 << SP_REGNUM);
12256
12257       /* There are two registers left in saved_regs_mask - LR and PC.  We
12258          only need to restore the LR register (the return address), but to
12259          save time we can load it directly into the PC, unless we need a
12260          special function exit sequence, or we are not really returning.  */
12261       if (really_return
12262           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
12263           && !crtl->calls_eh_return)
12264         /* Delete the LR from the register mask, so that the LR on
12265            the stack is loaded into the PC in the register mask.  */
12266         saved_regs_mask &= ~ (1 << LR_REGNUM);
12267       else
12268         saved_regs_mask &= ~ (1 << PC_REGNUM);
12269
12270       /* We must use SP as the base register, because SP is one of the
12271          registers being restored.  If an interrupt or page fault
12272          happens in the ldm instruction, the SP might or might not
12273          have been restored.  That would be bad, as then SP will no
12274          longer indicate the safe area of stack, and we can get stack
12275          corruption.  Using SP as the base register means that it will
12276          be reset correctly to the original value, should an interrupt
12277          occur.  If the stack pointer already points at the right
12278          place, then omit the subtraction.  */
12279       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
12280           || cfun->calls_alloca)
12281         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
12282                      4 * bit_count (saved_regs_mask));
12283       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
12284
12285       if (IS_INTERRUPT (func_type))
12286         /* Interrupt handlers will have pushed the
12287            IP onto the stack, so restore it now.  */
12288         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
12289     }
12290   else
12291     {
12292       /* This branch is executed for ARM mode (non-apcs frames) and
12293          Thumb-2 mode. Frame layout is essentially the same for those
12294          cases, except that in ARM mode frame pointer points to the
12295          first saved register, while in Thumb-2 mode the frame pointer points
12296          to the last saved register.
12297
12298          It is possible to make frame pointer point to last saved
12299          register in both cases, and remove some conditionals below.
12300          That means that fp setup in prologue would be just "mov fp, sp"
12301          and sp restore in epilogue would be just "mov sp, fp", whereas
12302          now we have to use add/sub in those cases. However, the value
12303          of that would be marginal, as both mov and add/sub are 32-bit
12304          in ARM mode, and it would require extra conditionals
12305          in arm_expand_prologue to distingish ARM-apcs-frame case
12306          (where frame pointer is required to point at first register)
12307          and ARM-non-apcs-frame. Therefore, such change is postponed
12308          until real need arise.  */
12309       unsigned HOST_WIDE_INT amount;
12310       int rfe;
12311       /* Restore stack pointer if necessary.  */
12312       if (TARGET_ARM && frame_pointer_needed)
12313         {
12314           operands[0] = stack_pointer_rtx;
12315           operands[1] = hard_frame_pointer_rtx;
12316           
12317           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
12318           output_add_immediate (operands);
12319         }
12320       else
12321         {
12322           if (frame_pointer_needed)
12323             {
12324               /* For Thumb-2 restore sp from the frame pointer.
12325                  Operand restrictions mean we have to incrememnt FP, then copy
12326                  to SP.  */
12327               amount = offsets->locals_base - offsets->saved_regs;
12328               operands[0] = hard_frame_pointer_rtx;
12329             }
12330           else
12331             {
12332               unsigned long count;
12333               operands[0] = stack_pointer_rtx;
12334               amount = offsets->outgoing_args - offsets->saved_regs;
12335               /* pop call clobbered registers if it avoids a
12336                  separate stack adjustment.  */
12337               count = offsets->saved_regs - offsets->saved_args;
12338               if (optimize_size
12339                   && count != 0
12340                   && !crtl->calls_eh_return
12341                   && bit_count(saved_regs_mask) * 4 == count
12342                   && !IS_INTERRUPT (func_type)
12343                   && !crtl->tail_call_emit)
12344                 {
12345                   unsigned long mask;
12346                   mask = (1 << (arm_size_return_regs() / 4)) - 1;
12347                   mask ^= 0xf;
12348                   mask &= ~saved_regs_mask;
12349                   reg = 0;
12350                   while (bit_count (mask) * 4 > amount)
12351                     {
12352                       while ((mask & (1 << reg)) == 0)
12353                         reg++;
12354                       mask &= ~(1 << reg);
12355                     }
12356                   if (bit_count (mask) * 4 == amount) {
12357                       amount = 0;
12358                       saved_regs_mask |= mask;
12359                   }
12360                 }
12361             }
12362           
12363           if (amount)
12364             {
12365               operands[1] = operands[0];
12366               operands[2] = GEN_INT (amount);
12367               output_add_immediate (operands);
12368             }
12369           if (frame_pointer_needed)
12370             asm_fprintf (f, "\tmov\t%r, %r\n",
12371                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
12372         }
12373
12374       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12375         {
12376           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
12377             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12378               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
12379                            reg, SP_REGNUM);
12380         }
12381       else
12382         {
12383           start_reg = FIRST_FPA_REGNUM;
12384
12385           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
12386             {
12387               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12388                 {
12389                   if (reg - start_reg == 3)
12390                     {
12391                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
12392                                    start_reg, SP_REGNUM);
12393                       start_reg = reg + 1;
12394                     }
12395                 }
12396               else
12397                 {
12398                   if (reg != start_reg)
12399                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
12400                                  start_reg, reg - start_reg,
12401                                  SP_REGNUM);
12402
12403                   start_reg = reg + 1;
12404                 }
12405             }
12406
12407           /* Just in case the last register checked also needs unstacking.  */
12408           if (reg != start_reg)
12409             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
12410                          start_reg, reg - start_reg, SP_REGNUM);
12411         }
12412
12413       if (TARGET_HARD_FLOAT && TARGET_VFP)
12414         {
12415           start_reg = FIRST_VFP_REGNUM;
12416           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12417             {
12418               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12419                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12420                 {
12421                   if (start_reg != reg)
12422                     vfp_output_fldmd (f, SP_REGNUM,
12423                                       (start_reg - FIRST_VFP_REGNUM) / 2,
12424                                       (reg - start_reg) / 2);
12425                   start_reg = reg + 2;
12426                 }
12427             }
12428           if (start_reg != reg)
12429             vfp_output_fldmd (f, SP_REGNUM,
12430                               (start_reg - FIRST_VFP_REGNUM) / 2,
12431                               (reg - start_reg) / 2);
12432         }
12433       if (TARGET_IWMMXT)
12434         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
12435           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12436             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
12437
12438       /* If we can, restore the LR into the PC.  */
12439       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
12440           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
12441           && !IS_STACKALIGN (func_type)
12442           && really_return
12443           && crtl->args.pretend_args_size == 0
12444           && saved_regs_mask & (1 << LR_REGNUM)
12445           && !crtl->calls_eh_return)
12446         {
12447           saved_regs_mask &= ~ (1 << LR_REGNUM);
12448           saved_regs_mask |=   (1 << PC_REGNUM);
12449           rfe = IS_INTERRUPT (func_type);
12450         }
12451       else
12452         rfe = 0;
12453
12454       /* Load the registers off the stack.  If we only have one register
12455          to load use the LDR instruction - it is faster.  For Thumb-2
12456          always use pop and the assembler will pick the best instruction.*/
12457       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
12458           && !IS_INTERRUPT(func_type))
12459         {
12460           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
12461         }
12462       else if (saved_regs_mask)
12463         {
12464           if (saved_regs_mask & (1 << SP_REGNUM))
12465             /* Note - write back to the stack register is not enabled
12466                (i.e. "ldmfd sp!...").  We know that the stack pointer is
12467                in the list of registers and if we add writeback the
12468                instruction becomes UNPREDICTABLE.  */
12469             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
12470                              rfe);
12471           else if (TARGET_ARM)
12472             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
12473                              rfe);
12474           else
12475             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
12476         }
12477
12478       if (crtl->args.pretend_args_size)
12479         {
12480           /* Unwind the pre-pushed regs.  */
12481           operands[0] = operands[1] = stack_pointer_rtx;
12482           operands[2] = GEN_INT (crtl->args.pretend_args_size);
12483           output_add_immediate (operands);
12484         }
12485     }
12486
12487   /* We may have already restored PC directly from the stack.  */
12488   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
12489     return "";
12490
12491   /* Stack adjustment for exception handler.  */
12492   if (crtl->calls_eh_return)
12493     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
12494                  ARM_EH_STACKADJ_REGNUM);
12495
12496   /* Generate the return instruction.  */
12497   switch ((int) ARM_FUNC_TYPE (func_type))
12498     {
12499     case ARM_FT_ISR:
12500     case ARM_FT_FIQ:
12501       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
12502       break;
12503
12504     case ARM_FT_EXCEPTION:
12505       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
12506       break;
12507
12508     case ARM_FT_INTERWORKED:
12509       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
12510       break;
12511
12512     default:
12513       if (IS_STACKALIGN (func_type))
12514         {
12515           /* See comment in arm_expand_prologue.  */
12516           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
12517         }
12518       if (arm_arch5 || arm_arch4t)
12519         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
12520       else
12521         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
12522       break;
12523     }
12524
12525   return "";
12526 }
12527
12528 static void
12529 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
12530                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
12531 {
12532   arm_stack_offsets *offsets;
12533
12534   if (TARGET_THUMB1)
12535     {
12536       int regno;
12537
12538       /* Emit any call-via-reg trampolines that are needed for v4t support
12539          of call_reg and call_value_reg type insns.  */
12540       for (regno = 0; regno < LR_REGNUM; regno++)
12541         {
12542           rtx label = cfun->machine->call_via[regno];
12543
12544           if (label != NULL)
12545             {
12546               switch_to_section (function_section (current_function_decl));
12547               targetm.asm_out.internal_label (asm_out_file, "L",
12548                                               CODE_LABEL_NUMBER (label));
12549               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
12550             }
12551         }
12552
12553       /* ??? Probably not safe to set this here, since it assumes that a
12554          function will be emitted as assembly immediately after we generate
12555          RTL for it.  This does not happen for inline functions.  */
12556       cfun->machine->return_used_this_function = 0;
12557     }
12558   else /* TARGET_32BIT */
12559     {
12560       /* We need to take into account any stack-frame rounding.  */
12561       offsets = arm_get_frame_offsets ();
12562
12563       gcc_assert (!use_return_insn (FALSE, NULL)
12564                   || (cfun->machine->return_used_this_function != 0)
12565                   || offsets->saved_regs == offsets->outgoing_args
12566                   || frame_pointer_needed);
12567
12568       /* Reset the ARM-specific per-function variables.  */
12569       after_arm_reorg = 0;
12570     }
12571 }
12572
12573 /* Generate and emit an insn that we will recognize as a push_multi.
12574    Unfortunately, since this insn does not reflect very well the actual
12575    semantics of the operation, we need to annotate the insn for the benefit
12576    of DWARF2 frame unwind information.  */
12577 static rtx
12578 emit_multi_reg_push (unsigned long mask)
12579 {
12580   int num_regs = 0;
12581   int num_dwarf_regs;
12582   int i, j;
12583   rtx par;
12584   rtx dwarf;
12585   int dwarf_par_index;
12586   rtx tmp, reg;
12587
12588   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12589     if (mask & (1 << i))
12590       num_regs++;
12591
12592   gcc_assert (num_regs && num_regs <= 16);
12593
12594   /* We don't record the PC in the dwarf frame information.  */
12595   num_dwarf_regs = num_regs;
12596   if (mask & (1 << PC_REGNUM))
12597     num_dwarf_regs--;
12598
12599   /* For the body of the insn we are going to generate an UNSPEC in
12600      parallel with several USEs.  This allows the insn to be recognized
12601      by the push_multi pattern in the arm.md file.  The insn looks
12602      something like this:
12603
12604        (parallel [
12605            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
12606                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
12607            (use (reg:SI 11 fp))
12608            (use (reg:SI 12 ip))
12609            (use (reg:SI 14 lr))
12610            (use (reg:SI 15 pc))
12611         ])
12612
12613      For the frame note however, we try to be more explicit and actually
12614      show each register being stored into the stack frame, plus a (single)
12615      decrement of the stack pointer.  We do it this way in order to be
12616      friendly to the stack unwinding code, which only wants to see a single
12617      stack decrement per instruction.  The RTL we generate for the note looks
12618      something like this:
12619
12620       (sequence [
12621            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
12622            (set (mem:SI (reg:SI sp)) (reg:SI r4))
12623            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
12624            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
12625            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
12626         ])
12627
12628       This sequence is used both by the code to support stack unwinding for
12629       exceptions handlers and the code to generate dwarf2 frame debugging.  */
12630
12631   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
12632   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
12633   dwarf_par_index = 1;
12634
12635   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12636     {
12637       if (mask & (1 << i))
12638         {
12639           reg = gen_rtx_REG (SImode, i);
12640
12641           XVECEXP (par, 0, 0)
12642             = gen_rtx_SET (VOIDmode,
12643                            gen_frame_mem (BLKmode,
12644                                           gen_rtx_PRE_DEC (BLKmode,
12645                                                            stack_pointer_rtx)),
12646                            gen_rtx_UNSPEC (BLKmode,
12647                                            gen_rtvec (1, reg),
12648                                            UNSPEC_PUSH_MULT));
12649
12650           if (i != PC_REGNUM)
12651             {
12652               tmp = gen_rtx_SET (VOIDmode,
12653                                  gen_frame_mem (SImode, stack_pointer_rtx),
12654                                  reg);
12655               RTX_FRAME_RELATED_P (tmp) = 1;
12656               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
12657               dwarf_par_index++;
12658             }
12659
12660           break;
12661         }
12662     }
12663
12664   for (j = 1, i++; j < num_regs; i++)
12665     {
12666       if (mask & (1 << i))
12667         {
12668           reg = gen_rtx_REG (SImode, i);
12669
12670           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
12671
12672           if (i != PC_REGNUM)
12673             {
12674               tmp
12675                 = gen_rtx_SET (VOIDmode,
12676                                gen_frame_mem (SImode,
12677                                               plus_constant (stack_pointer_rtx,
12678                                                              4 * j)),
12679                                reg);
12680               RTX_FRAME_RELATED_P (tmp) = 1;
12681               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
12682             }
12683
12684           j++;
12685         }
12686     }
12687
12688   par = emit_insn (par);
12689
12690   tmp = gen_rtx_SET (VOIDmode,
12691                      stack_pointer_rtx,
12692                      plus_constant (stack_pointer_rtx, -4 * num_regs));
12693   RTX_FRAME_RELATED_P (tmp) = 1;
12694   XVECEXP (dwarf, 0, 0) = tmp;
12695
12696   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12697
12698   return par;
12699 }
12700
12701 /* Calculate the size of the return value that is passed in registers.  */
12702 static unsigned
12703 arm_size_return_regs (void)
12704 {
12705   enum machine_mode mode;
12706
12707   if (crtl->return_rtx != 0)
12708     mode = GET_MODE (crtl->return_rtx);
12709   else
12710     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12711
12712   return GET_MODE_SIZE (mode);
12713 }
12714
12715 static rtx
12716 emit_sfm (int base_reg, int count)
12717 {
12718   rtx par;
12719   rtx dwarf;
12720   rtx tmp, reg;
12721   int i;
12722
12723   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12724   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12725
12726   reg = gen_rtx_REG (XFmode, base_reg++);
12727
12728   XVECEXP (par, 0, 0)
12729     = gen_rtx_SET (VOIDmode,
12730                    gen_frame_mem (BLKmode,
12731                                   gen_rtx_PRE_DEC (BLKmode,
12732                                                    stack_pointer_rtx)),
12733                    gen_rtx_UNSPEC (BLKmode,
12734                                    gen_rtvec (1, reg),
12735                                    UNSPEC_PUSH_MULT));
12736   tmp = gen_rtx_SET (VOIDmode,
12737                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
12738   RTX_FRAME_RELATED_P (tmp) = 1;
12739   XVECEXP (dwarf, 0, 1) = tmp;
12740
12741   for (i = 1; i < count; i++)
12742     {
12743       reg = gen_rtx_REG (XFmode, base_reg++);
12744       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12745
12746       tmp = gen_rtx_SET (VOIDmode,
12747                          gen_frame_mem (XFmode,
12748                                         plus_constant (stack_pointer_rtx,
12749                                                        i * 12)),
12750                          reg);
12751       RTX_FRAME_RELATED_P (tmp) = 1;
12752       XVECEXP (dwarf, 0, i + 1) = tmp;
12753     }
12754
12755   tmp = gen_rtx_SET (VOIDmode,
12756                      stack_pointer_rtx,
12757                      plus_constant (stack_pointer_rtx, -12 * count));
12758
12759   RTX_FRAME_RELATED_P (tmp) = 1;
12760   XVECEXP (dwarf, 0, 0) = tmp;
12761
12762   par = emit_insn (par);
12763   add_reg_note (par, REG_FRAME_RELATED_EXPR, dwarf);
12764
12765   return par;
12766 }
12767
12768
12769 /* Return true if the current function needs to save/restore LR.  */
12770
12771 static bool
12772 thumb_force_lr_save (void)
12773 {
12774   return !cfun->machine->lr_save_eliminated
12775          && (!leaf_function_p ()
12776              || thumb_far_jump_used_p ()
12777              || df_regs_ever_live_p (LR_REGNUM));
12778 }
12779
12780
12781 /* Compute the distance from register FROM to register TO.
12782    These can be the arg pointer (26), the soft frame pointer (25),
12783    the stack pointer (13) or the hard frame pointer (11).
12784    In thumb mode r7 is used as the soft frame pointer, if needed.
12785    Typical stack layout looks like this:
12786
12787        old stack pointer -> |    |
12788                              ----
12789                             |    | \
12790                             |    |   saved arguments for
12791                             |    |   vararg functions
12792                             |    | /
12793                               --
12794    hard FP & arg pointer -> |    | \
12795                             |    |   stack
12796                             |    |   frame
12797                             |    | /
12798                               --
12799                             |    | \
12800                             |    |   call saved
12801                             |    |   registers
12802       soft frame pointer -> |    | /
12803                               --
12804                             |    | \
12805                             |    |   local
12806                             |    |   variables
12807      locals base pointer -> |    | /
12808                               --
12809                             |    | \
12810                             |    |   outgoing
12811                             |    |   arguments
12812    current stack pointer -> |    | /
12813                               --
12814
12815   For a given function some or all of these stack components
12816   may not be needed, giving rise to the possibility of
12817   eliminating some of the registers.
12818
12819   The values returned by this function must reflect the behavior
12820   of arm_expand_prologue() and arm_compute_save_reg_mask().
12821
12822   The sign of the number returned reflects the direction of stack
12823   growth, so the values are positive for all eliminations except
12824   from the soft frame pointer to the hard frame pointer.
12825
12826   SFP may point just inside the local variables block to ensure correct
12827   alignment.  */
12828
12829
12830 /* Calculate stack offsets.  These are used to calculate register elimination
12831    offsets and in prologue/epilogue code.  Also calculates which registers
12832    should be saved.  */
12833
12834 static arm_stack_offsets *
12835 arm_get_frame_offsets (void)
12836 {
12837   struct arm_stack_offsets *offsets;
12838   unsigned long func_type;
12839   int leaf;
12840   int saved;
12841   int core_saved;
12842   HOST_WIDE_INT frame_size;
12843   int i;
12844
12845   offsets = &cfun->machine->stack_offsets;
12846
12847   /* We need to know if we are a leaf function.  Unfortunately, it
12848      is possible to be called after start_sequence has been called,
12849      which causes get_insns to return the insns for the sequence,
12850      not the function, which will cause leaf_function_p to return
12851      the incorrect result.
12852
12853      to know about leaf functions once reload has completed, and the
12854      frame size cannot be changed after that time, so we can safely
12855      use the cached value.  */
12856
12857   if (reload_completed)
12858     return offsets;
12859
12860   /* Initially this is the size of the local variables.  It will translated
12861      into an offset once we have determined the size of preceding data.  */
12862   frame_size = ROUND_UP_WORD (get_frame_size ());
12863
12864   leaf = leaf_function_p ();
12865
12866   /* Space for variadic functions.  */
12867   offsets->saved_args = crtl->args.pretend_args_size;
12868
12869   /* In Thumb mode this is incorrect, but never used.  */
12870   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
12871                    arm_compute_static_chain_stack_bytes();
12872
12873   if (TARGET_32BIT)
12874     {
12875       unsigned int regno;
12876
12877       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
12878       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12879       saved = core_saved;
12880
12881       /* We know that SP will be doubleword aligned on entry, and we must
12882          preserve that condition at any subroutine call.  We also require the
12883          soft frame pointer to be doubleword aligned.  */
12884
12885       if (TARGET_REALLY_IWMMXT)
12886         {
12887           /* Check for the call-saved iWMMXt registers.  */
12888           for (regno = FIRST_IWMMXT_REGNUM;
12889                regno <= LAST_IWMMXT_REGNUM;
12890                regno++)
12891             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12892               saved += 8;
12893         }
12894
12895       func_type = arm_current_func_type ();
12896       if (! IS_VOLATILE (func_type))
12897         {
12898           /* Space for saved FPA registers.  */
12899           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
12900             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12901             saved += 12;
12902
12903           /* Space for saved VFP registers.  */
12904           if (TARGET_HARD_FLOAT && TARGET_VFP)
12905             saved += arm_get_vfp_saved_size ();
12906         }
12907     }
12908   else /* TARGET_THUMB1 */
12909     {
12910       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
12911       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12912       saved = core_saved;
12913       if (TARGET_BACKTRACE)
12914         saved += 16;
12915     }
12916
12917   /* Saved registers include the stack frame.  */
12918   offsets->saved_regs = offsets->saved_args + saved +
12919                         arm_compute_static_chain_stack_bytes();
12920   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
12921   /* A leaf function does not need any stack alignment if it has nothing
12922      on the stack.  */
12923   if (leaf && frame_size == 0)
12924     {
12925       offsets->outgoing_args = offsets->soft_frame;
12926       offsets->locals_base = offsets->soft_frame;
12927       return offsets;
12928     }
12929
12930   /* Ensure SFP has the correct alignment.  */
12931   if (ARM_DOUBLEWORD_ALIGN
12932       && (offsets->soft_frame & 7))
12933     {
12934       offsets->soft_frame += 4;
12935       /* Try to align stack by pushing an extra reg.  Don't bother doing this
12936          when there is a stack frame as the alignment will be rolled into
12937          the normal stack adjustment.  */
12938       if (frame_size + crtl->outgoing_args_size == 0)
12939         {
12940           int reg = -1;
12941
12942           /* If it is safe to use r3, then do so.  This sometimes 
12943              generates better code on Thumb-2 by avoiding the need to
12944              use 32-bit push/pop instructions.  */
12945           if (!crtl->tail_call_emit
12946               && arm_size_return_regs () <= 12)
12947             {
12948               reg = 3;
12949             }
12950           else
12951             for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
12952               {
12953                 if ((offsets->saved_regs_mask & (1 << i)) == 0)
12954                   {
12955                     reg = i;
12956                     break;
12957                   }
12958               }
12959
12960           if (reg != -1)
12961             {
12962               offsets->saved_regs += 4;
12963               offsets->saved_regs_mask |= (1 << reg);
12964             }
12965         }
12966     }
12967
12968   offsets->locals_base = offsets->soft_frame + frame_size;
12969   offsets->outgoing_args = (offsets->locals_base
12970                             + crtl->outgoing_args_size);
12971
12972   if (ARM_DOUBLEWORD_ALIGN)
12973     {
12974       /* Ensure SP remains doubleword aligned.  */
12975       if (offsets->outgoing_args & 7)
12976         offsets->outgoing_args += 4;
12977       gcc_assert (!(offsets->outgoing_args & 7));
12978     }
12979
12980   return offsets;
12981 }
12982
12983
12984 /* Calculate the relative offsets for the different stack pointers.  Positive
12985    offsets are in the direction of stack growth.  */
12986
12987 HOST_WIDE_INT
12988 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12989 {
12990   arm_stack_offsets *offsets;
12991
12992   offsets = arm_get_frame_offsets ();
12993
12994   /* OK, now we have enough information to compute the distances.
12995      There must be an entry in these switch tables for each pair
12996      of registers in ELIMINABLE_REGS, even if some of the entries
12997      seem to be redundant or useless.  */
12998   switch (from)
12999     {
13000     case ARG_POINTER_REGNUM:
13001       switch (to)
13002         {
13003         case THUMB_HARD_FRAME_POINTER_REGNUM:
13004           return 0;
13005
13006         case FRAME_POINTER_REGNUM:
13007           /* This is the reverse of the soft frame pointer
13008              to hard frame pointer elimination below.  */
13009           return offsets->soft_frame - offsets->saved_args;
13010
13011         case ARM_HARD_FRAME_POINTER_REGNUM:
13012           /* This is only non-zero in the case where the static chain register
13013              is stored above the frame.  */
13014           return offsets->frame - offsets->saved_args - 4;
13015
13016         case STACK_POINTER_REGNUM:
13017           /* If nothing has been pushed on the stack at all
13018              then this will return -4.  This *is* correct!  */
13019           return offsets->outgoing_args - (offsets->saved_args + 4);
13020
13021         default:
13022           gcc_unreachable ();
13023         }
13024       gcc_unreachable ();
13025
13026     case FRAME_POINTER_REGNUM:
13027       switch (to)
13028         {
13029         case THUMB_HARD_FRAME_POINTER_REGNUM:
13030           return 0;
13031
13032         case ARM_HARD_FRAME_POINTER_REGNUM:
13033           /* The hard frame pointer points to the top entry in the
13034              stack frame.  The soft frame pointer to the bottom entry
13035              in the stack frame.  If there is no stack frame at all,
13036              then they are identical.  */
13037
13038           return offsets->frame - offsets->soft_frame;
13039
13040         case STACK_POINTER_REGNUM:
13041           return offsets->outgoing_args - offsets->soft_frame;
13042
13043         default:
13044           gcc_unreachable ();
13045         }
13046       gcc_unreachable ();
13047
13048     default:
13049       /* You cannot eliminate from the stack pointer.
13050          In theory you could eliminate from the hard frame
13051          pointer to the stack pointer, but this will never
13052          happen, since if a stack frame is not needed the
13053          hard frame pointer will never be used.  */
13054       gcc_unreachable ();
13055     }
13056 }
13057
13058
13059 /* Emit RTL to save coprocessor registers on function entry.  Returns the
13060    number of bytes pushed.  */
13061
13062 static int
13063 arm_save_coproc_regs(void)
13064 {
13065   int saved_size = 0;
13066   unsigned reg;
13067   unsigned start_reg;
13068   rtx insn;
13069
13070   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
13071     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
13072       {
13073         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
13074         insn = gen_rtx_MEM (V2SImode, insn);
13075         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
13076         RTX_FRAME_RELATED_P (insn) = 1;
13077         saved_size += 8;
13078       }
13079
13080   /* Save any floating point call-saved registers used by this
13081      function.  */
13082   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
13083     {
13084       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
13085         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13086           {
13087             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
13088             insn = gen_rtx_MEM (XFmode, insn);
13089             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
13090             RTX_FRAME_RELATED_P (insn) = 1;
13091             saved_size += 12;
13092           }
13093     }
13094   else
13095     {
13096       start_reg = LAST_FPA_REGNUM;
13097
13098       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
13099         {
13100           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
13101             {
13102               if (start_reg - reg == 3)
13103                 {
13104                   insn = emit_sfm (reg, 4);
13105                   RTX_FRAME_RELATED_P (insn) = 1;
13106                   saved_size += 48;
13107                   start_reg = reg - 1;
13108                 }
13109             }
13110           else
13111             {
13112               if (start_reg != reg)
13113                 {
13114                   insn = emit_sfm (reg + 1, start_reg - reg);
13115                   RTX_FRAME_RELATED_P (insn) = 1;
13116                   saved_size += (start_reg - reg) * 12;
13117                 }
13118               start_reg = reg - 1;
13119             }
13120         }
13121
13122       if (start_reg != reg)
13123         {
13124           insn = emit_sfm (reg + 1, start_reg - reg);
13125           saved_size += (start_reg - reg) * 12;
13126           RTX_FRAME_RELATED_P (insn) = 1;
13127         }
13128     }
13129   if (TARGET_HARD_FLOAT && TARGET_VFP)
13130     {
13131       start_reg = FIRST_VFP_REGNUM;
13132
13133       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
13134         {
13135           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
13136               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
13137             {
13138               if (start_reg != reg)
13139                 saved_size += vfp_emit_fstmd (start_reg,
13140                                               (reg - start_reg) / 2);
13141               start_reg = reg + 2;
13142             }
13143         }
13144       if (start_reg != reg)
13145         saved_size += vfp_emit_fstmd (start_reg,
13146                                       (reg - start_reg) / 2);
13147     }
13148   return saved_size;
13149 }
13150
13151
13152 /* Set the Thumb frame pointer from the stack pointer.  */
13153
13154 static void
13155 thumb_set_frame_pointer (arm_stack_offsets *offsets)
13156 {
13157   HOST_WIDE_INT amount;
13158   rtx insn, dwarf;
13159
13160   amount = offsets->outgoing_args - offsets->locals_base;
13161   if (amount < 1024)
13162     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13163                                   stack_pointer_rtx, GEN_INT (amount)));
13164   else
13165     {
13166       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13167       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
13168          expects the first two operands to be the same.  */
13169       if (TARGET_THUMB2)
13170         {
13171           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13172                                         stack_pointer_rtx,
13173                                         hard_frame_pointer_rtx));
13174         }
13175       else
13176         {
13177           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13178                                         hard_frame_pointer_rtx,
13179                                         stack_pointer_rtx));
13180         }
13181       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
13182                            plus_constant (stack_pointer_rtx, amount));
13183       RTX_FRAME_RELATED_P (dwarf) = 1;
13184       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
13185     }
13186
13187   RTX_FRAME_RELATED_P (insn) = 1;
13188 }
13189
13190 /* Generate the prologue instructions for entry into an ARM or Thumb-2
13191    function.  */
13192 void
13193 arm_expand_prologue (void)
13194 {
13195   rtx amount;
13196   rtx insn;
13197   rtx ip_rtx;
13198   unsigned long live_regs_mask;
13199   unsigned long func_type;
13200   int fp_offset = 0;
13201   int saved_pretend_args = 0;
13202   int saved_regs = 0;
13203   unsigned HOST_WIDE_INT args_to_push;
13204   arm_stack_offsets *offsets;
13205
13206   func_type = arm_current_func_type ();
13207
13208   /* Naked functions don't have prologues.  */
13209   if (IS_NAKED (func_type))
13210     return;
13211
13212   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
13213   args_to_push = crtl->args.pretend_args_size;
13214
13215   /* Compute which register we will have to save onto the stack.  */
13216   offsets = arm_get_frame_offsets ();
13217   live_regs_mask = offsets->saved_regs_mask;
13218
13219   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
13220
13221   if (IS_STACKALIGN (func_type))
13222     {
13223       rtx dwarf;
13224       rtx r0;
13225       rtx r1;
13226       /* Handle a word-aligned stack pointer.  We generate the following:
13227
13228           mov r0, sp
13229           bic r1, r0, #7
13230           mov sp, r1
13231           <save and restore r0 in normal prologue/epilogue>
13232           mov sp, r0
13233           bx lr
13234
13235          The unwinder doesn't need to know about the stack realignment.
13236          Just tell it we saved SP in r0.  */
13237       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
13238
13239       r0 = gen_rtx_REG (SImode, 0);
13240       r1 = gen_rtx_REG (SImode, 1);
13241       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
13242          compiler won't choke.  */
13243       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
13244       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
13245       insn = gen_movsi (r0, stack_pointer_rtx);
13246       RTX_FRAME_RELATED_P (insn) = 1;
13247       add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
13248       emit_insn (insn);
13249       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
13250       emit_insn (gen_movsi (stack_pointer_rtx, r1));
13251     }
13252
13253   /* For APCS frames, if IP register is clobbered
13254      when creating frame, save that register in a special
13255      way.  */
13256   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
13257     {
13258       if (IS_INTERRUPT (func_type))
13259         {
13260           /* Interrupt functions must not corrupt any registers.
13261              Creating a frame pointer however, corrupts the IP
13262              register, so we must push it first.  */
13263           insn = emit_multi_reg_push (1 << IP_REGNUM);
13264
13265           /* Do not set RTX_FRAME_RELATED_P on this insn.
13266              The dwarf stack unwinding code only wants to see one
13267              stack decrement per function, and this is not it.  If
13268              this instruction is labeled as being part of the frame
13269              creation sequence then dwarf2out_frame_debug_expr will
13270              die when it encounters the assignment of IP to FP
13271              later on, since the use of SP here establishes SP as
13272              the CFA register and not IP.
13273
13274              Anyway this instruction is not really part of the stack
13275              frame creation although it is part of the prologue.  */
13276         }
13277       else if (IS_NESTED (func_type))
13278         {
13279           /* The Static chain register is the same as the IP register
13280              used as a scratch register during stack frame creation.
13281              To get around this need to find somewhere to store IP
13282              whilst the frame is being created.  We try the following
13283              places in order:
13284
13285                1. The last argument register.
13286                2. A slot on the stack above the frame.  (This only
13287                   works if the function is not a varargs function).
13288                3. Register r3, after pushing the argument registers
13289                   onto the stack.
13290
13291              Note - we only need to tell the dwarf2 backend about the SP
13292              adjustment in the second variant; the static chain register
13293              doesn't need to be unwound, as it doesn't contain a value
13294              inherited from the caller.  */
13295
13296           if (df_regs_ever_live_p (3) == false)
13297             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
13298           else if (args_to_push == 0)
13299             {
13300               rtx dwarf;
13301
13302               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
13303               saved_regs += 4;
13304
13305               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
13306               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
13307               fp_offset = 4;
13308
13309               /* Just tell the dwarf backend that we adjusted SP.  */
13310               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13311                                    plus_constant (stack_pointer_rtx,
13312                                                   -fp_offset));
13313               RTX_FRAME_RELATED_P (insn) = 1;
13314               add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
13315             }
13316           else
13317             {
13318               /* Store the args on the stack.  */
13319               if (cfun->machine->uses_anonymous_args)
13320                 insn = emit_multi_reg_push
13321                   ((0xf0 >> (args_to_push / 4)) & 0xf);
13322               else
13323                 insn = emit_insn
13324                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13325                                GEN_INT (- args_to_push)));
13326
13327               RTX_FRAME_RELATED_P (insn) = 1;
13328
13329               saved_pretend_args = 1;
13330               fp_offset = args_to_push;
13331               args_to_push = 0;
13332
13333               /* Now reuse r3 to preserve IP.  */
13334               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
13335             }
13336         }
13337
13338       insn = emit_set_insn (ip_rtx,
13339                             plus_constant (stack_pointer_rtx, fp_offset));
13340       RTX_FRAME_RELATED_P (insn) = 1;
13341     }
13342
13343   if (args_to_push)
13344     {
13345       /* Push the argument registers, or reserve space for them.  */
13346       if (cfun->machine->uses_anonymous_args)
13347         insn = emit_multi_reg_push
13348           ((0xf0 >> (args_to_push / 4)) & 0xf);
13349       else
13350         insn = emit_insn
13351           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13352                        GEN_INT (- args_to_push)));
13353       RTX_FRAME_RELATED_P (insn) = 1;
13354     }
13355
13356   /* If this is an interrupt service routine, and the link register
13357      is going to be pushed, and we're not generating extra
13358      push of IP (needed when frame is needed and frame layout if apcs),
13359      subtracting four from LR now will mean that the function return
13360      can be done with a single instruction.  */
13361   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
13362       && (live_regs_mask & (1 << LR_REGNUM)) != 0
13363       && !(frame_pointer_needed && TARGET_APCS_FRAME)
13364       && TARGET_ARM)
13365     {
13366       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
13367       
13368       emit_set_insn (lr, plus_constant (lr, -4));
13369     }
13370
13371   if (live_regs_mask)
13372     {
13373       saved_regs += bit_count (live_regs_mask) * 4;
13374       if (optimize_size && !frame_pointer_needed
13375           && saved_regs == offsets->saved_regs - offsets->saved_args)
13376         {
13377           /* If no coprocessor registers are being pushed and we don't have
13378              to worry about a frame pointer then push extra registers to
13379              create the stack frame.  This is done is a way that does not
13380              alter the frame layout, so is independent of the epilogue.  */
13381           int n;
13382           int frame;
13383           n = 0;
13384           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
13385             n++;
13386           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
13387           if (frame && n * 4 >= frame)
13388             {
13389               n = frame / 4;
13390               live_regs_mask |= (1 << n) - 1;
13391               saved_regs += frame;
13392             }
13393         }
13394       insn = emit_multi_reg_push (live_regs_mask);
13395       RTX_FRAME_RELATED_P (insn) = 1;
13396     }
13397
13398   if (! IS_VOLATILE (func_type))
13399     saved_regs += arm_save_coproc_regs ();
13400
13401   if (frame_pointer_needed && TARGET_ARM)
13402     {
13403       /* Create the new frame pointer.  */
13404       if (TARGET_APCS_FRAME)
13405         {
13406           insn = GEN_INT (-(4 + args_to_push + fp_offset));
13407           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
13408           RTX_FRAME_RELATED_P (insn) = 1;
13409
13410           if (IS_NESTED (func_type))
13411             {
13412               /* Recover the static chain register.  */
13413               if (!df_regs_ever_live_p (3)
13414                   || saved_pretend_args)
13415                 insn = gen_rtx_REG (SImode, 3);
13416               else /* if (crtl->args.pretend_args_size == 0) */
13417                 {
13418                   insn = plus_constant (hard_frame_pointer_rtx, 4);
13419                   insn = gen_frame_mem (SImode, insn);
13420                 }
13421               emit_set_insn (ip_rtx, insn);
13422               /* Add a USE to stop propagate_one_insn() from barfing.  */
13423               emit_insn (gen_prologue_use (ip_rtx));
13424             }
13425         }
13426       else
13427         {
13428           insn = GEN_INT (saved_regs - 4);
13429           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13430                                         stack_pointer_rtx, insn));
13431           RTX_FRAME_RELATED_P (insn) = 1;
13432         }
13433     }
13434
13435   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
13436     {
13437       /* This add can produce multiple insns for a large constant, so we
13438          need to get tricky.  */
13439       rtx last = get_last_insn ();
13440
13441       amount = GEN_INT (offsets->saved_args + saved_regs
13442                         - offsets->outgoing_args);
13443
13444       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13445                                     amount));
13446       do
13447         {
13448           last = last ? NEXT_INSN (last) : get_insns ();
13449           RTX_FRAME_RELATED_P (last) = 1;
13450         }
13451       while (last != insn);
13452
13453       /* If the frame pointer is needed, emit a special barrier that
13454          will prevent the scheduler from moving stores to the frame
13455          before the stack adjustment.  */
13456       if (frame_pointer_needed)
13457         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
13458                                          hard_frame_pointer_rtx));
13459     }
13460
13461
13462   if (frame_pointer_needed && TARGET_THUMB2)
13463     thumb_set_frame_pointer (offsets);
13464
13465   if (flag_pic && arm_pic_register != INVALID_REGNUM)
13466     {
13467       unsigned long mask;
13468
13469       mask = live_regs_mask;
13470       mask &= THUMB2_WORK_REGS;
13471       if (!IS_NESTED (func_type))
13472         mask |= (1 << IP_REGNUM);
13473       arm_load_pic_register (mask);
13474     }
13475
13476   /* If we are profiling, make sure no instructions are scheduled before
13477      the call to mcount.  Similarly if the user has requested no
13478      scheduling in the prolog.  Similarly if we want non-call exceptions
13479      using the EABI unwinder, to prevent faulting instructions from being
13480      swapped with a stack adjustment.  */
13481   if (crtl->profile || !TARGET_SCHED_PROLOG
13482       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13483     emit_insn (gen_blockage ());
13484
13485   /* If the link register is being kept alive, with the return address in it,
13486      then make sure that it does not get reused by the ce2 pass.  */
13487   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
13488     cfun->machine->lr_save_eliminated = 1;
13489 }
13490 \f
13491 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
13492 static void
13493 arm_print_condition (FILE *stream)
13494 {
13495   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
13496     {
13497       /* Branch conversion is not implemented for Thumb-2.  */
13498       if (TARGET_THUMB)
13499         {
13500           output_operand_lossage ("predicated Thumb instruction");
13501           return;
13502         }
13503       if (current_insn_predicate != NULL)
13504         {
13505           output_operand_lossage
13506             ("predicated instruction in conditional sequence");
13507           return;
13508         }
13509
13510       fputs (arm_condition_codes[arm_current_cc], stream);
13511     }
13512   else if (current_insn_predicate)
13513     {
13514       enum arm_cond_code code;
13515
13516       if (TARGET_THUMB1)
13517         {
13518           output_operand_lossage ("predicated Thumb instruction");
13519           return;
13520         }
13521
13522       code = get_arm_condition_code (current_insn_predicate);
13523       fputs (arm_condition_codes[code], stream);
13524     }
13525 }
13526
13527
13528 /* If CODE is 'd', then the X is a condition operand and the instruction
13529    should only be executed if the condition is true.
13530    if CODE is 'D', then the X is a condition operand and the instruction
13531    should only be executed if the condition is false: however, if the mode
13532    of the comparison is CCFPEmode, then always execute the instruction -- we
13533    do this because in these circumstances !GE does not necessarily imply LT;
13534    in these cases the instruction pattern will take care to make sure that
13535    an instruction containing %d will follow, thereby undoing the effects of
13536    doing this instruction unconditionally.
13537    If CODE is 'N' then X is a floating point operand that must be negated
13538    before output.
13539    If CODE is 'B' then output a bitwise inverted value of X (a const int).
13540    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
13541 void
13542 arm_print_operand (FILE *stream, rtx x, int code)
13543 {
13544   switch (code)
13545     {
13546     case '@':
13547       fputs (ASM_COMMENT_START, stream);
13548       return;
13549
13550     case '_':
13551       fputs (user_label_prefix, stream);
13552       return;
13553
13554     case '|':
13555       fputs (REGISTER_PREFIX, stream);
13556       return;
13557
13558     case '?':
13559       arm_print_condition (stream);
13560       return;
13561
13562     case '(':
13563       /* Nothing in unified syntax, otherwise the current condition code.  */
13564       if (!TARGET_UNIFIED_ASM)
13565         arm_print_condition (stream);
13566       break;
13567
13568     case ')':
13569       /* The current condition code in unified syntax, otherwise nothing.  */
13570       if (TARGET_UNIFIED_ASM)
13571         arm_print_condition (stream);
13572       break;
13573   
13574     case '.':
13575       /* The current condition code for a condition code setting instruction.
13576          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
13577       if (TARGET_UNIFIED_ASM)
13578         {
13579           fputc('s', stream);
13580           arm_print_condition (stream);
13581         }
13582       else
13583         {
13584           arm_print_condition (stream);
13585           fputc('s', stream);
13586         }
13587       return;
13588
13589     case '!':
13590       /* If the instruction is conditionally executed then print
13591          the current condition code, otherwise print 's'.  */
13592       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
13593       if (current_insn_predicate)
13594         arm_print_condition (stream);
13595       else
13596         fputc('s', stream);
13597       break;
13598
13599     /* %# is a "break" sequence. It doesn't output anything, but is used to
13600        separate e.g. operand numbers from following text, if that text consists
13601        of further digits which we don't want to be part of the operand
13602        number.  */
13603     case '#':
13604       return;
13605
13606     case 'N':
13607       {
13608         REAL_VALUE_TYPE r;
13609         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
13610         r = REAL_VALUE_NEGATE (r);
13611         fprintf (stream, "%s", fp_const_from_val (&r));
13612       }
13613       return;
13614
13615     /* An integer or symbol address without a preceding # sign.  */
13616     case 'c':
13617       switch (GET_CODE (x))
13618         {
13619         case CONST_INT:
13620           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13621           break;
13622
13623         case SYMBOL_REF:
13624           output_addr_const (stream, x);
13625           break;
13626
13627         default:
13628           gcc_unreachable ();
13629         }
13630       return;
13631
13632     case 'B':
13633       if (GET_CODE (x) == CONST_INT)
13634         {
13635           HOST_WIDE_INT val;
13636           val = ARM_SIGN_EXTEND (~INTVAL (x));
13637           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
13638         }
13639       else
13640         {
13641           putc ('~', stream);
13642           output_addr_const (stream, x);
13643         }
13644       return;
13645
13646     case 'L':
13647       /* The low 16 bits of an immediate constant.  */
13648       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
13649       return;
13650
13651     case 'i':
13652       fprintf (stream, "%s", arithmetic_instr (x, 1));
13653       return;
13654
13655     /* Truncate Cirrus shift counts.  */
13656     case 's':
13657       if (GET_CODE (x) == CONST_INT)
13658         {
13659           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
13660           return;
13661         }
13662       arm_print_operand (stream, x, 0);
13663       return;
13664
13665     case 'I':
13666       fprintf (stream, "%s", arithmetic_instr (x, 0));
13667       return;
13668
13669     case 'S':
13670       {
13671         HOST_WIDE_INT val;
13672         const char *shift;
13673
13674         if (!shift_operator (x, SImode))
13675           {
13676             output_operand_lossage ("invalid shift operand");
13677             break;
13678           }
13679
13680         shift = shift_op (x, &val);
13681
13682         if (shift)
13683           {
13684             fprintf (stream, ", %s ", shift);
13685             if (val == -1)
13686               arm_print_operand (stream, XEXP (x, 1), 0);
13687             else
13688               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
13689           }
13690       }
13691       return;
13692
13693       /* An explanation of the 'Q', 'R' and 'H' register operands:
13694
13695          In a pair of registers containing a DI or DF value the 'Q'
13696          operand returns the register number of the register containing
13697          the least significant part of the value.  The 'R' operand returns
13698          the register number of the register containing the most
13699          significant part of the value.
13700
13701          The 'H' operand returns the higher of the two register numbers.
13702          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
13703          same as the 'Q' operand, since the most significant part of the
13704          value is held in the lower number register.  The reverse is true
13705          on systems where WORDS_BIG_ENDIAN is false.
13706
13707          The purpose of these operands is to distinguish between cases
13708          where the endian-ness of the values is important (for example
13709          when they are added together), and cases where the endian-ness
13710          is irrelevant, but the order of register operations is important.
13711          For example when loading a value from memory into a register
13712          pair, the endian-ness does not matter.  Provided that the value
13713          from the lower memory address is put into the lower numbered
13714          register, and the value from the higher address is put into the
13715          higher numbered register, the load will work regardless of whether
13716          the value being loaded is big-wordian or little-wordian.  The
13717          order of the two register loads can matter however, if the address
13718          of the memory location is actually held in one of the registers
13719          being overwritten by the load.  */
13720     case 'Q':
13721       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13722         {
13723           output_operand_lossage ("invalid operand for code '%c'", code);
13724           return;
13725         }
13726
13727       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
13728       return;
13729
13730     case 'R':
13731       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13732         {
13733           output_operand_lossage ("invalid operand for code '%c'", code);
13734           return;
13735         }
13736
13737       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
13738       return;
13739
13740     case 'H':
13741       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13742         {
13743           output_operand_lossage ("invalid operand for code '%c'", code);
13744           return;
13745         }
13746
13747       asm_fprintf (stream, "%r", REGNO (x) + 1);
13748       return;
13749
13750     case 'J':
13751       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13752         {
13753           output_operand_lossage ("invalid operand for code '%c'", code);
13754           return;
13755         }
13756
13757       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
13758       return;
13759
13760     case 'K':
13761       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13762         {
13763           output_operand_lossage ("invalid operand for code '%c'", code);
13764           return;
13765         }
13766
13767       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
13768       return;
13769
13770     case 'm':
13771       asm_fprintf (stream, "%r",
13772                    GET_CODE (XEXP (x, 0)) == REG
13773                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
13774       return;
13775
13776     case 'M':
13777       asm_fprintf (stream, "{%r-%r}",
13778                    REGNO (x),
13779                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
13780       return;
13781
13782     /* Like 'M', but writing doubleword vector registers, for use by Neon
13783        insns.  */
13784     case 'h':
13785       {
13786         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
13787         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
13788         if (numregs == 1)
13789           asm_fprintf (stream, "{d%d}", regno);
13790         else
13791           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
13792       }
13793       return;
13794
13795     case 'd':
13796       /* CONST_TRUE_RTX means always -- that's the default.  */
13797       if (x == const_true_rtx)
13798         return;
13799
13800       if (!COMPARISON_P (x))
13801         {
13802           output_operand_lossage ("invalid operand for code '%c'", code);
13803           return;
13804         }
13805
13806       fputs (arm_condition_codes[get_arm_condition_code (x)],
13807              stream);
13808       return;
13809
13810     case 'D':
13811       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
13812          want to do that.  */
13813       if (x == const_true_rtx)
13814         {
13815           output_operand_lossage ("instruction never executed");
13816           return;
13817         }
13818       if (!COMPARISON_P (x))
13819         {
13820           output_operand_lossage ("invalid operand for code '%c'", code);
13821           return;
13822         }
13823
13824       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
13825                                  (get_arm_condition_code (x))],
13826              stream);
13827       return;
13828
13829     /* Cirrus registers can be accessed in a variety of ways:
13830          single floating point (f)
13831          double floating point (d)
13832          32bit integer         (fx)
13833          64bit integer         (dx).  */
13834     case 'W':                   /* Cirrus register in F mode.  */
13835     case 'X':                   /* Cirrus register in D mode.  */
13836     case 'Y':                   /* Cirrus register in FX mode.  */
13837     case 'Z':                   /* Cirrus register in DX mode.  */
13838       gcc_assert (GET_CODE (x) == REG
13839                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
13840
13841       fprintf (stream, "mv%s%s",
13842                code == 'W' ? "f"
13843                : code == 'X' ? "d"
13844                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
13845
13846       return;
13847
13848     /* Print cirrus register in the mode specified by the register's mode.  */
13849     case 'V':
13850       {
13851         int mode = GET_MODE (x);
13852
13853         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
13854           {
13855             output_operand_lossage ("invalid operand for code '%c'", code);
13856             return;
13857           }
13858
13859         fprintf (stream, "mv%s%s",
13860                  mode == DFmode ? "d"
13861                  : mode == SImode ? "fx"
13862                  : mode == DImode ? "dx"
13863                  : "f", reg_names[REGNO (x)] + 2);
13864
13865         return;
13866       }
13867
13868     case 'U':
13869       if (GET_CODE (x) != REG
13870           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
13871           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
13872         /* Bad value for wCG register number.  */
13873         {
13874           output_operand_lossage ("invalid operand for code '%c'", code);
13875           return;
13876         }
13877
13878       else
13879         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
13880       return;
13881
13882       /* Print an iWMMXt control register name.  */
13883     case 'w':
13884       if (GET_CODE (x) != CONST_INT
13885           || INTVAL (x) < 0
13886           || INTVAL (x) >= 16)
13887         /* Bad value for wC register number.  */
13888         {
13889           output_operand_lossage ("invalid operand for code '%c'", code);
13890           return;
13891         }
13892
13893       else
13894         {
13895           static const char * wc_reg_names [16] =
13896             {
13897               "wCID",  "wCon",  "wCSSF", "wCASF",
13898               "wC4",   "wC5",   "wC6",   "wC7",
13899               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
13900               "wC12",  "wC13",  "wC14",  "wC15"
13901             };
13902
13903           fprintf (stream, wc_reg_names [INTVAL (x)]);
13904         }
13905       return;
13906
13907     /* Print a VFP/Neon double precision or quad precision register name.  */
13908     case 'P':
13909     case 'q':
13910       {
13911         int mode = GET_MODE (x);
13912         int is_quad = (code == 'q');
13913         int regno;
13914
13915         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
13916           {
13917             output_operand_lossage ("invalid operand for code '%c'", code);
13918             return;
13919           }
13920
13921         if (GET_CODE (x) != REG
13922             || !IS_VFP_REGNUM (REGNO (x)))
13923           {
13924             output_operand_lossage ("invalid operand for code '%c'", code);
13925             return;
13926           }
13927
13928         regno = REGNO (x);
13929         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
13930             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
13931           {
13932             output_operand_lossage ("invalid operand for code '%c'", code);
13933             return;
13934           }
13935
13936         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
13937           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
13938       }
13939       return;
13940
13941     /* These two codes print the low/high doubleword register of a Neon quad
13942        register, respectively.  For pair-structure types, can also print
13943        low/high quadword registers.  */
13944     case 'e':
13945     case 'f':
13946       {
13947         int mode = GET_MODE (x);
13948         int regno;
13949
13950         if ((GET_MODE_SIZE (mode) != 16
13951              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
13952           {
13953             output_operand_lossage ("invalid operand for code '%c'", code);
13954             return;
13955           }
13956
13957         regno = REGNO (x);
13958         if (!NEON_REGNO_OK_FOR_QUAD (regno))
13959           {
13960             output_operand_lossage ("invalid operand for code '%c'", code);
13961             return;
13962           }
13963
13964         if (GET_MODE_SIZE (mode) == 16)
13965           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
13966                                   + (code == 'f' ? 1 : 0));
13967         else
13968           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
13969                                   + (code == 'f' ? 1 : 0));
13970       }
13971       return;
13972
13973     /* Print a VFPv3 floating-point constant, represented as an integer
13974        index.  */
13975     case 'G':
13976       {
13977         int index = vfp3_const_double_index (x);
13978         gcc_assert (index != -1);
13979         fprintf (stream, "%d", index);
13980       }
13981       return;
13982
13983     /* Print bits representing opcode features for Neon.
13984
13985        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
13986        and polynomials as unsigned.
13987
13988        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
13989
13990        Bit 2 is 1 for rounding functions, 0 otherwise.  */
13991
13992     /* Identify the type as 's', 'u', 'p' or 'f'.  */
13993     case 'T':
13994       {
13995         HOST_WIDE_INT bits = INTVAL (x);
13996         fputc ("uspf"[bits & 3], stream);
13997       }
13998       return;
13999
14000     /* Likewise, but signed and unsigned integers are both 'i'.  */
14001     case 'F':
14002       {
14003         HOST_WIDE_INT bits = INTVAL (x);
14004         fputc ("iipf"[bits & 3], stream);
14005       }
14006       return;
14007
14008     /* As for 'T', but emit 'u' instead of 'p'.  */
14009     case 't':
14010       {
14011         HOST_WIDE_INT bits = INTVAL (x);
14012         fputc ("usuf"[bits & 3], stream);
14013       }
14014       return;
14015
14016     /* Bit 2: rounding (vs none).  */
14017     case 'O':
14018       {
14019         HOST_WIDE_INT bits = INTVAL (x);
14020         fputs ((bits & 4) != 0 ? "r" : "", stream);
14021       }
14022       return;
14023
14024     /* Memory operand for vld1/vst1 instruction.  */
14025     case 'A':
14026       {
14027         rtx addr;
14028         bool postinc = FALSE;
14029         gcc_assert (GET_CODE (x) == MEM);
14030         addr = XEXP (x, 0);
14031         if (GET_CODE (addr) == POST_INC)
14032           {
14033             postinc = 1;
14034             addr = XEXP (addr, 0);
14035           }
14036         asm_fprintf (stream, "[%r]", REGNO (addr));
14037         if (postinc)
14038           fputs("!", stream);
14039       }
14040       return;
14041
14042     /* Register specifier for vld1.16/vst1.16.  Translate the S register
14043        number into a D register number and element index.  */
14044     case 'z':
14045       {
14046         int mode = GET_MODE (x);
14047         int regno;
14048
14049         if (GET_MODE_SIZE (mode) != 2 || GET_CODE (x) != REG)
14050           {
14051             output_operand_lossage ("invalid operand for code '%c'", code);
14052             return;
14053           }
14054
14055         regno = REGNO (x);
14056         if (!VFP_REGNO_OK_FOR_SINGLE (regno))
14057           {
14058             output_operand_lossage ("invalid operand for code '%c'", code);
14059             return;
14060           }
14061
14062         regno = regno - FIRST_VFP_REGNUM;
14063         fprintf (stream, "d%d[%d]", regno/2, ((regno % 2) ? 2 : 0));
14064       }
14065       return;
14066       
14067     default:
14068       if (x == 0)
14069         {
14070           output_operand_lossage ("missing operand");
14071           return;
14072         }
14073
14074       switch (GET_CODE (x))
14075         {
14076         case REG:
14077           asm_fprintf (stream, "%r", REGNO (x));
14078           break;
14079
14080         case MEM:
14081           output_memory_reference_mode = GET_MODE (x);
14082           output_address (XEXP (x, 0));
14083           break;
14084
14085         case CONST_DOUBLE:
14086           if (TARGET_NEON)
14087             {
14088               char fpstr[20];
14089               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
14090                                sizeof (fpstr), 0, 1);
14091               fprintf (stream, "#%s", fpstr);
14092             }
14093           else
14094             fprintf (stream, "#%s", fp_immediate_constant (x));
14095           break;
14096
14097         default:
14098           gcc_assert (GET_CODE (x) != NEG);
14099           fputc ('#', stream);
14100           if (GET_CODE (x) == HIGH)
14101             {
14102               fputs (":lower16:", stream);
14103               x = XEXP (x, 0);
14104             }
14105             
14106           output_addr_const (stream, x);
14107           break;
14108         }
14109     }
14110 }
14111 \f
14112 /* Target hook for assembling integer objects.  The ARM version needs to
14113    handle word-sized values specially.  */
14114 static bool
14115 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
14116 {
14117   enum machine_mode mode;
14118
14119   if (size == UNITS_PER_WORD && aligned_p)
14120     {
14121       fputs ("\t.word\t", asm_out_file);
14122       output_addr_const (asm_out_file, x);
14123
14124       /* Mark symbols as position independent.  We only do this in the
14125          .text segment, not in the .data segment.  */
14126       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
14127           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
14128         {
14129           /* See legitimize_pic_address for an explanation of the
14130              TARGET_VXWORKS_RTP check.  */
14131           if (TARGET_VXWORKS_RTP
14132               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
14133             fputs ("(GOT)", asm_out_file);
14134           else
14135             fputs ("(GOTOFF)", asm_out_file);
14136         }
14137       fputc ('\n', asm_out_file);
14138       return true;
14139     }
14140
14141   mode = GET_MODE (x);
14142
14143   if (arm_vector_mode_supported_p (mode))
14144     {
14145       int i, units;
14146
14147       gcc_assert (GET_CODE (x) == CONST_VECTOR);
14148
14149       units = CONST_VECTOR_NUNITS (x);
14150       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
14151
14152       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14153         for (i = 0; i < units; i++)
14154           {
14155             rtx elt = CONST_VECTOR_ELT (x, i);
14156             assemble_integer
14157               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
14158           }
14159       else
14160         for (i = 0; i < units; i++)
14161           {
14162             rtx elt = CONST_VECTOR_ELT (x, i);
14163             REAL_VALUE_TYPE rval;
14164
14165             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
14166
14167             assemble_real
14168               (rval, GET_MODE_INNER (mode),
14169               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
14170           }
14171
14172       return true;
14173     }
14174
14175   return default_assemble_integer (x, size, aligned_p);
14176 }
14177
14178 static void
14179 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
14180 {
14181   section *s;
14182
14183   if (!TARGET_AAPCS_BASED)
14184     {
14185       (is_ctor ? 
14186        default_named_section_asm_out_constructor 
14187        : default_named_section_asm_out_destructor) (symbol, priority);
14188       return;
14189     }
14190
14191   /* Put these in the .init_array section, using a special relocation.  */
14192   if (priority != DEFAULT_INIT_PRIORITY)
14193     {
14194       char buf[18];
14195       sprintf (buf, "%s.%.5u", 
14196                is_ctor ? ".init_array" : ".fini_array",
14197                priority);
14198       s = get_section (buf, SECTION_WRITE, NULL_TREE);
14199     }
14200   else if (is_ctor)
14201     s = ctors_section;
14202   else
14203     s = dtors_section;
14204
14205   switch_to_section (s);
14206   assemble_align (POINTER_SIZE);
14207   fputs ("\t.word\t", asm_out_file);
14208   output_addr_const (asm_out_file, symbol);
14209   fputs ("(target1)\n", asm_out_file);
14210 }
14211
14212 /* Add a function to the list of static constructors.  */
14213
14214 static void
14215 arm_elf_asm_constructor (rtx symbol, int priority)
14216 {
14217   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
14218 }
14219
14220 /* Add a function to the list of static destructors.  */
14221
14222 static void
14223 arm_elf_asm_destructor (rtx symbol, int priority)
14224 {
14225   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
14226 }
14227 \f
14228 /* A finite state machine takes care of noticing whether or not instructions
14229    can be conditionally executed, and thus decrease execution time and code
14230    size by deleting branch instructions.  The fsm is controlled by
14231    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
14232
14233 /* The state of the fsm controlling condition codes are:
14234    0: normal, do nothing special
14235    1: make ASM_OUTPUT_OPCODE not output this instruction
14236    2: make ASM_OUTPUT_OPCODE not output this instruction
14237    3: make instructions conditional
14238    4: make instructions conditional
14239
14240    State transitions (state->state by whom under condition):
14241    0 -> 1 final_prescan_insn if the `target' is a label
14242    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
14243    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
14244    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
14245    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
14246           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
14247    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
14248           (the target insn is arm_target_insn).
14249
14250    If the jump clobbers the conditions then we use states 2 and 4.
14251
14252    A similar thing can be done with conditional return insns.
14253
14254    XXX In case the `target' is an unconditional branch, this conditionalising
14255    of the instructions always reduces code size, but not always execution
14256    time.  But then, I want to reduce the code size to somewhere near what
14257    /bin/cc produces.  */
14258
14259 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
14260    instructions.  When a COND_EXEC instruction is seen the subsequent
14261    instructions are scanned so that multiple conditional instructions can be
14262    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
14263    specify the length and true/false mask for the IT block.  These will be
14264    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
14265
14266 /* Returns the index of the ARM condition code string in
14267    `arm_condition_codes'.  COMPARISON should be an rtx like
14268    `(eq (...) (...))'.  */
14269 static enum arm_cond_code
14270 get_arm_condition_code (rtx comparison)
14271 {
14272   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
14273   enum arm_cond_code code;
14274   enum rtx_code comp_code = GET_CODE (comparison);
14275
14276   if (GET_MODE_CLASS (mode) != MODE_CC)
14277     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
14278                            XEXP (comparison, 1));
14279
14280   switch (mode)
14281     {
14282     case CC_DNEmode: code = ARM_NE; goto dominance;
14283     case CC_DEQmode: code = ARM_EQ; goto dominance;
14284     case CC_DGEmode: code = ARM_GE; goto dominance;
14285     case CC_DGTmode: code = ARM_GT; goto dominance;
14286     case CC_DLEmode: code = ARM_LE; goto dominance;
14287     case CC_DLTmode: code = ARM_LT; goto dominance;
14288     case CC_DGEUmode: code = ARM_CS; goto dominance;
14289     case CC_DGTUmode: code = ARM_HI; goto dominance;
14290     case CC_DLEUmode: code = ARM_LS; goto dominance;
14291     case CC_DLTUmode: code = ARM_CC;
14292
14293     dominance:
14294       gcc_assert (comp_code == EQ || comp_code == NE);
14295
14296       if (comp_code == EQ)
14297         return ARM_INVERSE_CONDITION_CODE (code);
14298       return code;
14299
14300     case CC_NOOVmode:
14301       switch (comp_code)
14302         {
14303         case NE: return ARM_NE;
14304         case EQ: return ARM_EQ;
14305         case GE: return ARM_PL;
14306         case LT: return ARM_MI;
14307         default: gcc_unreachable ();
14308         }
14309
14310     case CC_Zmode:
14311       switch (comp_code)
14312         {
14313         case NE: return ARM_NE;
14314         case EQ: return ARM_EQ;
14315         default: gcc_unreachable ();
14316         }
14317
14318     case CC_Nmode:
14319       switch (comp_code)
14320         {
14321         case NE: return ARM_MI;
14322         case EQ: return ARM_PL;
14323         default: gcc_unreachable ();
14324         }
14325
14326     case CCFPEmode:
14327     case CCFPmode:
14328       /* These encodings assume that AC=1 in the FPA system control
14329          byte.  This allows us to handle all cases except UNEQ and
14330          LTGT.  */
14331       switch (comp_code)
14332         {
14333         case GE: return ARM_GE;
14334         case GT: return ARM_GT;
14335         case LE: return ARM_LS;
14336         case LT: return ARM_MI;
14337         case NE: return ARM_NE;
14338         case EQ: return ARM_EQ;
14339         case ORDERED: return ARM_VC;
14340         case UNORDERED: return ARM_VS;
14341         case UNLT: return ARM_LT;
14342         case UNLE: return ARM_LE;
14343         case UNGT: return ARM_HI;
14344         case UNGE: return ARM_PL;
14345           /* UNEQ and LTGT do not have a representation.  */
14346         case UNEQ: /* Fall through.  */
14347         case LTGT: /* Fall through.  */
14348         default: gcc_unreachable ();
14349         }
14350
14351     case CC_SWPmode:
14352       switch (comp_code)
14353         {
14354         case NE: return ARM_NE;
14355         case EQ: return ARM_EQ;
14356         case GE: return ARM_LE;
14357         case GT: return ARM_LT;
14358         case LE: return ARM_GE;
14359         case LT: return ARM_GT;
14360         case GEU: return ARM_LS;
14361         case GTU: return ARM_CC;
14362         case LEU: return ARM_CS;
14363         case LTU: return ARM_HI;
14364         default: gcc_unreachable ();
14365         }
14366
14367     case CC_Cmode:
14368       switch (comp_code)
14369       {
14370       case LTU: return ARM_CS;
14371       case GEU: return ARM_CC;
14372       default: gcc_unreachable ();
14373       }
14374
14375     case CCmode:
14376       switch (comp_code)
14377         {
14378         case NE: return ARM_NE;
14379         case EQ: return ARM_EQ;
14380         case GE: return ARM_GE;
14381         case GT: return ARM_GT;
14382         case LE: return ARM_LE;
14383         case LT: return ARM_LT;
14384         case GEU: return ARM_CS;
14385         case GTU: return ARM_HI;
14386         case LEU: return ARM_LS;
14387         case LTU: return ARM_CC;
14388         default: gcc_unreachable ();
14389         }
14390
14391     default: gcc_unreachable ();
14392     }
14393 }
14394
14395 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
14396    instructions.  */
14397 void
14398 thumb2_final_prescan_insn (rtx insn)
14399 {
14400   rtx first_insn = insn;
14401   rtx body = PATTERN (insn);
14402   rtx predicate;
14403   enum arm_cond_code code;
14404   int n;
14405   int mask;
14406
14407   /* Remove the previous insn from the count of insns to be output.  */
14408   if (arm_condexec_count)
14409       arm_condexec_count--;
14410
14411   /* Nothing to do if we are already inside a conditional block.  */
14412   if (arm_condexec_count)
14413     return;
14414
14415   if (GET_CODE (body) != COND_EXEC)
14416     return;
14417
14418   /* Conditional jumps are implemented directly.  */
14419   if (GET_CODE (insn) == JUMP_INSN)
14420     return;
14421
14422   predicate = COND_EXEC_TEST (body);
14423   arm_current_cc = get_arm_condition_code (predicate);
14424
14425   n = get_attr_ce_count (insn);
14426   arm_condexec_count = 1;
14427   arm_condexec_mask = (1 << n) - 1;
14428   arm_condexec_masklen = n;
14429   /* See if subsequent instructions can be combined into the same block.  */
14430   for (;;)
14431     {
14432       insn = next_nonnote_insn (insn);
14433
14434       /* Jumping into the middle of an IT block is illegal, so a label or
14435          barrier terminates the block.  */
14436       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
14437         break;
14438
14439       body = PATTERN (insn);
14440       /* USE and CLOBBER aren't really insns, so just skip them.  */
14441       if (GET_CODE (body) == USE
14442           || GET_CODE (body) == CLOBBER)
14443         continue;
14444
14445       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
14446       if (GET_CODE (body) != COND_EXEC)
14447         break;
14448       /* Allow up to 4 conditionally executed instructions in a block.  */
14449       n = get_attr_ce_count (insn);
14450       if (arm_condexec_masklen + n > 4)
14451         break;
14452
14453       predicate = COND_EXEC_TEST (body);
14454       code = get_arm_condition_code (predicate);
14455       mask = (1 << n) - 1;
14456       if (arm_current_cc == code)
14457         arm_condexec_mask |= (mask << arm_condexec_masklen);
14458       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
14459         break;
14460
14461       arm_condexec_count++;
14462       arm_condexec_masklen += n;
14463
14464       /* A jump must be the last instruction in a conditional block.  */
14465       if (GET_CODE(insn) == JUMP_INSN)
14466         break;
14467     }
14468   /* Restore recog_data (getting the attributes of other insns can
14469      destroy this array, but final.c assumes that it remains intact
14470      across this call).  */
14471   extract_constrain_insn_cached (first_insn);
14472 }
14473
14474 void
14475 arm_final_prescan_insn (rtx insn)
14476 {
14477   /* BODY will hold the body of INSN.  */
14478   rtx body = PATTERN (insn);
14479
14480   /* This will be 1 if trying to repeat the trick, and things need to be
14481      reversed if it appears to fail.  */
14482   int reverse = 0;
14483
14484   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
14485      taken are clobbered, even if the rtl suggests otherwise.  It also
14486      means that we have to grub around within the jump expression to find
14487      out what the conditions are when the jump isn't taken.  */
14488   int jump_clobbers = 0;
14489
14490   /* If we start with a return insn, we only succeed if we find another one.  */
14491   int seeking_return = 0;
14492
14493   /* START_INSN will hold the insn from where we start looking.  This is the
14494      first insn after the following code_label if REVERSE is true.  */
14495   rtx start_insn = insn;
14496
14497   /* If in state 4, check if the target branch is reached, in order to
14498      change back to state 0.  */
14499   if (arm_ccfsm_state == 4)
14500     {
14501       if (insn == arm_target_insn)
14502         {
14503           arm_target_insn = NULL;
14504           arm_ccfsm_state = 0;
14505         }
14506       return;
14507     }
14508
14509   /* If in state 3, it is possible to repeat the trick, if this insn is an
14510      unconditional branch to a label, and immediately following this branch
14511      is the previous target label which is only used once, and the label this
14512      branch jumps to is not too far off.  */
14513   if (arm_ccfsm_state == 3)
14514     {
14515       if (simplejump_p (insn))
14516         {
14517           start_insn = next_nonnote_insn (start_insn);
14518           if (GET_CODE (start_insn) == BARRIER)
14519             {
14520               /* XXX Isn't this always a barrier?  */
14521               start_insn = next_nonnote_insn (start_insn);
14522             }
14523           if (GET_CODE (start_insn) == CODE_LABEL
14524               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
14525               && LABEL_NUSES (start_insn) == 1)
14526             reverse = TRUE;
14527           else
14528             return;
14529         }
14530       else if (GET_CODE (body) == RETURN)
14531         {
14532           start_insn = next_nonnote_insn (start_insn);
14533           if (GET_CODE (start_insn) == BARRIER)
14534             start_insn = next_nonnote_insn (start_insn);
14535           if (GET_CODE (start_insn) == CODE_LABEL
14536               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
14537               && LABEL_NUSES (start_insn) == 1)
14538             {
14539               reverse = TRUE;
14540               seeking_return = 1;
14541             }
14542           else
14543             return;
14544         }
14545       else
14546         return;
14547     }
14548
14549   gcc_assert (!arm_ccfsm_state || reverse);
14550   if (GET_CODE (insn) != JUMP_INSN)
14551     return;
14552
14553   /* This jump might be paralleled with a clobber of the condition codes
14554      the jump should always come first */
14555   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
14556     body = XVECEXP (body, 0, 0);
14557
14558   if (reverse
14559       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
14560           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
14561     {
14562       int insns_skipped;
14563       int fail = FALSE, succeed = FALSE;
14564       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
14565       int then_not_else = TRUE;
14566       rtx this_insn = start_insn, label = 0;
14567
14568       /* If the jump cannot be done with one instruction, we cannot
14569          conditionally execute the instruction in the inverse case.  */
14570       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
14571         {
14572           jump_clobbers = 1;
14573           return;
14574         }
14575
14576       /* Register the insn jumped to.  */
14577       if (reverse)
14578         {
14579           if (!seeking_return)
14580             label = XEXP (SET_SRC (body), 0);
14581         }
14582       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
14583         label = XEXP (XEXP (SET_SRC (body), 1), 0);
14584       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
14585         {
14586           label = XEXP (XEXP (SET_SRC (body), 2), 0);
14587           then_not_else = FALSE;
14588         }
14589       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
14590         seeking_return = 1;
14591       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
14592         {
14593           seeking_return = 1;
14594           then_not_else = FALSE;
14595         }
14596       else
14597         gcc_unreachable ();
14598
14599       /* See how many insns this branch skips, and what kind of insns.  If all
14600          insns are okay, and the label or unconditional branch to the same
14601          label is not too far away, succeed.  */
14602       for (insns_skipped = 0;
14603            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
14604         {
14605           rtx scanbody;
14606
14607           this_insn = next_nonnote_insn (this_insn);
14608           if (!this_insn)
14609             break;
14610
14611           switch (GET_CODE (this_insn))
14612             {
14613             case CODE_LABEL:
14614               /* Succeed if it is the target label, otherwise fail since
14615                  control falls in from somewhere else.  */
14616               if (this_insn == label)
14617                 {
14618                   if (jump_clobbers)
14619                     {
14620                       arm_ccfsm_state = 2;
14621                       this_insn = next_nonnote_insn (this_insn);
14622                     }
14623                   else
14624                     arm_ccfsm_state = 1;
14625                   succeed = TRUE;
14626                 }
14627               else
14628                 fail = TRUE;
14629               break;
14630
14631             case BARRIER:
14632               /* Succeed if the following insn is the target label.
14633                  Otherwise fail.
14634                  If return insns are used then the last insn in a function
14635                  will be a barrier.  */
14636               this_insn = next_nonnote_insn (this_insn);
14637               if (this_insn && this_insn == label)
14638                 {
14639                   if (jump_clobbers)
14640                     {
14641                       arm_ccfsm_state = 2;
14642                       this_insn = next_nonnote_insn (this_insn);
14643                     }
14644                   else
14645                     arm_ccfsm_state = 1;
14646                   succeed = TRUE;
14647                 }
14648               else
14649                 fail = TRUE;
14650               break;
14651
14652             case CALL_INSN:
14653               /* The AAPCS says that conditional calls should not be
14654                  used since they make interworking inefficient (the
14655                  linker can't transform BL<cond> into BLX).  That's
14656                  only a problem if the machine has BLX.  */
14657               if (arm_arch5)
14658                 {
14659                   fail = TRUE;
14660                   break;
14661                 }
14662
14663               /* Succeed if the following insn is the target label, or
14664                  if the following two insns are a barrier and the
14665                  target label.  */
14666               this_insn = next_nonnote_insn (this_insn);
14667               if (this_insn && GET_CODE (this_insn) == BARRIER)
14668                 this_insn = next_nonnote_insn (this_insn);
14669
14670               if (this_insn && this_insn == label
14671                   && insns_skipped < max_insns_skipped)
14672                 {
14673                   if (jump_clobbers)
14674                     {
14675                       arm_ccfsm_state = 2;
14676                       this_insn = next_nonnote_insn (this_insn);
14677                     }
14678                   else
14679                     arm_ccfsm_state = 1;
14680                   succeed = TRUE;
14681                 }
14682               else
14683                 fail = TRUE;
14684               break;
14685
14686             case JUMP_INSN:
14687               /* If this is an unconditional branch to the same label, succeed.
14688                  If it is to another label, do nothing.  If it is conditional,
14689                  fail.  */
14690               /* XXX Probably, the tests for SET and the PC are
14691                  unnecessary.  */
14692
14693               scanbody = PATTERN (this_insn);
14694               if (GET_CODE (scanbody) == SET
14695                   && GET_CODE (SET_DEST (scanbody)) == PC)
14696                 {
14697                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
14698                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
14699                     {
14700                       arm_ccfsm_state = 2;
14701                       succeed = TRUE;
14702                     }
14703                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
14704                     fail = TRUE;
14705                 }
14706               /* Fail if a conditional return is undesirable (e.g. on a
14707                  StrongARM), but still allow this if optimizing for size.  */
14708               else if (GET_CODE (scanbody) == RETURN
14709                        && !use_return_insn (TRUE, NULL)
14710                        && !optimize_size)
14711                 fail = TRUE;
14712               else if (GET_CODE (scanbody) == RETURN
14713                        && seeking_return)
14714                 {
14715                   arm_ccfsm_state = 2;
14716                   succeed = TRUE;
14717                 }
14718               else if (GET_CODE (scanbody) == PARALLEL)
14719                 {
14720                   switch (get_attr_conds (this_insn))
14721                     {
14722                     case CONDS_NOCOND:
14723                       break;
14724                     default:
14725                       fail = TRUE;
14726                       break;
14727                     }
14728                 }
14729               else
14730                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
14731
14732               break;
14733
14734             case INSN:
14735               /* Instructions using or affecting the condition codes make it
14736                  fail.  */
14737               scanbody = PATTERN (this_insn);
14738               if (!(GET_CODE (scanbody) == SET
14739                     || GET_CODE (scanbody) == PARALLEL)
14740                   || get_attr_conds (this_insn) != CONDS_NOCOND)
14741                 fail = TRUE;
14742
14743               /* A conditional cirrus instruction must be followed by
14744                  a non Cirrus instruction.  However, since we
14745                  conditionalize instructions in this function and by
14746                  the time we get here we can't add instructions
14747                  (nops), because shorten_branches() has already been
14748                  called, we will disable conditionalizing Cirrus
14749                  instructions to be safe.  */
14750               if (GET_CODE (scanbody) != USE
14751                   && GET_CODE (scanbody) != CLOBBER
14752                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
14753                 fail = TRUE;
14754               break;
14755
14756             default:
14757               break;
14758             }
14759         }
14760       if (succeed)
14761         {
14762           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
14763             arm_target_label = CODE_LABEL_NUMBER (label);
14764           else
14765             {
14766               gcc_assert (seeking_return || arm_ccfsm_state == 2);
14767
14768               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
14769                 {
14770                   this_insn = next_nonnote_insn (this_insn);
14771                   gcc_assert (!this_insn
14772                               || (GET_CODE (this_insn) != BARRIER
14773                                   && GET_CODE (this_insn) != CODE_LABEL));
14774                 }
14775               if (!this_insn)
14776                 {
14777                   /* Oh, dear! we ran off the end.. give up.  */
14778                   extract_constrain_insn_cached (insn);
14779                   arm_ccfsm_state = 0;
14780                   arm_target_insn = NULL;
14781                   return;
14782                 }
14783               arm_target_insn = this_insn;
14784             }
14785           if (jump_clobbers)
14786             {
14787               gcc_assert (!reverse);
14788               arm_current_cc =
14789                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
14790                                                             0), 0), 1));
14791               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
14792                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14793               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
14794                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14795             }
14796           else
14797             {
14798               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
14799                  what it was.  */
14800               if (!reverse)
14801                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
14802                                                                0));
14803             }
14804
14805           if (reverse || then_not_else)
14806             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14807         }
14808
14809       /* Restore recog_data (getting the attributes of other insns can
14810          destroy this array, but final.c assumes that it remains intact
14811          across this call.  */
14812       extract_constrain_insn_cached (insn);
14813     }
14814 }
14815
14816 /* Output IT instructions.  */
14817 void
14818 thumb2_asm_output_opcode (FILE * stream)
14819 {
14820   char buff[5];
14821   int n;
14822
14823   if (arm_condexec_mask)
14824     {
14825       for (n = 0; n < arm_condexec_masklen; n++)
14826         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
14827       buff[n] = 0;
14828       asm_fprintf(stream, "i%s\t%s\n\t", buff,
14829                   arm_condition_codes[arm_current_cc]);
14830       arm_condexec_mask = 0;
14831     }
14832 }
14833
14834 /* Returns true if REGNO is a valid register
14835    for holding a quantity of type MODE.  */
14836 int
14837 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
14838 {
14839   if (GET_MODE_CLASS (mode) == MODE_CC)
14840     return (regno == CC_REGNUM
14841             || (TARGET_HARD_FLOAT && TARGET_VFP
14842                 && regno == VFPCC_REGNUM));
14843
14844   if (TARGET_THUMB1)
14845     /* For the Thumb we only allow values bigger than SImode in
14846        registers 0 - 6, so that there is always a second low
14847        register available to hold the upper part of the value.
14848        We probably we ought to ensure that the register is the
14849        start of an even numbered register pair.  */
14850     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
14851
14852   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
14853       && IS_CIRRUS_REGNUM (regno))
14854     /* We have outlawed SI values in Cirrus registers because they
14855        reside in the lower 32 bits, but SF values reside in the
14856        upper 32 bits.  This causes gcc all sorts of grief.  We can't
14857        even split the registers into pairs because Cirrus SI values
14858        get sign extended to 64bits-- aldyh.  */
14859     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
14860
14861   if (TARGET_HARD_FLOAT && TARGET_VFP
14862       && IS_VFP_REGNUM (regno))
14863     {
14864       if (mode == SFmode || mode == SImode)
14865         return VFP_REGNO_OK_FOR_SINGLE (regno);
14866
14867       if (mode == DFmode)
14868         return VFP_REGNO_OK_FOR_DOUBLE (regno);
14869
14870       /* VFP registers can hold HFmode values, but there is no point in
14871          putting them there unless we have the NEON extensions for
14872          loading/storing them, too.  */
14873       if (mode == HFmode)
14874         return TARGET_NEON_FP16 && VFP_REGNO_OK_FOR_SINGLE (regno);
14875
14876       if (TARGET_NEON)
14877         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
14878                || (VALID_NEON_QREG_MODE (mode)
14879                    && NEON_REGNO_OK_FOR_QUAD (regno))
14880                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
14881                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
14882                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
14883                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
14884                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
14885
14886       return FALSE;
14887     }
14888
14889   if (TARGET_REALLY_IWMMXT)
14890     {
14891       if (IS_IWMMXT_GR_REGNUM (regno))
14892         return mode == SImode;
14893
14894       if (IS_IWMMXT_REGNUM (regno))
14895         return VALID_IWMMXT_REG_MODE (mode);
14896     }
14897   
14898   /* We allow almost any value to be stored in the general registers.
14899      Restrict doubleword quantities to even register pairs so that we can
14900      use ldrd.  Do not allow very large Neon structure opaque modes in
14901      general registers; they would use too many.  */
14902   if (regno <= LAST_ARM_REGNUM)
14903     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
14904       && ARM_NUM_REGS (mode) <= 4;
14905
14906   if (regno == FRAME_POINTER_REGNUM
14907       || regno == ARG_POINTER_REGNUM)
14908     /* We only allow integers in the fake hard registers.  */
14909     return GET_MODE_CLASS (mode) == MODE_INT;
14910
14911   /* The only registers left are the FPA registers
14912      which we only allow to hold FP values.  */
14913   return (TARGET_HARD_FLOAT && TARGET_FPA
14914           && GET_MODE_CLASS (mode) == MODE_FLOAT
14915           && regno >= FIRST_FPA_REGNUM
14916           && regno <= LAST_FPA_REGNUM);
14917 }
14918
14919 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
14920    not used in arm mode.  */
14921
14922 enum reg_class
14923 arm_regno_class (int regno)
14924 {
14925   if (TARGET_THUMB1)
14926     {
14927       if (regno == STACK_POINTER_REGNUM)
14928         return STACK_REG;
14929       if (regno == CC_REGNUM)
14930         return CC_REG;
14931       if (regno < 8)
14932         return LO_REGS;
14933       return HI_REGS;
14934     }
14935
14936   if (TARGET_THUMB2 && regno < 8)
14937     return LO_REGS;
14938
14939   if (   regno <= LAST_ARM_REGNUM
14940       || regno == FRAME_POINTER_REGNUM
14941       || regno == ARG_POINTER_REGNUM)
14942     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
14943
14944   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
14945     return TARGET_THUMB2 ? CC_REG : NO_REGS;
14946
14947   if (IS_CIRRUS_REGNUM (regno))
14948     return CIRRUS_REGS;
14949
14950   if (IS_VFP_REGNUM (regno))
14951     {
14952       if (regno <= D7_VFP_REGNUM)
14953         return VFP_D0_D7_REGS;
14954       else if (regno <= LAST_LO_VFP_REGNUM)
14955         return VFP_LO_REGS;
14956       else
14957         return VFP_HI_REGS;
14958     }
14959
14960   if (IS_IWMMXT_REGNUM (regno))
14961     return IWMMXT_REGS;
14962
14963   if (IS_IWMMXT_GR_REGNUM (regno))
14964     return IWMMXT_GR_REGS;
14965
14966   return FPA_REGS;
14967 }
14968
14969 /* Handle a special case when computing the offset
14970    of an argument from the frame pointer.  */
14971 int
14972 arm_debugger_arg_offset (int value, rtx addr)
14973 {
14974   rtx insn;
14975
14976   /* We are only interested if dbxout_parms() failed to compute the offset.  */
14977   if (value != 0)
14978     return 0;
14979
14980   /* We can only cope with the case where the address is held in a register.  */
14981   if (GET_CODE (addr) != REG)
14982     return 0;
14983
14984   /* If we are using the frame pointer to point at the argument, then
14985      an offset of 0 is correct.  */
14986   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
14987     return 0;
14988
14989   /* If we are using the stack pointer to point at the
14990      argument, then an offset of 0 is correct.  */
14991   /* ??? Check this is consistent with thumb2 frame layout.  */
14992   if ((TARGET_THUMB || !frame_pointer_needed)
14993       && REGNO (addr) == SP_REGNUM)
14994     return 0;
14995
14996   /* Oh dear.  The argument is pointed to by a register rather
14997      than being held in a register, or being stored at a known
14998      offset from the frame pointer.  Since GDB only understands
14999      those two kinds of argument we must translate the address
15000      held in the register into an offset from the frame pointer.
15001      We do this by searching through the insns for the function
15002      looking to see where this register gets its value.  If the
15003      register is initialized from the frame pointer plus an offset
15004      then we are in luck and we can continue, otherwise we give up.
15005
15006      This code is exercised by producing debugging information
15007      for a function with arguments like this:
15008
15009            double func (double a, double b, int c, double d) {return d;}
15010
15011      Without this code the stab for parameter 'd' will be set to
15012      an offset of 0 from the frame pointer, rather than 8.  */
15013
15014   /* The if() statement says:
15015
15016      If the insn is a normal instruction
15017      and if the insn is setting the value in a register
15018      and if the register being set is the register holding the address of the argument
15019      and if the address is computing by an addition
15020      that involves adding to a register
15021      which is the frame pointer
15022      a constant integer
15023
15024      then...  */
15025
15026   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
15027     {
15028       if (   GET_CODE (insn) == INSN
15029           && GET_CODE (PATTERN (insn)) == SET
15030           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
15031           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
15032           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
15033           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
15034           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
15035              )
15036         {
15037           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
15038
15039           break;
15040         }
15041     }
15042
15043   if (value == 0)
15044     {
15045       debug_rtx (addr);
15046       warning (0, "unable to compute real location of stacked parameter");
15047       value = 8; /* XXX magic hack */
15048     }
15049
15050   return value;
15051 }
15052 \f
15053 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
15054   do                                                                    \
15055     {                                                                   \
15056       if ((MASK) & insn_flags)                                          \
15057         add_builtin_function ((NAME), (TYPE), (CODE),                   \
15058                              BUILT_IN_MD, NULL, NULL_TREE);             \
15059     }                                                                   \
15060   while (0)
15061
15062 struct builtin_description
15063 {
15064   const unsigned int       mask;
15065   const enum insn_code     icode;
15066   const char * const       name;
15067   const enum arm_builtins  code;
15068   const enum rtx_code      comparison;
15069   const unsigned int       flag;
15070 };
15071
15072 static const struct builtin_description bdesc_2arg[] =
15073 {
15074 #define IWMMXT_BUILTIN(code, string, builtin) \
15075   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
15076     ARM_BUILTIN_##builtin, UNKNOWN, 0 },
15077
15078   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
15079   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
15080   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
15081   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
15082   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
15083   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
15084   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
15085   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
15086   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
15087   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
15088   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
15089   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
15090   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
15091   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
15092   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
15093   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
15094   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
15095   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
15096   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
15097   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
15098   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
15099   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
15100   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
15101   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
15102   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
15103   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
15104   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
15105   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
15106   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
15107   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
15108   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
15109   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
15110   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
15111   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
15112   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
15113   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
15114   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
15115   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
15116   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
15117   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
15118   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
15119   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
15120   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
15121   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
15122   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
15123   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
15124   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
15125   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
15126   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
15127   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
15128   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
15129   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
15130   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
15131   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
15132   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
15133   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
15134   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
15135   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
15136
15137 #define IWMMXT_BUILTIN2(code, builtin) \
15138   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, UNKNOWN, 0 },
15139
15140   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
15141   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
15142   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
15143   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
15144   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
15145   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
15146   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
15147   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
15148   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
15149   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
15150   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
15151   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
15152   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
15153   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
15154   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
15155   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
15156   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
15157   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
15158   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
15159   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
15160   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
15161   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
15162   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
15163   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
15164   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
15165   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
15166   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
15167   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
15168   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
15169   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
15170   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
15171   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
15172 };
15173
15174 static const struct builtin_description bdesc_1arg[] =
15175 {
15176   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
15177   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
15178   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
15179   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
15180   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
15181   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
15182   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
15183   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
15184   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
15185   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
15186   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
15187   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
15188   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
15189   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
15190   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
15191   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
15192   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
15193   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
15194 };
15195
15196 /* Set up all the iWMMXt builtins.  This is
15197    not called if TARGET_IWMMXT is zero.  */
15198
15199 static void
15200 arm_init_iwmmxt_builtins (void)
15201 {
15202   const struct builtin_description * d;
15203   size_t i;
15204   tree endlink = void_list_node;
15205
15206   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
15207   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
15208   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
15209
15210   tree int_ftype_int
15211     = build_function_type (integer_type_node,
15212                            tree_cons (NULL_TREE, integer_type_node, endlink));
15213   tree v8qi_ftype_v8qi_v8qi_int
15214     = build_function_type (V8QI_type_node,
15215                            tree_cons (NULL_TREE, V8QI_type_node,
15216                                       tree_cons (NULL_TREE, V8QI_type_node,
15217                                                  tree_cons (NULL_TREE,
15218                                                             integer_type_node,
15219                                                             endlink))));
15220   tree v4hi_ftype_v4hi_int
15221     = build_function_type (V4HI_type_node,
15222                            tree_cons (NULL_TREE, V4HI_type_node,
15223                                       tree_cons (NULL_TREE, integer_type_node,
15224                                                  endlink)));
15225   tree v2si_ftype_v2si_int
15226     = build_function_type (V2SI_type_node,
15227                            tree_cons (NULL_TREE, V2SI_type_node,
15228                                       tree_cons (NULL_TREE, integer_type_node,
15229                                                  endlink)));
15230   tree v2si_ftype_di_di
15231     = build_function_type (V2SI_type_node,
15232                            tree_cons (NULL_TREE, long_long_integer_type_node,
15233                                       tree_cons (NULL_TREE, long_long_integer_type_node,
15234                                                  endlink)));
15235   tree di_ftype_di_int
15236     = build_function_type (long_long_integer_type_node,
15237                            tree_cons (NULL_TREE, long_long_integer_type_node,
15238                                       tree_cons (NULL_TREE, integer_type_node,
15239                                                  endlink)));
15240   tree di_ftype_di_int_int
15241     = build_function_type (long_long_integer_type_node,
15242                            tree_cons (NULL_TREE, long_long_integer_type_node,
15243                                       tree_cons (NULL_TREE, integer_type_node,
15244                                                  tree_cons (NULL_TREE,
15245                                                             integer_type_node,
15246                                                             endlink))));
15247   tree int_ftype_v8qi
15248     = build_function_type (integer_type_node,
15249                            tree_cons (NULL_TREE, V8QI_type_node,
15250                                       endlink));
15251   tree int_ftype_v4hi
15252     = build_function_type (integer_type_node,
15253                            tree_cons (NULL_TREE, V4HI_type_node,
15254                                       endlink));
15255   tree int_ftype_v2si
15256     = build_function_type (integer_type_node,
15257                            tree_cons (NULL_TREE, V2SI_type_node,
15258                                       endlink));
15259   tree int_ftype_v8qi_int
15260     = build_function_type (integer_type_node,
15261                            tree_cons (NULL_TREE, V8QI_type_node,
15262                                       tree_cons (NULL_TREE, integer_type_node,
15263                                                  endlink)));
15264   tree int_ftype_v4hi_int
15265     = build_function_type (integer_type_node,
15266                            tree_cons (NULL_TREE, V4HI_type_node,
15267                                       tree_cons (NULL_TREE, integer_type_node,
15268                                                  endlink)));
15269   tree int_ftype_v2si_int
15270     = build_function_type (integer_type_node,
15271                            tree_cons (NULL_TREE, V2SI_type_node,
15272                                       tree_cons (NULL_TREE, integer_type_node,
15273                                                  endlink)));
15274   tree v8qi_ftype_v8qi_int_int
15275     = build_function_type (V8QI_type_node,
15276                            tree_cons (NULL_TREE, V8QI_type_node,
15277                                       tree_cons (NULL_TREE, integer_type_node,
15278                                                  tree_cons (NULL_TREE,
15279                                                             integer_type_node,
15280                                                             endlink))));
15281   tree v4hi_ftype_v4hi_int_int
15282     = build_function_type (V4HI_type_node,
15283                            tree_cons (NULL_TREE, V4HI_type_node,
15284                                       tree_cons (NULL_TREE, integer_type_node,
15285                                                  tree_cons (NULL_TREE,
15286                                                             integer_type_node,
15287                                                             endlink))));
15288   tree v2si_ftype_v2si_int_int
15289     = build_function_type (V2SI_type_node,
15290                            tree_cons (NULL_TREE, V2SI_type_node,
15291                                       tree_cons (NULL_TREE, integer_type_node,
15292                                                  tree_cons (NULL_TREE,
15293                                                             integer_type_node,
15294                                                             endlink))));
15295   /* Miscellaneous.  */
15296   tree v8qi_ftype_v4hi_v4hi
15297     = build_function_type (V8QI_type_node,
15298                            tree_cons (NULL_TREE, V4HI_type_node,
15299                                       tree_cons (NULL_TREE, V4HI_type_node,
15300                                                  endlink)));
15301   tree v4hi_ftype_v2si_v2si
15302     = build_function_type (V4HI_type_node,
15303                            tree_cons (NULL_TREE, V2SI_type_node,
15304                                       tree_cons (NULL_TREE, V2SI_type_node,
15305                                                  endlink)));
15306   tree v2si_ftype_v4hi_v4hi
15307     = build_function_type (V2SI_type_node,
15308                            tree_cons (NULL_TREE, V4HI_type_node,
15309                                       tree_cons (NULL_TREE, V4HI_type_node,
15310                                                  endlink)));
15311   tree v2si_ftype_v8qi_v8qi
15312     = build_function_type (V2SI_type_node,
15313                            tree_cons (NULL_TREE, V8QI_type_node,
15314                                       tree_cons (NULL_TREE, V8QI_type_node,
15315                                                  endlink)));
15316   tree v4hi_ftype_v4hi_di
15317     = build_function_type (V4HI_type_node,
15318                            tree_cons (NULL_TREE, V4HI_type_node,
15319                                       tree_cons (NULL_TREE,
15320                                                  long_long_integer_type_node,
15321                                                  endlink)));
15322   tree v2si_ftype_v2si_di
15323     = build_function_type (V2SI_type_node,
15324                            tree_cons (NULL_TREE, V2SI_type_node,
15325                                       tree_cons (NULL_TREE,
15326                                                  long_long_integer_type_node,
15327                                                  endlink)));
15328   tree void_ftype_int_int
15329     = build_function_type (void_type_node,
15330                            tree_cons (NULL_TREE, integer_type_node,
15331                                       tree_cons (NULL_TREE, integer_type_node,
15332                                                  endlink)));
15333   tree di_ftype_void
15334     = build_function_type (long_long_unsigned_type_node, endlink);
15335   tree di_ftype_v8qi
15336     = build_function_type (long_long_integer_type_node,
15337                            tree_cons (NULL_TREE, V8QI_type_node,
15338                                       endlink));
15339   tree di_ftype_v4hi
15340     = build_function_type (long_long_integer_type_node,
15341                            tree_cons (NULL_TREE, V4HI_type_node,
15342                                       endlink));
15343   tree di_ftype_v2si
15344     = build_function_type (long_long_integer_type_node,
15345                            tree_cons (NULL_TREE, V2SI_type_node,
15346                                       endlink));
15347   tree v2si_ftype_v4hi
15348     = build_function_type (V2SI_type_node,
15349                            tree_cons (NULL_TREE, V4HI_type_node,
15350                                       endlink));
15351   tree v4hi_ftype_v8qi
15352     = build_function_type (V4HI_type_node,
15353                            tree_cons (NULL_TREE, V8QI_type_node,
15354                                       endlink));
15355
15356   tree di_ftype_di_v4hi_v4hi
15357     = build_function_type (long_long_unsigned_type_node,
15358                            tree_cons (NULL_TREE,
15359                                       long_long_unsigned_type_node,
15360                                       tree_cons (NULL_TREE, V4HI_type_node,
15361                                                  tree_cons (NULL_TREE,
15362                                                             V4HI_type_node,
15363                                                             endlink))));
15364
15365   tree di_ftype_v4hi_v4hi
15366     = build_function_type (long_long_unsigned_type_node,
15367                            tree_cons (NULL_TREE, V4HI_type_node,
15368                                       tree_cons (NULL_TREE, V4HI_type_node,
15369                                                  endlink)));
15370
15371   /* Normal vector binops.  */
15372   tree v8qi_ftype_v8qi_v8qi
15373     = build_function_type (V8QI_type_node,
15374                            tree_cons (NULL_TREE, V8QI_type_node,
15375                                       tree_cons (NULL_TREE, V8QI_type_node,
15376                                                  endlink)));
15377   tree v4hi_ftype_v4hi_v4hi
15378     = build_function_type (V4HI_type_node,
15379                            tree_cons (NULL_TREE, V4HI_type_node,
15380                                       tree_cons (NULL_TREE, V4HI_type_node,
15381                                                  endlink)));
15382   tree v2si_ftype_v2si_v2si
15383     = build_function_type (V2SI_type_node,
15384                            tree_cons (NULL_TREE, V2SI_type_node,
15385                                       tree_cons (NULL_TREE, V2SI_type_node,
15386                                                  endlink)));
15387   tree di_ftype_di_di
15388     = build_function_type (long_long_unsigned_type_node,
15389                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
15390                                       tree_cons (NULL_TREE,
15391                                                  long_long_unsigned_type_node,
15392                                                  endlink)));
15393
15394   /* Add all builtins that are more or less simple operations on two
15395      operands.  */
15396   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15397     {
15398       /* Use one of the operands; the target can have a different mode for
15399          mask-generating compares.  */
15400       enum machine_mode mode;
15401       tree type;
15402
15403       if (d->name == 0)
15404         continue;
15405
15406       mode = insn_data[d->icode].operand[1].mode;
15407
15408       switch (mode)
15409         {
15410         case V8QImode:
15411           type = v8qi_ftype_v8qi_v8qi;
15412           break;
15413         case V4HImode:
15414           type = v4hi_ftype_v4hi_v4hi;
15415           break;
15416         case V2SImode:
15417           type = v2si_ftype_v2si_v2si;
15418           break;
15419         case DImode:
15420           type = di_ftype_di_di;
15421           break;
15422
15423         default:
15424           gcc_unreachable ();
15425         }
15426
15427       def_mbuiltin (d->mask, d->name, type, d->code);
15428     }
15429
15430   /* Add the remaining MMX insns with somewhat more complicated types.  */
15431   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
15432   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
15433   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
15434
15435   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
15436   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
15437   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
15438   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
15439   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
15440   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
15441
15442   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
15443   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
15444   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
15445   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
15446   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
15447   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
15448
15449   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
15450   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
15451   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
15452   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
15453   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
15454   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
15455
15456   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
15457   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
15458   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
15459   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
15460   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
15461   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
15462
15463   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
15464
15465   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
15466   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
15467   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
15468   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
15469
15470   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
15471   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
15472   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
15473   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
15474   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
15475   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
15476   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
15477   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
15478   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
15479
15480   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
15481   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
15482   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
15483
15484   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
15485   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
15486   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
15487
15488   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
15489   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
15490   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
15491   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
15492   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
15493   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
15494
15495   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
15496   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
15497   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
15498   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
15499   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
15500   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
15501   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
15502   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
15503   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
15504   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
15505   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
15506   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
15507
15508   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
15509   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
15510   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
15511   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
15512
15513   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
15514   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
15515   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
15516   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
15517   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
15518   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
15519   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
15520 }
15521
15522 static void
15523 arm_init_tls_builtins (void)
15524 {
15525   tree ftype, decl;
15526
15527   ftype = build_function_type (ptr_type_node, void_list_node);
15528   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
15529                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
15530                                NULL, NULL_TREE);
15531   TREE_NOTHROW (decl) = 1;
15532   TREE_READONLY (decl) = 1;
15533 }
15534
15535 enum neon_builtin_type_bits {
15536   T_V8QI  = 0x0001,
15537   T_V4HI  = 0x0002,
15538   T_V2SI  = 0x0004,
15539   T_V2SF  = 0x0008,
15540   T_DI    = 0x0010,
15541   T_V16QI = 0x0020,
15542   T_V8HI  = 0x0040,
15543   T_V4SI  = 0x0080,
15544   T_V4SF  = 0x0100,
15545   T_V2DI  = 0x0200,
15546   T_TI    = 0x0400,
15547   T_EI    = 0x0800,
15548   T_OI    = 0x1000
15549 };
15550
15551 #define v8qi_UP  T_V8QI
15552 #define v4hi_UP  T_V4HI
15553 #define v2si_UP  T_V2SI
15554 #define v2sf_UP  T_V2SF
15555 #define di_UP    T_DI
15556 #define v16qi_UP T_V16QI
15557 #define v8hi_UP  T_V8HI
15558 #define v4si_UP  T_V4SI
15559 #define v4sf_UP  T_V4SF
15560 #define v2di_UP  T_V2DI
15561 #define ti_UP    T_TI
15562 #define ei_UP    T_EI
15563 #define oi_UP    T_OI
15564
15565 #define UP(X) X##_UP
15566
15567 #define T_MAX 13
15568
15569 typedef enum {
15570   NEON_BINOP,
15571   NEON_TERNOP,
15572   NEON_UNOP,
15573   NEON_GETLANE,
15574   NEON_SETLANE,
15575   NEON_CREATE,
15576   NEON_DUP,
15577   NEON_DUPLANE,
15578   NEON_COMBINE,
15579   NEON_SPLIT,
15580   NEON_LANEMUL,
15581   NEON_LANEMULL,
15582   NEON_LANEMULH,
15583   NEON_LANEMAC,
15584   NEON_SCALARMUL,
15585   NEON_SCALARMULL,
15586   NEON_SCALARMULH,
15587   NEON_SCALARMAC,
15588   NEON_CONVERT,
15589   NEON_FIXCONV,
15590   NEON_SELECT,
15591   NEON_RESULTPAIR,
15592   NEON_REINTERP,
15593   NEON_VTBL,
15594   NEON_VTBX,
15595   NEON_LOAD1,
15596   NEON_LOAD1LANE,
15597   NEON_STORE1,
15598   NEON_STORE1LANE,
15599   NEON_LOADSTRUCT,
15600   NEON_LOADSTRUCTLANE,
15601   NEON_STORESTRUCT,
15602   NEON_STORESTRUCTLANE,
15603   NEON_LOGICBINOP,
15604   NEON_SHIFTINSERT,
15605   NEON_SHIFTIMM,
15606   NEON_SHIFTACC
15607 } neon_itype;
15608
15609 typedef struct {
15610   const char *name;
15611   const neon_itype itype;
15612   const int bits;
15613   const enum insn_code codes[T_MAX];
15614   const unsigned int num_vars;
15615   unsigned int base_fcode;
15616 } neon_builtin_datum;
15617
15618 #define CF(N,X) CODE_FOR_neon_##N##X
15619
15620 #define VAR1(T, N, A) \
15621   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
15622 #define VAR2(T, N, A, B) \
15623   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
15624 #define VAR3(T, N, A, B, C) \
15625   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
15626   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
15627 #define VAR4(T, N, A, B, C, D) \
15628   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
15629   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
15630 #define VAR5(T, N, A, B, C, D, E) \
15631   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
15632   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
15633 #define VAR6(T, N, A, B, C, D, E, F) \
15634   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
15635   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
15636 #define VAR7(T, N, A, B, C, D, E, F, G) \
15637   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
15638   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15639     CF (N, G) }, 7, 0
15640 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
15641   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15642                 | UP (H), \
15643   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15644     CF (N, G), CF (N, H) }, 8, 0
15645 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
15646   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15647                 | UP (H) | UP (I), \
15648   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15649     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
15650 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
15651   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15652                 | UP (H) | UP (I) | UP (J), \
15653   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15654     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
15655
15656 /* The mode entries in the following table correspond to the "key" type of the
15657    instruction variant, i.e. equivalent to that which would be specified after
15658    the assembler mnemonic, which usually refers to the last vector operand.
15659    (Signed/unsigned/polynomial types are not differentiated between though, and
15660    are all mapped onto the same mode for a given element size.) The modes
15661    listed per instruction should be the same as those defined for that
15662    instruction's pattern in neon.md.
15663    WARNING: Variants should be listed in the same increasing order as
15664    neon_builtin_type_bits.  */
15665
15666 static neon_builtin_datum neon_builtin_data[] =
15667 {
15668   { VAR10 (BINOP, vadd,
15669            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15670   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
15671   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
15672   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15673   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15674   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
15675   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15676   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15677   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
15678   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15679   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
15680   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
15681   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
15682   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
15683   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
15684   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
15685   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
15686   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
15687   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
15688   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
15689   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
15690   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
15691   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15692   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15693   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15694   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
15695   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
15696   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
15697   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15698   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15699   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15700   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
15701   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15702   { VAR10 (BINOP, vsub,
15703            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15704   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
15705   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
15706   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15707   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15708   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
15709   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15710   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15711   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15712   { VAR2 (BINOP, vcage, v2sf, v4sf) },
15713   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
15714   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15715   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15716   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
15717   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15718   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
15719   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15720   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15721   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
15722   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15723   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15724   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
15725   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
15726   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
15727   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
15728   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15729   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15730   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15731   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15732   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15733   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15734   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15735   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15736   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
15737   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
15738   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
15739   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15740   /* FIXME: vget_lane supports more variants than this!  */
15741   { VAR10 (GETLANE, vget_lane,
15742            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15743   { VAR10 (SETLANE, vset_lane,
15744            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15745   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
15746   { VAR10 (DUP, vdup_n,
15747            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15748   { VAR10 (DUPLANE, vdup_lane,
15749            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15750   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
15751   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
15752   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
15753   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
15754   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
15755   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
15756   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
15757   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15758   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15759   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
15760   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
15761   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15762   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
15763   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
15764   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15765   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15766   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
15767   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
15768   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15769   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
15770   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
15771   { VAR10 (BINOP, vext,
15772            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15773   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15774   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
15775   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
15776   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
15777   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
15778   { VAR10 (SELECT, vbsl,
15779            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15780   { VAR1 (VTBL, vtbl1, v8qi) },
15781   { VAR1 (VTBL, vtbl2, v8qi) },
15782   { VAR1 (VTBL, vtbl3, v8qi) },
15783   { VAR1 (VTBL, vtbl4, v8qi) },
15784   { VAR1 (VTBX, vtbx1, v8qi) },
15785   { VAR1 (VTBX, vtbx2, v8qi) },
15786   { VAR1 (VTBX, vtbx3, v8qi) },
15787   { VAR1 (VTBX, vtbx4, v8qi) },
15788   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15789   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15790   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15791   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
15792   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
15793   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
15794   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
15795   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
15796   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
15797   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
15798   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
15799   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
15800   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
15801   { VAR10 (LOAD1, vld1,
15802            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15803   { VAR10 (LOAD1LANE, vld1_lane,
15804            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15805   { VAR10 (LOAD1, vld1_dup,
15806            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15807   { VAR10 (STORE1, vst1,
15808            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15809   { VAR10 (STORE1LANE, vst1_lane,
15810            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15811   { VAR9 (LOADSTRUCT,
15812           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15813   { VAR7 (LOADSTRUCTLANE, vld2_lane,
15814           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15815   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
15816   { VAR9 (STORESTRUCT, vst2,
15817           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15818   { VAR7 (STORESTRUCTLANE, vst2_lane,
15819           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15820   { VAR9 (LOADSTRUCT,
15821           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15822   { VAR7 (LOADSTRUCTLANE, vld3_lane,
15823           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15824   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
15825   { VAR9 (STORESTRUCT, vst3,
15826           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15827   { VAR7 (STORESTRUCTLANE, vst3_lane,
15828           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15829   { VAR9 (LOADSTRUCT, vld4,
15830           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15831   { VAR7 (LOADSTRUCTLANE, vld4_lane,
15832           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15833   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
15834   { VAR9 (STORESTRUCT, vst4,
15835           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15836   { VAR7 (STORESTRUCTLANE, vst4_lane,
15837           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15838   { VAR10 (LOGICBINOP, vand,
15839            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15840   { VAR10 (LOGICBINOP, vorr,
15841            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15842   { VAR10 (BINOP, veor,
15843            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15844   { VAR10 (LOGICBINOP, vbic,
15845            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15846   { VAR10 (LOGICBINOP, vorn,
15847            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
15848 };
15849
15850 #undef CF
15851 #undef VAR1
15852 #undef VAR2
15853 #undef VAR3
15854 #undef VAR4
15855 #undef VAR5
15856 #undef VAR6
15857 #undef VAR7
15858 #undef VAR8
15859 #undef VAR9
15860 #undef VAR10
15861
15862 static void
15863 arm_init_neon_builtins (void)
15864 {
15865   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
15866
15867   tree neon_intQI_type_node;
15868   tree neon_intHI_type_node;
15869   tree neon_polyQI_type_node;
15870   tree neon_polyHI_type_node;
15871   tree neon_intSI_type_node;
15872   tree neon_intDI_type_node;
15873   tree neon_float_type_node;
15874
15875   tree intQI_pointer_node;
15876   tree intHI_pointer_node;
15877   tree intSI_pointer_node;
15878   tree intDI_pointer_node;
15879   tree float_pointer_node;
15880
15881   tree const_intQI_node;
15882   tree const_intHI_node;
15883   tree const_intSI_node;
15884   tree const_intDI_node;
15885   tree const_float_node;
15886
15887   tree const_intQI_pointer_node;
15888   tree const_intHI_pointer_node;
15889   tree const_intSI_pointer_node;
15890   tree const_intDI_pointer_node;
15891   tree const_float_pointer_node;
15892
15893   tree V8QI_type_node;
15894   tree V4HI_type_node;
15895   tree V2SI_type_node;
15896   tree V2SF_type_node;
15897   tree V16QI_type_node;
15898   tree V8HI_type_node;
15899   tree V4SI_type_node;
15900   tree V4SF_type_node;
15901   tree V2DI_type_node;
15902
15903   tree intUQI_type_node;
15904   tree intUHI_type_node;
15905   tree intUSI_type_node;
15906   tree intUDI_type_node;
15907
15908   tree intEI_type_node;
15909   tree intOI_type_node;
15910   tree intCI_type_node;
15911   tree intXI_type_node;
15912
15913   tree V8QI_pointer_node;
15914   tree V4HI_pointer_node;
15915   tree V2SI_pointer_node;
15916   tree V2SF_pointer_node;
15917   tree V16QI_pointer_node;
15918   tree V8HI_pointer_node;
15919   tree V4SI_pointer_node;
15920   tree V4SF_pointer_node;
15921   tree V2DI_pointer_node;
15922
15923   tree void_ftype_pv8qi_v8qi_v8qi;
15924   tree void_ftype_pv4hi_v4hi_v4hi;
15925   tree void_ftype_pv2si_v2si_v2si;
15926   tree void_ftype_pv2sf_v2sf_v2sf;
15927   tree void_ftype_pdi_di_di;
15928   tree void_ftype_pv16qi_v16qi_v16qi;
15929   tree void_ftype_pv8hi_v8hi_v8hi;
15930   tree void_ftype_pv4si_v4si_v4si;
15931   tree void_ftype_pv4sf_v4sf_v4sf;
15932   tree void_ftype_pv2di_v2di_v2di;
15933
15934   tree reinterp_ftype_dreg[5][5];
15935   tree reinterp_ftype_qreg[5][5];
15936   tree dreg_types[5], qreg_types[5];
15937
15938   /* Create distinguished type nodes for NEON vector element types,
15939      and pointers to values of such types, so we can detect them later.  */
15940   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15941   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15942   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15943   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15944   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
15945   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
15946   neon_float_type_node = make_node (REAL_TYPE);
15947   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
15948   layout_type (neon_float_type_node);
15949
15950   /* Define typedefs which exactly correspond to the modes we are basing vector
15951      types on.  If you change these names you'll need to change
15952      the table used by arm_mangle_type too.  */
15953   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
15954                                              "__builtin_neon_qi");
15955   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
15956                                              "__builtin_neon_hi");
15957   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
15958                                              "__builtin_neon_si");
15959   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
15960                                              "__builtin_neon_sf");
15961   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
15962                                              "__builtin_neon_di");
15963   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
15964                                              "__builtin_neon_poly8");
15965   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
15966                                              "__builtin_neon_poly16");
15967
15968   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
15969   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
15970   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
15971   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
15972   float_pointer_node = build_pointer_type (neon_float_type_node);
15973
15974   /* Next create constant-qualified versions of the above types.  */
15975   const_intQI_node = build_qualified_type (neon_intQI_type_node,
15976                                            TYPE_QUAL_CONST);
15977   const_intHI_node = build_qualified_type (neon_intHI_type_node,
15978                                            TYPE_QUAL_CONST);
15979   const_intSI_node = build_qualified_type (neon_intSI_type_node,
15980                                            TYPE_QUAL_CONST);
15981   const_intDI_node = build_qualified_type (neon_intDI_type_node,
15982                                            TYPE_QUAL_CONST);
15983   const_float_node = build_qualified_type (neon_float_type_node,
15984                                            TYPE_QUAL_CONST);
15985
15986   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
15987   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
15988   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
15989   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
15990   const_float_pointer_node = build_pointer_type (const_float_node);
15991
15992   /* Now create vector types based on our NEON element types.  */
15993   /* 64-bit vectors.  */
15994   V8QI_type_node =
15995     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
15996   V4HI_type_node =
15997     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
15998   V2SI_type_node =
15999     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
16000   V2SF_type_node =
16001     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
16002   /* 128-bit vectors.  */
16003   V16QI_type_node =
16004     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
16005   V8HI_type_node =
16006     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
16007   V4SI_type_node =
16008     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
16009   V4SF_type_node =
16010     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
16011   V2DI_type_node =
16012     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
16013
16014   /* Unsigned integer types for various mode sizes.  */
16015   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
16016   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
16017   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
16018   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
16019
16020   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
16021                                              "__builtin_neon_uqi");
16022   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
16023                                              "__builtin_neon_uhi");
16024   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
16025                                              "__builtin_neon_usi");
16026   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
16027                                              "__builtin_neon_udi");
16028
16029   /* Opaque integer types for structures of vectors.  */
16030   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
16031   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
16032   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
16033   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
16034
16035   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
16036                                              "__builtin_neon_ti");
16037   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
16038                                              "__builtin_neon_ei");
16039   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
16040                                              "__builtin_neon_oi");
16041   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
16042                                              "__builtin_neon_ci");
16043   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
16044                                              "__builtin_neon_xi");
16045
16046   /* Pointers to vector types.  */
16047   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
16048   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
16049   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
16050   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
16051   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
16052   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
16053   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
16054   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
16055   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
16056
16057   /* Operations which return results as pairs.  */
16058   void_ftype_pv8qi_v8qi_v8qi =
16059     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
16060                               V8QI_type_node, NULL);
16061   void_ftype_pv4hi_v4hi_v4hi =
16062     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
16063                               V4HI_type_node, NULL);
16064   void_ftype_pv2si_v2si_v2si =
16065     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
16066                               V2SI_type_node, NULL);
16067   void_ftype_pv2sf_v2sf_v2sf =
16068     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
16069                               V2SF_type_node, NULL);
16070   void_ftype_pdi_di_di =
16071     build_function_type_list (void_type_node, intDI_pointer_node,
16072                               neon_intDI_type_node, neon_intDI_type_node, NULL);
16073   void_ftype_pv16qi_v16qi_v16qi =
16074     build_function_type_list (void_type_node, V16QI_pointer_node,
16075                               V16QI_type_node, V16QI_type_node, NULL);
16076   void_ftype_pv8hi_v8hi_v8hi =
16077     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
16078                               V8HI_type_node, NULL);
16079   void_ftype_pv4si_v4si_v4si =
16080     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
16081                               V4SI_type_node, NULL);
16082   void_ftype_pv4sf_v4sf_v4sf =
16083     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
16084                               V4SF_type_node, NULL);
16085   void_ftype_pv2di_v2di_v2di =
16086     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
16087                               V2DI_type_node, NULL);
16088
16089   dreg_types[0] = V8QI_type_node;
16090   dreg_types[1] = V4HI_type_node;
16091   dreg_types[2] = V2SI_type_node;
16092   dreg_types[3] = V2SF_type_node;
16093   dreg_types[4] = neon_intDI_type_node;
16094
16095   qreg_types[0] = V16QI_type_node;
16096   qreg_types[1] = V8HI_type_node;
16097   qreg_types[2] = V4SI_type_node;
16098   qreg_types[3] = V4SF_type_node;
16099   qreg_types[4] = V2DI_type_node;
16100
16101   for (i = 0; i < 5; i++)
16102     {
16103       int j;
16104       for (j = 0; j < 5; j++)
16105         {
16106           reinterp_ftype_dreg[i][j]
16107             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
16108           reinterp_ftype_qreg[i][j]
16109             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
16110         }
16111     }
16112
16113   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
16114     {
16115       neon_builtin_datum *d = &neon_builtin_data[i];
16116       unsigned int j, codeidx = 0;
16117
16118       d->base_fcode = fcode;
16119
16120       for (j = 0; j < T_MAX; j++)
16121         {
16122           const char* const modenames[] = {
16123             "v8qi", "v4hi", "v2si", "v2sf", "di",
16124             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
16125           };
16126           char namebuf[60];
16127           tree ftype = NULL;
16128           enum insn_code icode;
16129           int is_load = 0, is_store = 0;
16130
16131           if ((d->bits & (1 << j)) == 0)
16132             continue;
16133
16134           icode = d->codes[codeidx++];
16135
16136           switch (d->itype)
16137             {
16138             case NEON_LOAD1:
16139             case NEON_LOAD1LANE:
16140             case NEON_LOADSTRUCT:
16141             case NEON_LOADSTRUCTLANE:
16142               is_load = 1;
16143               /* Fall through.  */
16144             case NEON_STORE1:
16145             case NEON_STORE1LANE:
16146             case NEON_STORESTRUCT:
16147             case NEON_STORESTRUCTLANE:
16148               if (!is_load)
16149                 is_store = 1;
16150               /* Fall through.  */
16151             case NEON_UNOP:
16152             case NEON_BINOP:
16153             case NEON_LOGICBINOP:
16154             case NEON_SHIFTINSERT:
16155             case NEON_TERNOP:
16156             case NEON_GETLANE:
16157             case NEON_SETLANE:
16158             case NEON_CREATE:
16159             case NEON_DUP:
16160             case NEON_DUPLANE:
16161             case NEON_SHIFTIMM:
16162             case NEON_SHIFTACC:
16163             case NEON_COMBINE:
16164             case NEON_SPLIT:
16165             case NEON_CONVERT:
16166             case NEON_FIXCONV:
16167             case NEON_LANEMUL:
16168             case NEON_LANEMULL:
16169             case NEON_LANEMULH:
16170             case NEON_LANEMAC:
16171             case NEON_SCALARMUL:
16172             case NEON_SCALARMULL:
16173             case NEON_SCALARMULH:
16174             case NEON_SCALARMAC:
16175             case NEON_SELECT:
16176             case NEON_VTBL:
16177             case NEON_VTBX:
16178               {
16179                 int k;
16180                 tree return_type = void_type_node, args = void_list_node;
16181
16182                 /* Build a function type directly from the insn_data for this
16183                    builtin.  The build_function_type() function takes care of
16184                    removing duplicates for us.  */
16185                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
16186                   {
16187                     tree eltype;
16188
16189                     if (is_load && k == 1)
16190                       {
16191                         /* Neon load patterns always have the memory operand
16192                            (a SImode pointer) in the operand 1 position.  We
16193                            want a const pointer to the element type in that
16194                            position.  */
16195                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
16196
16197                         switch (1 << j)
16198                           {
16199                           case T_V8QI:
16200                           case T_V16QI:
16201                             eltype = const_intQI_pointer_node;
16202                             break;
16203
16204                           case T_V4HI:
16205                           case T_V8HI:
16206                             eltype = const_intHI_pointer_node;
16207                             break;
16208
16209                           case T_V2SI:
16210                           case T_V4SI:
16211                             eltype = const_intSI_pointer_node;
16212                             break;
16213
16214                           case T_V2SF:
16215                           case T_V4SF:
16216                             eltype = const_float_pointer_node;
16217                             break;
16218
16219                           case T_DI:
16220                           case T_V2DI:
16221                             eltype = const_intDI_pointer_node;
16222                             break;
16223
16224                           default: gcc_unreachable ();
16225                           }
16226                       }
16227                     else if (is_store && k == 0)
16228                       {
16229                         /* Similarly, Neon store patterns use operand 0 as
16230                            the memory location to store to (a SImode pointer).
16231                            Use a pointer to the element type of the store in
16232                            that position.  */
16233                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
16234
16235                         switch (1 << j)
16236                           {
16237                           case T_V8QI:
16238                           case T_V16QI:
16239                             eltype = intQI_pointer_node;
16240                             break;
16241
16242                           case T_V4HI:
16243                           case T_V8HI:
16244                             eltype = intHI_pointer_node;
16245                             break;
16246
16247                           case T_V2SI:
16248                           case T_V4SI:
16249                             eltype = intSI_pointer_node;
16250                             break;
16251
16252                           case T_V2SF:
16253                           case T_V4SF:
16254                             eltype = float_pointer_node;
16255                             break;
16256
16257                           case T_DI:
16258                           case T_V2DI:
16259                             eltype = intDI_pointer_node;
16260                             break;
16261
16262                           default: gcc_unreachable ();
16263                           }
16264                       }
16265                     else
16266                       {
16267                         switch (insn_data[icode].operand[k].mode)
16268                           {
16269                           case VOIDmode: eltype = void_type_node; break;
16270                           /* Scalars.  */
16271                           case QImode: eltype = neon_intQI_type_node; break;
16272                           case HImode: eltype = neon_intHI_type_node; break;
16273                           case SImode: eltype = neon_intSI_type_node; break;
16274                           case SFmode: eltype = neon_float_type_node; break;
16275                           case DImode: eltype = neon_intDI_type_node; break;
16276                           case TImode: eltype = intTI_type_node; break;
16277                           case EImode: eltype = intEI_type_node; break;
16278                           case OImode: eltype = intOI_type_node; break;
16279                           case CImode: eltype = intCI_type_node; break;
16280                           case XImode: eltype = intXI_type_node; break;
16281                           /* 64-bit vectors.  */
16282                           case V8QImode: eltype = V8QI_type_node; break;
16283                           case V4HImode: eltype = V4HI_type_node; break;
16284                           case V2SImode: eltype = V2SI_type_node; break;
16285                           case V2SFmode: eltype = V2SF_type_node; break;
16286                           /* 128-bit vectors.  */
16287                           case V16QImode: eltype = V16QI_type_node; break;
16288                           case V8HImode: eltype = V8HI_type_node; break;
16289                           case V4SImode: eltype = V4SI_type_node; break;
16290                           case V4SFmode: eltype = V4SF_type_node; break;
16291                           case V2DImode: eltype = V2DI_type_node; break;
16292                           default: gcc_unreachable ();
16293                           }
16294                       }
16295
16296                     if (k == 0 && !is_store)
16297                       return_type = eltype;
16298                     else
16299                       args = tree_cons (NULL_TREE, eltype, args);
16300                   }
16301
16302                 ftype = build_function_type (return_type, args);
16303               }
16304               break;
16305
16306             case NEON_RESULTPAIR:
16307               {
16308                 switch (insn_data[icode].operand[1].mode)
16309                   {
16310                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
16311                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
16312                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
16313                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
16314                   case DImode: ftype = void_ftype_pdi_di_di; break;
16315                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
16316                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
16317                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
16318                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
16319                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
16320                   default: gcc_unreachable ();
16321                   }
16322               }
16323               break;
16324
16325             case NEON_REINTERP:
16326               {
16327                 /* We iterate over 5 doubleword types, then 5 quadword
16328                    types.  */
16329                 int rhs = j % 5;
16330                 switch (insn_data[icode].operand[0].mode)
16331                   {
16332                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
16333                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
16334                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
16335                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
16336                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
16337                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
16338                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
16339                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
16340                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
16341                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
16342                   default: gcc_unreachable ();
16343                   }
16344               }
16345               break;
16346
16347             default:
16348               gcc_unreachable ();
16349             }
16350
16351           gcc_assert (ftype != NULL);
16352
16353           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
16354
16355           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
16356                                 NULL_TREE);
16357         }
16358     }
16359 }
16360
16361 static void
16362 arm_init_fp16_builtins (void)
16363 {
16364   tree fp16_type = make_node (REAL_TYPE);
16365   TYPE_PRECISION (fp16_type) = 16;
16366   layout_type (fp16_type);
16367   (*lang_hooks.types.register_builtin_type) (fp16_type, "__fp16");
16368 }
16369
16370 static void
16371 arm_init_builtins (void)
16372 {
16373   arm_init_tls_builtins ();
16374
16375   if (TARGET_REALLY_IWMMXT)
16376     arm_init_iwmmxt_builtins ();
16377
16378   if (TARGET_NEON)
16379     arm_init_neon_builtins ();
16380
16381   if (arm_fp16_format)
16382     arm_init_fp16_builtins ();
16383 }
16384
16385 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
16386
16387 static const char *
16388 arm_invalid_parameter_type (const_tree t)
16389 {
16390   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
16391     return N_("function parameters cannot have __fp16 type");
16392   return NULL;
16393 }
16394
16395 /* Implement TARGET_INVALID_PARAMETER_TYPE.  */
16396
16397 static const char *
16398 arm_invalid_return_type (const_tree t)
16399 {
16400   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
16401     return N_("functions cannot return __fp16 type");
16402   return NULL;
16403 }
16404
16405 /* Implement TARGET_PROMOTED_TYPE.  */
16406
16407 static tree
16408 arm_promoted_type (const_tree t)
16409 {
16410   if (SCALAR_FLOAT_TYPE_P (t) && TYPE_PRECISION (t) == 16)
16411     return float_type_node;
16412   return NULL_TREE;
16413 }
16414
16415 /* Implement TARGET_CONVERT_TO_TYPE.
16416    Specifically, this hook implements the peculiarity of the ARM
16417    half-precision floating-point C semantics that requires conversions between
16418    __fp16 to or from double to do an intermediate conversion to float.  */
16419
16420 static tree
16421 arm_convert_to_type (tree type, tree expr)
16422 {
16423   tree fromtype = TREE_TYPE (expr);
16424   if (!SCALAR_FLOAT_TYPE_P (fromtype) || !SCALAR_FLOAT_TYPE_P (type))
16425     return NULL_TREE;
16426   if ((TYPE_PRECISION (fromtype) == 16 && TYPE_PRECISION (type) > 32)
16427       || (TYPE_PRECISION (type) == 16 && TYPE_PRECISION (fromtype) > 32))
16428     return convert (type, convert (float_type_node, expr));
16429   return NULL_TREE;
16430 }
16431
16432 /* Implement TARGET_SCALAR_MODE_SUPPORTED_P.
16433    This simply adds HFmode as a supported mode; even though we don't
16434    implement arithmetic on this type directly, it's supported by
16435    optabs conversions, much the way the double-word arithmetic is
16436    special-cased in the default hook.  */
16437
16438 static bool
16439 arm_scalar_mode_supported_p (enum machine_mode mode)
16440 {
16441   if (mode == HFmode)
16442     return (arm_fp16_format != ARM_FP16_FORMAT_NONE);
16443   else
16444     return default_scalar_mode_supported_p (mode);
16445 }
16446
16447 /* Errors in the source file can cause expand_expr to return const0_rtx
16448    where we expect a vector.  To avoid crashing, use one of the vector
16449    clear instructions.  */
16450
16451 static rtx
16452 safe_vector_operand (rtx x, enum machine_mode mode)
16453 {
16454   if (x != const0_rtx)
16455     return x;
16456   x = gen_reg_rtx (mode);
16457
16458   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
16459                                : gen_rtx_SUBREG (DImode, x, 0)));
16460   return x;
16461 }
16462
16463 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
16464
16465 static rtx
16466 arm_expand_binop_builtin (enum insn_code icode,
16467                           tree exp, rtx target)
16468 {
16469   rtx pat;
16470   tree arg0 = CALL_EXPR_ARG (exp, 0);
16471   tree arg1 = CALL_EXPR_ARG (exp, 1);
16472   rtx op0 = expand_normal (arg0);
16473   rtx op1 = expand_normal (arg1);
16474   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16475   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16476   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
16477
16478   if (VECTOR_MODE_P (mode0))
16479     op0 = safe_vector_operand (op0, mode0);
16480   if (VECTOR_MODE_P (mode1))
16481     op1 = safe_vector_operand (op1, mode1);
16482
16483   if (! target
16484       || GET_MODE (target) != tmode
16485       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16486     target = gen_reg_rtx (tmode);
16487
16488   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
16489
16490   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16491     op0 = copy_to_mode_reg (mode0, op0);
16492   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16493     op1 = copy_to_mode_reg (mode1, op1);
16494
16495   pat = GEN_FCN (icode) (target, op0, op1);
16496   if (! pat)
16497     return 0;
16498   emit_insn (pat);
16499   return target;
16500 }
16501
16502 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
16503
16504 static rtx
16505 arm_expand_unop_builtin (enum insn_code icode,
16506                          tree exp, rtx target, int do_load)
16507 {
16508   rtx pat;
16509   tree arg0 = CALL_EXPR_ARG (exp, 0);
16510   rtx op0 = expand_normal (arg0);
16511   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16512   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16513
16514   if (! target
16515       || GET_MODE (target) != tmode
16516       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16517     target = gen_reg_rtx (tmode);
16518   if (do_load)
16519     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16520   else
16521     {
16522       if (VECTOR_MODE_P (mode0))
16523         op0 = safe_vector_operand (op0, mode0);
16524
16525       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16526         op0 = copy_to_mode_reg (mode0, op0);
16527     }
16528
16529   pat = GEN_FCN (icode) (target, op0);
16530   if (! pat)
16531     return 0;
16532   emit_insn (pat);
16533   return target;
16534 }
16535
16536 static int
16537 neon_builtin_compare (const void *a, const void *b)
16538 {
16539   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
16540   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
16541   unsigned int soughtcode = key->base_fcode;
16542
16543   if (soughtcode >= memb->base_fcode
16544       && soughtcode < memb->base_fcode + memb->num_vars)
16545     return 0;
16546   else if (soughtcode < memb->base_fcode)
16547     return -1;
16548   else
16549     return 1;
16550 }
16551
16552 static enum insn_code
16553 locate_neon_builtin_icode (int fcode, neon_itype *itype)
16554 {
16555   neon_builtin_datum key, *found;
16556   int idx;
16557
16558   key.base_fcode = fcode;
16559   found = (neon_builtin_datum *)
16560     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
16561                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
16562   gcc_assert (found);
16563   idx = fcode - (int) found->base_fcode;
16564   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
16565
16566   if (itype)
16567     *itype = found->itype;
16568
16569   return found->codes[idx];
16570 }
16571
16572 typedef enum {
16573   NEON_ARG_COPY_TO_REG,
16574   NEON_ARG_CONSTANT,
16575   NEON_ARG_STOP
16576 } builtin_arg;
16577
16578 #define NEON_MAX_BUILTIN_ARGS 5
16579
16580 /* Expand a Neon builtin.  */
16581 static rtx
16582 arm_expand_neon_args (rtx target, int icode, int have_retval,
16583                       tree exp, ...)
16584 {
16585   va_list ap;
16586   rtx pat;
16587   tree arg[NEON_MAX_BUILTIN_ARGS];
16588   rtx op[NEON_MAX_BUILTIN_ARGS];
16589   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16590   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
16591   int argc = 0;
16592
16593   if (have_retval
16594       && (!target
16595           || GET_MODE (target) != tmode
16596           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
16597     target = gen_reg_rtx (tmode);
16598
16599   va_start (ap, exp);
16600
16601   for (;;)
16602     {
16603       builtin_arg thisarg = (builtin_arg) va_arg (ap, int);
16604
16605       if (thisarg == NEON_ARG_STOP)
16606         break;
16607       else
16608         {
16609           arg[argc] = CALL_EXPR_ARG (exp, argc);
16610           op[argc] = expand_normal (arg[argc]);
16611           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
16612
16613           switch (thisarg)
16614             {
16615             case NEON_ARG_COPY_TO_REG:
16616               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
16617               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
16618                      (op[argc], mode[argc]))
16619                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
16620               break;
16621
16622             case NEON_ARG_CONSTANT:
16623               /* FIXME: This error message is somewhat unhelpful.  */
16624               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
16625                     (op[argc], mode[argc]))
16626                 error ("argument must be a constant");
16627               break;
16628
16629             case NEON_ARG_STOP:
16630               gcc_unreachable ();
16631             }
16632
16633           argc++;
16634         }
16635     }
16636
16637   va_end (ap);
16638
16639   if (have_retval)
16640     switch (argc)
16641       {
16642       case 1:
16643         pat = GEN_FCN (icode) (target, op[0]);
16644         break;
16645
16646       case 2:
16647         pat = GEN_FCN (icode) (target, op[0], op[1]);
16648         break;
16649
16650       case 3:
16651         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
16652         break;
16653
16654       case 4:
16655         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
16656         break;
16657
16658       case 5:
16659         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
16660         break;
16661
16662       default:
16663         gcc_unreachable ();
16664       }
16665   else
16666     switch (argc)
16667       {
16668       case 1:
16669         pat = GEN_FCN (icode) (op[0]);
16670         break;
16671
16672       case 2:
16673         pat = GEN_FCN (icode) (op[0], op[1]);
16674         break;
16675
16676       case 3:
16677         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
16678         break;
16679
16680       case 4:
16681         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
16682         break;
16683
16684       case 5:
16685         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
16686         break;
16687
16688       default:
16689         gcc_unreachable ();
16690       }
16691
16692   if (!pat)
16693     return 0;
16694
16695   emit_insn (pat);
16696
16697   return target;
16698 }
16699
16700 /* Expand a Neon builtin. These are "special" because they don't have symbolic
16701    constants defined per-instruction or per instruction-variant. Instead, the
16702    required info is looked up in the table neon_builtin_data.  */
16703 static rtx
16704 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
16705 {
16706   neon_itype itype;
16707   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
16708
16709   switch (itype)
16710     {
16711     case NEON_UNOP:
16712     case NEON_CONVERT:
16713     case NEON_DUPLANE:
16714       return arm_expand_neon_args (target, icode, 1, exp,
16715         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16716
16717     case NEON_BINOP:
16718     case NEON_SETLANE:
16719     case NEON_SCALARMUL:
16720     case NEON_SCALARMULL:
16721     case NEON_SCALARMULH:
16722     case NEON_SHIFTINSERT:
16723     case NEON_LOGICBINOP:
16724       return arm_expand_neon_args (target, icode, 1, exp,
16725         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16726         NEON_ARG_STOP);
16727
16728     case NEON_TERNOP:
16729       return arm_expand_neon_args (target, icode, 1, exp,
16730         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16731         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16732
16733     case NEON_GETLANE:
16734     case NEON_FIXCONV:
16735     case NEON_SHIFTIMM:
16736       return arm_expand_neon_args (target, icode, 1, exp,
16737         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
16738         NEON_ARG_STOP);
16739
16740     case NEON_CREATE:
16741       return arm_expand_neon_args (target, icode, 1, exp,
16742         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16743
16744     case NEON_DUP:
16745     case NEON_SPLIT:
16746     case NEON_REINTERP:
16747       return arm_expand_neon_args (target, icode, 1, exp,
16748         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16749
16750     case NEON_COMBINE:
16751     case NEON_VTBL:
16752       return arm_expand_neon_args (target, icode, 1, exp,
16753         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16754
16755     case NEON_RESULTPAIR:
16756       return arm_expand_neon_args (target, icode, 0, exp,
16757         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16758         NEON_ARG_STOP);
16759
16760     case NEON_LANEMUL:
16761     case NEON_LANEMULL:
16762     case NEON_LANEMULH:
16763       return arm_expand_neon_args (target, icode, 1, exp,
16764         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16765         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16766
16767     case NEON_LANEMAC:
16768       return arm_expand_neon_args (target, icode, 1, exp,
16769         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16770         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16771
16772     case NEON_SHIFTACC:
16773       return arm_expand_neon_args (target, icode, 1, exp,
16774         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16775         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16776
16777     case NEON_SCALARMAC:
16778       return arm_expand_neon_args (target, icode, 1, exp,
16779         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16780         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16781
16782     case NEON_SELECT:
16783     case NEON_VTBX:
16784       return arm_expand_neon_args (target, icode, 1, exp,
16785         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16786         NEON_ARG_STOP);
16787
16788     case NEON_LOAD1:
16789     case NEON_LOADSTRUCT:
16790       return arm_expand_neon_args (target, icode, 1, exp,
16791         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16792
16793     case NEON_LOAD1LANE:
16794     case NEON_LOADSTRUCTLANE:
16795       return arm_expand_neon_args (target, icode, 1, exp,
16796         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16797         NEON_ARG_STOP);
16798
16799     case NEON_STORE1:
16800     case NEON_STORESTRUCT:
16801       return arm_expand_neon_args (target, icode, 0, exp,
16802         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16803
16804     case NEON_STORE1LANE:
16805     case NEON_STORESTRUCTLANE:
16806       return arm_expand_neon_args (target, icode, 0, exp,
16807         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16808         NEON_ARG_STOP);
16809     }
16810
16811   gcc_unreachable ();
16812 }
16813
16814 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
16815 void
16816 neon_reinterpret (rtx dest, rtx src)
16817 {
16818   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
16819 }
16820
16821 /* Emit code to place a Neon pair result in memory locations (with equal
16822    registers).  */
16823 void
16824 neon_emit_pair_result_insn (enum machine_mode mode,
16825                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
16826                             rtx op1, rtx op2)
16827 {
16828   rtx mem = gen_rtx_MEM (mode, destaddr);
16829   rtx tmp1 = gen_reg_rtx (mode);
16830   rtx tmp2 = gen_reg_rtx (mode);
16831
16832   emit_insn (intfn (tmp1, op1, tmp2, op2));
16833
16834   emit_move_insn (mem, tmp1);
16835   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
16836   emit_move_insn (mem, tmp2);
16837 }
16838
16839 /* Set up operands for a register copy from src to dest, taking care not to
16840    clobber registers in the process.
16841    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
16842    be called with a large N, so that should be OK.  */
16843
16844 void
16845 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
16846 {
16847   unsigned int copied = 0, opctr = 0;
16848   unsigned int done = (1 << count) - 1;
16849   unsigned int i, j;
16850
16851   while (copied != done)
16852     {
16853       for (i = 0; i < count; i++)
16854         {
16855           int good = 1;
16856
16857           for (j = 0; good && j < count; j++)
16858             if (i != j && (copied & (1 << j)) == 0
16859                 && reg_overlap_mentioned_p (src[j], dest[i]))
16860               good = 0;
16861
16862           if (good)
16863             {
16864               operands[opctr++] = dest[i];
16865               operands[opctr++] = src[i];
16866               copied |= 1 << i;
16867             }
16868         }
16869     }
16870
16871   gcc_assert (opctr == count * 2);
16872 }
16873
16874 /* Expand an expression EXP that calls a built-in function,
16875    with result going to TARGET if that's convenient
16876    (and in mode MODE if that's convenient).
16877    SUBTARGET may be used as the target for computing one of EXP's operands.
16878    IGNORE is nonzero if the value is to be ignored.  */
16879
16880 static rtx
16881 arm_expand_builtin (tree exp,
16882                     rtx target,
16883                     rtx subtarget ATTRIBUTE_UNUSED,
16884                     enum machine_mode mode ATTRIBUTE_UNUSED,
16885                     int ignore ATTRIBUTE_UNUSED)
16886 {
16887   const struct builtin_description * d;
16888   enum insn_code    icode;
16889   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16890   tree              arg0;
16891   tree              arg1;
16892   tree              arg2;
16893   rtx               op0;
16894   rtx               op1;
16895   rtx               op2;
16896   rtx               pat;
16897   int               fcode = DECL_FUNCTION_CODE (fndecl);
16898   size_t            i;
16899   enum machine_mode tmode;
16900   enum machine_mode mode0;
16901   enum machine_mode mode1;
16902   enum machine_mode mode2;
16903
16904   if (fcode >= ARM_BUILTIN_NEON_BASE)
16905     return arm_expand_neon_builtin (fcode, exp, target);
16906
16907   switch (fcode)
16908     {
16909     case ARM_BUILTIN_TEXTRMSB:
16910     case ARM_BUILTIN_TEXTRMUB:
16911     case ARM_BUILTIN_TEXTRMSH:
16912     case ARM_BUILTIN_TEXTRMUH:
16913     case ARM_BUILTIN_TEXTRMSW:
16914     case ARM_BUILTIN_TEXTRMUW:
16915       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
16916                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
16917                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
16918                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
16919                : CODE_FOR_iwmmxt_textrmw);
16920
16921       arg0 = CALL_EXPR_ARG (exp, 0);
16922       arg1 = CALL_EXPR_ARG (exp, 1);
16923       op0 = expand_normal (arg0);
16924       op1 = expand_normal (arg1);
16925       tmode = insn_data[icode].operand[0].mode;
16926       mode0 = insn_data[icode].operand[1].mode;
16927       mode1 = insn_data[icode].operand[2].mode;
16928
16929       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16930         op0 = copy_to_mode_reg (mode0, op0);
16931       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16932         {
16933           /* @@@ better error message */
16934           error ("selector must be an immediate");
16935           return gen_reg_rtx (tmode);
16936         }
16937       if (target == 0
16938           || GET_MODE (target) != tmode
16939           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16940         target = gen_reg_rtx (tmode);
16941       pat = GEN_FCN (icode) (target, op0, op1);
16942       if (! pat)
16943         return 0;
16944       emit_insn (pat);
16945       return target;
16946
16947     case ARM_BUILTIN_TINSRB:
16948     case ARM_BUILTIN_TINSRH:
16949     case ARM_BUILTIN_TINSRW:
16950       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
16951                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
16952                : CODE_FOR_iwmmxt_tinsrw);
16953       arg0 = CALL_EXPR_ARG (exp, 0);
16954       arg1 = CALL_EXPR_ARG (exp, 1);
16955       arg2 = CALL_EXPR_ARG (exp, 2);
16956       op0 = expand_normal (arg0);
16957       op1 = expand_normal (arg1);
16958       op2 = expand_normal (arg2);
16959       tmode = insn_data[icode].operand[0].mode;
16960       mode0 = insn_data[icode].operand[1].mode;
16961       mode1 = insn_data[icode].operand[2].mode;
16962       mode2 = insn_data[icode].operand[3].mode;
16963
16964       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16965         op0 = copy_to_mode_reg (mode0, op0);
16966       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16967         op1 = copy_to_mode_reg (mode1, op1);
16968       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16969         {
16970           /* @@@ better error message */
16971           error ("selector must be an immediate");
16972           return const0_rtx;
16973         }
16974       if (target == 0
16975           || GET_MODE (target) != tmode
16976           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16977         target = gen_reg_rtx (tmode);
16978       pat = GEN_FCN (icode) (target, op0, op1, op2);
16979       if (! pat)
16980         return 0;
16981       emit_insn (pat);
16982       return target;
16983
16984     case ARM_BUILTIN_SETWCX:
16985       arg0 = CALL_EXPR_ARG (exp, 0);
16986       arg1 = CALL_EXPR_ARG (exp, 1);
16987       op0 = force_reg (SImode, expand_normal (arg0));
16988       op1 = expand_normal (arg1);
16989       emit_insn (gen_iwmmxt_tmcr (op1, op0));
16990       return 0;
16991
16992     case ARM_BUILTIN_GETWCX:
16993       arg0 = CALL_EXPR_ARG (exp, 0);
16994       op0 = expand_normal (arg0);
16995       target = gen_reg_rtx (SImode);
16996       emit_insn (gen_iwmmxt_tmrc (target, op0));
16997       return target;
16998
16999     case ARM_BUILTIN_WSHUFH:
17000       icode = CODE_FOR_iwmmxt_wshufh;
17001       arg0 = CALL_EXPR_ARG (exp, 0);
17002       arg1 = CALL_EXPR_ARG (exp, 1);
17003       op0 = expand_normal (arg0);
17004       op1 = expand_normal (arg1);
17005       tmode = insn_data[icode].operand[0].mode;
17006       mode1 = insn_data[icode].operand[1].mode;
17007       mode2 = insn_data[icode].operand[2].mode;
17008
17009       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17010         op0 = copy_to_mode_reg (mode1, op0);
17011       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17012         {
17013           /* @@@ better error message */
17014           error ("mask must be an immediate");
17015           return const0_rtx;
17016         }
17017       if (target == 0
17018           || GET_MODE (target) != tmode
17019           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17020         target = gen_reg_rtx (tmode);
17021       pat = GEN_FCN (icode) (target, op0, op1);
17022       if (! pat)
17023         return 0;
17024       emit_insn (pat);
17025       return target;
17026
17027     case ARM_BUILTIN_WSADB:
17028       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
17029     case ARM_BUILTIN_WSADH:
17030       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
17031     case ARM_BUILTIN_WSADBZ:
17032       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
17033     case ARM_BUILTIN_WSADHZ:
17034       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
17035
17036       /* Several three-argument builtins.  */
17037     case ARM_BUILTIN_WMACS:
17038     case ARM_BUILTIN_WMACU:
17039     case ARM_BUILTIN_WALIGN:
17040     case ARM_BUILTIN_TMIA:
17041     case ARM_BUILTIN_TMIAPH:
17042     case ARM_BUILTIN_TMIATT:
17043     case ARM_BUILTIN_TMIATB:
17044     case ARM_BUILTIN_TMIABT:
17045     case ARM_BUILTIN_TMIABB:
17046       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
17047                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
17048                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
17049                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
17050                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
17051                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
17052                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
17053                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
17054                : CODE_FOR_iwmmxt_walign);
17055       arg0 = CALL_EXPR_ARG (exp, 0);
17056       arg1 = CALL_EXPR_ARG (exp, 1);
17057       arg2 = CALL_EXPR_ARG (exp, 2);
17058       op0 = expand_normal (arg0);
17059       op1 = expand_normal (arg1);
17060       op2 = expand_normal (arg2);
17061       tmode = insn_data[icode].operand[0].mode;
17062       mode0 = insn_data[icode].operand[1].mode;
17063       mode1 = insn_data[icode].operand[2].mode;
17064       mode2 = insn_data[icode].operand[3].mode;
17065
17066       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17067         op0 = copy_to_mode_reg (mode0, op0);
17068       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
17069         op1 = copy_to_mode_reg (mode1, op1);
17070       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
17071         op2 = copy_to_mode_reg (mode2, op2);
17072       if (target == 0
17073           || GET_MODE (target) != tmode
17074           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17075         target = gen_reg_rtx (tmode);
17076       pat = GEN_FCN (icode) (target, op0, op1, op2);
17077       if (! pat)
17078         return 0;
17079       emit_insn (pat);
17080       return target;
17081
17082     case ARM_BUILTIN_WZERO:
17083       target = gen_reg_rtx (DImode);
17084       emit_insn (gen_iwmmxt_clrdi (target));
17085       return target;
17086
17087     case ARM_BUILTIN_THREAD_POINTER:
17088       return arm_load_tp (target);
17089
17090     default:
17091       break;
17092     }
17093
17094   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17095     if (d->code == (const enum arm_builtins) fcode)
17096       return arm_expand_binop_builtin (d->icode, exp, target);
17097
17098   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17099     if (d->code == (const enum arm_builtins) fcode)
17100       return arm_expand_unop_builtin (d->icode, exp, target, 0);
17101
17102   /* @@@ Should really do something sensible here.  */
17103   return NULL_RTX;
17104 }
17105 \f
17106 /* Return the number (counting from 0) of
17107    the least significant set bit in MASK.  */
17108
17109 inline static int
17110 number_of_first_bit_set (unsigned mask)
17111 {
17112   int bit;
17113
17114   for (bit = 0;
17115        (mask & (1 << bit)) == 0;
17116        ++bit)
17117     continue;
17118
17119   return bit;
17120 }
17121
17122 /* Emit code to push or pop registers to or from the stack.  F is the
17123    assembly file.  MASK is the registers to push or pop.  PUSH is
17124    nonzero if we should push, and zero if we should pop.  For debugging
17125    output, if pushing, adjust CFA_OFFSET by the amount of space added
17126    to the stack.  REAL_REGS should have the same number of bits set as
17127    MASK, and will be used instead (in the same order) to describe which
17128    registers were saved - this is used to mark the save slots when we
17129    push high registers after moving them to low registers.  */
17130 static void
17131 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
17132                unsigned long real_regs)
17133 {
17134   int regno;
17135   int lo_mask = mask & 0xFF;
17136   int pushed_words = 0;
17137
17138   gcc_assert (mask);
17139
17140   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
17141     {
17142       /* Special case.  Do not generate a POP PC statement here, do it in
17143          thumb_exit() */
17144       thumb_exit (f, -1);
17145       return;
17146     }
17147
17148   if (ARM_EABI_UNWIND_TABLES && push)
17149     {
17150       fprintf (f, "\t.save\t{");
17151       for (regno = 0; regno < 15; regno++)
17152         {
17153           if (real_regs & (1 << regno))
17154             {
17155               if (real_regs & ((1 << regno) -1))
17156                 fprintf (f, ", ");
17157               asm_fprintf (f, "%r", regno);
17158             }
17159         }
17160       fprintf (f, "}\n");
17161     }
17162
17163   fprintf (f, "\t%s\t{", push ? "push" : "pop");
17164
17165   /* Look at the low registers first.  */
17166   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
17167     {
17168       if (lo_mask & 1)
17169         {
17170           asm_fprintf (f, "%r", regno);
17171
17172           if ((lo_mask & ~1) != 0)
17173             fprintf (f, ", ");
17174
17175           pushed_words++;
17176         }
17177     }
17178
17179   if (push && (mask & (1 << LR_REGNUM)))
17180     {
17181       /* Catch pushing the LR.  */
17182       if (mask & 0xFF)
17183         fprintf (f, ", ");
17184
17185       asm_fprintf (f, "%r", LR_REGNUM);
17186
17187       pushed_words++;
17188     }
17189   else if (!push && (mask & (1 << PC_REGNUM)))
17190     {
17191       /* Catch popping the PC.  */
17192       if (TARGET_INTERWORK || TARGET_BACKTRACE
17193           || crtl->calls_eh_return)
17194         {
17195           /* The PC is never poped directly, instead
17196              it is popped into r3 and then BX is used.  */
17197           fprintf (f, "}\n");
17198
17199           thumb_exit (f, -1);
17200
17201           return;
17202         }
17203       else
17204         {
17205           if (mask & 0xFF)
17206             fprintf (f, ", ");
17207
17208           asm_fprintf (f, "%r", PC_REGNUM);
17209         }
17210     }
17211
17212   fprintf (f, "}\n");
17213
17214   if (push && pushed_words && dwarf2out_do_frame ())
17215     {
17216       char *l = dwarf2out_cfi_label (false);
17217       int pushed_mask = real_regs;
17218
17219       *cfa_offset += pushed_words * 4;
17220       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
17221
17222       pushed_words = 0;
17223       pushed_mask = real_regs;
17224       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
17225         {
17226           if (pushed_mask & 1)
17227             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
17228         }
17229     }
17230 }
17231
17232 /* Generate code to return from a thumb function.
17233    If 'reg_containing_return_addr' is -1, then the return address is
17234    actually on the stack, at the stack pointer.  */
17235 static void
17236 thumb_exit (FILE *f, int reg_containing_return_addr)
17237 {
17238   unsigned regs_available_for_popping;
17239   unsigned regs_to_pop;
17240   int pops_needed;
17241   unsigned available;
17242   unsigned required;
17243   int mode;
17244   int size;
17245   int restore_a4 = FALSE;
17246
17247   /* Compute the registers we need to pop.  */
17248   regs_to_pop = 0;
17249   pops_needed = 0;
17250
17251   if (reg_containing_return_addr == -1)
17252     {
17253       regs_to_pop |= 1 << LR_REGNUM;
17254       ++pops_needed;
17255     }
17256
17257   if (TARGET_BACKTRACE)
17258     {
17259       /* Restore the (ARM) frame pointer and stack pointer.  */
17260       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
17261       pops_needed += 2;
17262     }
17263
17264   /* If there is nothing to pop then just emit the BX instruction and
17265      return.  */
17266   if (pops_needed == 0)
17267     {
17268       if (crtl->calls_eh_return)
17269         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
17270
17271       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
17272       return;
17273     }
17274   /* Otherwise if we are not supporting interworking and we have not created
17275      a backtrace structure and the function was not entered in ARM mode then
17276      just pop the return address straight into the PC.  */
17277   else if (!TARGET_INTERWORK
17278            && !TARGET_BACKTRACE
17279            && !is_called_in_ARM_mode (current_function_decl)
17280            && !crtl->calls_eh_return)
17281     {
17282       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
17283       return;
17284     }
17285
17286   /* Find out how many of the (return) argument registers we can corrupt.  */
17287   regs_available_for_popping = 0;
17288
17289   /* If returning via __builtin_eh_return, the bottom three registers
17290      all contain information needed for the return.  */
17291   if (crtl->calls_eh_return)
17292     size = 12;
17293   else
17294     {
17295       /* If we can deduce the registers used from the function's
17296          return value.  This is more reliable that examining
17297          df_regs_ever_live_p () because that will be set if the register is
17298          ever used in the function, not just if the register is used
17299          to hold a return value.  */
17300
17301       if (crtl->return_rtx != 0)
17302         mode = GET_MODE (crtl->return_rtx);
17303       else
17304         mode = DECL_MODE (DECL_RESULT (current_function_decl));
17305
17306       size = GET_MODE_SIZE (mode);
17307
17308       if (size == 0)
17309         {
17310           /* In a void function we can use any argument register.
17311              In a function that returns a structure on the stack
17312              we can use the second and third argument registers.  */
17313           if (mode == VOIDmode)
17314             regs_available_for_popping =
17315               (1 << ARG_REGISTER (1))
17316               | (1 << ARG_REGISTER (2))
17317               | (1 << ARG_REGISTER (3));
17318           else
17319             regs_available_for_popping =
17320               (1 << ARG_REGISTER (2))
17321               | (1 << ARG_REGISTER (3));
17322         }
17323       else if (size <= 4)
17324         regs_available_for_popping =
17325           (1 << ARG_REGISTER (2))
17326           | (1 << ARG_REGISTER (3));
17327       else if (size <= 8)
17328         regs_available_for_popping =
17329           (1 << ARG_REGISTER (3));
17330     }
17331
17332   /* Match registers to be popped with registers into which we pop them.  */
17333   for (available = regs_available_for_popping,
17334        required  = regs_to_pop;
17335        required != 0 && available != 0;
17336        available &= ~(available & - available),
17337        required  &= ~(required  & - required))
17338     -- pops_needed;
17339
17340   /* If we have any popping registers left over, remove them.  */
17341   if (available > 0)
17342     regs_available_for_popping &= ~available;
17343
17344   /* Otherwise if we need another popping register we can use
17345      the fourth argument register.  */
17346   else if (pops_needed)
17347     {
17348       /* If we have not found any free argument registers and
17349          reg a4 contains the return address, we must move it.  */
17350       if (regs_available_for_popping == 0
17351           && reg_containing_return_addr == LAST_ARG_REGNUM)
17352         {
17353           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
17354           reg_containing_return_addr = LR_REGNUM;
17355         }
17356       else if (size > 12)
17357         {
17358           /* Register a4 is being used to hold part of the return value,
17359              but we have dire need of a free, low register.  */
17360           restore_a4 = TRUE;
17361
17362           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
17363         }
17364
17365       if (reg_containing_return_addr != LAST_ARG_REGNUM)
17366         {
17367           /* The fourth argument register is available.  */
17368           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
17369
17370           --pops_needed;
17371         }
17372     }
17373
17374   /* Pop as many registers as we can.  */
17375   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17376                  regs_available_for_popping);
17377
17378   /* Process the registers we popped.  */
17379   if (reg_containing_return_addr == -1)
17380     {
17381       /* The return address was popped into the lowest numbered register.  */
17382       regs_to_pop &= ~(1 << LR_REGNUM);
17383
17384       reg_containing_return_addr =
17385         number_of_first_bit_set (regs_available_for_popping);
17386
17387       /* Remove this register for the mask of available registers, so that
17388          the return address will not be corrupted by further pops.  */
17389       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
17390     }
17391
17392   /* If we popped other registers then handle them here.  */
17393   if (regs_available_for_popping)
17394     {
17395       int frame_pointer;
17396
17397       /* Work out which register currently contains the frame pointer.  */
17398       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
17399
17400       /* Move it into the correct place.  */
17401       asm_fprintf (f, "\tmov\t%r, %r\n",
17402                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
17403
17404       /* (Temporarily) remove it from the mask of popped registers.  */
17405       regs_available_for_popping &= ~(1 << frame_pointer);
17406       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
17407
17408       if (regs_available_for_popping)
17409         {
17410           int stack_pointer;
17411
17412           /* We popped the stack pointer as well,
17413              find the register that contains it.  */
17414           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
17415
17416           /* Move it into the stack register.  */
17417           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
17418
17419           /* At this point we have popped all necessary registers, so
17420              do not worry about restoring regs_available_for_popping
17421              to its correct value:
17422
17423              assert (pops_needed == 0)
17424              assert (regs_available_for_popping == (1 << frame_pointer))
17425              assert (regs_to_pop == (1 << STACK_POINTER))  */
17426         }
17427       else
17428         {
17429           /* Since we have just move the popped value into the frame
17430              pointer, the popping register is available for reuse, and
17431              we know that we still have the stack pointer left to pop.  */
17432           regs_available_for_popping |= (1 << frame_pointer);
17433         }
17434     }
17435
17436   /* If we still have registers left on the stack, but we no longer have
17437      any registers into which we can pop them, then we must move the return
17438      address into the link register and make available the register that
17439      contained it.  */
17440   if (regs_available_for_popping == 0 && pops_needed > 0)
17441     {
17442       regs_available_for_popping |= 1 << reg_containing_return_addr;
17443
17444       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
17445                    reg_containing_return_addr);
17446
17447       reg_containing_return_addr = LR_REGNUM;
17448     }
17449
17450   /* If we have registers left on the stack then pop some more.
17451      We know that at most we will want to pop FP and SP.  */
17452   if (pops_needed > 0)
17453     {
17454       int  popped_into;
17455       int  move_to;
17456
17457       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17458                      regs_available_for_popping);
17459
17460       /* We have popped either FP or SP.
17461          Move whichever one it is into the correct register.  */
17462       popped_into = number_of_first_bit_set (regs_available_for_popping);
17463       move_to     = number_of_first_bit_set (regs_to_pop);
17464
17465       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
17466
17467       regs_to_pop &= ~(1 << move_to);
17468
17469       --pops_needed;
17470     }
17471
17472   /* If we still have not popped everything then we must have only
17473      had one register available to us and we are now popping the SP.  */
17474   if (pops_needed > 0)
17475     {
17476       int  popped_into;
17477
17478       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17479                      regs_available_for_popping);
17480
17481       popped_into = number_of_first_bit_set (regs_available_for_popping);
17482
17483       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
17484       /*
17485         assert (regs_to_pop == (1 << STACK_POINTER))
17486         assert (pops_needed == 1)
17487       */
17488     }
17489
17490   /* If necessary restore the a4 register.  */
17491   if (restore_a4)
17492     {
17493       if (reg_containing_return_addr != LR_REGNUM)
17494         {
17495           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
17496           reg_containing_return_addr = LR_REGNUM;
17497         }
17498
17499       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
17500     }
17501
17502   if (crtl->calls_eh_return)
17503     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
17504
17505   /* Return to caller.  */
17506   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
17507 }
17508
17509 \f
17510 void
17511 thumb1_final_prescan_insn (rtx insn)
17512 {
17513   if (flag_print_asm_name)
17514     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
17515                  INSN_ADDRESSES (INSN_UID (insn)));
17516 }
17517
17518 int
17519 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
17520 {
17521   unsigned HOST_WIDE_INT mask = 0xff;
17522   int i;
17523
17524   val = val & (unsigned HOST_WIDE_INT)0xffffffffu;
17525   if (val == 0) /* XXX */
17526     return 0;
17527
17528   for (i = 0; i < 25; i++)
17529     if ((val & (mask << i)) == val)
17530       return 1;
17531
17532   return 0;
17533 }
17534
17535 /* Returns nonzero if the current function contains,
17536    or might contain a far jump.  */
17537 static int
17538 thumb_far_jump_used_p (void)
17539 {
17540   rtx insn;
17541
17542   /* This test is only important for leaf functions.  */
17543   /* assert (!leaf_function_p ()); */
17544
17545   /* If we have already decided that far jumps may be used,
17546      do not bother checking again, and always return true even if
17547      it turns out that they are not being used.  Once we have made
17548      the decision that far jumps are present (and that hence the link
17549      register will be pushed onto the stack) we cannot go back on it.  */
17550   if (cfun->machine->far_jump_used)
17551     return 1;
17552
17553   /* If this function is not being called from the prologue/epilogue
17554      generation code then it must be being called from the
17555      INITIAL_ELIMINATION_OFFSET macro.  */
17556   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
17557     {
17558       /* In this case we know that we are being asked about the elimination
17559          of the arg pointer register.  If that register is not being used,
17560          then there are no arguments on the stack, and we do not have to
17561          worry that a far jump might force the prologue to push the link
17562          register, changing the stack offsets.  In this case we can just
17563          return false, since the presence of far jumps in the function will
17564          not affect stack offsets.
17565
17566          If the arg pointer is live (or if it was live, but has now been
17567          eliminated and so set to dead) then we do have to test to see if
17568          the function might contain a far jump.  This test can lead to some
17569          false negatives, since before reload is completed, then length of
17570          branch instructions is not known, so gcc defaults to returning their
17571          longest length, which in turn sets the far jump attribute to true.
17572
17573          A false negative will not result in bad code being generated, but it
17574          will result in a needless push and pop of the link register.  We
17575          hope that this does not occur too often.
17576
17577          If we need doubleword stack alignment this could affect the other
17578          elimination offsets so we can't risk getting it wrong.  */
17579       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
17580         cfun->machine->arg_pointer_live = 1;
17581       else if (!cfun->machine->arg_pointer_live)
17582         return 0;
17583     }
17584
17585   /* Check to see if the function contains a branch
17586      insn with the far jump attribute set.  */
17587   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17588     {
17589       if (GET_CODE (insn) == JUMP_INSN
17590           /* Ignore tablejump patterns.  */
17591           && GET_CODE (PATTERN (insn)) != ADDR_VEC
17592           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
17593           && get_attr_far_jump (insn) == FAR_JUMP_YES
17594           )
17595         {
17596           /* Record the fact that we have decided that
17597              the function does use far jumps.  */
17598           cfun->machine->far_jump_used = 1;
17599           return 1;
17600         }
17601     }
17602
17603   return 0;
17604 }
17605
17606 /* Return nonzero if FUNC must be entered in ARM mode.  */
17607 int
17608 is_called_in_ARM_mode (tree func)
17609 {
17610   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
17611
17612   /* Ignore the problem about functions whose address is taken.  */
17613   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
17614     return TRUE;
17615
17616 #ifdef ARM_PE
17617   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
17618 #else
17619   return FALSE;
17620 #endif
17621 }
17622
17623 /* The bits which aren't usefully expanded as rtl.  */
17624 const char *
17625 thumb_unexpanded_epilogue (void)
17626 {
17627   arm_stack_offsets *offsets;
17628   int regno;
17629   unsigned long live_regs_mask = 0;
17630   int high_regs_pushed = 0;
17631   int had_to_push_lr;
17632   int size;
17633
17634   if (cfun->machine->return_used_this_function != 0)
17635     return "";
17636
17637   if (IS_NAKED (arm_current_func_type ()))
17638     return "";
17639
17640   offsets = arm_get_frame_offsets ();
17641   live_regs_mask = offsets->saved_regs_mask;
17642   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
17643
17644   /* If we can deduce the registers used from the function's return value.
17645      This is more reliable that examining df_regs_ever_live_p () because that
17646      will be set if the register is ever used in the function, not just if
17647      the register is used to hold a return value.  */
17648   size = arm_size_return_regs ();
17649
17650   /* The prolog may have pushed some high registers to use as
17651      work registers.  e.g. the testsuite file:
17652      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
17653      compiles to produce:
17654         push    {r4, r5, r6, r7, lr}
17655         mov     r7, r9
17656         mov     r6, r8
17657         push    {r6, r7}
17658      as part of the prolog.  We have to undo that pushing here.  */
17659
17660   if (high_regs_pushed)
17661     {
17662       unsigned long mask = live_regs_mask & 0xff;
17663       int next_hi_reg;
17664
17665       /* The available low registers depend on the size of the value we are
17666          returning.  */
17667       if (size <= 12)
17668         mask |=  1 << 3;
17669       if (size <= 8)
17670         mask |= 1 << 2;
17671
17672       if (mask == 0)
17673         /* Oh dear!  We have no low registers into which we can pop
17674            high registers!  */
17675         internal_error
17676           ("no low registers available for popping high registers");
17677
17678       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
17679         if (live_regs_mask & (1 << next_hi_reg))
17680           break;
17681
17682       while (high_regs_pushed)
17683         {
17684           /* Find lo register(s) into which the high register(s) can
17685              be popped.  */
17686           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
17687             {
17688               if (mask & (1 << regno))
17689                 high_regs_pushed--;
17690               if (high_regs_pushed == 0)
17691                 break;
17692             }
17693
17694           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
17695
17696           /* Pop the values into the low register(s).  */
17697           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
17698
17699           /* Move the value(s) into the high registers.  */
17700           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
17701             {
17702               if (mask & (1 << regno))
17703                 {
17704                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
17705                                regno);
17706
17707                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
17708                     if (live_regs_mask & (1 << next_hi_reg))
17709                       break;
17710                 }
17711             }
17712         }
17713       live_regs_mask &= ~0x0f00;
17714     }
17715
17716   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
17717   live_regs_mask &= 0xff;
17718
17719   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
17720     {
17721       /* Pop the return address into the PC.  */
17722       if (had_to_push_lr)
17723         live_regs_mask |= 1 << PC_REGNUM;
17724
17725       /* Either no argument registers were pushed or a backtrace
17726          structure was created which includes an adjusted stack
17727          pointer, so just pop everything.  */
17728       if (live_regs_mask)
17729         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
17730                        live_regs_mask);
17731
17732       /* We have either just popped the return address into the
17733          PC or it is was kept in LR for the entire function.  */
17734       if (!had_to_push_lr)
17735         thumb_exit (asm_out_file, LR_REGNUM);
17736     }
17737   else
17738     {
17739       /* Pop everything but the return address.  */
17740       if (live_regs_mask)
17741         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
17742                        live_regs_mask);
17743
17744       if (had_to_push_lr)
17745         {
17746           if (size > 12)
17747             {
17748               /* We have no free low regs, so save one.  */
17749               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
17750                            LAST_ARG_REGNUM);
17751             }
17752
17753           /* Get the return address into a temporary register.  */
17754           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
17755                          1 << LAST_ARG_REGNUM);
17756
17757           if (size > 12)
17758             {
17759               /* Move the return address to lr.  */
17760               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
17761                            LAST_ARG_REGNUM);
17762               /* Restore the low register.  */
17763               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
17764                            IP_REGNUM);
17765               regno = LR_REGNUM;
17766             }
17767           else
17768             regno = LAST_ARG_REGNUM;
17769         }
17770       else
17771         regno = LR_REGNUM;
17772
17773       /* Remove the argument registers that were pushed onto the stack.  */
17774       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
17775                    SP_REGNUM, SP_REGNUM,
17776                    crtl->args.pretend_args_size);
17777
17778       thumb_exit (asm_out_file, regno);
17779     }
17780
17781   return "";
17782 }
17783
17784 /* Functions to save and restore machine-specific function data.  */
17785 static struct machine_function *
17786 arm_init_machine_status (void)
17787 {
17788   struct machine_function *machine;
17789   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
17790
17791 #if ARM_FT_UNKNOWN != 0
17792   machine->func_type = ARM_FT_UNKNOWN;
17793 #endif
17794   return machine;
17795 }
17796
17797 /* Return an RTX indicating where the return address to the
17798    calling function can be found.  */
17799 rtx
17800 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
17801 {
17802   if (count != 0)
17803     return NULL_RTX;
17804
17805   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
17806 }
17807
17808 /* Do anything needed before RTL is emitted for each function.  */
17809 void
17810 arm_init_expanders (void)
17811 {
17812   /* Arrange to initialize and mark the machine per-function status.  */
17813   init_machine_status = arm_init_machine_status;
17814
17815   /* This is to stop the combine pass optimizing away the alignment
17816      adjustment of va_arg.  */
17817   /* ??? It is claimed that this should not be necessary.  */
17818   if (cfun)
17819     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
17820 }
17821
17822
17823 /* Like arm_compute_initial_elimination offset.  Simpler because there
17824    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
17825    to point at the base of the local variables after static stack
17826    space for a function has been allocated.  */
17827
17828 HOST_WIDE_INT
17829 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
17830 {
17831   arm_stack_offsets *offsets;
17832
17833   offsets = arm_get_frame_offsets ();
17834
17835   switch (from)
17836     {
17837     case ARG_POINTER_REGNUM:
17838       switch (to)
17839         {
17840         case STACK_POINTER_REGNUM:
17841           return offsets->outgoing_args - offsets->saved_args;
17842
17843         case FRAME_POINTER_REGNUM:
17844           return offsets->soft_frame - offsets->saved_args;
17845
17846         case ARM_HARD_FRAME_POINTER_REGNUM:
17847           return offsets->saved_regs - offsets->saved_args;
17848
17849         case THUMB_HARD_FRAME_POINTER_REGNUM:
17850           return offsets->locals_base - offsets->saved_args;
17851
17852         default:
17853           gcc_unreachable ();
17854         }
17855       break;
17856
17857     case FRAME_POINTER_REGNUM:
17858       switch (to)
17859         {
17860         case STACK_POINTER_REGNUM:
17861           return offsets->outgoing_args - offsets->soft_frame;
17862
17863         case ARM_HARD_FRAME_POINTER_REGNUM:
17864           return offsets->saved_regs - offsets->soft_frame;
17865
17866         case THUMB_HARD_FRAME_POINTER_REGNUM:
17867           return offsets->locals_base - offsets->soft_frame;
17868
17869         default:
17870           gcc_unreachable ();
17871         }
17872       break;
17873
17874     default:
17875       gcc_unreachable ();
17876     }
17877 }
17878
17879 /* Generate the rest of a function's prologue.  */
17880 void
17881 thumb1_expand_prologue (void)
17882 {
17883   rtx insn, dwarf;
17884
17885   HOST_WIDE_INT amount;
17886   arm_stack_offsets *offsets;
17887   unsigned long func_type;
17888   int regno;
17889   unsigned long live_regs_mask;
17890
17891   func_type = arm_current_func_type ();
17892
17893   /* Naked functions don't have prologues.  */
17894   if (IS_NAKED (func_type))
17895     return;
17896
17897   if (IS_INTERRUPT (func_type))
17898     {
17899       error ("interrupt Service Routines cannot be coded in Thumb mode");
17900       return;
17901     }
17902
17903   offsets = arm_get_frame_offsets ();
17904   live_regs_mask = offsets->saved_regs_mask;
17905   /* Load the pic register before setting the frame pointer,
17906      so we can use r7 as a temporary work register.  */
17907   if (flag_pic && arm_pic_register != INVALID_REGNUM)
17908     arm_load_pic_register (live_regs_mask);
17909
17910   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
17911     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
17912                     stack_pointer_rtx);
17913
17914   amount = offsets->outgoing_args - offsets->saved_regs;
17915   if (amount)
17916     {
17917       if (amount < 512)
17918         {
17919           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17920                                         GEN_INT (- amount)));
17921           RTX_FRAME_RELATED_P (insn) = 1;
17922         }
17923       else
17924         {
17925           rtx reg;
17926
17927           /* The stack decrement is too big for an immediate value in a single
17928              insn.  In theory we could issue multiple subtracts, but after
17929              three of them it becomes more space efficient to place the full
17930              value in the constant pool and load into a register.  (Also the
17931              ARM debugger really likes to see only one stack decrement per
17932              function).  So instead we look for a scratch register into which
17933              we can load the decrement, and then we subtract this from the
17934              stack pointer.  Unfortunately on the thumb the only available
17935              scratch registers are the argument registers, and we cannot use
17936              these as they may hold arguments to the function.  Instead we
17937              attempt to locate a call preserved register which is used by this
17938              function.  If we can find one, then we know that it will have
17939              been pushed at the start of the prologue and so we can corrupt
17940              it now.  */
17941           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
17942             if (live_regs_mask & (1 << regno))
17943               break;
17944
17945           gcc_assert(regno <= LAST_LO_REGNUM);
17946
17947           reg = gen_rtx_REG (SImode, regno);
17948
17949           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
17950
17951           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
17952                                         stack_pointer_rtx, reg));
17953           RTX_FRAME_RELATED_P (insn) = 1;
17954           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17955                                plus_constant (stack_pointer_rtx,
17956                                               -amount));
17957           RTX_FRAME_RELATED_P (dwarf) = 1;
17958           add_reg_note (insn, REG_FRAME_RELATED_EXPR, dwarf);
17959         }
17960     }
17961
17962   if (frame_pointer_needed)
17963     thumb_set_frame_pointer (offsets);
17964
17965   /* If we are profiling, make sure no instructions are scheduled before
17966      the call to mcount.  Similarly if the user has requested no
17967      scheduling in the prolog.  Similarly if we want non-call exceptions
17968      using the EABI unwinder, to prevent faulting instructions from being
17969      swapped with a stack adjustment.  */
17970   if (crtl->profile || !TARGET_SCHED_PROLOG
17971       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
17972     emit_insn (gen_blockage ());
17973
17974   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
17975   if (live_regs_mask & 0xff)
17976     cfun->machine->lr_save_eliminated = 0;
17977 }
17978
17979
17980 void
17981 thumb1_expand_epilogue (void)
17982 {
17983   HOST_WIDE_INT amount;
17984   arm_stack_offsets *offsets;
17985   int regno;
17986
17987   /* Naked functions don't have prologues.  */
17988   if (IS_NAKED (arm_current_func_type ()))
17989     return;
17990
17991   offsets = arm_get_frame_offsets ();
17992   amount = offsets->outgoing_args - offsets->saved_regs;
17993
17994   if (frame_pointer_needed)
17995     {
17996       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
17997       amount = offsets->locals_base - offsets->saved_regs;
17998     }
17999
18000   gcc_assert (amount >= 0);
18001   if (amount)
18002     {
18003       if (amount < 512)
18004         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
18005                                GEN_INT (amount)));
18006       else
18007         {
18008           /* r3 is always free in the epilogue.  */
18009           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
18010
18011           emit_insn (gen_movsi (reg, GEN_INT (amount)));
18012           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
18013         }
18014     }
18015
18016   /* Emit a USE (stack_pointer_rtx), so that
18017      the stack adjustment will not be deleted.  */
18018   emit_insn (gen_prologue_use (stack_pointer_rtx));
18019
18020   if (crtl->profile || !TARGET_SCHED_PROLOG)
18021     emit_insn (gen_blockage ());
18022
18023   /* Emit a clobber for each insn that will be restored in the epilogue,
18024      so that flow2 will get register lifetimes correct.  */
18025   for (regno = 0; regno < 13; regno++)
18026     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
18027       emit_clobber (gen_rtx_REG (SImode, regno));
18028
18029   if (! df_regs_ever_live_p (LR_REGNUM))
18030     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
18031 }
18032
18033 static void
18034 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
18035 {
18036   arm_stack_offsets *offsets;
18037   unsigned long live_regs_mask = 0;
18038   unsigned long l_mask;
18039   unsigned high_regs_pushed = 0;
18040   int cfa_offset = 0;
18041   int regno;
18042
18043   if (IS_NAKED (arm_current_func_type ()))
18044     return;
18045
18046   if (is_called_in_ARM_mode (current_function_decl))
18047     {
18048       const char * name;
18049
18050       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
18051       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
18052                   == SYMBOL_REF);
18053       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
18054
18055       /* Generate code sequence to switch us into Thumb mode.  */
18056       /* The .code 32 directive has already been emitted by
18057          ASM_DECLARE_FUNCTION_NAME.  */
18058       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
18059       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
18060
18061       /* Generate a label, so that the debugger will notice the
18062          change in instruction sets.  This label is also used by
18063          the assembler to bypass the ARM code when this function
18064          is called from a Thumb encoded function elsewhere in the
18065          same file.  Hence the definition of STUB_NAME here must
18066          agree with the definition in gas/config/tc-arm.c.  */
18067
18068 #define STUB_NAME ".real_start_of"
18069
18070       fprintf (f, "\t.code\t16\n");
18071 #ifdef ARM_PE
18072       if (arm_dllexport_name_p (name))
18073         name = arm_strip_name_encoding (name);
18074 #endif
18075       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
18076       fprintf (f, "\t.thumb_func\n");
18077       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
18078     }
18079
18080   if (crtl->args.pretend_args_size)
18081     {
18082       /* Output unwind directive for the stack adjustment.  */
18083       if (ARM_EABI_UNWIND_TABLES)
18084         fprintf (f, "\t.pad #%d\n",
18085                  crtl->args.pretend_args_size);
18086
18087       if (cfun->machine->uses_anonymous_args)
18088         {
18089           int num_pushes;
18090
18091           fprintf (f, "\tpush\t{");
18092
18093           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
18094
18095           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
18096                regno <= LAST_ARG_REGNUM;
18097                regno++)
18098             asm_fprintf (f, "%r%s", regno,
18099                          regno == LAST_ARG_REGNUM ? "" : ", ");
18100
18101           fprintf (f, "}\n");
18102         }
18103       else
18104         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
18105                      SP_REGNUM, SP_REGNUM,
18106                      crtl->args.pretend_args_size);
18107
18108       /* We don't need to record the stores for unwinding (would it
18109          help the debugger any if we did?), but record the change in
18110          the stack pointer.  */
18111       if (dwarf2out_do_frame ())
18112         {
18113           char *l = dwarf2out_cfi_label (false);
18114
18115           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
18116           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
18117         }
18118     }
18119
18120   /* Get the registers we are going to push.  */
18121   offsets = arm_get_frame_offsets ();
18122   live_regs_mask = offsets->saved_regs_mask;
18123   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
18124   l_mask = live_regs_mask & 0x40ff;
18125   /* Then count how many other high registers will need to be pushed.  */
18126   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
18127
18128   if (TARGET_BACKTRACE)
18129     {
18130       unsigned offset;
18131       unsigned work_register;
18132
18133       /* We have been asked to create a stack backtrace structure.
18134          The code looks like this:
18135
18136          0   .align 2
18137          0   func:
18138          0     sub   SP, #16         Reserve space for 4 registers.
18139          2     push  {R7}            Push low registers.
18140          4     add   R7, SP, #20     Get the stack pointer before the push.
18141          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
18142          8     mov   R7, PC          Get hold of the start of this code plus 12.
18143         10     str   R7, [SP, #16]   Store it.
18144         12     mov   R7, FP          Get hold of the current frame pointer.
18145         14     str   R7, [SP, #4]    Store it.
18146         16     mov   R7, LR          Get hold of the current return address.
18147         18     str   R7, [SP, #12]   Store it.
18148         20     add   R7, SP, #16     Point at the start of the backtrace structure.
18149         22     mov   FP, R7          Put this value into the frame pointer.  */
18150
18151       work_register = thumb_find_work_register (live_regs_mask);
18152
18153       if (ARM_EABI_UNWIND_TABLES)
18154         asm_fprintf (f, "\t.pad #16\n");
18155
18156       asm_fprintf
18157         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
18158          SP_REGNUM, SP_REGNUM);
18159
18160       if (dwarf2out_do_frame ())
18161         {
18162           char *l = dwarf2out_cfi_label (false);
18163
18164           cfa_offset = cfa_offset + 16;
18165           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
18166         }
18167
18168       if (l_mask)
18169         {
18170           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
18171           offset = bit_count (l_mask) * UNITS_PER_WORD;
18172         }
18173       else
18174         offset = 0;
18175
18176       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
18177                    offset + 16 + crtl->args.pretend_args_size);
18178
18179       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18180                    offset + 4);
18181
18182       /* Make sure that the instruction fetching the PC is in the right place
18183          to calculate "start of backtrace creation code + 12".  */
18184       if (l_mask)
18185         {
18186           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
18187           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18188                        offset + 12);
18189           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
18190                        ARM_HARD_FRAME_POINTER_REGNUM);
18191           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18192                        offset);
18193         }
18194       else
18195         {
18196           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
18197                        ARM_HARD_FRAME_POINTER_REGNUM);
18198           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18199                        offset);
18200           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
18201           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18202                        offset + 12);
18203         }
18204
18205       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
18206       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
18207                    offset + 8);
18208       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
18209                    offset + 12);
18210       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
18211                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
18212     }
18213   /* Optimization:  If we are not pushing any low registers but we are going
18214      to push some high registers then delay our first push.  This will just
18215      be a push of LR and we can combine it with the push of the first high
18216      register.  */
18217   else if ((l_mask & 0xff) != 0
18218            || (high_regs_pushed == 0 && l_mask))
18219     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
18220
18221   if (high_regs_pushed)
18222     {
18223       unsigned pushable_regs;
18224       unsigned next_hi_reg;
18225
18226       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
18227         if (live_regs_mask & (1 << next_hi_reg))
18228           break;
18229
18230       pushable_regs = l_mask & 0xff;
18231
18232       if (pushable_regs == 0)
18233         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
18234
18235       while (high_regs_pushed > 0)
18236         {
18237           unsigned long real_regs_mask = 0;
18238
18239           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
18240             {
18241               if (pushable_regs & (1 << regno))
18242                 {
18243                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
18244
18245                   high_regs_pushed --;
18246                   real_regs_mask |= (1 << next_hi_reg);
18247
18248                   if (high_regs_pushed)
18249                     {
18250                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
18251                            next_hi_reg --)
18252                         if (live_regs_mask & (1 << next_hi_reg))
18253                           break;
18254                     }
18255                   else
18256                     {
18257                       pushable_regs &= ~((1 << regno) - 1);
18258                       break;
18259                     }
18260                 }
18261             }
18262
18263           /* If we had to find a work register and we have not yet
18264              saved the LR then add it to the list of regs to push.  */
18265           if (l_mask == (1 << LR_REGNUM))
18266             {
18267               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
18268                              1, &cfa_offset,
18269                              real_regs_mask | (1 << LR_REGNUM));
18270               l_mask = 0;
18271             }
18272           else
18273             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
18274         }
18275     }
18276 }
18277
18278 /* Handle the case of a double word load into a low register from
18279    a computed memory address.  The computed address may involve a
18280    register which is overwritten by the load.  */
18281 const char *
18282 thumb_load_double_from_address (rtx *operands)
18283 {
18284   rtx addr;
18285   rtx base;
18286   rtx offset;
18287   rtx arg1;
18288   rtx arg2;
18289
18290   gcc_assert (GET_CODE (operands[0]) == REG);
18291   gcc_assert (GET_CODE (operands[1]) == MEM);
18292
18293   /* Get the memory address.  */
18294   addr = XEXP (operands[1], 0);
18295
18296   /* Work out how the memory address is computed.  */
18297   switch (GET_CODE (addr))
18298     {
18299     case REG:
18300       operands[2] = adjust_address (operands[1], SImode, 4);
18301
18302       if (REGNO (operands[0]) == REGNO (addr))
18303         {
18304           output_asm_insn ("ldr\t%H0, %2", operands);
18305           output_asm_insn ("ldr\t%0, %1", operands);
18306         }
18307       else
18308         {
18309           output_asm_insn ("ldr\t%0, %1", operands);
18310           output_asm_insn ("ldr\t%H0, %2", operands);
18311         }
18312       break;
18313
18314     case CONST:
18315       /* Compute <address> + 4 for the high order load.  */
18316       operands[2] = adjust_address (operands[1], SImode, 4);
18317
18318       output_asm_insn ("ldr\t%0, %1", operands);
18319       output_asm_insn ("ldr\t%H0, %2", operands);
18320       break;
18321
18322     case PLUS:
18323       arg1   = XEXP (addr, 0);
18324       arg2   = XEXP (addr, 1);
18325
18326       if (CONSTANT_P (arg1))
18327         base = arg2, offset = arg1;
18328       else
18329         base = arg1, offset = arg2;
18330
18331       gcc_assert (GET_CODE (base) == REG);
18332
18333       /* Catch the case of <address> = <reg> + <reg> */
18334       if (GET_CODE (offset) == REG)
18335         {
18336           int reg_offset = REGNO (offset);
18337           int reg_base   = REGNO (base);
18338           int reg_dest   = REGNO (operands[0]);
18339
18340           /* Add the base and offset registers together into the
18341              higher destination register.  */
18342           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
18343                        reg_dest + 1, reg_base, reg_offset);
18344
18345           /* Load the lower destination register from the address in
18346              the higher destination register.  */
18347           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
18348                        reg_dest, reg_dest + 1);
18349
18350           /* Load the higher destination register from its own address
18351              plus 4.  */
18352           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
18353                        reg_dest + 1, reg_dest + 1);
18354         }
18355       else
18356         {
18357           /* Compute <address> + 4 for the high order load.  */
18358           operands[2] = adjust_address (operands[1], SImode, 4);
18359
18360           /* If the computed address is held in the low order register
18361              then load the high order register first, otherwise always
18362              load the low order register first.  */
18363           if (REGNO (operands[0]) == REGNO (base))
18364             {
18365               output_asm_insn ("ldr\t%H0, %2", operands);
18366               output_asm_insn ("ldr\t%0, %1", operands);
18367             }
18368           else
18369             {
18370               output_asm_insn ("ldr\t%0, %1", operands);
18371               output_asm_insn ("ldr\t%H0, %2", operands);
18372             }
18373         }
18374       break;
18375
18376     case LABEL_REF:
18377       /* With no registers to worry about we can just load the value
18378          directly.  */
18379       operands[2] = adjust_address (operands[1], SImode, 4);
18380
18381       output_asm_insn ("ldr\t%H0, %2", operands);
18382       output_asm_insn ("ldr\t%0, %1", operands);
18383       break;
18384
18385     default:
18386       gcc_unreachable ();
18387     }
18388
18389   return "";
18390 }
18391
18392 const char *
18393 thumb_output_move_mem_multiple (int n, rtx *operands)
18394 {
18395   rtx tmp;
18396
18397   switch (n)
18398     {
18399     case 2:
18400       if (REGNO (operands[4]) > REGNO (operands[5]))
18401         {
18402           tmp = operands[4];
18403           operands[4] = operands[5];
18404           operands[5] = tmp;
18405         }
18406       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
18407       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
18408       break;
18409
18410     case 3:
18411       if (REGNO (operands[4]) > REGNO (operands[5]))
18412         {
18413           tmp = operands[4];
18414           operands[4] = operands[5];
18415           operands[5] = tmp;
18416         }
18417       if (REGNO (operands[5]) > REGNO (operands[6]))
18418         {
18419           tmp = operands[5];
18420           operands[5] = operands[6];
18421           operands[6] = tmp;
18422         }
18423       if (REGNO (operands[4]) > REGNO (operands[5]))
18424         {
18425           tmp = operands[4];
18426           operands[4] = operands[5];
18427           operands[5] = tmp;
18428         }
18429
18430       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
18431       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
18432       break;
18433
18434     default:
18435       gcc_unreachable ();
18436     }
18437
18438   return "";
18439 }
18440
18441 /* Output a call-via instruction for thumb state.  */
18442 const char *
18443 thumb_call_via_reg (rtx reg)
18444 {
18445   int regno = REGNO (reg);
18446   rtx *labelp;
18447
18448   gcc_assert (regno < LR_REGNUM);
18449
18450   /* If we are in the normal text section we can use a single instance
18451      per compilation unit.  If we are doing function sections, then we need
18452      an entry per section, since we can't rely on reachability.  */
18453   if (in_section == text_section)
18454     {
18455       thumb_call_reg_needed = 1;
18456
18457       if (thumb_call_via_label[regno] == NULL)
18458         thumb_call_via_label[regno] = gen_label_rtx ();
18459       labelp = thumb_call_via_label + regno;
18460     }
18461   else
18462     {
18463       if (cfun->machine->call_via[regno] == NULL)
18464         cfun->machine->call_via[regno] = gen_label_rtx ();
18465       labelp = cfun->machine->call_via + regno;
18466     }
18467
18468   output_asm_insn ("bl\t%a0", labelp);
18469   return "";
18470 }
18471
18472 /* Routines for generating rtl.  */
18473 void
18474 thumb_expand_movmemqi (rtx *operands)
18475 {
18476   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
18477   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
18478   HOST_WIDE_INT len = INTVAL (operands[2]);
18479   HOST_WIDE_INT offset = 0;
18480
18481   while (len >= 12)
18482     {
18483       emit_insn (gen_movmem12b (out, in, out, in));
18484       len -= 12;
18485     }
18486
18487   if (len >= 8)
18488     {
18489       emit_insn (gen_movmem8b (out, in, out, in));
18490       len -= 8;
18491     }
18492
18493   if (len >= 4)
18494     {
18495       rtx reg = gen_reg_rtx (SImode);
18496       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
18497       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
18498       len -= 4;
18499       offset += 4;
18500     }
18501
18502   if (len >= 2)
18503     {
18504       rtx reg = gen_reg_rtx (HImode);
18505       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
18506                                               plus_constant (in, offset))));
18507       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
18508                             reg));
18509       len -= 2;
18510       offset += 2;
18511     }
18512
18513   if (len)
18514     {
18515       rtx reg = gen_reg_rtx (QImode);
18516       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
18517                                               plus_constant (in, offset))));
18518       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
18519                             reg));
18520     }
18521 }
18522
18523 void
18524 thumb_reload_out_hi (rtx *operands)
18525 {
18526   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
18527 }
18528
18529 /* Handle reading a half-word from memory during reload.  */
18530 void
18531 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
18532 {
18533   gcc_unreachable ();
18534 }
18535
18536 /* Return the length of a function name prefix
18537     that starts with the character 'c'.  */
18538 static int
18539 arm_get_strip_length (int c)
18540 {
18541   switch (c)
18542     {
18543     ARM_NAME_ENCODING_LENGTHS
18544       default: return 0;
18545     }
18546 }
18547
18548 /* Return a pointer to a function's name with any
18549    and all prefix encodings stripped from it.  */
18550 const char *
18551 arm_strip_name_encoding (const char *name)
18552 {
18553   int skip;
18554
18555   while ((skip = arm_get_strip_length (* name)))
18556     name += skip;
18557
18558   return name;
18559 }
18560
18561 /* If there is a '*' anywhere in the name's prefix, then
18562    emit the stripped name verbatim, otherwise prepend an
18563    underscore if leading underscores are being used.  */
18564 void
18565 arm_asm_output_labelref (FILE *stream, const char *name)
18566 {
18567   int skip;
18568   int verbatim = 0;
18569
18570   while ((skip = arm_get_strip_length (* name)))
18571     {
18572       verbatim |= (*name == '*');
18573       name += skip;
18574     }
18575
18576   if (verbatim)
18577     fputs (name, stream);
18578   else
18579     asm_fprintf (stream, "%U%s", name);
18580 }
18581
18582 static void
18583 arm_file_start (void)
18584 {
18585   int val;
18586
18587   if (TARGET_UNIFIED_ASM)
18588     asm_fprintf (asm_out_file, "\t.syntax unified\n");
18589
18590   if (TARGET_BPABI)
18591     {
18592       const char *fpu_name;
18593       if (arm_select[0].string)
18594         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
18595       else if (arm_select[1].string)
18596         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
18597       else
18598         asm_fprintf (asm_out_file, "\t.cpu %s\n",
18599                      all_cores[arm_default_cpu].name);
18600
18601       if (TARGET_SOFT_FLOAT)
18602         {
18603           if (TARGET_VFP)
18604             fpu_name = "softvfp";
18605           else
18606             fpu_name = "softfpa";
18607         }
18608       else
18609         {
18610           int set_float_abi_attributes = 0;
18611           switch (arm_fpu_arch)
18612             {
18613             case FPUTYPE_FPA:
18614               fpu_name = "fpa";
18615               break;
18616             case FPUTYPE_FPA_EMU2:
18617               fpu_name = "fpe2";
18618               break;
18619             case FPUTYPE_FPA_EMU3:
18620               fpu_name = "fpe3";
18621               break;
18622             case FPUTYPE_MAVERICK:
18623               fpu_name = "maverick";
18624               break;
18625             case FPUTYPE_VFP:
18626               fpu_name = "vfp";
18627               set_float_abi_attributes = 1;
18628               break;
18629             case FPUTYPE_VFP3D16:
18630               fpu_name = "vfpv3-d16";
18631               set_float_abi_attributes = 1;
18632               break;
18633             case FPUTYPE_VFP3:
18634               fpu_name = "vfpv3";
18635               set_float_abi_attributes = 1;
18636               break;
18637             case FPUTYPE_NEON:
18638               fpu_name = "neon";
18639               set_float_abi_attributes = 1;
18640               break;
18641             case FPUTYPE_NEON_FP16:
18642               fpu_name = "neon-fp16";
18643               set_float_abi_attributes = 1;
18644               break;
18645             default:
18646               abort();
18647             }
18648           if (set_float_abi_attributes)
18649             {
18650               if (TARGET_HARD_FLOAT)
18651                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
18652               if (TARGET_HARD_FLOAT_ABI)
18653                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
18654             }
18655         }
18656       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
18657
18658       /* Some of these attributes only apply when the corresponding features
18659          are used.  However we don't have any easy way of figuring this out.
18660          Conservatively record the setting that would have been used.  */
18661
18662       /* Tag_ABI_FP_rounding.  */
18663       if (flag_rounding_math)
18664         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
18665       if (!flag_unsafe_math_optimizations)
18666         {
18667           /* Tag_ABI_FP_denomal.  */
18668           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
18669           /* Tag_ABI_FP_exceptions.  */
18670           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
18671         }
18672       /* Tag_ABI_FP_user_exceptions.  */
18673       if (flag_signaling_nans)
18674         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
18675       /* Tag_ABI_FP_number_model.  */
18676       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
18677                    flag_finite_math_only ? 1 : 3);
18678
18679       /* Tag_ABI_align8_needed.  */
18680       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
18681       /* Tag_ABI_align8_preserved.  */
18682       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
18683       /* Tag_ABI_enum_size.  */
18684       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
18685                    flag_short_enums ? 1 : 2);
18686
18687       /* Tag_ABI_optimization_goals.  */
18688       if (optimize_size)
18689         val = 4;
18690       else if (optimize >= 2)
18691         val = 2;
18692       else if (optimize)
18693         val = 1;
18694       else
18695         val = 6;
18696       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
18697
18698       /* Tag_ABI_FP_16bit_format.  */
18699       if (arm_fp16_format)
18700         asm_fprintf (asm_out_file, "\t.eabi_attribute 38, %d\n",
18701                      (int)arm_fp16_format);
18702
18703       if (arm_lang_output_object_attributes_hook)
18704         arm_lang_output_object_attributes_hook();
18705     }
18706   default_file_start();
18707 }
18708
18709 static void
18710 arm_file_end (void)
18711 {
18712   int regno;
18713
18714   if (NEED_INDICATE_EXEC_STACK)
18715     /* Add .note.GNU-stack.  */
18716     file_end_indicate_exec_stack ();
18717
18718   if (! thumb_call_reg_needed)
18719     return;
18720
18721   switch_to_section (text_section);
18722   asm_fprintf (asm_out_file, "\t.code 16\n");
18723   ASM_OUTPUT_ALIGN (asm_out_file, 1);
18724
18725   for (regno = 0; regno < LR_REGNUM; regno++)
18726     {
18727       rtx label = thumb_call_via_label[regno];
18728
18729       if (label != 0)
18730         {
18731           targetm.asm_out.internal_label (asm_out_file, "L",
18732                                           CODE_LABEL_NUMBER (label));
18733           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
18734         }
18735     }
18736 }
18737
18738 #ifndef ARM_PE
18739 /* Symbols in the text segment can be accessed without indirecting via the
18740    constant pool; it may take an extra binary operation, but this is still
18741    faster than indirecting via memory.  Don't do this when not optimizing,
18742    since we won't be calculating al of the offsets necessary to do this
18743    simplification.  */
18744
18745 static void
18746 arm_encode_section_info (tree decl, rtx rtl, int first)
18747 {
18748   if (optimize > 0 && TREE_CONSTANT (decl))
18749     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
18750
18751   default_encode_section_info (decl, rtl, first);
18752 }
18753 #endif /* !ARM_PE */
18754
18755 static void
18756 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
18757 {
18758   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
18759       && !strcmp (prefix, "L"))
18760     {
18761       arm_ccfsm_state = 0;
18762       arm_target_insn = NULL;
18763     }
18764   default_internal_label (stream, prefix, labelno);
18765 }
18766
18767 /* Output code to add DELTA to the first argument, and then jump
18768    to FUNCTION.  Used for C++ multiple inheritance.  */
18769 static void
18770 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
18771                      HOST_WIDE_INT delta,
18772                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
18773                      tree function)
18774 {
18775   static int thunk_label = 0;
18776   char label[256];
18777   char labelpc[256];
18778   int mi_delta = delta;
18779   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
18780   int shift = 0;
18781   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
18782                     ? 1 : 0);
18783   if (mi_delta < 0)
18784     mi_delta = - mi_delta;
18785
18786   if (TARGET_THUMB1)
18787     {
18788       int labelno = thunk_label++;
18789       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
18790       /* Thunks are entered in arm mode when avaiable.  */
18791       if (TARGET_THUMB1_ONLY)
18792         {
18793           /* push r3 so we can use it as a temporary.  */
18794           /* TODO: Omit this save if r3 is not used.  */
18795           fputs ("\tpush {r3}\n", file);
18796           fputs ("\tldr\tr3, ", file);
18797         }
18798       else
18799         {
18800           fputs ("\tldr\tr12, ", file);
18801         }
18802       assemble_name (file, label);
18803       fputc ('\n', file);
18804       if (flag_pic)
18805         {
18806           /* If we are generating PIC, the ldr instruction below loads
18807              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
18808              the address of the add + 8, so we have:
18809
18810              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
18811                  = target + 1.
18812
18813              Note that we have "+ 1" because some versions of GNU ld
18814              don't set the low bit of the result for R_ARM_REL32
18815              relocations against thumb function symbols.
18816              On ARMv6M this is +4, not +8.  */
18817           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
18818           assemble_name (file, labelpc);
18819           fputs (":\n", file);
18820           if (TARGET_THUMB1_ONLY)
18821             {
18822               /* This is 2 insns after the start of the thunk, so we know it
18823                  is 4-byte aligned.  */
18824               fputs ("\tadd\tr3, pc, r3\n", file);
18825               fputs ("\tmov r12, r3\n", file);
18826             }
18827           else
18828             fputs ("\tadd\tr12, pc, r12\n", file);
18829         }
18830       else if (TARGET_THUMB1_ONLY)
18831         fputs ("\tmov r12, r3\n", file);
18832     }
18833   if (TARGET_THUMB1_ONLY)
18834     {
18835       if (mi_delta > 255)
18836         {
18837           fputs ("\tldr\tr3, ", file);
18838           assemble_name (file, label);
18839           fputs ("+4\n", file);
18840           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
18841                        mi_op, this_regno, this_regno);
18842         }
18843       else if (mi_delta != 0)
18844         {
18845           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18846                        mi_op, this_regno, this_regno,
18847                        mi_delta);
18848         }
18849     }
18850   else
18851     {
18852       /* TODO: Use movw/movt for large constants when available.  */
18853       while (mi_delta != 0)
18854         {
18855           if ((mi_delta & (3 << shift)) == 0)
18856             shift += 2;
18857           else
18858             {
18859               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18860                            mi_op, this_regno, this_regno,
18861                            mi_delta & (0xff << shift));
18862               mi_delta &= ~(0xff << shift);
18863               shift += 8;
18864             }
18865         }
18866     }
18867   if (TARGET_THUMB1)
18868     {
18869       if (TARGET_THUMB1_ONLY)
18870         fputs ("\tpop\t{r3}\n", file);
18871
18872       fprintf (file, "\tbx\tr12\n");
18873       ASM_OUTPUT_ALIGN (file, 2);
18874       assemble_name (file, label);
18875       fputs (":\n", file);
18876       if (flag_pic)
18877         {
18878           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
18879           rtx tem = XEXP (DECL_RTL (function), 0);
18880           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
18881           tem = gen_rtx_MINUS (GET_MODE (tem),
18882                                tem,
18883                                gen_rtx_SYMBOL_REF (Pmode,
18884                                                    ggc_strdup (labelpc)));
18885           assemble_integer (tem, 4, BITS_PER_WORD, 1);
18886         }
18887       else
18888         /* Output ".word .LTHUNKn".  */
18889         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
18890
18891       if (TARGET_THUMB1_ONLY && mi_delta > 255)
18892         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
18893     }
18894   else
18895     {
18896       fputs ("\tb\t", file);
18897       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
18898       if (NEED_PLT_RELOC)
18899         fputs ("(PLT)", file);
18900       fputc ('\n', file);
18901     }
18902 }
18903
18904 int
18905 arm_emit_vector_const (FILE *file, rtx x)
18906 {
18907   int i;
18908   const char * pattern;
18909
18910   gcc_assert (GET_CODE (x) == CONST_VECTOR);
18911
18912   switch (GET_MODE (x))
18913     {
18914     case V2SImode: pattern = "%08x"; break;
18915     case V4HImode: pattern = "%04x"; break;
18916     case V8QImode: pattern = "%02x"; break;
18917     default:       gcc_unreachable ();
18918     }
18919
18920   fprintf (file, "0x");
18921   for (i = CONST_VECTOR_NUNITS (x); i--;)
18922     {
18923       rtx element;
18924
18925       element = CONST_VECTOR_ELT (x, i);
18926       fprintf (file, pattern, INTVAL (element));
18927     }
18928
18929   return 1;
18930 }
18931
18932 /* Emit a fp16 constant appropriately padded to occupy a 4-byte word.
18933    HFmode constant pool entries are actually loaded with ldr.  */
18934 void
18935 arm_emit_fp16_const (rtx c)
18936 {
18937   REAL_VALUE_TYPE r;
18938   long bits;
18939
18940   REAL_VALUE_FROM_CONST_DOUBLE (r, c);
18941   bits = real_to_target (NULL, &r, HFmode);
18942   if (WORDS_BIG_ENDIAN)
18943     assemble_zeros (2);
18944   assemble_integer (GEN_INT (bits), 2, BITS_PER_WORD, 1);
18945   if (!WORDS_BIG_ENDIAN)
18946     assemble_zeros (2);
18947 }
18948
18949 const char *
18950 arm_output_load_gr (rtx *operands)
18951 {
18952   rtx reg;
18953   rtx offset;
18954   rtx wcgr;
18955   rtx sum;
18956
18957   if (GET_CODE (operands [1]) != MEM
18958       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
18959       || GET_CODE (reg = XEXP (sum, 0)) != REG
18960       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
18961       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
18962     return "wldrw%?\t%0, %1";
18963
18964   /* Fix up an out-of-range load of a GR register.  */
18965   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
18966   wcgr = operands[0];
18967   operands[0] = reg;
18968   output_asm_insn ("ldr%?\t%0, %1", operands);
18969
18970   operands[0] = wcgr;
18971   operands[1] = reg;
18972   output_asm_insn ("tmcr%?\t%0, %1", operands);
18973   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
18974
18975   return "";
18976 }
18977
18978 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
18979
18980    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
18981    named arg and all anonymous args onto the stack.
18982    XXX I know the prologue shouldn't be pushing registers, but it is faster
18983    that way.  */
18984
18985 static void
18986 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
18987                             enum machine_mode mode,
18988                             tree type,
18989                             int *pretend_size,
18990                             int second_time ATTRIBUTE_UNUSED)
18991 {
18992   int nregs = cum->nregs;
18993   if (nregs & 1
18994       && ARM_DOUBLEWORD_ALIGN
18995       && arm_needs_doubleword_align (mode, type))
18996     nregs++;
18997
18998   cfun->machine->uses_anonymous_args = 1;
18999   if (nregs < NUM_ARG_REGS)
19000     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
19001 }
19002
19003 /* Return nonzero if the CONSUMER instruction (a store) does not need
19004    PRODUCER's value to calculate the address.  */
19005
19006 int
19007 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
19008 {
19009   rtx value = PATTERN (producer);
19010   rtx addr = PATTERN (consumer);
19011
19012   if (GET_CODE (value) == COND_EXEC)
19013     value = COND_EXEC_CODE (value);
19014   if (GET_CODE (value) == PARALLEL)
19015     value = XVECEXP (value, 0, 0);
19016   value = XEXP (value, 0);
19017   if (GET_CODE (addr) == COND_EXEC)
19018     addr = COND_EXEC_CODE (addr);
19019   if (GET_CODE (addr) == PARALLEL)
19020     addr = XVECEXP (addr, 0, 0);
19021   addr = XEXP (addr, 0);
19022
19023   return !reg_overlap_mentioned_p (value, addr);
19024 }
19025
19026 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
19027    have an early register shift value or amount dependency on the
19028    result of PRODUCER.  */
19029
19030 int
19031 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
19032 {
19033   rtx value = PATTERN (producer);
19034   rtx op = PATTERN (consumer);
19035   rtx early_op;
19036
19037   if (GET_CODE (value) == COND_EXEC)
19038     value = COND_EXEC_CODE (value);
19039   if (GET_CODE (value) == PARALLEL)
19040     value = XVECEXP (value, 0, 0);
19041   value = XEXP (value, 0);
19042   if (GET_CODE (op) == COND_EXEC)
19043     op = COND_EXEC_CODE (op);
19044   if (GET_CODE (op) == PARALLEL)
19045     op = XVECEXP (op, 0, 0);
19046   op = XEXP (op, 1);
19047
19048   early_op = XEXP (op, 0);
19049   /* This is either an actual independent shift, or a shift applied to
19050      the first operand of another operation.  We want the whole shift
19051      operation.  */
19052   if (GET_CODE (early_op) == REG)
19053     early_op = op;
19054
19055   return !reg_overlap_mentioned_p (value, early_op);
19056 }
19057
19058 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
19059    have an early register shift value dependency on the result of
19060    PRODUCER.  */
19061
19062 int
19063 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
19064 {
19065   rtx value = PATTERN (producer);
19066   rtx op = PATTERN (consumer);
19067   rtx early_op;
19068
19069   if (GET_CODE (value) == COND_EXEC)
19070     value = COND_EXEC_CODE (value);
19071   if (GET_CODE (value) == PARALLEL)
19072     value = XVECEXP (value, 0, 0);
19073   value = XEXP (value, 0);
19074   if (GET_CODE (op) == COND_EXEC)
19075     op = COND_EXEC_CODE (op);
19076   if (GET_CODE (op) == PARALLEL)
19077     op = XVECEXP (op, 0, 0);
19078   op = XEXP (op, 1);
19079
19080   early_op = XEXP (op, 0);
19081
19082   /* This is either an actual independent shift, or a shift applied to
19083      the first operand of another operation.  We want the value being
19084      shifted, in either case.  */
19085   if (GET_CODE (early_op) != REG)
19086     early_op = XEXP (early_op, 0);
19087
19088   return !reg_overlap_mentioned_p (value, early_op);
19089 }
19090
19091 /* Return nonzero if the CONSUMER (a mul or mac op) does not
19092    have an early register mult dependency on the result of
19093    PRODUCER.  */
19094
19095 int
19096 arm_no_early_mul_dep (rtx producer, rtx consumer)
19097 {
19098   rtx value = PATTERN (producer);
19099   rtx op = PATTERN (consumer);
19100
19101   if (GET_CODE (value) == COND_EXEC)
19102     value = COND_EXEC_CODE (value);
19103   if (GET_CODE (value) == PARALLEL)
19104     value = XVECEXP (value, 0, 0);
19105   value = XEXP (value, 0);
19106   if (GET_CODE (op) == COND_EXEC)
19107     op = COND_EXEC_CODE (op);
19108   if (GET_CODE (op) == PARALLEL)
19109     op = XVECEXP (op, 0, 0);
19110   op = XEXP (op, 1);
19111
19112   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
19113     {
19114       if (GET_CODE (XEXP (op, 0)) == MULT)
19115         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
19116       else
19117         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
19118     }
19119
19120   return 0;
19121 }
19122
19123 /* We can't rely on the caller doing the proper promotion when
19124    using APCS or ATPCS.  */
19125
19126 static bool
19127 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
19128 {
19129     return !TARGET_AAPCS_BASED;
19130 }
19131
19132
19133 /* AAPCS based ABIs use short enums by default.  */
19134
19135 static bool
19136 arm_default_short_enums (void)
19137 {
19138   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
19139 }
19140
19141
19142 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
19143
19144 static bool
19145 arm_align_anon_bitfield (void)
19146 {
19147   return TARGET_AAPCS_BASED;
19148 }
19149
19150
19151 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
19152
19153 static tree
19154 arm_cxx_guard_type (void)
19155 {
19156   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
19157 }
19158
19159 /* Return non-zero if the consumer (a multiply-accumulate instruction)
19160    has an accumulator dependency on the result of the producer (a
19161    multiplication instruction) and no other dependency on that result.  */
19162 int
19163 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
19164 {
19165   rtx mul = PATTERN (producer);
19166   rtx mac = PATTERN (consumer);
19167   rtx mul_result;
19168   rtx mac_op0, mac_op1, mac_acc;
19169
19170   if (GET_CODE (mul) == COND_EXEC)
19171     mul = COND_EXEC_CODE (mul);
19172   if (GET_CODE (mac) == COND_EXEC)
19173     mac = COND_EXEC_CODE (mac);
19174
19175   /* Check that mul is of the form (set (...) (mult ...))
19176      and mla is of the form (set (...) (plus (mult ...) (...))).  */
19177   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
19178       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
19179           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
19180     return 0;
19181
19182   mul_result = XEXP (mul, 0);
19183   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
19184   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
19185   mac_acc = XEXP (XEXP (mac, 1), 1);
19186
19187   return (reg_overlap_mentioned_p (mul_result, mac_acc)
19188           && !reg_overlap_mentioned_p (mul_result, mac_op0)
19189           && !reg_overlap_mentioned_p (mul_result, mac_op1));
19190 }
19191
19192
19193 /* The EABI says test the least significant bit of a guard variable.  */
19194
19195 static bool
19196 arm_cxx_guard_mask_bit (void)
19197 {
19198   return TARGET_AAPCS_BASED;
19199 }
19200
19201
19202 /* The EABI specifies that all array cookies are 8 bytes long.  */
19203
19204 static tree
19205 arm_get_cookie_size (tree type)
19206 {
19207   tree size;
19208
19209   if (!TARGET_AAPCS_BASED)
19210     return default_cxx_get_cookie_size (type);
19211
19212   size = build_int_cst (sizetype, 8);
19213   return size;
19214 }
19215
19216
19217 /* The EABI says that array cookies should also contain the element size.  */
19218
19219 static bool
19220 arm_cookie_has_size (void)
19221 {
19222   return TARGET_AAPCS_BASED;
19223 }
19224
19225
19226 /* The EABI says constructors and destructors should return a pointer to
19227    the object constructed/destroyed.  */
19228
19229 static bool
19230 arm_cxx_cdtor_returns_this (void)
19231 {
19232   return TARGET_AAPCS_BASED;
19233 }
19234
19235 /* The EABI says that an inline function may never be the key
19236    method.  */
19237
19238 static bool
19239 arm_cxx_key_method_may_be_inline (void)
19240 {
19241   return !TARGET_AAPCS_BASED;
19242 }
19243
19244 static void
19245 arm_cxx_determine_class_data_visibility (tree decl)
19246 {
19247   if (!TARGET_AAPCS_BASED
19248       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
19249     return;
19250
19251   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
19252      is exported.  However, on systems without dynamic vague linkage,
19253      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
19254   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
19255     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
19256   else
19257     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
19258   DECL_VISIBILITY_SPECIFIED (decl) = 1;
19259 }
19260
19261 static bool
19262 arm_cxx_class_data_always_comdat (void)
19263 {
19264   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
19265      vague linkage if the class has no key function.  */
19266   return !TARGET_AAPCS_BASED;
19267 }
19268
19269
19270 /* The EABI says __aeabi_atexit should be used to register static
19271    destructors.  */
19272
19273 static bool
19274 arm_cxx_use_aeabi_atexit (void)
19275 {
19276   return TARGET_AAPCS_BASED;
19277 }
19278
19279
19280 void
19281 arm_set_return_address (rtx source, rtx scratch)
19282 {
19283   arm_stack_offsets *offsets;
19284   HOST_WIDE_INT delta;
19285   rtx addr;
19286   unsigned long saved_regs;
19287
19288   offsets = arm_get_frame_offsets ();
19289   saved_regs = offsets->saved_regs_mask;
19290
19291   if ((saved_regs & (1 << LR_REGNUM)) == 0)
19292     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
19293   else
19294     {
19295       if (frame_pointer_needed)
19296         addr = plus_constant(hard_frame_pointer_rtx, -4);
19297       else
19298         {
19299           /* LR will be the first saved register.  */
19300           delta = offsets->outgoing_args - (offsets->frame + 4);
19301
19302
19303           if (delta >= 4096)
19304             {
19305               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
19306                                      GEN_INT (delta & ~4095)));
19307               addr = scratch;
19308               delta &= 4095;
19309             }
19310           else
19311             addr = stack_pointer_rtx;
19312
19313           addr = plus_constant (addr, delta);
19314         }
19315       emit_move_insn (gen_frame_mem (Pmode, addr), source);
19316     }
19317 }
19318
19319
19320 void
19321 thumb_set_return_address (rtx source, rtx scratch)
19322 {
19323   arm_stack_offsets *offsets;
19324   HOST_WIDE_INT delta;
19325   HOST_WIDE_INT limit;
19326   int reg;
19327   rtx addr;
19328   unsigned long mask;
19329
19330   emit_use (source);
19331
19332   offsets = arm_get_frame_offsets ();
19333   mask = offsets->saved_regs_mask;
19334   if (mask & (1 << LR_REGNUM))
19335     {
19336       limit = 1024;
19337       /* Find the saved regs.  */
19338       if (frame_pointer_needed)
19339         {
19340           delta = offsets->soft_frame - offsets->saved_args;
19341           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
19342           if (TARGET_THUMB1)
19343             limit = 128;
19344         }
19345       else
19346         {
19347           delta = offsets->outgoing_args - offsets->saved_args;
19348           reg = SP_REGNUM;
19349         }
19350       /* Allow for the stack frame.  */
19351       if (TARGET_THUMB1 && TARGET_BACKTRACE)
19352         delta -= 16;
19353       /* The link register is always the first saved register.  */
19354       delta -= 4;
19355
19356       /* Construct the address.  */
19357       addr = gen_rtx_REG (SImode, reg);
19358       if (delta > limit)
19359         {
19360           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
19361           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
19362           addr = scratch;
19363         }
19364       else
19365         addr = plus_constant (addr, delta);
19366
19367       emit_move_insn (gen_frame_mem (Pmode, addr), source);
19368     }
19369   else
19370     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
19371 }
19372
19373 /* Implements target hook vector_mode_supported_p.  */
19374 bool
19375 arm_vector_mode_supported_p (enum machine_mode mode)
19376 {
19377   /* Neon also supports V2SImode, etc. listed in the clause below.  */
19378   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
19379       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
19380     return true;
19381
19382   if ((mode == V2SImode)
19383       || (mode == V4HImode)
19384       || (mode == V8QImode))
19385     return true;
19386
19387   return false;
19388 }
19389
19390 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
19391    ARM insns and therefore guarantee that the shift count is modulo 256.
19392    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
19393    guarantee no particular behavior for out-of-range counts.  */
19394
19395 static unsigned HOST_WIDE_INT
19396 arm_shift_truncation_mask (enum machine_mode mode)
19397 {
19398   return mode == SImode ? 255 : 0;
19399 }
19400
19401
19402 /* Map internal gcc register numbers to DWARF2 register numbers.  */
19403
19404 unsigned int
19405 arm_dbx_register_number (unsigned int regno)
19406 {
19407   if (regno < 16)
19408     return regno;
19409
19410   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
19411      compatibility.  The EABI defines them as registers 96-103.  */
19412   if (IS_FPA_REGNUM (regno))
19413     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
19414
19415   /* FIXME: VFPv3 register numbering.  */
19416   if (IS_VFP_REGNUM (regno))
19417     return 64 + regno - FIRST_VFP_REGNUM;
19418
19419   if (IS_IWMMXT_GR_REGNUM (regno))
19420     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
19421
19422   if (IS_IWMMXT_REGNUM (regno))
19423     return 112 + regno - FIRST_IWMMXT_REGNUM;
19424
19425   gcc_unreachable ();
19426 }
19427
19428
19429 #ifdef TARGET_UNWIND_INFO
19430 /* Emit unwind directives for a store-multiple instruction or stack pointer
19431    push during alignment.
19432    These should only ever be generated by the function prologue code, so
19433    expect them to have a particular form.  */
19434
19435 static void
19436 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
19437 {
19438   int i;
19439   HOST_WIDE_INT offset;
19440   HOST_WIDE_INT nregs;
19441   int reg_size;
19442   unsigned reg;
19443   unsigned lastreg;
19444   rtx e;
19445
19446   e = XVECEXP (p, 0, 0);
19447   if (GET_CODE (e) != SET)
19448     abort ();
19449
19450   /* First insn will adjust the stack pointer.  */
19451   if (GET_CODE (e) != SET
19452       || GET_CODE (XEXP (e, 0)) != REG
19453       || REGNO (XEXP (e, 0)) != SP_REGNUM
19454       || GET_CODE (XEXP (e, 1)) != PLUS)
19455     abort ();
19456
19457   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
19458   nregs = XVECLEN (p, 0) - 1;
19459
19460   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
19461   if (reg < 16)
19462     {
19463       /* The function prologue may also push pc, but not annotate it as it is
19464          never restored.  We turn this into a stack pointer adjustment.  */
19465       if (nregs * 4 == offset - 4)
19466         {
19467           fprintf (asm_out_file, "\t.pad #4\n");
19468           offset -= 4;
19469         }
19470       reg_size = 4;
19471       fprintf (asm_out_file, "\t.save {");
19472     }
19473   else if (IS_VFP_REGNUM (reg))
19474     {
19475       reg_size = 8;
19476       fprintf (asm_out_file, "\t.vsave {");
19477     }
19478   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
19479     {
19480       /* FPA registers are done differently.  */
19481       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
19482       return;
19483     }
19484   else
19485     /* Unknown register type.  */
19486     abort ();
19487
19488   /* If the stack increment doesn't match the size of the saved registers,
19489      something has gone horribly wrong.  */
19490   if (offset != nregs * reg_size)
19491     abort ();
19492
19493   offset = 0;
19494   lastreg = 0;
19495   /* The remaining insns will describe the stores.  */
19496   for (i = 1; i <= nregs; i++)
19497     {
19498       /* Expect (set (mem <addr>) (reg)).
19499          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
19500       e = XVECEXP (p, 0, i);
19501       if (GET_CODE (e) != SET
19502           || GET_CODE (XEXP (e, 0)) != MEM
19503           || GET_CODE (XEXP (e, 1)) != REG)
19504         abort ();
19505
19506       reg = REGNO (XEXP (e, 1));
19507       if (reg < lastreg)
19508         abort ();
19509
19510       if (i != 1)
19511         fprintf (asm_out_file, ", ");
19512       /* We can't use %r for vfp because we need to use the
19513          double precision register names.  */
19514       if (IS_VFP_REGNUM (reg))
19515         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
19516       else
19517         asm_fprintf (asm_out_file, "%r", reg);
19518
19519 #ifdef ENABLE_CHECKING
19520       /* Check that the addresses are consecutive.  */
19521       e = XEXP (XEXP (e, 0), 0);
19522       if (GET_CODE (e) == PLUS)
19523         {
19524           offset += reg_size;
19525           if (GET_CODE (XEXP (e, 0)) != REG
19526               || REGNO (XEXP (e, 0)) != SP_REGNUM
19527               || GET_CODE (XEXP (e, 1)) != CONST_INT
19528               || offset != INTVAL (XEXP (e, 1)))
19529             abort ();
19530         }
19531       else if (i != 1
19532                || GET_CODE (e) != REG
19533                || REGNO (e) != SP_REGNUM)
19534         abort ();
19535 #endif
19536     }
19537   fprintf (asm_out_file, "}\n");
19538 }
19539
19540 /*  Emit unwind directives for a SET.  */
19541
19542 static void
19543 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
19544 {
19545   rtx e0;
19546   rtx e1;
19547   unsigned reg;
19548
19549   e0 = XEXP (p, 0);
19550   e1 = XEXP (p, 1);
19551   switch (GET_CODE (e0))
19552     {
19553     case MEM:
19554       /* Pushing a single register.  */
19555       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
19556           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
19557           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
19558         abort ();
19559
19560       asm_fprintf (asm_out_file, "\t.save ");
19561       if (IS_VFP_REGNUM (REGNO (e1)))
19562         asm_fprintf(asm_out_file, "{d%d}\n",
19563                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
19564       else
19565         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
19566       break;
19567
19568     case REG:
19569       if (REGNO (e0) == SP_REGNUM)
19570         {
19571           /* A stack increment.  */
19572           if (GET_CODE (e1) != PLUS
19573               || GET_CODE (XEXP (e1, 0)) != REG
19574               || REGNO (XEXP (e1, 0)) != SP_REGNUM
19575               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
19576             abort ();
19577
19578           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
19579                        -INTVAL (XEXP (e1, 1)));
19580         }
19581       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
19582         {
19583           HOST_WIDE_INT offset;
19584
19585           if (GET_CODE (e1) == PLUS)
19586             {
19587               if (GET_CODE (XEXP (e1, 0)) != REG
19588                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
19589                 abort ();
19590               reg = REGNO (XEXP (e1, 0));
19591               offset = INTVAL (XEXP (e1, 1));
19592               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
19593                            HARD_FRAME_POINTER_REGNUM, reg,
19594                            INTVAL (XEXP (e1, 1)));
19595             }
19596           else if (GET_CODE (e1) == REG)
19597             {
19598               reg = REGNO (e1);
19599               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
19600                            HARD_FRAME_POINTER_REGNUM, reg);
19601             }
19602           else
19603             abort ();
19604         }
19605       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
19606         {
19607           /* Move from sp to reg.  */
19608           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
19609         }
19610      else if (GET_CODE (e1) == PLUS
19611               && GET_CODE (XEXP (e1, 0)) == REG
19612               && REGNO (XEXP (e1, 0)) == SP_REGNUM
19613               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
19614         {
19615           /* Set reg to offset from sp.  */
19616           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
19617                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
19618         }
19619       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
19620         {
19621           /* Stack pointer save before alignment.  */
19622           reg = REGNO (e0);
19623           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
19624                        reg + 0x90, reg);
19625         }
19626       else
19627         abort ();
19628       break;
19629
19630     default:
19631       abort ();
19632     }
19633 }
19634
19635
19636 /* Emit unwind directives for the given insn.  */
19637
19638 static void
19639 arm_unwind_emit (FILE * asm_out_file, rtx insn)
19640 {
19641   rtx pat;
19642
19643   if (!ARM_EABI_UNWIND_TABLES)
19644     return;
19645
19646   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
19647       && (TREE_NOTHROW (current_function_decl)
19648           || crtl->all_throwers_are_sibcalls))
19649     return;
19650
19651   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
19652     return;
19653
19654   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
19655   if (pat)
19656     pat = XEXP (pat, 0);
19657   else
19658     pat = PATTERN (insn);
19659
19660   switch (GET_CODE (pat))
19661     {
19662     case SET:
19663       arm_unwind_emit_set (asm_out_file, pat);
19664       break;
19665
19666     case SEQUENCE:
19667       /* Store multiple.  */
19668       arm_unwind_emit_sequence (asm_out_file, pat);
19669       break;
19670
19671     default:
19672       abort();
19673     }
19674 }
19675
19676
19677 /* Output a reference from a function exception table to the type_info
19678    object X.  The EABI specifies that the symbol should be relocated by
19679    an R_ARM_TARGET2 relocation.  */
19680
19681 static bool
19682 arm_output_ttype (rtx x)
19683 {
19684   fputs ("\t.word\t", asm_out_file);
19685   output_addr_const (asm_out_file, x);
19686   /* Use special relocations for symbol references.  */
19687   if (GET_CODE (x) != CONST_INT)
19688     fputs ("(TARGET2)", asm_out_file);
19689   fputc ('\n', asm_out_file);
19690
19691   return TRUE;
19692 }
19693 #endif /* TARGET_UNWIND_INFO */
19694
19695
19696 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
19697    stack alignment.  */
19698
19699 static void
19700 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
19701 {
19702   rtx unspec = SET_SRC (pattern);
19703   gcc_assert (GET_CODE (unspec) == UNSPEC);
19704
19705   switch (index)
19706     {
19707     case UNSPEC_STACK_ALIGN:
19708       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
19709          put anything on the stack, so hopefully it won't matter.
19710          CFA = SP will be correct after alignment.  */
19711       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
19712                               SET_DEST (pattern));
19713       break;
19714     default:
19715       gcc_unreachable ();
19716     }
19717 }
19718
19719
19720 /* Output unwind directives for the start/end of a function.  */
19721
19722 void
19723 arm_output_fn_unwind (FILE * f, bool prologue)
19724 {
19725   if (!ARM_EABI_UNWIND_TABLES)
19726     return;
19727
19728   if (prologue)
19729     fputs ("\t.fnstart\n", f);
19730   else
19731     {
19732       /* If this function will never be unwound, then mark it as such.
19733          The came condition is used in arm_unwind_emit to suppress
19734          the frame annotations.  */
19735       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
19736           && (TREE_NOTHROW (current_function_decl)
19737               || crtl->all_throwers_are_sibcalls))
19738         fputs("\t.cantunwind\n", f);
19739
19740       fputs ("\t.fnend\n", f);
19741     }
19742 }
19743
19744 static bool
19745 arm_emit_tls_decoration (FILE *fp, rtx x)
19746 {
19747   enum tls_reloc reloc;
19748   rtx val;
19749
19750   val = XVECEXP (x, 0, 0);
19751   reloc = (enum tls_reloc) INTVAL (XVECEXP (x, 0, 1));
19752
19753   output_addr_const (fp, val);
19754
19755   switch (reloc)
19756     {
19757     case TLS_GD32:
19758       fputs ("(tlsgd)", fp);
19759       break;
19760     case TLS_LDM32:
19761       fputs ("(tlsldm)", fp);
19762       break;
19763     case TLS_LDO32:
19764       fputs ("(tlsldo)", fp);
19765       break;
19766     case TLS_IE32:
19767       fputs ("(gottpoff)", fp);
19768       break;
19769     case TLS_LE32:
19770       fputs ("(tpoff)", fp);
19771       break;
19772     default:
19773       gcc_unreachable ();
19774     }
19775
19776   switch (reloc)
19777     {
19778     case TLS_GD32:
19779     case TLS_LDM32:
19780     case TLS_IE32:
19781       fputs (" + (. - ", fp);
19782       output_addr_const (fp, XVECEXP (x, 0, 2));
19783       fputs (" - ", fp);
19784       output_addr_const (fp, XVECEXP (x, 0, 3));
19785       fputc (')', fp);
19786       break;
19787     default:
19788       break;
19789     }
19790
19791   return TRUE;
19792 }
19793
19794 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
19795
19796 static void
19797 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
19798 {
19799   gcc_assert (size == 4);
19800   fputs ("\t.word\t", file);
19801   output_addr_const (file, x);
19802   fputs ("(tlsldo)", file);
19803 }
19804
19805 bool
19806 arm_output_addr_const_extra (FILE *fp, rtx x)
19807 {
19808   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
19809     return arm_emit_tls_decoration (fp, x);
19810   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
19811     {
19812       char label[256];
19813       int labelno = INTVAL (XVECEXP (x, 0, 0));
19814
19815       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
19816       assemble_name_raw (fp, label);
19817
19818       return TRUE;
19819     }
19820   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
19821     {
19822       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
19823       if (GOT_PCREL)
19824         fputs ("+.", fp);
19825       fputs ("-(", fp);
19826       output_addr_const (fp, XVECEXP (x, 0, 0));
19827       fputc (')', fp);
19828       return TRUE;
19829     }
19830   else if (GET_CODE (x) == CONST_VECTOR)
19831     return arm_emit_vector_const (fp, x);
19832
19833   return FALSE;
19834 }
19835
19836 /* Output assembly for a shift instruction.
19837    SET_FLAGS determines how the instruction modifies the condition codes.
19838    0 - Do not set condition codes.
19839    1 - Set condition codes.
19840    2 - Use smallest instruction.  */
19841 const char *
19842 arm_output_shift(rtx * operands, int set_flags)
19843 {
19844   char pattern[100];
19845   static const char flag_chars[3] = {'?', '.', '!'};
19846   const char *shift;
19847   HOST_WIDE_INT val;
19848   char c;
19849   
19850   c = flag_chars[set_flags];
19851   if (TARGET_UNIFIED_ASM)
19852     {
19853       shift = shift_op(operands[3], &val);
19854       if (shift)
19855         {
19856           if (val != -1)
19857             operands[2] = GEN_INT(val);
19858           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
19859         }
19860       else
19861         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
19862     }
19863   else
19864     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
19865   output_asm_insn (pattern, operands);
19866   return "";
19867 }
19868
19869 /* Output a Thumb-1 casesi dispatch sequence.  */
19870 const char *
19871 thumb1_output_casesi (rtx *operands)
19872 {
19873   rtx diff_vec = PATTERN (next_real_insn (operands[0]));
19874   addr_diff_vec_flags flags;
19875
19876   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
19877
19878   flags = ADDR_DIFF_VEC_FLAGS (diff_vec);
19879
19880   switch (GET_MODE(diff_vec))
19881     {
19882     case QImode:
19883       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
19884               "bl\t%___gnu_thumb1_case_uqi" : "bl\t%___gnu_thumb1_case_sqi");
19885     case HImode:
19886       return (ADDR_DIFF_VEC_FLAGS (diff_vec).offset_unsigned ? 
19887               "bl\t%___gnu_thumb1_case_uhi" : "bl\t%___gnu_thumb1_case_shi");
19888     case SImode:
19889       return "bl\t%___gnu_thumb1_case_si";
19890     default:
19891       gcc_unreachable ();
19892     }
19893 }
19894
19895 /* Output a Thumb-2 casesi instruction.  */
19896 const char *
19897 thumb2_output_casesi (rtx *operands)
19898 {
19899   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
19900
19901   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
19902
19903   output_asm_insn ("cmp\t%0, %1", operands);
19904   output_asm_insn ("bhi\t%l3", operands);
19905   switch (GET_MODE(diff_vec))
19906     {
19907     case QImode:
19908       return "tbb\t[%|pc, %0]";
19909     case HImode:
19910       return "tbh\t[%|pc, %0, lsl #1]";
19911     case SImode:
19912       if (flag_pic)
19913         {
19914           output_asm_insn ("adr\t%4, %l2", operands);
19915           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
19916           output_asm_insn ("add\t%4, %4, %5", operands);
19917           return "bx\t%4";
19918         }
19919       else
19920         {
19921           output_asm_insn ("adr\t%4, %l2", operands);
19922           return "ldr\t%|pc, [%4, %0, lsl #2]";
19923         }
19924     default:
19925       gcc_unreachable ();
19926     }
19927 }
19928
19929 /* Most ARM cores are single issue, but some newer ones can dual issue.
19930    The scheduler descriptions rely on this being correct.  */
19931 static int
19932 arm_issue_rate (void)
19933 {
19934   switch (arm_tune)
19935     {
19936     case cortexr4:
19937     case cortexr4f:
19938     case cortexa8:
19939     case cortexa9:
19940       return 2;
19941
19942     default:
19943       return 1;
19944     }
19945 }
19946
19947 /* A table and a function to perform ARM-specific name mangling for
19948    NEON vector types in order to conform to the AAPCS (see "Procedure
19949    Call Standard for the ARM Architecture", Appendix A).  To qualify
19950    for emission with the mangled names defined in that document, a
19951    vector type must not only be of the correct mode but also be
19952    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
19953 typedef struct
19954 {
19955   enum machine_mode mode;
19956   const char *element_type_name;
19957   const char *aapcs_name;
19958 } arm_mangle_map_entry;
19959
19960 static arm_mangle_map_entry arm_mangle_map[] = {
19961   /* 64-bit containerized types.  */
19962   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
19963   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
19964   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
19965   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
19966   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
19967   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
19968   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
19969   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
19970   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
19971   /* 128-bit containerized types.  */
19972   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
19973   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
19974   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
19975   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
19976   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
19977   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
19978   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
19979   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
19980   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
19981   { VOIDmode, NULL, NULL }
19982 };
19983
19984 const char *
19985 arm_mangle_type (const_tree type)
19986 {
19987   arm_mangle_map_entry *pos = arm_mangle_map;
19988
19989   /* The ARM ABI documents (10th October 2008) say that "__va_list"
19990      has to be managled as if it is in the "std" namespace.  */
19991   if (TARGET_AAPCS_BASED 
19992       && lang_hooks.types_compatible_p (CONST_CAST_TREE (type), va_list_type))
19993     {
19994       static bool warned;
19995       if (!warned && warn_psabi)
19996         {
19997           warned = true;
19998           inform (input_location,
19999                   "the mangling of %<va_list%> has changed in GCC 4.4");
20000         }
20001       return "St9__va_list";
20002     }
20003
20004   /* Half-precision float.  */
20005   if (TREE_CODE (type) == REAL_TYPE && TYPE_PRECISION (type) == 16)
20006     return "Dh";
20007
20008   if (TREE_CODE (type) != VECTOR_TYPE)
20009     return NULL;
20010
20011   /* Check the mode of the vector type, and the name of the vector
20012      element type, against the table.  */
20013   while (pos->mode != VOIDmode)
20014     {
20015       tree elt_type = TREE_TYPE (type);
20016
20017       if (pos->mode == TYPE_MODE (type)
20018           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
20019           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
20020                       pos->element_type_name))
20021         return pos->aapcs_name;
20022
20023       pos++;
20024     }
20025
20026   /* Use the default mangling for unrecognized (possibly user-defined)
20027      vector types.  */
20028   return NULL;
20029 }
20030
20031 /* Order of allocation of core registers for Thumb: this allocation is
20032    written over the corresponding initial entries of the array
20033    initialized with REG_ALLOC_ORDER.  We allocate all low registers
20034    first.  Saving and restoring a low register is usually cheaper than
20035    using a call-clobbered high register.  */
20036
20037 static const int thumb_core_reg_alloc_order[] =
20038 {
20039    3,  2,  1,  0,  4,  5,  6,  7,
20040   14, 12,  8,  9, 10, 11, 13, 15
20041 };
20042
20043 /* Adjust register allocation order when compiling for Thumb.  */
20044
20045 void
20046 arm_order_regs_for_local_alloc (void)
20047 {
20048   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
20049   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
20050   if (TARGET_THUMB)
20051     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
20052             sizeof (thumb_core_reg_alloc_order));
20053 }
20054
20055 /* Set default optimization options.  */
20056 void
20057 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
20058 {
20059   /* Enable section anchors by default at -O1 or higher.
20060      Use 2 to distinguish from an explicit -fsection-anchors
20061      given on the command line.  */
20062   if (level > 0)
20063     flag_section_anchors = 2;
20064 }
20065
20066 #include "gt-arm.h"