OSDN Git Service

* arm.c (struct processors): Pass for speed down into cost helper
[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
57 /* Forward definitions of types.  */
58 typedef struct minipool_node    Mnode;
59 typedef struct minipool_fixup   Mfix;
60
61 const struct attribute_spec arm_attribute_table[];
62
63 void (*arm_lang_output_object_attributes_hook)(void);
64
65 /* Forward function declarations.  */
66 static int arm_compute_static_chain_stack_bytes (void);
67 static arm_stack_offsets *arm_get_frame_offsets (void);
68 static void arm_add_gc_roots (void);
69 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
70                              HOST_WIDE_INT, rtx, rtx, int, int);
71 static unsigned bit_count (unsigned long);
72 static int arm_address_register_rtx_p (rtx, int);
73 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
74 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
75 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
76 inline static int thumb1_index_register_rtx_p (rtx, int);
77 static int thumb_far_jump_used_p (void);
78 static bool thumb_force_lr_save (void);
79 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
80 static rtx emit_sfm (int, int);
81 static unsigned arm_size_return_regs (void);
82 static bool arm_assemble_integer (rtx, unsigned int, int);
83 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
84 static arm_cc get_arm_condition_code (rtx);
85 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
86 static rtx is_jump_table (rtx);
87 static const char *output_multi_immediate (rtx *, const char *, const char *,
88                                            int, HOST_WIDE_INT);
89 static const char *shift_op (rtx, HOST_WIDE_INT *);
90 static struct machine_function *arm_init_machine_status (void);
91 static void thumb_exit (FILE *, int);
92 static rtx is_jump_table (rtx);
93 static HOST_WIDE_INT get_jump_table_size (rtx);
94 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
95 static Mnode *add_minipool_forward_ref (Mfix *);
96 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
97 static Mnode *add_minipool_backward_ref (Mfix *);
98 static void assign_minipool_offsets (Mfix *);
99 static void arm_print_value (FILE *, rtx);
100 static void dump_minipool (rtx);
101 static int arm_barrier_cost (rtx);
102 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
103 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
104 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
105                                rtx);
106 static void arm_reorg (void);
107 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
108 static unsigned long arm_compute_save_reg0_reg12_mask (void);
109 static unsigned long arm_compute_save_reg_mask (void);
110 static unsigned long arm_isr_value (tree);
111 static unsigned long arm_compute_func_type (void);
112 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
113 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
114 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
115 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
116 #endif
117 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
118 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
119 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
120 static int arm_comp_type_attributes (const_tree, const_tree);
121 static void arm_set_default_type_attributes (tree);
122 static int arm_adjust_cost (rtx, rtx, rtx, int);
123 static int count_insns_for_constant (HOST_WIDE_INT, int);
124 static int arm_get_strip_length (int);
125 static bool arm_function_ok_for_sibcall (tree, tree);
126 static void arm_internal_label (FILE *, const char *, unsigned long);
127 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
128                                  tree);
129 static bool arm_rtx_costs_1 (rtx, enum rtx_code, int*, bool);
130 static bool arm_size_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *);
131 static bool arm_slowmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
132 static bool arm_fastmul_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
133 static bool arm_xscale_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
134 static bool arm_9e_rtx_costs (rtx, enum rtx_code, enum rtx_code, int *, bool);
135 static bool arm_rtx_costs (rtx, int, int, int *, bool);
136 static int arm_address_cost (rtx, bool);
137 static bool arm_memory_load_p (rtx);
138 static bool arm_cirrus_insn_p (rtx);
139 static void cirrus_reorg (rtx);
140 static void arm_init_builtins (void);
141 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
142 static void arm_init_iwmmxt_builtins (void);
143 static rtx safe_vector_operand (rtx, enum machine_mode);
144 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
145 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
146 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
147 static void emit_constant_insn (rtx cond, rtx pattern);
148 static rtx emit_set_insn (rtx, rtx);
149 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
150                                   tree, bool);
151
152 #ifdef OBJECT_FORMAT_ELF
153 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
154 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
155 #endif
156 #ifndef ARM_PE
157 static void arm_encode_section_info (tree, rtx, int);
158 #endif
159
160 static void arm_file_end (void);
161 static void arm_file_start (void);
162
163 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
164                                         tree, int *, int);
165 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
166                                    enum machine_mode, const_tree, bool);
167 static bool arm_promote_prototypes (const_tree);
168 static bool arm_default_short_enums (void);
169 static bool arm_align_anon_bitfield (void);
170 static bool arm_return_in_msb (const_tree);
171 static bool arm_must_pass_in_stack (enum machine_mode, const_tree);
172 static bool arm_return_in_memory (const_tree, const_tree);
173 #ifdef TARGET_UNWIND_INFO
174 static void arm_unwind_emit (FILE *, rtx);
175 static bool arm_output_ttype (rtx);
176 #endif
177 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
178
179 static tree arm_cxx_guard_type (void);
180 static bool arm_cxx_guard_mask_bit (void);
181 static tree arm_get_cookie_size (tree);
182 static bool arm_cookie_has_size (void);
183 static bool arm_cxx_cdtor_returns_this (void);
184 static bool arm_cxx_key_method_may_be_inline (void);
185 static void arm_cxx_determine_class_data_visibility (tree);
186 static bool arm_cxx_class_data_always_comdat (void);
187 static bool arm_cxx_use_aeabi_atexit (void);
188 static void arm_init_libfuncs (void);
189 static bool arm_handle_option (size_t, const char *, int);
190 static void arm_target_help (void);
191 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
192 static bool arm_cannot_copy_insn_p (rtx);
193 static bool arm_tls_symbol_p (rtx x);
194 static int arm_issue_rate (void);
195 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
196 static bool arm_allocate_stack_slots_for_args (void);
197
198 \f
199 /* Initialize the GCC target structure.  */
200 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
201 #undef  TARGET_MERGE_DECL_ATTRIBUTES
202 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
203 #endif
204
205 #undef  TARGET_ATTRIBUTE_TABLE
206 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
207
208 #undef TARGET_ASM_FILE_START
209 #define TARGET_ASM_FILE_START arm_file_start
210 #undef TARGET_ASM_FILE_END
211 #define TARGET_ASM_FILE_END arm_file_end
212
213 #undef  TARGET_ASM_ALIGNED_SI_OP
214 #define TARGET_ASM_ALIGNED_SI_OP NULL
215 #undef  TARGET_ASM_INTEGER
216 #define TARGET_ASM_INTEGER arm_assemble_integer
217
218 #undef  TARGET_ASM_FUNCTION_PROLOGUE
219 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
220
221 #undef  TARGET_ASM_FUNCTION_EPILOGUE
222 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
223
224 #undef  TARGET_DEFAULT_TARGET_FLAGS
225 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
226 #undef  TARGET_HANDLE_OPTION
227 #define TARGET_HANDLE_OPTION arm_handle_option
228 #undef  TARGET_HELP
229 #define TARGET_HELP arm_target_help
230
231 #undef  TARGET_COMP_TYPE_ATTRIBUTES
232 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
233
234 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
235 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
236
237 #undef  TARGET_SCHED_ADJUST_COST
238 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
239
240 #undef TARGET_ENCODE_SECTION_INFO
241 #ifdef ARM_PE
242 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
243 #else
244 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
245 #endif
246
247 #undef  TARGET_STRIP_NAME_ENCODING
248 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
249
250 #undef  TARGET_ASM_INTERNAL_LABEL
251 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
252
253 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
254 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
255
256 #undef  TARGET_ASM_OUTPUT_MI_THUNK
257 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
258 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
259 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
260
261 #undef  TARGET_RTX_COSTS
262 #define TARGET_RTX_COSTS arm_rtx_costs
263 #undef  TARGET_ADDRESS_COST
264 #define TARGET_ADDRESS_COST arm_address_cost
265
266 #undef TARGET_SHIFT_TRUNCATION_MASK
267 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
268 #undef TARGET_VECTOR_MODE_SUPPORTED_P
269 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
270
271 #undef  TARGET_MACHINE_DEPENDENT_REORG
272 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
273
274 #undef  TARGET_INIT_BUILTINS
275 #define TARGET_INIT_BUILTINS  arm_init_builtins
276 #undef  TARGET_EXPAND_BUILTIN
277 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
278
279 #undef TARGET_INIT_LIBFUNCS
280 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
281
282 #undef TARGET_PROMOTE_FUNCTION_ARGS
283 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_const_tree_true
284 #undef TARGET_PROMOTE_FUNCTION_RETURN
285 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_const_tree_true
286 #undef TARGET_PROMOTE_PROTOTYPES
287 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
288 #undef TARGET_PASS_BY_REFERENCE
289 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
290 #undef TARGET_ARG_PARTIAL_BYTES
291 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
292
293 #undef  TARGET_SETUP_INCOMING_VARARGS
294 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
295
296 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
297 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arm_allocate_stack_slots_for_args
298
299 #undef TARGET_DEFAULT_SHORT_ENUMS
300 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
301
302 #undef TARGET_ALIGN_ANON_BITFIELD
303 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
304
305 #undef TARGET_NARROW_VOLATILE_BITFIELD
306 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
307
308 #undef TARGET_CXX_GUARD_TYPE
309 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
310
311 #undef TARGET_CXX_GUARD_MASK_BIT
312 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
313
314 #undef TARGET_CXX_GET_COOKIE_SIZE
315 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
316
317 #undef TARGET_CXX_COOKIE_HAS_SIZE
318 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
319
320 #undef TARGET_CXX_CDTOR_RETURNS_THIS
321 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
322
323 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
324 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
325
326 #undef TARGET_CXX_USE_AEABI_ATEXIT
327 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
328
329 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
330 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
331   arm_cxx_determine_class_data_visibility
332
333 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
334 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
335
336 #undef TARGET_RETURN_IN_MSB
337 #define TARGET_RETURN_IN_MSB arm_return_in_msb
338
339 #undef TARGET_RETURN_IN_MEMORY
340 #define TARGET_RETURN_IN_MEMORY arm_return_in_memory
341
342 #undef TARGET_MUST_PASS_IN_STACK
343 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
344
345 #ifdef TARGET_UNWIND_INFO
346 #undef TARGET_UNWIND_EMIT
347 #define TARGET_UNWIND_EMIT arm_unwind_emit
348
349 /* EABI unwinding tables use a different format for the typeinfo tables.  */
350 #undef TARGET_ASM_TTYPE
351 #define TARGET_ASM_TTYPE arm_output_ttype
352
353 #undef TARGET_ARM_EABI_UNWINDER
354 #define TARGET_ARM_EABI_UNWINDER true
355 #endif /* TARGET_UNWIND_INFO */
356
357 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
358 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
359
360 #undef  TARGET_CANNOT_COPY_INSN_P
361 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
362
363 #ifdef HAVE_AS_TLS
364 #undef TARGET_HAVE_TLS
365 #define TARGET_HAVE_TLS true
366 #endif
367
368 #undef TARGET_CANNOT_FORCE_CONST_MEM
369 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
370
371 #undef TARGET_MAX_ANCHOR_OFFSET
372 #define TARGET_MAX_ANCHOR_OFFSET 4095
373
374 /* The minimum is set such that the total size of the block
375    for a particular anchor is -4088 + 1 + 4095 bytes, which is
376    divisible by eight, ensuring natural spacing of anchors.  */
377 #undef TARGET_MIN_ANCHOR_OFFSET
378 #define TARGET_MIN_ANCHOR_OFFSET -4088
379
380 #undef TARGET_SCHED_ISSUE_RATE
381 #define TARGET_SCHED_ISSUE_RATE arm_issue_rate
382
383 #undef TARGET_MANGLE_TYPE
384 #define TARGET_MANGLE_TYPE arm_mangle_type
385
386 #ifdef HAVE_AS_TLS
387 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
388 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
389 #endif
390
391 struct gcc_target targetm = TARGET_INITIALIZER;
392 \f
393 /* Obstack for minipool constant handling.  */
394 static struct obstack minipool_obstack;
395 static char *         minipool_startobj;
396
397 /* The maximum number of insns skipped which
398    will be conditionalised if possible.  */
399 static int max_insns_skipped = 5;
400
401 extern FILE * asm_out_file;
402
403 /* True if we are currently building a constant table.  */
404 int making_const_table;
405
406 /* Define the information needed to generate branch insns.  This is
407    stored from the compare operation.  */
408 rtx arm_compare_op0, arm_compare_op1;
409
410 /* The processor for which instructions should be scheduled.  */
411 enum processor_type arm_tune = arm_none;
412
413 /* The default processor used if not overridden by commandline.  */
414 static enum processor_type arm_default_cpu = arm_none;
415
416 /* Which floating point model to use.  */
417 enum arm_fp_model arm_fp_model;
418
419 /* Which floating point hardware is available.  */
420 enum fputype arm_fpu_arch;
421
422 /* Which floating point hardware to schedule for.  */
423 enum fputype arm_fpu_tune;
424
425 /* Whether to use floating point hardware.  */
426 enum float_abi_type arm_float_abi;
427
428 /* Which ABI to use.  */
429 enum arm_abi_type arm_abi;
430
431 /* Which thread pointer model to use.  */
432 enum arm_tp_type target_thread_pointer = TP_AUTO;
433
434 /* Used to parse -mstructure_size_boundary command line option.  */
435 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
436
437 /* Used for Thumb call_via trampolines.  */
438 rtx thumb_call_via_label[14];
439 static int thumb_call_reg_needed;
440
441 /* Bit values used to identify processor capabilities.  */
442 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
443 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
444 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
445 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
446 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
447 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
448 #define FL_THUMB      (1 << 6)        /* Thumb aware */
449 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
450 #define FL_STRONG     (1 << 8)        /* StrongARM */
451 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
452 #define FL_XSCALE     (1 << 10)       /* XScale */
453 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
454 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
455                                          media instructions.  */
456 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
457 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
458                                          Note: ARM6 & 7 derivatives only.  */
459 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
460 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
461 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
462                                          profile.  */
463 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
464 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
465 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
466
467 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
468
469 #define FL_FOR_ARCH2    FL_NOTM
470 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
471 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
472 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
473 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
474 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
475 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
476 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
477 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
478 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
479 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
480 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
481 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
482 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
483 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
484 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
485 #define FL_FOR_ARCH6M   (FL_FOR_ARCH6 & ~FL_NOTM)
486 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
487 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
488 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
489 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
490
491 /* The bits in this mask specify which
492    instructions we are allowed to generate.  */
493 static unsigned long insn_flags = 0;
494
495 /* The bits in this mask specify which instruction scheduling options should
496    be used.  */
497 static unsigned long tune_flags = 0;
498
499 /* The following are used in the arm.md file as equivalents to bits
500    in the above two flag variables.  */
501
502 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
503 int arm_arch3m = 0;
504
505 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
506 int arm_arch4 = 0;
507
508 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
509 int arm_arch4t = 0;
510
511 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
512 int arm_arch5 = 0;
513
514 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
515 int arm_arch5e = 0;
516
517 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
518 int arm_arch6 = 0;
519
520 /* Nonzero if this chip supports the ARM 6K extensions.  */
521 int arm_arch6k = 0;
522
523 /* Nonzero if instructions not present in the 'M' profile can be used.  */
524 int arm_arch_notm = 0;
525
526 /* Nonzero if this chip can benefit from load scheduling.  */
527 int arm_ld_sched = 0;
528
529 /* Nonzero if this chip is a StrongARM.  */
530 int arm_tune_strongarm = 0;
531
532 /* Nonzero if this chip is a Cirrus variant.  */
533 int arm_arch_cirrus = 0;
534
535 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
536 int arm_arch_iwmmxt = 0;
537
538 /* Nonzero if this chip is an XScale.  */
539 int arm_arch_xscale = 0;
540
541 /* Nonzero if tuning for XScale  */
542 int arm_tune_xscale = 0;
543
544 /* Nonzero if we want to tune for stores that access the write-buffer.
545    This typically means an ARM6 or ARM7 with MMU or MPU.  */
546 int arm_tune_wbuf = 0;
547
548 /* Nonzero if tuning for Cortex-A9.  */
549 int arm_tune_cortex_a9 = 0;
550
551 /* Nonzero if generating Thumb instructions.  */
552 int thumb_code = 0;
553
554 /* Nonzero if we should define __THUMB_INTERWORK__ in the
555    preprocessor.
556    XXX This is a bit of a hack, it's intended to help work around
557    problems in GLD which doesn't understand that armv5t code is
558    interworking clean.  */
559 int arm_cpp_interwork = 0;
560
561 /* Nonzero if chip supports Thumb 2.  */
562 int arm_arch_thumb2;
563
564 /* Nonzero if chip supports integer division instruction.  */
565 int arm_arch_hwdiv;
566
567 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
568    must report the mode of the memory reference from PRINT_OPERAND to
569    PRINT_OPERAND_ADDRESS.  */
570 enum machine_mode output_memory_reference_mode;
571
572 /* The register number to be used for the PIC offset register.  */
573 unsigned arm_pic_register = INVALID_REGNUM;
574
575 /* Set to 1 when a return insn is output, this means that the epilogue
576    is not needed.  */
577 int return_used_this_function;
578
579 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
580    the next function.  */
581 static int after_arm_reorg = 0;
582
583 /* The maximum number of insns to be used when loading a constant.  */
584 static int arm_constant_limit = 3;
585
586 /* For an explanation of these variables, see final_prescan_insn below.  */
587 int arm_ccfsm_state;
588 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
589 enum arm_cond_code arm_current_cc;
590 rtx arm_target_insn;
591 int arm_target_label;
592 /* The number of conditionally executed insns, including the current insn.  */
593 int arm_condexec_count = 0;
594 /* A bitmask specifying the patterns for the IT block.
595    Zero means do not output an IT block before this insn. */
596 int arm_condexec_mask = 0;
597 /* The number of bits used in arm_condexec_mask.  */
598 int arm_condexec_masklen = 0;
599
600 /* The condition codes of the ARM, and the inverse function.  */
601 static const char * const arm_condition_codes[] =
602 {
603   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
604   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
605 };
606
607 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
608 #define streq(string1, string2) (strcmp (string1, string2) == 0)
609
610 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
611                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
612                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
613 \f
614 /* Initialization code.  */
615
616 struct processors
617 {
618   const char *const name;
619   enum processor_type core;
620   const char *arch;
621   const unsigned long flags;
622   bool (* rtx_costs) (rtx, enum rtx_code, enum rtx_code, int *, bool);
623 };
624
625 /* Not all of these give usefully different compilation alternatives,
626    but there is no simple way of generalizing them.  */
627 static const struct processors all_cores[] =
628 {
629   /* ARM Cores */
630 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
631   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
632 #include "arm-cores.def"
633 #undef ARM_CORE
634   {NULL, arm_none, NULL, 0, NULL}
635 };
636
637 static const struct processors all_architectures[] =
638 {
639   /* ARM Architectures */
640   /* We don't specify rtx_costs here as it will be figured out
641      from the core.  */
642
643   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
644   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
645   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
646   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
647   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
648   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
649      implementations that support it, so we will leave it out for now.  */
650   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
651   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
652   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
653   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
654   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
655   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
656   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
657   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
658   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
659   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
660   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
661   {"armv6-m", cortexm1,   "6M",                           FL_FOR_ARCH6M, NULL},
662   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
663   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
664   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
665   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
666   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
667   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
668   {NULL, arm_none, NULL, 0 , NULL}
669 };
670
671 struct arm_cpu_select
672 {
673   const char *              string;
674   const char *              name;
675   const struct processors * processors;
676 };
677
678 /* This is a magic structure.  The 'string' field is magically filled in
679    with a pointer to the value specified by the user on the command line
680    assuming that the user has specified such a value.  */
681
682 static struct arm_cpu_select arm_select[] =
683 {
684   /* string       name            processors  */
685   { NULL,       "-mcpu=",       all_cores  },
686   { NULL,       "-march=",      all_architectures },
687   { NULL,       "-mtune=",      all_cores }
688 };
689
690 /* Defines representing the indexes into the above table.  */
691 #define ARM_OPT_SET_CPU 0
692 #define ARM_OPT_SET_ARCH 1
693 #define ARM_OPT_SET_TUNE 2
694
695 /* The name of the preprocessor macro to define for this architecture.  */
696
697 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
698
699 struct fpu_desc
700 {
701   const char * name;
702   enum fputype fpu;
703 };
704
705
706 /* Available values for -mfpu=.  */
707
708 static const struct fpu_desc all_fpus[] =
709 {
710   {"fpa",       FPUTYPE_FPA},
711   {"fpe2",      FPUTYPE_FPA_EMU2},
712   {"fpe3",      FPUTYPE_FPA_EMU2},
713   {"maverick",  FPUTYPE_MAVERICK},
714   {"vfp",       FPUTYPE_VFP},
715   {"vfp3",      FPUTYPE_VFP3},
716   {"vfpv3",     FPUTYPE_VFP3},
717   {"vfpv3-d16", FPUTYPE_VFP3D16},
718   {"neon",      FPUTYPE_NEON}
719 };
720
721
722 /* Floating point models used by the different hardware.
723    See fputype in arm.h.  */
724
725 static const enum fputype fp_model_for_fpu[] =
726 {
727   /* No FP hardware.  */
728   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
729   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
730   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
731   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
732   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
733   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP  */
734   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3D16  */
735   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3  */
736   ARM_FP_MODEL_VFP              /* FPUTYPE_NEON  */
737 };
738
739
740 struct float_abi
741 {
742   const char * name;
743   enum float_abi_type abi_type;
744 };
745
746
747 /* Available values for -mfloat-abi=.  */
748
749 static const struct float_abi all_float_abis[] =
750 {
751   {"soft",      ARM_FLOAT_ABI_SOFT},
752   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
753   {"hard",      ARM_FLOAT_ABI_HARD}
754 };
755
756
757 struct abi_name
758 {
759   const char *name;
760   enum arm_abi_type abi_type;
761 };
762
763
764 /* Available values for -mabi=.  */
765
766 static const struct abi_name arm_all_abis[] =
767 {
768   {"apcs-gnu",    ARM_ABI_APCS},
769   {"atpcs",   ARM_ABI_ATPCS},
770   {"aapcs",   ARM_ABI_AAPCS},
771   {"iwmmxt",  ARM_ABI_IWMMXT},
772   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
773 };
774
775 /* Supported TLS relocations.  */
776
777 enum tls_reloc {
778   TLS_GD32,
779   TLS_LDM32,
780   TLS_LDO32,
781   TLS_IE32,
782   TLS_LE32
783 };
784
785 /* Emit an insn that's a simple single-set.  Both the operands must be known
786    to be valid.  */
787 inline static rtx
788 emit_set_insn (rtx x, rtx y)
789 {
790   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
791 }
792
793 /* Return the number of bits set in VALUE.  */
794 static unsigned
795 bit_count (unsigned long value)
796 {
797   unsigned long count = 0;
798
799   while (value)
800     {
801       count++;
802       value &= value - 1;  /* Clear the least-significant set bit.  */
803     }
804
805   return count;
806 }
807
808 /* Set up library functions unique to ARM.  */
809
810 static void
811 arm_init_libfuncs (void)
812 {
813   /* There are no special library functions unless we are using the
814      ARM BPABI.  */
815   if (!TARGET_BPABI)
816     return;
817
818   /* The functions below are described in Section 4 of the "Run-Time
819      ABI for the ARM architecture", Version 1.0.  */
820
821   /* Double-precision floating-point arithmetic.  Table 2.  */
822   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
823   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
824   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
825   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
826   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
827
828   /* Double-precision comparisons.  Table 3.  */
829   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
830   set_optab_libfunc (ne_optab, DFmode, NULL);
831   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
832   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
833   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
834   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
835   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
836
837   /* Single-precision floating-point arithmetic.  Table 4.  */
838   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
839   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
840   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
841   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
842   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
843
844   /* Single-precision comparisons.  Table 5.  */
845   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
846   set_optab_libfunc (ne_optab, SFmode, NULL);
847   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
848   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
849   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
850   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
851   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
852
853   /* Floating-point to integer conversions.  Table 6.  */
854   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
855   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
856   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
857   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
858   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
859   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
860   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
861   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
862
863   /* Conversions between floating types.  Table 7.  */
864   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
865   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
866
867   /* Integer to floating-point conversions.  Table 8.  */
868   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
869   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
870   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
871   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
872   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
873   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
874   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
875   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
876
877   /* Long long.  Table 9.  */
878   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
879   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
880   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
881   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
882   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
883   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
884   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
885   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
886
887   /* Integer (32/32->32) division.  \S 4.3.1.  */
888   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
889   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
890
891   /* The divmod functions are designed so that they can be used for
892      plain division, even though they return both the quotient and the
893      remainder.  The quotient is returned in the usual location (i.e.,
894      r0 for SImode, {r0, r1} for DImode), just as would be expected
895      for an ordinary division routine.  Because the AAPCS calling
896      conventions specify that all of { r0, r1, r2, r3 } are
897      callee-saved registers, there is no need to tell the compiler
898      explicitly that those registers are clobbered by these
899      routines.  */
900   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
901   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
902
903   /* For SImode division the ABI provides div-without-mod routines,
904      which are faster.  */
905   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
906   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
907
908   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
909      divmod libcalls instead.  */
910   set_optab_libfunc (smod_optab, DImode, NULL);
911   set_optab_libfunc (umod_optab, DImode, NULL);
912   set_optab_libfunc (smod_optab, SImode, NULL);
913   set_optab_libfunc (umod_optab, SImode, NULL);
914 }
915
916 /* Implement TARGET_HANDLE_OPTION.  */
917
918 static bool
919 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
920 {
921   switch (code)
922     {
923     case OPT_march_:
924       arm_select[1].string = arg;
925       return true;
926
927     case OPT_mcpu_:
928       arm_select[0].string = arg;
929       return true;
930
931     case OPT_mhard_float:
932       target_float_abi_name = "hard";
933       return true;
934
935     case OPT_msoft_float:
936       target_float_abi_name = "soft";
937       return true;
938
939     case OPT_mtune_:
940       arm_select[2].string = arg;
941       return true;
942
943     default:
944       return true;
945     }
946 }
947
948 static void
949 arm_target_help (void)
950 {
951   int i;
952   static int columns = 0;
953   int remaining;
954
955   /* If we have not done so already, obtain the desired maximum width of
956      the output.  Note - this is a duplication of the code at the start of
957      gcc/opts.c:print_specific_help() - the two copies should probably be
958      replaced by a single function.  */
959   if (columns == 0)
960     {
961       const char *p;
962
963       GET_ENVIRONMENT (p, "COLUMNS");
964       if (p != NULL)
965         {
966           int value = atoi (p);
967
968           if (value > 0)
969             columns = value;
970         }
971
972       if (columns == 0)
973         /* Use a reasonable default.  */
974         columns = 80;
975     }
976
977   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
978
979   /* The - 2 is because we know that the last entry in the array is NULL.  */
980   i = ARRAY_SIZE (all_cores) - 2;
981   gcc_assert (i > 0);
982   printf ("    %s", all_cores[i].name);
983   remaining = columns - (strlen (all_cores[i].name) + 4);
984   gcc_assert (remaining >= 0);
985
986   while (i--)
987     {
988       int len = strlen (all_cores[i].name);
989
990       if (remaining > len + 2)
991         {
992           printf (", %s", all_cores[i].name);
993           remaining -= len + 2;
994         }
995       else
996         {
997           if (remaining > 0)
998             printf (",");
999           printf ("\n    %s", all_cores[i].name);
1000           remaining = columns - (len + 4);
1001         }
1002     }
1003
1004   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
1005
1006   i = ARRAY_SIZE (all_architectures) - 2;
1007   gcc_assert (i > 0);
1008   
1009   printf ("    %s", all_architectures[i].name);
1010   remaining = columns - (strlen (all_architectures[i].name) + 4);
1011   gcc_assert (remaining >= 0);
1012
1013   while (i--)
1014     {
1015       int len = strlen (all_architectures[i].name);
1016
1017       if (remaining > len + 2)
1018         {
1019           printf (", %s", all_architectures[i].name);
1020           remaining -= len + 2;
1021         }
1022       else
1023         {
1024           if (remaining > 0)
1025             printf (",");
1026           printf ("\n    %s", all_architectures[i].name);
1027           remaining = columns - (len + 4);
1028         }
1029     }
1030   printf ("\n");
1031
1032 }
1033
1034 /* Fix up any incompatible options that the user has specified.
1035    This has now turned into a maze.  */
1036 void
1037 arm_override_options (void)
1038 {
1039   unsigned i;
1040   enum processor_type target_arch_cpu = arm_none;
1041   enum processor_type selected_cpu = arm_none;
1042
1043   /* Set up the flags based on the cpu/architecture selected by the user.  */
1044   for (i = ARRAY_SIZE (arm_select); i--;)
1045     {
1046       struct arm_cpu_select * ptr = arm_select + i;
1047
1048       if (ptr->string != NULL && ptr->string[0] != '\0')
1049         {
1050           const struct processors * sel;
1051
1052           for (sel = ptr->processors; sel->name != NULL; sel++)
1053             if (streq (ptr->string, sel->name))
1054               {
1055                 /* Set the architecture define.  */
1056                 if (i != ARM_OPT_SET_TUNE)
1057                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1058
1059                 /* Determine the processor core for which we should
1060                    tune code-generation.  */
1061                 if (/* -mcpu= is a sensible default.  */
1062                     i == ARM_OPT_SET_CPU
1063                     /* -mtune= overrides -mcpu= and -march=.  */
1064                     || i == ARM_OPT_SET_TUNE)
1065                   arm_tune = (enum processor_type) (sel - ptr->processors);
1066
1067                 /* Remember the CPU associated with this architecture.
1068                    If no other option is used to set the CPU type,
1069                    we'll use this to guess the most suitable tuning
1070                    options.  */
1071                 if (i == ARM_OPT_SET_ARCH)
1072                   target_arch_cpu = sel->core;
1073
1074                 if (i == ARM_OPT_SET_CPU)
1075                   selected_cpu = (enum processor_type) (sel - ptr->processors);
1076                   
1077                 if (i != ARM_OPT_SET_TUNE)
1078                   {
1079                     /* If we have been given an architecture and a processor
1080                        make sure that they are compatible.  We only generate
1081                        a warning though, and we prefer the CPU over the
1082                        architecture.  */
1083                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
1084                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
1085                                ptr->string);
1086
1087                     insn_flags = sel->flags;
1088                   }
1089
1090                 break;
1091               }
1092
1093           if (sel->name == NULL)
1094             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
1095         }
1096     }
1097
1098   /* Guess the tuning options from the architecture if necessary.  */
1099   if (arm_tune == arm_none)
1100     arm_tune = target_arch_cpu;
1101
1102   /* If the user did not specify a processor, choose one for them.  */
1103   if (insn_flags == 0)
1104     {
1105       const struct processors * sel;
1106       unsigned int        sought;
1107
1108       selected_cpu = TARGET_CPU_DEFAULT;
1109       if (selected_cpu == arm_none)
1110         {
1111 #ifdef SUBTARGET_CPU_DEFAULT
1112           /* Use the subtarget default CPU if none was specified by
1113              configure.  */
1114           selected_cpu = SUBTARGET_CPU_DEFAULT;
1115 #endif
1116           /* Default to ARM6.  */
1117           if (selected_cpu == arm_none)
1118             selected_cpu = arm6;
1119         }
1120       sel = &all_cores[selected_cpu];
1121
1122       insn_flags = sel->flags;
1123
1124       /* Now check to see if the user has specified some command line
1125          switch that require certain abilities from the cpu.  */
1126       sought = 0;
1127
1128       if (TARGET_INTERWORK || TARGET_THUMB)
1129         {
1130           sought |= (FL_THUMB | FL_MODE32);
1131
1132           /* There are no ARM processors that support both APCS-26 and
1133              interworking.  Therefore we force FL_MODE26 to be removed
1134              from insn_flags here (if it was set), so that the search
1135              below will always be able to find a compatible processor.  */
1136           insn_flags &= ~FL_MODE26;
1137         }
1138
1139       if (sought != 0 && ((sought & insn_flags) != sought))
1140         {
1141           /* Try to locate a CPU type that supports all of the abilities
1142              of the default CPU, plus the extra abilities requested by
1143              the user.  */
1144           for (sel = all_cores; sel->name != NULL; sel++)
1145             if ((sel->flags & sought) == (sought | insn_flags))
1146               break;
1147
1148           if (sel->name == NULL)
1149             {
1150               unsigned current_bit_count = 0;
1151               const struct processors * best_fit = NULL;
1152
1153               /* Ideally we would like to issue an error message here
1154                  saying that it was not possible to find a CPU compatible
1155                  with the default CPU, but which also supports the command
1156                  line options specified by the programmer, and so they
1157                  ought to use the -mcpu=<name> command line option to
1158                  override the default CPU type.
1159
1160                  If we cannot find a cpu that has both the
1161                  characteristics of the default cpu and the given
1162                  command line options we scan the array again looking
1163                  for a best match.  */
1164               for (sel = all_cores; sel->name != NULL; sel++)
1165                 if ((sel->flags & sought) == sought)
1166                   {
1167                     unsigned count;
1168
1169                     count = bit_count (sel->flags & insn_flags);
1170
1171                     if (count >= current_bit_count)
1172                       {
1173                         best_fit = sel;
1174                         current_bit_count = count;
1175                       }
1176                   }
1177
1178               gcc_assert (best_fit);
1179               sel = best_fit;
1180             }
1181
1182           insn_flags = sel->flags;
1183         }
1184       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1185       arm_default_cpu = (enum processor_type) (sel - all_cores);
1186       if (arm_tune == arm_none)
1187         arm_tune = arm_default_cpu;
1188     }
1189
1190   /* The processor for which we should tune should now have been
1191      chosen.  */
1192   gcc_assert (arm_tune != arm_none);
1193
1194   tune_flags = all_cores[(int)arm_tune].flags;
1195
1196   if (target_abi_name)
1197     {
1198       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1199         {
1200           if (streq (arm_all_abis[i].name, target_abi_name))
1201             {
1202               arm_abi = arm_all_abis[i].abi_type;
1203               break;
1204             }
1205         }
1206       if (i == ARRAY_SIZE (arm_all_abis))
1207         error ("invalid ABI option: -mabi=%s", target_abi_name);
1208     }
1209   else
1210     arm_abi = ARM_DEFAULT_ABI;
1211
1212   /* Make sure that the processor choice does not conflict with any of the
1213      other command line choices.  */
1214   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1215     error ("target CPU does not support ARM mode");
1216
1217   /* BPABI targets use linker tricks to allow interworking on cores
1218      without thumb support.  */
1219   if (TARGET_INTERWORK && !((insn_flags & FL_THUMB) || TARGET_BPABI))
1220     {
1221       warning (0, "target CPU does not support interworking" );
1222       target_flags &= ~MASK_INTERWORK;
1223     }
1224
1225   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1226     {
1227       warning (0, "target CPU does not support THUMB instructions");
1228       target_flags &= ~MASK_THUMB;
1229     }
1230
1231   if (TARGET_APCS_FRAME && TARGET_THUMB)
1232     {
1233       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1234       target_flags &= ~MASK_APCS_FRAME;
1235     }
1236
1237   /* Callee super interworking implies thumb interworking.  Adding
1238      this to the flags here simplifies the logic elsewhere.  */
1239   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1240       target_flags |= MASK_INTERWORK;
1241
1242   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1243      from here where no function is being compiled currently.  */
1244   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1245     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1246
1247   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1248     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1249
1250   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1251     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1252
1253   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1254     {
1255       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1256       target_flags |= MASK_APCS_FRAME;
1257     }
1258
1259   if (TARGET_POKE_FUNCTION_NAME)
1260     target_flags |= MASK_APCS_FRAME;
1261
1262   if (TARGET_APCS_REENT && flag_pic)
1263     error ("-fpic and -mapcs-reent are incompatible");
1264
1265   if (TARGET_APCS_REENT)
1266     warning (0, "APCS reentrant code not supported.  Ignored");
1267
1268   /* If this target is normally configured to use APCS frames, warn if they
1269      are turned off and debugging is turned on.  */
1270   if (TARGET_ARM
1271       && write_symbols != NO_DEBUG
1272       && !TARGET_APCS_FRAME
1273       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1274     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1275
1276   if (TARGET_APCS_FLOAT)
1277     warning (0, "passing floating point arguments in fp regs not yet supported");
1278
1279   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1280   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1281   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1282   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1283   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1284   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1285   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1286   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1287   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1288   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1289   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1290   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1291
1292   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1293   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1294   thumb_code = (TARGET_ARM == 0);
1295   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1296   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1297   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1298   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1299   arm_tune_cortex_a9 = (arm_tune == cortexa9) != 0;
1300
1301   /* If we are not using the default (ARM mode) section anchor offset
1302      ranges, then set the correct ranges now.  */
1303   if (TARGET_THUMB1)
1304     {
1305       /* Thumb-1 LDR instructions cannot have negative offsets.
1306          Permissible positive offset ranges are 5-bit (for byte loads),
1307          6-bit (for halfword loads), or 7-bit (for word loads).
1308          Empirical results suggest a 7-bit anchor range gives the best
1309          overall code size.  */
1310       targetm.min_anchor_offset = 0;
1311       targetm.max_anchor_offset = 127;
1312     }
1313   else if (TARGET_THUMB2)
1314     {
1315       /* The minimum is set such that the total size of the block
1316          for a particular anchor is 248 + 1 + 4095 bytes, which is
1317          divisible by eight, ensuring natural spacing of anchors.  */
1318       targetm.min_anchor_offset = -248;
1319       targetm.max_anchor_offset = 4095;
1320     }
1321
1322   /* V5 code we generate is completely interworking capable, so we turn off
1323      TARGET_INTERWORK here to avoid many tests later on.  */
1324
1325   /* XXX However, we must pass the right pre-processor defines to CPP
1326      or GLD can get confused.  This is a hack.  */
1327   if (TARGET_INTERWORK)
1328     arm_cpp_interwork = 1;
1329
1330   if (arm_arch5)
1331     target_flags &= ~MASK_INTERWORK;
1332
1333   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1334     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1335
1336   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1337     error ("iwmmxt abi requires an iwmmxt capable cpu");
1338
1339   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1340   if (target_fpu_name == NULL && target_fpe_name != NULL)
1341     {
1342       if (streq (target_fpe_name, "2"))
1343         target_fpu_name = "fpe2";
1344       else if (streq (target_fpe_name, "3"))
1345         target_fpu_name = "fpe3";
1346       else
1347         error ("invalid floating point emulation option: -mfpe=%s",
1348                target_fpe_name);
1349     }
1350   if (target_fpu_name != NULL)
1351     {
1352       /* The user specified a FPU.  */
1353       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1354         {
1355           if (streq (all_fpus[i].name, target_fpu_name))
1356             {
1357               arm_fpu_arch = all_fpus[i].fpu;
1358               arm_fpu_tune = arm_fpu_arch;
1359               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1360               break;
1361             }
1362         }
1363       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1364         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1365     }
1366   else
1367     {
1368 #ifdef FPUTYPE_DEFAULT
1369       /* Use the default if it is specified for this platform.  */
1370       arm_fpu_arch = FPUTYPE_DEFAULT;
1371       arm_fpu_tune = FPUTYPE_DEFAULT;
1372 #else
1373       /* Pick one based on CPU type.  */
1374       /* ??? Some targets assume FPA is the default.
1375       if ((insn_flags & FL_VFP) != 0)
1376         arm_fpu_arch = FPUTYPE_VFP;
1377       else
1378       */
1379       if (arm_arch_cirrus)
1380         arm_fpu_arch = FPUTYPE_MAVERICK;
1381       else
1382         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1383 #endif
1384       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1385         arm_fpu_tune = FPUTYPE_FPA;
1386       else
1387         arm_fpu_tune = arm_fpu_arch;
1388       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1389       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1390     }
1391
1392   if (target_float_abi_name != NULL)
1393     {
1394       /* The user specified a FP ABI.  */
1395       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1396         {
1397           if (streq (all_float_abis[i].name, target_float_abi_name))
1398             {
1399               arm_float_abi = all_float_abis[i].abi_type;
1400               break;
1401             }
1402         }
1403       if (i == ARRAY_SIZE (all_float_abis))
1404         error ("invalid floating point abi: -mfloat-abi=%s",
1405                target_float_abi_name);
1406     }
1407   else
1408     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1409
1410   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1411     sorry ("-mfloat-abi=hard and VFP");
1412
1413   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1414      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1415      will ever exist.  GCC makes no attempt to support this combination.  */
1416   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1417     sorry ("iWMMXt and hardware floating point");
1418
1419   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1420   if (TARGET_THUMB2 && TARGET_IWMMXT)
1421     sorry ("Thumb-2 iWMMXt");
1422
1423   /* If soft-float is specified then don't use FPU.  */
1424   if (TARGET_SOFT_FLOAT)
1425     arm_fpu_arch = FPUTYPE_NONE;
1426
1427   /* For arm2/3 there is no need to do any scheduling if there is only
1428      a floating point emulator, or we are doing software floating-point.  */
1429   if ((TARGET_SOFT_FLOAT
1430        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1431        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1432       && (tune_flags & FL_MODE32) == 0)
1433     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1434
1435   if (target_thread_switch)
1436     {
1437       if (strcmp (target_thread_switch, "soft") == 0)
1438         target_thread_pointer = TP_SOFT;
1439       else if (strcmp (target_thread_switch, "auto") == 0)
1440         target_thread_pointer = TP_AUTO;
1441       else if (strcmp (target_thread_switch, "cp15") == 0)
1442         target_thread_pointer = TP_CP15;
1443       else
1444         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1445     }
1446
1447   /* Use the cp15 method if it is available.  */
1448   if (target_thread_pointer == TP_AUTO)
1449     {
1450       if (arm_arch6k && !TARGET_THUMB)
1451         target_thread_pointer = TP_CP15;
1452       else
1453         target_thread_pointer = TP_SOFT;
1454     }
1455
1456   if (TARGET_HARD_TP && TARGET_THUMB1)
1457     error ("can not use -mtp=cp15 with 16-bit Thumb");
1458
1459   /* Override the default structure alignment for AAPCS ABI.  */
1460   if (TARGET_AAPCS_BASED)
1461     arm_structure_size_boundary = 8;
1462
1463   if (structure_size_string != NULL)
1464     {
1465       int size = strtol (structure_size_string, NULL, 0);
1466
1467       if (size == 8 || size == 32
1468           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1469         arm_structure_size_boundary = size;
1470       else
1471         warning (0, "structure size boundary can only be set to %s",
1472                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1473     }
1474
1475   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1476     {
1477       error ("RTP PIC is incompatible with Thumb");
1478       flag_pic = 0;
1479     }
1480
1481   /* If stack checking is disabled, we can use r10 as the PIC register,
1482      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1483   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1484     {
1485       if (TARGET_VXWORKS_RTP)
1486         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1487       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1488     }
1489
1490   if (flag_pic && TARGET_VXWORKS_RTP)
1491     arm_pic_register = 9;
1492
1493   if (arm_pic_register_string != NULL)
1494     {
1495       int pic_register = decode_reg_name (arm_pic_register_string);
1496
1497       if (!flag_pic)
1498         warning (0, "-mpic-register= is useless without -fpic");
1499
1500       /* Prevent the user from choosing an obviously stupid PIC register.  */
1501       else if (pic_register < 0 || call_used_regs[pic_register]
1502                || pic_register == HARD_FRAME_POINTER_REGNUM
1503                || pic_register == STACK_POINTER_REGNUM
1504                || pic_register >= PC_REGNUM
1505                || (TARGET_VXWORKS_RTP
1506                    && (unsigned int) pic_register != arm_pic_register))
1507         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1508       else
1509         arm_pic_register = pic_register;
1510     }
1511
1512   /* Enable -mfix-cortex-m3-ldrd by default for Cortex-M3 cores.  */
1513   if (fix_cm3_ldrd == 2)
1514     {
1515       if (selected_cpu == cortexm3)
1516         fix_cm3_ldrd = 1;
1517       else
1518         fix_cm3_ldrd = 0;
1519     }
1520
1521   /* ??? We might want scheduling for thumb2.  */
1522   if (TARGET_THUMB && flag_schedule_insns)
1523     {
1524       /* Don't warn since it's on by default in -O2.  */
1525       flag_schedule_insns = 0;
1526     }
1527
1528   if (optimize_size)
1529     {
1530       arm_constant_limit = 1;
1531
1532       /* If optimizing for size, bump the number of instructions that we
1533          are prepared to conditionally execute (even on a StrongARM).  */
1534       max_insns_skipped = 6;
1535     }
1536   else
1537     {
1538       /* For processors with load scheduling, it never costs more than
1539          2 cycles to load a constant, and the load scheduler may well
1540          reduce that to 1.  */
1541       if (arm_ld_sched)
1542         arm_constant_limit = 1;
1543
1544       /* On XScale the longer latency of a load makes it more difficult
1545          to achieve a good schedule, so it's faster to synthesize
1546          constants that can be done in two insns.  */
1547       if (arm_tune_xscale)
1548         arm_constant_limit = 2;
1549
1550       /* StrongARM has early execution of branches, so a sequence
1551          that is worth skipping is shorter.  */
1552       if (arm_tune_strongarm)
1553         max_insns_skipped = 3;
1554     }
1555
1556   /* Register global variables with the garbage collector.  */
1557   arm_add_gc_roots ();
1558 }
1559
1560 static void
1561 arm_add_gc_roots (void)
1562 {
1563   gcc_obstack_init(&minipool_obstack);
1564   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1565 }
1566 \f
1567 /* A table of known ARM exception types.
1568    For use with the interrupt function attribute.  */
1569
1570 typedef struct
1571 {
1572   const char *const arg;
1573   const unsigned long return_value;
1574 }
1575 isr_attribute_arg;
1576
1577 static const isr_attribute_arg isr_attribute_args [] =
1578 {
1579   { "IRQ",   ARM_FT_ISR },
1580   { "irq",   ARM_FT_ISR },
1581   { "FIQ",   ARM_FT_FIQ },
1582   { "fiq",   ARM_FT_FIQ },
1583   { "ABORT", ARM_FT_ISR },
1584   { "abort", ARM_FT_ISR },
1585   { "ABORT", ARM_FT_ISR },
1586   { "abort", ARM_FT_ISR },
1587   { "UNDEF", ARM_FT_EXCEPTION },
1588   { "undef", ARM_FT_EXCEPTION },
1589   { "SWI",   ARM_FT_EXCEPTION },
1590   { "swi",   ARM_FT_EXCEPTION },
1591   { NULL,    ARM_FT_NORMAL }
1592 };
1593
1594 /* Returns the (interrupt) function type of the current
1595    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1596
1597 static unsigned long
1598 arm_isr_value (tree argument)
1599 {
1600   const isr_attribute_arg * ptr;
1601   const char *              arg;
1602
1603   if (!arm_arch_notm)
1604     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1605
1606   /* No argument - default to IRQ.  */
1607   if (argument == NULL_TREE)
1608     return ARM_FT_ISR;
1609
1610   /* Get the value of the argument.  */
1611   if (TREE_VALUE (argument) == NULL_TREE
1612       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1613     return ARM_FT_UNKNOWN;
1614
1615   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1616
1617   /* Check it against the list of known arguments.  */
1618   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1619     if (streq (arg, ptr->arg))
1620       return ptr->return_value;
1621
1622   /* An unrecognized interrupt type.  */
1623   return ARM_FT_UNKNOWN;
1624 }
1625
1626 /* Computes the type of the current function.  */
1627
1628 static unsigned long
1629 arm_compute_func_type (void)
1630 {
1631   unsigned long type = ARM_FT_UNKNOWN;
1632   tree a;
1633   tree attr;
1634
1635   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1636
1637   /* Decide if the current function is volatile.  Such functions
1638      never return, and many memory cycles can be saved by not storing
1639      register values that will never be needed again.  This optimization
1640      was added to speed up context switching in a kernel application.  */
1641   if (optimize > 0
1642       && (TREE_NOTHROW (current_function_decl)
1643           || !(flag_unwind_tables
1644                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1645       && TREE_THIS_VOLATILE (current_function_decl))
1646     type |= ARM_FT_VOLATILE;
1647
1648   if (cfun->static_chain_decl != NULL)
1649     type |= ARM_FT_NESTED;
1650
1651   attr = DECL_ATTRIBUTES (current_function_decl);
1652
1653   a = lookup_attribute ("naked", attr);
1654   if (a != NULL_TREE)
1655     type |= ARM_FT_NAKED;
1656
1657   a = lookup_attribute ("isr", attr);
1658   if (a == NULL_TREE)
1659     a = lookup_attribute ("interrupt", attr);
1660
1661   if (a == NULL_TREE)
1662     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1663   else
1664     type |= arm_isr_value (TREE_VALUE (a));
1665
1666   return type;
1667 }
1668
1669 /* Returns the type of the current function.  */
1670
1671 unsigned long
1672 arm_current_func_type (void)
1673 {
1674   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1675     cfun->machine->func_type = arm_compute_func_type ();
1676
1677   return cfun->machine->func_type;
1678 }
1679
1680 bool
1681 arm_allocate_stack_slots_for_args (void)
1682 {
1683   /* Naked functions should not allocate stack slots for arguments.  */
1684   return !IS_NAKED (arm_current_func_type ());
1685 }
1686
1687 \f
1688 /* Return 1 if it is possible to return using a single instruction.
1689    If SIBLING is non-null, this is a test for a return before a sibling
1690    call.  SIBLING is the call insn, so we can examine its register usage.  */
1691
1692 int
1693 use_return_insn (int iscond, rtx sibling)
1694 {
1695   int regno;
1696   unsigned int func_type;
1697   unsigned long saved_int_regs;
1698   unsigned HOST_WIDE_INT stack_adjust;
1699   arm_stack_offsets *offsets;
1700
1701   /* Never use a return instruction before reload has run.  */
1702   if (!reload_completed)
1703     return 0;
1704
1705   func_type = arm_current_func_type ();
1706
1707   /* Naked, volatile and stack alignment functions need special
1708      consideration.  */
1709   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1710     return 0;
1711
1712   /* So do interrupt functions that use the frame pointer and Thumb
1713      interrupt functions.  */
1714   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1715     return 0;
1716
1717   offsets = arm_get_frame_offsets ();
1718   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1719
1720   /* As do variadic functions.  */
1721   if (crtl->args.pretend_args_size
1722       || cfun->machine->uses_anonymous_args
1723       /* Or if the function calls __builtin_eh_return () */
1724       || crtl->calls_eh_return
1725       /* Or if the function calls alloca */
1726       || cfun->calls_alloca
1727       /* Or if there is a stack adjustment.  However, if the stack pointer
1728          is saved on the stack, we can use a pre-incrementing stack load.  */
1729       || !(stack_adjust == 0 || (TARGET_APCS_FRAME && frame_pointer_needed
1730                                  && stack_adjust == 4)))
1731     return 0;
1732
1733   saved_int_regs = offsets->saved_regs_mask;
1734
1735   /* Unfortunately, the insn
1736
1737        ldmib sp, {..., sp, ...}
1738
1739      triggers a bug on most SA-110 based devices, such that the stack
1740      pointer won't be correctly restored if the instruction takes a
1741      page fault.  We work around this problem by popping r3 along with
1742      the other registers, since that is never slower than executing
1743      another instruction.
1744
1745      We test for !arm_arch5 here, because code for any architecture
1746      less than this could potentially be run on one of the buggy
1747      chips.  */
1748   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1749     {
1750       /* Validate that r3 is a call-clobbered register (always true in
1751          the default abi) ...  */
1752       if (!call_used_regs[3])
1753         return 0;
1754
1755       /* ... that it isn't being used for a return value ... */
1756       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1757         return 0;
1758
1759       /* ... or for a tail-call argument ...  */
1760       if (sibling)
1761         {
1762           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1763
1764           if (find_regno_fusage (sibling, USE, 3))
1765             return 0;
1766         }
1767
1768       /* ... and that there are no call-saved registers in r0-r2
1769          (always true in the default ABI).  */
1770       if (saved_int_regs & 0x7)
1771         return 0;
1772     }
1773
1774   /* Can't be done if interworking with Thumb, and any registers have been
1775      stacked.  */
1776   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1777     return 0;
1778
1779   /* On StrongARM, conditional returns are expensive if they aren't
1780      taken and multiple registers have been stacked.  */
1781   if (iscond && arm_tune_strongarm)
1782     {
1783       /* Conditional return when just the LR is stored is a simple
1784          conditional-load instruction, that's not expensive.  */
1785       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1786         return 0;
1787
1788       if (flag_pic 
1789           && arm_pic_register != INVALID_REGNUM
1790           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1791         return 0;
1792     }
1793
1794   /* If there are saved registers but the LR isn't saved, then we need
1795      two instructions for the return.  */
1796   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1797     return 0;
1798
1799   /* Can't be done if any of the FPA regs are pushed,
1800      since this also requires an insn.  */
1801   if (TARGET_HARD_FLOAT && TARGET_FPA)
1802     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1803       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
1804         return 0;
1805
1806   /* Likewise VFP regs.  */
1807   if (TARGET_HARD_FLOAT && TARGET_VFP)
1808     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1809       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
1810         return 0;
1811
1812   if (TARGET_REALLY_IWMMXT)
1813     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1814       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
1815         return 0;
1816
1817   return 1;
1818 }
1819
1820 /* Return TRUE if int I is a valid immediate ARM constant.  */
1821
1822 int
1823 const_ok_for_arm (HOST_WIDE_INT i)
1824 {
1825   int lowbit;
1826
1827   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1828      be all zero, or all one.  */
1829   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1830       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1831           != ((~(unsigned HOST_WIDE_INT) 0)
1832               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1833     return FALSE;
1834
1835   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1836
1837   /* Fast return for 0 and small values.  We must do this for zero, since
1838      the code below can't handle that one case.  */
1839   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1840     return TRUE;
1841
1842   /* Get the number of trailing zeros.  */
1843   lowbit = ffs((int) i) - 1;
1844   
1845   /* Only even shifts are allowed in ARM mode so round down to the
1846      nearest even number.  */
1847   if (TARGET_ARM)
1848     lowbit &= ~1;
1849
1850   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1851     return TRUE;
1852
1853   if (TARGET_ARM)
1854     {
1855       /* Allow rotated constants in ARM mode.  */
1856       if (lowbit <= 4
1857            && ((i & ~0xc000003f) == 0
1858                || (i & ~0xf000000f) == 0
1859                || (i & ~0xfc000003) == 0))
1860         return TRUE;
1861     }
1862   else
1863     {
1864       HOST_WIDE_INT v;
1865
1866       /* Allow repeated pattern.  */
1867       v = i & 0xff;
1868       v |= v << 16;
1869       if (i == v || i == (v | (v << 8)))
1870         return TRUE;
1871     }
1872
1873   return FALSE;
1874 }
1875
1876 /* Return true if I is a valid constant for the operation CODE.  */
1877 static int
1878 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1879 {
1880   if (const_ok_for_arm (i))
1881     return 1;
1882
1883   switch (code)
1884     {
1885     case PLUS:
1886     case COMPARE:
1887     case EQ:
1888     case NE:
1889     case GT:
1890     case LE:
1891     case LT:
1892     case GE:
1893     case GEU:
1894     case LTU:
1895     case GTU:
1896     case LEU:
1897     case UNORDERED:
1898     case ORDERED:
1899     case UNEQ:
1900     case UNGE:
1901     case UNLT:
1902     case UNGT:
1903     case UNLE:
1904       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1905
1906     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1907     case XOR:
1908     case IOR:
1909       return 0;
1910
1911     case AND:
1912       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1913
1914     default:
1915       gcc_unreachable ();
1916     }
1917 }
1918
1919 /* Emit a sequence of insns to handle a large constant.
1920    CODE is the code of the operation required, it can be any of SET, PLUS,
1921    IOR, AND, XOR, MINUS;
1922    MODE is the mode in which the operation is being performed;
1923    VAL is the integer to operate on;
1924    SOURCE is the other operand (a register, or a null-pointer for SET);
1925    SUBTARGETS means it is safe to create scratch registers if that will
1926    either produce a simpler sequence, or we will want to cse the values.
1927    Return value is the number of insns emitted.  */
1928
1929 /* ??? Tweak this for thumb2.  */
1930 int
1931 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1932                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1933 {
1934   rtx cond;
1935
1936   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1937     cond = COND_EXEC_TEST (PATTERN (insn));
1938   else
1939     cond = NULL_RTX;
1940
1941   if (subtargets || code == SET
1942       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1943           && REGNO (target) != REGNO (source)))
1944     {
1945       /* After arm_reorg has been called, we can't fix up expensive
1946          constants by pushing them into memory so we must synthesize
1947          them in-line, regardless of the cost.  This is only likely to
1948          be more costly on chips that have load delay slots and we are
1949          compiling without running the scheduler (so no splitting
1950          occurred before the final instruction emission).
1951
1952          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1953       */
1954       if (!after_arm_reorg
1955           && !cond
1956           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1957                                 1, 0)
1958               > arm_constant_limit + (code != SET)))
1959         {
1960           if (code == SET)
1961             {
1962               /* Currently SET is the only monadic value for CODE, all
1963                  the rest are diadic.  */
1964               if (TARGET_USE_MOVT)
1965                 arm_emit_movpair (target, GEN_INT (val));
1966               else
1967                 emit_set_insn (target, GEN_INT (val));
1968
1969               return 1;
1970             }
1971           else
1972             {
1973               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1974
1975               if (TARGET_USE_MOVT)
1976                 arm_emit_movpair (temp, GEN_INT (val));
1977               else
1978                 emit_set_insn (temp, GEN_INT (val));
1979
1980               /* For MINUS, the value is subtracted from, since we never
1981                  have subtraction of a constant.  */
1982               if (code == MINUS)
1983                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1984               else
1985                 emit_set_insn (target,
1986                                gen_rtx_fmt_ee (code, mode, source, temp));
1987               return 2;
1988             }
1989         }
1990     }
1991
1992   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1993                            1);
1994 }
1995
1996 /* Return the number of ARM instructions required to synthesize the given
1997    constant.  */
1998 static int
1999 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
2000 {
2001   HOST_WIDE_INT temp1;
2002   int num_insns = 0;
2003   do
2004     {
2005       int end;
2006
2007       if (i <= 0)
2008         i += 32;
2009       if (remainder & (3 << (i - 2)))
2010         {
2011           end = i - 8;
2012           if (end < 0)
2013             end += 32;
2014           temp1 = remainder & ((0x0ff << end)
2015                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
2016           remainder &= ~temp1;
2017           num_insns++;
2018           i -= 6;
2019         }
2020       i -= 2;
2021     } while (remainder);
2022   return num_insns;
2023 }
2024
2025 /* Emit an instruction with the indicated PATTERN.  If COND is
2026    non-NULL, conditionalize the execution of the instruction on COND
2027    being true.  */
2028
2029 static void
2030 emit_constant_insn (rtx cond, rtx pattern)
2031 {
2032   if (cond)
2033     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2034   emit_insn (pattern);
2035 }
2036
2037 /* As above, but extra parameter GENERATE which, if clear, suppresses
2038    RTL generation.  */
2039 /* ??? This needs more work for thumb2.  */
2040
2041 static int
2042 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2043                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2044                   int generate)
2045 {
2046   int can_invert = 0;
2047   int can_negate = 0;
2048   int can_negate_initial = 0;
2049   int can_shift = 0;
2050   int i;
2051   int num_bits_set = 0;
2052   int set_sign_bit_copies = 0;
2053   int clear_sign_bit_copies = 0;
2054   int clear_zero_bit_copies = 0;
2055   int set_zero_bit_copies = 0;
2056   int insns = 0;
2057   unsigned HOST_WIDE_INT temp1, temp2;
2058   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2059
2060   /* Find out which operations are safe for a given CODE.  Also do a quick
2061      check for degenerate cases; these can occur when DImode operations
2062      are split.  */
2063   switch (code)
2064     {
2065     case SET:
2066       can_invert = 1;
2067       can_shift = 1;
2068       can_negate = 1;
2069       break;
2070
2071     case PLUS:
2072       can_negate = 1;
2073       can_negate_initial = 1;
2074       break;
2075
2076     case IOR:
2077       if (remainder == 0xffffffff)
2078         {
2079           if (generate)
2080             emit_constant_insn (cond,
2081                                 gen_rtx_SET (VOIDmode, target,
2082                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2083           return 1;
2084         }
2085       if (remainder == 0)
2086         {
2087           if (reload_completed && rtx_equal_p (target, source))
2088             return 0;
2089           if (generate)
2090             emit_constant_insn (cond,
2091                                 gen_rtx_SET (VOIDmode, target, source));
2092           return 1;
2093         }
2094       break;
2095
2096     case AND:
2097       if (remainder == 0)
2098         {
2099           if (generate)
2100             emit_constant_insn (cond,
2101                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2102           return 1;
2103         }
2104       if (remainder == 0xffffffff)
2105         {
2106           if (reload_completed && rtx_equal_p (target, source))
2107             return 0;
2108           if (generate)
2109             emit_constant_insn (cond,
2110                                 gen_rtx_SET (VOIDmode, target, source));
2111           return 1;
2112         }
2113       can_invert = 1;
2114       break;
2115
2116     case XOR:
2117       if (remainder == 0)
2118         {
2119           if (reload_completed && rtx_equal_p (target, source))
2120             return 0;
2121           if (generate)
2122             emit_constant_insn (cond,
2123                                 gen_rtx_SET (VOIDmode, target, source));
2124           return 1;
2125         }
2126
2127       /* We don't know how to handle other cases yet.  */
2128       gcc_assert (remainder == 0xffffffff);
2129
2130       if (generate)
2131         emit_constant_insn (cond,
2132                             gen_rtx_SET (VOIDmode, target,
2133                                          gen_rtx_NOT (mode, source)));
2134       return 1;
2135
2136     case MINUS:
2137       /* We treat MINUS as (val - source), since (source - val) is always
2138          passed as (source + (-val)).  */
2139       if (remainder == 0)
2140         {
2141           if (generate)
2142             emit_constant_insn (cond,
2143                                 gen_rtx_SET (VOIDmode, target,
2144                                              gen_rtx_NEG (mode, source)));
2145           return 1;
2146         }
2147       if (const_ok_for_arm (val))
2148         {
2149           if (generate)
2150             emit_constant_insn (cond,
2151                                 gen_rtx_SET (VOIDmode, target,
2152                                              gen_rtx_MINUS (mode, GEN_INT (val),
2153                                                             source)));
2154           return 1;
2155         }
2156       can_negate = 1;
2157
2158       break;
2159
2160     default:
2161       gcc_unreachable ();
2162     }
2163
2164   /* If we can do it in one insn get out quickly.  */
2165   if (const_ok_for_arm (val)
2166       || (can_negate_initial && const_ok_for_arm (-val))
2167       || (can_invert && const_ok_for_arm (~val)))
2168     {
2169       if (generate)
2170         emit_constant_insn (cond,
2171                             gen_rtx_SET (VOIDmode, target,
2172                                          (source
2173                                           ? gen_rtx_fmt_ee (code, mode, source,
2174                                                             GEN_INT (val))
2175                                           : GEN_INT (val))));
2176       return 1;
2177     }
2178
2179   /* Calculate a few attributes that may be useful for specific
2180      optimizations.  */
2181   for (i = 31; i >= 0; i--)
2182     {
2183       if ((remainder & (1 << i)) == 0)
2184         clear_sign_bit_copies++;
2185       else
2186         break;
2187     }
2188
2189   for (i = 31; i >= 0; i--)
2190     {
2191       if ((remainder & (1 << i)) != 0)
2192         set_sign_bit_copies++;
2193       else
2194         break;
2195     }
2196
2197   for (i = 0; i <= 31; i++)
2198     {
2199       if ((remainder & (1 << i)) == 0)
2200         clear_zero_bit_copies++;
2201       else
2202         break;
2203     }
2204
2205   for (i = 0; i <= 31; i++)
2206     {
2207       if ((remainder & (1 << i)) != 0)
2208         set_zero_bit_copies++;
2209       else
2210         break;
2211     }
2212
2213   switch (code)
2214     {
2215     case SET:
2216       /* See if we can use movw.  */
2217       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2218         {
2219           if (generate)
2220             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2221                                                    GEN_INT (val)));
2222           return 1;
2223         }
2224
2225       /* See if we can do this by sign_extending a constant that is known
2226          to be negative.  This is a good, way of doing it, since the shift
2227          may well merge into a subsequent insn.  */
2228       if (set_sign_bit_copies > 1)
2229         {
2230           if (const_ok_for_arm
2231               (temp1 = ARM_SIGN_EXTEND (remainder
2232                                         << (set_sign_bit_copies - 1))))
2233             {
2234               if (generate)
2235                 {
2236                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2237                   emit_constant_insn (cond,
2238                                       gen_rtx_SET (VOIDmode, new_src,
2239                                                    GEN_INT (temp1)));
2240                   emit_constant_insn (cond,
2241                                       gen_ashrsi3 (target, new_src,
2242                                                    GEN_INT (set_sign_bit_copies - 1)));
2243                 }
2244               return 2;
2245             }
2246           /* For an inverted constant, we will need to set the low bits,
2247              these will be shifted out of harm's way.  */
2248           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2249           if (const_ok_for_arm (~temp1))
2250             {
2251               if (generate)
2252                 {
2253                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2254                   emit_constant_insn (cond,
2255                                       gen_rtx_SET (VOIDmode, new_src,
2256                                                    GEN_INT (temp1)));
2257                   emit_constant_insn (cond,
2258                                       gen_ashrsi3 (target, new_src,
2259                                                    GEN_INT (set_sign_bit_copies - 1)));
2260                 }
2261               return 2;
2262             }
2263         }
2264
2265       /* See if we can calculate the value as the difference between two
2266          valid immediates.  */
2267       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2268         {
2269           int topshift = clear_sign_bit_copies & ~1;
2270
2271           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2272                                    & (0xff000000 >> topshift));
2273
2274           /* If temp1 is zero, then that means the 9 most significant
2275              bits of remainder were 1 and we've caused it to overflow.
2276              When topshift is 0 we don't need to do anything since we
2277              can borrow from 'bit 32'.  */
2278           if (temp1 == 0 && topshift != 0)
2279             temp1 = 0x80000000 >> (topshift - 1);
2280
2281           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2282
2283           if (const_ok_for_arm (temp2))
2284             {
2285               if (generate)
2286                 {
2287                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2288                   emit_constant_insn (cond,
2289                                       gen_rtx_SET (VOIDmode, new_src,
2290                                                    GEN_INT (temp1)));
2291                   emit_constant_insn (cond,
2292                                       gen_addsi3 (target, new_src,
2293                                                   GEN_INT (-temp2)));
2294                 }
2295
2296               return 2;
2297             }
2298         }
2299
2300       /* See if we can generate this by setting the bottom (or the top)
2301          16 bits, and then shifting these into the other half of the
2302          word.  We only look for the simplest cases, to do more would cost
2303          too much.  Be careful, however, not to generate this when the
2304          alternative would take fewer insns.  */
2305       if (val & 0xffff0000)
2306         {
2307           temp1 = remainder & 0xffff0000;
2308           temp2 = remainder & 0x0000ffff;
2309
2310           /* Overlaps outside this range are best done using other methods.  */
2311           for (i = 9; i < 24; i++)
2312             {
2313               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2314                   && !const_ok_for_arm (temp2))
2315                 {
2316                   rtx new_src = (subtargets
2317                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2318                                  : target);
2319                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2320                                             source, subtargets, generate);
2321                   source = new_src;
2322                   if (generate)
2323                     emit_constant_insn
2324                       (cond,
2325                        gen_rtx_SET
2326                        (VOIDmode, target,
2327                         gen_rtx_IOR (mode,
2328                                      gen_rtx_ASHIFT (mode, source,
2329                                                      GEN_INT (i)),
2330                                      source)));
2331                   return insns + 1;
2332                 }
2333             }
2334
2335           /* Don't duplicate cases already considered.  */
2336           for (i = 17; i < 24; i++)
2337             {
2338               if (((temp1 | (temp1 >> i)) == remainder)
2339                   && !const_ok_for_arm (temp1))
2340                 {
2341                   rtx new_src = (subtargets
2342                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2343                                  : target);
2344                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2345                                             source, subtargets, generate);
2346                   source = new_src;
2347                   if (generate)
2348                     emit_constant_insn
2349                       (cond,
2350                        gen_rtx_SET (VOIDmode, target,
2351                                     gen_rtx_IOR
2352                                     (mode,
2353                                      gen_rtx_LSHIFTRT (mode, source,
2354                                                        GEN_INT (i)),
2355                                      source)));
2356                   return insns + 1;
2357                 }
2358             }
2359         }
2360       break;
2361
2362     case IOR:
2363     case XOR:
2364       /* If we have IOR or XOR, and the constant can be loaded in a
2365          single instruction, and we can find a temporary to put it in,
2366          then this can be done in two instructions instead of 3-4.  */
2367       if (subtargets
2368           /* TARGET can't be NULL if SUBTARGETS is 0 */
2369           || (reload_completed && !reg_mentioned_p (target, source)))
2370         {
2371           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2372             {
2373               if (generate)
2374                 {
2375                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2376
2377                   emit_constant_insn (cond,
2378                                       gen_rtx_SET (VOIDmode, sub,
2379                                                    GEN_INT (val)));
2380                   emit_constant_insn (cond,
2381                                       gen_rtx_SET (VOIDmode, target,
2382                                                    gen_rtx_fmt_ee (code, mode,
2383                                                                    source, sub)));
2384                 }
2385               return 2;
2386             }
2387         }
2388
2389       if (code == XOR)
2390         break;
2391
2392       if (set_sign_bit_copies > 8
2393           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2394         {
2395           if (generate)
2396             {
2397               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2398               rtx shift = GEN_INT (set_sign_bit_copies);
2399
2400               emit_constant_insn
2401                 (cond,
2402                  gen_rtx_SET (VOIDmode, sub,
2403                               gen_rtx_NOT (mode,
2404                                            gen_rtx_ASHIFT (mode,
2405                                                            source,
2406                                                            shift))));
2407               emit_constant_insn
2408                 (cond,
2409                  gen_rtx_SET (VOIDmode, target,
2410                               gen_rtx_NOT (mode,
2411                                            gen_rtx_LSHIFTRT (mode, sub,
2412                                                              shift))));
2413             }
2414           return 2;
2415         }
2416
2417       if (set_zero_bit_copies > 8
2418           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2419         {
2420           if (generate)
2421             {
2422               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2423               rtx shift = GEN_INT (set_zero_bit_copies);
2424
2425               emit_constant_insn
2426                 (cond,
2427                  gen_rtx_SET (VOIDmode, sub,
2428                               gen_rtx_NOT (mode,
2429                                            gen_rtx_LSHIFTRT (mode,
2430                                                              source,
2431                                                              shift))));
2432               emit_constant_insn
2433                 (cond,
2434                  gen_rtx_SET (VOIDmode, target,
2435                               gen_rtx_NOT (mode,
2436                                            gen_rtx_ASHIFT (mode, sub,
2437                                                            shift))));
2438             }
2439           return 2;
2440         }
2441
2442       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2443         {
2444           if (generate)
2445             {
2446               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2447               emit_constant_insn (cond,
2448                                   gen_rtx_SET (VOIDmode, sub,
2449                                                gen_rtx_NOT (mode, source)));
2450               source = sub;
2451               if (subtargets)
2452                 sub = gen_reg_rtx (mode);
2453               emit_constant_insn (cond,
2454                                   gen_rtx_SET (VOIDmode, sub,
2455                                                gen_rtx_AND (mode, source,
2456                                                             GEN_INT (temp1))));
2457               emit_constant_insn (cond,
2458                                   gen_rtx_SET (VOIDmode, target,
2459                                                gen_rtx_NOT (mode, sub)));
2460             }
2461           return 3;
2462         }
2463       break;
2464
2465     case AND:
2466       /* See if two shifts will do 2 or more insn's worth of work.  */
2467       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2468         {
2469           HOST_WIDE_INT shift_mask = ((0xffffffff
2470                                        << (32 - clear_sign_bit_copies))
2471                                       & 0xffffffff);
2472
2473           if ((remainder | shift_mask) != 0xffffffff)
2474             {
2475               if (generate)
2476                 {
2477                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2478                   insns = arm_gen_constant (AND, mode, cond,
2479                                             remainder | shift_mask,
2480                                             new_src, source, subtargets, 1);
2481                   source = new_src;
2482                 }
2483               else
2484                 {
2485                   rtx targ = subtargets ? NULL_RTX : target;
2486                   insns = arm_gen_constant (AND, mode, cond,
2487                                             remainder | shift_mask,
2488                                             targ, source, subtargets, 0);
2489                 }
2490             }
2491
2492           if (generate)
2493             {
2494               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2495               rtx shift = GEN_INT (clear_sign_bit_copies);
2496
2497               emit_insn (gen_ashlsi3 (new_src, source, shift));
2498               emit_insn (gen_lshrsi3 (target, new_src, shift));
2499             }
2500
2501           return insns + 2;
2502         }
2503
2504       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2505         {
2506           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2507
2508           if ((remainder | shift_mask) != 0xffffffff)
2509             {
2510               if (generate)
2511                 {
2512                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2513
2514                   insns = arm_gen_constant (AND, mode, cond,
2515                                             remainder | shift_mask,
2516                                             new_src, source, subtargets, 1);
2517                   source = new_src;
2518                 }
2519               else
2520                 {
2521                   rtx targ = subtargets ? NULL_RTX : target;
2522
2523                   insns = arm_gen_constant (AND, mode, cond,
2524                                             remainder | shift_mask,
2525                                             targ, source, subtargets, 0);
2526                 }
2527             }
2528
2529           if (generate)
2530             {
2531               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2532               rtx shift = GEN_INT (clear_zero_bit_copies);
2533
2534               emit_insn (gen_lshrsi3 (new_src, source, shift));
2535               emit_insn (gen_ashlsi3 (target, new_src, shift));
2536             }
2537
2538           return insns + 2;
2539         }
2540
2541       break;
2542
2543     default:
2544       break;
2545     }
2546
2547   for (i = 0; i < 32; i++)
2548     if (remainder & (1 << i))
2549       num_bits_set++;
2550
2551   if (code == AND || (can_invert && num_bits_set > 16))
2552     remainder = (~remainder) & 0xffffffff;
2553   else if (code == PLUS && num_bits_set > 16)
2554     remainder = (-remainder) & 0xffffffff;
2555   else
2556     {
2557       can_invert = 0;
2558       can_negate = 0;
2559     }
2560
2561   /* Now try and find a way of doing the job in either two or three
2562      instructions.
2563      We start by looking for the largest block of zeros that are aligned on
2564      a 2-bit boundary, we then fill up the temps, wrapping around to the
2565      top of the word when we drop off the bottom.
2566      In the worst case this code should produce no more than four insns.
2567      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2568      best place to start.  */
2569
2570   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2571      the same.  */
2572   {
2573     int best_start = 0;
2574     if (!TARGET_THUMB2)
2575       {
2576         int best_consecutive_zeros = 0;
2577
2578         for (i = 0; i < 32; i += 2)
2579           {
2580             int consecutive_zeros = 0;
2581
2582             if (!(remainder & (3 << i)))
2583               {
2584                 while ((i < 32) && !(remainder & (3 << i)))
2585                   {
2586                     consecutive_zeros += 2;
2587                     i += 2;
2588                   }
2589                 if (consecutive_zeros > best_consecutive_zeros)
2590                   {
2591                     best_consecutive_zeros = consecutive_zeros;
2592                     best_start = i - consecutive_zeros;
2593                   }
2594                 i -= 2;
2595               }
2596           }
2597
2598         /* So long as it won't require any more insns to do so, it's
2599            desirable to emit a small constant (in bits 0...9) in the last
2600            insn.  This way there is more chance that it can be combined with
2601            a later addressing insn to form a pre-indexed load or store
2602            operation.  Consider:
2603
2604                    *((volatile int *)0xe0000100) = 1;
2605                    *((volatile int *)0xe0000110) = 2;
2606
2607            We want this to wind up as:
2608
2609                     mov rA, #0xe0000000
2610                     mov rB, #1
2611                     str rB, [rA, #0x100]
2612                     mov rB, #2
2613                     str rB, [rA, #0x110]
2614
2615            rather than having to synthesize both large constants from scratch.
2616
2617            Therefore, we calculate how many insns would be required to emit
2618            the constant starting from `best_start', and also starting from
2619            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2620            yield a shorter sequence, we may as well use zero.  */
2621         if (best_start != 0
2622             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2623             && (count_insns_for_constant (remainder, 0) <=
2624                 count_insns_for_constant (remainder, best_start)))
2625           best_start = 0;
2626       }
2627
2628     /* Now start emitting the insns.  */
2629     i = best_start;
2630     do
2631       {
2632         int end;
2633
2634         if (i <= 0)
2635           i += 32;
2636         if (remainder & (3 << (i - 2)))
2637           {
2638             end = i - 8;
2639             if (end < 0)
2640               end += 32;
2641             temp1 = remainder & ((0x0ff << end)
2642                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2643             remainder &= ~temp1;
2644
2645             if (generate)
2646               {
2647                 rtx new_src, temp1_rtx;
2648
2649                 if (code == SET || code == MINUS)
2650                   {
2651                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2652                     if (can_invert && code != MINUS)
2653                       temp1 = ~temp1;
2654                   }
2655                 else
2656                   {
2657                     if (remainder && subtargets)
2658                       new_src = gen_reg_rtx (mode);
2659                     else
2660                       new_src = target;
2661                     if (can_invert)
2662                       temp1 = ~temp1;
2663                     else if (can_negate)
2664                       temp1 = -temp1;
2665                   }
2666
2667                 temp1 = trunc_int_for_mode (temp1, mode);
2668                 temp1_rtx = GEN_INT (temp1);
2669
2670                 if (code == SET)
2671                   ;
2672                 else if (code == MINUS)
2673                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2674                 else
2675                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2676
2677                 emit_constant_insn (cond,
2678                                     gen_rtx_SET (VOIDmode, new_src,
2679                                                  temp1_rtx));
2680                 source = new_src;
2681               }
2682
2683             if (code == SET)
2684               {
2685                 can_invert = 0;
2686                 code = PLUS;
2687               }
2688             else if (code == MINUS)
2689               code = PLUS;
2690
2691             insns++;
2692             if (TARGET_ARM)
2693               i -= 6;
2694             else
2695               i -= 7;
2696           }
2697         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2698            shifts.  */
2699         if (TARGET_ARM)
2700           i -= 2;
2701         else
2702           i--;
2703       }
2704     while (remainder);
2705   }
2706
2707   return insns;
2708 }
2709
2710 /* Canonicalize a comparison so that we are more likely to recognize it.
2711    This can be done for a few constant compares, where we can make the
2712    immediate value easier to load.  */
2713
2714 enum rtx_code
2715 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2716                              rtx * op1)
2717 {
2718   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2719   unsigned HOST_WIDE_INT maxval;
2720   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2721
2722   switch (code)
2723     {
2724     case EQ:
2725     case NE:
2726       return code;
2727
2728     case GT:
2729     case LE:
2730       if (i != maxval
2731           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2732         {
2733           *op1 = GEN_INT (i + 1);
2734           return code == GT ? GE : LT;
2735         }
2736       break;
2737
2738     case GE:
2739     case LT:
2740       if (i != ~maxval
2741           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2742         {
2743           *op1 = GEN_INT (i - 1);
2744           return code == GE ? GT : LE;
2745         }
2746       break;
2747
2748     case GTU:
2749     case LEU:
2750       if (i != ~((unsigned HOST_WIDE_INT) 0)
2751           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2752         {
2753           *op1 = GEN_INT (i + 1);
2754           return code == GTU ? GEU : LTU;
2755         }
2756       break;
2757
2758     case GEU:
2759     case LTU:
2760       if (i != 0
2761           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2762         {
2763           *op1 = GEN_INT (i - 1);
2764           return code == GEU ? GTU : LEU;
2765         }
2766       break;
2767
2768     default:
2769       gcc_unreachable ();
2770     }
2771
2772   return code;
2773 }
2774
2775
2776 /* Define how to find the value returned by a function.  */
2777
2778 rtx
2779 arm_function_value(const_tree type, const_tree func ATTRIBUTE_UNUSED)
2780 {
2781   enum machine_mode mode;
2782   int unsignedp ATTRIBUTE_UNUSED;
2783   rtx r ATTRIBUTE_UNUSED;
2784
2785   mode = TYPE_MODE (type);
2786   /* Promote integer types.  */
2787   if (INTEGRAL_TYPE_P (type))
2788     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2789
2790   /* Promotes small structs returned in a register to full-word size
2791      for big-endian AAPCS.  */
2792   if (arm_return_in_msb (type))
2793     {
2794       HOST_WIDE_INT size = int_size_in_bytes (type);
2795       if (size % UNITS_PER_WORD != 0)
2796         {
2797           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2798           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2799         }
2800     }
2801
2802   return LIBCALL_VALUE(mode);
2803 }
2804
2805 /* Determine the amount of memory needed to store the possible return
2806    registers of an untyped call.  */
2807 int
2808 arm_apply_result_size (void)
2809 {
2810   int size = 16;
2811
2812   if (TARGET_ARM)
2813     {
2814       if (TARGET_HARD_FLOAT_ABI)
2815         {
2816           if (TARGET_FPA)
2817             size += 12;
2818           if (TARGET_MAVERICK)
2819             size += 8;
2820         }
2821       if (TARGET_IWMMXT_ABI)
2822         size += 8;
2823     }
2824
2825   return size;
2826 }
2827
2828 /* Decide whether a type should be returned in memory (true)
2829    or in a register (false).  This is called as the target hook
2830    TARGET_RETURN_IN_MEMORY.  */
2831 static bool
2832 arm_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
2833 {
2834   HOST_WIDE_INT size;
2835
2836   size = int_size_in_bytes (type);
2837
2838   /* Vector values should be returned using ARM registers, not memory (unless
2839      they're over 16 bytes, which will break since we only have four
2840      call-clobbered registers to play with).  */
2841   if (TREE_CODE (type) == VECTOR_TYPE)
2842     return (size < 0 || size > (4 * UNITS_PER_WORD));
2843
2844   if (!AGGREGATE_TYPE_P (type) &&
2845       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2846     /* All simple types are returned in registers.
2847        For AAPCS, complex types are treated the same as aggregates.  */
2848     return 0;
2849
2850   if (arm_abi != ARM_ABI_APCS)
2851     {
2852       /* ATPCS and later return aggregate types in memory only if they are
2853          larger than a word (or are variable size).  */
2854       return (size < 0 || size > UNITS_PER_WORD);
2855     }
2856
2857   /* For the arm-wince targets we choose to be compatible with Microsoft's
2858      ARM and Thumb compilers, which always return aggregates in memory.  */
2859 #ifndef ARM_WINCE
2860   /* All structures/unions bigger than one word are returned in memory.
2861      Also catch the case where int_size_in_bytes returns -1.  In this case
2862      the aggregate is either huge or of variable size, and in either case
2863      we will want to return it via memory and not in a register.  */
2864   if (size < 0 || size > UNITS_PER_WORD)
2865     return 1;
2866
2867   if (TREE_CODE (type) == RECORD_TYPE)
2868     {
2869       tree field;
2870
2871       /* For a struct the APCS says that we only return in a register
2872          if the type is 'integer like' and every addressable element
2873          has an offset of zero.  For practical purposes this means
2874          that the structure can have at most one non bit-field element
2875          and that this element must be the first one in the structure.  */
2876
2877       /* Find the first field, ignoring non FIELD_DECL things which will
2878          have been created by C++.  */
2879       for (field = TYPE_FIELDS (type);
2880            field && TREE_CODE (field) != FIELD_DECL;
2881            field = TREE_CHAIN (field))
2882         continue;
2883
2884       if (field == NULL)
2885         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2886
2887       /* Check that the first field is valid for returning in a register.  */
2888
2889       /* ... Floats are not allowed */
2890       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2891         return 1;
2892
2893       /* ... Aggregates that are not themselves valid for returning in
2894          a register are not allowed.  */
2895       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
2896         return 1;
2897
2898       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2899          since they are not addressable.  */
2900       for (field = TREE_CHAIN (field);
2901            field;
2902            field = TREE_CHAIN (field))
2903         {
2904           if (TREE_CODE (field) != FIELD_DECL)
2905             continue;
2906
2907           if (!DECL_BIT_FIELD_TYPE (field))
2908             return 1;
2909         }
2910
2911       return 0;
2912     }
2913
2914   if (TREE_CODE (type) == UNION_TYPE)
2915     {
2916       tree field;
2917
2918       /* Unions can be returned in registers if every element is
2919          integral, or can be returned in an integer register.  */
2920       for (field = TYPE_FIELDS (type);
2921            field;
2922            field = TREE_CHAIN (field))
2923         {
2924           if (TREE_CODE (field) != FIELD_DECL)
2925             continue;
2926
2927           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2928             return 1;
2929
2930           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
2931             return 1;
2932         }
2933
2934       return 0;
2935     }
2936 #endif /* not ARM_WINCE */
2937
2938   /* Return all other types in memory.  */
2939   return 1;
2940 }
2941
2942 /* Indicate whether or not words of a double are in big-endian order.  */
2943
2944 int
2945 arm_float_words_big_endian (void)
2946 {
2947   if (TARGET_MAVERICK)
2948     return 0;
2949
2950   /* For FPA, float words are always big-endian.  For VFP, floats words
2951      follow the memory system mode.  */
2952
2953   if (TARGET_FPA)
2954     {
2955       return 1;
2956     }
2957
2958   if (TARGET_VFP)
2959     return (TARGET_BIG_END ? 1 : 0);
2960
2961   return 1;
2962 }
2963
2964 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2965    for a call to a function whose data type is FNTYPE.
2966    For a library call, FNTYPE is NULL.  */
2967 void
2968 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2969                           rtx libname  ATTRIBUTE_UNUSED,
2970                           tree fndecl ATTRIBUTE_UNUSED)
2971 {
2972   /* On the ARM, the offset starts at 0.  */
2973   pcum->nregs = 0;
2974   pcum->iwmmxt_nregs = 0;
2975   pcum->can_split = true;
2976
2977   /* Varargs vectors are treated the same as long long.
2978      named_count avoids having to change the way arm handles 'named' */
2979   pcum->named_count = 0;
2980   pcum->nargs = 0;
2981
2982   if (TARGET_REALLY_IWMMXT && fntype)
2983     {
2984       tree fn_arg;
2985
2986       for (fn_arg = TYPE_ARG_TYPES (fntype);
2987            fn_arg;
2988            fn_arg = TREE_CHAIN (fn_arg))
2989         pcum->named_count += 1;
2990
2991       if (! pcum->named_count)
2992         pcum->named_count = INT_MAX;
2993     }
2994 }
2995
2996
2997 /* Return true if mode/type need doubleword alignment.  */
2998 bool
2999 arm_needs_doubleword_align (enum machine_mode mode, tree type)
3000 {
3001   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
3002           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
3003 }
3004
3005
3006 /* Determine where to put an argument to a function.
3007    Value is zero to push the argument on the stack,
3008    or a hard register in which to store the argument.
3009
3010    MODE is the argument's machine mode.
3011    TYPE is the data type of the argument (as a tree).
3012     This is null for libcalls where that information may
3013     not be available.
3014    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3015     the preceding args and about the function being called.
3016    NAMED is nonzero if this argument is a named parameter
3017     (otherwise it is an extra parameter matching an ellipsis).  */
3018
3019 rtx
3020 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3021                   tree type, int named)
3022 {
3023   int nregs;
3024
3025   /* Varargs vectors are treated the same as long long.
3026      named_count avoids having to change the way arm handles 'named' */
3027   if (TARGET_IWMMXT_ABI
3028       && arm_vector_mode_supported_p (mode)
3029       && pcum->named_count > pcum->nargs + 1)
3030     {
3031       if (pcum->iwmmxt_nregs <= 9)
3032         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
3033       else
3034         {
3035           pcum->can_split = false;
3036           return NULL_RTX;
3037         }
3038     }
3039
3040   /* Put doubleword aligned quantities in even register pairs.  */
3041   if (pcum->nregs & 1
3042       && ARM_DOUBLEWORD_ALIGN
3043       && arm_needs_doubleword_align (mode, type))
3044     pcum->nregs++;
3045
3046   if (mode == VOIDmode)
3047     /* Pick an arbitrary value for operand 2 of the call insn.  */
3048     return const0_rtx;
3049
3050   /* Only allow splitting an arg between regs and memory if all preceding
3051      args were allocated to regs.  For args passed by reference we only count
3052      the reference pointer.  */
3053   if (pcum->can_split)
3054     nregs = 1;
3055   else
3056     nregs = ARM_NUM_REGS2 (mode, type);
3057
3058   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
3059     return NULL_RTX;
3060
3061   return gen_rtx_REG (mode, pcum->nregs);
3062 }
3063
3064 static int
3065 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3066                        tree type, bool named ATTRIBUTE_UNUSED)
3067 {
3068   int nregs = pcum->nregs;
3069
3070   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
3071     return 0;
3072
3073   if (NUM_ARG_REGS > nregs
3074       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
3075       && pcum->can_split)
3076     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
3077
3078   return 0;
3079 }
3080
3081 /* Variable sized types are passed by reference.  This is a GCC
3082    extension to the ARM ABI.  */
3083
3084 static bool
3085 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3086                        enum machine_mode mode ATTRIBUTE_UNUSED,
3087                        const_tree type, bool named ATTRIBUTE_UNUSED)
3088 {
3089   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3090 }
3091 \f
3092 /* Encode the current state of the #pragma [no_]long_calls.  */
3093 typedef enum
3094 {
3095   OFF,          /* No #pragma [no_]long_calls is in effect.  */
3096   LONG,         /* #pragma long_calls is in effect.  */
3097   SHORT         /* #pragma no_long_calls is in effect.  */
3098 } arm_pragma_enum;
3099
3100 static arm_pragma_enum arm_pragma_long_calls = OFF;
3101
3102 void
3103 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3104 {
3105   arm_pragma_long_calls = LONG;
3106 }
3107
3108 void
3109 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3110 {
3111   arm_pragma_long_calls = SHORT;
3112 }
3113
3114 void
3115 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3116 {
3117   arm_pragma_long_calls = OFF;
3118 }
3119 \f
3120 /* Table of machine attributes.  */
3121 const struct attribute_spec arm_attribute_table[] =
3122 {
3123   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3124   /* Function calls made to this symbol must be done indirectly, because
3125      it may lie outside of the 26 bit addressing range of a normal function
3126      call.  */
3127   { "long_call",    0, 0, false, true,  true,  NULL },
3128   /* Whereas these functions are always known to reside within the 26 bit
3129      addressing range.  */
3130   { "short_call",   0, 0, false, true,  true,  NULL },
3131   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
3132   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
3133   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
3134   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
3135 #ifdef ARM_PE
3136   /* ARM/PE has three new attributes:
3137      interfacearm - ?
3138      dllexport - for exporting a function/variable that will live in a dll
3139      dllimport - for importing a function/variable from a dll
3140
3141      Microsoft allows multiple declspecs in one __declspec, separating
3142      them with spaces.  We do NOT support this.  Instead, use __declspec
3143      multiple times.
3144   */
3145   { "dllimport",    0, 0, true,  false, false, NULL },
3146   { "dllexport",    0, 0, true,  false, false, NULL },
3147   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
3148 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3149   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
3150   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
3151   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
3152 #endif
3153   { NULL,           0, 0, false, false, false, NULL }
3154 };
3155
3156 /* Handle an attribute requiring a FUNCTION_DECL;
3157    arguments as in struct attribute_spec.handler.  */
3158 static tree
3159 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3160                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3161 {
3162   if (TREE_CODE (*node) != FUNCTION_DECL)
3163     {
3164       warning (OPT_Wattributes, "%qs attribute only applies to functions",
3165                IDENTIFIER_POINTER (name));
3166       *no_add_attrs = true;
3167     }
3168
3169   return NULL_TREE;
3170 }
3171
3172 /* Handle an "interrupt" or "isr" attribute;
3173    arguments as in struct attribute_spec.handler.  */
3174 static tree
3175 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3176                           bool *no_add_attrs)
3177 {
3178   if (DECL_P (*node))
3179     {
3180       if (TREE_CODE (*node) != FUNCTION_DECL)
3181         {
3182           warning (OPT_Wattributes, "%qs attribute only applies to functions",
3183                    IDENTIFIER_POINTER (name));
3184           *no_add_attrs = true;
3185         }
3186       /* FIXME: the argument if any is checked for type attributes;
3187          should it be checked for decl ones?  */
3188     }
3189   else
3190     {
3191       if (TREE_CODE (*node) == FUNCTION_TYPE
3192           || TREE_CODE (*node) == METHOD_TYPE)
3193         {
3194           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3195             {
3196               warning (OPT_Wattributes, "%qs attribute ignored",
3197                        IDENTIFIER_POINTER (name));
3198               *no_add_attrs = true;
3199             }
3200         }
3201       else if (TREE_CODE (*node) == POINTER_TYPE
3202                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3203                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3204                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3205         {
3206           *node = build_variant_type_copy (*node);
3207           TREE_TYPE (*node) = build_type_attribute_variant
3208             (TREE_TYPE (*node),
3209              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3210           *no_add_attrs = true;
3211         }
3212       else
3213         {
3214           /* Possibly pass this attribute on from the type to a decl.  */
3215           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3216                        | (int) ATTR_FLAG_FUNCTION_NEXT
3217                        | (int) ATTR_FLAG_ARRAY_NEXT))
3218             {
3219               *no_add_attrs = true;
3220               return tree_cons (name, args, NULL_TREE);
3221             }
3222           else
3223             {
3224               warning (OPT_Wattributes, "%qs attribute ignored",
3225                        IDENTIFIER_POINTER (name));
3226             }
3227         }
3228     }
3229
3230   return NULL_TREE;
3231 }
3232
3233 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3234 /* Handle the "notshared" attribute.  This attribute is another way of
3235    requesting hidden visibility.  ARM's compiler supports
3236    "__declspec(notshared)"; we support the same thing via an
3237    attribute.  */
3238
3239 static tree
3240 arm_handle_notshared_attribute (tree *node,
3241                                 tree name ATTRIBUTE_UNUSED,
3242                                 tree args ATTRIBUTE_UNUSED,
3243                                 int flags ATTRIBUTE_UNUSED,
3244                                 bool *no_add_attrs)
3245 {
3246   tree decl = TYPE_NAME (*node);
3247
3248   if (decl)
3249     {
3250       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3251       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3252       *no_add_attrs = false;
3253     }
3254   return NULL_TREE;
3255 }
3256 #endif
3257
3258 /* Return 0 if the attributes for two types are incompatible, 1 if they
3259    are compatible, and 2 if they are nearly compatible (which causes a
3260    warning to be generated).  */
3261 static int
3262 arm_comp_type_attributes (const_tree type1, const_tree type2)
3263 {
3264   int l1, l2, s1, s2;
3265
3266   /* Check for mismatch of non-default calling convention.  */
3267   if (TREE_CODE (type1) != FUNCTION_TYPE)
3268     return 1;
3269
3270   /* Check for mismatched call attributes.  */
3271   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3272   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3273   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3274   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3275
3276   /* Only bother to check if an attribute is defined.  */
3277   if (l1 | l2 | s1 | s2)
3278     {
3279       /* If one type has an attribute, the other must have the same attribute.  */
3280       if ((l1 != l2) || (s1 != s2))
3281         return 0;
3282
3283       /* Disallow mixed attributes.  */
3284       if ((l1 & s2) || (l2 & s1))
3285         return 0;
3286     }
3287
3288   /* Check for mismatched ISR attribute.  */
3289   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3290   if (! l1)
3291     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3292   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3293   if (! l2)
3294     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3295   if (l1 != l2)
3296     return 0;
3297
3298   return 1;
3299 }
3300
3301 /*  Assigns default attributes to newly defined type.  This is used to
3302     set short_call/long_call attributes for function types of
3303     functions defined inside corresponding #pragma scopes.  */
3304 static void
3305 arm_set_default_type_attributes (tree type)
3306 {
3307   /* Add __attribute__ ((long_call)) to all functions, when
3308      inside #pragma long_calls or __attribute__ ((short_call)),
3309      when inside #pragma no_long_calls.  */
3310   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3311     {
3312       tree type_attr_list, attr_name;
3313       type_attr_list = TYPE_ATTRIBUTES (type);
3314
3315       if (arm_pragma_long_calls == LONG)
3316         attr_name = get_identifier ("long_call");
3317       else if (arm_pragma_long_calls == SHORT)
3318         attr_name = get_identifier ("short_call");
3319       else
3320         return;
3321
3322       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3323       TYPE_ATTRIBUTES (type) = type_attr_list;
3324     }
3325 }
3326 \f
3327 /* Return true if DECL is known to be linked into section SECTION.  */
3328
3329 static bool
3330 arm_function_in_section_p (tree decl, section *section)
3331 {
3332   /* We can only be certain about functions defined in the same
3333      compilation unit.  */
3334   if (!TREE_STATIC (decl))
3335     return false;
3336
3337   /* Make sure that SYMBOL always binds to the definition in this
3338      compilation unit.  */
3339   if (!targetm.binds_local_p (decl))
3340     return false;
3341
3342   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3343   if (!DECL_SECTION_NAME (decl))
3344     {
3345       /* Make sure that we will not create a unique section for DECL.  */
3346       if (flag_function_sections || DECL_ONE_ONLY (decl))
3347         return false;
3348     }
3349
3350   return function_section (decl) == section;
3351 }
3352
3353 /* Return nonzero if a 32-bit "long_call" should be generated for
3354    a call from the current function to DECL.  We generate a long_call
3355    if the function:
3356
3357         a.  has an __attribute__((long call))
3358      or b.  is within the scope of a #pragma long_calls
3359      or c.  the -mlong-calls command line switch has been specified
3360
3361    However we do not generate a long call if the function:
3362
3363         d.  has an __attribute__ ((short_call))
3364      or e.  is inside the scope of a #pragma no_long_calls
3365      or f.  is defined in the same section as the current function.  */
3366
3367 bool
3368 arm_is_long_call_p (tree decl)
3369 {
3370   tree attrs;
3371
3372   if (!decl)
3373     return TARGET_LONG_CALLS;
3374
3375   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3376   if (lookup_attribute ("short_call", attrs))
3377     return false;
3378
3379   /* For "f", be conservative, and only cater for cases in which the
3380      whole of the current function is placed in the same section.  */
3381   if (!flag_reorder_blocks_and_partition
3382       && TREE_CODE (decl) == FUNCTION_DECL
3383       && arm_function_in_section_p (decl, current_function_section ()))
3384     return false;
3385
3386   if (lookup_attribute ("long_call", attrs))
3387     return true;
3388
3389   return TARGET_LONG_CALLS;
3390 }
3391
3392 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3393 static bool
3394 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3395 {
3396   unsigned long func_type;
3397
3398   if (cfun->machine->sibcall_blocked)
3399     return false;
3400
3401   /* Never tailcall something for which we have no decl, or if we
3402      are in Thumb mode.  */
3403   if (decl == NULL || TARGET_THUMB)
3404     return false;
3405
3406   /* The PIC register is live on entry to VxWorks PLT entries, so we
3407      must make the call before restoring the PIC register.  */
3408   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3409     return false;
3410
3411   /* Cannot tail-call to long calls, since these are out of range of
3412      a branch instruction.  */
3413   if (arm_is_long_call_p (decl))
3414     return false;
3415
3416   /* If we are interworking and the function is not declared static
3417      then we can't tail-call it unless we know that it exists in this
3418      compilation unit (since it might be a Thumb routine).  */
3419   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3420     return false;
3421
3422   func_type = arm_current_func_type ();
3423   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3424   if (IS_INTERRUPT (func_type))
3425     return false;
3426
3427   /* Never tailcall if function may be called with a misaligned SP.  */
3428   if (IS_STACKALIGN (func_type))
3429     return false;
3430
3431   /* Everything else is ok.  */
3432   return true;
3433 }
3434
3435 \f
3436 /* Addressing mode support functions.  */
3437
3438 /* Return nonzero if X is a legitimate immediate operand when compiling
3439    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3440 int
3441 legitimate_pic_operand_p (rtx x)
3442 {
3443   if (GET_CODE (x) == SYMBOL_REF
3444       || (GET_CODE (x) == CONST
3445           && GET_CODE (XEXP (x, 0)) == PLUS
3446           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3447     return 0;
3448
3449   return 1;
3450 }
3451
3452 /* Record that the current function needs a PIC register.  Initialize
3453    cfun->machine->pic_reg if we have not already done so.  */
3454
3455 static void
3456 require_pic_register (void)
3457 {
3458   /* A lot of the logic here is made obscure by the fact that this
3459      routine gets called as part of the rtx cost estimation process.
3460      We don't want those calls to affect any assumptions about the real
3461      function; and further, we can't call entry_of_function() until we
3462      start the real expansion process.  */
3463   if (!crtl->uses_pic_offset_table)
3464     {
3465       gcc_assert (can_create_pseudo_p ());
3466       if (arm_pic_register != INVALID_REGNUM)
3467         {
3468           cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3469
3470           /* Play games to avoid marking the function as needing pic
3471              if we are being called as part of the cost-estimation
3472              process.  */
3473           if (current_ir_type () != IR_GIMPLE)
3474             crtl->uses_pic_offset_table = 1;
3475         }
3476       else
3477         {
3478           rtx seq;
3479
3480           cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3481
3482           /* Play games to avoid marking the function as needing pic
3483              if we are being called as part of the cost-estimation
3484              process.  */
3485           if (current_ir_type () != IR_GIMPLE)
3486             {
3487               crtl->uses_pic_offset_table = 1;
3488               start_sequence ();
3489
3490               arm_load_pic_register (0UL);
3491
3492               seq = get_insns ();
3493               end_sequence ();
3494               emit_insn_after (seq, entry_of_function ());
3495             }
3496         }
3497     }
3498 }
3499
3500 rtx
3501 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3502 {
3503   if (GET_CODE (orig) == SYMBOL_REF
3504       || GET_CODE (orig) == LABEL_REF)
3505     {
3506       rtx pic_ref, address;
3507       rtx insn;
3508       int subregs = 0;
3509
3510       /* If this function doesn't have a pic register, create one now.  */
3511       require_pic_register ();
3512
3513       if (reg == 0)
3514         {
3515           gcc_assert (can_create_pseudo_p ());
3516           reg = gen_reg_rtx (Pmode);
3517
3518           subregs = 1;
3519         }
3520
3521       if (subregs)
3522         address = gen_reg_rtx (Pmode);
3523       else
3524         address = reg;
3525
3526       if (TARGET_ARM)
3527         emit_insn (gen_pic_load_addr_arm (address, orig));
3528       else if (TARGET_THUMB2)
3529         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3530       else /* TARGET_THUMB1 */
3531         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3532
3533       /* VxWorks does not impose a fixed gap between segments; the run-time
3534          gap can be different from the object-file gap.  We therefore can't
3535          use GOTOFF unless we are absolutely sure that the symbol is in the
3536          same segment as the GOT.  Unfortunately, the flexibility of linker
3537          scripts means that we can't be sure of that in general, so assume
3538          that GOTOFF is never valid on VxWorks.  */
3539       if ((GET_CODE (orig) == LABEL_REF
3540            || (GET_CODE (orig) == SYMBOL_REF &&
3541                SYMBOL_REF_LOCAL_P (orig)))
3542           && NEED_GOT_RELOC
3543           && !TARGET_VXWORKS_RTP)
3544         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3545       else
3546         {
3547           pic_ref = gen_const_mem (Pmode,
3548                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3549                                                  address));
3550         }
3551
3552       insn = emit_move_insn (reg, pic_ref);
3553
3554       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3555          by loop.  */
3556       set_unique_reg_note (insn, REG_EQUAL, orig);
3557
3558       return reg;
3559     }
3560   else if (GET_CODE (orig) == CONST)
3561     {
3562       rtx base, offset;
3563
3564       if (GET_CODE (XEXP (orig, 0)) == PLUS
3565           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3566         return orig;
3567
3568       /* Handle the case where we have: const (UNSPEC_TLS).  */
3569       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3570           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3571         return orig;
3572
3573       /* Handle the case where we have:
3574          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
3575          CONST_INT.  */
3576       if (GET_CODE (XEXP (orig, 0)) == PLUS
3577           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
3578           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
3579         {
3580           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
3581           return orig;
3582         }
3583
3584       if (reg == 0)
3585         {
3586           gcc_assert (can_create_pseudo_p ());
3587           reg = gen_reg_rtx (Pmode);
3588         }
3589
3590       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3591
3592       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3593       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3594                                        base == reg ? 0 : reg);
3595
3596       if (GET_CODE (offset) == CONST_INT)
3597         {
3598           /* The base register doesn't really matter, we only want to
3599              test the index for the appropriate mode.  */
3600           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3601             {
3602               gcc_assert (can_create_pseudo_p ());
3603               offset = force_reg (Pmode, offset);
3604             }
3605
3606           if (GET_CODE (offset) == CONST_INT)
3607             return plus_constant (base, INTVAL (offset));
3608         }
3609
3610       if (GET_MODE_SIZE (mode) > 4
3611           && (GET_MODE_CLASS (mode) == MODE_INT
3612               || TARGET_SOFT_FLOAT))
3613         {
3614           emit_insn (gen_addsi3 (reg, base, offset));
3615           return reg;
3616         }
3617
3618       return gen_rtx_PLUS (Pmode, base, offset);
3619     }
3620
3621   return orig;
3622 }
3623
3624
3625 /* Find a spare register to use during the prolog of a function.  */
3626
3627 static int
3628 thumb_find_work_register (unsigned long pushed_regs_mask)
3629 {
3630   int reg;
3631
3632   /* Check the argument registers first as these are call-used.  The
3633      register allocation order means that sometimes r3 might be used
3634      but earlier argument registers might not, so check them all.  */
3635   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3636     if (!df_regs_ever_live_p (reg))
3637       return reg;
3638
3639   /* Before going on to check the call-saved registers we can try a couple
3640      more ways of deducing that r3 is available.  The first is when we are
3641      pushing anonymous arguments onto the stack and we have less than 4
3642      registers worth of fixed arguments(*).  In this case r3 will be part of
3643      the variable argument list and so we can be sure that it will be
3644      pushed right at the start of the function.  Hence it will be available
3645      for the rest of the prologue.
3646      (*): ie crtl->args.pretend_args_size is greater than 0.  */
3647   if (cfun->machine->uses_anonymous_args
3648       && crtl->args.pretend_args_size > 0)
3649     return LAST_ARG_REGNUM;
3650
3651   /* The other case is when we have fixed arguments but less than 4 registers
3652      worth.  In this case r3 might be used in the body of the function, but
3653      it is not being used to convey an argument into the function.  In theory
3654      we could just check crtl->args.size to see how many bytes are
3655      being passed in argument registers, but it seems that it is unreliable.
3656      Sometimes it will have the value 0 when in fact arguments are being
3657      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3658      check the args_info.nregs field as well.  The problem with this field is
3659      that it makes no allowances for arguments that are passed to the
3660      function but which are not used.  Hence we could miss an opportunity
3661      when a function has an unused argument in r3.  But it is better to be
3662      safe than to be sorry.  */
3663   if (! cfun->machine->uses_anonymous_args
3664       && crtl->args.size >= 0
3665       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3666       && crtl->args.info.nregs < 4)
3667     return LAST_ARG_REGNUM;
3668
3669   /* Otherwise look for a call-saved register that is going to be pushed.  */
3670   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3671     if (pushed_regs_mask & (1 << reg))
3672       return reg;
3673
3674   if (TARGET_THUMB2)
3675     {
3676       /* Thumb-2 can use high regs.  */
3677       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3678         if (pushed_regs_mask & (1 << reg))
3679           return reg;
3680     }
3681   /* Something went wrong - thumb_compute_save_reg_mask()
3682      should have arranged for a suitable register to be pushed.  */
3683   gcc_unreachable ();
3684 }
3685
3686 static GTY(()) int pic_labelno;
3687
3688 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3689    low register.  */
3690
3691 void
3692 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3693 {
3694   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
3695
3696   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3697     return;
3698
3699   gcc_assert (flag_pic);
3700
3701   pic_reg = cfun->machine->pic_reg;
3702   if (TARGET_VXWORKS_RTP)
3703     {
3704       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3705       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3706       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3707
3708       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
3709
3710       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3711       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
3712     }
3713   else
3714     {
3715       /* We use an UNSPEC rather than a LABEL_REF because this label
3716          never appears in the code stream.  */
3717
3718       labelno = GEN_INT (pic_labelno++);
3719       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3720       l1 = gen_rtx_CONST (VOIDmode, l1);
3721
3722       /* On the ARM the PC register contains 'dot + 8' at the time of the
3723          addition, on the Thumb it is 'dot + 4'.  */
3724       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
3725       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
3726                                 UNSPEC_GOTSYM_OFF);
3727       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3728
3729       if (TARGET_ARM)
3730         {
3731           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3732           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
3733         }
3734       else if (TARGET_THUMB2)
3735         {
3736           /* Thumb-2 only allows very limited access to the PC.  Calculate the
3737              address in a temporary register.  */
3738           if (arm_pic_register != INVALID_REGNUM)
3739             {
3740               pic_tmp = gen_rtx_REG (SImode,
3741                                      thumb_find_work_register (saved_regs));
3742             }
3743           else
3744             {
3745               gcc_assert (can_create_pseudo_p ());
3746               pic_tmp = gen_reg_rtx (Pmode);
3747             }
3748
3749           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
3750           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3751           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
3752         }
3753       else /* TARGET_THUMB1 */
3754         {
3755           if (arm_pic_register != INVALID_REGNUM
3756               && REGNO (pic_reg) > LAST_LO_REGNUM)
3757             {
3758               /* We will have pushed the pic register, so we should always be
3759                  able to find a work register.  */
3760               pic_tmp = gen_rtx_REG (SImode,
3761                                      thumb_find_work_register (saved_regs));
3762               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3763               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3764             }
3765           else
3766             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
3767           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
3768         }
3769     }
3770
3771   /* Need to emit this whether or not we obey regdecls,
3772      since setjmp/longjmp can cause life info to screw up.  */
3773   emit_use (pic_reg);
3774 }
3775
3776
3777 /* Return nonzero if X is valid as an ARM state addressing register.  */
3778 static int
3779 arm_address_register_rtx_p (rtx x, int strict_p)
3780 {
3781   int regno;
3782
3783   if (GET_CODE (x) != REG)
3784     return 0;
3785
3786   regno = REGNO (x);
3787
3788   if (strict_p)
3789     return ARM_REGNO_OK_FOR_BASE_P (regno);
3790
3791   return (regno <= LAST_ARM_REGNUM
3792           || regno >= FIRST_PSEUDO_REGISTER
3793           || regno == FRAME_POINTER_REGNUM
3794           || regno == ARG_POINTER_REGNUM);
3795 }
3796
3797 /* Return TRUE if this rtx is the difference of a symbol and a label,
3798    and will reduce to a PC-relative relocation in the object file.
3799    Expressions like this can be left alone when generating PIC, rather
3800    than forced through the GOT.  */
3801 static int
3802 pcrel_constant_p (rtx x)
3803 {
3804   if (GET_CODE (x) == MINUS)
3805     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3806
3807   return FALSE;
3808 }
3809
3810 /* Return nonzero if X is a valid ARM state address operand.  */
3811 int
3812 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3813                           int strict_p)
3814 {
3815   bool use_ldrd;
3816   enum rtx_code code = GET_CODE (x);
3817
3818   if (arm_address_register_rtx_p (x, strict_p))
3819     return 1;
3820
3821   use_ldrd = (TARGET_LDRD
3822               && (mode == DImode
3823                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3824
3825   if (code == POST_INC || code == PRE_DEC
3826       || ((code == PRE_INC || code == POST_DEC)
3827           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3828     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3829
3830   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3831            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3832            && GET_CODE (XEXP (x, 1)) == PLUS
3833            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3834     {
3835       rtx addend = XEXP (XEXP (x, 1), 1);
3836
3837       /* Don't allow ldrd post increment by register because it's hard
3838          to fixup invalid register choices.  */
3839       if (use_ldrd
3840           && GET_CODE (x) == POST_MODIFY
3841           && GET_CODE (addend) == REG)
3842         return 0;
3843
3844       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3845               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3846     }
3847
3848   /* After reload constants split into minipools will have addresses
3849      from a LABEL_REF.  */
3850   else if (reload_completed
3851            && (code == LABEL_REF
3852                || (code == CONST
3853                    && GET_CODE (XEXP (x, 0)) == PLUS
3854                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3855                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3856     return 1;
3857
3858   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
3859     return 0;
3860
3861   else if (code == PLUS)
3862     {
3863       rtx xop0 = XEXP (x, 0);
3864       rtx xop1 = XEXP (x, 1);
3865
3866       return ((arm_address_register_rtx_p (xop0, strict_p)
3867                && GET_CODE(xop1) == CONST_INT
3868                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3869               || (arm_address_register_rtx_p (xop1, strict_p)
3870                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3871     }
3872
3873 #if 0
3874   /* Reload currently can't handle MINUS, so disable this for now */
3875   else if (GET_CODE (x) == MINUS)
3876     {
3877       rtx xop0 = XEXP (x, 0);
3878       rtx xop1 = XEXP (x, 1);
3879
3880       return (arm_address_register_rtx_p (xop0, strict_p)
3881               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3882     }
3883 #endif
3884
3885   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3886            && code == SYMBOL_REF
3887            && CONSTANT_POOL_ADDRESS_P (x)
3888            && ! (flag_pic
3889                  && symbol_mentioned_p (get_pool_constant (x))
3890                  && ! pcrel_constant_p (get_pool_constant (x))))
3891     return 1;
3892
3893   return 0;
3894 }
3895
3896 /* Return nonzero if X is a valid Thumb-2 address operand.  */
3897 int
3898 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3899 {
3900   bool use_ldrd;
3901   enum rtx_code code = GET_CODE (x);
3902   
3903   if (arm_address_register_rtx_p (x, strict_p))
3904     return 1;
3905
3906   use_ldrd = (TARGET_LDRD
3907               && (mode == DImode
3908                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3909
3910   if (code == POST_INC || code == PRE_DEC
3911       || ((code == PRE_INC || code == POST_DEC)
3912           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3913     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3914
3915   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3916            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3917            && GET_CODE (XEXP (x, 1)) == PLUS
3918            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3919     {
3920       /* Thumb-2 only has autoincrement by constant.  */
3921       rtx addend = XEXP (XEXP (x, 1), 1);
3922       HOST_WIDE_INT offset;
3923
3924       if (GET_CODE (addend) != CONST_INT)
3925         return 0;
3926
3927       offset = INTVAL(addend);
3928       if (GET_MODE_SIZE (mode) <= 4)
3929         return (offset > -256 && offset < 256);
3930       
3931       return (use_ldrd && offset > -1024 && offset < 1024
3932               && (offset & 3) == 0);
3933     }
3934
3935   /* After reload constants split into minipools will have addresses
3936      from a LABEL_REF.  */
3937   else if (reload_completed
3938            && (code == LABEL_REF
3939                || (code == CONST
3940                    && GET_CODE (XEXP (x, 0)) == PLUS
3941                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3942                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3943     return 1;
3944
3945   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
3946     return 0;
3947
3948   else if (code == PLUS)
3949     {
3950       rtx xop0 = XEXP (x, 0);
3951       rtx xop1 = XEXP (x, 1);
3952
3953       return ((arm_address_register_rtx_p (xop0, strict_p)
3954                && thumb2_legitimate_index_p (mode, xop1, strict_p))
3955               || (arm_address_register_rtx_p (xop1, strict_p)
3956                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3957     }
3958
3959   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3960            && code == SYMBOL_REF
3961            && CONSTANT_POOL_ADDRESS_P (x)
3962            && ! (flag_pic
3963                  && symbol_mentioned_p (get_pool_constant (x))
3964                  && ! pcrel_constant_p (get_pool_constant (x))))
3965     return 1;
3966
3967   return 0;
3968 }
3969
3970 /* Return nonzero if INDEX is valid for an address index operand in
3971    ARM state.  */
3972 static int
3973 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3974                         int strict_p)
3975 {
3976   HOST_WIDE_INT range;
3977   enum rtx_code code = GET_CODE (index);
3978
3979   /* Standard coprocessor addressing modes.  */
3980   if (TARGET_HARD_FLOAT
3981       && (TARGET_FPA || TARGET_MAVERICK)
3982       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3983           || (TARGET_MAVERICK && mode == DImode)))
3984     return (code == CONST_INT && INTVAL (index) < 1024
3985             && INTVAL (index) > -1024
3986             && (INTVAL (index) & 3) == 0);
3987
3988   if (TARGET_NEON
3989       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
3990     return (code == CONST_INT
3991             && INTVAL (index) < 1016
3992             && INTVAL (index) > -1024
3993             && (INTVAL (index) & 3) == 0);
3994
3995   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3996     return (code == CONST_INT
3997             && INTVAL (index) < 1024
3998             && INTVAL (index) > -1024
3999             && (INTVAL (index) & 3) == 0);
4000
4001   if (arm_address_register_rtx_p (index, strict_p)
4002       && (GET_MODE_SIZE (mode) <= 4))
4003     return 1;
4004
4005   if (mode == DImode || mode == DFmode)
4006     {
4007       if (code == CONST_INT)
4008         {
4009           HOST_WIDE_INT val = INTVAL (index);
4010
4011           if (TARGET_LDRD)
4012             return val > -256 && val < 256;
4013           else
4014             return val > -4096 && val < 4092;
4015         }
4016
4017       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
4018     }
4019
4020   if (GET_MODE_SIZE (mode) <= 4
4021       && ! (arm_arch4
4022             && (mode == HImode
4023                 || (mode == QImode && outer == SIGN_EXTEND))))
4024     {
4025       if (code == MULT)
4026         {
4027           rtx xiop0 = XEXP (index, 0);
4028           rtx xiop1 = XEXP (index, 1);
4029
4030           return ((arm_address_register_rtx_p (xiop0, strict_p)
4031                    && power_of_two_operand (xiop1, SImode))
4032                   || (arm_address_register_rtx_p (xiop1, strict_p)
4033                       && power_of_two_operand (xiop0, SImode)));
4034         }
4035       else if (code == LSHIFTRT || code == ASHIFTRT
4036                || code == ASHIFT || code == ROTATERT)
4037         {
4038           rtx op = XEXP (index, 1);
4039
4040           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4041                   && GET_CODE (op) == CONST_INT
4042                   && INTVAL (op) > 0
4043                   && INTVAL (op) <= 31);
4044         }
4045     }
4046
4047   /* For ARM v4 we may be doing a sign-extend operation during the
4048      load.  */
4049   if (arm_arch4)
4050     {
4051       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
4052         range = 256;
4053       else
4054         range = 4096;
4055     }
4056   else
4057     range = (mode == HImode) ? 4095 : 4096;
4058
4059   return (code == CONST_INT
4060           && INTVAL (index) < range
4061           && INTVAL (index) > -range);
4062 }
4063
4064 /* Return true if OP is a valid index scaling factor for Thumb-2 address
4065    index operand.  i.e. 1, 2, 4 or 8.  */
4066 static bool
4067 thumb2_index_mul_operand (rtx op)
4068 {
4069   HOST_WIDE_INT val;
4070   
4071   if (GET_CODE(op) != CONST_INT)
4072     return false;
4073
4074   val = INTVAL(op);
4075   return (val == 1 || val == 2 || val == 4 || val == 8);
4076 }
4077   
4078 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
4079 static int
4080 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4081 {
4082   enum rtx_code code = GET_CODE (index);
4083
4084   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
4085   /* Standard coprocessor addressing modes.  */
4086   if (TARGET_HARD_FLOAT
4087       && (TARGET_FPA || TARGET_MAVERICK)
4088       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4089           || (TARGET_MAVERICK && mode == DImode)))
4090     return (code == CONST_INT && INTVAL (index) < 1024
4091             && INTVAL (index) > -1024
4092             && (INTVAL (index) & 3) == 0);
4093
4094   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4095     {
4096       /* For DImode assume values will usually live in core regs
4097          and only allow LDRD addressing modes.  */
4098       if (!TARGET_LDRD || mode != DImode)
4099         return (code == CONST_INT
4100                 && INTVAL (index) < 1024
4101                 && INTVAL (index) > -1024
4102                 && (INTVAL (index) & 3) == 0);
4103     }
4104
4105   if (TARGET_NEON
4106       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4107     return (code == CONST_INT
4108             && INTVAL (index) < 1016
4109             && INTVAL (index) > -1024
4110             && (INTVAL (index) & 3) == 0);
4111
4112   if (arm_address_register_rtx_p (index, strict_p)
4113       && (GET_MODE_SIZE (mode) <= 4))
4114     return 1;
4115
4116   if (mode == DImode || mode == DFmode)
4117     {
4118       HOST_WIDE_INT val = INTVAL (index);
4119       /* ??? Can we assume ldrd for thumb2?  */
4120       /* Thumb-2 ldrd only has reg+const addressing modes.  */
4121       if (code != CONST_INT)
4122         return 0;
4123
4124       /* ldrd supports offsets of +-1020.
4125          However the ldr fallback does not.  */
4126       return val > -256 && val < 256 && (val & 3) == 0;
4127     }
4128
4129   if (code == MULT)
4130     {
4131       rtx xiop0 = XEXP (index, 0);
4132       rtx xiop1 = XEXP (index, 1);
4133
4134       return ((arm_address_register_rtx_p (xiop0, strict_p)
4135                && thumb2_index_mul_operand (xiop1))
4136               || (arm_address_register_rtx_p (xiop1, strict_p)
4137                   && thumb2_index_mul_operand (xiop0)));
4138     }
4139   else if (code == ASHIFT)
4140     {
4141       rtx op = XEXP (index, 1);
4142
4143       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4144               && GET_CODE (op) == CONST_INT
4145               && INTVAL (op) > 0
4146               && INTVAL (op) <= 3);
4147     }
4148
4149   return (code == CONST_INT
4150           && INTVAL (index) < 4096
4151           && INTVAL (index) > -256);
4152 }
4153
4154 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
4155 static int
4156 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
4157 {
4158   int regno;
4159
4160   if (GET_CODE (x) != REG)
4161     return 0;
4162
4163   regno = REGNO (x);
4164
4165   if (strict_p)
4166     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
4167
4168   return (regno <= LAST_LO_REGNUM
4169           || regno > LAST_VIRTUAL_REGISTER
4170           || regno == FRAME_POINTER_REGNUM
4171           || (GET_MODE_SIZE (mode) >= 4
4172               && (regno == STACK_POINTER_REGNUM
4173                   || regno >= FIRST_PSEUDO_REGISTER
4174                   || x == hard_frame_pointer_rtx
4175                   || x == arg_pointer_rtx)));
4176 }
4177
4178 /* Return nonzero if x is a legitimate index register.  This is the case
4179    for any base register that can access a QImode object.  */
4180 inline static int
4181 thumb1_index_register_rtx_p (rtx x, int strict_p)
4182 {
4183   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4184 }
4185
4186 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4187
4188    The AP may be eliminated to either the SP or the FP, so we use the
4189    least common denominator, e.g. SImode, and offsets from 0 to 64.
4190
4191    ??? Verify whether the above is the right approach.
4192
4193    ??? Also, the FP may be eliminated to the SP, so perhaps that
4194    needs special handling also.
4195
4196    ??? Look at how the mips16 port solves this problem.  It probably uses
4197    better ways to solve some of these problems.
4198
4199    Although it is not incorrect, we don't accept QImode and HImode
4200    addresses based on the frame pointer or arg pointer until the
4201    reload pass starts.  This is so that eliminating such addresses
4202    into stack based ones won't produce impossible code.  */
4203 int
4204 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4205 {
4206   /* ??? Not clear if this is right.  Experiment.  */
4207   if (GET_MODE_SIZE (mode) < 4
4208       && !(reload_in_progress || reload_completed)
4209       && (reg_mentioned_p (frame_pointer_rtx, x)
4210           || reg_mentioned_p (arg_pointer_rtx, x)
4211           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4212           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4213           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4214           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4215     return 0;
4216
4217   /* Accept any base register.  SP only in SImode or larger.  */
4218   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4219     return 1;
4220
4221   /* This is PC relative data before arm_reorg runs.  */
4222   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4223            && GET_CODE (x) == SYMBOL_REF
4224            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4225     return 1;
4226
4227   /* This is PC relative data after arm_reorg runs.  */
4228   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
4229            && (GET_CODE (x) == LABEL_REF
4230                || (GET_CODE (x) == CONST
4231                    && GET_CODE (XEXP (x, 0)) == PLUS
4232                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4233                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4234     return 1;
4235
4236   /* Post-inc indexing only supported for SImode and larger.  */
4237   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4238            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4239     return 1;
4240
4241   else if (GET_CODE (x) == PLUS)
4242     {
4243       /* REG+REG address can be any two index registers.  */
4244       /* We disallow FRAME+REG addressing since we know that FRAME
4245          will be replaced with STACK, and SP relative addressing only
4246          permits SP+OFFSET.  */
4247       if (GET_MODE_SIZE (mode) <= 4
4248           && XEXP (x, 0) != frame_pointer_rtx
4249           && XEXP (x, 1) != frame_pointer_rtx
4250           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4251           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4252         return 1;
4253
4254       /* REG+const has 5-7 bit offset for non-SP registers.  */
4255       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4256                 || XEXP (x, 0) == arg_pointer_rtx)
4257                && GET_CODE (XEXP (x, 1)) == CONST_INT
4258                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4259         return 1;
4260
4261       /* REG+const has 10-bit offset for SP, but only SImode and
4262          larger is supported.  */
4263       /* ??? Should probably check for DI/DFmode overflow here
4264          just like GO_IF_LEGITIMATE_OFFSET does.  */
4265       else if (GET_CODE (XEXP (x, 0)) == REG
4266                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4267                && GET_MODE_SIZE (mode) >= 4
4268                && GET_CODE (XEXP (x, 1)) == CONST_INT
4269                && INTVAL (XEXP (x, 1)) >= 0
4270                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4271                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4272         return 1;
4273
4274       else if (GET_CODE (XEXP (x, 0)) == REG
4275                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4276                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4277                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4278                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4279                && GET_MODE_SIZE (mode) >= 4
4280                && GET_CODE (XEXP (x, 1)) == CONST_INT
4281                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4282         return 1;
4283     }
4284
4285   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4286            && GET_MODE_SIZE (mode) == 4
4287            && GET_CODE (x) == SYMBOL_REF
4288            && CONSTANT_POOL_ADDRESS_P (x)
4289            && ! (flag_pic
4290                  && symbol_mentioned_p (get_pool_constant (x))
4291                  && ! pcrel_constant_p (get_pool_constant (x))))
4292     return 1;
4293
4294   return 0;
4295 }
4296
4297 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4298    instruction of mode MODE.  */
4299 int
4300 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4301 {
4302   switch (GET_MODE_SIZE (mode))
4303     {
4304     case 1:
4305       return val >= 0 && val < 32;
4306
4307     case 2:
4308       return val >= 0 && val < 64 && (val & 1) == 0;
4309
4310     default:
4311       return (val >= 0
4312               && (val + GET_MODE_SIZE (mode)) <= 128
4313               && (val & 3) == 0);
4314     }
4315 }
4316
4317 /* Build the SYMBOL_REF for __tls_get_addr.  */
4318
4319 static GTY(()) rtx tls_get_addr_libfunc;
4320
4321 static rtx
4322 get_tls_get_addr (void)
4323 {
4324   if (!tls_get_addr_libfunc)
4325     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4326   return tls_get_addr_libfunc;
4327 }
4328
4329 static rtx
4330 arm_load_tp (rtx target)
4331 {
4332   if (!target)
4333     target = gen_reg_rtx (SImode);
4334
4335   if (TARGET_HARD_TP)
4336     {
4337       /* Can return in any reg.  */
4338       emit_insn (gen_load_tp_hard (target));
4339     }
4340   else
4341     {
4342       /* Always returned in r0.  Immediately copy the result into a pseudo,
4343          otherwise other uses of r0 (e.g. setting up function arguments) may
4344          clobber the value.  */
4345
4346       rtx tmp;
4347
4348       emit_insn (gen_load_tp_soft ());
4349
4350       tmp = gen_rtx_REG (SImode, 0);
4351       emit_move_insn (target, tmp);
4352     }
4353   return target;
4354 }
4355
4356 static rtx
4357 load_tls_operand (rtx x, rtx reg)
4358 {
4359   rtx tmp;
4360
4361   if (reg == NULL_RTX)
4362     reg = gen_reg_rtx (SImode);
4363
4364   tmp = gen_rtx_CONST (SImode, x);
4365
4366   emit_move_insn (reg, tmp);
4367
4368   return reg;
4369 }
4370
4371 static rtx
4372 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4373 {
4374   rtx insns, label, labelno, sum;
4375
4376   start_sequence ();
4377
4378   labelno = GEN_INT (pic_labelno++);
4379   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4380   label = gen_rtx_CONST (VOIDmode, label);
4381
4382   sum = gen_rtx_UNSPEC (Pmode,
4383                         gen_rtvec (4, x, GEN_INT (reloc), label,
4384                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4385                         UNSPEC_TLS);
4386   reg = load_tls_operand (sum, reg);
4387
4388   if (TARGET_ARM)
4389     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4390   else if (TARGET_THUMB2)
4391     {
4392       rtx tmp;
4393       /* Thumb-2 only allows very limited access to the PC.  Calculate
4394          the address in a temporary register.  */
4395       tmp = gen_reg_rtx (SImode);
4396       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4397       emit_insn (gen_addsi3(reg, reg, tmp));
4398     }
4399   else /* TARGET_THUMB1 */
4400     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4401
4402   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4403                                      Pmode, 1, reg, Pmode);
4404
4405   insns = get_insns ();
4406   end_sequence ();
4407
4408   return insns;
4409 }
4410
4411 rtx
4412 legitimize_tls_address (rtx x, rtx reg)
4413 {
4414   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4415   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4416
4417   switch (model)
4418     {
4419     case TLS_MODEL_GLOBAL_DYNAMIC:
4420       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4421       dest = gen_reg_rtx (Pmode);
4422       emit_libcall_block (insns, dest, ret, x);
4423       return dest;
4424
4425     case TLS_MODEL_LOCAL_DYNAMIC:
4426       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4427
4428       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4429          share the LDM result with other LD model accesses.  */
4430       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4431                             UNSPEC_TLS);
4432       dest = gen_reg_rtx (Pmode);
4433       emit_libcall_block (insns, dest, ret, eqv);
4434
4435       /* Load the addend.  */
4436       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4437                                UNSPEC_TLS);
4438       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4439       return gen_rtx_PLUS (Pmode, dest, addend);
4440
4441     case TLS_MODEL_INITIAL_EXEC:
4442       labelno = GEN_INT (pic_labelno++);
4443       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4444       label = gen_rtx_CONST (VOIDmode, label);
4445       sum = gen_rtx_UNSPEC (Pmode,
4446                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4447                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4448                             UNSPEC_TLS);
4449       reg = load_tls_operand (sum, reg);
4450
4451       if (TARGET_ARM)
4452         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4453       else if (TARGET_THUMB2)
4454         {
4455           rtx tmp;
4456           /* Thumb-2 only allows very limited access to the PC.  Calculate
4457              the address in a temporary register.  */
4458           tmp = gen_reg_rtx (SImode);
4459           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4460           emit_insn (gen_addsi3(reg, reg, tmp));
4461           emit_move_insn (reg, gen_const_mem (SImode, reg));
4462         }
4463       else
4464         {
4465           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4466           emit_move_insn (reg, gen_const_mem (SImode, reg));
4467         }
4468
4469       tp = arm_load_tp (NULL_RTX);
4470
4471       return gen_rtx_PLUS (Pmode, tp, reg);
4472
4473     case TLS_MODEL_LOCAL_EXEC:
4474       tp = arm_load_tp (NULL_RTX);
4475
4476       reg = gen_rtx_UNSPEC (Pmode,
4477                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4478                             UNSPEC_TLS);
4479       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4480
4481       return gen_rtx_PLUS (Pmode, tp, reg);
4482
4483     default:
4484       abort ();
4485     }
4486 }
4487
4488 /* Try machine-dependent ways of modifying an illegitimate address
4489    to be legitimate.  If we find one, return the new, valid address.  */
4490 rtx
4491 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4492 {
4493   if (arm_tls_symbol_p (x))
4494     return legitimize_tls_address (x, NULL_RTX);
4495
4496   if (GET_CODE (x) == PLUS)
4497     {
4498       rtx xop0 = XEXP (x, 0);
4499       rtx xop1 = XEXP (x, 1);
4500
4501       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4502         xop0 = force_reg (SImode, xop0);
4503
4504       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4505         xop1 = force_reg (SImode, xop1);
4506
4507       if (ARM_BASE_REGISTER_RTX_P (xop0)
4508           && GET_CODE (xop1) == CONST_INT)
4509         {
4510           HOST_WIDE_INT n, low_n;
4511           rtx base_reg, val;
4512           n = INTVAL (xop1);
4513
4514           /* VFP addressing modes actually allow greater offsets, but for
4515              now we just stick with the lowest common denominator.  */
4516           if (mode == DImode
4517               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4518             {
4519               low_n = n & 0x0f;
4520               n &= ~0x0f;
4521               if (low_n > 4)
4522                 {
4523                   n += 16;
4524                   low_n -= 16;
4525                 }
4526             }
4527           else
4528             {
4529               low_n = ((mode) == TImode ? 0
4530                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4531               n -= low_n;
4532             }
4533
4534           base_reg = gen_reg_rtx (SImode);
4535           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4536           emit_move_insn (base_reg, val);
4537           x = plus_constant (base_reg, low_n);
4538         }
4539       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4540         x = gen_rtx_PLUS (SImode, xop0, xop1);
4541     }
4542
4543   /* XXX We don't allow MINUS any more -- see comment in
4544      arm_legitimate_address_p ().  */
4545   else if (GET_CODE (x) == MINUS)
4546     {
4547       rtx xop0 = XEXP (x, 0);
4548       rtx xop1 = XEXP (x, 1);
4549
4550       if (CONSTANT_P (xop0))
4551         xop0 = force_reg (SImode, xop0);
4552
4553       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4554         xop1 = force_reg (SImode, xop1);
4555
4556       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4557         x = gen_rtx_MINUS (SImode, xop0, xop1);
4558     }
4559
4560   /* Make sure to take full advantage of the pre-indexed addressing mode
4561      with absolute addresses which often allows for the base register to
4562      be factorized for multiple adjacent memory references, and it might
4563      even allows for the mini pool to be avoided entirely. */
4564   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4565     {
4566       unsigned int bits;
4567       HOST_WIDE_INT mask, base, index;
4568       rtx base_reg;
4569
4570       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4571          use a 8-bit index. So let's use a 12-bit index for SImode only and
4572          hope that arm_gen_constant will enable ldrb to use more bits. */
4573       bits = (mode == SImode) ? 12 : 8;
4574       mask = (1 << bits) - 1;
4575       base = INTVAL (x) & ~mask;
4576       index = INTVAL (x) & mask;
4577       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4578         {
4579           /* It'll most probably be more efficient to generate the base
4580              with more bits set and use a negative index instead. */
4581           base |= mask;
4582           index -= mask;
4583         }
4584       base_reg = force_reg (SImode, GEN_INT (base));
4585       x = plus_constant (base_reg, index);
4586     }
4587
4588   if (flag_pic)
4589     {
4590       /* We need to find and carefully transform any SYMBOL and LABEL
4591          references; so go back to the original address expression.  */
4592       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4593
4594       if (new_x != orig_x)
4595         x = new_x;
4596     }
4597
4598   return x;
4599 }
4600
4601
4602 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4603    to be legitimate.  If we find one, return the new, valid address.  */
4604 rtx
4605 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4606 {
4607   if (arm_tls_symbol_p (x))
4608     return legitimize_tls_address (x, NULL_RTX);
4609
4610   if (GET_CODE (x) == PLUS
4611       && GET_CODE (XEXP (x, 1)) == CONST_INT
4612       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4613           || INTVAL (XEXP (x, 1)) < 0))
4614     {
4615       rtx xop0 = XEXP (x, 0);
4616       rtx xop1 = XEXP (x, 1);
4617       HOST_WIDE_INT offset = INTVAL (xop1);
4618
4619       /* Try and fold the offset into a biasing of the base register and
4620          then offsetting that.  Don't do this when optimizing for space
4621          since it can cause too many CSEs.  */
4622       if (optimize_size && offset >= 0
4623           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4624         {
4625           HOST_WIDE_INT delta;
4626
4627           if (offset >= 256)
4628             delta = offset - (256 - GET_MODE_SIZE (mode));
4629           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4630             delta = 31 * GET_MODE_SIZE (mode);
4631           else
4632             delta = offset & (~31 * GET_MODE_SIZE (mode));
4633
4634           xop0 = force_operand (plus_constant (xop0, offset - delta),
4635                                 NULL_RTX);
4636           x = plus_constant (xop0, delta);
4637         }
4638       else if (offset < 0 && offset > -256)
4639         /* Small negative offsets are best done with a subtract before the
4640            dereference, forcing these into a register normally takes two
4641            instructions.  */
4642         x = force_operand (x, NULL_RTX);
4643       else
4644         {
4645           /* For the remaining cases, force the constant into a register.  */
4646           xop1 = force_reg (SImode, xop1);
4647           x = gen_rtx_PLUS (SImode, xop0, xop1);
4648         }
4649     }
4650   else if (GET_CODE (x) == PLUS
4651            && s_register_operand (XEXP (x, 1), SImode)
4652            && !s_register_operand (XEXP (x, 0), SImode))
4653     {
4654       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4655
4656       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4657     }
4658
4659   if (flag_pic)
4660     {
4661       /* We need to find and carefully transform any SYMBOL and LABEL
4662          references; so go back to the original address expression.  */
4663       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4664
4665       if (new_x != orig_x)
4666         x = new_x;
4667     }
4668
4669   return x;
4670 }
4671
4672 rtx
4673 thumb_legitimize_reload_address (rtx *x_p,
4674                                  enum machine_mode mode,
4675                                  int opnum, int type,
4676                                  int ind_levels ATTRIBUTE_UNUSED)
4677 {
4678   rtx x = *x_p;
4679
4680   if (GET_CODE (x) == PLUS
4681       && GET_MODE_SIZE (mode) < 4
4682       && REG_P (XEXP (x, 0))
4683       && XEXP (x, 0) == stack_pointer_rtx
4684       && GET_CODE (XEXP (x, 1)) == CONST_INT
4685       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4686     {
4687       rtx orig_x = x;
4688
4689       x = copy_rtx (x);
4690       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4691                    Pmode, VOIDmode, 0, 0, opnum, type);
4692       return x;
4693     }
4694
4695   /* If both registers are hi-regs, then it's better to reload the
4696      entire expression rather than each register individually.  That
4697      only requires one reload register rather than two.  */
4698   if (GET_CODE (x) == PLUS
4699       && REG_P (XEXP (x, 0))
4700       && REG_P (XEXP (x, 1))
4701       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4702       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4703     {
4704       rtx orig_x = x;
4705
4706       x = copy_rtx (x);
4707       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4708                    Pmode, VOIDmode, 0, 0, opnum, type);
4709       return x;
4710     }
4711
4712   return NULL;
4713 }
4714
4715 /* Test for various thread-local symbols.  */
4716
4717 /* Return TRUE if X is a thread-local symbol.  */
4718
4719 static bool
4720 arm_tls_symbol_p (rtx x)
4721 {
4722   if (! TARGET_HAVE_TLS)
4723     return false;
4724
4725   if (GET_CODE (x) != SYMBOL_REF)
4726     return false;
4727
4728   return SYMBOL_REF_TLS_MODEL (x) != 0;
4729 }
4730
4731 /* Helper for arm_tls_referenced_p.  */
4732
4733 static int
4734 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4735 {
4736   if (GET_CODE (*x) == SYMBOL_REF)
4737     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4738
4739   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4740      TLS offsets, not real symbol references.  */
4741   if (GET_CODE (*x) == UNSPEC
4742       && XINT (*x, 1) == UNSPEC_TLS)
4743     return -1;
4744
4745   return 0;
4746 }
4747
4748 /* Return TRUE if X contains any TLS symbol references.  */
4749
4750 bool
4751 arm_tls_referenced_p (rtx x)
4752 {
4753   if (! TARGET_HAVE_TLS)
4754     return false;
4755
4756   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4757 }
4758
4759 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
4760
4761 bool
4762 arm_cannot_force_const_mem (rtx x)
4763 {
4764   rtx base, offset;
4765
4766   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
4767     {
4768       split_const (x, &base, &offset);
4769       if (GET_CODE (base) == SYMBOL_REF
4770           && !offset_within_block_p (base, INTVAL (offset)))
4771         return true;
4772     }
4773   return arm_tls_referenced_p (x);
4774 }
4775 \f
4776 #define REG_OR_SUBREG_REG(X)                                            \
4777   (GET_CODE (X) == REG                                                  \
4778    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4779
4780 #define REG_OR_SUBREG_RTX(X)                    \
4781    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4782
4783 #ifndef COSTS_N_INSNS
4784 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4785 #endif
4786 static inline int
4787 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4788 {
4789   enum machine_mode mode = GET_MODE (x);
4790
4791   switch (code)
4792     {
4793     case ASHIFT:
4794     case ASHIFTRT:
4795     case LSHIFTRT:
4796     case ROTATERT:
4797     case PLUS:
4798     case MINUS:
4799     case COMPARE:
4800     case NEG:
4801     case NOT:
4802       return COSTS_N_INSNS (1);
4803
4804     case MULT:
4805       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4806         {
4807           int cycles = 0;
4808           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4809
4810           while (i)
4811             {
4812               i >>= 2;
4813               cycles++;
4814             }
4815           return COSTS_N_INSNS (2) + cycles;
4816         }
4817       return COSTS_N_INSNS (1) + 16;
4818
4819     case SET:
4820       return (COSTS_N_INSNS (1)
4821               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4822                      + GET_CODE (SET_DEST (x)) == MEM));
4823
4824     case CONST_INT:
4825       if (outer == SET)
4826         {
4827           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4828             return 0;
4829           if (thumb_shiftable_const (INTVAL (x)))
4830             return COSTS_N_INSNS (2);
4831           return COSTS_N_INSNS (3);
4832         }
4833       else if ((outer == PLUS || outer == COMPARE)
4834                && INTVAL (x) < 256 && INTVAL (x) > -256)
4835         return 0;
4836       else if (outer == AND
4837                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4838         return COSTS_N_INSNS (1);
4839       else if (outer == ASHIFT || outer == ASHIFTRT
4840                || outer == LSHIFTRT)
4841         return 0;
4842       return COSTS_N_INSNS (2);
4843
4844     case CONST:
4845     case CONST_DOUBLE:
4846     case LABEL_REF:
4847     case SYMBOL_REF:
4848       return COSTS_N_INSNS (3);
4849
4850     case UDIV:
4851     case UMOD:
4852     case DIV:
4853     case MOD:
4854       return 100;
4855
4856     case TRUNCATE:
4857       return 99;
4858
4859     case AND:
4860     case XOR:
4861     case IOR:
4862       /* XXX guess.  */
4863       return 8;
4864
4865     case MEM:
4866       /* XXX another guess.  */
4867       /* Memory costs quite a lot for the first word, but subsequent words
4868          load at the equivalent of a single insn each.  */
4869       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4870               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4871                  ? 4 : 0));
4872
4873     case IF_THEN_ELSE:
4874       /* XXX a guess.  */
4875       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4876         return 14;
4877       return 2;
4878
4879     case ZERO_EXTEND:
4880       /* XXX still guessing.  */
4881       switch (GET_MODE (XEXP (x, 0)))
4882         {
4883         case QImode:
4884           return (1 + (mode == DImode ? 4 : 0)
4885                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4886
4887         case HImode:
4888           return (4 + (mode == DImode ? 4 : 0)
4889                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4890
4891         case SImode:
4892           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4893
4894         default:
4895           return 99;
4896         }
4897
4898     default:
4899       return 99;
4900     }
4901 }
4902
4903 static inline bool
4904 arm_rtx_costs_1 (rtx x, enum rtx_code outer, int* total, bool speed)
4905 {
4906   enum machine_mode mode = GET_MODE (x);
4907   enum rtx_code subcode;
4908   rtx operand;
4909   enum rtx_code code = GET_CODE (x);
4910   int extra_cost;
4911   *total = 0;
4912
4913   switch (code)
4914     {
4915     case MEM:
4916       /* Memory costs quite a lot for the first word, but subsequent words
4917          load at the equivalent of a single insn each.  */
4918       *total = COSTS_N_INSNS (2 + ARM_NUM_REGS (mode));
4919       return true;
4920
4921     case DIV:
4922     case MOD:
4923     case UDIV:
4924     case UMOD:
4925       if (TARGET_HARD_FLOAT && mode == SFmode)
4926         *total = COSTS_N_INSNS (2);
4927       else if (TARGET_HARD_FLOAT && mode == DFmode)
4928         *total = COSTS_N_INSNS (4);
4929       else
4930         *total = COSTS_N_INSNS (20);
4931       return false;
4932
4933     case ROTATE:
4934       if (GET_CODE (XEXP (x, 1)) == REG)
4935         *total = COSTS_N_INSNS (1); /* Need to subtract from 32 */
4936       else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4937         *total = rtx_cost (XEXP (x, 1), code, speed);
4938
4939       /* Fall through */
4940     case ROTATERT:
4941       if (mode != SImode)
4942         {
4943           *total += COSTS_N_INSNS (4);
4944           return true;
4945         }
4946
4947       /* Fall through */
4948     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4949       *total += rtx_cost (XEXP (x, 0), code, speed);
4950       if (mode == DImode)
4951         {
4952           *total += COSTS_N_INSNS (3);
4953           return true;
4954         }
4955
4956       *total += COSTS_N_INSNS (1);
4957       /* Increase the cost of complex shifts because they aren't any faster,
4958          and reduce dual issue opportunities.  */
4959       if (arm_tune_cortex_a9
4960           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
4961         ++*total;
4962
4963       return true;
4964
4965     case MINUS:
4966       if (TARGET_THUMB2)
4967         {
4968           if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4969             {
4970               if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
4971                 *total = COSTS_N_INSNS (1);
4972               else
4973                 *total = COSTS_N_INSNS (20);
4974             }
4975           else
4976             *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4977           /* Thumb2 does not have RSB, so all arguments must be
4978              registers (subtracting a constant is canonicalized as
4979              addition of the negated constant).  */
4980           return false;
4981         }
4982
4983       if (mode == DImode)
4984         {
4985           *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4986           if (GET_CODE (XEXP (x, 0)) == CONST_INT
4987               && const_ok_for_arm (INTVAL (XEXP (x, 0))))
4988             {
4989               *total += rtx_cost (XEXP (x, 1), code, speed);
4990               return true;
4991             }
4992
4993           if (GET_CODE (XEXP (x, 1)) == CONST_INT
4994               && const_ok_for_arm (INTVAL (XEXP (x, 1))))
4995             {
4996               *total += rtx_cost (XEXP (x, 0), code, speed);
4997               return true;
4998             }
4999
5000           return false;
5001         }
5002
5003       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5004         {
5005           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5006             {
5007               *total = COSTS_N_INSNS (1);
5008               if (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
5009                   && arm_const_double_rtx (XEXP (x, 0)))
5010                 {
5011                   *total += rtx_cost (XEXP (x, 1), code, speed);
5012                   return true;
5013                 }
5014
5015               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5016                   && arm_const_double_rtx (XEXP (x, 1)))
5017                 {
5018                   *total += rtx_cost (XEXP (x, 0), code, speed);
5019                   return true;
5020                 }
5021
5022               return false;
5023             }
5024           *total = COSTS_N_INSNS (20);
5025           return false;
5026         }
5027
5028       *total = COSTS_N_INSNS (1);
5029       if (GET_CODE (XEXP (x, 0)) == CONST_INT
5030           && const_ok_for_arm (INTVAL (XEXP (x, 0))))
5031         {
5032           *total += rtx_cost (XEXP (x, 1), code, speed);
5033           return true;
5034         }
5035
5036       subcode = GET_CODE (XEXP (x, 1));
5037       if (subcode == ASHIFT || subcode == ASHIFTRT
5038           || subcode == LSHIFTRT
5039           || subcode == ROTATE || subcode == ROTATERT)
5040         {
5041           *total += rtx_cost (XEXP (x, 0), code, speed);
5042           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
5043           return true;
5044         }
5045
5046       if (subcode == MULT
5047           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5048           && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
5049                (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0))
5050         {
5051           *total += rtx_cost (XEXP (x, 0), code, speed);
5052           *total += rtx_cost (XEXP (XEXP (x, 1), 0), subcode, speed);
5053           return true;
5054         }
5055
5056       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMPARE
5057           || GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == RTX_COMM_COMPARE)
5058         {
5059           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5060           if (GET_CODE (XEXP (XEXP (x, 1), 0)) == REG
5061               && REGNO (XEXP (XEXP (x, 1), 0)) != CC_REGNUM)
5062             *total += COSTS_N_INSNS (1);
5063
5064           return true;
5065         }
5066
5067       /* Fall through */
5068
5069     case PLUS:
5070       if (code == PLUS && arm_arch6 && mode == SImode
5071           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5072               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5073         {
5074           *total = COSTS_N_INSNS (1);
5075           *total += rtx_cost (XEXP (XEXP (x, 0), 0), GET_CODE (XEXP (x, 0)),
5076                               speed);
5077           *total += rtx_cost (XEXP (x, 1), code, speed);
5078           return true;
5079         }
5080
5081       /* MLA: All arguments must be registers.  We filter out
5082          multiplication by a power of two, so that we fall down into
5083          the code below.  */
5084       if (GET_CODE (XEXP (x, 0)) == MULT
5085           && ! (GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5086                 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
5087                      (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
5088         {
5089           /* The cost comes from the cost of the multiply.  */
5090           return false;
5091         }
5092
5093       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5094         {
5095           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5096             {
5097               *total = COSTS_N_INSNS (1);
5098               if (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5099                   && arm_const_double_rtx (XEXP (x, 1)))
5100                 {
5101                   *total += rtx_cost (XEXP (x, 0), code, speed);
5102                   return true;
5103                 }
5104
5105               return false;
5106             }
5107
5108           *total = COSTS_N_INSNS (20);
5109           return false;
5110         }
5111
5112       if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMPARE
5113           || GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == RTX_COMM_COMPARE)
5114         {
5115           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 1), code, speed);
5116           if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG
5117               && REGNO (XEXP (XEXP (x, 0), 0)) != CC_REGNUM)
5118             *total += COSTS_N_INSNS (1);
5119           return true;
5120         }
5121
5122       /* Fall through */
5123
5124     case AND: case XOR: case IOR:
5125       extra_cost = 0;
5126
5127       /* Normally the frame registers will be spilt into reg+const during
5128          reload, so it is a bad idea to combine them with other instructions,
5129          since then they might not be moved outside of loops.  As a compromise
5130          we allow integration with ops that have a constant as their second
5131          operand.  */
5132       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
5133            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
5134            && GET_CODE (XEXP (x, 1)) != CONST_INT)
5135           || (REG_OR_SUBREG_REG (XEXP (x, 0))
5136               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
5137         *total = 4;
5138
5139       if (mode == DImode)
5140         {
5141           *total += COSTS_N_INSNS (2);
5142           if (GET_CODE (XEXP (x, 1)) == CONST_INT
5143               && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5144             {
5145               *total += rtx_cost (XEXP (x, 0), code, speed);
5146               return true;
5147             }
5148
5149           return false;
5150         }
5151
5152       *total += COSTS_N_INSNS (1);
5153       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5154           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5155         {
5156           *total += rtx_cost (XEXP (x, 0), code, speed);
5157           return true;
5158         }
5159       subcode = GET_CODE (XEXP (x, 0));
5160       if (subcode == ASHIFT || subcode == ASHIFTRT
5161           || subcode == LSHIFTRT
5162           || subcode == ROTATE || subcode == ROTATERT)
5163         {
5164           *total += rtx_cost (XEXP (x, 1), code, speed);
5165           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5166           return true;
5167         }
5168
5169       if (subcode == MULT
5170           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5171           && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
5172                (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0))
5173         {
5174           *total += rtx_cost (XEXP (x, 1), code, speed);
5175           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5176           return true;
5177         }
5178
5179       if (subcode == UMIN || subcode == UMAX
5180           || subcode == SMIN || subcode == SMAX)
5181         {
5182           *total = COSTS_N_INSNS (3);
5183           return true;
5184         }
5185
5186       return false;
5187
5188     case MULT:
5189       /* This should have been handled by the CPU specific routines.  */
5190       gcc_unreachable ();
5191
5192     case TRUNCATE:
5193       if (arm_arch3m && mode == SImode
5194           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5195           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5196           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
5197               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5198           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
5199               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
5200         {
5201           *total = rtx_cost (XEXP (XEXP (x, 0), 0), LSHIFTRT, speed);
5202           return true;
5203         }
5204       *total = COSTS_N_INSNS (2); /* Plus the cost of the MULT */
5205       return false;
5206
5207     case NEG:
5208       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5209         {
5210           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5211             {
5212               *total = COSTS_N_INSNS (1);
5213               return false;
5214             }
5215           *total = COSTS_N_INSNS (2);
5216           return false;
5217         }
5218
5219       /* Fall through */
5220     case NOT:
5221       *total = COSTS_N_INSNS (ARM_NUM_REGS(mode));
5222       if (mode == SImode && code == NOT)
5223         {
5224           subcode = GET_CODE (XEXP (x, 0));
5225           if (subcode == ASHIFT || subcode == ASHIFTRT
5226               || subcode == LSHIFTRT
5227               || subcode == ROTATE || subcode == ROTATERT
5228               || (subcode == MULT
5229                   && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5230                   && ((INTVAL (XEXP (XEXP (x, 0), 1)) & 
5231                        (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
5232             {
5233               *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5234               /* Register shifts cost an extra cycle.  */
5235               if (GET_CODE (XEXP (XEXP (x, 0), 1)) != CONST_INT)
5236                 *total += COSTS_N_INSNS (1) + rtx_cost (XEXP (XEXP (x, 0), 1),
5237                                                         subcode, speed);
5238               return true;
5239             }
5240         }
5241
5242       return false;
5243
5244     case IF_THEN_ELSE:
5245       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5246         {
5247           *total = COSTS_N_INSNS (4);
5248           return true;
5249         }
5250
5251       operand = XEXP (x, 0);
5252
5253       if (!((GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMPARE
5254              || GET_RTX_CLASS (GET_CODE (operand)) == RTX_COMM_COMPARE)
5255             && GET_CODE (XEXP (operand, 0)) == REG
5256             && REGNO (XEXP (operand, 0)) == CC_REGNUM))
5257         *total += COSTS_N_INSNS (1);
5258       *total += (rtx_cost (XEXP (x, 1), code, speed)
5259                  + rtx_cost (XEXP (x, 2), code, speed));
5260       return true;
5261
5262     case NE:
5263       if (mode == SImode && XEXP (x, 1) == const0_rtx)
5264         {
5265           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5266           return true;
5267         }
5268       goto scc_insn;
5269
5270     case GE:
5271       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
5272           && mode == SImode && XEXP (x, 1) == const0_rtx)
5273         {
5274           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5275           return true;
5276         }
5277       goto scc_insn;
5278
5279     case LT:
5280       if ((GET_CODE (XEXP (x, 0)) != REG || REGNO (XEXP (x, 0)) != CC_REGNUM)
5281           && mode == SImode && XEXP (x, 1) == const0_rtx)
5282         {
5283           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5284           return true;
5285         }
5286       goto scc_insn;
5287
5288     case EQ:
5289     case GT:
5290     case LE:
5291     case GEU:
5292     case LTU:
5293     case GTU:
5294     case LEU:
5295     case UNORDERED:
5296     case ORDERED:
5297     case UNEQ:
5298     case UNGE:
5299     case UNLT:
5300     case UNGT:
5301     case UNLE:
5302     scc_insn:
5303       /* SCC insns.  In the case where the comparison has already been
5304          performed, then they cost 2 instructions.  Otherwise they need
5305          an additional comparison before them.  */
5306       *total = COSTS_N_INSNS (2);
5307       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
5308         {
5309           return true;
5310         }
5311
5312       /* Fall through */
5313     case COMPARE:
5314       if (GET_CODE (XEXP (x, 0)) == REG && REGNO (XEXP (x, 0)) == CC_REGNUM)
5315         {
5316           *total = 0;
5317           return true;
5318         }
5319
5320       *total += COSTS_N_INSNS (1);
5321       if (GET_CODE (XEXP (x, 1)) == CONST_INT
5322           && const_ok_for_op (INTVAL (XEXP (x, 1)), code))
5323         {
5324           *total += rtx_cost (XEXP (x, 0), code, speed);
5325           return true;
5326         }
5327
5328       subcode = GET_CODE (XEXP (x, 0));
5329       if (subcode == ASHIFT || subcode == ASHIFTRT
5330           || subcode == LSHIFTRT
5331           || subcode == ROTATE || subcode == ROTATERT)
5332         {
5333           *total += rtx_cost (XEXP (x, 1), code, speed);
5334           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5335           return true;
5336         }
5337
5338       if (subcode == MULT
5339           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5340           && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
5341                (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0))
5342         {
5343           *total += rtx_cost (XEXP (x, 1), code, speed);
5344           *total += rtx_cost (XEXP (XEXP (x, 0), 0), subcode, speed);
5345           return true;
5346         }
5347       
5348       return false;
5349
5350     case UMIN:
5351     case UMAX:
5352     case SMIN:
5353     case SMAX:
5354       *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, speed);
5355       if (GET_CODE (XEXP (x, 1)) != CONST_INT
5356           || !const_ok_for_arm (INTVAL (XEXP (x, 1))))
5357         *total += rtx_cost (XEXP (x, 1), code, speed);
5358       return true;
5359
5360     case ABS:
5361       if (GET_MODE_CLASS (mode == MODE_FLOAT))
5362         {
5363           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5364             {
5365               *total = COSTS_N_INSNS (1);
5366               return false;
5367             }
5368           *total = COSTS_N_INSNS (20);
5369           return false;
5370         }
5371       *total = COSTS_N_INSNS (1);
5372       if (mode == DImode)
5373         *total += COSTS_N_INSNS (3);
5374       return false;
5375
5376     case SIGN_EXTEND:
5377       if (GET_MODE_CLASS (mode) == MODE_INT)
5378         {
5379           *total = 0;
5380           if (mode == DImode)
5381             *total += COSTS_N_INSNS (1);
5382
5383           if (GET_MODE (XEXP (x, 0)) != SImode)
5384             {
5385               if (arm_arch6)
5386                 {
5387                   if (GET_CODE (XEXP (x, 0)) != MEM)
5388                     *total += COSTS_N_INSNS (1);
5389                 }
5390               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
5391                 *total += COSTS_N_INSNS (2);
5392             }
5393
5394           return false;
5395         }
5396
5397       /* Fall through */
5398     case ZERO_EXTEND:
5399       *total = 0;
5400       if (GET_MODE_CLASS (mode) == MODE_INT)
5401         {
5402           if (mode == DImode)
5403             *total += COSTS_N_INSNS (1);
5404
5405           if (GET_MODE (XEXP (x, 0)) != SImode)
5406             {
5407               if (arm_arch6)
5408                 {
5409                   if (GET_CODE (XEXP (x, 0)) != MEM)
5410                     *total += COSTS_N_INSNS (1);
5411                 }
5412               else if (!arm_arch4 || GET_CODE (XEXP (x, 0)) != MEM)
5413                 *total += COSTS_N_INSNS (GET_MODE (XEXP (x, 0)) == QImode ?
5414                                          1 : 2);
5415             }
5416
5417           return false;
5418         }
5419
5420       switch (GET_MODE (XEXP (x, 0)))
5421         {
5422         case V8QImode:
5423         case V4HImode:
5424         case V2SImode:
5425         case V4QImode:
5426         case V2HImode:
5427           *total = COSTS_N_INSNS (1);
5428           return false;
5429
5430         default:
5431           gcc_unreachable ();
5432         }
5433       gcc_unreachable ();
5434
5435     case ZERO_EXTRACT:
5436     case SIGN_EXTRACT:
5437       *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, speed);
5438       return true;
5439
5440     case CONST_INT:
5441       if (const_ok_for_arm (INTVAL (x))
5442           || const_ok_for_arm (~INTVAL (x)))
5443         *total = COSTS_N_INSNS (1);
5444       else
5445         *total = COSTS_N_INSNS (arm_gen_constant (SET, mode, NULL_RTX,
5446                                                   INTVAL (x), NULL_RTX,
5447                                                   NULL_RTX, 0, 0));
5448       return true;
5449
5450     case CONST:
5451     case LABEL_REF:
5452     case SYMBOL_REF:
5453       *total = COSTS_N_INSNS (3);
5454       return true;
5455
5456     case HIGH:
5457       *total = COSTS_N_INSNS (1);
5458       return true;
5459
5460     case LO_SUM:
5461       *total = COSTS_N_INSNS (1);
5462       *total += rtx_cost (XEXP (x, 0), code, speed);
5463       return true;
5464
5465     case CONST_DOUBLE:
5466       if (TARGET_HARD_FLOAT && vfp3_const_double_rtx (x))
5467         *total = COSTS_N_INSNS (1);
5468       else
5469         *total = COSTS_N_INSNS (4);
5470       return true;
5471
5472     default:
5473       *total = COSTS_N_INSNS (4);
5474       return false;
5475     }
5476 }
5477
5478 /* RTX costs when optimizing for size.  */
5479 static bool
5480 arm_size_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
5481                     int *total)
5482 {
5483   enum machine_mode mode = GET_MODE (x);
5484   if (TARGET_THUMB1)
5485     {
5486       /* XXX TBD.  For now, use the standard costs.  */
5487       *total = thumb1_rtx_costs (x, code, outer_code);
5488       return true;
5489     }
5490
5491   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
5492   switch (code)
5493     {
5494     case MEM:
5495       /* A memory access costs 1 insn if the mode is small, or the address is
5496          a single register, otherwise it costs one insn per word.  */
5497       if (REG_P (XEXP (x, 0)))
5498         *total = COSTS_N_INSNS (1);
5499       else
5500         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5501       return true;
5502
5503     case DIV:
5504     case MOD:
5505     case UDIV:
5506     case UMOD:
5507       /* Needs a libcall, so it costs about this.  */
5508       *total = COSTS_N_INSNS (2);
5509       return false;
5510
5511     case ROTATE:
5512       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5513         {
5514           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
5515           return true;
5516         }
5517       /* Fall through */
5518     case ROTATERT:
5519     case ASHIFT:
5520     case LSHIFTRT:
5521     case ASHIFTRT:
5522       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5523         {
5524           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
5525           return true;
5526         }
5527       else if (mode == SImode)
5528         {
5529           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
5530           /* Slightly disparage register shifts, but not by much.  */
5531           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5532             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
5533           return true;
5534         }
5535
5536       /* Needs a libcall.  */
5537       *total = COSTS_N_INSNS (2);
5538       return false;
5539
5540     case MINUS:
5541       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5542         {
5543           *total = COSTS_N_INSNS (1);
5544           return false;
5545         }
5546
5547       if (mode == SImode)
5548         {
5549           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5550           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5551
5552           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5553               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5554               || subcode1 == ROTATE || subcode1 == ROTATERT
5555               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5556               || subcode1 == ASHIFTRT)
5557             {
5558               /* It's just the cost of the two operands.  */
5559               *total = 0;
5560               return false;
5561             }
5562
5563           *total = COSTS_N_INSNS (1);
5564           return false;
5565         }
5566
5567       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5568       return false;
5569
5570     case PLUS:
5571       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5572         {
5573           *total = COSTS_N_INSNS (1);
5574           return false;
5575         }
5576
5577       /* Fall through */
5578     case AND: case XOR: case IOR:
5579       if (mode == SImode)
5580         {
5581           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5582
5583           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5584               || subcode == LSHIFTRT || subcode == ASHIFTRT
5585               || (code == AND && subcode == NOT))
5586             {
5587               /* It's just the cost of the two operands.  */
5588               *total = 0;
5589               return false;
5590             }
5591         }
5592
5593       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5594       return false;
5595
5596     case MULT:
5597       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5598       return false;
5599
5600     case NEG:
5601       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5602         {
5603           *total = COSTS_N_INSNS (1);
5604           return false;
5605         }
5606
5607       /* Fall through */
5608     case NOT:
5609       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5610
5611       return false;
5612
5613     case IF_THEN_ELSE:
5614       *total = 0;
5615       return false;
5616
5617     case COMPARE:
5618       if (cc_register (XEXP (x, 0), VOIDmode))
5619         * total = 0;
5620       else
5621         *total = COSTS_N_INSNS (1);
5622       return false;
5623
5624     case ABS:
5625       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5626         *total = COSTS_N_INSNS (1);
5627       else
5628         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5629       return false;
5630
5631     case SIGN_EXTEND:
5632       *total = 0;
5633       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5634         {
5635           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5636             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5637         }
5638       if (mode == DImode)
5639         *total += COSTS_N_INSNS (1);
5640       return false;
5641
5642     case ZERO_EXTEND:
5643       *total = 0;
5644       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5645         {
5646           switch (GET_MODE (XEXP (x, 0)))
5647             {
5648             case QImode:
5649               *total += COSTS_N_INSNS (1);
5650               break;
5651
5652             case HImode:
5653               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5654
5655             case SImode:
5656               break;
5657
5658             default:
5659               *total += COSTS_N_INSNS (2);
5660             }
5661         }
5662
5663       if (mode == DImode)
5664         *total += COSTS_N_INSNS (1);
5665
5666       return false;
5667
5668     case CONST_INT:
5669       if (const_ok_for_arm (INTVAL (x)))
5670         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5671       else if (const_ok_for_arm (~INTVAL (x)))
5672         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5673       else if (const_ok_for_arm (-INTVAL (x)))
5674         {
5675           if (outer_code == COMPARE || outer_code == PLUS
5676               || outer_code == MINUS)
5677             *total = 0;
5678           else
5679             *total = COSTS_N_INSNS (1);
5680         }
5681       else
5682         *total = COSTS_N_INSNS (2);
5683       return true;
5684
5685     case CONST:
5686     case LABEL_REF:
5687     case SYMBOL_REF:
5688       *total = COSTS_N_INSNS (2);
5689       return true;
5690
5691     case CONST_DOUBLE:
5692       *total = COSTS_N_INSNS (4);
5693       return true;
5694
5695     case HIGH:
5696     case LO_SUM:
5697       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
5698          cost of these slightly.  */
5699       *total = COSTS_N_INSNS (1) + 1;
5700       return true;
5701
5702     default:
5703       if (mode != VOIDmode)
5704         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5705       else
5706         *total = COSTS_N_INSNS (4); /* How knows?  */
5707       return false;
5708     }
5709 }
5710
5711 /* RTX costs when optimizing for size.  */
5712 static bool
5713 arm_rtx_costs (rtx x, int code, int outer_code, int *total,
5714                bool speed)
5715 {
5716   if (!speed)
5717     return arm_size_rtx_costs (x, code, outer_code, total);
5718   else
5719     return all_cores[(int)arm_tune].rtx_costs (x, code, outer_code, total,
5720                                                speed);
5721 }
5722
5723 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
5724    supported on any "slowmul" cores, so it can be ignored.  */
5725
5726 static bool
5727 arm_slowmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
5728                        int *total, bool speed)
5729 {
5730   enum machine_mode mode = GET_MODE (x);
5731
5732   if (TARGET_THUMB)
5733     {
5734       *total = thumb1_rtx_costs (x, code, outer_code);
5735       return true;
5736     }
5737
5738   switch (code)
5739     {
5740     case MULT:
5741       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5742           || mode == DImode)
5743         {
5744           *total = COSTS_N_INSNS (20);
5745           return false;
5746         }
5747
5748       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5749         {
5750           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5751                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5752           int cost, const_ok = const_ok_for_arm (i);
5753           int j, booth_unit_size;
5754
5755           /* Tune as appropriate.  */
5756           cost = const_ok ? 4 : 8;
5757           booth_unit_size = 2;
5758           for (j = 0; i && j < 32; j += booth_unit_size)
5759             {
5760               i >>= booth_unit_size;
5761               cost++;
5762             }
5763
5764           *total = COSTS_N_INSNS (cost);
5765           *total += rtx_cost (XEXP (x, 0), code, speed);
5766           return true;
5767         }
5768
5769       *total = COSTS_N_INSNS (20);
5770       return false;
5771
5772     default:
5773       return arm_rtx_costs_1 (x, outer_code, total, speed);;
5774     }
5775 }
5776
5777
5778 /* RTX cost for cores with a fast multiply unit (M variants).  */
5779
5780 static bool
5781 arm_fastmul_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
5782                        int *total, bool speed)
5783 {
5784   enum machine_mode mode = GET_MODE (x);
5785
5786   if (TARGET_THUMB1)
5787     {
5788       *total = thumb1_rtx_costs (x, code, outer_code);
5789       return true;
5790     }
5791
5792   /* ??? should thumb2 use different costs?  */
5793   switch (code)
5794     {
5795     case MULT:
5796       /* There is no point basing this on the tuning, since it is always the
5797          fast variant if it exists at all.  */
5798       if (mode == DImode
5799           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5800           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5801               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5802         {
5803           *total = COSTS_N_INSNS(2);
5804           return false;
5805         }
5806
5807
5808       if (mode == DImode)
5809         {
5810           *total = COSTS_N_INSNS (5);
5811           return false;
5812         }
5813
5814       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5815         {
5816           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5817                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5818           int cost, const_ok = const_ok_for_arm (i);
5819           int j, booth_unit_size;
5820
5821           /* Tune as appropriate.  */
5822           cost = const_ok ? 4 : 8;
5823           booth_unit_size = 8;
5824           for (j = 0; i && j < 32; j += booth_unit_size)
5825             {
5826               i >>= booth_unit_size;
5827               cost++;
5828             }
5829
5830           *total = COSTS_N_INSNS(cost);
5831           return false;
5832         }
5833
5834       if (mode == SImode)
5835         {
5836           *total = COSTS_N_INSNS (4);
5837           return false;
5838         }
5839
5840       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5841         {
5842           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
5843             {
5844               *total = COSTS_N_INSNS (1);
5845               return false;
5846             }
5847         }
5848
5849       /* Requires a lib call */
5850       *total = COSTS_N_INSNS (20);
5851       return false;
5852
5853     default:
5854       return arm_rtx_costs_1 (x, outer_code, total, speed);
5855     }
5856 }
5857
5858
5859 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
5860    so it can be ignored.  */
5861
5862 static bool
5863 arm_xscale_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code, int *total, bool speed)
5864 {
5865   enum machine_mode mode = GET_MODE (x);
5866
5867   if (TARGET_THUMB)
5868     {
5869       *total = thumb1_rtx_costs (x, code, outer_code);
5870       return true;
5871     }
5872
5873   switch (code)
5874     {
5875     case COMPARE:
5876       if (GET_CODE (XEXP (x, 0)) != MULT)
5877         return arm_rtx_costs_1 (x, outer_code, total, speed);
5878
5879       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5880          will stall until the multiplication is complete.  */
5881       *total = COSTS_N_INSNS (3);
5882       return false;
5883
5884     case MULT:
5885       /* There is no point basing this on the tuning, since it is always the
5886          fast variant if it exists at all.  */
5887       if (mode == DImode
5888           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5889           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5890               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5891         {
5892           *total = COSTS_N_INSNS (2);
5893           return false;
5894         }
5895
5896
5897       if (mode == DImode)
5898         {
5899           *total = COSTS_N_INSNS (5);
5900           return false;
5901         }
5902
5903       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5904         {
5905           /* If operand 1 is a constant we can more accurately
5906              calculate the cost of the multiply.  The multiplier can
5907              retire 15 bits on the first cycle and a further 12 on the
5908              second.  We do, of course, have to load the constant into
5909              a register first.  */
5910           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
5911           /* There's a general overhead of one cycle.  */
5912           int cost = 1;
5913           unsigned HOST_WIDE_INT masked_const;
5914
5915           if (i & 0x80000000)
5916             i = ~i;
5917
5918           i &= (unsigned HOST_WIDE_INT) 0xffffffff;
5919
5920           masked_const = i & 0xffff8000;
5921           if (masked_const != 0)
5922             {
5923               cost++;
5924               masked_const = i & 0xf8000000;
5925               if (masked_const != 0)
5926                 cost++;
5927             }
5928           *total = COSTS_N_INSNS (cost);
5929           return false;
5930         }
5931
5932       if (mode == SImode)
5933         {
5934           *total = COSTS_N_INSNS (3);
5935           return false;
5936         }
5937
5938       /* Requires a lib call */
5939       *total = COSTS_N_INSNS (20);
5940       return false;
5941
5942     default:
5943       return arm_rtx_costs_1 (x, outer_code, total, speed);
5944     }
5945 }
5946
5947
5948 /* RTX costs for 9e (and later) cores.  */
5949
5950 static bool
5951 arm_9e_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer_code,
5952                   int *total, bool speed)
5953 {
5954   enum machine_mode mode = GET_MODE (x);
5955
5956   if (TARGET_THUMB1)
5957     {
5958       switch (code)
5959         {
5960         case MULT:
5961           *total = COSTS_N_INSNS (3);
5962           return true;
5963
5964         default:
5965           *total = thumb1_rtx_costs (x, code, outer_code);
5966           return true;
5967         }
5968     }
5969
5970   switch (code)
5971     {
5972     case MULT:
5973       /* There is no point basing this on the tuning, since it is always the
5974          fast variant if it exists at all.  */
5975       if (mode == DImode
5976           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5977           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5978               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5979         {
5980           *total = COSTS_N_INSNS (2);
5981           return false;
5982         }
5983
5984
5985       if (mode == DImode)
5986         {
5987           *total = COSTS_N_INSNS (5);
5988           return false;
5989         }
5990
5991       if (mode == SImode)
5992         {
5993           *total = COSTS_N_INSNS (2);
5994           return false;
5995         }
5996
5997       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5998         {
5999           if (TARGET_HARD_FLOAT && (mode == SFmode || mode == DFmode))
6000             {
6001               *total = COSTS_N_INSNS (1);
6002               return false;
6003             }
6004         }
6005
6006       *total = COSTS_N_INSNS (20);
6007       return false;
6008
6009     default:
6010       return arm_rtx_costs_1 (x, outer_code, total, speed);
6011     }
6012 }
6013 /* All address computations that can be done are free, but rtx cost returns
6014    the same for practically all of them.  So we weight the different types
6015    of address here in the order (most pref first):
6016    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
6017 static inline int
6018 arm_arm_address_cost (rtx x)
6019 {
6020   enum rtx_code c  = GET_CODE (x);
6021
6022   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
6023     return 0;
6024   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
6025     return 10;
6026
6027   if (c == PLUS || c == MINUS)
6028     {
6029       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6030         return 2;
6031
6032       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
6033         return 3;
6034
6035       return 4;
6036     }
6037
6038   return 6;
6039 }
6040
6041 static inline int
6042 arm_thumb_address_cost (rtx x)
6043 {
6044   enum rtx_code c  = GET_CODE (x);
6045
6046   if (c == REG)
6047     return 1;
6048   if (c == PLUS
6049       && GET_CODE (XEXP (x, 0)) == REG
6050       && GET_CODE (XEXP (x, 1)) == CONST_INT)
6051     return 1;
6052
6053   return 2;
6054 }
6055
6056 static int
6057 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
6058 {
6059   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
6060 }
6061
6062 static int
6063 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
6064 {
6065   rtx i_pat, d_pat;
6066
6067   /* Some true dependencies can have a higher cost depending
6068      on precisely how certain input operands are used.  */
6069   if (arm_tune_xscale
6070       && REG_NOTE_KIND (link) == 0
6071       && recog_memoized (insn) >= 0
6072       && recog_memoized (dep) >= 0)
6073     {
6074       int shift_opnum = get_attr_shift (insn);
6075       enum attr_type attr_type = get_attr_type (dep);
6076
6077       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
6078          operand for INSN.  If we have a shifted input operand and the
6079          instruction we depend on is another ALU instruction, then we may
6080          have to account for an additional stall.  */
6081       if (shift_opnum != 0
6082           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
6083         {
6084           rtx shifted_operand;
6085           int opno;
6086
6087           /* Get the shifted operand.  */
6088           extract_insn (insn);
6089           shifted_operand = recog_data.operand[shift_opnum];
6090
6091           /* Iterate over all the operands in DEP.  If we write an operand
6092              that overlaps with SHIFTED_OPERAND, then we have increase the
6093              cost of this dependency.  */
6094           extract_insn (dep);
6095           preprocess_constraints ();
6096           for (opno = 0; opno < recog_data.n_operands; opno++)
6097             {
6098               /* We can ignore strict inputs.  */
6099               if (recog_data.operand_type[opno] == OP_IN)
6100                 continue;
6101
6102               if (reg_overlap_mentioned_p (recog_data.operand[opno],
6103                                            shifted_operand))
6104                 return 2;
6105             }
6106         }
6107     }
6108
6109   /* XXX This is not strictly true for the FPA.  */
6110   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
6111       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
6112     return 0;
6113
6114   /* Call insns don't incur a stall, even if they follow a load.  */
6115   if (REG_NOTE_KIND (link) == 0
6116       && GET_CODE (insn) == CALL_INSN)
6117     return 1;
6118
6119   if ((i_pat = single_set (insn)) != NULL
6120       && GET_CODE (SET_SRC (i_pat)) == MEM
6121       && (d_pat = single_set (dep)) != NULL
6122       && GET_CODE (SET_DEST (d_pat)) == MEM)
6123     {
6124       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
6125       /* This is a load after a store, there is no conflict if the load reads
6126          from a cached area.  Assume that loads from the stack, and from the
6127          constant pool are cached, and that others will miss.  This is a
6128          hack.  */
6129
6130       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
6131           || reg_mentioned_p (stack_pointer_rtx, src_mem)
6132           || reg_mentioned_p (frame_pointer_rtx, src_mem)
6133           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
6134         return 1;
6135     }
6136
6137   return cost;
6138 }
6139
6140 static int fp_consts_inited = 0;
6141
6142 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
6143 static const char * const strings_fp[8] =
6144 {
6145   "0",   "1",   "2",   "3",
6146   "4",   "5",   "0.5", "10"
6147 };
6148
6149 static REAL_VALUE_TYPE values_fp[8];
6150
6151 static void
6152 init_fp_table (void)
6153 {
6154   int i;
6155   REAL_VALUE_TYPE r;
6156
6157   if (TARGET_VFP)
6158     fp_consts_inited = 1;
6159   else
6160     fp_consts_inited = 8;
6161
6162   for (i = 0; i < fp_consts_inited; i++)
6163     {
6164       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
6165       values_fp[i] = r;
6166     }
6167 }
6168
6169 /* Return TRUE if rtx X is a valid immediate FP constant.  */
6170 int
6171 arm_const_double_rtx (rtx x)
6172 {
6173   REAL_VALUE_TYPE r;
6174   int i;
6175
6176   if (!fp_consts_inited)
6177     init_fp_table ();
6178
6179   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6180   if (REAL_VALUE_MINUS_ZERO (r))
6181     return 0;
6182
6183   for (i = 0; i < fp_consts_inited; i++)
6184     if (REAL_VALUES_EQUAL (r, values_fp[i]))
6185       return 1;
6186
6187   return 0;
6188 }
6189
6190 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
6191 int
6192 neg_const_double_rtx_ok_for_fpa (rtx x)
6193 {
6194   REAL_VALUE_TYPE r;
6195   int i;
6196
6197   if (!fp_consts_inited)
6198     init_fp_table ();
6199
6200   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6201   r = REAL_VALUE_NEGATE (r);
6202   if (REAL_VALUE_MINUS_ZERO (r))
6203     return 0;
6204
6205   for (i = 0; i < 8; i++)
6206     if (REAL_VALUES_EQUAL (r, values_fp[i]))
6207       return 1;
6208
6209   return 0;
6210 }
6211
6212
6213 /* VFPv3 has a fairly wide range of representable immediates, formed from
6214    "quarter-precision" floating-point values. These can be evaluated using this
6215    formula (with ^ for exponentiation):
6216
6217      -1^s * n * 2^-r
6218
6219    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
6220    16 <= n <= 31 and 0 <= r <= 7.
6221
6222    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
6223
6224      - A (most-significant) is the sign bit.
6225      - BCD are the exponent (encoded as r XOR 3).
6226      - EFGH are the mantissa (encoded as n - 16).
6227 */
6228
6229 /* Return an integer index for a VFPv3 immediate operand X suitable for the
6230    fconst[sd] instruction, or -1 if X isn't suitable.  */
6231 static int
6232 vfp3_const_double_index (rtx x)
6233 {
6234   REAL_VALUE_TYPE r, m;
6235   int sign, exponent;
6236   unsigned HOST_WIDE_INT mantissa, mant_hi;
6237   unsigned HOST_WIDE_INT mask;
6238   HOST_WIDE_INT m1, m2;
6239   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
6240
6241   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
6242     return -1;
6243
6244   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6245
6246   /* We can't represent these things, so detect them first.  */
6247   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
6248     return -1;
6249
6250   /* Extract sign, exponent and mantissa.  */
6251   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
6252   r = REAL_VALUE_ABS (r);
6253   exponent = REAL_EXP (&r);
6254   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
6255      highest (sign) bit, with a fixed binary point at bit point_pos.
6256      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
6257      bits for the mantissa, this may fail (low bits would be lost).  */
6258   real_ldexp (&m, &r, point_pos - exponent);
6259   REAL_VALUE_TO_INT (&m1, &m2, m);
6260   mantissa = m1;
6261   mant_hi = m2;
6262
6263   /* If there are bits set in the low part of the mantissa, we can't
6264      represent this value.  */
6265   if (mantissa != 0)
6266     return -1;
6267
6268   /* Now make it so that mantissa contains the most-significant bits, and move
6269      the point_pos to indicate that the least-significant bits have been
6270      discarded.  */
6271   point_pos -= HOST_BITS_PER_WIDE_INT;
6272   mantissa = mant_hi;
6273
6274   /* We can permit four significant bits of mantissa only, plus a high bit
6275      which is always 1.  */
6276   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
6277   if ((mantissa & mask) != 0)
6278     return -1;
6279
6280   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
6281   mantissa >>= point_pos - 5;
6282
6283   /* The mantissa may be zero. Disallow that case. (It's possible to load the
6284      floating-point immediate zero with Neon using an integer-zero load, but
6285      that case is handled elsewhere.)  */
6286   if (mantissa == 0)
6287     return -1;
6288
6289   gcc_assert (mantissa >= 16 && mantissa <= 31);
6290
6291   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
6292      normalized significands are in the range [1, 2). (Our mantissa is shifted
6293      left 4 places at this point relative to normalized IEEE754 values).  GCC
6294      internally uses [0.5, 1) (see real.c), so the exponent returned from
6295      REAL_EXP must be altered.  */
6296   exponent = 5 - exponent;
6297
6298   if (exponent < 0 || exponent > 7)
6299     return -1;
6300
6301   /* Sign, mantissa and exponent are now in the correct form to plug into the
6302      formula described in the comment above.  */
6303   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
6304 }
6305
6306 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
6307 int
6308 vfp3_const_double_rtx (rtx x)
6309 {
6310   if (!TARGET_VFP3)
6311     return 0;
6312
6313   return vfp3_const_double_index (x) != -1;
6314 }
6315
6316 /* Recognize immediates which can be used in various Neon instructions. Legal
6317    immediates are described by the following table (for VMVN variants, the
6318    bitwise inverse of the constant shown is recognized. In either case, VMOV
6319    is output and the correct instruction to use for a given constant is chosen
6320    by the assembler). The constant shown is replicated across all elements of
6321    the destination vector.
6322
6323    insn elems variant constant (binary)
6324    ---- ----- ------- -----------------
6325    vmov  i32     0    00000000 00000000 00000000 abcdefgh
6326    vmov  i32     1    00000000 00000000 abcdefgh 00000000
6327    vmov  i32     2    00000000 abcdefgh 00000000 00000000
6328    vmov  i32     3    abcdefgh 00000000 00000000 00000000
6329    vmov  i16     4    00000000 abcdefgh
6330    vmov  i16     5    abcdefgh 00000000
6331    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
6332    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
6333    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
6334    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
6335    vmvn  i16    10    00000000 abcdefgh
6336    vmvn  i16    11    abcdefgh 00000000
6337    vmov  i32    12    00000000 00000000 abcdefgh 11111111
6338    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
6339    vmov  i32    14    00000000 abcdefgh 11111111 11111111
6340    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
6341    vmov   i8    16    abcdefgh
6342    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
6343                       eeeeeeee ffffffff gggggggg hhhhhhhh
6344    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
6345
6346    For case 18, B = !b. Representable values are exactly those accepted by
6347    vfp3_const_double_index, but are output as floating-point numbers rather
6348    than indices.
6349
6350    Variants 0-5 (inclusive) may also be used as immediates for the second
6351    operand of VORR/VBIC instructions.
6352
6353    The INVERSE argument causes the bitwise inverse of the given operand to be
6354    recognized instead (used for recognizing legal immediates for the VAND/VORN
6355    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
6356    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
6357    output, rather than the real insns vbic/vorr).
6358
6359    INVERSE makes no difference to the recognition of float vectors.
6360
6361    The return value is the variant of immediate as shown in the above table, or
6362    -1 if the given value doesn't match any of the listed patterns.
6363 */
6364 static int
6365 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
6366                       rtx *modconst, int *elementwidth)
6367 {
6368 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
6369   matches = 1;                                  \
6370   for (i = 0; i < idx; i += (STRIDE))           \
6371     if (!(TEST))                                \
6372       matches = 0;                              \
6373   if (matches)                                  \
6374     {                                           \
6375       immtype = (CLASS);                        \
6376       elsize = (ELSIZE);                        \
6377       break;                                    \
6378     }
6379
6380   unsigned int i, elsize, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
6381   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
6382   unsigned char bytes[16];
6383   int immtype = -1, matches;
6384   unsigned int invmask = inverse ? 0xff : 0;
6385
6386   /* Vectors of float constants.  */
6387   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6388     {
6389       rtx el0 = CONST_VECTOR_ELT (op, 0);
6390       REAL_VALUE_TYPE r0;
6391
6392       if (!vfp3_const_double_rtx (el0))
6393         return -1;
6394
6395       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
6396
6397       for (i = 1; i < n_elts; i++)
6398         {
6399           rtx elt = CONST_VECTOR_ELT (op, i);
6400           REAL_VALUE_TYPE re;
6401
6402           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
6403
6404           if (!REAL_VALUES_EQUAL (r0, re))
6405             return -1;
6406         }
6407
6408       if (modconst)
6409         *modconst = CONST_VECTOR_ELT (op, 0);
6410
6411       if (elementwidth)
6412         *elementwidth = 0;
6413
6414       return 18;
6415     }
6416
6417   /* Splat vector constant out into a byte vector.  */
6418   for (i = 0; i < n_elts; i++)
6419     {
6420       rtx el = CONST_VECTOR_ELT (op, i);
6421       unsigned HOST_WIDE_INT elpart;
6422       unsigned int part, parts;
6423
6424       if (GET_CODE (el) == CONST_INT)
6425         {
6426           elpart = INTVAL (el);
6427           parts = 1;
6428         }
6429       else if (GET_CODE (el) == CONST_DOUBLE)
6430         {
6431           elpart = CONST_DOUBLE_LOW (el);
6432           parts = 2;
6433         }
6434       else
6435         gcc_unreachable ();
6436
6437       for (part = 0; part < parts; part++)
6438         {
6439           unsigned int byte;
6440           for (byte = 0; byte < innersize; byte++)
6441             {
6442               bytes[idx++] = (elpart & 0xff) ^ invmask;
6443               elpart >>= BITS_PER_UNIT;
6444             }
6445           if (GET_CODE (el) == CONST_DOUBLE)
6446             elpart = CONST_DOUBLE_HIGH (el);
6447         }
6448     }
6449
6450   /* Sanity check.  */
6451   gcc_assert (idx == GET_MODE_SIZE (mode));
6452
6453   do
6454     {
6455       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
6456                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6457
6458       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6459                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6460
6461       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
6462                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6463
6464       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
6465                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
6466
6467       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
6468
6469       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
6470
6471       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
6472                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6473
6474       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6475                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6476
6477       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
6478                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6479
6480       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
6481                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
6482
6483       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
6484
6485       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
6486
6487       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6488                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6489
6490       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6491                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6492
6493       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
6494                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6495
6496       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
6497                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6498
6499       CHECK (1, 8, 16, bytes[i] == bytes[0]);
6500
6501       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
6502                         && bytes[i] == bytes[(i + 8) % idx]);
6503     }
6504   while (0);
6505
6506   if (immtype == -1)
6507     return -1;
6508
6509   if (elementwidth)
6510     *elementwidth = elsize;
6511
6512   if (modconst)
6513     {
6514       unsigned HOST_WIDE_INT imm = 0;
6515
6516       /* Un-invert bytes of recognized vector, if necessary.  */
6517       if (invmask != 0)
6518         for (i = 0; i < idx; i++)
6519           bytes[i] ^= invmask;
6520
6521       if (immtype == 17)
6522         {
6523           /* FIXME: Broken on 32-bit H_W_I hosts.  */
6524           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
6525
6526           for (i = 0; i < 8; i++)
6527             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
6528                    << (i * BITS_PER_UNIT);
6529
6530           *modconst = GEN_INT (imm);
6531         }
6532       else
6533         {
6534           unsigned HOST_WIDE_INT imm = 0;
6535
6536           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
6537             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
6538
6539           *modconst = GEN_INT (imm);
6540         }
6541     }
6542
6543   return immtype;
6544 #undef CHECK
6545 }
6546
6547 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
6548    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
6549    float elements), and a modified constant (whatever should be output for a
6550    VMOV) in *MODCONST.  */
6551
6552 int
6553 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
6554                                rtx *modconst, int *elementwidth)
6555 {
6556   rtx tmpconst;
6557   int tmpwidth;
6558   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
6559
6560   if (retval == -1)
6561     return 0;
6562
6563   if (modconst)
6564     *modconst = tmpconst;
6565
6566   if (elementwidth)
6567     *elementwidth = tmpwidth;
6568
6569   return 1;
6570 }
6571
6572 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
6573    the immediate is valid, write a constant suitable for using as an operand
6574    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
6575    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
6576
6577 int
6578 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
6579                                 rtx *modconst, int *elementwidth)
6580 {
6581   rtx tmpconst;
6582   int tmpwidth;
6583   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
6584
6585   if (retval < 0 || retval > 5)
6586     return 0;
6587
6588   if (modconst)
6589     *modconst = tmpconst;
6590
6591   if (elementwidth)
6592     *elementwidth = tmpwidth;
6593
6594   return 1;
6595 }
6596
6597 /* Return a string suitable for output of Neon immediate logic operation
6598    MNEM.  */
6599
6600 char *
6601 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
6602                              int inverse, int quad)
6603 {
6604   int width, is_valid;
6605   static char templ[40];
6606
6607   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
6608
6609   gcc_assert (is_valid != 0);
6610
6611   if (quad)
6612     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
6613   else
6614     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
6615
6616   return templ;
6617 }
6618
6619 /* Output a sequence of pairwise operations to implement a reduction.
6620    NOTE: We do "too much work" here, because pairwise operations work on two
6621    registers-worth of operands in one go. Unfortunately we can't exploit those
6622    extra calculations to do the full operation in fewer steps, I don't think.
6623    Although all vector elements of the result but the first are ignored, we
6624    actually calculate the same result in each of the elements. An alternative
6625    such as initially loading a vector with zero to use as each of the second
6626    operands would use up an additional register and take an extra instruction,
6627    for no particular gain.  */
6628
6629 void
6630 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
6631                       rtx (*reduc) (rtx, rtx, rtx))
6632 {
6633   enum machine_mode inner = GET_MODE_INNER (mode);
6634   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
6635   rtx tmpsum = op1;
6636
6637   for (i = parts / 2; i >= 1; i /= 2)
6638     {
6639       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
6640       emit_insn (reduc (dest, tmpsum, tmpsum));
6641       tmpsum = dest;
6642     }
6643 }
6644
6645 /* Initialize a vector with non-constant elements.  FIXME: We can do better
6646    than the current implementation (building a vector on the stack and then
6647    loading it) in many cases.  See rs6000.c.  */
6648
6649 void
6650 neon_expand_vector_init (rtx target, rtx vals)
6651 {
6652   enum machine_mode mode = GET_MODE (target);
6653   enum machine_mode inner = GET_MODE_INNER (mode);
6654   unsigned int i, n_elts = GET_MODE_NUNITS (mode);
6655   rtx mem;
6656
6657   gcc_assert (VECTOR_MODE_P (mode));
6658
6659   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
6660   for (i = 0; i < n_elts; i++)
6661     emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
6662                    XVECEXP (vals, 0, i));
6663
6664   emit_move_insn (target, mem);
6665 }
6666
6667 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
6668    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
6669    reported source locations are bogus.  */
6670
6671 static void
6672 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
6673               const char *err)
6674 {
6675   HOST_WIDE_INT lane;
6676
6677   gcc_assert (GET_CODE (operand) == CONST_INT);
6678
6679   lane = INTVAL (operand);
6680
6681   if (lane < low || lane >= high)
6682     error (err);
6683 }
6684
6685 /* Bounds-check lanes.  */
6686
6687 void
6688 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6689 {
6690   bounds_check (operand, low, high, "lane out of range");
6691 }
6692
6693 /* Bounds-check constants.  */
6694
6695 void
6696 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6697 {
6698   bounds_check (operand, low, high, "constant out of range");
6699 }
6700
6701 HOST_WIDE_INT
6702 neon_element_bits (enum machine_mode mode)
6703 {
6704   if (mode == DImode)
6705     return GET_MODE_BITSIZE (mode);
6706   else
6707     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
6708 }
6709
6710 \f
6711 /* Predicates for `match_operand' and `match_operator'.  */
6712
6713 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
6714 int
6715 cirrus_memory_offset (rtx op)
6716 {
6717   /* Reject eliminable registers.  */
6718   if (! (reload_in_progress || reload_completed)
6719       && (   reg_mentioned_p (frame_pointer_rtx, op)
6720           || reg_mentioned_p (arg_pointer_rtx, op)
6721           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6722           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6723           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6724           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6725     return 0;
6726
6727   if (GET_CODE (op) == MEM)
6728     {
6729       rtx ind;
6730
6731       ind = XEXP (op, 0);
6732
6733       /* Match: (mem (reg)).  */
6734       if (GET_CODE (ind) == REG)
6735         return 1;
6736
6737       /* Match:
6738          (mem (plus (reg)
6739                     (const))).  */
6740       if (GET_CODE (ind) == PLUS
6741           && GET_CODE (XEXP (ind, 0)) == REG
6742           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6743           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
6744         return 1;
6745     }
6746
6747   return 0;
6748 }
6749
6750 /* Return TRUE if OP is a valid coprocessor memory address pattern.
6751    WB is true if full writeback address modes are allowed and is false
6752    if limited writeback address modes (POST_INC and PRE_DEC) are
6753    allowed.  */
6754
6755 int
6756 arm_coproc_mem_operand (rtx op, bool wb)
6757 {
6758   rtx ind;
6759
6760   /* Reject eliminable registers.  */
6761   if (! (reload_in_progress || reload_completed)
6762       && (   reg_mentioned_p (frame_pointer_rtx, op)
6763           || reg_mentioned_p (arg_pointer_rtx, op)
6764           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6765           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6766           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6767           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6768     return FALSE;
6769
6770   /* Constants are converted into offsets from labels.  */
6771   if (GET_CODE (op) != MEM)
6772     return FALSE;
6773
6774   ind = XEXP (op, 0);
6775
6776   if (reload_completed
6777       && (GET_CODE (ind) == LABEL_REF
6778           || (GET_CODE (ind) == CONST
6779               && GET_CODE (XEXP (ind, 0)) == PLUS
6780               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6781               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6782     return TRUE;
6783
6784   /* Match: (mem (reg)).  */
6785   if (GET_CODE (ind) == REG)
6786     return arm_address_register_rtx_p (ind, 0);
6787
6788   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
6789      acceptable in any case (subject to verification by
6790      arm_address_register_rtx_p).  We need WB to be true to accept
6791      PRE_INC and POST_DEC.  */
6792   if (GET_CODE (ind) == POST_INC
6793       || GET_CODE (ind) == PRE_DEC
6794       || (wb
6795           && (GET_CODE (ind) == PRE_INC
6796               || GET_CODE (ind) == POST_DEC)))
6797     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6798
6799   if (wb
6800       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
6801       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6802       && GET_CODE (XEXP (ind, 1)) == PLUS
6803       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6804     ind = XEXP (ind, 1);
6805
6806   /* Match:
6807      (plus (reg)
6808            (const)).  */
6809   if (GET_CODE (ind) == PLUS
6810       && GET_CODE (XEXP (ind, 0)) == REG
6811       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6812       && GET_CODE (XEXP (ind, 1)) == CONST_INT
6813       && INTVAL (XEXP (ind, 1)) > -1024
6814       && INTVAL (XEXP (ind, 1)) <  1024
6815       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6816     return TRUE;
6817
6818   return FALSE;
6819 }
6820
6821 /* Return TRUE if OP is a memory operand which we can load or store a vector
6822    to/from. If CORE is true, we're moving from ARM registers not Neon
6823    registers.  */
6824 int
6825 neon_vector_mem_operand (rtx op, bool core)
6826 {
6827   rtx ind;
6828
6829   /* Reject eliminable registers.  */
6830   if (! (reload_in_progress || reload_completed)
6831       && (   reg_mentioned_p (frame_pointer_rtx, op)
6832           || reg_mentioned_p (arg_pointer_rtx, op)
6833           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6834           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6835           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6836           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6837     return FALSE;
6838
6839   /* Constants are converted into offsets from labels.  */
6840   if (GET_CODE (op) != MEM)
6841     return FALSE;
6842
6843   ind = XEXP (op, 0);
6844
6845   if (reload_completed
6846       && (GET_CODE (ind) == LABEL_REF
6847           || (GET_CODE (ind) == CONST
6848               && GET_CODE (XEXP (ind, 0)) == PLUS
6849               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6850               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6851     return TRUE;
6852
6853   /* Match: (mem (reg)).  */
6854   if (GET_CODE (ind) == REG)
6855     return arm_address_register_rtx_p (ind, 0);
6856
6857   /* Allow post-increment with Neon registers.  */
6858   if (!core && GET_CODE (ind) == POST_INC)
6859     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6860
6861 #if 0
6862   /* FIXME: We can support this too if we use VLD1/VST1.  */
6863   if (!core
6864       && GET_CODE (ind) == POST_MODIFY
6865       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6866       && GET_CODE (XEXP (ind, 1)) == PLUS
6867       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6868     ind = XEXP (ind, 1);
6869 #endif
6870
6871   /* Match:
6872      (plus (reg)
6873           (const)).  */
6874   if (!core
6875       && GET_CODE (ind) == PLUS
6876       && GET_CODE (XEXP (ind, 0)) == REG
6877       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6878       && GET_CODE (XEXP (ind, 1)) == CONST_INT
6879       && INTVAL (XEXP (ind, 1)) > -1024
6880       && INTVAL (XEXP (ind, 1)) < 1016
6881       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6882     return TRUE;
6883
6884   return FALSE;
6885 }
6886
6887 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
6888    type.  */
6889 int
6890 neon_struct_mem_operand (rtx op)
6891 {
6892   rtx ind;
6893
6894   /* Reject eliminable registers.  */
6895   if (! (reload_in_progress || reload_completed)
6896       && (   reg_mentioned_p (frame_pointer_rtx, op)
6897           || reg_mentioned_p (arg_pointer_rtx, op)
6898           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6899           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6900           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6901           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6902     return FALSE;
6903
6904   /* Constants are converted into offsets from labels.  */
6905   if (GET_CODE (op) != MEM)
6906     return FALSE;
6907
6908   ind = XEXP (op, 0);
6909
6910   if (reload_completed
6911       && (GET_CODE (ind) == LABEL_REF
6912           || (GET_CODE (ind) == CONST
6913               && GET_CODE (XEXP (ind, 0)) == PLUS
6914               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6915               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6916     return TRUE;
6917
6918   /* Match: (mem (reg)).  */
6919   if (GET_CODE (ind) == REG)
6920     return arm_address_register_rtx_p (ind, 0);
6921
6922   return FALSE;
6923 }
6924
6925 /* Return true if X is a register that will be eliminated later on.  */
6926 int
6927 arm_eliminable_register (rtx x)
6928 {
6929   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
6930                        || REGNO (x) == ARG_POINTER_REGNUM
6931                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
6932                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
6933 }
6934
6935 /* Return GENERAL_REGS if a scratch register required to reload x to/from
6936    coprocessor registers.  Otherwise return NO_REGS.  */
6937
6938 enum reg_class
6939 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
6940 {
6941   if (TARGET_NEON
6942       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
6943           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6944       && neon_vector_mem_operand (x, FALSE))
6945      return NO_REGS;
6946
6947   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
6948     return NO_REGS;
6949
6950   return GENERAL_REGS;
6951 }
6952
6953 /* Values which must be returned in the most-significant end of the return
6954    register.  */
6955
6956 static bool
6957 arm_return_in_msb (const_tree valtype)
6958 {
6959   return (TARGET_AAPCS_BASED
6960           && BYTES_BIG_ENDIAN
6961           && (AGGREGATE_TYPE_P (valtype)
6962               || TREE_CODE (valtype) == COMPLEX_TYPE));
6963 }
6964
6965 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
6966    Use by the Cirrus Maverick code which has to workaround
6967    a hardware bug triggered by such instructions.  */
6968 static bool
6969 arm_memory_load_p (rtx insn)
6970 {
6971   rtx body, lhs, rhs;;
6972
6973   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
6974     return false;
6975
6976   body = PATTERN (insn);
6977
6978   if (GET_CODE (body) != SET)
6979     return false;
6980
6981   lhs = XEXP (body, 0);
6982   rhs = XEXP (body, 1);
6983
6984   lhs = REG_OR_SUBREG_RTX (lhs);
6985
6986   /* If the destination is not a general purpose
6987      register we do not have to worry.  */
6988   if (GET_CODE (lhs) != REG
6989       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
6990     return false;
6991
6992   /* As well as loads from memory we also have to react
6993      to loads of invalid constants which will be turned
6994      into loads from the minipool.  */
6995   return (GET_CODE (rhs) == MEM
6996           || GET_CODE (rhs) == SYMBOL_REF
6997           || note_invalid_constants (insn, -1, false));
6998 }
6999
7000 /* Return TRUE if INSN is a Cirrus instruction.  */
7001 static bool
7002 arm_cirrus_insn_p (rtx insn)
7003 {
7004   enum attr_cirrus attr;
7005
7006   /* get_attr cannot accept USE or CLOBBER.  */
7007   if (!insn
7008       || GET_CODE (insn) != INSN
7009       || GET_CODE (PATTERN (insn)) == USE
7010       || GET_CODE (PATTERN (insn)) == CLOBBER)
7011     return 0;
7012
7013   attr = get_attr_cirrus (insn);
7014
7015   return attr != CIRRUS_NOT;
7016 }
7017
7018 /* Cirrus reorg for invalid instruction combinations.  */
7019 static void
7020 cirrus_reorg (rtx first)
7021 {
7022   enum attr_cirrus attr;
7023   rtx body = PATTERN (first);
7024   rtx t;
7025   int nops;
7026
7027   /* Any branch must be followed by 2 non Cirrus instructions.  */
7028   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
7029     {
7030       nops = 0;
7031       t = next_nonnote_insn (first);
7032
7033       if (arm_cirrus_insn_p (t))
7034         ++ nops;
7035
7036       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
7037         ++ nops;
7038
7039       while (nops --)
7040         emit_insn_after (gen_nop (), first);
7041
7042       return;
7043     }
7044
7045   /* (float (blah)) is in parallel with a clobber.  */
7046   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
7047     body = XVECEXP (body, 0, 0);
7048
7049   if (GET_CODE (body) == SET)
7050     {
7051       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
7052
7053       /* cfldrd, cfldr64, cfstrd, cfstr64 must
7054          be followed by a non Cirrus insn.  */
7055       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
7056         {
7057           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
7058             emit_insn_after (gen_nop (), first);
7059
7060           return;
7061         }
7062       else if (arm_memory_load_p (first))
7063         {
7064           unsigned int arm_regno;
7065
7066           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
7067              ldr/cfmv64hr combination where the Rd field is the same
7068              in both instructions must be split with a non Cirrus
7069              insn.  Example:
7070
7071              ldr r0, blah
7072              nop
7073              cfmvsr mvf0, r0.  */
7074
7075           /* Get Arm register number for ldr insn.  */
7076           if (GET_CODE (lhs) == REG)
7077             arm_regno = REGNO (lhs);
7078           else
7079             {
7080               gcc_assert (GET_CODE (rhs) == REG);
7081               arm_regno = REGNO (rhs);
7082             }
7083
7084           /* Next insn.  */
7085           first = next_nonnote_insn (first);
7086
7087           if (! arm_cirrus_insn_p (first))
7088             return;
7089
7090           body = PATTERN (first);
7091
7092           /* (float (blah)) is in parallel with a clobber.  */
7093           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
7094             body = XVECEXP (body, 0, 0);
7095
7096           if (GET_CODE (body) == FLOAT)
7097             body = XEXP (body, 0);
7098
7099           if (get_attr_cirrus (first) == CIRRUS_MOVE
7100               && GET_CODE (XEXP (body, 1)) == REG
7101               && arm_regno == REGNO (XEXP (body, 1)))
7102             emit_insn_after (gen_nop (), first);
7103
7104           return;
7105         }
7106     }
7107
7108   /* get_attr cannot accept USE or CLOBBER.  */
7109   if (!first
7110       || GET_CODE (first) != INSN
7111       || GET_CODE (PATTERN (first)) == USE
7112       || GET_CODE (PATTERN (first)) == CLOBBER)
7113     return;
7114
7115   attr = get_attr_cirrus (first);
7116
7117   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
7118      must be followed by a non-coprocessor instruction.  */
7119   if (attr == CIRRUS_COMPARE)
7120     {
7121       nops = 0;
7122
7123       t = next_nonnote_insn (first);
7124
7125       if (arm_cirrus_insn_p (t))
7126         ++ nops;
7127
7128       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
7129         ++ nops;
7130
7131       while (nops --)
7132         emit_insn_after (gen_nop (), first);
7133
7134       return;
7135     }
7136 }
7137
7138 /* Return TRUE if X references a SYMBOL_REF.  */
7139 int
7140 symbol_mentioned_p (rtx x)
7141 {
7142   const char * fmt;
7143   int i;
7144
7145   if (GET_CODE (x) == SYMBOL_REF)
7146     return 1;
7147
7148   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
7149      are constant offsets, not symbols.  */
7150   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
7151     return 0;
7152
7153   fmt = GET_RTX_FORMAT (GET_CODE (x));
7154
7155   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7156     {
7157       if (fmt[i] == 'E')
7158         {
7159           int j;
7160
7161           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7162             if (symbol_mentioned_p (XVECEXP (x, i, j)))
7163               return 1;
7164         }
7165       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
7166         return 1;
7167     }
7168
7169   return 0;
7170 }
7171
7172 /* Return TRUE if X references a LABEL_REF.  */
7173 int
7174 label_mentioned_p (rtx x)
7175 {
7176   const char * fmt;
7177   int i;
7178
7179   if (GET_CODE (x) == LABEL_REF)
7180     return 1;
7181
7182   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
7183      instruction, but they are constant offsets, not symbols.  */
7184   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
7185     return 0;
7186
7187   fmt = GET_RTX_FORMAT (GET_CODE (x));
7188   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
7189     {
7190       if (fmt[i] == 'E')
7191         {
7192           int j;
7193
7194           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7195             if (label_mentioned_p (XVECEXP (x, i, j)))
7196               return 1;
7197         }
7198       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
7199         return 1;
7200     }
7201
7202   return 0;
7203 }
7204
7205 int
7206 tls_mentioned_p (rtx x)
7207 {
7208   switch (GET_CODE (x))
7209     {
7210     case CONST:
7211       return tls_mentioned_p (XEXP (x, 0));
7212
7213     case UNSPEC:
7214       if (XINT (x, 1) == UNSPEC_TLS)
7215         return 1;
7216
7217     default:
7218       return 0;
7219     }
7220 }
7221
7222 /* Must not copy a SET whose source operand is PC-relative.  */
7223
7224 static bool
7225 arm_cannot_copy_insn_p (rtx insn)
7226 {
7227   rtx pat = PATTERN (insn);
7228
7229   if (GET_CODE (pat) == SET)
7230     {
7231       rtx rhs = SET_SRC (pat);
7232
7233       if (GET_CODE (rhs) == UNSPEC
7234           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
7235         return TRUE;
7236
7237       if (GET_CODE (rhs) == MEM
7238           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
7239           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
7240         return TRUE;
7241     }
7242
7243   return FALSE;
7244 }
7245
7246 enum rtx_code
7247 minmax_code (rtx x)
7248 {
7249   enum rtx_code code = GET_CODE (x);
7250
7251   switch (code)
7252     {
7253     case SMAX:
7254       return GE;
7255     case SMIN:
7256       return LE;
7257     case UMIN:
7258       return LEU;
7259     case UMAX:
7260       return GEU;
7261     default:
7262       gcc_unreachable ();
7263     }
7264 }
7265
7266 /* Return 1 if memory locations are adjacent.  */
7267 int
7268 adjacent_mem_locations (rtx a, rtx b)
7269 {
7270   /* We don't guarantee to preserve the order of these memory refs.  */
7271   if (volatile_refs_p (a) || volatile_refs_p (b))
7272     return 0;
7273
7274   if ((GET_CODE (XEXP (a, 0)) == REG
7275        || (GET_CODE (XEXP (a, 0)) == PLUS
7276            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
7277       && (GET_CODE (XEXP (b, 0)) == REG
7278           || (GET_CODE (XEXP (b, 0)) == PLUS
7279               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
7280     {
7281       HOST_WIDE_INT val0 = 0, val1 = 0;
7282       rtx reg0, reg1;
7283       int val_diff;
7284
7285       if (GET_CODE (XEXP (a, 0)) == PLUS)
7286         {
7287           reg0 = XEXP (XEXP (a, 0), 0);
7288           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
7289         }
7290       else
7291         reg0 = XEXP (a, 0);
7292
7293       if (GET_CODE (XEXP (b, 0)) == PLUS)
7294         {
7295           reg1 = XEXP (XEXP (b, 0), 0);
7296           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
7297         }
7298       else
7299         reg1 = XEXP (b, 0);
7300
7301       /* Don't accept any offset that will require multiple
7302          instructions to handle, since this would cause the
7303          arith_adjacentmem pattern to output an overlong sequence.  */
7304       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
7305         return 0;
7306
7307       /* Don't allow an eliminable register: register elimination can make
7308          the offset too large.  */
7309       if (arm_eliminable_register (reg0))
7310         return 0;
7311
7312       val_diff = val1 - val0;
7313
7314       if (arm_ld_sched)
7315         {
7316           /* If the target has load delay slots, then there's no benefit
7317              to using an ldm instruction unless the offset is zero and
7318              we are optimizing for size.  */
7319           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
7320                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
7321                   && (val_diff == 4 || val_diff == -4));
7322         }
7323
7324       return ((REGNO (reg0) == REGNO (reg1))
7325               && (val_diff == 4 || val_diff == -4));
7326     }
7327
7328   return 0;
7329 }
7330
7331 int
7332 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7333                         HOST_WIDE_INT *load_offset)
7334 {
7335   int unsorted_regs[4];
7336   HOST_WIDE_INT unsorted_offsets[4];
7337   int order[4];
7338   int base_reg = -1;
7339   int i;
7340
7341   /* Can only handle 2, 3, or 4 insns at present,
7342      though could be easily extended if required.  */
7343   gcc_assert (nops >= 2 && nops <= 4);
7344
7345   memset (order, 0, 4 * sizeof (int));
7346
7347   /* Loop over the operands and check that the memory references are
7348      suitable (i.e. immediate offsets from the same base register).  At
7349      the same time, extract the target register, and the memory
7350      offsets.  */
7351   for (i = 0; i < nops; i++)
7352     {
7353       rtx reg;
7354       rtx offset;
7355
7356       /* Convert a subreg of a mem into the mem itself.  */
7357       if (GET_CODE (operands[nops + i]) == SUBREG)
7358         operands[nops + i] = alter_subreg (operands + (nops + i));
7359
7360       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7361
7362       /* Don't reorder volatile memory references; it doesn't seem worth
7363          looking for the case where the order is ok anyway.  */
7364       if (MEM_VOLATILE_P (operands[nops + i]))
7365         return 0;
7366
7367       offset = const0_rtx;
7368
7369       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7370            || (GET_CODE (reg) == SUBREG
7371                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7372           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7373               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7374                    == REG)
7375                   || (GET_CODE (reg) == SUBREG
7376                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7377               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7378                   == CONST_INT)))
7379         {
7380           if (i == 0)
7381             {
7382               base_reg = REGNO (reg);
7383               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7384                                   ? REGNO (operands[i])
7385                                   : REGNO (SUBREG_REG (operands[i])));
7386               order[0] = 0;
7387             }
7388           else
7389             {
7390               if (base_reg != (int) REGNO (reg))
7391                 /* Not addressed from the same base register.  */
7392                 return 0;
7393
7394               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7395                                   ? REGNO (operands[i])
7396                                   : REGNO (SUBREG_REG (operands[i])));
7397               if (unsorted_regs[i] < unsorted_regs[order[0]])
7398                 order[0] = i;
7399             }
7400
7401           /* If it isn't an integer register, or if it overwrites the
7402              base register but isn't the last insn in the list, then
7403              we can't do this.  */
7404           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
7405               || (i != nops - 1 && unsorted_regs[i] == base_reg))
7406             return 0;
7407
7408           unsorted_offsets[i] = INTVAL (offset);
7409         }
7410       else
7411         /* Not a suitable memory address.  */
7412         return 0;
7413     }
7414
7415   /* All the useful information has now been extracted from the
7416      operands into unsorted_regs and unsorted_offsets; additionally,
7417      order[0] has been set to the lowest numbered register in the
7418      list.  Sort the registers into order, and check that the memory
7419      offsets are ascending and adjacent.  */
7420
7421   for (i = 1; i < nops; i++)
7422     {
7423       int j;
7424
7425       order[i] = order[i - 1];
7426       for (j = 0; j < nops; j++)
7427         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7428             && (order[i] == order[i - 1]
7429                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7430           order[i] = j;
7431
7432       /* Have we found a suitable register? if not, one must be used more
7433          than once.  */
7434       if (order[i] == order[i - 1])
7435         return 0;
7436
7437       /* Is the memory address adjacent and ascending? */
7438       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7439         return 0;
7440     }
7441
7442   if (base)
7443     {
7444       *base = base_reg;
7445
7446       for (i = 0; i < nops; i++)
7447         regs[i] = unsorted_regs[order[i]];
7448
7449       *load_offset = unsorted_offsets[order[0]];
7450     }
7451
7452   if (unsorted_offsets[order[0]] == 0)
7453     return 1; /* ldmia */
7454
7455   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
7456     return 2; /* ldmib */
7457
7458   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
7459     return 3; /* ldmda */
7460
7461   if (unsorted_offsets[order[nops - 1]] == -4)
7462     return 4; /* ldmdb */
7463
7464   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
7465      if the offset isn't small enough.  The reason 2 ldrs are faster
7466      is because these ARMs are able to do more than one cache access
7467      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
7468      whilst the ARM8 has a double bandwidth cache.  This means that
7469      these cores can do both an instruction fetch and a data fetch in
7470      a single cycle, so the trick of calculating the address into a
7471      scratch register (one of the result regs) and then doing a load
7472      multiple actually becomes slower (and no smaller in code size).
7473      That is the transformation
7474
7475         ldr     rd1, [rbase + offset]
7476         ldr     rd2, [rbase + offset + 4]
7477
7478      to
7479
7480         add     rd1, rbase, offset
7481         ldmia   rd1, {rd1, rd2}
7482
7483      produces worse code -- '3 cycles + any stalls on rd2' instead of
7484      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
7485      access per cycle, the first sequence could never complete in less
7486      than 6 cycles, whereas the ldm sequence would only take 5 and
7487      would make better use of sequential accesses if not hitting the
7488      cache.
7489
7490      We cheat here and test 'arm_ld_sched' which we currently know to
7491      only be true for the ARM8, ARM9 and StrongARM.  If this ever
7492      changes, then the test below needs to be reworked.  */
7493   if (nops == 2 && arm_ld_sched)
7494     return 0;
7495
7496   /* Can't do it without setting up the offset, only do this if it takes
7497      no more than one insn.  */
7498   return (const_ok_for_arm (unsorted_offsets[order[0]])
7499           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
7500 }
7501
7502 const char *
7503 emit_ldm_seq (rtx *operands, int nops)
7504 {
7505   int regs[4];
7506   int base_reg;
7507   HOST_WIDE_INT offset;
7508   char buf[100];
7509   int i;
7510
7511   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7512     {
7513     case 1:
7514       strcpy (buf, "ldm%(ia%)\t");
7515       break;
7516
7517     case 2:
7518       strcpy (buf, "ldm%(ib%)\t");
7519       break;
7520
7521     case 3:
7522       strcpy (buf, "ldm%(da%)\t");
7523       break;
7524
7525     case 4:
7526       strcpy (buf, "ldm%(db%)\t");
7527       break;
7528
7529     case 5:
7530       if (offset >= 0)
7531         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7532                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7533                  (long) offset);
7534       else
7535         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7536                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7537                  (long) -offset);
7538       output_asm_insn (buf, operands);
7539       base_reg = regs[0];
7540       strcpy (buf, "ldm%(ia%)\t");
7541       break;
7542
7543     default:
7544       gcc_unreachable ();
7545     }
7546
7547   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7548            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7549
7550   for (i = 1; i < nops; i++)
7551     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7552              reg_names[regs[i]]);
7553
7554   strcat (buf, "}\t%@ phole ldm");
7555
7556   output_asm_insn (buf, operands);
7557   return "";
7558 }
7559
7560 int
7561 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7562                          HOST_WIDE_INT * load_offset)
7563 {
7564   int unsorted_regs[4];
7565   HOST_WIDE_INT unsorted_offsets[4];
7566   int order[4];
7567   int base_reg = -1;
7568   int i;
7569
7570   /* Can only handle 2, 3, or 4 insns at present, though could be easily
7571      extended if required.  */
7572   gcc_assert (nops >= 2 && nops <= 4);
7573
7574   memset (order, 0, 4 * sizeof (int));
7575
7576   /* Loop over the operands and check that the memory references are
7577      suitable (i.e. immediate offsets from the same base register).  At
7578      the same time, extract the target register, and the memory
7579      offsets.  */
7580   for (i = 0; i < nops; i++)
7581     {
7582       rtx reg;
7583       rtx offset;
7584
7585       /* Convert a subreg of a mem into the mem itself.  */
7586       if (GET_CODE (operands[nops + i]) == SUBREG)
7587         operands[nops + i] = alter_subreg (operands + (nops + i));
7588
7589       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7590
7591       /* Don't reorder volatile memory references; it doesn't seem worth
7592          looking for the case where the order is ok anyway.  */
7593       if (MEM_VOLATILE_P (operands[nops + i]))
7594         return 0;
7595
7596       offset = const0_rtx;
7597
7598       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7599            || (GET_CODE (reg) == SUBREG
7600                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7601           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7602               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7603                    == REG)
7604                   || (GET_CODE (reg) == SUBREG
7605                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7606               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7607                   == CONST_INT)))
7608         {
7609           if (i == 0)
7610             {
7611               base_reg = REGNO (reg);
7612               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7613                                   ? REGNO (operands[i])
7614                                   : REGNO (SUBREG_REG (operands[i])));
7615               order[0] = 0;
7616             }
7617           else
7618             {
7619               if (base_reg != (int) REGNO (reg))
7620                 /* Not addressed from the same base register.  */
7621                 return 0;
7622
7623               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7624                                   ? REGNO (operands[i])
7625                                   : REGNO (SUBREG_REG (operands[i])));
7626               if (unsorted_regs[i] < unsorted_regs[order[0]])
7627                 order[0] = i;
7628             }
7629
7630           /* If it isn't an integer register, then we can't do this.  */
7631           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
7632             return 0;
7633
7634           unsorted_offsets[i] = INTVAL (offset);
7635         }
7636       else
7637         /* Not a suitable memory address.  */
7638         return 0;
7639     }
7640
7641   /* All the useful information has now been extracted from the
7642      operands into unsorted_regs and unsorted_offsets; additionally,
7643      order[0] has been set to the lowest numbered register in the
7644      list.  Sort the registers into order, and check that the memory
7645      offsets are ascending and adjacent.  */
7646
7647   for (i = 1; i < nops; i++)
7648     {
7649       int j;
7650
7651       order[i] = order[i - 1];
7652       for (j = 0; j < nops; j++)
7653         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7654             && (order[i] == order[i - 1]
7655                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7656           order[i] = j;
7657
7658       /* Have we found a suitable register? if not, one must be used more
7659          than once.  */
7660       if (order[i] == order[i - 1])
7661         return 0;
7662
7663       /* Is the memory address adjacent and ascending? */
7664       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7665         return 0;
7666     }
7667
7668   if (base)
7669     {
7670       *base = base_reg;
7671
7672       for (i = 0; i < nops; i++)
7673         regs[i] = unsorted_regs[order[i]];
7674
7675       *load_offset = unsorted_offsets[order[0]];
7676     }
7677
7678   if (unsorted_offsets[order[0]] == 0)
7679     return 1; /* stmia */
7680
7681   if (unsorted_offsets[order[0]] == 4)
7682     return 2; /* stmib */
7683
7684   if (unsorted_offsets[order[nops - 1]] == 0)
7685     return 3; /* stmda */
7686
7687   if (unsorted_offsets[order[nops - 1]] == -4)
7688     return 4; /* stmdb */
7689
7690   return 0;
7691 }
7692
7693 const char *
7694 emit_stm_seq (rtx *operands, int nops)
7695 {
7696   int regs[4];
7697   int base_reg;
7698   HOST_WIDE_INT offset;
7699   char buf[100];
7700   int i;
7701
7702   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7703     {
7704     case 1:
7705       strcpy (buf, "stm%(ia%)\t");
7706       break;
7707
7708     case 2:
7709       strcpy (buf, "stm%(ib%)\t");
7710       break;
7711
7712     case 3:
7713       strcpy (buf, "stm%(da%)\t");
7714       break;
7715
7716     case 4:
7717       strcpy (buf, "stm%(db%)\t");
7718       break;
7719
7720     default:
7721       gcc_unreachable ();
7722     }
7723
7724   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7725            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7726
7727   for (i = 1; i < nops; i++)
7728     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7729              reg_names[regs[i]]);
7730
7731   strcat (buf, "}\t%@ phole stm");
7732
7733   output_asm_insn (buf, operands);
7734   return "";
7735 }
7736 \f
7737 /* Routines for use in generating RTL.  */
7738
7739 rtx
7740 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
7741                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
7742 {
7743   HOST_WIDE_INT offset = *offsetp;
7744   int i = 0, j;
7745   rtx result;
7746   int sign = up ? 1 : -1;
7747   rtx mem, addr;
7748
7749   /* XScale has load-store double instructions, but they have stricter
7750      alignment requirements than load-store multiple, so we cannot
7751      use them.
7752
7753      For XScale ldm requires 2 + NREGS cycles to complete and blocks
7754      the pipeline until completion.
7755
7756         NREGS           CYCLES
7757           1               3
7758           2               4
7759           3               5
7760           4               6
7761
7762      An ldr instruction takes 1-3 cycles, but does not block the
7763      pipeline.
7764
7765         NREGS           CYCLES
7766           1              1-3
7767           2              2-6
7768           3              3-9
7769           4              4-12
7770
7771      Best case ldr will always win.  However, the more ldr instructions
7772      we issue, the less likely we are to be able to schedule them well.
7773      Using ldr instructions also increases code size.
7774
7775      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
7776      for counts of 3 or 4 regs.  */
7777   if (arm_tune_xscale && count <= 2 && ! optimize_size)
7778     {
7779       rtx seq;
7780
7781       start_sequence ();
7782
7783       for (i = 0; i < count; i++)
7784         {
7785           addr = plus_constant (from, i * 4 * sign);
7786           mem = adjust_automodify_address (basemem, SImode, addr, offset);
7787           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
7788           offset += 4 * sign;
7789         }
7790
7791       if (write_back)
7792         {
7793           emit_move_insn (from, plus_constant (from, count * 4 * sign));
7794           *offsetp = offset;
7795         }
7796
7797       seq = get_insns ();
7798       end_sequence ();
7799
7800       return seq;
7801     }
7802
7803   result = gen_rtx_PARALLEL (VOIDmode,
7804                              rtvec_alloc (count + (write_back ? 1 : 0)));
7805   if (write_back)
7806     {
7807       XVECEXP (result, 0, 0)
7808         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
7809       i = 1;
7810       count++;
7811     }
7812
7813   for (j = 0; i < count; i++, j++)
7814     {
7815       addr = plus_constant (from, j * 4 * sign);
7816       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
7817       XVECEXP (result, 0, i)
7818         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
7819       offset += 4 * sign;
7820     }
7821
7822   if (write_back)
7823     *offsetp = offset;
7824
7825   return result;
7826 }
7827
7828 rtx
7829 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
7830                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
7831 {
7832   HOST_WIDE_INT offset = *offsetp;
7833   int i = 0, j;
7834   rtx result;
7835   int sign = up ? 1 : -1;
7836   rtx mem, addr;
7837
7838   /* See arm_gen_load_multiple for discussion of
7839      the pros/cons of ldm/stm usage for XScale.  */
7840   if (arm_tune_xscale && count <= 2 && ! optimize_size)
7841     {
7842       rtx seq;
7843
7844       start_sequence ();
7845
7846       for (i = 0; i < count; i++)
7847         {
7848           addr = plus_constant (to, i * 4 * sign);
7849           mem = adjust_automodify_address (basemem, SImode, addr, offset);
7850           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
7851           offset += 4 * sign;
7852         }
7853
7854       if (write_back)
7855         {
7856           emit_move_insn (to, plus_constant (to, count * 4 * sign));
7857           *offsetp = offset;
7858         }
7859
7860       seq = get_insns ();
7861       end_sequence ();
7862
7863       return seq;
7864     }
7865
7866   result = gen_rtx_PARALLEL (VOIDmode,
7867                              rtvec_alloc (count + (write_back ? 1 : 0)));
7868   if (write_back)
7869     {
7870       XVECEXP (result, 0, 0)
7871         = gen_rtx_SET (VOIDmode, to,
7872                        plus_constant (to, count * 4 * sign));
7873       i = 1;
7874       count++;
7875     }
7876
7877   for (j = 0; i < count; i++, j++)
7878     {
7879       addr = plus_constant (to, j * 4 * sign);
7880       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
7881       XVECEXP (result, 0, i)
7882         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
7883       offset += 4 * sign;
7884     }
7885
7886   if (write_back)
7887     *offsetp = offset;
7888
7889   return result;
7890 }
7891
7892 int
7893 arm_gen_movmemqi (rtx *operands)
7894 {
7895   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
7896   HOST_WIDE_INT srcoffset, dstoffset;
7897   int i;
7898   rtx src, dst, srcbase, dstbase;
7899   rtx part_bytes_reg = NULL;
7900   rtx mem;
7901
7902   if (GET_CODE (operands[2]) != CONST_INT
7903       || GET_CODE (operands[3]) != CONST_INT
7904       || INTVAL (operands[2]) > 64
7905       || INTVAL (operands[3]) & 3)
7906     return 0;
7907
7908   dstbase = operands[0];
7909   srcbase = operands[1];
7910
7911   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
7912   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
7913
7914   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
7915   out_words_to_go = INTVAL (operands[2]) / 4;
7916   last_bytes = INTVAL (operands[2]) & 3;
7917   dstoffset = srcoffset = 0;
7918
7919   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
7920     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
7921
7922   for (i = 0; in_words_to_go >= 2; i+=4)
7923     {
7924       if (in_words_to_go > 4)
7925         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
7926                                           srcbase, &srcoffset));
7927       else
7928         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
7929                                           FALSE, srcbase, &srcoffset));
7930
7931       if (out_words_to_go)
7932         {
7933           if (out_words_to_go > 4)
7934             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
7935                                                dstbase, &dstoffset));
7936           else if (out_words_to_go != 1)
7937             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
7938                                                dst, TRUE,
7939                                                (last_bytes == 0
7940                                                 ? FALSE : TRUE),
7941                                                dstbase, &dstoffset));
7942           else
7943             {
7944               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
7945               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
7946               if (last_bytes != 0)
7947                 {
7948                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
7949                   dstoffset += 4;
7950                 }
7951             }
7952         }
7953
7954       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
7955       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
7956     }
7957
7958   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
7959   if (out_words_to_go)
7960     {
7961       rtx sreg;
7962
7963       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7964       sreg = copy_to_reg (mem);
7965
7966       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
7967       emit_move_insn (mem, sreg);
7968       in_words_to_go--;
7969
7970       gcc_assert (!in_words_to_go);     /* Sanity check */
7971     }
7972
7973   if (in_words_to_go)
7974     {
7975       gcc_assert (in_words_to_go > 0);
7976
7977       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7978       part_bytes_reg = copy_to_mode_reg (SImode, mem);
7979     }
7980
7981   gcc_assert (!last_bytes || part_bytes_reg);
7982
7983   if (BYTES_BIG_ENDIAN && last_bytes)
7984     {
7985       rtx tmp = gen_reg_rtx (SImode);
7986
7987       /* The bytes we want are in the top end of the word.  */
7988       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
7989                               GEN_INT (8 * (4 - last_bytes))));
7990       part_bytes_reg = tmp;
7991
7992       while (last_bytes)
7993         {
7994           mem = adjust_automodify_address (dstbase, QImode,
7995                                            plus_constant (dst, last_bytes - 1),
7996                                            dstoffset + last_bytes - 1);
7997           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7998
7999           if (--last_bytes)
8000             {
8001               tmp = gen_reg_rtx (SImode);
8002               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
8003               part_bytes_reg = tmp;
8004             }
8005         }
8006
8007     }
8008   else
8009     {
8010       if (last_bytes > 1)
8011         {
8012           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
8013           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
8014           last_bytes -= 2;
8015           if (last_bytes)
8016             {
8017               rtx tmp = gen_reg_rtx (SImode);
8018               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
8019               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
8020               part_bytes_reg = tmp;
8021               dstoffset += 2;
8022             }
8023         }
8024
8025       if (last_bytes)
8026         {
8027           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
8028           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
8029         }
8030     }
8031
8032   return 1;
8033 }
8034
8035 /* Select a dominance comparison mode if possible for a test of the general
8036    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
8037    COND_OR == DOM_CC_X_AND_Y => (X && Y)
8038    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
8039    COND_OR == DOM_CC_X_OR_Y => (X || Y)
8040    In all cases OP will be either EQ or NE, but we don't need to know which
8041    here.  If we are unable to support a dominance comparison we return
8042    CC mode.  This will then fail to match for the RTL expressions that
8043    generate this call.  */
8044 enum machine_mode
8045 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
8046 {
8047   enum rtx_code cond1, cond2;
8048   int swapped = 0;
8049
8050   /* Currently we will probably get the wrong result if the individual
8051      comparisons are not simple.  This also ensures that it is safe to
8052      reverse a comparison if necessary.  */
8053   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
8054        != CCmode)
8055       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
8056           != CCmode))
8057     return CCmode;
8058
8059   /* The if_then_else variant of this tests the second condition if the
8060      first passes, but is true if the first fails.  Reverse the first
8061      condition to get a true "inclusive-or" expression.  */
8062   if (cond_or == DOM_CC_NX_OR_Y)
8063     cond1 = reverse_condition (cond1);
8064
8065   /* If the comparisons are not equal, and one doesn't dominate the other,
8066      then we can't do this.  */
8067   if (cond1 != cond2
8068       && !comparison_dominates_p (cond1, cond2)
8069       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
8070     return CCmode;
8071
8072   if (swapped)
8073     {
8074       enum rtx_code temp = cond1;
8075       cond1 = cond2;
8076       cond2 = temp;
8077     }
8078
8079   switch (cond1)
8080     {
8081     case EQ:
8082       if (cond_or == DOM_CC_X_AND_Y)
8083         return CC_DEQmode;
8084
8085       switch (cond2)
8086         {
8087         case EQ: return CC_DEQmode;
8088         case LE: return CC_DLEmode;
8089         case LEU: return CC_DLEUmode;
8090         case GE: return CC_DGEmode;
8091         case GEU: return CC_DGEUmode;
8092         default: gcc_unreachable ();
8093         }
8094
8095     case LT:
8096       if (cond_or == DOM_CC_X_AND_Y)
8097         return CC_DLTmode;
8098
8099       switch (cond2)
8100         {
8101         case  LT:
8102             return CC_DLTmode;
8103         case LE:
8104           return CC_DLEmode;
8105         case NE:
8106           return CC_DNEmode;
8107         default:
8108           gcc_unreachable ();
8109         }
8110
8111     case GT:
8112       if (cond_or == DOM_CC_X_AND_Y)
8113         return CC_DGTmode;
8114
8115       switch (cond2)
8116         {
8117         case GT:
8118           return CC_DGTmode;
8119         case GE:
8120           return CC_DGEmode;
8121         case NE:
8122           return CC_DNEmode;
8123         default:
8124           gcc_unreachable ();
8125         }
8126
8127     case LTU:
8128       if (cond_or == DOM_CC_X_AND_Y)
8129         return CC_DLTUmode;
8130
8131       switch (cond2)
8132         {
8133         case LTU:
8134           return CC_DLTUmode;
8135         case LEU:
8136           return CC_DLEUmode;
8137         case NE:
8138           return CC_DNEmode;
8139         default:
8140           gcc_unreachable ();
8141         }
8142
8143     case GTU:
8144       if (cond_or == DOM_CC_X_AND_Y)
8145         return CC_DGTUmode;
8146
8147       switch (cond2)
8148         {
8149         case GTU:
8150           return CC_DGTUmode;
8151         case GEU:
8152           return CC_DGEUmode;
8153         case NE:
8154           return CC_DNEmode;
8155         default:
8156           gcc_unreachable ();
8157         }
8158
8159     /* The remaining cases only occur when both comparisons are the
8160        same.  */
8161     case NE:
8162       gcc_assert (cond1 == cond2);
8163       return CC_DNEmode;
8164
8165     case LE:
8166       gcc_assert (cond1 == cond2);
8167       return CC_DLEmode;
8168
8169     case GE:
8170       gcc_assert (cond1 == cond2);
8171       return CC_DGEmode;
8172
8173     case LEU:
8174       gcc_assert (cond1 == cond2);
8175       return CC_DLEUmode;
8176
8177     case GEU:
8178       gcc_assert (cond1 == cond2);
8179       return CC_DGEUmode;
8180
8181     default:
8182       gcc_unreachable ();
8183     }
8184 }
8185
8186 enum machine_mode
8187 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
8188 {
8189   /* All floating point compares return CCFP if it is an equality
8190      comparison, and CCFPE otherwise.  */
8191   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
8192     {
8193       switch (op)
8194         {
8195         case EQ:
8196         case NE:
8197         case UNORDERED:
8198         case ORDERED:
8199         case UNLT:
8200         case UNLE:
8201         case UNGT:
8202         case UNGE:
8203         case UNEQ:
8204         case LTGT:
8205           return CCFPmode;
8206
8207         case LT:
8208         case LE:
8209         case GT:
8210         case GE:
8211           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
8212             return CCFPmode;
8213           return CCFPEmode;
8214
8215         default:
8216           gcc_unreachable ();
8217         }
8218     }
8219
8220   /* A compare with a shifted operand.  Because of canonicalization, the
8221      comparison will have to be swapped when we emit the assembler.  */
8222   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
8223       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
8224           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
8225           || GET_CODE (x) == ROTATERT))
8226     return CC_SWPmode;
8227
8228   /* This operation is performed swapped, but since we only rely on the Z
8229      flag we don't need an additional mode.  */
8230   if (GET_MODE (y) == SImode && REG_P (y)
8231       && GET_CODE (x) == NEG
8232       && (op == EQ || op == NE))
8233     return CC_Zmode;
8234
8235   /* This is a special case that is used by combine to allow a
8236      comparison of a shifted byte load to be split into a zero-extend
8237      followed by a comparison of the shifted integer (only valid for
8238      equalities and unsigned inequalities).  */
8239   if (GET_MODE (x) == SImode
8240       && GET_CODE (x) == ASHIFT
8241       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
8242       && GET_CODE (XEXP (x, 0)) == SUBREG
8243       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
8244       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
8245       && (op == EQ || op == NE
8246           || op == GEU || op == GTU || op == LTU || op == LEU)
8247       && GET_CODE (y) == CONST_INT)
8248     return CC_Zmode;
8249
8250   /* A construct for a conditional compare, if the false arm contains
8251      0, then both conditions must be true, otherwise either condition
8252      must be true.  Not all conditions are possible, so CCmode is
8253      returned if it can't be done.  */
8254   if (GET_CODE (x) == IF_THEN_ELSE
8255       && (XEXP (x, 2) == const0_rtx
8256           || XEXP (x, 2) == const1_rtx)
8257       && COMPARISON_P (XEXP (x, 0))
8258       && COMPARISON_P (XEXP (x, 1)))
8259     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8260                                          INTVAL (XEXP (x, 2)));
8261
8262   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
8263   if (GET_CODE (x) == AND
8264       && COMPARISON_P (XEXP (x, 0))
8265       && COMPARISON_P (XEXP (x, 1)))
8266     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8267                                          DOM_CC_X_AND_Y);
8268
8269   if (GET_CODE (x) == IOR
8270       && COMPARISON_P (XEXP (x, 0))
8271       && COMPARISON_P (XEXP (x, 1)))
8272     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
8273                                          DOM_CC_X_OR_Y);
8274
8275   /* An operation (on Thumb) where we want to test for a single bit.
8276      This is done by shifting that bit up into the top bit of a
8277      scratch register; we can then branch on the sign bit.  */
8278   if (TARGET_THUMB1
8279       && GET_MODE (x) == SImode
8280       && (op == EQ || op == NE)
8281       && GET_CODE (x) == ZERO_EXTRACT
8282       && XEXP (x, 1) == const1_rtx)
8283     return CC_Nmode;
8284
8285   /* An operation that sets the condition codes as a side-effect, the
8286      V flag is not set correctly, so we can only use comparisons where
8287      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
8288      instead.)  */
8289   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
8290   if (GET_MODE (x) == SImode
8291       && y == const0_rtx
8292       && (op == EQ || op == NE || op == LT || op == GE)
8293       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
8294           || GET_CODE (x) == AND || GET_CODE (x) == IOR
8295           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
8296           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
8297           || GET_CODE (x) == LSHIFTRT
8298           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
8299           || GET_CODE (x) == ROTATERT
8300           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
8301     return CC_NOOVmode;
8302
8303   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
8304     return CC_Zmode;
8305
8306   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
8307       && GET_CODE (x) == PLUS
8308       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
8309     return CC_Cmode;
8310
8311   return CCmode;
8312 }
8313
8314 /* X and Y are two things to compare using CODE.  Emit the compare insn and
8315    return the rtx for register 0 in the proper mode.  FP means this is a
8316    floating point compare: I don't think that it is needed on the arm.  */
8317 rtx
8318 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
8319 {
8320   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
8321   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
8322
8323   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
8324
8325   return cc_reg;
8326 }
8327
8328 /* Generate a sequence of insns that will generate the correct return
8329    address mask depending on the physical architecture that the program
8330    is running on.  */
8331 rtx
8332 arm_gen_return_addr_mask (void)
8333 {
8334   rtx reg = gen_reg_rtx (Pmode);
8335
8336   emit_insn (gen_return_addr_mask (reg));
8337   return reg;
8338 }
8339
8340 void
8341 arm_reload_in_hi (rtx *operands)
8342 {
8343   rtx ref = operands[1];
8344   rtx base, scratch;
8345   HOST_WIDE_INT offset = 0;
8346
8347   if (GET_CODE (ref) == SUBREG)
8348     {
8349       offset = SUBREG_BYTE (ref);
8350       ref = SUBREG_REG (ref);
8351     }
8352
8353   if (GET_CODE (ref) == REG)
8354     {
8355       /* We have a pseudo which has been spilt onto the stack; there
8356          are two cases here: the first where there is a simple
8357          stack-slot replacement and a second where the stack-slot is
8358          out of range, or is used as a subreg.  */
8359       if (reg_equiv_mem[REGNO (ref)])
8360         {
8361           ref = reg_equiv_mem[REGNO (ref)];
8362           base = find_replacement (&XEXP (ref, 0));
8363         }
8364       else
8365         /* The slot is out of range, or was dressed up in a SUBREG.  */
8366         base = reg_equiv_address[REGNO (ref)];
8367     }
8368   else
8369     base = find_replacement (&XEXP (ref, 0));
8370
8371   /* Handle the case where the address is too complex to be offset by 1.  */
8372   if (GET_CODE (base) == MINUS
8373       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8374     {
8375       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8376
8377       emit_set_insn (base_plus, base);
8378       base = base_plus;
8379     }
8380   else if (GET_CODE (base) == PLUS)
8381     {
8382       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8383       HOST_WIDE_INT hi, lo;
8384
8385       offset += INTVAL (XEXP (base, 1));
8386       base = XEXP (base, 0);
8387
8388       /* Rework the address into a legal sequence of insns.  */
8389       /* Valid range for lo is -4095 -> 4095 */
8390       lo = (offset >= 0
8391             ? (offset & 0xfff)
8392             : -((-offset) & 0xfff));
8393
8394       /* Corner case, if lo is the max offset then we would be out of range
8395          once we have added the additional 1 below, so bump the msb into the
8396          pre-loading insn(s).  */
8397       if (lo == 4095)
8398         lo &= 0x7ff;
8399
8400       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8401              ^ (HOST_WIDE_INT) 0x80000000)
8402             - (HOST_WIDE_INT) 0x80000000);
8403
8404       gcc_assert (hi + lo == offset);
8405
8406       if (hi != 0)
8407         {
8408           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8409
8410           /* Get the base address; addsi3 knows how to handle constants
8411              that require more than one insn.  */
8412           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8413           base = base_plus;
8414           offset = lo;
8415         }
8416     }
8417
8418   /* Operands[2] may overlap operands[0] (though it won't overlap
8419      operands[1]), that's why we asked for a DImode reg -- so we can
8420      use the bit that does not overlap.  */
8421   if (REGNO (operands[2]) == REGNO (operands[0]))
8422     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8423   else
8424     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8425
8426   emit_insn (gen_zero_extendqisi2 (scratch,
8427                                    gen_rtx_MEM (QImode,
8428                                                 plus_constant (base,
8429                                                                offset))));
8430   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
8431                                    gen_rtx_MEM (QImode,
8432                                                 plus_constant (base,
8433                                                                offset + 1))));
8434   if (!BYTES_BIG_ENDIAN)
8435     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8436                    gen_rtx_IOR (SImode,
8437                                 gen_rtx_ASHIFT
8438                                 (SImode,
8439                                  gen_rtx_SUBREG (SImode, operands[0], 0),
8440                                  GEN_INT (8)),
8441                                 scratch));
8442   else
8443     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8444                    gen_rtx_IOR (SImode,
8445                                 gen_rtx_ASHIFT (SImode, scratch,
8446                                                 GEN_INT (8)),
8447                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
8448 }
8449
8450 /* Handle storing a half-word to memory during reload by synthesizing as two
8451    byte stores.  Take care not to clobber the input values until after we
8452    have moved them somewhere safe.  This code assumes that if the DImode
8453    scratch in operands[2] overlaps either the input value or output address
8454    in some way, then that value must die in this insn (we absolutely need
8455    two scratch registers for some corner cases).  */
8456 void
8457 arm_reload_out_hi (rtx *operands)
8458 {
8459   rtx ref = operands[0];
8460   rtx outval = operands[1];
8461   rtx base, scratch;
8462   HOST_WIDE_INT offset = 0;
8463
8464   if (GET_CODE (ref) == SUBREG)
8465     {
8466       offset = SUBREG_BYTE (ref);
8467       ref = SUBREG_REG (ref);
8468     }
8469
8470   if (GET_CODE (ref) == REG)
8471     {
8472       /* We have a pseudo which has been spilt onto the stack; there
8473          are two cases here: the first where there is a simple
8474          stack-slot replacement and a second where the stack-slot is
8475          out of range, or is used as a subreg.  */
8476       if (reg_equiv_mem[REGNO (ref)])
8477         {
8478           ref = reg_equiv_mem[REGNO (ref)];
8479           base = find_replacement (&XEXP (ref, 0));
8480         }
8481       else
8482         /* The slot is out of range, or was dressed up in a SUBREG.  */
8483         base = reg_equiv_address[REGNO (ref)];
8484     }
8485   else
8486     base = find_replacement (&XEXP (ref, 0));
8487
8488   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8489
8490   /* Handle the case where the address is too complex to be offset by 1.  */
8491   if (GET_CODE (base) == MINUS
8492       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8493     {
8494       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8495
8496       /* Be careful not to destroy OUTVAL.  */
8497       if (reg_overlap_mentioned_p (base_plus, outval))
8498         {
8499           /* Updating base_plus might destroy outval, see if we can
8500              swap the scratch and base_plus.  */
8501           if (!reg_overlap_mentioned_p (scratch, outval))
8502             {
8503               rtx tmp = scratch;
8504               scratch = base_plus;
8505               base_plus = tmp;
8506             }
8507           else
8508             {
8509               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8510
8511               /* Be conservative and copy OUTVAL into the scratch now,
8512                  this should only be necessary if outval is a subreg
8513                  of something larger than a word.  */
8514               /* XXX Might this clobber base?  I can't see how it can,
8515                  since scratch is known to overlap with OUTVAL, and
8516                  must be wider than a word.  */
8517               emit_insn (gen_movhi (scratch_hi, outval));
8518               outval = scratch_hi;
8519             }
8520         }
8521
8522       emit_set_insn (base_plus, base);
8523       base = base_plus;
8524     }
8525   else if (GET_CODE (base) == PLUS)
8526     {
8527       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8528       HOST_WIDE_INT hi, lo;
8529
8530       offset += INTVAL (XEXP (base, 1));
8531       base = XEXP (base, 0);
8532
8533       /* Rework the address into a legal sequence of insns.  */
8534       /* Valid range for lo is -4095 -> 4095 */
8535       lo = (offset >= 0
8536             ? (offset & 0xfff)
8537             : -((-offset) & 0xfff));
8538
8539       /* Corner case, if lo is the max offset then we would be out of range
8540          once we have added the additional 1 below, so bump the msb into the
8541          pre-loading insn(s).  */
8542       if (lo == 4095)
8543         lo &= 0x7ff;
8544
8545       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8546              ^ (HOST_WIDE_INT) 0x80000000)
8547             - (HOST_WIDE_INT) 0x80000000);
8548
8549       gcc_assert (hi + lo == offset);
8550
8551       if (hi != 0)
8552         {
8553           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8554
8555           /* Be careful not to destroy OUTVAL.  */
8556           if (reg_overlap_mentioned_p (base_plus, outval))
8557             {
8558               /* Updating base_plus might destroy outval, see if we
8559                  can swap the scratch and base_plus.  */
8560               if (!reg_overlap_mentioned_p (scratch, outval))
8561                 {
8562                   rtx tmp = scratch;
8563                   scratch = base_plus;
8564                   base_plus = tmp;
8565                 }
8566               else
8567                 {
8568                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8569
8570                   /* Be conservative and copy outval into scratch now,
8571                      this should only be necessary if outval is a
8572                      subreg of something larger than a word.  */
8573                   /* XXX Might this clobber base?  I can't see how it
8574                      can, since scratch is known to overlap with
8575                      outval.  */
8576                   emit_insn (gen_movhi (scratch_hi, outval));
8577                   outval = scratch_hi;
8578                 }
8579             }
8580
8581           /* Get the base address; addsi3 knows how to handle constants
8582              that require more than one insn.  */
8583           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8584           base = base_plus;
8585           offset = lo;
8586         }
8587     }
8588
8589   if (BYTES_BIG_ENDIAN)
8590     {
8591       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8592                                          plus_constant (base, offset + 1)),
8593                             gen_lowpart (QImode, outval)));
8594       emit_insn (gen_lshrsi3 (scratch,
8595                               gen_rtx_SUBREG (SImode, outval, 0),
8596                               GEN_INT (8)));
8597       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8598                             gen_lowpart (QImode, scratch)));
8599     }
8600   else
8601     {
8602       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8603                             gen_lowpart (QImode, outval)));
8604       emit_insn (gen_lshrsi3 (scratch,
8605                               gen_rtx_SUBREG (SImode, outval, 0),
8606                               GEN_INT (8)));
8607       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8608                                          plus_constant (base, offset + 1)),
8609                             gen_lowpart (QImode, scratch)));
8610     }
8611 }
8612
8613 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
8614    (padded to the size of a word) should be passed in a register.  */
8615
8616 static bool
8617 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
8618 {
8619   if (TARGET_AAPCS_BASED)
8620     return must_pass_in_stack_var_size (mode, type);
8621   else
8622     return must_pass_in_stack_var_size_or_pad (mode, type);
8623 }
8624
8625
8626 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
8627    Return true if an argument passed on the stack should be padded upwards,
8628    i.e. if the least-significant byte has useful data.
8629    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
8630    aggregate types are placed in the lowest memory address.  */
8631
8632 bool
8633 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
8634 {
8635   if (!TARGET_AAPCS_BASED)
8636     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
8637
8638   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
8639     return false;
8640
8641   return true;
8642 }
8643
8644
8645 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
8646    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
8647    byte of the register has useful data, and return the opposite if the
8648    most significant byte does.
8649    For AAPCS, small aggregates and small complex types are always padded
8650    upwards.  */
8651
8652 bool
8653 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
8654                     tree type, int first ATTRIBUTE_UNUSED)
8655 {
8656   if (TARGET_AAPCS_BASED
8657       && BYTES_BIG_ENDIAN
8658       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
8659       && int_size_in_bytes (type) <= 4)
8660     return true;
8661
8662   /* Otherwise, use default padding.  */
8663   return !BYTES_BIG_ENDIAN;
8664 }
8665
8666 \f
8667 /* Print a symbolic form of X to the debug file, F.  */
8668 static void
8669 arm_print_value (FILE *f, rtx x)
8670 {
8671   switch (GET_CODE (x))
8672     {
8673     case CONST_INT:
8674       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
8675       return;
8676
8677     case CONST_DOUBLE:
8678       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
8679       return;
8680
8681     case CONST_VECTOR:
8682       {
8683         int i;
8684
8685         fprintf (f, "<");
8686         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
8687           {
8688             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
8689             if (i < (CONST_VECTOR_NUNITS (x) - 1))
8690               fputc (',', f);
8691           }
8692         fprintf (f, ">");
8693       }
8694       return;
8695
8696     case CONST_STRING:
8697       fprintf (f, "\"%s\"", XSTR (x, 0));
8698       return;
8699
8700     case SYMBOL_REF:
8701       fprintf (f, "`%s'", XSTR (x, 0));
8702       return;
8703
8704     case LABEL_REF:
8705       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
8706       return;
8707
8708     case CONST:
8709       arm_print_value (f, XEXP (x, 0));
8710       return;
8711
8712     case PLUS:
8713       arm_print_value (f, XEXP (x, 0));
8714       fprintf (f, "+");
8715       arm_print_value (f, XEXP (x, 1));
8716       return;
8717
8718     case PC:
8719       fprintf (f, "pc");
8720       return;
8721
8722     default:
8723       fprintf (f, "????");
8724       return;
8725     }
8726 }
8727 \f
8728 /* Routines for manipulation of the constant pool.  */
8729
8730 /* Arm instructions cannot load a large constant directly into a
8731    register; they have to come from a pc relative load.  The constant
8732    must therefore be placed in the addressable range of the pc
8733    relative load.  Depending on the precise pc relative load
8734    instruction the range is somewhere between 256 bytes and 4k.  This
8735    means that we often have to dump a constant inside a function, and
8736    generate code to branch around it.
8737
8738    It is important to minimize this, since the branches will slow
8739    things down and make the code larger.
8740
8741    Normally we can hide the table after an existing unconditional
8742    branch so that there is no interruption of the flow, but in the
8743    worst case the code looks like this:
8744
8745         ldr     rn, L1
8746         ...
8747         b       L2
8748         align
8749         L1:     .long value
8750         L2:
8751         ...
8752
8753         ldr     rn, L3
8754         ...
8755         b       L4
8756         align
8757         L3:     .long value
8758         L4:
8759         ...
8760
8761    We fix this by performing a scan after scheduling, which notices
8762    which instructions need to have their operands fetched from the
8763    constant table and builds the table.
8764
8765    The algorithm starts by building a table of all the constants that
8766    need fixing up and all the natural barriers in the function (places
8767    where a constant table can be dropped without breaking the flow).
8768    For each fixup we note how far the pc-relative replacement will be
8769    able to reach and the offset of the instruction into the function.
8770
8771    Having built the table we then group the fixes together to form
8772    tables that are as large as possible (subject to addressing
8773    constraints) and emit each table of constants after the last
8774    barrier that is within range of all the instructions in the group.
8775    If a group does not contain a barrier, then we forcibly create one
8776    by inserting a jump instruction into the flow.  Once the table has
8777    been inserted, the insns are then modified to reference the
8778    relevant entry in the pool.
8779
8780    Possible enhancements to the algorithm (not implemented) are:
8781
8782    1) For some processors and object formats, there may be benefit in
8783    aligning the pools to the start of cache lines; this alignment
8784    would need to be taken into account when calculating addressability
8785    of a pool.  */
8786
8787 /* These typedefs are located at the start of this file, so that
8788    they can be used in the prototypes there.  This comment is to
8789    remind readers of that fact so that the following structures
8790    can be understood more easily.
8791
8792      typedef struct minipool_node    Mnode;
8793      typedef struct minipool_fixup   Mfix;  */
8794
8795 struct minipool_node
8796 {
8797   /* Doubly linked chain of entries.  */
8798   Mnode * next;
8799   Mnode * prev;
8800   /* The maximum offset into the code that this entry can be placed.  While
8801      pushing fixes for forward references, all entries are sorted in order
8802      of increasing max_address.  */
8803   HOST_WIDE_INT max_address;
8804   /* Similarly for an entry inserted for a backwards ref.  */
8805   HOST_WIDE_INT min_address;
8806   /* The number of fixes referencing this entry.  This can become zero
8807      if we "unpush" an entry.  In this case we ignore the entry when we
8808      come to emit the code.  */
8809   int refcount;
8810   /* The offset from the start of the minipool.  */
8811   HOST_WIDE_INT offset;
8812   /* The value in table.  */
8813   rtx value;
8814   /* The mode of value.  */
8815   enum machine_mode mode;
8816   /* The size of the value.  With iWMMXt enabled
8817      sizes > 4 also imply an alignment of 8-bytes.  */
8818   int fix_size;
8819 };
8820
8821 struct minipool_fixup
8822 {
8823   Mfix *            next;
8824   rtx               insn;
8825   HOST_WIDE_INT     address;
8826   rtx *             loc;
8827   enum machine_mode mode;
8828   int               fix_size;
8829   rtx               value;
8830   Mnode *           minipool;
8831   HOST_WIDE_INT     forwards;
8832   HOST_WIDE_INT     backwards;
8833 };
8834
8835 /* Fixes less than a word need padding out to a word boundary.  */
8836 #define MINIPOOL_FIX_SIZE(mode) \
8837   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
8838
8839 static Mnode *  minipool_vector_head;
8840 static Mnode *  minipool_vector_tail;
8841 static rtx      minipool_vector_label;
8842 static int      minipool_pad;
8843
8844 /* The linked list of all minipool fixes required for this function.  */
8845 Mfix *          minipool_fix_head;
8846 Mfix *          minipool_fix_tail;
8847 /* The fix entry for the current minipool, once it has been placed.  */
8848 Mfix *          minipool_barrier;
8849
8850 /* Determines if INSN is the start of a jump table.  Returns the end
8851    of the TABLE or NULL_RTX.  */
8852 static rtx
8853 is_jump_table (rtx insn)
8854 {
8855   rtx table;
8856
8857   if (GET_CODE (insn) == JUMP_INSN
8858       && JUMP_LABEL (insn) != NULL
8859       && ((table = next_real_insn (JUMP_LABEL (insn)))
8860           == next_real_insn (insn))
8861       && table != NULL
8862       && GET_CODE (table) == JUMP_INSN
8863       && (GET_CODE (PATTERN (table)) == ADDR_VEC
8864           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
8865     return table;
8866
8867   return NULL_RTX;
8868 }
8869
8870 #ifndef JUMP_TABLES_IN_TEXT_SECTION
8871 #define JUMP_TABLES_IN_TEXT_SECTION 0
8872 #endif
8873
8874 static HOST_WIDE_INT
8875 get_jump_table_size (rtx insn)
8876 {
8877   /* ADDR_VECs only take room if read-only data does into the text
8878      section.  */
8879   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
8880     {
8881       rtx body = PATTERN (insn);
8882       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
8883       HOST_WIDE_INT size;
8884       HOST_WIDE_INT modesize;
8885
8886       modesize = GET_MODE_SIZE (GET_MODE (body));
8887       size = modesize * XVECLEN (body, elt);
8888       switch (modesize)
8889         {
8890         case 1:
8891           /* Round up size  of TBB table to a halfword boundary.  */
8892           size = (size + 1) & ~(HOST_WIDE_INT)1;
8893           break;
8894         case 2:
8895           /* No padding necessary for TBH.  */
8896           break;
8897         case 4:
8898           /* Add two bytes for alignment on Thumb.  */
8899           if (TARGET_THUMB)
8900             size += 2;
8901           break;
8902         default:
8903           gcc_unreachable ();
8904         }
8905       return size;
8906     }
8907
8908   return 0;
8909 }
8910
8911 /* Move a minipool fix MP from its current location to before MAX_MP.
8912    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
8913    constraints may need updating.  */
8914 static Mnode *
8915 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
8916                                HOST_WIDE_INT max_address)
8917 {
8918   /* The code below assumes these are different.  */
8919   gcc_assert (mp != max_mp);
8920
8921   if (max_mp == NULL)
8922     {
8923       if (max_address < mp->max_address)
8924         mp->max_address = max_address;
8925     }
8926   else
8927     {
8928       if (max_address > max_mp->max_address - mp->fix_size)
8929         mp->max_address = max_mp->max_address - mp->fix_size;
8930       else
8931         mp->max_address = max_address;
8932
8933       /* Unlink MP from its current position.  Since max_mp is non-null,
8934        mp->prev must be non-null.  */
8935       mp->prev->next = mp->next;
8936       if (mp->next != NULL)
8937         mp->next->prev = mp->prev;
8938       else
8939         minipool_vector_tail = mp->prev;
8940
8941       /* Re-insert it before MAX_MP.  */
8942       mp->next = max_mp;
8943       mp->prev = max_mp->prev;
8944       max_mp->prev = mp;
8945
8946       if (mp->prev != NULL)
8947         mp->prev->next = mp;
8948       else
8949         minipool_vector_head = mp;
8950     }
8951
8952   /* Save the new entry.  */
8953   max_mp = mp;
8954
8955   /* Scan over the preceding entries and adjust their addresses as
8956      required.  */
8957   while (mp->prev != NULL
8958          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8959     {
8960       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8961       mp = mp->prev;
8962     }
8963
8964   return max_mp;
8965 }
8966
8967 /* Add a constant to the minipool for a forward reference.  Returns the
8968    node added or NULL if the constant will not fit in this pool.  */
8969 static Mnode *
8970 add_minipool_forward_ref (Mfix *fix)
8971 {
8972   /* If set, max_mp is the first pool_entry that has a lower
8973      constraint than the one we are trying to add.  */
8974   Mnode *       max_mp = NULL;
8975   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
8976   Mnode *       mp;
8977
8978   /* If the minipool starts before the end of FIX->INSN then this FIX
8979      can not be placed into the current pool.  Furthermore, adding the
8980      new constant pool entry may cause the pool to start FIX_SIZE bytes
8981      earlier.  */
8982   if (minipool_vector_head &&
8983       (fix->address + get_attr_length (fix->insn)
8984        >= minipool_vector_head->max_address - fix->fix_size))
8985     return NULL;
8986
8987   /* Scan the pool to see if a constant with the same value has
8988      already been added.  While we are doing this, also note the
8989      location where we must insert the constant if it doesn't already
8990      exist.  */
8991   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8992     {
8993       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8994           && fix->mode == mp->mode
8995           && (GET_CODE (fix->value) != CODE_LABEL
8996               || (CODE_LABEL_NUMBER (fix->value)
8997                   == CODE_LABEL_NUMBER (mp->value)))
8998           && rtx_equal_p (fix->value, mp->value))
8999         {
9000           /* More than one fix references this entry.  */
9001           mp->refcount++;
9002           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
9003         }
9004
9005       /* Note the insertion point if necessary.  */
9006       if (max_mp == NULL
9007           && mp->max_address > max_address)
9008         max_mp = mp;
9009
9010       /* If we are inserting an 8-bytes aligned quantity and
9011          we have not already found an insertion point, then
9012          make sure that all such 8-byte aligned quantities are
9013          placed at the start of the pool.  */
9014       if (ARM_DOUBLEWORD_ALIGN
9015           && max_mp == NULL
9016           && fix->fix_size >= 8
9017           && mp->fix_size < 8)
9018         {
9019           max_mp = mp;
9020           max_address = mp->max_address;
9021         }
9022     }
9023
9024   /* The value is not currently in the minipool, so we need to create
9025      a new entry for it.  If MAX_MP is NULL, the entry will be put on
9026      the end of the list since the placement is less constrained than
9027      any existing entry.  Otherwise, we insert the new fix before
9028      MAX_MP and, if necessary, adjust the constraints on the other
9029      entries.  */
9030   mp = XNEW (Mnode);
9031   mp->fix_size = fix->fix_size;
9032   mp->mode = fix->mode;
9033   mp->value = fix->value;
9034   mp->refcount = 1;
9035   /* Not yet required for a backwards ref.  */
9036   mp->min_address = -65536;
9037
9038   if (max_mp == NULL)
9039     {
9040       mp->max_address = max_address;
9041       mp->next = NULL;
9042       mp->prev = minipool_vector_tail;
9043
9044       if (mp->prev == NULL)
9045         {
9046           minipool_vector_head = mp;
9047           minipool_vector_label = gen_label_rtx ();
9048         }
9049       else
9050         mp->prev->next = mp;
9051
9052       minipool_vector_tail = mp;
9053     }
9054   else
9055     {
9056       if (max_address > max_mp->max_address - mp->fix_size)
9057         mp->max_address = max_mp->max_address - mp->fix_size;
9058       else
9059         mp->max_address = max_address;
9060
9061       mp->next = max_mp;
9062       mp->prev = max_mp->prev;
9063       max_mp->prev = mp;
9064       if (mp->prev != NULL)
9065         mp->prev->next = mp;
9066       else
9067         minipool_vector_head = mp;
9068     }
9069
9070   /* Save the new entry.  */
9071   max_mp = mp;
9072
9073   /* Scan over the preceding entries and adjust their addresses as
9074      required.  */
9075   while (mp->prev != NULL
9076          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
9077     {
9078       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
9079       mp = mp->prev;
9080     }
9081
9082   return max_mp;
9083 }
9084
9085 static Mnode *
9086 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
9087                                 HOST_WIDE_INT  min_address)
9088 {
9089   HOST_WIDE_INT offset;
9090
9091   /* The code below assumes these are different.  */
9092   gcc_assert (mp != min_mp);
9093
9094   if (min_mp == NULL)
9095     {
9096       if (min_address > mp->min_address)
9097         mp->min_address = min_address;
9098     }
9099   else
9100     {
9101       /* We will adjust this below if it is too loose.  */
9102       mp->min_address = min_address;
9103
9104       /* Unlink MP from its current position.  Since min_mp is non-null,
9105          mp->next must be non-null.  */
9106       mp->next->prev = mp->prev;
9107       if (mp->prev != NULL)
9108         mp->prev->next = mp->next;
9109       else
9110         minipool_vector_head = mp->next;
9111
9112       /* Reinsert it after MIN_MP.  */
9113       mp->prev = min_mp;
9114       mp->next = min_mp->next;
9115       min_mp->next = mp;
9116       if (mp->next != NULL)
9117         mp->next->prev = mp;
9118       else
9119         minipool_vector_tail = mp;
9120     }
9121
9122   min_mp = mp;
9123
9124   offset = 0;
9125   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9126     {
9127       mp->offset = offset;
9128       if (mp->refcount > 0)
9129         offset += mp->fix_size;
9130
9131       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
9132         mp->next->min_address = mp->min_address + mp->fix_size;
9133     }
9134
9135   return min_mp;
9136 }
9137
9138 /* Add a constant to the minipool for a backward reference.  Returns the
9139    node added or NULL if the constant will not fit in this pool.
9140
9141    Note that the code for insertion for a backwards reference can be
9142    somewhat confusing because the calculated offsets for each fix do
9143    not take into account the size of the pool (which is still under
9144    construction.  */
9145 static Mnode *
9146 add_minipool_backward_ref (Mfix *fix)
9147 {
9148   /* If set, min_mp is the last pool_entry that has a lower constraint
9149      than the one we are trying to add.  */
9150   Mnode *min_mp = NULL;
9151   /* This can be negative, since it is only a constraint.  */
9152   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
9153   Mnode *mp;
9154
9155   /* If we can't reach the current pool from this insn, or if we can't
9156      insert this entry at the end of the pool without pushing other
9157      fixes out of range, then we don't try.  This ensures that we
9158      can't fail later on.  */
9159   if (min_address >= minipool_barrier->address
9160       || (minipool_vector_tail->min_address + fix->fix_size
9161           >= minipool_barrier->address))
9162     return NULL;
9163
9164   /* Scan the pool to see if a constant with the same value has
9165      already been added.  While we are doing this, also note the
9166      location where we must insert the constant if it doesn't already
9167      exist.  */
9168   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
9169     {
9170       if (GET_CODE (fix->value) == GET_CODE (mp->value)
9171           && fix->mode == mp->mode
9172           && (GET_CODE (fix->value) != CODE_LABEL
9173               || (CODE_LABEL_NUMBER (fix->value)
9174                   == CODE_LABEL_NUMBER (mp->value)))
9175           && rtx_equal_p (fix->value, mp->value)
9176           /* Check that there is enough slack to move this entry to the
9177              end of the table (this is conservative).  */
9178           && (mp->max_address
9179               > (minipool_barrier->address
9180                  + minipool_vector_tail->offset
9181                  + minipool_vector_tail->fix_size)))
9182         {
9183           mp->refcount++;
9184           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
9185         }
9186
9187       if (min_mp != NULL)
9188         mp->min_address += fix->fix_size;
9189       else
9190         {
9191           /* Note the insertion point if necessary.  */
9192           if (mp->min_address < min_address)
9193             {
9194               /* For now, we do not allow the insertion of 8-byte alignment
9195                  requiring nodes anywhere but at the start of the pool.  */
9196               if (ARM_DOUBLEWORD_ALIGN
9197                   && fix->fix_size >= 8 && mp->fix_size < 8)
9198                 return NULL;
9199               else
9200                 min_mp = mp;
9201             }
9202           else if (mp->max_address
9203                    < minipool_barrier->address + mp->offset + fix->fix_size)
9204             {
9205               /* Inserting before this entry would push the fix beyond
9206                  its maximum address (which can happen if we have
9207                  re-located a forwards fix); force the new fix to come
9208                  after it.  */
9209               if (ARM_DOUBLEWORD_ALIGN
9210                   && fix->fix_size >= 8 && mp->fix_size < 8)
9211                 return NULL;
9212               else
9213                 {
9214                   min_mp = mp;
9215                   min_address = mp->min_address + fix->fix_size;
9216                 }
9217             }
9218           /* Do not insert a non-8-byte aligned quantity before 8-byte
9219              aligned quantities.  */
9220           else if (ARM_DOUBLEWORD_ALIGN
9221                    && fix->fix_size < 8
9222                    && mp->fix_size >= 8)
9223             {
9224               min_mp = mp;
9225               min_address = mp->min_address + fix->fix_size;
9226             }
9227         }
9228     }
9229
9230   /* We need to create a new entry.  */
9231   mp = XNEW (Mnode);
9232   mp->fix_size = fix->fix_size;
9233   mp->mode = fix->mode;
9234   mp->value = fix->value;
9235   mp->refcount = 1;
9236   mp->max_address = minipool_barrier->address + 65536;
9237
9238   mp->min_address = min_address;
9239
9240   if (min_mp == NULL)
9241     {
9242       mp->prev = NULL;
9243       mp->next = minipool_vector_head;
9244
9245       if (mp->next == NULL)
9246         {
9247           minipool_vector_tail = mp;
9248           minipool_vector_label = gen_label_rtx ();
9249         }
9250       else
9251         mp->next->prev = mp;
9252
9253       minipool_vector_head = mp;
9254     }
9255   else
9256     {
9257       mp->next = min_mp->next;
9258       mp->prev = min_mp;
9259       min_mp->next = mp;
9260
9261       if (mp->next != NULL)
9262         mp->next->prev = mp;
9263       else
9264         minipool_vector_tail = mp;
9265     }
9266
9267   /* Save the new entry.  */
9268   min_mp = mp;
9269
9270   if (mp->prev)
9271     mp = mp->prev;
9272   else
9273     mp->offset = 0;
9274
9275   /* Scan over the following entries and adjust their offsets.  */
9276   while (mp->next != NULL)
9277     {
9278       if (mp->next->min_address < mp->min_address + mp->fix_size)
9279         mp->next->min_address = mp->min_address + mp->fix_size;
9280
9281       if (mp->refcount)
9282         mp->next->offset = mp->offset + mp->fix_size;
9283       else
9284         mp->next->offset = mp->offset;
9285
9286       mp = mp->next;
9287     }
9288
9289   return min_mp;
9290 }
9291
9292 static void
9293 assign_minipool_offsets (Mfix *barrier)
9294 {
9295   HOST_WIDE_INT offset = 0;
9296   Mnode *mp;
9297
9298   minipool_barrier = barrier;
9299
9300   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9301     {
9302       mp->offset = offset;
9303
9304       if (mp->refcount > 0)
9305         offset += mp->fix_size;
9306     }
9307 }
9308
9309 /* Output the literal table */
9310 static void
9311 dump_minipool (rtx scan)
9312 {
9313   Mnode * mp;
9314   Mnode * nmp;
9315   int align64 = 0;
9316
9317   if (ARM_DOUBLEWORD_ALIGN)
9318     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
9319       if (mp->refcount > 0 && mp->fix_size >= 8)
9320         {
9321           align64 = 1;
9322           break;
9323         }
9324
9325   if (dump_file)
9326     fprintf (dump_file,
9327              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
9328              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
9329
9330   scan = emit_label_after (gen_label_rtx (), scan);
9331   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
9332   scan = emit_label_after (minipool_vector_label, scan);
9333
9334   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
9335     {
9336       if (mp->refcount > 0)
9337         {
9338           if (dump_file)
9339             {
9340               fprintf (dump_file,
9341                        ";;  Offset %u, min %ld, max %ld ",
9342                        (unsigned) mp->offset, (unsigned long) mp->min_address,
9343                        (unsigned long) mp->max_address);
9344               arm_print_value (dump_file, mp->value);
9345               fputc ('\n', dump_file);
9346             }
9347
9348           switch (mp->fix_size)
9349             {
9350 #ifdef HAVE_consttable_1
9351             case 1:
9352               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
9353               break;
9354
9355 #endif
9356 #ifdef HAVE_consttable_2
9357             case 2:
9358               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
9359               break;
9360
9361 #endif
9362 #ifdef HAVE_consttable_4
9363             case 4:
9364               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
9365               break;
9366
9367 #endif
9368 #ifdef HAVE_consttable_8
9369             case 8:
9370               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
9371               break;
9372
9373 #endif
9374 #ifdef HAVE_consttable_16
9375             case 16:
9376               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
9377               break;
9378
9379 #endif
9380             default:
9381               gcc_unreachable ();
9382             }
9383         }
9384
9385       nmp = mp->next;
9386       free (mp);
9387     }
9388
9389   minipool_vector_head = minipool_vector_tail = NULL;
9390   scan = emit_insn_after (gen_consttable_end (), scan);
9391   scan = emit_barrier_after (scan);
9392 }
9393
9394 /* Return the cost of forcibly inserting a barrier after INSN.  */
9395 static int
9396 arm_barrier_cost (rtx insn)
9397 {
9398   /* Basing the location of the pool on the loop depth is preferable,
9399      but at the moment, the basic block information seems to be
9400      corrupt by this stage of the compilation.  */
9401   int base_cost = 50;
9402   rtx next = next_nonnote_insn (insn);
9403
9404   if (next != NULL && GET_CODE (next) == CODE_LABEL)
9405     base_cost -= 20;
9406
9407   switch (GET_CODE (insn))
9408     {
9409     case CODE_LABEL:
9410       /* It will always be better to place the table before the label, rather
9411          than after it.  */
9412       return 50;
9413
9414     case INSN:
9415     case CALL_INSN:
9416       return base_cost;
9417
9418     case JUMP_INSN:
9419       return base_cost - 10;
9420
9421     default:
9422       return base_cost + 10;
9423     }
9424 }
9425
9426 /* Find the best place in the insn stream in the range
9427    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
9428    Create the barrier by inserting a jump and add a new fix entry for
9429    it.  */
9430 static Mfix *
9431 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
9432 {
9433   HOST_WIDE_INT count = 0;
9434   rtx barrier;
9435   rtx from = fix->insn;
9436   /* The instruction after which we will insert the jump.  */
9437   rtx selected = NULL;
9438   int selected_cost;
9439   /* The address at which the jump instruction will be placed.  */
9440   HOST_WIDE_INT selected_address;
9441   Mfix * new_fix;
9442   HOST_WIDE_INT max_count = max_address - fix->address;
9443   rtx label = gen_label_rtx ();
9444
9445   selected_cost = arm_barrier_cost (from);
9446   selected_address = fix->address;
9447
9448   while (from && count < max_count)
9449     {
9450       rtx tmp;
9451       int new_cost;
9452
9453       /* This code shouldn't have been called if there was a natural barrier
9454          within range.  */
9455       gcc_assert (GET_CODE (from) != BARRIER);
9456
9457       /* Count the length of this insn.  */
9458       count += get_attr_length (from);
9459
9460       /* If there is a jump table, add its length.  */
9461       tmp = is_jump_table (from);
9462       if (tmp != NULL)
9463         {
9464           count += get_jump_table_size (tmp);
9465
9466           /* Jump tables aren't in a basic block, so base the cost on
9467              the dispatch insn.  If we select this location, we will
9468              still put the pool after the table.  */
9469           new_cost = arm_barrier_cost (from);
9470
9471           if (count < max_count 
9472               && (!selected || new_cost <= selected_cost))
9473             {
9474               selected = tmp;
9475               selected_cost = new_cost;
9476               selected_address = fix->address + count;
9477             }
9478
9479           /* Continue after the dispatch table.  */
9480           from = NEXT_INSN (tmp);
9481           continue;
9482         }
9483
9484       new_cost = arm_barrier_cost (from);
9485
9486       if (count < max_count
9487           && (!selected || new_cost <= selected_cost))
9488         {
9489           selected = from;
9490           selected_cost = new_cost;
9491           selected_address = fix->address + count;
9492         }
9493
9494       from = NEXT_INSN (from);
9495     }
9496
9497   /* Make sure that we found a place to insert the jump.  */
9498   gcc_assert (selected);
9499
9500   /* Create a new JUMP_INSN that branches around a barrier.  */
9501   from = emit_jump_insn_after (gen_jump (label), selected);
9502   JUMP_LABEL (from) = label;
9503   barrier = emit_barrier_after (from);
9504   emit_label_after (label, barrier);
9505
9506   /* Create a minipool barrier entry for the new barrier.  */
9507   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
9508   new_fix->insn = barrier;
9509   new_fix->address = selected_address;
9510   new_fix->next = fix->next;
9511   fix->next = new_fix;
9512
9513   return new_fix;
9514 }
9515
9516 /* Record that there is a natural barrier in the insn stream at
9517    ADDRESS.  */
9518 static void
9519 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
9520 {
9521   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9522
9523   fix->insn = insn;
9524   fix->address = address;
9525
9526   fix->next = NULL;
9527   if (minipool_fix_head != NULL)
9528     minipool_fix_tail->next = fix;
9529   else
9530     minipool_fix_head = fix;
9531
9532   minipool_fix_tail = fix;
9533 }
9534
9535 /* Record INSN, which will need fixing up to load a value from the
9536    minipool.  ADDRESS is the offset of the insn since the start of the
9537    function; LOC is a pointer to the part of the insn which requires
9538    fixing; VALUE is the constant that must be loaded, which is of type
9539    MODE.  */
9540 static void
9541 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
9542                    enum machine_mode mode, rtx value)
9543 {
9544   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9545
9546   fix->insn = insn;
9547   fix->address = address;
9548   fix->loc = loc;
9549   fix->mode = mode;
9550   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
9551   fix->value = value;
9552   fix->forwards = get_attr_pool_range (insn);
9553   fix->backwards = get_attr_neg_pool_range (insn);
9554   fix->minipool = NULL;
9555
9556   /* If an insn doesn't have a range defined for it, then it isn't
9557      expecting to be reworked by this code.  Better to stop now than
9558      to generate duff assembly code.  */
9559   gcc_assert (fix->forwards || fix->backwards);
9560
9561   /* If an entry requires 8-byte alignment then assume all constant pools
9562      require 4 bytes of padding.  Trying to do this later on a per-pool
9563      basis is awkward because existing pool entries have to be modified.  */
9564   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
9565     minipool_pad = 4;
9566
9567   if (dump_file)
9568     {
9569       fprintf (dump_file,
9570                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
9571                GET_MODE_NAME (mode),
9572                INSN_UID (insn), (unsigned long) address,
9573                -1 * (long)fix->backwards, (long)fix->forwards);
9574       arm_print_value (dump_file, fix->value);
9575       fprintf (dump_file, "\n");
9576     }
9577
9578   /* Add it to the chain of fixes.  */
9579   fix->next = NULL;
9580
9581   if (minipool_fix_head != NULL)
9582     minipool_fix_tail->next = fix;
9583   else
9584     minipool_fix_head = fix;
9585
9586   minipool_fix_tail = fix;
9587 }
9588
9589 /* Return the cost of synthesizing a 64-bit constant VAL inline.
9590    Returns the number of insns needed, or 99 if we don't know how to
9591    do it.  */
9592 int
9593 arm_const_double_inline_cost (rtx val)
9594 {
9595   rtx lowpart, highpart;
9596   enum machine_mode mode;
9597
9598   mode = GET_MODE (val);
9599
9600   if (mode == VOIDmode)
9601     mode = DImode;
9602
9603   gcc_assert (GET_MODE_SIZE (mode) == 8);
9604
9605   lowpart = gen_lowpart (SImode, val);
9606   highpart = gen_highpart_mode (SImode, mode, val);
9607
9608   gcc_assert (GET_CODE (lowpart) == CONST_INT);
9609   gcc_assert (GET_CODE (highpart) == CONST_INT);
9610
9611   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
9612                             NULL_RTX, NULL_RTX, 0, 0)
9613           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
9614                               NULL_RTX, NULL_RTX, 0, 0));
9615 }
9616
9617 /* Return true if it is worthwhile to split a 64-bit constant into two
9618    32-bit operations.  This is the case if optimizing for size, or
9619    if we have load delay slots, or if one 32-bit part can be done with
9620    a single data operation.  */
9621 bool
9622 arm_const_double_by_parts (rtx val)
9623 {
9624   enum machine_mode mode = GET_MODE (val);
9625   rtx part;
9626
9627   if (optimize_size || arm_ld_sched)
9628     return true;
9629
9630   if (mode == VOIDmode)
9631     mode = DImode;
9632
9633   part = gen_highpart_mode (SImode, mode, val);
9634
9635   gcc_assert (GET_CODE (part) == CONST_INT);
9636
9637   if (const_ok_for_arm (INTVAL (part))
9638       || const_ok_for_arm (~INTVAL (part)))
9639     return true;
9640
9641   part = gen_lowpart (SImode, val);
9642
9643   gcc_assert (GET_CODE (part) == CONST_INT);
9644
9645   if (const_ok_for_arm (INTVAL (part))
9646       || const_ok_for_arm (~INTVAL (part)))
9647     return true;
9648
9649   return false;
9650 }
9651
9652 /* Scan INSN and note any of its operands that need fixing.
9653    If DO_PUSHES is false we do not actually push any of the fixups
9654    needed.  The function returns TRUE if any fixups were needed/pushed.
9655    This is used by arm_memory_load_p() which needs to know about loads
9656    of constants that will be converted into minipool loads.  */
9657 static bool
9658 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
9659 {
9660   bool result = false;
9661   int opno;
9662
9663   extract_insn (insn);
9664
9665   if (!constrain_operands (1))
9666     fatal_insn_not_found (insn);
9667
9668   if (recog_data.n_alternatives == 0)
9669     return false;
9670
9671   /* Fill in recog_op_alt with information about the constraints of
9672      this insn.  */
9673   preprocess_constraints ();
9674
9675   for (opno = 0; opno < recog_data.n_operands; opno++)
9676     {
9677       /* Things we need to fix can only occur in inputs.  */
9678       if (recog_data.operand_type[opno] != OP_IN)
9679         continue;
9680
9681       /* If this alternative is a memory reference, then any mention
9682          of constants in this alternative is really to fool reload
9683          into allowing us to accept one there.  We need to fix them up
9684          now so that we output the right code.  */
9685       if (recog_op_alt[opno][which_alternative].memory_ok)
9686         {
9687           rtx op = recog_data.operand[opno];
9688
9689           if (CONSTANT_P (op))
9690             {
9691               if (do_pushes)
9692                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
9693                                    recog_data.operand_mode[opno], op);
9694               result = true;
9695             }
9696           else if (GET_CODE (op) == MEM
9697                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
9698                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
9699             {
9700               if (do_pushes)
9701                 {
9702                   rtx cop = avoid_constant_pool_reference (op);
9703
9704                   /* Casting the address of something to a mode narrower
9705                      than a word can cause avoid_constant_pool_reference()
9706                      to return the pool reference itself.  That's no good to
9707                      us here.  Lets just hope that we can use the
9708                      constant pool value directly.  */
9709                   if (op == cop)
9710                     cop = get_pool_constant (XEXP (op, 0));
9711
9712                   push_minipool_fix (insn, address,
9713                                      recog_data.operand_loc[opno],
9714                                      recog_data.operand_mode[opno], cop);
9715                 }
9716
9717               result = true;
9718             }
9719         }
9720     }
9721
9722   return result;
9723 }
9724
9725 /* Gcc puts the pool in the wrong place for ARM, since we can only
9726    load addresses a limited distance around the pc.  We do some
9727    special munging to move the constant pool values to the correct
9728    point in the code.  */
9729 static void
9730 arm_reorg (void)
9731 {
9732   rtx insn;
9733   HOST_WIDE_INT address = 0;
9734   Mfix * fix;
9735
9736   minipool_fix_head = minipool_fix_tail = NULL;
9737
9738   /* The first insn must always be a note, or the code below won't
9739      scan it properly.  */
9740   insn = get_insns ();
9741   gcc_assert (GET_CODE (insn) == NOTE);
9742   minipool_pad = 0;
9743
9744   /* Scan all the insns and record the operands that will need fixing.  */
9745   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
9746     {
9747       if (TARGET_CIRRUS_FIX_INVALID_INSNS
9748           && (arm_cirrus_insn_p (insn)
9749               || GET_CODE (insn) == JUMP_INSN
9750               || arm_memory_load_p (insn)))
9751         cirrus_reorg (insn);
9752
9753       if (GET_CODE (insn) == BARRIER)
9754         push_minipool_barrier (insn, address);
9755       else if (INSN_P (insn))
9756         {
9757           rtx table;
9758
9759           note_invalid_constants (insn, address, true);
9760           address += get_attr_length (insn);
9761
9762           /* If the insn is a vector jump, add the size of the table
9763              and skip the table.  */
9764           if ((table = is_jump_table (insn)) != NULL)
9765             {
9766               address += get_jump_table_size (table);
9767               insn = table;
9768             }
9769         }
9770     }
9771
9772   fix = minipool_fix_head;
9773
9774   /* Now scan the fixups and perform the required changes.  */
9775   while (fix)
9776     {
9777       Mfix * ftmp;
9778       Mfix * fdel;
9779       Mfix *  last_added_fix;
9780       Mfix * last_barrier = NULL;
9781       Mfix * this_fix;
9782
9783       /* Skip any further barriers before the next fix.  */
9784       while (fix && GET_CODE (fix->insn) == BARRIER)
9785         fix = fix->next;
9786
9787       /* No more fixes.  */
9788       if (fix == NULL)
9789         break;
9790
9791       last_added_fix = NULL;
9792
9793       for (ftmp = fix; ftmp; ftmp = ftmp->next)
9794         {
9795           if (GET_CODE (ftmp->insn) == BARRIER)
9796             {
9797               if (ftmp->address >= minipool_vector_head->max_address)
9798                 break;
9799
9800               last_barrier = ftmp;
9801             }
9802           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
9803             break;
9804
9805           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
9806         }
9807
9808       /* If we found a barrier, drop back to that; any fixes that we
9809          could have reached but come after the barrier will now go in
9810          the next mini-pool.  */
9811       if (last_barrier != NULL)
9812         {
9813           /* Reduce the refcount for those fixes that won't go into this
9814              pool after all.  */
9815           for (fdel = last_barrier->next;
9816                fdel && fdel != ftmp;
9817                fdel = fdel->next)
9818             {
9819               fdel->minipool->refcount--;
9820               fdel->minipool = NULL;
9821             }
9822
9823           ftmp = last_barrier;
9824         }
9825       else
9826         {
9827           /* ftmp is first fix that we can't fit into this pool and
9828              there no natural barriers that we could use.  Insert a
9829              new barrier in the code somewhere between the previous
9830              fix and this one, and arrange to jump around it.  */
9831           HOST_WIDE_INT max_address;
9832
9833           /* The last item on the list of fixes must be a barrier, so
9834              we can never run off the end of the list of fixes without
9835              last_barrier being set.  */
9836           gcc_assert (ftmp);
9837
9838           max_address = minipool_vector_head->max_address;
9839           /* Check that there isn't another fix that is in range that
9840              we couldn't fit into this pool because the pool was
9841              already too large: we need to put the pool before such an
9842              instruction.  The pool itself may come just after the
9843              fix because create_fix_barrier also allows space for a
9844              jump instruction.  */
9845           if (ftmp->address < max_address)
9846             max_address = ftmp->address + 1;
9847
9848           last_barrier = create_fix_barrier (last_added_fix, max_address);
9849         }
9850
9851       assign_minipool_offsets (last_barrier);
9852
9853       while (ftmp)
9854         {
9855           if (GET_CODE (ftmp->insn) != BARRIER
9856               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
9857                   == NULL))
9858             break;
9859
9860           ftmp = ftmp->next;
9861         }
9862
9863       /* Scan over the fixes we have identified for this pool, fixing them
9864          up and adding the constants to the pool itself.  */
9865       for (this_fix = fix; this_fix && ftmp != this_fix;
9866            this_fix = this_fix->next)
9867         if (GET_CODE (this_fix->insn) != BARRIER)
9868           {
9869             rtx addr
9870               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
9871                                                   minipool_vector_label),
9872                                this_fix->minipool->offset);
9873             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
9874           }
9875
9876       dump_minipool (last_barrier->insn);
9877       fix = ftmp;
9878     }
9879
9880   /* From now on we must synthesize any constants that we can't handle
9881      directly.  This can happen if the RTL gets split during final
9882      instruction generation.  */
9883   after_arm_reorg = 1;
9884
9885   /* Free the minipool memory.  */
9886   obstack_free (&minipool_obstack, minipool_startobj);
9887 }
9888 \f
9889 /* Routines to output assembly language.  */
9890
9891 /* If the rtx is the correct value then return the string of the number.
9892    In this way we can ensure that valid double constants are generated even
9893    when cross compiling.  */
9894 const char *
9895 fp_immediate_constant (rtx x)
9896 {
9897   REAL_VALUE_TYPE r;
9898   int i;
9899
9900   if (!fp_consts_inited)
9901     init_fp_table ();
9902
9903   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9904   for (i = 0; i < 8; i++)
9905     if (REAL_VALUES_EQUAL (r, values_fp[i]))
9906       return strings_fp[i];
9907
9908   gcc_unreachable ();
9909 }
9910
9911 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
9912 static const char *
9913 fp_const_from_val (REAL_VALUE_TYPE *r)
9914 {
9915   int i;
9916
9917   if (!fp_consts_inited)
9918     init_fp_table ();
9919
9920   for (i = 0; i < 8; i++)
9921     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
9922       return strings_fp[i];
9923
9924   gcc_unreachable ();
9925 }
9926
9927 /* Output the operands of a LDM/STM instruction to STREAM.
9928    MASK is the ARM register set mask of which only bits 0-15 are important.
9929    REG is the base register, either the frame pointer or the stack pointer,
9930    INSTR is the possibly suffixed load or store instruction.
9931    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
9932
9933 static void
9934 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
9935                  unsigned long mask, int rfe)
9936 {
9937   unsigned i;
9938   bool not_first = FALSE;
9939
9940   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
9941   fputc ('\t', stream);
9942   asm_fprintf (stream, instr, reg);
9943   fputc ('{', stream);
9944
9945   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9946     if (mask & (1 << i))
9947       {
9948         if (not_first)
9949           fprintf (stream, ", ");
9950
9951         asm_fprintf (stream, "%r", i);
9952         not_first = TRUE;
9953       }
9954
9955   if (rfe)
9956     fprintf (stream, "}^\n");
9957   else
9958     fprintf (stream, "}\n");
9959 }
9960
9961
9962 /* Output a FLDMD instruction to STREAM.
9963    BASE if the register containing the address.
9964    REG and COUNT specify the register range.
9965    Extra registers may be added to avoid hardware bugs.
9966
9967    We output FLDMD even for ARMv5 VFP implementations.  Although
9968    FLDMD is technically not supported until ARMv6, it is believed
9969    that all VFP implementations support its use in this context.  */
9970
9971 static void
9972 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
9973 {
9974   int i;
9975
9976   /* Workaround ARM10 VFPr1 bug.  */
9977   if (count == 2 && !arm_arch6)
9978     {
9979       if (reg == 15)
9980         reg--;
9981       count++;
9982     }
9983
9984   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
9985      load into multiple parts if we have to handle more than 16 registers.  */
9986   if (count > 16)
9987     {
9988       vfp_output_fldmd (stream, base, reg, 16);
9989       vfp_output_fldmd (stream, base, reg + 16, count - 16);
9990       return;
9991     }
9992
9993   fputc ('\t', stream);
9994   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9995
9996   for (i = reg; i < reg + count; i++)
9997     {
9998       if (i > reg)
9999         fputs (", ", stream);
10000       asm_fprintf (stream, "d%d", i);
10001     }
10002   fputs ("}\n", stream);
10003
10004 }
10005
10006
10007 /* Output the assembly for a store multiple.  */
10008
10009 const char *
10010 vfp_output_fstmd (rtx * operands)
10011 {
10012   char pattern[100];
10013   int p;
10014   int base;
10015   int i;
10016
10017   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
10018   p = strlen (pattern);
10019
10020   gcc_assert (GET_CODE (operands[1]) == REG);
10021
10022   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
10023   for (i = 1; i < XVECLEN (operands[2], 0); i++)
10024     {
10025       p += sprintf (&pattern[p], ", d%d", base + i);
10026     }
10027   strcpy (&pattern[p], "}");
10028
10029   output_asm_insn (pattern, operands);
10030   return "";
10031 }
10032
10033
10034 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
10035    number of bytes pushed.  */
10036
10037 static int
10038 vfp_emit_fstmd (int base_reg, int count)
10039 {
10040   rtx par;
10041   rtx dwarf;
10042   rtx tmp, reg;
10043   int i;
10044
10045   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
10046      register pairs are stored by a store multiple insn.  We avoid this
10047      by pushing an extra pair.  */
10048   if (count == 2 && !arm_arch6)
10049     {
10050       if (base_reg == LAST_VFP_REGNUM - 3)
10051         base_reg -= 2;
10052       count++;
10053     }
10054
10055   /* FSTMD may not store more than 16 doubleword registers at once.  Split
10056      larger stores into multiple parts (up to a maximum of two, in
10057      practice).  */
10058   if (count > 16)
10059     {
10060       int saved;
10061       /* NOTE: base_reg is an internal register number, so each D register
10062          counts as 2.  */
10063       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
10064       saved += vfp_emit_fstmd (base_reg, 16);
10065       return saved;
10066     }
10067
10068   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10069   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10070
10071   reg = gen_rtx_REG (DFmode, base_reg);
10072   base_reg += 2;
10073
10074   XVECEXP (par, 0, 0)
10075     = gen_rtx_SET (VOIDmode,
10076                    gen_frame_mem (BLKmode,
10077                                   gen_rtx_PRE_DEC (BLKmode,
10078                                                    stack_pointer_rtx)),
10079                    gen_rtx_UNSPEC (BLKmode,
10080                                    gen_rtvec (1, reg),
10081                                    UNSPEC_PUSH_MULT));
10082
10083   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10084                      plus_constant (stack_pointer_rtx, -(count * 8)));
10085   RTX_FRAME_RELATED_P (tmp) = 1;
10086   XVECEXP (dwarf, 0, 0) = tmp;
10087
10088   tmp = gen_rtx_SET (VOIDmode,
10089                      gen_frame_mem (DFmode, stack_pointer_rtx),
10090                      reg);
10091   RTX_FRAME_RELATED_P (tmp) = 1;
10092   XVECEXP (dwarf, 0, 1) = tmp;
10093
10094   for (i = 1; i < count; i++)
10095     {
10096       reg = gen_rtx_REG (DFmode, base_reg);
10097       base_reg += 2;
10098       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10099
10100       tmp = gen_rtx_SET (VOIDmode,
10101                          gen_frame_mem (DFmode,
10102                                         plus_constant (stack_pointer_rtx,
10103                                                        i * 8)),
10104                          reg);
10105       RTX_FRAME_RELATED_P (tmp) = 1;
10106       XVECEXP (dwarf, 0, i + 1) = tmp;
10107     }
10108
10109   par = emit_insn (par);
10110   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10111                                        REG_NOTES (par));
10112   RTX_FRAME_RELATED_P (par) = 1;
10113
10114   return count * 8;
10115 }
10116
10117 /* Emit a call instruction with pattern PAT.  ADDR is the address of
10118    the call target.  */
10119
10120 void
10121 arm_emit_call_insn (rtx pat, rtx addr)
10122 {
10123   rtx insn;
10124
10125   insn = emit_call_insn (pat);
10126
10127   /* The PIC register is live on entry to VxWorks PIC PLT entries.
10128      If the call might use such an entry, add a use of the PIC register
10129      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
10130   if (TARGET_VXWORKS_RTP
10131       && flag_pic
10132       && GET_CODE (addr) == SYMBOL_REF
10133       && (SYMBOL_REF_DECL (addr)
10134           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
10135           : !SYMBOL_REF_LOCAL_P (addr)))
10136     {
10137       require_pic_register ();
10138       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
10139     }
10140 }
10141
10142 /* Output a 'call' insn.  */
10143 const char *
10144 output_call (rtx *operands)
10145 {
10146   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
10147
10148   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
10149   if (REGNO (operands[0]) == LR_REGNUM)
10150     {
10151       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
10152       output_asm_insn ("mov%?\t%0, %|lr", operands);
10153     }
10154
10155   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10156
10157   if (TARGET_INTERWORK || arm_arch4t)
10158     output_asm_insn ("bx%?\t%0", operands);
10159   else
10160     output_asm_insn ("mov%?\t%|pc, %0", operands);
10161
10162   return "";
10163 }
10164
10165 /* Output a 'call' insn that is a reference in memory.  */
10166 const char *
10167 output_call_mem (rtx *operands)
10168 {
10169   if (TARGET_INTERWORK && !arm_arch5)
10170     {
10171       output_asm_insn ("ldr%?\t%|ip, %0", operands);
10172       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10173       output_asm_insn ("bx%?\t%|ip", operands);
10174     }
10175   else if (regno_use_in (LR_REGNUM, operands[0]))
10176     {
10177       /* LR is used in the memory address.  We load the address in the
10178          first instruction.  It's safe to use IP as the target of the
10179          load since the call will kill it anyway.  */
10180       output_asm_insn ("ldr%?\t%|ip, %0", operands);
10181       if (arm_arch5)
10182         output_asm_insn ("blx%?\t%|ip", operands);
10183       else
10184         {
10185           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10186           if (arm_arch4t)
10187             output_asm_insn ("bx%?\t%|ip", operands);
10188           else
10189             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
10190         }
10191     }
10192   else
10193     {
10194       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
10195       output_asm_insn ("ldr%?\t%|pc, %0", operands);
10196     }
10197
10198   return "";
10199 }
10200
10201
10202 /* Output a move from arm registers to an fpa registers.
10203    OPERANDS[0] is an fpa register.
10204    OPERANDS[1] is the first registers of an arm register pair.  */
10205 const char *
10206 output_mov_long_double_fpa_from_arm (rtx *operands)
10207 {
10208   int arm_reg0 = REGNO (operands[1]);
10209   rtx ops[3];
10210
10211   gcc_assert (arm_reg0 != IP_REGNUM);
10212
10213   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10214   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10215   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
10216
10217   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
10218   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
10219
10220   return "";
10221 }
10222
10223 /* Output a move from an fpa register to arm registers.
10224    OPERANDS[0] is the first registers of an arm register pair.
10225    OPERANDS[1] is an fpa register.  */
10226 const char *
10227 output_mov_long_double_arm_from_fpa (rtx *operands)
10228 {
10229   int arm_reg0 = REGNO (operands[0]);
10230   rtx ops[3];
10231
10232   gcc_assert (arm_reg0 != IP_REGNUM);
10233
10234   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10235   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10236   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
10237
10238   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
10239   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
10240   return "";
10241 }
10242
10243 /* Output a move from arm registers to arm registers of a long double
10244    OPERANDS[0] is the destination.
10245    OPERANDS[1] is the source.  */
10246 const char *
10247 output_mov_long_double_arm_from_arm (rtx *operands)
10248 {
10249   /* We have to be careful here because the two might overlap.  */
10250   int dest_start = REGNO (operands[0]);
10251   int src_start = REGNO (operands[1]);
10252   rtx ops[2];
10253   int i;
10254
10255   if (dest_start < src_start)
10256     {
10257       for (i = 0; i < 3; i++)
10258         {
10259           ops[0] = gen_rtx_REG (SImode, dest_start + i);
10260           ops[1] = gen_rtx_REG (SImode, src_start + i);
10261           output_asm_insn ("mov%?\t%0, %1", ops);
10262         }
10263     }
10264   else
10265     {
10266       for (i = 2; i >= 0; i--)
10267         {
10268           ops[0] = gen_rtx_REG (SImode, dest_start + i);
10269           ops[1] = gen_rtx_REG (SImode, src_start + i);
10270           output_asm_insn ("mov%?\t%0, %1", ops);
10271         }
10272     }
10273
10274   return "";
10275 }
10276
10277
10278 /* Emit a MOVW/MOVT pair.  */
10279 void arm_emit_movpair (rtx dest, rtx src)
10280 {
10281   emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
10282   emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
10283 }
10284
10285
10286 /* Output a move from arm registers to an fpa registers.
10287    OPERANDS[0] is an fpa register.
10288    OPERANDS[1] is the first registers of an arm register pair.  */
10289 const char *
10290 output_mov_double_fpa_from_arm (rtx *operands)
10291 {
10292   int arm_reg0 = REGNO (operands[1]);
10293   rtx ops[2];
10294
10295   gcc_assert (arm_reg0 != IP_REGNUM);
10296
10297   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10298   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10299   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
10300   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
10301   return "";
10302 }
10303
10304 /* Output a move from an fpa register to arm registers.
10305    OPERANDS[0] is the first registers of an arm register pair.
10306    OPERANDS[1] is an fpa register.  */
10307 const char *
10308 output_mov_double_arm_from_fpa (rtx *operands)
10309 {
10310   int arm_reg0 = REGNO (operands[0]);
10311   rtx ops[2];
10312
10313   gcc_assert (arm_reg0 != IP_REGNUM);
10314
10315   ops[0] = gen_rtx_REG (SImode, arm_reg0);
10316   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
10317   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
10318   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
10319   return "";
10320 }
10321
10322 /* Output a move between double words.
10323    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
10324    or MEM<-REG and all MEMs must be offsettable addresses.  */
10325 const char *
10326 output_move_double (rtx *operands)
10327 {
10328   enum rtx_code code0 = GET_CODE (operands[0]);
10329   enum rtx_code code1 = GET_CODE (operands[1]);
10330   rtx otherops[3];
10331
10332   if (code0 == REG)
10333     {
10334       unsigned int reg0 = REGNO (operands[0]);
10335
10336       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10337
10338       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
10339
10340       switch (GET_CODE (XEXP (operands[1], 0)))
10341         {
10342         case REG:
10343           if (TARGET_LDRD
10344               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
10345             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
10346           else
10347             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10348           break;
10349
10350         case PRE_INC:
10351           gcc_assert (TARGET_LDRD);
10352           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
10353           break;
10354
10355         case PRE_DEC:
10356           if (TARGET_LDRD)
10357             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
10358           else
10359             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
10360           break;
10361
10362         case POST_INC:
10363           if (TARGET_LDRD)
10364             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
10365           else
10366             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
10367           break;
10368
10369         case POST_DEC:
10370           gcc_assert (TARGET_LDRD);
10371           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
10372           break;
10373
10374         case PRE_MODIFY:
10375         case POST_MODIFY:
10376           /* Autoicrement addressing modes should never have overlapping
10377              base and destination registers, and overlapping index registers
10378              are already prohibited, so this doesn't need to worry about
10379              fix_cm3_ldrd.  */
10380           otherops[0] = operands[0];
10381           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
10382           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
10383
10384           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
10385             {
10386               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
10387                 {
10388                   /* Registers overlap so split out the increment.  */
10389                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
10390                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
10391                 }
10392               else
10393                 {
10394                   /* IWMMXT allows offsets larger than ldrd can handle,
10395                      fix these up with a pair of ldr.  */
10396                   if (GET_CODE (otherops[2]) == CONST_INT
10397                       && (INTVAL(otherops[2]) <= -256
10398                           || INTVAL(otherops[2]) >= 256))
10399                     {
10400                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10401                       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10402                       output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10403                     }
10404                   else
10405                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
10406                 }
10407             }
10408           else
10409             {
10410               /* IWMMXT allows offsets larger than ldrd can handle,
10411                  fix these up with a pair of ldr.  */
10412               if (GET_CODE (otherops[2]) == CONST_INT
10413                   && (INTVAL(otherops[2]) <= -256
10414                       || INTVAL(otherops[2]) >= 256))
10415                 {
10416                   otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10417                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10418                   otherops[0] = operands[0];
10419                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10420                 }
10421               else
10422                 /* We only allow constant increments, so this is safe.  */
10423                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
10424             }
10425           break;
10426
10427         case LABEL_REF:
10428         case CONST:
10429           /* We might be able to use ldrd %0, %1 here.  However the range is
10430              different to ldr/adr, and it is broken on some ARMv7-M
10431              implementations.  */
10432           /* Use the second register of the pair to avoid problematic
10433              overlap.  */
10434           otherops[1] = operands[1];
10435           output_asm_insn ("adr%?\t%0, %1", otherops);
10436           operands[1] = otherops[0];
10437           if (TARGET_LDRD)
10438             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10439           else
10440             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
10441           break;
10442
10443           /* ??? This needs checking for thumb2.  */
10444         default:
10445           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
10446                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
10447             {
10448               otherops[0] = operands[0];
10449               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
10450               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
10451
10452               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
10453                 {
10454                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10455                     {
10456                       switch ((int) INTVAL (otherops[2]))
10457                         {
10458                         case -8:
10459                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
10460                           return "";
10461                         case -4:
10462                           if (TARGET_THUMB2)
10463                             break;
10464                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
10465                           return "";
10466                         case 4:
10467                           if (TARGET_THUMB2)
10468                             break;
10469                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
10470                           return "";
10471                         }
10472                     }
10473                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
10474                   operands[1] = otherops[0];
10475                   if (TARGET_LDRD
10476                       && (GET_CODE (otherops[2]) == REG
10477                           || (GET_CODE (otherops[2]) == CONST_INT
10478                               && INTVAL (otherops[2]) > -256
10479                               && INTVAL (otherops[2]) < 256)))
10480                     {
10481                       if (reg_overlap_mentioned_p (operands[0],
10482                                                    otherops[2]))
10483                         {
10484                           rtx tmp;
10485                           /* Swap base and index registers over to
10486                              avoid a conflict.  */
10487                           tmp = otherops[1];
10488                           otherops[1] = otherops[2];
10489                           otherops[2] = tmp;
10490                         }
10491                       /* If both registers conflict, it will usually
10492                          have been fixed by a splitter.  */
10493                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
10494                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
10495                         {
10496                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
10497                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10498                         }
10499                       else
10500                         {
10501                           otherops[0] = operands[0];
10502                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
10503                         }
10504                       return "";
10505                     }
10506
10507                   if (GET_CODE (otherops[2]) == CONST_INT)
10508                     {
10509                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
10510                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
10511                       else
10512                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
10513                     }
10514                   else
10515                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
10516                 }
10517               else
10518                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
10519
10520               if (TARGET_LDRD)
10521                 return "ldr%(d%)\t%0, [%1]";
10522
10523               return "ldm%(ia%)\t%1, %M0";
10524             }
10525           else
10526             {
10527               otherops[1] = adjust_address (operands[1], SImode, 4);
10528               /* Take care of overlapping base/data reg.  */
10529               if (reg_mentioned_p (operands[0], operands[1]))
10530                 {
10531                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10532                   output_asm_insn ("ldr%?\t%0, %1", operands);
10533                 }
10534               else
10535                 {
10536                   output_asm_insn ("ldr%?\t%0, %1", operands);
10537                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10538                 }
10539             }
10540         }
10541     }
10542   else
10543     {
10544       /* Constraints should ensure this.  */
10545       gcc_assert (code0 == MEM && code1 == REG);
10546       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
10547
10548       switch (GET_CODE (XEXP (operands[0], 0)))
10549         {
10550         case REG:
10551           if (TARGET_LDRD)
10552             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
10553           else
10554             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10555           break;
10556
10557         case PRE_INC:
10558           gcc_assert (TARGET_LDRD);
10559           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
10560           break;
10561
10562         case PRE_DEC:
10563           if (TARGET_LDRD)
10564             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
10565           else
10566             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
10567           break;
10568
10569         case POST_INC:
10570           if (TARGET_LDRD)
10571             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
10572           else
10573             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
10574           break;
10575
10576         case POST_DEC:
10577           gcc_assert (TARGET_LDRD);
10578           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
10579           break;
10580
10581         case PRE_MODIFY:
10582         case POST_MODIFY:
10583           otherops[0] = operands[1];
10584           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
10585           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
10586
10587           /* IWMMXT allows offsets larger than ldrd can handle,
10588              fix these up with a pair of ldr.  */
10589           if (GET_CODE (otherops[2]) == CONST_INT
10590               && (INTVAL(otherops[2]) <= -256
10591                   || INTVAL(otherops[2]) >= 256))
10592             {
10593               rtx reg1;
10594               reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10595               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10596                 {
10597                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10598                   otherops[0] = reg1;
10599                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10600                 }
10601               else
10602                 {
10603                   otherops[0] = reg1;
10604                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10605                   otherops[0] = operands[1];
10606                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10607                 }
10608             }
10609           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10610             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
10611           else
10612             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
10613           break;
10614
10615         case PLUS:
10616           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
10617           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10618             {
10619               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
10620                 {
10621                 case -8:
10622                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
10623                   return "";
10624
10625                 case -4:
10626                   if (TARGET_THUMB2)
10627                     break;
10628                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
10629                   return "";
10630
10631                 case 4:
10632                   if (TARGET_THUMB2)
10633                     break;
10634                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
10635                   return "";
10636                 }
10637             }
10638           if (TARGET_LDRD
10639               && (GET_CODE (otherops[2]) == REG
10640                   || (GET_CODE (otherops[2]) == CONST_INT
10641                       && INTVAL (otherops[2]) > -256
10642                       && INTVAL (otherops[2]) < 256)))
10643             {
10644               otherops[0] = operands[1];
10645               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
10646               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
10647               return "";
10648             }
10649           /* Fall through */
10650
10651         default:
10652           otherops[0] = adjust_address (operands[0], SImode, 4);
10653           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10654           output_asm_insn ("str%?\t%1, %0", operands);
10655           output_asm_insn ("str%?\t%1, %0", otherops);
10656         }
10657     }
10658
10659   return "";
10660 }
10661
10662 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
10663    handles MEMs accepted by neon_vector_mem_operand with CORE=true.  */
10664
10665 const char *
10666 output_move_quad (rtx *operands)
10667 {
10668   if (REG_P (operands[0]))
10669     {
10670       /* Load, or reg->reg move.  */
10671
10672       if (MEM_P (operands[1]))
10673         {
10674           switch (GET_CODE (XEXP (operands[1], 0)))
10675             {
10676             case REG:
10677               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10678               break;
10679
10680             case LABEL_REF:
10681             case CONST:
10682               output_asm_insn ("adr%?\t%0, %1", operands);
10683               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
10684               break;
10685
10686             default:
10687               gcc_unreachable ();
10688             }
10689         }
10690       else
10691         {
10692           rtx ops[2];
10693           int dest, src, i;
10694
10695           gcc_assert (REG_P (operands[1]));
10696
10697           dest = REGNO (operands[0]);
10698           src = REGNO (operands[1]);
10699
10700           /* This seems pretty dumb, but hopefully GCC won't try to do it
10701              very often.  */
10702           if (dest < src)
10703             for (i = 0; i < 4; i++)
10704               {
10705                 ops[0] = gen_rtx_REG (SImode, dest + i);
10706                 ops[1] = gen_rtx_REG (SImode, src + i);
10707                 output_asm_insn ("mov%?\t%0, %1", ops);
10708               }
10709           else
10710             for (i = 3; i >= 0; i--)
10711               {
10712                 ops[0] = gen_rtx_REG (SImode, dest + i);
10713                 ops[1] = gen_rtx_REG (SImode, src + i);
10714                 output_asm_insn ("mov%?\t%0, %1", ops);
10715               }
10716         }
10717     }
10718   else
10719     {
10720       gcc_assert (MEM_P (operands[0]));
10721       gcc_assert (REG_P (operands[1]));
10722       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
10723
10724       switch (GET_CODE (XEXP (operands[0], 0)))
10725         {
10726         case REG:
10727           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10728           break;
10729
10730         default:
10731           gcc_unreachable ();
10732         }
10733     }
10734
10735   return "";
10736 }
10737
10738 /* Output a VFP load or store instruction.  */
10739
10740 const char *
10741 output_move_vfp (rtx *operands)
10742 {
10743   rtx reg, mem, addr, ops[2];
10744   int load = REG_P (operands[0]);
10745   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
10746   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
10747   const char *templ;
10748   char buff[50];
10749   enum machine_mode mode;
10750
10751   reg = operands[!load];
10752   mem = operands[load];
10753
10754   mode = GET_MODE (reg);
10755
10756   gcc_assert (REG_P (reg));
10757   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
10758   gcc_assert (mode == SFmode
10759               || mode == DFmode
10760               || mode == SImode
10761               || mode == DImode
10762               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
10763   gcc_assert (MEM_P (mem));
10764
10765   addr = XEXP (mem, 0);
10766
10767   switch (GET_CODE (addr))
10768     {
10769     case PRE_DEC:
10770       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
10771       ops[0] = XEXP (addr, 0);
10772       ops[1] = reg;
10773       break;
10774
10775     case POST_INC:
10776       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
10777       ops[0] = XEXP (addr, 0);
10778       ops[1] = reg;
10779       break;
10780
10781     default:
10782       templ = "f%s%c%%?\t%%%s0, %%1%s";
10783       ops[0] = reg;
10784       ops[1] = mem;
10785       break;
10786     }
10787
10788   sprintf (buff, templ,
10789            load ? "ld" : "st",
10790            dp ? 'd' : 's',
10791            dp ? "P" : "",
10792            integer_p ? "\t%@ int" : "");
10793   output_asm_insn (buff, ops);
10794
10795   return "";
10796 }
10797
10798 /* Output a Neon quad-word load or store, or a load or store for
10799    larger structure modes.
10800
10801    WARNING: The ordering of elements is weird in big-endian mode,
10802    because we use VSTM, as required by the EABI.  GCC RTL defines
10803    element ordering based on in-memory order.  This can be differ
10804    from the architectural ordering of elements within a NEON register.
10805    The intrinsics defined in arm_neon.h use the NEON register element
10806    ordering, not the GCC RTL element ordering.
10807
10808    For example, the in-memory ordering of a big-endian a quadword
10809    vector with 16-bit elements when stored from register pair {d0,d1}
10810    will be (lowest address first, d0[N] is NEON register element N):
10811
10812      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
10813
10814    When necessary, quadword registers (dN, dN+1) are moved to ARM
10815    registers from rN in the order:
10816
10817      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
10818
10819    So that STM/LDM can be used on vectors in ARM registers, and the
10820    same memory layout will result as if VSTM/VLDM were used.  */
10821
10822 const char *
10823 output_move_neon (rtx *operands)
10824 {
10825   rtx reg, mem, addr, ops[2];
10826   int regno, load = REG_P (operands[0]);
10827   const char *templ;
10828   char buff[50];
10829   enum machine_mode mode;
10830
10831   reg = operands[!load];
10832   mem = operands[load];
10833
10834   mode = GET_MODE (reg);
10835
10836   gcc_assert (REG_P (reg));
10837   regno = REGNO (reg);
10838   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
10839               || NEON_REGNO_OK_FOR_QUAD (regno));
10840   gcc_assert (VALID_NEON_DREG_MODE (mode)
10841               || VALID_NEON_QREG_MODE (mode)
10842               || VALID_NEON_STRUCT_MODE (mode));
10843   gcc_assert (MEM_P (mem));
10844
10845   addr = XEXP (mem, 0);
10846
10847   /* Strip off const from addresses like (const (plus (...))).  */
10848   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
10849     addr = XEXP (addr, 0);
10850
10851   switch (GET_CODE (addr))
10852     {
10853     case POST_INC:
10854       templ = "v%smia%%?\t%%0!, %%h1";
10855       ops[0] = XEXP (addr, 0);
10856       ops[1] = reg;
10857       break;
10858
10859     case POST_MODIFY:
10860       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
10861       gcc_unreachable ();
10862
10863     case LABEL_REF:
10864     case PLUS:
10865       {
10866         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
10867         int i;
10868         int overlap = -1;
10869         for (i = 0; i < nregs; i++)
10870           {
10871             /* We're only using DImode here because it's a convenient size.  */
10872             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
10873             ops[1] = adjust_address (mem, SImode, 8 * i);
10874             if (reg_overlap_mentioned_p (ops[0], mem))
10875               {
10876                 gcc_assert (overlap == -1);
10877                 overlap = i;
10878               }
10879             else
10880               {
10881                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10882                 output_asm_insn (buff, ops);
10883               }
10884           }
10885         if (overlap != -1)
10886           {
10887             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
10888             ops[1] = adjust_address (mem, SImode, 8 * overlap);
10889             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10890             output_asm_insn (buff, ops);
10891           }
10892
10893         return "";
10894       }
10895
10896     default:
10897       templ = "v%smia%%?\t%%m0, %%h1";
10898       ops[0] = mem;
10899       ops[1] = reg;
10900     }
10901
10902   sprintf (buff, templ, load ? "ld" : "st");
10903   output_asm_insn (buff, ops);
10904
10905   return "";
10906 }
10907
10908 /* Output an ADD r, s, #n where n may be too big for one instruction.
10909    If adding zero to one register, output nothing.  */
10910 const char *
10911 output_add_immediate (rtx *operands)
10912 {
10913   HOST_WIDE_INT n = INTVAL (operands[2]);
10914
10915   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
10916     {
10917       if (n < 0)
10918         output_multi_immediate (operands,
10919                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
10920                                 -n);
10921       else
10922         output_multi_immediate (operands,
10923                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
10924                                 n);
10925     }
10926
10927   return "";
10928 }
10929
10930 /* Output a multiple immediate operation.
10931    OPERANDS is the vector of operands referred to in the output patterns.
10932    INSTR1 is the output pattern to use for the first constant.
10933    INSTR2 is the output pattern to use for subsequent constants.
10934    IMMED_OP is the index of the constant slot in OPERANDS.
10935    N is the constant value.  */
10936 static const char *
10937 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
10938                         int immed_op, HOST_WIDE_INT n)
10939 {
10940 #if HOST_BITS_PER_WIDE_INT > 32
10941   n &= 0xffffffff;
10942 #endif
10943
10944   if (n == 0)
10945     {
10946       /* Quick and easy output.  */
10947       operands[immed_op] = const0_rtx;
10948       output_asm_insn (instr1, operands);
10949     }
10950   else
10951     {
10952       int i;
10953       const char * instr = instr1;
10954
10955       /* Note that n is never zero here (which would give no output).  */
10956       for (i = 0; i < 32; i += 2)
10957         {
10958           if (n & (3 << i))
10959             {
10960               operands[immed_op] = GEN_INT (n & (255 << i));
10961               output_asm_insn (instr, operands);
10962               instr = instr2;
10963               i += 6;
10964             }
10965         }
10966     }
10967
10968   return "";
10969 }
10970
10971 /* Return the name of a shifter operation.  */
10972 static const char *
10973 arm_shift_nmem(enum rtx_code code)
10974 {
10975   switch (code)
10976     {
10977     case ASHIFT:
10978       return ARM_LSL_NAME;
10979
10980     case ASHIFTRT:
10981       return "asr";
10982
10983     case LSHIFTRT:
10984       return "lsr";
10985
10986     case ROTATERT:
10987       return "ror";
10988
10989     default:
10990       abort();
10991     }
10992 }
10993
10994 /* Return the appropriate ARM instruction for the operation code.
10995    The returned result should not be overwritten.  OP is the rtx of the
10996    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
10997    was shifted.  */
10998 const char *
10999 arithmetic_instr (rtx op, int shift_first_arg)
11000 {
11001   switch (GET_CODE (op))
11002     {
11003     case PLUS:
11004       return "add";
11005
11006     case MINUS:
11007       return shift_first_arg ? "rsb" : "sub";
11008
11009     case IOR:
11010       return "orr";
11011
11012     case XOR:
11013       return "eor";
11014
11015     case AND:
11016       return "and";
11017
11018     case ASHIFT:
11019     case ASHIFTRT:
11020     case LSHIFTRT:
11021     case ROTATERT:
11022       return arm_shift_nmem(GET_CODE(op));
11023
11024     default:
11025       gcc_unreachable ();
11026     }
11027 }
11028
11029 /* Ensure valid constant shifts and return the appropriate shift mnemonic
11030    for the operation code.  The returned result should not be overwritten.
11031    OP is the rtx code of the shift.
11032    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
11033    shift.  */
11034 static const char *
11035 shift_op (rtx op, HOST_WIDE_INT *amountp)
11036 {
11037   const char * mnem;
11038   enum rtx_code code = GET_CODE (op);
11039
11040   switch (GET_CODE (XEXP (op, 1)))
11041     {
11042     case REG:
11043     case SUBREG:
11044       *amountp = -1;
11045       break;
11046
11047     case CONST_INT:
11048       *amountp = INTVAL (XEXP (op, 1));
11049       break;
11050
11051     default:
11052       gcc_unreachable ();
11053     }
11054
11055   switch (code)
11056     {
11057     case ROTATE:
11058       gcc_assert (*amountp != -1);
11059       *amountp = 32 - *amountp;
11060       code = ROTATERT;
11061
11062       /* Fall through.  */
11063
11064     case ASHIFT:
11065     case ASHIFTRT:
11066     case LSHIFTRT:
11067     case ROTATERT:
11068       mnem = arm_shift_nmem(code);
11069       break;
11070
11071     case MULT:
11072       /* We never have to worry about the amount being other than a
11073          power of 2, since this case can never be reloaded from a reg.  */
11074       gcc_assert (*amountp != -1);
11075       *amountp = int_log2 (*amountp);
11076       return ARM_LSL_NAME;
11077
11078     default:
11079       gcc_unreachable ();
11080     }
11081
11082   if (*amountp != -1)
11083     {
11084       /* This is not 100% correct, but follows from the desire to merge
11085          multiplication by a power of 2 with the recognizer for a
11086          shift.  >=32 is not a valid shift for "lsl", so we must try and
11087          output a shift that produces the correct arithmetical result.
11088          Using lsr #32 is identical except for the fact that the carry bit
11089          is not set correctly if we set the flags; but we never use the
11090          carry bit from such an operation, so we can ignore that.  */
11091       if (code == ROTATERT)
11092         /* Rotate is just modulo 32.  */
11093         *amountp &= 31;
11094       else if (*amountp != (*amountp & 31))
11095         {
11096           if (code == ASHIFT)
11097             mnem = "lsr";
11098           *amountp = 32;
11099         }
11100
11101       /* Shifts of 0 are no-ops.  */
11102       if (*amountp == 0)
11103         return NULL;
11104     }
11105
11106   return mnem;
11107 }
11108
11109 /* Obtain the shift from the POWER of two.  */
11110
11111 static HOST_WIDE_INT
11112 int_log2 (HOST_WIDE_INT power)
11113 {
11114   HOST_WIDE_INT shift = 0;
11115
11116   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
11117     {
11118       gcc_assert (shift <= 31);
11119       shift++;
11120     }
11121
11122   return shift;
11123 }
11124
11125 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
11126    because /bin/as is horribly restrictive.  The judgement about
11127    whether or not each character is 'printable' (and can be output as
11128    is) or not (and must be printed with an octal escape) must be made
11129    with reference to the *host* character set -- the situation is
11130    similar to that discussed in the comments above pp_c_char in
11131    c-pretty-print.c.  */
11132
11133 #define MAX_ASCII_LEN 51
11134
11135 void
11136 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
11137 {
11138   int i;
11139   int len_so_far = 0;
11140
11141   fputs ("\t.ascii\t\"", stream);
11142
11143   for (i = 0; i < len; i++)
11144     {
11145       int c = p[i];
11146
11147       if (len_so_far >= MAX_ASCII_LEN)
11148         {
11149           fputs ("\"\n\t.ascii\t\"", stream);
11150           len_so_far = 0;
11151         }
11152
11153       if (ISPRINT (c))
11154         {
11155           if (c == '\\' || c == '\"')
11156             {
11157               putc ('\\', stream);
11158               len_so_far++;
11159             }
11160           putc (c, stream);
11161           len_so_far++;
11162         }
11163       else
11164         {
11165           fprintf (stream, "\\%03o", c);
11166           len_so_far += 4;
11167         }
11168     }
11169
11170   fputs ("\"\n", stream);
11171 }
11172 \f
11173 /* Compute the register save mask for registers 0 through 12
11174    inclusive.  This code is used by arm_compute_save_reg_mask.  */
11175
11176 static unsigned long
11177 arm_compute_save_reg0_reg12_mask (void)
11178 {
11179   unsigned long func_type = arm_current_func_type ();
11180   unsigned long save_reg_mask = 0;
11181   unsigned int reg;
11182
11183   if (IS_INTERRUPT (func_type))
11184     {
11185       unsigned int max_reg;
11186       /* Interrupt functions must not corrupt any registers,
11187          even call clobbered ones.  If this is a leaf function
11188          we can just examine the registers used by the RTL, but
11189          otherwise we have to assume that whatever function is
11190          called might clobber anything, and so we have to save
11191          all the call-clobbered registers as well.  */
11192       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
11193         /* FIQ handlers have registers r8 - r12 banked, so
11194            we only need to check r0 - r7, Normal ISRs only
11195            bank r14 and r15, so we must check up to r12.
11196            r13 is the stack pointer which is always preserved,
11197            so we do not need to consider it here.  */
11198         max_reg = 7;
11199       else
11200         max_reg = 12;
11201
11202       for (reg = 0; reg <= max_reg; reg++)
11203         if (df_regs_ever_live_p (reg)
11204             || (! current_function_is_leaf && call_used_regs[reg]))
11205           save_reg_mask |= (1 << reg);
11206
11207       /* Also save the pic base register if necessary.  */
11208       if (flag_pic
11209           && !TARGET_SINGLE_PIC_BASE
11210           && arm_pic_register != INVALID_REGNUM
11211           && crtl->uses_pic_offset_table)
11212         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11213     }
11214   else
11215     {
11216       /* In the normal case we only need to save those registers
11217          which are call saved and which are used by this function.  */
11218       for (reg = 0; reg <= 11; reg++)
11219         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
11220           save_reg_mask |= (1 << reg);
11221
11222       /* Handle the frame pointer as a special case.  */
11223       if (frame_pointer_needed)
11224         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
11225
11226       /* If we aren't loading the PIC register,
11227          don't stack it even though it may be live.  */
11228       if (flag_pic
11229           && !TARGET_SINGLE_PIC_BASE
11230           && arm_pic_register != INVALID_REGNUM
11231           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
11232               || crtl->uses_pic_offset_table))
11233         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11234
11235       /* The prologue will copy SP into R0, so save it.  */
11236       if (IS_STACKALIGN (func_type))
11237         save_reg_mask |= 1;
11238     }
11239
11240   /* Save registers so the exception handler can modify them.  */
11241   if (crtl->calls_eh_return)
11242     {
11243       unsigned int i;
11244
11245       for (i = 0; ; i++)
11246         {
11247           reg = EH_RETURN_DATA_REGNO (i);
11248           if (reg == INVALID_REGNUM)
11249             break;
11250           save_reg_mask |= 1 << reg;
11251         }
11252     }
11253
11254   return save_reg_mask;
11255 }
11256
11257
11258 /* Compute the number of bytes used to store the static chain register on the 
11259    stack, above the stack frame. We need to know this accurately to get the
11260    alignment of the rest of the stack frame correct. */
11261
11262 static int arm_compute_static_chain_stack_bytes (void)
11263 {
11264   unsigned long func_type = arm_current_func_type ();
11265   int static_chain_stack_bytes = 0;
11266
11267   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
11268       IS_NESTED (func_type) &&
11269       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
11270     static_chain_stack_bytes = 4;
11271
11272   return static_chain_stack_bytes;
11273 }
11274
11275
11276 /* Compute a bit mask of which registers need to be
11277    saved on the stack for the current function.
11278    This is used by arm_get_frame_offsets, which may add extra registers.  */
11279
11280 static unsigned long
11281 arm_compute_save_reg_mask (void)
11282 {
11283   unsigned int save_reg_mask = 0;
11284   unsigned long func_type = arm_current_func_type ();
11285   unsigned int reg;
11286
11287   if (IS_NAKED (func_type))
11288     /* This should never really happen.  */
11289     return 0;
11290
11291   /* If we are creating a stack frame, then we must save the frame pointer,
11292      IP (which will hold the old stack pointer), LR and the PC.  */
11293   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
11294     save_reg_mask |=
11295       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
11296       | (1 << IP_REGNUM)
11297       | (1 << LR_REGNUM)
11298       | (1 << PC_REGNUM);
11299
11300   /* Volatile functions do not return, so there
11301      is no need to save any other registers.  */
11302   if (IS_VOLATILE (func_type))
11303     return save_reg_mask;
11304
11305   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
11306
11307   /* Decide if we need to save the link register.
11308      Interrupt routines have their own banked link register,
11309      so they never need to save it.
11310      Otherwise if we do not use the link register we do not need to save
11311      it.  If we are pushing other registers onto the stack however, we
11312      can save an instruction in the epilogue by pushing the link register
11313      now and then popping it back into the PC.  This incurs extra memory
11314      accesses though, so we only do it when optimizing for size, and only
11315      if we know that we will not need a fancy return sequence.  */
11316   if (df_regs_ever_live_p (LR_REGNUM)
11317       || (save_reg_mask
11318           && optimize_size
11319           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
11320           && !crtl->calls_eh_return))
11321     save_reg_mask |= 1 << LR_REGNUM;
11322
11323   if (cfun->machine->lr_save_eliminated)
11324     save_reg_mask &= ~ (1 << LR_REGNUM);
11325
11326   if (TARGET_REALLY_IWMMXT
11327       && ((bit_count (save_reg_mask)
11328            + ARM_NUM_INTS (crtl->args.pretend_args_size +
11329                            arm_compute_static_chain_stack_bytes())
11330            ) % 2) != 0)
11331     {
11332       /* The total number of registers that are going to be pushed
11333          onto the stack is odd.  We need to ensure that the stack
11334          is 64-bit aligned before we start to save iWMMXt registers,
11335          and also before we start to create locals.  (A local variable
11336          might be a double or long long which we will load/store using
11337          an iWMMXt instruction).  Therefore we need to push another
11338          ARM register, so that the stack will be 64-bit aligned.  We
11339          try to avoid using the arg registers (r0 -r3) as they might be
11340          used to pass values in a tail call.  */
11341       for (reg = 4; reg <= 12; reg++)
11342         if ((save_reg_mask & (1 << reg)) == 0)
11343           break;
11344
11345       if (reg <= 12)
11346         save_reg_mask |= (1 << reg);
11347       else
11348         {
11349           cfun->machine->sibcall_blocked = 1;
11350           save_reg_mask |= (1 << 3);
11351         }
11352     }
11353
11354   /* We may need to push an additional register for use initializing the
11355      PIC base register.  */
11356   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
11357       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
11358     {
11359       reg = thumb_find_work_register (1 << 4);
11360       if (!call_used_regs[reg])
11361         save_reg_mask |= (1 << reg);
11362     }
11363
11364   return save_reg_mask;
11365 }
11366
11367
11368 /* Compute a bit mask of which registers need to be
11369    saved on the stack for the current function.  */
11370 static unsigned long
11371 thumb1_compute_save_reg_mask (void)
11372 {
11373   unsigned long mask;
11374   unsigned reg;
11375
11376   mask = 0;
11377   for (reg = 0; reg < 12; reg ++)
11378     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11379       mask |= 1 << reg;
11380
11381   if (flag_pic
11382       && !TARGET_SINGLE_PIC_BASE
11383       && arm_pic_register != INVALID_REGNUM
11384       && crtl->uses_pic_offset_table)
11385     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11386
11387   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
11388   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
11389     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
11390
11391   /* LR will also be pushed if any lo regs are pushed.  */
11392   if (mask & 0xff || thumb_force_lr_save ())
11393     mask |= (1 << LR_REGNUM);
11394
11395   /* Make sure we have a low work register if we need one.
11396      We will need one if we are going to push a high register,
11397      but we are not currently intending to push a low register.  */
11398   if ((mask & 0xff) == 0
11399       && ((mask & 0x0f00) || TARGET_BACKTRACE))
11400     {
11401       /* Use thumb_find_work_register to choose which register
11402          we will use.  If the register is live then we will
11403          have to push it.  Use LAST_LO_REGNUM as our fallback
11404          choice for the register to select.  */
11405       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
11406       /* Make sure the register returned by thumb_find_work_register is
11407          not part of the return value.  */
11408       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
11409         reg = LAST_LO_REGNUM;
11410
11411       if (! call_used_regs[reg])
11412         mask |= 1 << reg;
11413     }
11414
11415   /* The 504 below is 8 bytes less than 512 because there are two possible
11416      alignment words.  We can't tell here if they will be present or not so we
11417      have to play it safe and assume that they are. */
11418   if ((CALLER_INTERWORKING_SLOT_SIZE +
11419        ROUND_UP_WORD (get_frame_size ()) +
11420        crtl->outgoing_args_size) >= 504)
11421     {
11422       /* This is the same as the code in thumb1_expand_prologue() which
11423          determines which register to use for stack decrement. */
11424       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
11425         if (mask & (1 << reg))
11426           break;
11427
11428       if (reg > LAST_LO_REGNUM)
11429         {
11430           /* Make sure we have a register available for stack decrement. */
11431           mask |= 1 << LAST_LO_REGNUM;
11432         }
11433     }
11434
11435   return mask;
11436 }
11437
11438
11439 /* Return the number of bytes required to save VFP registers.  */
11440 static int
11441 arm_get_vfp_saved_size (void)
11442 {
11443   unsigned int regno;
11444   int count;
11445   int saved;
11446
11447   saved = 0;
11448   /* Space for saved VFP registers.  */
11449   if (TARGET_HARD_FLOAT && TARGET_VFP)
11450     {
11451       count = 0;
11452       for (regno = FIRST_VFP_REGNUM;
11453            regno < LAST_VFP_REGNUM;
11454            regno += 2)
11455         {
11456           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
11457               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
11458             {
11459               if (count > 0)
11460                 {
11461                   /* Workaround ARM10 VFPr1 bug.  */
11462                   if (count == 2 && !arm_arch6)
11463                     count++;
11464                   saved += count * 8;
11465                 }
11466               count = 0;
11467             }
11468           else
11469             count++;
11470         }
11471       if (count > 0)
11472         {
11473           if (count == 2 && !arm_arch6)
11474             count++;
11475           saved += count * 8;
11476         }
11477     }
11478   return saved;
11479 }
11480
11481
11482 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
11483    everything bar the final return instruction.  */
11484 const char *
11485 output_return_instruction (rtx operand, int really_return, int reverse)
11486 {
11487   char conditional[10];
11488   char instr[100];
11489   unsigned reg;
11490   unsigned long live_regs_mask;
11491   unsigned long func_type;
11492   arm_stack_offsets *offsets;
11493
11494   func_type = arm_current_func_type ();
11495
11496   if (IS_NAKED (func_type))
11497     return "";
11498
11499   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11500     {
11501       /* If this function was declared non-returning, and we have
11502          found a tail call, then we have to trust that the called
11503          function won't return.  */
11504       if (really_return)
11505         {
11506           rtx ops[2];
11507
11508           /* Otherwise, trap an attempted return by aborting.  */
11509           ops[0] = operand;
11510           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
11511                                        : "abort");
11512           assemble_external_libcall (ops[1]);
11513           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
11514         }
11515
11516       return "";
11517     }
11518
11519   gcc_assert (!cfun->calls_alloca || really_return);
11520
11521   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
11522
11523   return_used_this_function = 1;
11524
11525   offsets = arm_get_frame_offsets ();
11526   live_regs_mask = offsets->saved_regs_mask;
11527
11528   if (live_regs_mask)
11529     {
11530       const char * return_reg;
11531
11532       /* If we do not have any special requirements for function exit
11533          (e.g. interworking) then we can load the return address
11534          directly into the PC.  Otherwise we must load it into LR.  */
11535       if (really_return
11536           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
11537         return_reg = reg_names[PC_REGNUM];
11538       else
11539         return_reg = reg_names[LR_REGNUM];
11540
11541       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
11542         {
11543           /* There are three possible reasons for the IP register
11544              being saved.  1) a stack frame was created, in which case
11545              IP contains the old stack pointer, or 2) an ISR routine
11546              corrupted it, or 3) it was saved to align the stack on
11547              iWMMXt.  In case 1, restore IP into SP, otherwise just
11548              restore IP.  */
11549           if (frame_pointer_needed)
11550             {
11551               live_regs_mask &= ~ (1 << IP_REGNUM);
11552               live_regs_mask |=   (1 << SP_REGNUM);
11553             }
11554           else
11555             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
11556         }
11557
11558       /* On some ARM architectures it is faster to use LDR rather than
11559          LDM to load a single register.  On other architectures, the
11560          cost is the same.  In 26 bit mode, or for exception handlers,
11561          we have to use LDM to load the PC so that the CPSR is also
11562          restored.  */
11563       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11564         if (live_regs_mask == (1U << reg))
11565           break;
11566
11567       if (reg <= LAST_ARM_REGNUM
11568           && (reg != LR_REGNUM
11569               || ! really_return
11570               || ! IS_INTERRUPT (func_type)))
11571         {
11572           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
11573                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
11574         }
11575       else
11576         {
11577           char *p;
11578           int first = 1;
11579
11580           /* Generate the load multiple instruction to restore the
11581              registers.  Note we can get here, even if
11582              frame_pointer_needed is true, but only if sp already
11583              points to the base of the saved core registers.  */
11584           if (live_regs_mask & (1 << SP_REGNUM))
11585             {
11586               unsigned HOST_WIDE_INT stack_adjust;
11587
11588               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
11589               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
11590
11591               if (stack_adjust && arm_arch5 && TARGET_ARM)
11592                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
11593               else
11594                 {
11595                   /* If we can't use ldmib (SA110 bug),
11596                      then try to pop r3 instead.  */
11597                   if (stack_adjust)
11598                     live_regs_mask |= 1 << 3;
11599                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
11600                 }
11601             }
11602           else
11603             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
11604
11605           p = instr + strlen (instr);
11606
11607           for (reg = 0; reg <= SP_REGNUM; reg++)
11608             if (live_regs_mask & (1 << reg))
11609               {
11610                 int l = strlen (reg_names[reg]);
11611
11612                 if (first)
11613                   first = 0;
11614                 else
11615                   {
11616                     memcpy (p, ", ", 2);
11617                     p += 2;
11618                   }
11619
11620                 memcpy (p, "%|", 2);
11621                 memcpy (p + 2, reg_names[reg], l);
11622                 p += l + 2;
11623               }
11624
11625           if (live_regs_mask & (1 << LR_REGNUM))
11626             {
11627               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
11628               /* If returning from an interrupt, restore the CPSR.  */
11629               if (IS_INTERRUPT (func_type))
11630                 strcat (p, "^");
11631             }
11632           else
11633             strcpy (p, "}");
11634         }
11635
11636       output_asm_insn (instr, & operand);
11637
11638       /* See if we need to generate an extra instruction to
11639          perform the actual function return.  */
11640       if (really_return
11641           && func_type != ARM_FT_INTERWORKED
11642           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
11643         {
11644           /* The return has already been handled
11645              by loading the LR into the PC.  */
11646           really_return = 0;
11647         }
11648     }
11649
11650   if (really_return)
11651     {
11652       switch ((int) ARM_FUNC_TYPE (func_type))
11653         {
11654         case ARM_FT_ISR:
11655         case ARM_FT_FIQ:
11656           /* ??? This is wrong for unified assembly syntax.  */
11657           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
11658           break;
11659
11660         case ARM_FT_INTERWORKED:
11661           sprintf (instr, "bx%s\t%%|lr", conditional);
11662           break;
11663
11664         case ARM_FT_EXCEPTION:
11665           /* ??? This is wrong for unified assembly syntax.  */
11666           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
11667           break;
11668
11669         default:
11670           /* Use bx if it's available.  */
11671           if (arm_arch5 || arm_arch4t)
11672             sprintf (instr, "bx%s\t%%|lr", conditional);
11673           else
11674             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
11675           break;
11676         }
11677
11678       output_asm_insn (instr, & operand);
11679     }
11680
11681   return "";
11682 }
11683
11684 /* Write the function name into the code section, directly preceding
11685    the function prologue.
11686
11687    Code will be output similar to this:
11688      t0
11689          .ascii "arm_poke_function_name", 0
11690          .align
11691      t1
11692          .word 0xff000000 + (t1 - t0)
11693      arm_poke_function_name
11694          mov     ip, sp
11695          stmfd   sp!, {fp, ip, lr, pc}
11696          sub     fp, ip, #4
11697
11698    When performing a stack backtrace, code can inspect the value
11699    of 'pc' stored at 'fp' + 0.  If the trace function then looks
11700    at location pc - 12 and the top 8 bits are set, then we know
11701    that there is a function name embedded immediately preceding this
11702    location and has length ((pc[-3]) & 0xff000000).
11703
11704    We assume that pc is declared as a pointer to an unsigned long.
11705
11706    It is of no benefit to output the function name if we are assembling
11707    a leaf function.  These function types will not contain a stack
11708    backtrace structure, therefore it is not possible to determine the
11709    function name.  */
11710 void
11711 arm_poke_function_name (FILE *stream, const char *name)
11712 {
11713   unsigned long alignlength;
11714   unsigned long length;
11715   rtx           x;
11716
11717   length      = strlen (name) + 1;
11718   alignlength = ROUND_UP_WORD (length);
11719
11720   ASM_OUTPUT_ASCII (stream, name, length);
11721   ASM_OUTPUT_ALIGN (stream, 2);
11722   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
11723   assemble_aligned_integer (UNITS_PER_WORD, x);
11724 }
11725
11726 /* Place some comments into the assembler stream
11727    describing the current function.  */
11728 static void
11729 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
11730 {
11731   unsigned long func_type;
11732
11733   if (TARGET_THUMB1)
11734     {
11735       thumb1_output_function_prologue (f, frame_size);
11736       return;
11737     }
11738
11739   /* Sanity check.  */
11740   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
11741
11742   func_type = arm_current_func_type ();
11743
11744   switch ((int) ARM_FUNC_TYPE (func_type))
11745     {
11746     default:
11747     case ARM_FT_NORMAL:
11748       break;
11749     case ARM_FT_INTERWORKED:
11750       asm_fprintf (f, "\t%@ Function supports interworking.\n");
11751       break;
11752     case ARM_FT_ISR:
11753       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
11754       break;
11755     case ARM_FT_FIQ:
11756       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
11757       break;
11758     case ARM_FT_EXCEPTION:
11759       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
11760       break;
11761     }
11762
11763   if (IS_NAKED (func_type))
11764     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
11765
11766   if (IS_VOLATILE (func_type))
11767     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
11768
11769   if (IS_NESTED (func_type))
11770     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
11771   if (IS_STACKALIGN (func_type))
11772     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
11773
11774   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
11775                crtl->args.size,
11776                crtl->args.pretend_args_size, frame_size);
11777
11778   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
11779                frame_pointer_needed,
11780                cfun->machine->uses_anonymous_args);
11781
11782   if (cfun->machine->lr_save_eliminated)
11783     asm_fprintf (f, "\t%@ link register save eliminated.\n");
11784
11785   if (crtl->calls_eh_return)
11786     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
11787
11788   return_used_this_function = 0;
11789 }
11790
11791 const char *
11792 arm_output_epilogue (rtx sibling)
11793 {
11794   int reg;
11795   unsigned long saved_regs_mask;
11796   unsigned long func_type;
11797   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
11798      frame that is $fp + 4 for a non-variadic function.  */
11799   int floats_offset = 0;
11800   rtx operands[3];
11801   FILE * f = asm_out_file;
11802   unsigned int lrm_count = 0;
11803   int really_return = (sibling == NULL);
11804   int start_reg;
11805   arm_stack_offsets *offsets;
11806
11807   /* If we have already generated the return instruction
11808      then it is futile to generate anything else.  */
11809   if (use_return_insn (FALSE, sibling) && return_used_this_function)
11810     return "";
11811
11812   func_type = arm_current_func_type ();
11813
11814   if (IS_NAKED (func_type))
11815     /* Naked functions don't have epilogues.  */
11816     return "";
11817
11818   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11819     {
11820       rtx op;
11821
11822       /* A volatile function should never return.  Call abort.  */
11823       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
11824       assemble_external_libcall (op);
11825       output_asm_insn ("bl\t%a0", &op);
11826
11827       return "";
11828     }
11829
11830   /* If we are throwing an exception, then we really must be doing a
11831      return, so we can't tail-call.  */
11832   gcc_assert (!crtl->calls_eh_return || really_return);
11833
11834   offsets = arm_get_frame_offsets ();
11835   saved_regs_mask = offsets->saved_regs_mask;
11836
11837   if (TARGET_IWMMXT)
11838     lrm_count = bit_count (saved_regs_mask);
11839
11840   floats_offset = offsets->saved_args;
11841   /* Compute how far away the floats will be.  */
11842   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11843     if (saved_regs_mask & (1 << reg))
11844       floats_offset += 4;
11845
11846   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
11847     {
11848       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
11849       int vfp_offset = offsets->frame;
11850
11851       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11852         {
11853           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11854             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11855               {
11856                 floats_offset += 12;
11857                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
11858                              reg, FP_REGNUM, floats_offset - vfp_offset);
11859               }
11860         }
11861       else
11862         {
11863           start_reg = LAST_FPA_REGNUM;
11864
11865           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11866             {
11867               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11868                 {
11869                   floats_offset += 12;
11870
11871                   /* We can't unstack more than four registers at once.  */
11872                   if (start_reg - reg == 3)
11873                     {
11874                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
11875                                    reg, FP_REGNUM, floats_offset - vfp_offset);
11876                       start_reg = reg - 1;
11877                     }
11878                 }
11879               else
11880                 {
11881                   if (reg != start_reg)
11882                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11883                                  reg + 1, start_reg - reg,
11884                                  FP_REGNUM, floats_offset - vfp_offset);
11885                   start_reg = reg - 1;
11886                 }
11887             }
11888
11889           /* Just in case the last register checked also needs unstacking.  */
11890           if (reg != start_reg)
11891             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11892                          reg + 1, start_reg - reg,
11893                          FP_REGNUM, floats_offset - vfp_offset);
11894         }
11895
11896       if (TARGET_HARD_FLOAT && TARGET_VFP)
11897         {
11898           int saved_size;
11899
11900           /* The fldmd insns do not have base+offset addressing
11901              modes, so we use IP to hold the address.  */
11902           saved_size = arm_get_vfp_saved_size ();
11903
11904           if (saved_size > 0)
11905             {
11906               floats_offset += saved_size;
11907               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
11908                            FP_REGNUM, floats_offset - vfp_offset);
11909             }
11910           start_reg = FIRST_VFP_REGNUM;
11911           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11912             {
11913               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11914                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
11915                 {
11916                   if (start_reg != reg)
11917                     vfp_output_fldmd (f, IP_REGNUM,
11918                                       (start_reg - FIRST_VFP_REGNUM) / 2,
11919                                       (reg - start_reg) / 2);
11920                   start_reg = reg + 2;
11921                 }
11922             }
11923           if (start_reg != reg)
11924             vfp_output_fldmd (f, IP_REGNUM,
11925                               (start_reg - FIRST_VFP_REGNUM) / 2,
11926                               (reg - start_reg) / 2);
11927         }
11928
11929       if (TARGET_IWMMXT)
11930         {
11931           /* The frame pointer is guaranteed to be non-double-word aligned.
11932              This is because it is set to (old_stack_pointer - 4) and the
11933              old_stack_pointer was double word aligned.  Thus the offset to
11934              the iWMMXt registers to be loaded must also be non-double-word
11935              sized, so that the resultant address *is* double-word aligned.
11936              We can ignore floats_offset since that was already included in
11937              the live_regs_mask.  */
11938           lrm_count += (lrm_count % 2 ? 2 : 1);
11939
11940           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
11941             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11942               {
11943                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
11944                              reg, FP_REGNUM, lrm_count * 4);
11945                 lrm_count += 2;
11946               }
11947         }
11948
11949       /* saved_regs_mask should contain the IP, which at the time of stack
11950          frame generation actually contains the old stack pointer.  So a
11951          quick way to unwind the stack is just pop the IP register directly
11952          into the stack pointer.  */
11953       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
11954       saved_regs_mask &= ~ (1 << IP_REGNUM);
11955       saved_regs_mask |=   (1 << SP_REGNUM);
11956
11957       /* There are two registers left in saved_regs_mask - LR and PC.  We
11958          only need to restore the LR register (the return address), but to
11959          save time we can load it directly into the PC, unless we need a
11960          special function exit sequence, or we are not really returning.  */
11961       if (really_return
11962           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
11963           && !crtl->calls_eh_return)
11964         /* Delete the LR from the register mask, so that the LR on
11965            the stack is loaded into the PC in the register mask.  */
11966         saved_regs_mask &= ~ (1 << LR_REGNUM);
11967       else
11968         saved_regs_mask &= ~ (1 << PC_REGNUM);
11969
11970       /* We must use SP as the base register, because SP is one of the
11971          registers being restored.  If an interrupt or page fault
11972          happens in the ldm instruction, the SP might or might not
11973          have been restored.  That would be bad, as then SP will no
11974          longer indicate the safe area of stack, and we can get stack
11975          corruption.  Using SP as the base register means that it will
11976          be reset correctly to the original value, should an interrupt
11977          occur.  If the stack pointer already points at the right
11978          place, then omit the subtraction.  */
11979       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
11980           || cfun->calls_alloca)
11981         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
11982                      4 * bit_count (saved_regs_mask));
11983       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
11984
11985       if (IS_INTERRUPT (func_type))
11986         /* Interrupt handlers will have pushed the
11987            IP onto the stack, so restore it now.  */
11988         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
11989     }
11990   else
11991     {
11992       /* This branch is executed for ARM mode (non-apcs frames) and
11993          Thumb-2 mode. Frame layout is essentially the same for those
11994          cases, except that in ARM mode frame pointer points to the
11995          first saved register, while in Thumb-2 mode the frame pointer points
11996          to the last saved register.
11997
11998          It is possible to make frame pointer point to last saved
11999          register in both cases, and remove some conditionals below.
12000          That means that fp setup in prologue would be just "mov fp, sp"
12001          and sp restore in epilogue would be just "mov sp, fp", whereas
12002          now we have to use add/sub in those cases. However, the value
12003          of that would be marginal, as both mov and add/sub are 32-bit
12004          in ARM mode, and it would require extra conditionals
12005          in arm_expand_prologue to distingish ARM-apcs-frame case
12006          (where frame pointer is required to point at first register)
12007          and ARM-non-apcs-frame. Therefore, such change is postponed
12008          until real need arise.  */
12009       unsigned HOST_WIDE_INT amount;
12010       int rfe;
12011       /* Restore stack pointer if necessary.  */
12012       if (TARGET_ARM && frame_pointer_needed)
12013         {
12014           operands[0] = stack_pointer_rtx;
12015           operands[1] = hard_frame_pointer_rtx;
12016           
12017           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
12018           output_add_immediate (operands);
12019         }
12020       else
12021         {
12022           if (frame_pointer_needed)
12023             {
12024               /* For Thumb-2 restore sp from the frame pointer.
12025                  Operand restrictions mean we have to incrememnt FP, then copy
12026                  to SP.  */
12027               amount = offsets->locals_base - offsets->saved_regs;
12028               operands[0] = hard_frame_pointer_rtx;
12029             }
12030           else
12031             {
12032               unsigned long count;
12033               operands[0] = stack_pointer_rtx;
12034               amount = offsets->outgoing_args - offsets->saved_regs;
12035               /* pop call clobbered registers if it avoids a
12036                  separate stack adjustment.  */
12037               count = offsets->saved_regs - offsets->saved_args;
12038               if (optimize_size
12039                   && count != 0
12040                   && !crtl->calls_eh_return
12041                   && bit_count(saved_regs_mask) * 4 == count
12042                   && !IS_INTERRUPT (func_type)
12043                   && !crtl->tail_call_emit)
12044                 {
12045                   unsigned long mask;
12046                   mask = (1 << (arm_size_return_regs() / 4)) - 1;
12047                   mask ^= 0xf;
12048                   mask &= ~saved_regs_mask;
12049                   reg = 0;
12050                   while (bit_count (mask) * 4 > amount)
12051                     {
12052                       while ((mask & (1 << reg)) == 0)
12053                         reg++;
12054                       mask &= ~(1 << reg);
12055                     }
12056                   if (bit_count (mask) * 4 == amount) {
12057                       amount = 0;
12058                       saved_regs_mask |= mask;
12059                   }
12060                 }
12061             }
12062           
12063           if (amount)
12064             {
12065               operands[1] = operands[0];
12066               operands[2] = GEN_INT (amount);
12067               output_add_immediate (operands);
12068             }
12069           if (frame_pointer_needed)
12070             asm_fprintf (f, "\tmov\t%r, %r\n",
12071                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
12072         }
12073
12074       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12075         {
12076           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
12077             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12078               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
12079                            reg, SP_REGNUM);
12080         }
12081       else
12082         {
12083           start_reg = FIRST_FPA_REGNUM;
12084
12085           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
12086             {
12087               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12088                 {
12089                   if (reg - start_reg == 3)
12090                     {
12091                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
12092                                    start_reg, SP_REGNUM);
12093                       start_reg = reg + 1;
12094                     }
12095                 }
12096               else
12097                 {
12098                   if (reg != start_reg)
12099                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
12100                                  start_reg, reg - start_reg,
12101                                  SP_REGNUM);
12102
12103                   start_reg = reg + 1;
12104                 }
12105             }
12106
12107           /* Just in case the last register checked also needs unstacking.  */
12108           if (reg != start_reg)
12109             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
12110                          start_reg, reg - start_reg, SP_REGNUM);
12111         }
12112
12113       if (TARGET_HARD_FLOAT && TARGET_VFP)
12114         {
12115           start_reg = FIRST_VFP_REGNUM;
12116           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12117             {
12118               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12119                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12120                 {
12121                   if (start_reg != reg)
12122                     vfp_output_fldmd (f, SP_REGNUM,
12123                                       (start_reg - FIRST_VFP_REGNUM) / 2,
12124                                       (reg - start_reg) / 2);
12125                   start_reg = reg + 2;
12126                 }
12127             }
12128           if (start_reg != reg)
12129             vfp_output_fldmd (f, SP_REGNUM,
12130                               (start_reg - FIRST_VFP_REGNUM) / 2,
12131                               (reg - start_reg) / 2);
12132         }
12133       if (TARGET_IWMMXT)
12134         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
12135           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12136             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
12137
12138       /* If we can, restore the LR into the PC.  */
12139       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
12140           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
12141           && !IS_STACKALIGN (func_type)
12142           && really_return
12143           && crtl->args.pretend_args_size == 0
12144           && saved_regs_mask & (1 << LR_REGNUM)
12145           && !crtl->calls_eh_return)
12146         {
12147           saved_regs_mask &= ~ (1 << LR_REGNUM);
12148           saved_regs_mask |=   (1 << PC_REGNUM);
12149           rfe = IS_INTERRUPT (func_type);
12150         }
12151       else
12152         rfe = 0;
12153
12154       /* Load the registers off the stack.  If we only have one register
12155          to load use the LDR instruction - it is faster.  For Thumb-2
12156          always use pop and the assembler will pick the best instruction.*/
12157       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
12158           && !IS_INTERRUPT(func_type))
12159         {
12160           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
12161         }
12162       else if (saved_regs_mask)
12163         {
12164           if (saved_regs_mask & (1 << SP_REGNUM))
12165             /* Note - write back to the stack register is not enabled
12166                (i.e. "ldmfd sp!...").  We know that the stack pointer is
12167                in the list of registers and if we add writeback the
12168                instruction becomes UNPREDICTABLE.  */
12169             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
12170                              rfe);
12171           else if (TARGET_ARM)
12172             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
12173                              rfe);
12174           else
12175             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
12176         }
12177
12178       if (crtl->args.pretend_args_size)
12179         {
12180           /* Unwind the pre-pushed regs.  */
12181           operands[0] = operands[1] = stack_pointer_rtx;
12182           operands[2] = GEN_INT (crtl->args.pretend_args_size);
12183           output_add_immediate (operands);
12184         }
12185     }
12186
12187   /* We may have already restored PC directly from the stack.  */
12188   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
12189     return "";
12190
12191   /* Stack adjustment for exception handler.  */
12192   if (crtl->calls_eh_return)
12193     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
12194                  ARM_EH_STACKADJ_REGNUM);
12195
12196   /* Generate the return instruction.  */
12197   switch ((int) ARM_FUNC_TYPE (func_type))
12198     {
12199     case ARM_FT_ISR:
12200     case ARM_FT_FIQ:
12201       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
12202       break;
12203
12204     case ARM_FT_EXCEPTION:
12205       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
12206       break;
12207
12208     case ARM_FT_INTERWORKED:
12209       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
12210       break;
12211
12212     default:
12213       if (IS_STACKALIGN (func_type))
12214         {
12215           /* See comment in arm_expand_prologue.  */
12216           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
12217         }
12218       if (arm_arch5 || arm_arch4t)
12219         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
12220       else
12221         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
12222       break;
12223     }
12224
12225   return "";
12226 }
12227
12228 static void
12229 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
12230                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
12231 {
12232   arm_stack_offsets *offsets;
12233
12234   if (TARGET_THUMB1)
12235     {
12236       int regno;
12237
12238       /* Emit any call-via-reg trampolines that are needed for v4t support
12239          of call_reg and call_value_reg type insns.  */
12240       for (regno = 0; regno < LR_REGNUM; regno++)
12241         {
12242           rtx label = cfun->machine->call_via[regno];
12243
12244           if (label != NULL)
12245             {
12246               switch_to_section (function_section (current_function_decl));
12247               targetm.asm_out.internal_label (asm_out_file, "L",
12248                                               CODE_LABEL_NUMBER (label));
12249               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
12250             }
12251         }
12252
12253       /* ??? Probably not safe to set this here, since it assumes that a
12254          function will be emitted as assembly immediately after we generate
12255          RTL for it.  This does not happen for inline functions.  */
12256       return_used_this_function = 0;
12257     }
12258   else /* TARGET_32BIT */
12259     {
12260       /* We need to take into account any stack-frame rounding.  */
12261       offsets = arm_get_frame_offsets ();
12262
12263       gcc_assert (!use_return_insn (FALSE, NULL)
12264                   || !return_used_this_function
12265                   || offsets->saved_regs == offsets->outgoing_args
12266                   || frame_pointer_needed);
12267
12268       /* Reset the ARM-specific per-function variables.  */
12269       after_arm_reorg = 0;
12270     }
12271 }
12272
12273 /* Generate and emit an insn that we will recognize as a push_multi.
12274    Unfortunately, since this insn does not reflect very well the actual
12275    semantics of the operation, we need to annotate the insn for the benefit
12276    of DWARF2 frame unwind information.  */
12277 static rtx
12278 emit_multi_reg_push (unsigned long mask)
12279 {
12280   int num_regs = 0;
12281   int num_dwarf_regs;
12282   int i, j;
12283   rtx par;
12284   rtx dwarf;
12285   int dwarf_par_index;
12286   rtx tmp, reg;
12287
12288   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12289     if (mask & (1 << i))
12290       num_regs++;
12291
12292   gcc_assert (num_regs && num_regs <= 16);
12293
12294   /* We don't record the PC in the dwarf frame information.  */
12295   num_dwarf_regs = num_regs;
12296   if (mask & (1 << PC_REGNUM))
12297     num_dwarf_regs--;
12298
12299   /* For the body of the insn we are going to generate an UNSPEC in
12300      parallel with several USEs.  This allows the insn to be recognized
12301      by the push_multi pattern in the arm.md file.  The insn looks
12302      something like this:
12303
12304        (parallel [
12305            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
12306                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
12307            (use (reg:SI 11 fp))
12308            (use (reg:SI 12 ip))
12309            (use (reg:SI 14 lr))
12310            (use (reg:SI 15 pc))
12311         ])
12312
12313      For the frame note however, we try to be more explicit and actually
12314      show each register being stored into the stack frame, plus a (single)
12315      decrement of the stack pointer.  We do it this way in order to be
12316      friendly to the stack unwinding code, which only wants to see a single
12317      stack decrement per instruction.  The RTL we generate for the note looks
12318      something like this:
12319
12320       (sequence [
12321            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
12322            (set (mem:SI (reg:SI sp)) (reg:SI r4))
12323            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
12324            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
12325            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
12326         ])
12327
12328       This sequence is used both by the code to support stack unwinding for
12329       exceptions handlers and the code to generate dwarf2 frame debugging.  */
12330
12331   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
12332   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
12333   dwarf_par_index = 1;
12334
12335   for (i = 0; i <= LAST_ARM_REGNUM; i++)
12336     {
12337       if (mask & (1 << i))
12338         {
12339           reg = gen_rtx_REG (SImode, i);
12340
12341           XVECEXP (par, 0, 0)
12342             = gen_rtx_SET (VOIDmode,
12343                            gen_frame_mem (BLKmode,
12344                                           gen_rtx_PRE_DEC (BLKmode,
12345                                                            stack_pointer_rtx)),
12346                            gen_rtx_UNSPEC (BLKmode,
12347                                            gen_rtvec (1, reg),
12348                                            UNSPEC_PUSH_MULT));
12349
12350           if (i != PC_REGNUM)
12351             {
12352               tmp = gen_rtx_SET (VOIDmode,
12353                                  gen_frame_mem (SImode, stack_pointer_rtx),
12354                                  reg);
12355               RTX_FRAME_RELATED_P (tmp) = 1;
12356               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
12357               dwarf_par_index++;
12358             }
12359
12360           break;
12361         }
12362     }
12363
12364   for (j = 1, i++; j < num_regs; i++)
12365     {
12366       if (mask & (1 << i))
12367         {
12368           reg = gen_rtx_REG (SImode, i);
12369
12370           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
12371
12372           if (i != PC_REGNUM)
12373             {
12374               tmp
12375                 = gen_rtx_SET (VOIDmode,
12376                                gen_frame_mem (SImode,
12377                                               plus_constant (stack_pointer_rtx,
12378                                                              4 * j)),
12379                                reg);
12380               RTX_FRAME_RELATED_P (tmp) = 1;
12381               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
12382             }
12383
12384           j++;
12385         }
12386     }
12387
12388   par = emit_insn (par);
12389
12390   tmp = gen_rtx_SET (VOIDmode,
12391                      stack_pointer_rtx,
12392                      plus_constant (stack_pointer_rtx, -4 * num_regs));
12393   RTX_FRAME_RELATED_P (tmp) = 1;
12394   XVECEXP (dwarf, 0, 0) = tmp;
12395
12396   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12397                                        REG_NOTES (par));
12398   return par;
12399 }
12400
12401 /* Calculate the size of the return value that is passed in registers.  */
12402 static unsigned
12403 arm_size_return_regs (void)
12404 {
12405   enum machine_mode mode;
12406
12407   if (crtl->return_rtx != 0)
12408     mode = GET_MODE (crtl->return_rtx);
12409   else
12410     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12411
12412   return GET_MODE_SIZE (mode);
12413 }
12414
12415 static rtx
12416 emit_sfm (int base_reg, int count)
12417 {
12418   rtx par;
12419   rtx dwarf;
12420   rtx tmp, reg;
12421   int i;
12422
12423   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12424   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12425
12426   reg = gen_rtx_REG (XFmode, base_reg++);
12427
12428   XVECEXP (par, 0, 0)
12429     = gen_rtx_SET (VOIDmode,
12430                    gen_frame_mem (BLKmode,
12431                                   gen_rtx_PRE_DEC (BLKmode,
12432                                                    stack_pointer_rtx)),
12433                    gen_rtx_UNSPEC (BLKmode,
12434                                    gen_rtvec (1, reg),
12435                                    UNSPEC_PUSH_MULT));
12436   tmp = gen_rtx_SET (VOIDmode,
12437                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
12438   RTX_FRAME_RELATED_P (tmp) = 1;
12439   XVECEXP (dwarf, 0, 1) = tmp;
12440
12441   for (i = 1; i < count; i++)
12442     {
12443       reg = gen_rtx_REG (XFmode, base_reg++);
12444       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12445
12446       tmp = gen_rtx_SET (VOIDmode,
12447                          gen_frame_mem (XFmode,
12448                                         plus_constant (stack_pointer_rtx,
12449                                                        i * 12)),
12450                          reg);
12451       RTX_FRAME_RELATED_P (tmp) = 1;
12452       XVECEXP (dwarf, 0, i + 1) = tmp;
12453     }
12454
12455   tmp = gen_rtx_SET (VOIDmode,
12456                      stack_pointer_rtx,
12457                      plus_constant (stack_pointer_rtx, -12 * count));
12458
12459   RTX_FRAME_RELATED_P (tmp) = 1;
12460   XVECEXP (dwarf, 0, 0) = tmp;
12461
12462   par = emit_insn (par);
12463   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12464                                        REG_NOTES (par));
12465   return par;
12466 }
12467
12468
12469 /* Return true if the current function needs to save/restore LR.  */
12470
12471 static bool
12472 thumb_force_lr_save (void)
12473 {
12474   return !cfun->machine->lr_save_eliminated
12475          && (!leaf_function_p ()
12476              || thumb_far_jump_used_p ()
12477              || df_regs_ever_live_p (LR_REGNUM));
12478 }
12479
12480
12481 /* Compute the distance from register FROM to register TO.
12482    These can be the arg pointer (26), the soft frame pointer (25),
12483    the stack pointer (13) or the hard frame pointer (11).
12484    In thumb mode r7 is used as the soft frame pointer, if needed.
12485    Typical stack layout looks like this:
12486
12487        old stack pointer -> |    |
12488                              ----
12489                             |    | \
12490                             |    |   saved arguments for
12491                             |    |   vararg functions
12492                             |    | /
12493                               --
12494    hard FP & arg pointer -> |    | \
12495                             |    |   stack
12496                             |    |   frame
12497                             |    | /
12498                               --
12499                             |    | \
12500                             |    |   call saved
12501                             |    |   registers
12502       soft frame pointer -> |    | /
12503                               --
12504                             |    | \
12505                             |    |   local
12506                             |    |   variables
12507      locals base pointer -> |    | /
12508                               --
12509                             |    | \
12510                             |    |   outgoing
12511                             |    |   arguments
12512    current stack pointer -> |    | /
12513                               --
12514
12515   For a given function some or all of these stack components
12516   may not be needed, giving rise to the possibility of
12517   eliminating some of the registers.
12518
12519   The values returned by this function must reflect the behavior
12520   of arm_expand_prologue() and arm_compute_save_reg_mask().
12521
12522   The sign of the number returned reflects the direction of stack
12523   growth, so the values are positive for all eliminations except
12524   from the soft frame pointer to the hard frame pointer.
12525
12526   SFP may point just inside the local variables block to ensure correct
12527   alignment.  */
12528
12529
12530 /* Calculate stack offsets.  These are used to calculate register elimination
12531    offsets and in prologue/epilogue code.  Also calculates which registers
12532    should be saved.  */
12533
12534 static arm_stack_offsets *
12535 arm_get_frame_offsets (void)
12536 {
12537   struct arm_stack_offsets *offsets;
12538   unsigned long func_type;
12539   int leaf;
12540   int saved;
12541   int core_saved;
12542   HOST_WIDE_INT frame_size;
12543   int i;
12544
12545   offsets = &cfun->machine->stack_offsets;
12546
12547   /* We need to know if we are a leaf function.  Unfortunately, it
12548      is possible to be called after start_sequence has been called,
12549      which causes get_insns to return the insns for the sequence,
12550      not the function, which will cause leaf_function_p to return
12551      the incorrect result.
12552
12553      to know about leaf functions once reload has completed, and the
12554      frame size cannot be changed after that time, so we can safely
12555      use the cached value.  */
12556
12557   if (reload_completed)
12558     return offsets;
12559
12560   /* Initially this is the size of the local variables.  It will translated
12561      into an offset once we have determined the size of preceding data.  */
12562   frame_size = ROUND_UP_WORD (get_frame_size ());
12563
12564   leaf = leaf_function_p ();
12565
12566   /* Space for variadic functions.  */
12567   offsets->saved_args = crtl->args.pretend_args_size;
12568
12569   /* In Thumb mode this is incorrect, but never used.  */
12570   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
12571                    arm_compute_static_chain_stack_bytes();
12572
12573   if (TARGET_32BIT)
12574     {
12575       unsigned int regno;
12576
12577       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
12578       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12579       saved = core_saved;
12580
12581       /* We know that SP will be doubleword aligned on entry, and we must
12582          preserve that condition at any subroutine call.  We also require the
12583          soft frame pointer to be doubleword aligned.  */
12584
12585       if (TARGET_REALLY_IWMMXT)
12586         {
12587           /* Check for the call-saved iWMMXt registers.  */
12588           for (regno = FIRST_IWMMXT_REGNUM;
12589                regno <= LAST_IWMMXT_REGNUM;
12590                regno++)
12591             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12592               saved += 8;
12593         }
12594
12595       func_type = arm_current_func_type ();
12596       if (! IS_VOLATILE (func_type))
12597         {
12598           /* Space for saved FPA registers.  */
12599           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
12600             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12601             saved += 12;
12602
12603           /* Space for saved VFP registers.  */
12604           if (TARGET_HARD_FLOAT && TARGET_VFP)
12605             saved += arm_get_vfp_saved_size ();
12606         }
12607     }
12608   else /* TARGET_THUMB1 */
12609     {
12610       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
12611       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12612       saved = core_saved;
12613       if (TARGET_BACKTRACE)
12614         saved += 16;
12615     }
12616
12617   /* Saved registers include the stack frame.  */
12618   offsets->saved_regs = offsets->saved_args + saved +
12619                         arm_compute_static_chain_stack_bytes();
12620   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
12621   /* A leaf function does not need any stack alignment if it has nothing
12622      on the stack.  */
12623   if (leaf && frame_size == 0)
12624     {
12625       offsets->outgoing_args = offsets->soft_frame;
12626       offsets->locals_base = offsets->soft_frame;
12627       return offsets;
12628     }
12629
12630   /* Ensure SFP has the correct alignment.  */
12631   if (ARM_DOUBLEWORD_ALIGN
12632       && (offsets->soft_frame & 7))
12633     {
12634       offsets->soft_frame += 4;
12635       /* Try to align stack by pushing an extra reg.  Don't bother doing this
12636          when there is a stack frame as the alignment will be rolled into
12637          the normal stack adjustment.  */
12638       if (frame_size + crtl->outgoing_args_size == 0)
12639         {
12640           int reg = -1;
12641
12642           for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
12643             {
12644               if ((offsets->saved_regs_mask & (1 << i)) == 0)
12645                 {
12646                   reg = i;
12647                   break;
12648                 }
12649             }
12650
12651           if (reg == -1 && arm_size_return_regs () <= 12
12652               && !crtl->tail_call_emit)
12653             {
12654               /* Push/pop an argument register (r3) if all callee saved
12655                  registers are already being pushed.  */
12656               reg = 3;
12657             }
12658
12659           if (reg != -1)
12660             {
12661               offsets->saved_regs += 4;
12662               offsets->saved_regs_mask |= (1 << reg);
12663             }
12664         }
12665     }
12666
12667   offsets->locals_base = offsets->soft_frame + frame_size;
12668   offsets->outgoing_args = (offsets->locals_base
12669                             + crtl->outgoing_args_size);
12670
12671   if (ARM_DOUBLEWORD_ALIGN)
12672     {
12673       /* Ensure SP remains doubleword aligned.  */
12674       if (offsets->outgoing_args & 7)
12675         offsets->outgoing_args += 4;
12676       gcc_assert (!(offsets->outgoing_args & 7));
12677     }
12678
12679   return offsets;
12680 }
12681
12682
12683 /* Calculate the relative offsets for the different stack pointers.  Positive
12684    offsets are in the direction of stack growth.  */
12685
12686 HOST_WIDE_INT
12687 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12688 {
12689   arm_stack_offsets *offsets;
12690
12691   offsets = arm_get_frame_offsets ();
12692
12693   /* OK, now we have enough information to compute the distances.
12694      There must be an entry in these switch tables for each pair
12695      of registers in ELIMINABLE_REGS, even if some of the entries
12696      seem to be redundant or useless.  */
12697   switch (from)
12698     {
12699     case ARG_POINTER_REGNUM:
12700       switch (to)
12701         {
12702         case THUMB_HARD_FRAME_POINTER_REGNUM:
12703           return 0;
12704
12705         case FRAME_POINTER_REGNUM:
12706           /* This is the reverse of the soft frame pointer
12707              to hard frame pointer elimination below.  */
12708           return offsets->soft_frame - offsets->saved_args;
12709
12710         case ARM_HARD_FRAME_POINTER_REGNUM:
12711           /* This is only non-zero in the case where the static chain register
12712              is stored above the frame.  */
12713           return offsets->frame - offsets->saved_args - 4;
12714
12715         case STACK_POINTER_REGNUM:
12716           /* If nothing has been pushed on the stack at all
12717              then this will return -4.  This *is* correct!  */
12718           return offsets->outgoing_args - (offsets->saved_args + 4);
12719
12720         default:
12721           gcc_unreachable ();
12722         }
12723       gcc_unreachable ();
12724
12725     case FRAME_POINTER_REGNUM:
12726       switch (to)
12727         {
12728         case THUMB_HARD_FRAME_POINTER_REGNUM:
12729           return 0;
12730
12731         case ARM_HARD_FRAME_POINTER_REGNUM:
12732           /* The hard frame pointer points to the top entry in the
12733              stack frame.  The soft frame pointer to the bottom entry
12734              in the stack frame.  If there is no stack frame at all,
12735              then they are identical.  */
12736
12737           return offsets->frame - offsets->soft_frame;
12738
12739         case STACK_POINTER_REGNUM:
12740           return offsets->outgoing_args - offsets->soft_frame;
12741
12742         default:
12743           gcc_unreachable ();
12744         }
12745       gcc_unreachable ();
12746
12747     default:
12748       /* You cannot eliminate from the stack pointer.
12749          In theory you could eliminate from the hard frame
12750          pointer to the stack pointer, but this will never
12751          happen, since if a stack frame is not needed the
12752          hard frame pointer will never be used.  */
12753       gcc_unreachable ();
12754     }
12755 }
12756
12757
12758 /* Emit RTL to save coprocessor registers on function entry.  Returns the
12759    number of bytes pushed.  */
12760
12761 static int
12762 arm_save_coproc_regs(void)
12763 {
12764   int saved_size = 0;
12765   unsigned reg;
12766   unsigned start_reg;
12767   rtx insn;
12768
12769   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
12770     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
12771       {
12772         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
12773         insn = gen_rtx_MEM (V2SImode, insn);
12774         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
12775         RTX_FRAME_RELATED_P (insn) = 1;
12776         saved_size += 8;
12777       }
12778
12779   /* Save any floating point call-saved registers used by this
12780      function.  */
12781   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12782     {
12783       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12784         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12785           {
12786             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
12787             insn = gen_rtx_MEM (XFmode, insn);
12788             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
12789             RTX_FRAME_RELATED_P (insn) = 1;
12790             saved_size += 12;
12791           }
12792     }
12793   else
12794     {
12795       start_reg = LAST_FPA_REGNUM;
12796
12797       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12798         {
12799           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12800             {
12801               if (start_reg - reg == 3)
12802                 {
12803                   insn = emit_sfm (reg, 4);
12804                   RTX_FRAME_RELATED_P (insn) = 1;
12805                   saved_size += 48;
12806                   start_reg = reg - 1;
12807                 }
12808             }
12809           else
12810             {
12811               if (start_reg != reg)
12812                 {
12813                   insn = emit_sfm (reg + 1, start_reg - reg);
12814                   RTX_FRAME_RELATED_P (insn) = 1;
12815                   saved_size += (start_reg - reg) * 12;
12816                 }
12817               start_reg = reg - 1;
12818             }
12819         }
12820
12821       if (start_reg != reg)
12822         {
12823           insn = emit_sfm (reg + 1, start_reg - reg);
12824           saved_size += (start_reg - reg) * 12;
12825           RTX_FRAME_RELATED_P (insn) = 1;
12826         }
12827     }
12828   if (TARGET_HARD_FLOAT && TARGET_VFP)
12829     {
12830       start_reg = FIRST_VFP_REGNUM;
12831
12832       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12833         {
12834           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12835               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12836             {
12837               if (start_reg != reg)
12838                 saved_size += vfp_emit_fstmd (start_reg,
12839                                               (reg - start_reg) / 2);
12840               start_reg = reg + 2;
12841             }
12842         }
12843       if (start_reg != reg)
12844         saved_size += vfp_emit_fstmd (start_reg,
12845                                       (reg - start_reg) / 2);
12846     }
12847   return saved_size;
12848 }
12849
12850
12851 /* Set the Thumb frame pointer from the stack pointer.  */
12852
12853 static void
12854 thumb_set_frame_pointer (arm_stack_offsets *offsets)
12855 {
12856   HOST_WIDE_INT amount;
12857   rtx insn, dwarf;
12858
12859   amount = offsets->outgoing_args - offsets->locals_base;
12860   if (amount < 1024)
12861     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12862                                   stack_pointer_rtx, GEN_INT (amount)));
12863   else
12864     {
12865       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
12866       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
12867          expects the first two operands to be the same.  */
12868       if (TARGET_THUMB2)
12869         {
12870           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12871                                         stack_pointer_rtx,
12872                                         hard_frame_pointer_rtx));
12873         }
12874       else
12875         {
12876           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12877                                         hard_frame_pointer_rtx,
12878                                         stack_pointer_rtx));
12879         }
12880       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
12881                            plus_constant (stack_pointer_rtx, amount));
12882       RTX_FRAME_RELATED_P (dwarf) = 1;
12883       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12884                                             REG_NOTES (insn));
12885     }
12886
12887   RTX_FRAME_RELATED_P (insn) = 1;
12888 }
12889
12890 /* Generate the prologue instructions for entry into an ARM or Thumb-2
12891    function.  */
12892 void
12893 arm_expand_prologue (void)
12894 {
12895   rtx amount;
12896   rtx insn;
12897   rtx ip_rtx;
12898   unsigned long live_regs_mask;
12899   unsigned long func_type;
12900   int fp_offset = 0;
12901   int saved_pretend_args = 0;
12902   int saved_regs = 0;
12903   unsigned HOST_WIDE_INT args_to_push;
12904   arm_stack_offsets *offsets;
12905
12906   func_type = arm_current_func_type ();
12907
12908   /* Naked functions don't have prologues.  */
12909   if (IS_NAKED (func_type))
12910     return;
12911
12912   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
12913   args_to_push = crtl->args.pretend_args_size;
12914
12915   /* Compute which register we will have to save onto the stack.  */
12916   offsets = arm_get_frame_offsets ();
12917   live_regs_mask = offsets->saved_regs_mask;
12918
12919   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
12920
12921   if (IS_STACKALIGN (func_type))
12922     {
12923       rtx dwarf;
12924       rtx r0;
12925       rtx r1;
12926       /* Handle a word-aligned stack pointer.  We generate the following:
12927
12928           mov r0, sp
12929           bic r1, r0, #7
12930           mov sp, r1
12931           <save and restore r0 in normal prologue/epilogue>
12932           mov sp, r0
12933           bx lr
12934
12935          The unwinder doesn't need to know about the stack realignment.
12936          Just tell it we saved SP in r0.  */
12937       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
12938
12939       r0 = gen_rtx_REG (SImode, 0);
12940       r1 = gen_rtx_REG (SImode, 1);
12941       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
12942          compiler won't choke.  */
12943       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
12944       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
12945       insn = gen_movsi (r0, stack_pointer_rtx);
12946       RTX_FRAME_RELATED_P (insn) = 1;
12947       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12948                                             dwarf, REG_NOTES (insn));
12949       emit_insn (insn);
12950       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
12951       emit_insn (gen_movsi (stack_pointer_rtx, r1));
12952     }
12953
12954   /* For APCS frames, if IP register is clobbered
12955      when creating frame, save that register in a special
12956      way.  */
12957   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
12958     {
12959       if (IS_INTERRUPT (func_type))
12960         {
12961           /* Interrupt functions must not corrupt any registers.
12962              Creating a frame pointer however, corrupts the IP
12963              register, so we must push it first.  */
12964           insn = emit_multi_reg_push (1 << IP_REGNUM);
12965
12966           /* Do not set RTX_FRAME_RELATED_P on this insn.
12967              The dwarf stack unwinding code only wants to see one
12968              stack decrement per function, and this is not it.  If
12969              this instruction is labeled as being part of the frame
12970              creation sequence then dwarf2out_frame_debug_expr will
12971              die when it encounters the assignment of IP to FP
12972              later on, since the use of SP here establishes SP as
12973              the CFA register and not IP.
12974
12975              Anyway this instruction is not really part of the stack
12976              frame creation although it is part of the prologue.  */
12977         }
12978       else if (IS_NESTED (func_type))
12979         {
12980           /* The Static chain register is the same as the IP register
12981              used as a scratch register during stack frame creation.
12982              To get around this need to find somewhere to store IP
12983              whilst the frame is being created.  We try the following
12984              places in order:
12985
12986                1. The last argument register.
12987                2. A slot on the stack above the frame.  (This only
12988                   works if the function is not a varargs function).
12989                3. Register r3, after pushing the argument registers
12990                   onto the stack.
12991
12992              Note - we only need to tell the dwarf2 backend about the SP
12993              adjustment in the second variant; the static chain register
12994              doesn't need to be unwound, as it doesn't contain a value
12995              inherited from the caller.  */
12996
12997           if (df_regs_ever_live_p (3) == false)
12998             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
12999           else if (args_to_push == 0)
13000             {
13001               rtx dwarf;
13002
13003               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
13004               saved_regs += 4;
13005
13006               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
13007               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
13008               fp_offset = 4;
13009
13010               /* Just tell the dwarf backend that we adjusted SP.  */
13011               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13012                                    plus_constant (stack_pointer_rtx,
13013                                                   -fp_offset));
13014               RTX_FRAME_RELATED_P (insn) = 1;
13015               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
13016                                                     dwarf, REG_NOTES (insn));
13017             }
13018           else
13019             {
13020               /* Store the args on the stack.  */
13021               if (cfun->machine->uses_anonymous_args)
13022                 insn = emit_multi_reg_push
13023                   ((0xf0 >> (args_to_push / 4)) & 0xf);
13024               else
13025                 insn = emit_insn
13026                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13027                                GEN_INT (- args_to_push)));
13028
13029               RTX_FRAME_RELATED_P (insn) = 1;
13030
13031               saved_pretend_args = 1;
13032               fp_offset = args_to_push;
13033               args_to_push = 0;
13034
13035               /* Now reuse r3 to preserve IP.  */
13036               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
13037             }
13038         }
13039
13040       insn = emit_set_insn (ip_rtx,
13041                             plus_constant (stack_pointer_rtx, fp_offset));
13042       RTX_FRAME_RELATED_P (insn) = 1;
13043     }
13044
13045   if (args_to_push)
13046     {
13047       /* Push the argument registers, or reserve space for them.  */
13048       if (cfun->machine->uses_anonymous_args)
13049         insn = emit_multi_reg_push
13050           ((0xf0 >> (args_to_push / 4)) & 0xf);
13051       else
13052         insn = emit_insn
13053           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13054                        GEN_INT (- args_to_push)));
13055       RTX_FRAME_RELATED_P (insn) = 1;
13056     }
13057
13058   /* If this is an interrupt service routine, and the link register
13059      is going to be pushed, and we're not generating extra
13060      push of IP (needed when frame is needed and frame layout if apcs),
13061      subtracting four from LR now will mean that the function return
13062      can be done with a single instruction.  */
13063   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
13064       && (live_regs_mask & (1 << LR_REGNUM)) != 0
13065       && !(frame_pointer_needed && TARGET_APCS_FRAME)
13066       && TARGET_ARM)
13067     {
13068       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
13069       
13070       emit_set_insn (lr, plus_constant (lr, -4));
13071     }
13072
13073   if (live_regs_mask)
13074     {
13075       saved_regs += bit_count (live_regs_mask) * 4;
13076       if (optimize_size && !frame_pointer_needed
13077           && saved_regs == offsets->saved_regs - offsets->saved_args)
13078         {
13079           /* If no coprocessor registers are being pushed and we don't have
13080              to worry about a frame pointer then push extra registers to
13081              create the stack frame.  This is done is a way that does not
13082              alter the frame layout, so is independent of the epilogue.  */
13083           int n;
13084           int frame;
13085           n = 0;
13086           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
13087             n++;
13088           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
13089           if (frame && n * 4 >= frame)
13090             {
13091               n = frame / 4;
13092               live_regs_mask |= (1 << n) - 1;
13093               saved_regs += frame;
13094             }
13095         }
13096       insn = emit_multi_reg_push (live_regs_mask);
13097       RTX_FRAME_RELATED_P (insn) = 1;
13098     }
13099
13100   if (! IS_VOLATILE (func_type))
13101     saved_regs += arm_save_coproc_regs ();
13102
13103   if (frame_pointer_needed && TARGET_ARM)
13104     {
13105       /* Create the new frame pointer.  */
13106       if (TARGET_APCS_FRAME)
13107         {
13108           insn = GEN_INT (-(4 + args_to_push + fp_offset));
13109           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
13110           RTX_FRAME_RELATED_P (insn) = 1;
13111
13112           if (IS_NESTED (func_type))
13113             {
13114               /* Recover the static chain register.  */
13115               if (!df_regs_ever_live_p (3)
13116                   || saved_pretend_args)
13117                 insn = gen_rtx_REG (SImode, 3);
13118               else /* if (crtl->args.pretend_args_size == 0) */
13119                 {
13120                   insn = plus_constant (hard_frame_pointer_rtx, 4);
13121                   insn = gen_frame_mem (SImode, insn);
13122                 }
13123               emit_set_insn (ip_rtx, insn);
13124               /* Add a USE to stop propagate_one_insn() from barfing.  */
13125               emit_insn (gen_prologue_use (ip_rtx));
13126             }
13127         }
13128       else
13129         {
13130           insn = GEN_INT (saved_regs - 4);
13131           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13132                                         stack_pointer_rtx, insn));
13133           RTX_FRAME_RELATED_P (insn) = 1;
13134         }
13135     }
13136
13137   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
13138     {
13139       /* This add can produce multiple insns for a large constant, so we
13140          need to get tricky.  */
13141       rtx last = get_last_insn ();
13142
13143       amount = GEN_INT (offsets->saved_args + saved_regs
13144                         - offsets->outgoing_args);
13145
13146       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13147                                     amount));
13148       do
13149         {
13150           last = last ? NEXT_INSN (last) : get_insns ();
13151           RTX_FRAME_RELATED_P (last) = 1;
13152         }
13153       while (last != insn);
13154
13155       /* If the frame pointer is needed, emit a special barrier that
13156          will prevent the scheduler from moving stores to the frame
13157          before the stack adjustment.  */
13158       if (frame_pointer_needed)
13159         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
13160                                          hard_frame_pointer_rtx));
13161     }
13162
13163
13164   if (frame_pointer_needed && TARGET_THUMB2)
13165     thumb_set_frame_pointer (offsets);
13166
13167   if (flag_pic && arm_pic_register != INVALID_REGNUM)
13168     {
13169       unsigned long mask;
13170
13171       mask = live_regs_mask;
13172       mask &= THUMB2_WORK_REGS;
13173       if (!IS_NESTED (func_type))
13174         mask |= (1 << IP_REGNUM);
13175       arm_load_pic_register (mask);
13176     }
13177
13178   /* If we are profiling, make sure no instructions are scheduled before
13179      the call to mcount.  Similarly if the user has requested no
13180      scheduling in the prolog.  Similarly if we want non-call exceptions
13181      using the EABI unwinder, to prevent faulting instructions from being
13182      swapped with a stack adjustment.  */
13183   if (crtl->profile || !TARGET_SCHED_PROLOG
13184       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13185     emit_insn (gen_blockage ());
13186
13187   /* If the link register is being kept alive, with the return address in it,
13188      then make sure that it does not get reused by the ce2 pass.  */
13189   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
13190     cfun->machine->lr_save_eliminated = 1;
13191 }
13192 \f
13193 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
13194 static void
13195 arm_print_condition (FILE *stream)
13196 {
13197   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
13198     {
13199       /* Branch conversion is not implemented for Thumb-2.  */
13200       if (TARGET_THUMB)
13201         {
13202           output_operand_lossage ("predicated Thumb instruction");
13203           return;
13204         }
13205       if (current_insn_predicate != NULL)
13206         {
13207           output_operand_lossage
13208             ("predicated instruction in conditional sequence");
13209           return;
13210         }
13211
13212       fputs (arm_condition_codes[arm_current_cc], stream);
13213     }
13214   else if (current_insn_predicate)
13215     {
13216       enum arm_cond_code code;
13217
13218       if (TARGET_THUMB1)
13219         {
13220           output_operand_lossage ("predicated Thumb instruction");
13221           return;
13222         }
13223
13224       code = get_arm_condition_code (current_insn_predicate);
13225       fputs (arm_condition_codes[code], stream);
13226     }
13227 }
13228
13229
13230 /* If CODE is 'd', then the X is a condition operand and the instruction
13231    should only be executed if the condition is true.
13232    if CODE is 'D', then the X is a condition operand and the instruction
13233    should only be executed if the condition is false: however, if the mode
13234    of the comparison is CCFPEmode, then always execute the instruction -- we
13235    do this because in these circumstances !GE does not necessarily imply LT;
13236    in these cases the instruction pattern will take care to make sure that
13237    an instruction containing %d will follow, thereby undoing the effects of
13238    doing this instruction unconditionally.
13239    If CODE is 'N' then X is a floating point operand that must be negated
13240    before output.
13241    If CODE is 'B' then output a bitwise inverted value of X (a const int).
13242    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
13243 void
13244 arm_print_operand (FILE *stream, rtx x, int code)
13245 {
13246   switch (code)
13247     {
13248     case '@':
13249       fputs (ASM_COMMENT_START, stream);
13250       return;
13251
13252     case '_':
13253       fputs (user_label_prefix, stream);
13254       return;
13255
13256     case '|':
13257       fputs (REGISTER_PREFIX, stream);
13258       return;
13259
13260     case '?':
13261       arm_print_condition (stream);
13262       return;
13263
13264     case '(':
13265       /* Nothing in unified syntax, otherwise the current condition code.  */
13266       if (!TARGET_UNIFIED_ASM)
13267         arm_print_condition (stream);
13268       break;
13269
13270     case ')':
13271       /* The current condition code in unified syntax, otherwise nothing.  */
13272       if (TARGET_UNIFIED_ASM)
13273         arm_print_condition (stream);
13274       break;
13275   
13276     case '.':
13277       /* The current condition code for a condition code setting instruction.
13278          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
13279       if (TARGET_UNIFIED_ASM)
13280         {
13281           fputc('s', stream);
13282           arm_print_condition (stream);
13283         }
13284       else
13285         {
13286           arm_print_condition (stream);
13287           fputc('s', stream);
13288         }
13289       return;
13290
13291     case '!':
13292       /* If the instruction is conditionally executed then print
13293          the current condition code, otherwise print 's'.  */
13294       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
13295       if (current_insn_predicate)
13296         arm_print_condition (stream);
13297       else
13298         fputc('s', stream);
13299       break;
13300
13301     /* %# is a "break" sequence. It doesn't output anything, but is used to
13302        separate e.g. operand numbers from following text, if that text consists
13303        of further digits which we don't want to be part of the operand
13304        number.  */
13305     case '#':
13306       return;
13307
13308     case 'N':
13309       {
13310         REAL_VALUE_TYPE r;
13311         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
13312         r = REAL_VALUE_NEGATE (r);
13313         fprintf (stream, "%s", fp_const_from_val (&r));
13314       }
13315       return;
13316
13317     /* An integer or symbol address without a preceding # sign.  */
13318     case 'c':
13319       switch (GET_CODE (x))
13320         {
13321         case CONST_INT:
13322           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13323           break;
13324
13325         case SYMBOL_REF:
13326           output_addr_const (stream, x);
13327           break;
13328
13329         default:
13330           gcc_unreachable ();
13331         }
13332       return;
13333
13334     case 'B':
13335       if (GET_CODE (x) == CONST_INT)
13336         {
13337           HOST_WIDE_INT val;
13338           val = ARM_SIGN_EXTEND (~INTVAL (x));
13339           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
13340         }
13341       else
13342         {
13343           putc ('~', stream);
13344           output_addr_const (stream, x);
13345         }
13346       return;
13347
13348     case 'L':
13349       /* The low 16 bits of an immediate constant.  */
13350       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
13351       return;
13352
13353     case 'i':
13354       fprintf (stream, "%s", arithmetic_instr (x, 1));
13355       return;
13356
13357     /* Truncate Cirrus shift counts.  */
13358     case 's':
13359       if (GET_CODE (x) == CONST_INT)
13360         {
13361           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
13362           return;
13363         }
13364       arm_print_operand (stream, x, 0);
13365       return;
13366
13367     case 'I':
13368       fprintf (stream, "%s", arithmetic_instr (x, 0));
13369       return;
13370
13371     case 'S':
13372       {
13373         HOST_WIDE_INT val;
13374         const char *shift;
13375
13376         if (!shift_operator (x, SImode))
13377           {
13378             output_operand_lossage ("invalid shift operand");
13379             break;
13380           }
13381
13382         shift = shift_op (x, &val);
13383
13384         if (shift)
13385           {
13386             fprintf (stream, ", %s ", shift);
13387             if (val == -1)
13388               arm_print_operand (stream, XEXP (x, 1), 0);
13389             else
13390               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
13391           }
13392       }
13393       return;
13394
13395       /* An explanation of the 'Q', 'R' and 'H' register operands:
13396
13397          In a pair of registers containing a DI or DF value the 'Q'
13398          operand returns the register number of the register containing
13399          the least significant part of the value.  The 'R' operand returns
13400          the register number of the register containing the most
13401          significant part of the value.
13402
13403          The 'H' operand returns the higher of the two register numbers.
13404          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
13405          same as the 'Q' operand, since the most significant part of the
13406          value is held in the lower number register.  The reverse is true
13407          on systems where WORDS_BIG_ENDIAN is false.
13408
13409          The purpose of these operands is to distinguish between cases
13410          where the endian-ness of the values is important (for example
13411          when they are added together), and cases where the endian-ness
13412          is irrelevant, but the order of register operations is important.
13413          For example when loading a value from memory into a register
13414          pair, the endian-ness does not matter.  Provided that the value
13415          from the lower memory address is put into the lower numbered
13416          register, and the value from the higher address is put into the
13417          higher numbered register, the load will work regardless of whether
13418          the value being loaded is big-wordian or little-wordian.  The
13419          order of the two register loads can matter however, if the address
13420          of the memory location is actually held in one of the registers
13421          being overwritten by the load.  */
13422     case 'Q':
13423       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13424         {
13425           output_operand_lossage ("invalid operand for code '%c'", code);
13426           return;
13427         }
13428
13429       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
13430       return;
13431
13432     case 'R':
13433       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13434         {
13435           output_operand_lossage ("invalid operand for code '%c'", code);
13436           return;
13437         }
13438
13439       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
13440       return;
13441
13442     case 'H':
13443       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13444         {
13445           output_operand_lossage ("invalid operand for code '%c'", code);
13446           return;
13447         }
13448
13449       asm_fprintf (stream, "%r", REGNO (x) + 1);
13450       return;
13451
13452     case 'J':
13453       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13454         {
13455           output_operand_lossage ("invalid operand for code '%c'", code);
13456           return;
13457         }
13458
13459       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
13460       return;
13461
13462     case 'K':
13463       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13464         {
13465           output_operand_lossage ("invalid operand for code '%c'", code);
13466           return;
13467         }
13468
13469       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
13470       return;
13471
13472     case 'm':
13473       asm_fprintf (stream, "%r",
13474                    GET_CODE (XEXP (x, 0)) == REG
13475                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
13476       return;
13477
13478     case 'M':
13479       asm_fprintf (stream, "{%r-%r}",
13480                    REGNO (x),
13481                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
13482       return;
13483
13484     /* Like 'M', but writing doubleword vector registers, for use by Neon
13485        insns.  */
13486     case 'h':
13487       {
13488         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
13489         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
13490         if (numregs == 1)
13491           asm_fprintf (stream, "{d%d}", regno);
13492         else
13493           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
13494       }
13495       return;
13496
13497     case 'd':
13498       /* CONST_TRUE_RTX means always -- that's the default.  */
13499       if (x == const_true_rtx)
13500         return;
13501
13502       if (!COMPARISON_P (x))
13503         {
13504           output_operand_lossage ("invalid operand for code '%c'", code);
13505           return;
13506         }
13507
13508       fputs (arm_condition_codes[get_arm_condition_code (x)],
13509              stream);
13510       return;
13511
13512     case 'D':
13513       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
13514          want to do that.  */
13515       if (x == const_true_rtx)
13516         {
13517           output_operand_lossage ("instruction never executed");
13518           return;
13519         }
13520       if (!COMPARISON_P (x))
13521         {
13522           output_operand_lossage ("invalid operand for code '%c'", code);
13523           return;
13524         }
13525
13526       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
13527                                  (get_arm_condition_code (x))],
13528              stream);
13529       return;
13530
13531     /* Cirrus registers can be accessed in a variety of ways:
13532          single floating point (f)
13533          double floating point (d)
13534          32bit integer         (fx)
13535          64bit integer         (dx).  */
13536     case 'W':                   /* Cirrus register in F mode.  */
13537     case 'X':                   /* Cirrus register in D mode.  */
13538     case 'Y':                   /* Cirrus register in FX mode.  */
13539     case 'Z':                   /* Cirrus register in DX mode.  */
13540       gcc_assert (GET_CODE (x) == REG
13541                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
13542
13543       fprintf (stream, "mv%s%s",
13544                code == 'W' ? "f"
13545                : code == 'X' ? "d"
13546                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
13547
13548       return;
13549
13550     /* Print cirrus register in the mode specified by the register's mode.  */
13551     case 'V':
13552       {
13553         int mode = GET_MODE (x);
13554
13555         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
13556           {
13557             output_operand_lossage ("invalid operand for code '%c'", code);
13558             return;
13559           }
13560
13561         fprintf (stream, "mv%s%s",
13562                  mode == DFmode ? "d"
13563                  : mode == SImode ? "fx"
13564                  : mode == DImode ? "dx"
13565                  : "f", reg_names[REGNO (x)] + 2);
13566
13567         return;
13568       }
13569
13570     case 'U':
13571       if (GET_CODE (x) != REG
13572           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
13573           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
13574         /* Bad value for wCG register number.  */
13575         {
13576           output_operand_lossage ("invalid operand for code '%c'", code);
13577           return;
13578         }
13579
13580       else
13581         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
13582       return;
13583
13584       /* Print an iWMMXt control register name.  */
13585     case 'w':
13586       if (GET_CODE (x) != CONST_INT
13587           || INTVAL (x) < 0
13588           || INTVAL (x) >= 16)
13589         /* Bad value for wC register number.  */
13590         {
13591           output_operand_lossage ("invalid operand for code '%c'", code);
13592           return;
13593         }
13594
13595       else
13596         {
13597           static const char * wc_reg_names [16] =
13598             {
13599               "wCID",  "wCon",  "wCSSF", "wCASF",
13600               "wC4",   "wC5",   "wC6",   "wC7",
13601               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
13602               "wC12",  "wC13",  "wC14",  "wC15"
13603             };
13604
13605           fprintf (stream, wc_reg_names [INTVAL (x)]);
13606         }
13607       return;
13608
13609     /* Print a VFP/Neon double precision or quad precision register name.  */
13610     case 'P':
13611     case 'q':
13612       {
13613         int mode = GET_MODE (x);
13614         int is_quad = (code == 'q');
13615         int regno;
13616
13617         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
13618           {
13619             output_operand_lossage ("invalid operand for code '%c'", code);
13620             return;
13621           }
13622
13623         if (GET_CODE (x) != REG
13624             || !IS_VFP_REGNUM (REGNO (x)))
13625           {
13626             output_operand_lossage ("invalid operand for code '%c'", code);
13627             return;
13628           }
13629
13630         regno = REGNO (x);
13631         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
13632             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
13633           {
13634             output_operand_lossage ("invalid operand for code '%c'", code);
13635             return;
13636           }
13637
13638         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
13639           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
13640       }
13641       return;
13642
13643     /* These two codes print the low/high doubleword register of a Neon quad
13644        register, respectively.  For pair-structure types, can also print
13645        low/high quadword registers.  */
13646     case 'e':
13647     case 'f':
13648       {
13649         int mode = GET_MODE (x);
13650         int regno;
13651
13652         if ((GET_MODE_SIZE (mode) != 16
13653              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
13654           {
13655             output_operand_lossage ("invalid operand for code '%c'", code);
13656             return;
13657           }
13658
13659         regno = REGNO (x);
13660         if (!NEON_REGNO_OK_FOR_QUAD (regno))
13661           {
13662             output_operand_lossage ("invalid operand for code '%c'", code);
13663             return;
13664           }
13665
13666         if (GET_MODE_SIZE (mode) == 16)
13667           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
13668                                   + (code == 'f' ? 1 : 0));
13669         else
13670           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
13671                                   + (code == 'f' ? 1 : 0));
13672       }
13673       return;
13674
13675     /* Print a VFPv3 floating-point constant, represented as an integer
13676        index.  */
13677     case 'G':
13678       {
13679         int index = vfp3_const_double_index (x);
13680         gcc_assert (index != -1);
13681         fprintf (stream, "%d", index);
13682       }
13683       return;
13684
13685     /* Print bits representing opcode features for Neon.
13686
13687        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
13688        and polynomials as unsigned.
13689
13690        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
13691
13692        Bit 2 is 1 for rounding functions, 0 otherwise.  */
13693
13694     /* Identify the type as 's', 'u', 'p' or 'f'.  */
13695     case 'T':
13696       {
13697         HOST_WIDE_INT bits = INTVAL (x);
13698         fputc ("uspf"[bits & 3], stream);
13699       }
13700       return;
13701
13702     /* Likewise, but signed and unsigned integers are both 'i'.  */
13703     case 'F':
13704       {
13705         HOST_WIDE_INT bits = INTVAL (x);
13706         fputc ("iipf"[bits & 3], stream);
13707       }
13708       return;
13709
13710     /* As for 'T', but emit 'u' instead of 'p'.  */
13711     case 't':
13712       {
13713         HOST_WIDE_INT bits = INTVAL (x);
13714         fputc ("usuf"[bits & 3], stream);
13715       }
13716       return;
13717
13718     /* Bit 2: rounding (vs none).  */
13719     case 'O':
13720       {
13721         HOST_WIDE_INT bits = INTVAL (x);
13722         fputs ((bits & 4) != 0 ? "r" : "", stream);
13723       }
13724       return;
13725
13726     default:
13727       if (x == 0)
13728         {
13729           output_operand_lossage ("missing operand");
13730           return;
13731         }
13732
13733       switch (GET_CODE (x))
13734         {
13735         case REG:
13736           asm_fprintf (stream, "%r", REGNO (x));
13737           break;
13738
13739         case MEM:
13740           output_memory_reference_mode = GET_MODE (x);
13741           output_address (XEXP (x, 0));
13742           break;
13743
13744         case CONST_DOUBLE:
13745           if (TARGET_NEON)
13746             {
13747               char fpstr[20];
13748               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
13749                                sizeof (fpstr), 0, 1);
13750               fprintf (stream, "#%s", fpstr);
13751             }
13752           else
13753             fprintf (stream, "#%s", fp_immediate_constant (x));
13754           break;
13755
13756         default:
13757           gcc_assert (GET_CODE (x) != NEG);
13758           fputc ('#', stream);
13759           output_addr_const (stream, x);
13760           break;
13761         }
13762     }
13763 }
13764 \f
13765 /* Target hook for assembling integer objects.  The ARM version needs to
13766    handle word-sized values specially.  */
13767 static bool
13768 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
13769 {
13770   enum machine_mode mode;
13771
13772   if (size == UNITS_PER_WORD && aligned_p)
13773     {
13774       fputs ("\t.word\t", asm_out_file);
13775       output_addr_const (asm_out_file, x);
13776
13777       /* Mark symbols as position independent.  We only do this in the
13778          .text segment, not in the .data segment.  */
13779       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
13780           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
13781         {
13782           /* See legitimize_pic_address for an explanation of the
13783              TARGET_VXWORKS_RTP check.  */
13784           if (TARGET_VXWORKS_RTP
13785               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
13786             fputs ("(GOT)", asm_out_file);
13787           else
13788             fputs ("(GOTOFF)", asm_out_file);
13789         }
13790       fputc ('\n', asm_out_file);
13791       return true;
13792     }
13793
13794   mode = GET_MODE (x);
13795
13796   if (arm_vector_mode_supported_p (mode))
13797     {
13798       int i, units;
13799
13800       gcc_assert (GET_CODE (x) == CONST_VECTOR);
13801
13802       units = CONST_VECTOR_NUNITS (x);
13803       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
13804
13805       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13806         for (i = 0; i < units; i++)
13807           {
13808             rtx elt = CONST_VECTOR_ELT (x, i);
13809             assemble_integer
13810               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
13811           }
13812       else
13813         for (i = 0; i < units; i++)
13814           {
13815             rtx elt = CONST_VECTOR_ELT (x, i);
13816             REAL_VALUE_TYPE rval;
13817
13818             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
13819
13820             assemble_real
13821               (rval, GET_MODE_INNER (mode),
13822               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
13823           }
13824
13825       return true;
13826     }
13827
13828   return default_assemble_integer (x, size, aligned_p);
13829 }
13830
13831 static void
13832 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
13833 {
13834   section *s;
13835
13836   if (!TARGET_AAPCS_BASED)
13837     {
13838       (is_ctor ? 
13839        default_named_section_asm_out_constructor 
13840        : default_named_section_asm_out_destructor) (symbol, priority);
13841       return;
13842     }
13843
13844   /* Put these in the .init_array section, using a special relocation.  */
13845   if (priority != DEFAULT_INIT_PRIORITY)
13846     {
13847       char buf[18];
13848       sprintf (buf, "%s.%.5u", 
13849                is_ctor ? ".init_array" : ".fini_array",
13850                priority);
13851       s = get_section (buf, SECTION_WRITE, NULL_TREE);
13852     }
13853   else if (is_ctor)
13854     s = ctors_section;
13855   else
13856     s = dtors_section;
13857
13858   switch_to_section (s);
13859   assemble_align (POINTER_SIZE);
13860   fputs ("\t.word\t", asm_out_file);
13861   output_addr_const (asm_out_file, symbol);
13862   fputs ("(target1)\n", asm_out_file);
13863 }
13864
13865 /* Add a function to the list of static constructors.  */
13866
13867 static void
13868 arm_elf_asm_constructor (rtx symbol, int priority)
13869 {
13870   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
13871 }
13872
13873 /* Add a function to the list of static destructors.  */
13874
13875 static void
13876 arm_elf_asm_destructor (rtx symbol, int priority)
13877 {
13878   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
13879 }
13880 \f
13881 /* A finite state machine takes care of noticing whether or not instructions
13882    can be conditionally executed, and thus decrease execution time and code
13883    size by deleting branch instructions.  The fsm is controlled by
13884    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
13885
13886 /* The state of the fsm controlling condition codes are:
13887    0: normal, do nothing special
13888    1: make ASM_OUTPUT_OPCODE not output this instruction
13889    2: make ASM_OUTPUT_OPCODE not output this instruction
13890    3: make instructions conditional
13891    4: make instructions conditional
13892
13893    State transitions (state->state by whom under condition):
13894    0 -> 1 final_prescan_insn if the `target' is a label
13895    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
13896    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
13897    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
13898    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
13899           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
13900    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
13901           (the target insn is arm_target_insn).
13902
13903    If the jump clobbers the conditions then we use states 2 and 4.
13904
13905    A similar thing can be done with conditional return insns.
13906
13907    XXX In case the `target' is an unconditional branch, this conditionalising
13908    of the instructions always reduces code size, but not always execution
13909    time.  But then, I want to reduce the code size to somewhere near what
13910    /bin/cc produces.  */
13911
13912 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
13913    instructions.  When a COND_EXEC instruction is seen the subsequent
13914    instructions are scanned so that multiple conditional instructions can be
13915    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
13916    specify the length and true/false mask for the IT block.  These will be
13917    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
13918
13919 /* Returns the index of the ARM condition code string in
13920    `arm_condition_codes'.  COMPARISON should be an rtx like
13921    `(eq (...) (...))'.  */
13922 static enum arm_cond_code
13923 get_arm_condition_code (rtx comparison)
13924 {
13925   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
13926   int code;
13927   enum rtx_code comp_code = GET_CODE (comparison);
13928
13929   if (GET_MODE_CLASS (mode) != MODE_CC)
13930     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
13931                            XEXP (comparison, 1));
13932
13933   switch (mode)
13934     {
13935     case CC_DNEmode: code = ARM_NE; goto dominance;
13936     case CC_DEQmode: code = ARM_EQ; goto dominance;
13937     case CC_DGEmode: code = ARM_GE; goto dominance;
13938     case CC_DGTmode: code = ARM_GT; goto dominance;
13939     case CC_DLEmode: code = ARM_LE; goto dominance;
13940     case CC_DLTmode: code = ARM_LT; goto dominance;
13941     case CC_DGEUmode: code = ARM_CS; goto dominance;
13942     case CC_DGTUmode: code = ARM_HI; goto dominance;
13943     case CC_DLEUmode: code = ARM_LS; goto dominance;
13944     case CC_DLTUmode: code = ARM_CC;
13945
13946     dominance:
13947       gcc_assert (comp_code == EQ || comp_code == NE);
13948
13949       if (comp_code == EQ)
13950         return ARM_INVERSE_CONDITION_CODE (code);
13951       return code;
13952
13953     case CC_NOOVmode:
13954       switch (comp_code)
13955         {
13956         case NE: return ARM_NE;
13957         case EQ: return ARM_EQ;
13958         case GE: return ARM_PL;
13959         case LT: return ARM_MI;
13960         default: gcc_unreachable ();
13961         }
13962
13963     case CC_Zmode:
13964       switch (comp_code)
13965         {
13966         case NE: return ARM_NE;
13967         case EQ: return ARM_EQ;
13968         default: gcc_unreachable ();
13969         }
13970
13971     case CC_Nmode:
13972       switch (comp_code)
13973         {
13974         case NE: return ARM_MI;
13975         case EQ: return ARM_PL;
13976         default: gcc_unreachable ();
13977         }
13978
13979     case CCFPEmode:
13980     case CCFPmode:
13981       /* These encodings assume that AC=1 in the FPA system control
13982          byte.  This allows us to handle all cases except UNEQ and
13983          LTGT.  */
13984       switch (comp_code)
13985         {
13986         case GE: return ARM_GE;
13987         case GT: return ARM_GT;
13988         case LE: return ARM_LS;
13989         case LT: return ARM_MI;
13990         case NE: return ARM_NE;
13991         case EQ: return ARM_EQ;
13992         case ORDERED: return ARM_VC;
13993         case UNORDERED: return ARM_VS;
13994         case UNLT: return ARM_LT;
13995         case UNLE: return ARM_LE;
13996         case UNGT: return ARM_HI;
13997         case UNGE: return ARM_PL;
13998           /* UNEQ and LTGT do not have a representation.  */
13999         case UNEQ: /* Fall through.  */
14000         case LTGT: /* Fall through.  */
14001         default: gcc_unreachable ();
14002         }
14003
14004     case CC_SWPmode:
14005       switch (comp_code)
14006         {
14007         case NE: return ARM_NE;
14008         case EQ: return ARM_EQ;
14009         case GE: return ARM_LE;
14010         case GT: return ARM_LT;
14011         case LE: return ARM_GE;
14012         case LT: return ARM_GT;
14013         case GEU: return ARM_LS;
14014         case GTU: return ARM_CC;
14015         case LEU: return ARM_CS;
14016         case LTU: return ARM_HI;
14017         default: gcc_unreachable ();
14018         }
14019
14020     case CC_Cmode:
14021       switch (comp_code)
14022       {
14023       case LTU: return ARM_CS;
14024       case GEU: return ARM_CC;
14025       default: gcc_unreachable ();
14026       }
14027
14028     case CCmode:
14029       switch (comp_code)
14030         {
14031         case NE: return ARM_NE;
14032         case EQ: return ARM_EQ;
14033         case GE: return ARM_GE;
14034         case GT: return ARM_GT;
14035         case LE: return ARM_LE;
14036         case LT: return ARM_LT;
14037         case GEU: return ARM_CS;
14038         case GTU: return ARM_HI;
14039         case LEU: return ARM_LS;
14040         case LTU: return ARM_CC;
14041         default: gcc_unreachable ();
14042         }
14043
14044     default: gcc_unreachable ();
14045     }
14046 }
14047
14048 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
14049    instructions.  */
14050 void
14051 thumb2_final_prescan_insn (rtx insn)
14052 {
14053   rtx first_insn = insn;
14054   rtx body = PATTERN (insn);
14055   rtx predicate;
14056   enum arm_cond_code code;
14057   int n;
14058   int mask;
14059
14060   /* Remove the previous insn from the count of insns to be output.  */
14061   if (arm_condexec_count)
14062       arm_condexec_count--;
14063
14064   /* Nothing to do if we are already inside a conditional block.  */
14065   if (arm_condexec_count)
14066     return;
14067
14068   if (GET_CODE (body) != COND_EXEC)
14069     return;
14070
14071   /* Conditional jumps are implemented directly.  */
14072   if (GET_CODE (insn) == JUMP_INSN)
14073     return;
14074
14075   predicate = COND_EXEC_TEST (body);
14076   arm_current_cc = get_arm_condition_code (predicate);
14077
14078   n = get_attr_ce_count (insn);
14079   arm_condexec_count = 1;
14080   arm_condexec_mask = (1 << n) - 1;
14081   arm_condexec_masklen = n;
14082   /* See if subsequent instructions can be combined into the same block.  */
14083   for (;;)
14084     {
14085       insn = next_nonnote_insn (insn);
14086
14087       /* Jumping into the middle of an IT block is illegal, so a label or
14088          barrier terminates the block.  */
14089       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
14090         break;
14091
14092       body = PATTERN (insn);
14093       /* USE and CLOBBER aren't really insns, so just skip them.  */
14094       if (GET_CODE (body) == USE
14095           || GET_CODE (body) == CLOBBER)
14096         continue;
14097
14098       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
14099       if (GET_CODE (body) != COND_EXEC)
14100         break;
14101       /* Allow up to 4 conditionally executed instructions in a block.  */
14102       n = get_attr_ce_count (insn);
14103       if (arm_condexec_masklen + n > 4)
14104         break;
14105
14106       predicate = COND_EXEC_TEST (body);
14107       code = get_arm_condition_code (predicate);
14108       mask = (1 << n) - 1;
14109       if (arm_current_cc == code)
14110         arm_condexec_mask |= (mask << arm_condexec_masklen);
14111       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
14112         break;
14113
14114       arm_condexec_count++;
14115       arm_condexec_masklen += n;
14116
14117       /* A jump must be the last instruction in a conditional block.  */
14118       if (GET_CODE(insn) == JUMP_INSN)
14119         break;
14120     }
14121   /* Restore recog_data (getting the attributes of other insns can
14122      destroy this array, but final.c assumes that it remains intact
14123      across this call).  */
14124   extract_constrain_insn_cached (first_insn);
14125 }
14126
14127 void
14128 arm_final_prescan_insn (rtx insn)
14129 {
14130   /* BODY will hold the body of INSN.  */
14131   rtx body = PATTERN (insn);
14132
14133   /* This will be 1 if trying to repeat the trick, and things need to be
14134      reversed if it appears to fail.  */
14135   int reverse = 0;
14136
14137   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
14138      taken are clobbered, even if the rtl suggests otherwise.  It also
14139      means that we have to grub around within the jump expression to find
14140      out what the conditions are when the jump isn't taken.  */
14141   int jump_clobbers = 0;
14142
14143   /* If we start with a return insn, we only succeed if we find another one.  */
14144   int seeking_return = 0;
14145
14146   /* START_INSN will hold the insn from where we start looking.  This is the
14147      first insn after the following code_label if REVERSE is true.  */
14148   rtx start_insn = insn;
14149
14150   /* If in state 4, check if the target branch is reached, in order to
14151      change back to state 0.  */
14152   if (arm_ccfsm_state == 4)
14153     {
14154       if (insn == arm_target_insn)
14155         {
14156           arm_target_insn = NULL;
14157           arm_ccfsm_state = 0;
14158         }
14159       return;
14160     }
14161
14162   /* If in state 3, it is possible to repeat the trick, if this insn is an
14163      unconditional branch to a label, and immediately following this branch
14164      is the previous target label which is only used once, and the label this
14165      branch jumps to is not too far off.  */
14166   if (arm_ccfsm_state == 3)
14167     {
14168       if (simplejump_p (insn))
14169         {
14170           start_insn = next_nonnote_insn (start_insn);
14171           if (GET_CODE (start_insn) == BARRIER)
14172             {
14173               /* XXX Isn't this always a barrier?  */
14174               start_insn = next_nonnote_insn (start_insn);
14175             }
14176           if (GET_CODE (start_insn) == CODE_LABEL
14177               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
14178               && LABEL_NUSES (start_insn) == 1)
14179             reverse = TRUE;
14180           else
14181             return;
14182         }
14183       else if (GET_CODE (body) == RETURN)
14184         {
14185           start_insn = next_nonnote_insn (start_insn);
14186           if (GET_CODE (start_insn) == BARRIER)
14187             start_insn = next_nonnote_insn (start_insn);
14188           if (GET_CODE (start_insn) == CODE_LABEL
14189               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
14190               && LABEL_NUSES (start_insn) == 1)
14191             {
14192               reverse = TRUE;
14193               seeking_return = 1;
14194             }
14195           else
14196             return;
14197         }
14198       else
14199         return;
14200     }
14201
14202   gcc_assert (!arm_ccfsm_state || reverse);
14203   if (GET_CODE (insn) != JUMP_INSN)
14204     return;
14205
14206   /* This jump might be paralleled with a clobber of the condition codes
14207      the jump should always come first */
14208   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
14209     body = XVECEXP (body, 0, 0);
14210
14211   if (reverse
14212       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
14213           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
14214     {
14215       int insns_skipped;
14216       int fail = FALSE, succeed = FALSE;
14217       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
14218       int then_not_else = TRUE;
14219       rtx this_insn = start_insn, label = 0;
14220
14221       /* If the jump cannot be done with one instruction, we cannot
14222          conditionally execute the instruction in the inverse case.  */
14223       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
14224         {
14225           jump_clobbers = 1;
14226           return;
14227         }
14228
14229       /* Register the insn jumped to.  */
14230       if (reverse)
14231         {
14232           if (!seeking_return)
14233             label = XEXP (SET_SRC (body), 0);
14234         }
14235       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
14236         label = XEXP (XEXP (SET_SRC (body), 1), 0);
14237       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
14238         {
14239           label = XEXP (XEXP (SET_SRC (body), 2), 0);
14240           then_not_else = FALSE;
14241         }
14242       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
14243         seeking_return = 1;
14244       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
14245         {
14246           seeking_return = 1;
14247           then_not_else = FALSE;
14248         }
14249       else
14250         gcc_unreachable ();
14251
14252       /* See how many insns this branch skips, and what kind of insns.  If all
14253          insns are okay, and the label or unconditional branch to the same
14254          label is not too far away, succeed.  */
14255       for (insns_skipped = 0;
14256            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
14257         {
14258           rtx scanbody;
14259
14260           this_insn = next_nonnote_insn (this_insn);
14261           if (!this_insn)
14262             break;
14263
14264           switch (GET_CODE (this_insn))
14265             {
14266             case CODE_LABEL:
14267               /* Succeed if it is the target label, otherwise fail since
14268                  control falls in from somewhere else.  */
14269               if (this_insn == label)
14270                 {
14271                   if (jump_clobbers)
14272                     {
14273                       arm_ccfsm_state = 2;
14274                       this_insn = next_nonnote_insn (this_insn);
14275                     }
14276                   else
14277                     arm_ccfsm_state = 1;
14278                   succeed = TRUE;
14279                 }
14280               else
14281                 fail = TRUE;
14282               break;
14283
14284             case BARRIER:
14285               /* Succeed if the following insn is the target label.
14286                  Otherwise fail.
14287                  If return insns are used then the last insn in a function
14288                  will be a barrier.  */
14289               this_insn = next_nonnote_insn (this_insn);
14290               if (this_insn && this_insn == label)
14291                 {
14292                   if (jump_clobbers)
14293                     {
14294                       arm_ccfsm_state = 2;
14295                       this_insn = next_nonnote_insn (this_insn);
14296                     }
14297                   else
14298                     arm_ccfsm_state = 1;
14299                   succeed = TRUE;
14300                 }
14301               else
14302                 fail = TRUE;
14303               break;
14304
14305             case CALL_INSN:
14306               /* The AAPCS says that conditional calls should not be
14307                  used since they make interworking inefficient (the
14308                  linker can't transform BL<cond> into BLX).  That's
14309                  only a problem if the machine has BLX.  */
14310               if (arm_arch5)
14311                 {
14312                   fail = TRUE;
14313                   break;
14314                 }
14315
14316               /* Succeed if the following insn is the target label, or
14317                  if the following two insns are a barrier and the
14318                  target label.  */
14319               this_insn = next_nonnote_insn (this_insn);
14320               if (this_insn && GET_CODE (this_insn) == BARRIER)
14321                 this_insn = next_nonnote_insn (this_insn);
14322
14323               if (this_insn && this_insn == label
14324                   && insns_skipped < max_insns_skipped)
14325                 {
14326                   if (jump_clobbers)
14327                     {
14328                       arm_ccfsm_state = 2;
14329                       this_insn = next_nonnote_insn (this_insn);
14330                     }
14331                   else
14332                     arm_ccfsm_state = 1;
14333                   succeed = TRUE;
14334                 }
14335               else
14336                 fail = TRUE;
14337               break;
14338
14339             case JUMP_INSN:
14340               /* If this is an unconditional branch to the same label, succeed.
14341                  If it is to another label, do nothing.  If it is conditional,
14342                  fail.  */
14343               /* XXX Probably, the tests for SET and the PC are
14344                  unnecessary.  */
14345
14346               scanbody = PATTERN (this_insn);
14347               if (GET_CODE (scanbody) == SET
14348                   && GET_CODE (SET_DEST (scanbody)) == PC)
14349                 {
14350                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
14351                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
14352                     {
14353                       arm_ccfsm_state = 2;
14354                       succeed = TRUE;
14355                     }
14356                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
14357                     fail = TRUE;
14358                 }
14359               /* Fail if a conditional return is undesirable (e.g. on a
14360                  StrongARM), but still allow this if optimizing for size.  */
14361               else if (GET_CODE (scanbody) == RETURN
14362                        && !use_return_insn (TRUE, NULL)
14363                        && !optimize_size)
14364                 fail = TRUE;
14365               else if (GET_CODE (scanbody) == RETURN
14366                        && seeking_return)
14367                 {
14368                   arm_ccfsm_state = 2;
14369                   succeed = TRUE;
14370                 }
14371               else if (GET_CODE (scanbody) == PARALLEL)
14372                 {
14373                   switch (get_attr_conds (this_insn))
14374                     {
14375                     case CONDS_NOCOND:
14376                       break;
14377                     default:
14378                       fail = TRUE;
14379                       break;
14380                     }
14381                 }
14382               else
14383                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
14384
14385               break;
14386
14387             case INSN:
14388               /* Instructions using or affecting the condition codes make it
14389                  fail.  */
14390               scanbody = PATTERN (this_insn);
14391               if (!(GET_CODE (scanbody) == SET
14392                     || GET_CODE (scanbody) == PARALLEL)
14393                   || get_attr_conds (this_insn) != CONDS_NOCOND)
14394                 fail = TRUE;
14395
14396               /* A conditional cirrus instruction must be followed by
14397                  a non Cirrus instruction.  However, since we
14398                  conditionalize instructions in this function and by
14399                  the time we get here we can't add instructions
14400                  (nops), because shorten_branches() has already been
14401                  called, we will disable conditionalizing Cirrus
14402                  instructions to be safe.  */
14403               if (GET_CODE (scanbody) != USE
14404                   && GET_CODE (scanbody) != CLOBBER
14405                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
14406                 fail = TRUE;
14407               break;
14408
14409             default:
14410               break;
14411             }
14412         }
14413       if (succeed)
14414         {
14415           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
14416             arm_target_label = CODE_LABEL_NUMBER (label);
14417           else
14418             {
14419               gcc_assert (seeking_return || arm_ccfsm_state == 2);
14420
14421               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
14422                 {
14423                   this_insn = next_nonnote_insn (this_insn);
14424                   gcc_assert (!this_insn
14425                               || (GET_CODE (this_insn) != BARRIER
14426                                   && GET_CODE (this_insn) != CODE_LABEL));
14427                 }
14428               if (!this_insn)
14429                 {
14430                   /* Oh, dear! we ran off the end.. give up.  */
14431                   extract_constrain_insn_cached (insn);
14432                   arm_ccfsm_state = 0;
14433                   arm_target_insn = NULL;
14434                   return;
14435                 }
14436               arm_target_insn = this_insn;
14437             }
14438           if (jump_clobbers)
14439             {
14440               gcc_assert (!reverse);
14441               arm_current_cc =
14442                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
14443                                                             0), 0), 1));
14444               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
14445                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14446               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
14447                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14448             }
14449           else
14450             {
14451               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
14452                  what it was.  */
14453               if (!reverse)
14454                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
14455                                                                0));
14456             }
14457
14458           if (reverse || then_not_else)
14459             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14460         }
14461
14462       /* Restore recog_data (getting the attributes of other insns can
14463          destroy this array, but final.c assumes that it remains intact
14464          across this call.  */
14465       extract_constrain_insn_cached (insn);
14466     }
14467 }
14468
14469 /* Output IT instructions.  */
14470 void
14471 thumb2_asm_output_opcode (FILE * stream)
14472 {
14473   char buff[5];
14474   int n;
14475
14476   if (arm_condexec_mask)
14477     {
14478       for (n = 0; n < arm_condexec_masklen; n++)
14479         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
14480       buff[n] = 0;
14481       asm_fprintf(stream, "i%s\t%s\n\t", buff,
14482                   arm_condition_codes[arm_current_cc]);
14483       arm_condexec_mask = 0;
14484     }
14485 }
14486
14487 /* Returns true if REGNO is a valid register
14488    for holding a quantity of type MODE.  */
14489 int
14490 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
14491 {
14492   if (GET_MODE_CLASS (mode) == MODE_CC)
14493     return (regno == CC_REGNUM
14494             || (TARGET_HARD_FLOAT && TARGET_VFP
14495                 && regno == VFPCC_REGNUM));
14496
14497   if (TARGET_THUMB1)
14498     /* For the Thumb we only allow values bigger than SImode in
14499        registers 0 - 6, so that there is always a second low
14500        register available to hold the upper part of the value.
14501        We probably we ought to ensure that the register is the
14502        start of an even numbered register pair.  */
14503     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
14504
14505   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
14506       && IS_CIRRUS_REGNUM (regno))
14507     /* We have outlawed SI values in Cirrus registers because they
14508        reside in the lower 32 bits, but SF values reside in the
14509        upper 32 bits.  This causes gcc all sorts of grief.  We can't
14510        even split the registers into pairs because Cirrus SI values
14511        get sign extended to 64bits-- aldyh.  */
14512     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
14513
14514   if (TARGET_HARD_FLOAT && TARGET_VFP
14515       && IS_VFP_REGNUM (regno))
14516     {
14517       if (mode == SFmode || mode == SImode)
14518         return VFP_REGNO_OK_FOR_SINGLE (regno);
14519
14520       if (mode == DFmode)
14521         return VFP_REGNO_OK_FOR_DOUBLE (regno);
14522
14523       if (TARGET_NEON)
14524         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
14525                || (VALID_NEON_QREG_MODE (mode)
14526                    && NEON_REGNO_OK_FOR_QUAD (regno))
14527                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
14528                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
14529                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
14530                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
14531                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
14532
14533       return FALSE;
14534     }
14535
14536   if (TARGET_REALLY_IWMMXT)
14537     {
14538       if (IS_IWMMXT_GR_REGNUM (regno))
14539         return mode == SImode;
14540
14541       if (IS_IWMMXT_REGNUM (regno))
14542         return VALID_IWMMXT_REG_MODE (mode);
14543     }
14544   
14545   /* We allow any value to be stored in the general registers.
14546      Restrict doubleword quantities to even register pairs so that we can
14547      use ldrd.  Do not allow Neon structure opaque modes in general registers;
14548      they would use too many.  */
14549   if (regno <= LAST_ARM_REGNUM)
14550     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
14551       && !VALID_NEON_STRUCT_MODE (mode);
14552
14553   if (regno == FRAME_POINTER_REGNUM
14554       || regno == ARG_POINTER_REGNUM)
14555     /* We only allow integers in the fake hard registers.  */
14556     return GET_MODE_CLASS (mode) == MODE_INT;
14557
14558   /* The only registers left are the FPA registers
14559      which we only allow to hold FP values.  */
14560   return (TARGET_HARD_FLOAT && TARGET_FPA
14561           && GET_MODE_CLASS (mode) == MODE_FLOAT
14562           && regno >= FIRST_FPA_REGNUM
14563           && regno <= LAST_FPA_REGNUM);
14564 }
14565
14566 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
14567    not used in arm mode.  */
14568 int
14569 arm_regno_class (int regno)
14570 {
14571   if (TARGET_THUMB1)
14572     {
14573       if (regno == STACK_POINTER_REGNUM)
14574         return STACK_REG;
14575       if (regno == CC_REGNUM)
14576         return CC_REG;
14577       if (regno < 8)
14578         return LO_REGS;
14579       return HI_REGS;
14580     }
14581
14582   if (TARGET_THUMB2 && regno < 8)
14583     return LO_REGS;
14584
14585   if (   regno <= LAST_ARM_REGNUM
14586       || regno == FRAME_POINTER_REGNUM
14587       || regno == ARG_POINTER_REGNUM)
14588     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
14589
14590   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
14591     return TARGET_THUMB2 ? CC_REG : NO_REGS;
14592
14593   if (IS_CIRRUS_REGNUM (regno))
14594     return CIRRUS_REGS;
14595
14596   if (IS_VFP_REGNUM (regno))
14597     {
14598       if (regno <= D7_VFP_REGNUM)
14599         return VFP_D0_D7_REGS;
14600       else if (regno <= LAST_LO_VFP_REGNUM)
14601         return VFP_LO_REGS;
14602       else
14603         return VFP_HI_REGS;
14604     }
14605
14606   if (IS_IWMMXT_REGNUM (regno))
14607     return IWMMXT_REGS;
14608
14609   if (IS_IWMMXT_GR_REGNUM (regno))
14610     return IWMMXT_GR_REGS;
14611
14612   return FPA_REGS;
14613 }
14614
14615 /* Handle a special case when computing the offset
14616    of an argument from the frame pointer.  */
14617 int
14618 arm_debugger_arg_offset (int value, rtx addr)
14619 {
14620   rtx insn;
14621
14622   /* We are only interested if dbxout_parms() failed to compute the offset.  */
14623   if (value != 0)
14624     return 0;
14625
14626   /* We can only cope with the case where the address is held in a register.  */
14627   if (GET_CODE (addr) != REG)
14628     return 0;
14629
14630   /* If we are using the frame pointer to point at the argument, then
14631      an offset of 0 is correct.  */
14632   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
14633     return 0;
14634
14635   /* If we are using the stack pointer to point at the
14636      argument, then an offset of 0 is correct.  */
14637   /* ??? Check this is consistent with thumb2 frame layout.  */
14638   if ((TARGET_THUMB || !frame_pointer_needed)
14639       && REGNO (addr) == SP_REGNUM)
14640     return 0;
14641
14642   /* Oh dear.  The argument is pointed to by a register rather
14643      than being held in a register, or being stored at a known
14644      offset from the frame pointer.  Since GDB only understands
14645      those two kinds of argument we must translate the address
14646      held in the register into an offset from the frame pointer.
14647      We do this by searching through the insns for the function
14648      looking to see where this register gets its value.  If the
14649      register is initialized from the frame pointer plus an offset
14650      then we are in luck and we can continue, otherwise we give up.
14651
14652      This code is exercised by producing debugging information
14653      for a function with arguments like this:
14654
14655            double func (double a, double b, int c, double d) {return d;}
14656
14657      Without this code the stab for parameter 'd' will be set to
14658      an offset of 0 from the frame pointer, rather than 8.  */
14659
14660   /* The if() statement says:
14661
14662      If the insn is a normal instruction
14663      and if the insn is setting the value in a register
14664      and if the register being set is the register holding the address of the argument
14665      and if the address is computing by an addition
14666      that involves adding to a register
14667      which is the frame pointer
14668      a constant integer
14669
14670      then...  */
14671
14672   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14673     {
14674       if (   GET_CODE (insn) == INSN
14675           && GET_CODE (PATTERN (insn)) == SET
14676           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
14677           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
14678           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
14679           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
14680           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
14681              )
14682         {
14683           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
14684
14685           break;
14686         }
14687     }
14688
14689   if (value == 0)
14690     {
14691       debug_rtx (addr);
14692       warning (0, "unable to compute real location of stacked parameter");
14693       value = 8; /* XXX magic hack */
14694     }
14695
14696   return value;
14697 }
14698 \f
14699 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
14700   do                                                                    \
14701     {                                                                   \
14702       if ((MASK) & insn_flags)                                          \
14703         add_builtin_function ((NAME), (TYPE), (CODE),                   \
14704                              BUILT_IN_MD, NULL, NULL_TREE);             \
14705     }                                                                   \
14706   while (0)
14707
14708 struct builtin_description
14709 {
14710   const unsigned int       mask;
14711   const enum insn_code     icode;
14712   const char * const       name;
14713   const enum arm_builtins  code;
14714   const enum rtx_code      comparison;
14715   const unsigned int       flag;
14716 };
14717
14718 static const struct builtin_description bdesc_2arg[] =
14719 {
14720 #define IWMMXT_BUILTIN(code, string, builtin) \
14721   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
14722     ARM_BUILTIN_##builtin, 0, 0 },
14723
14724   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
14725   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
14726   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
14727   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
14728   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
14729   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
14730   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
14731   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
14732   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
14733   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
14734   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
14735   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
14736   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
14737   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
14738   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
14739   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
14740   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
14741   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
14742   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
14743   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
14744   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
14745   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
14746   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
14747   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
14748   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
14749   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
14750   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
14751   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
14752   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
14753   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
14754   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
14755   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
14756   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
14757   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
14758   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
14759   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
14760   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
14761   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
14762   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
14763   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
14764   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
14765   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
14766   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
14767   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
14768   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
14769   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
14770   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
14771   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
14772   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
14773   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
14774   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
14775   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
14776   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
14777   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
14778   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
14779   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
14780   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
14781   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
14782
14783 #define IWMMXT_BUILTIN2(code, builtin) \
14784   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
14785
14786   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
14787   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
14788   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
14789   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
14790   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
14791   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
14792   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
14793   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
14794   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
14795   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
14796   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
14797   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
14798   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
14799   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
14800   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
14801   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
14802   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
14803   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
14804   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
14805   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
14806   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
14807   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
14808   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
14809   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
14810   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
14811   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
14812   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
14813   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
14814   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
14815   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
14816   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
14817   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
14818 };
14819
14820 static const struct builtin_description bdesc_1arg[] =
14821 {
14822   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
14823   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
14824   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
14825   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
14826   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
14827   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
14828   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
14829   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
14830   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
14831   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
14832   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
14833   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
14834   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
14835   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
14836   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
14837   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
14838   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
14839   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
14840 };
14841
14842 /* Set up all the iWMMXt builtins.  This is
14843    not called if TARGET_IWMMXT is zero.  */
14844
14845 static void
14846 arm_init_iwmmxt_builtins (void)
14847 {
14848   const struct builtin_description * d;
14849   size_t i;
14850   tree endlink = void_list_node;
14851
14852   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14853   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14854   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14855
14856   tree int_ftype_int
14857     = build_function_type (integer_type_node,
14858                            tree_cons (NULL_TREE, integer_type_node, endlink));
14859   tree v8qi_ftype_v8qi_v8qi_int
14860     = build_function_type (V8QI_type_node,
14861                            tree_cons (NULL_TREE, V8QI_type_node,
14862                                       tree_cons (NULL_TREE, V8QI_type_node,
14863                                                  tree_cons (NULL_TREE,
14864                                                             integer_type_node,
14865                                                             endlink))));
14866   tree v4hi_ftype_v4hi_int
14867     = build_function_type (V4HI_type_node,
14868                            tree_cons (NULL_TREE, V4HI_type_node,
14869                                       tree_cons (NULL_TREE, integer_type_node,
14870                                                  endlink)));
14871   tree v2si_ftype_v2si_int
14872     = build_function_type (V2SI_type_node,
14873                            tree_cons (NULL_TREE, V2SI_type_node,
14874                                       tree_cons (NULL_TREE, integer_type_node,
14875                                                  endlink)));
14876   tree v2si_ftype_di_di
14877     = build_function_type (V2SI_type_node,
14878                            tree_cons (NULL_TREE, long_long_integer_type_node,
14879                                       tree_cons (NULL_TREE, long_long_integer_type_node,
14880                                                  endlink)));
14881   tree di_ftype_di_int
14882     = build_function_type (long_long_integer_type_node,
14883                            tree_cons (NULL_TREE, long_long_integer_type_node,
14884                                       tree_cons (NULL_TREE, integer_type_node,
14885                                                  endlink)));
14886   tree di_ftype_di_int_int
14887     = build_function_type (long_long_integer_type_node,
14888                            tree_cons (NULL_TREE, long_long_integer_type_node,
14889                                       tree_cons (NULL_TREE, integer_type_node,
14890                                                  tree_cons (NULL_TREE,
14891                                                             integer_type_node,
14892                                                             endlink))));
14893   tree int_ftype_v8qi
14894     = build_function_type (integer_type_node,
14895                            tree_cons (NULL_TREE, V8QI_type_node,
14896                                       endlink));
14897   tree int_ftype_v4hi
14898     = build_function_type (integer_type_node,
14899                            tree_cons (NULL_TREE, V4HI_type_node,
14900                                       endlink));
14901   tree int_ftype_v2si
14902     = build_function_type (integer_type_node,
14903                            tree_cons (NULL_TREE, V2SI_type_node,
14904                                       endlink));
14905   tree int_ftype_v8qi_int
14906     = build_function_type (integer_type_node,
14907                            tree_cons (NULL_TREE, V8QI_type_node,
14908                                       tree_cons (NULL_TREE, integer_type_node,
14909                                                  endlink)));
14910   tree int_ftype_v4hi_int
14911     = build_function_type (integer_type_node,
14912                            tree_cons (NULL_TREE, V4HI_type_node,
14913                                       tree_cons (NULL_TREE, integer_type_node,
14914                                                  endlink)));
14915   tree int_ftype_v2si_int
14916     = build_function_type (integer_type_node,
14917                            tree_cons (NULL_TREE, V2SI_type_node,
14918                                       tree_cons (NULL_TREE, integer_type_node,
14919                                                  endlink)));
14920   tree v8qi_ftype_v8qi_int_int
14921     = build_function_type (V8QI_type_node,
14922                            tree_cons (NULL_TREE, V8QI_type_node,
14923                                       tree_cons (NULL_TREE, integer_type_node,
14924                                                  tree_cons (NULL_TREE,
14925                                                             integer_type_node,
14926                                                             endlink))));
14927   tree v4hi_ftype_v4hi_int_int
14928     = build_function_type (V4HI_type_node,
14929                            tree_cons (NULL_TREE, V4HI_type_node,
14930                                       tree_cons (NULL_TREE, integer_type_node,
14931                                                  tree_cons (NULL_TREE,
14932                                                             integer_type_node,
14933                                                             endlink))));
14934   tree v2si_ftype_v2si_int_int
14935     = build_function_type (V2SI_type_node,
14936                            tree_cons (NULL_TREE, V2SI_type_node,
14937                                       tree_cons (NULL_TREE, integer_type_node,
14938                                                  tree_cons (NULL_TREE,
14939                                                             integer_type_node,
14940                                                             endlink))));
14941   /* Miscellaneous.  */
14942   tree v8qi_ftype_v4hi_v4hi
14943     = build_function_type (V8QI_type_node,
14944                            tree_cons (NULL_TREE, V4HI_type_node,
14945                                       tree_cons (NULL_TREE, V4HI_type_node,
14946                                                  endlink)));
14947   tree v4hi_ftype_v2si_v2si
14948     = build_function_type (V4HI_type_node,
14949                            tree_cons (NULL_TREE, V2SI_type_node,
14950                                       tree_cons (NULL_TREE, V2SI_type_node,
14951                                                  endlink)));
14952   tree v2si_ftype_v4hi_v4hi
14953     = build_function_type (V2SI_type_node,
14954                            tree_cons (NULL_TREE, V4HI_type_node,
14955                                       tree_cons (NULL_TREE, V4HI_type_node,
14956                                                  endlink)));
14957   tree v2si_ftype_v8qi_v8qi
14958     = build_function_type (V2SI_type_node,
14959                            tree_cons (NULL_TREE, V8QI_type_node,
14960                                       tree_cons (NULL_TREE, V8QI_type_node,
14961                                                  endlink)));
14962   tree v4hi_ftype_v4hi_di
14963     = build_function_type (V4HI_type_node,
14964                            tree_cons (NULL_TREE, V4HI_type_node,
14965                                       tree_cons (NULL_TREE,
14966                                                  long_long_integer_type_node,
14967                                                  endlink)));
14968   tree v2si_ftype_v2si_di
14969     = build_function_type (V2SI_type_node,
14970                            tree_cons (NULL_TREE, V2SI_type_node,
14971                                       tree_cons (NULL_TREE,
14972                                                  long_long_integer_type_node,
14973                                                  endlink)));
14974   tree void_ftype_int_int
14975     = build_function_type (void_type_node,
14976                            tree_cons (NULL_TREE, integer_type_node,
14977                                       tree_cons (NULL_TREE, integer_type_node,
14978                                                  endlink)));
14979   tree di_ftype_void
14980     = build_function_type (long_long_unsigned_type_node, endlink);
14981   tree di_ftype_v8qi
14982     = build_function_type (long_long_integer_type_node,
14983                            tree_cons (NULL_TREE, V8QI_type_node,
14984                                       endlink));
14985   tree di_ftype_v4hi
14986     = build_function_type (long_long_integer_type_node,
14987                            tree_cons (NULL_TREE, V4HI_type_node,
14988                                       endlink));
14989   tree di_ftype_v2si
14990     = build_function_type (long_long_integer_type_node,
14991                            tree_cons (NULL_TREE, V2SI_type_node,
14992                                       endlink));
14993   tree v2si_ftype_v4hi
14994     = build_function_type (V2SI_type_node,
14995                            tree_cons (NULL_TREE, V4HI_type_node,
14996                                       endlink));
14997   tree v4hi_ftype_v8qi
14998     = build_function_type (V4HI_type_node,
14999                            tree_cons (NULL_TREE, V8QI_type_node,
15000                                       endlink));
15001
15002   tree di_ftype_di_v4hi_v4hi
15003     = build_function_type (long_long_unsigned_type_node,
15004                            tree_cons (NULL_TREE,
15005                                       long_long_unsigned_type_node,
15006                                       tree_cons (NULL_TREE, V4HI_type_node,
15007                                                  tree_cons (NULL_TREE,
15008                                                             V4HI_type_node,
15009                                                             endlink))));
15010
15011   tree di_ftype_v4hi_v4hi
15012     = build_function_type (long_long_unsigned_type_node,
15013                            tree_cons (NULL_TREE, V4HI_type_node,
15014                                       tree_cons (NULL_TREE, V4HI_type_node,
15015                                                  endlink)));
15016
15017   /* Normal vector binops.  */
15018   tree v8qi_ftype_v8qi_v8qi
15019     = build_function_type (V8QI_type_node,
15020                            tree_cons (NULL_TREE, V8QI_type_node,
15021                                       tree_cons (NULL_TREE, V8QI_type_node,
15022                                                  endlink)));
15023   tree v4hi_ftype_v4hi_v4hi
15024     = build_function_type (V4HI_type_node,
15025                            tree_cons (NULL_TREE, V4HI_type_node,
15026                                       tree_cons (NULL_TREE, V4HI_type_node,
15027                                                  endlink)));
15028   tree v2si_ftype_v2si_v2si
15029     = build_function_type (V2SI_type_node,
15030                            tree_cons (NULL_TREE, V2SI_type_node,
15031                                       tree_cons (NULL_TREE, V2SI_type_node,
15032                                                  endlink)));
15033   tree di_ftype_di_di
15034     = build_function_type (long_long_unsigned_type_node,
15035                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
15036                                       tree_cons (NULL_TREE,
15037                                                  long_long_unsigned_type_node,
15038                                                  endlink)));
15039
15040   /* Add all builtins that are more or less simple operations on two
15041      operands.  */
15042   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15043     {
15044       /* Use one of the operands; the target can have a different mode for
15045          mask-generating compares.  */
15046       enum machine_mode mode;
15047       tree type;
15048
15049       if (d->name == 0)
15050         continue;
15051
15052       mode = insn_data[d->icode].operand[1].mode;
15053
15054       switch (mode)
15055         {
15056         case V8QImode:
15057           type = v8qi_ftype_v8qi_v8qi;
15058           break;
15059         case V4HImode:
15060           type = v4hi_ftype_v4hi_v4hi;
15061           break;
15062         case V2SImode:
15063           type = v2si_ftype_v2si_v2si;
15064           break;
15065         case DImode:
15066           type = di_ftype_di_di;
15067           break;
15068
15069         default:
15070           gcc_unreachable ();
15071         }
15072
15073       def_mbuiltin (d->mask, d->name, type, d->code);
15074     }
15075
15076   /* Add the remaining MMX insns with somewhat more complicated types.  */
15077   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
15078   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
15079   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
15080
15081   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
15082   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
15083   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
15084   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
15085   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
15086   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
15087
15088   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
15089   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
15090   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
15091   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
15092   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
15093   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
15094
15095   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
15096   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
15097   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
15098   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
15099   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
15100   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
15101
15102   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
15103   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
15104   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
15105   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
15106   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
15107   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
15108
15109   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
15110
15111   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
15112   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
15113   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
15114   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
15115
15116   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
15117   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
15118   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
15119   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
15120   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
15121   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
15122   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
15123   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
15124   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
15125
15126   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
15127   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
15128   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
15129
15130   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
15131   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
15132   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
15133
15134   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
15135   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
15136   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
15137   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
15138   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
15139   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
15140
15141   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
15142   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
15143   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
15144   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
15145   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
15146   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
15147   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
15148   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
15149   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
15150   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
15151   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
15152   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
15153
15154   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
15155   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
15156   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
15157   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
15158
15159   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
15160   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
15161   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
15162   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
15163   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
15164   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
15165   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
15166 }
15167
15168 static void
15169 arm_init_tls_builtins (void)
15170 {
15171   tree ftype, decl;
15172
15173   ftype = build_function_type (ptr_type_node, void_list_node);
15174   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
15175                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
15176                                NULL, NULL_TREE);
15177   TREE_NOTHROW (decl) = 1;
15178   TREE_READONLY (decl) = 1;
15179 }
15180
15181 typedef enum {
15182   T_V8QI  = 0x0001,
15183   T_V4HI  = 0x0002,
15184   T_V2SI  = 0x0004,
15185   T_V2SF  = 0x0008,
15186   T_DI    = 0x0010,
15187   T_V16QI = 0x0020,
15188   T_V8HI  = 0x0040,
15189   T_V4SI  = 0x0080,
15190   T_V4SF  = 0x0100,
15191   T_V2DI  = 0x0200,
15192   T_TI    = 0x0400,
15193   T_EI    = 0x0800,
15194   T_OI    = 0x1000
15195 } neon_builtin_type_bits;
15196
15197 #define v8qi_UP  T_V8QI
15198 #define v4hi_UP  T_V4HI
15199 #define v2si_UP  T_V2SI
15200 #define v2sf_UP  T_V2SF
15201 #define di_UP    T_DI
15202 #define v16qi_UP T_V16QI
15203 #define v8hi_UP  T_V8HI
15204 #define v4si_UP  T_V4SI
15205 #define v4sf_UP  T_V4SF
15206 #define v2di_UP  T_V2DI
15207 #define ti_UP    T_TI
15208 #define ei_UP    T_EI
15209 #define oi_UP    T_OI
15210
15211 #define UP(X) X##_UP
15212
15213 #define T_MAX 13
15214
15215 typedef enum {
15216   NEON_BINOP,
15217   NEON_TERNOP,
15218   NEON_UNOP,
15219   NEON_GETLANE,
15220   NEON_SETLANE,
15221   NEON_CREATE,
15222   NEON_DUP,
15223   NEON_DUPLANE,
15224   NEON_COMBINE,
15225   NEON_SPLIT,
15226   NEON_LANEMUL,
15227   NEON_LANEMULL,
15228   NEON_LANEMULH,
15229   NEON_LANEMAC,
15230   NEON_SCALARMUL,
15231   NEON_SCALARMULL,
15232   NEON_SCALARMULH,
15233   NEON_SCALARMAC,
15234   NEON_CONVERT,
15235   NEON_FIXCONV,
15236   NEON_SELECT,
15237   NEON_RESULTPAIR,
15238   NEON_REINTERP,
15239   NEON_VTBL,
15240   NEON_VTBX,
15241   NEON_LOAD1,
15242   NEON_LOAD1LANE,
15243   NEON_STORE1,
15244   NEON_STORE1LANE,
15245   NEON_LOADSTRUCT,
15246   NEON_LOADSTRUCTLANE,
15247   NEON_STORESTRUCT,
15248   NEON_STORESTRUCTLANE,
15249   NEON_LOGICBINOP,
15250   NEON_SHIFTINSERT,
15251   NEON_SHIFTIMM,
15252   NEON_SHIFTACC
15253 } neon_itype;
15254
15255 typedef struct {
15256   const char *name;
15257   const neon_itype itype;
15258   const neon_builtin_type_bits bits;
15259   const enum insn_code codes[T_MAX];
15260   const unsigned int num_vars;
15261   unsigned int base_fcode;
15262 } neon_builtin_datum;
15263
15264 #define CF(N,X) CODE_FOR_neon_##N##X
15265
15266 #define VAR1(T, N, A) \
15267   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
15268 #define VAR2(T, N, A, B) \
15269   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
15270 #define VAR3(T, N, A, B, C) \
15271   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
15272   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
15273 #define VAR4(T, N, A, B, C, D) \
15274   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
15275   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
15276 #define VAR5(T, N, A, B, C, D, E) \
15277   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
15278   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
15279 #define VAR6(T, N, A, B, C, D, E, F) \
15280   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
15281   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
15282 #define VAR7(T, N, A, B, C, D, E, F, G) \
15283   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
15284   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15285     CF (N, G) }, 7, 0
15286 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
15287   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15288                 | UP (H), \
15289   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15290     CF (N, G), CF (N, H) }, 8, 0
15291 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
15292   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15293                 | UP (H) | UP (I), \
15294   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15295     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
15296 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
15297   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
15298                 | UP (H) | UP (I) | UP (J), \
15299   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
15300     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
15301
15302 /* The mode entries in the following table correspond to the "key" type of the
15303    instruction variant, i.e. equivalent to that which would be specified after
15304    the assembler mnemonic, which usually refers to the last vector operand.
15305    (Signed/unsigned/polynomial types are not differentiated between though, and
15306    are all mapped onto the same mode for a given element size.) The modes
15307    listed per instruction should be the same as those defined for that
15308    instruction's pattern in neon.md.
15309    WARNING: Variants should be listed in the same increasing order as
15310    neon_builtin_type_bits.  */
15311
15312 static neon_builtin_datum neon_builtin_data[] =
15313 {
15314   { VAR10 (BINOP, vadd,
15315            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15316   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
15317   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
15318   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15319   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15320   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
15321   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15322   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15323   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
15324   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15325   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
15326   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
15327   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
15328   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
15329   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
15330   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
15331   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
15332   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
15333   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
15334   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
15335   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
15336   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
15337   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15338   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15339   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15340   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
15341   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
15342   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
15343   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15344   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15345   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15346   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
15347   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15348   { VAR10 (BINOP, vsub,
15349            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15350   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
15351   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
15352   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15353   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15354   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
15355   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15356   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15357   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15358   { VAR2 (BINOP, vcage, v2sf, v4sf) },
15359   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
15360   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15361   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15362   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
15363   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15364   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
15365   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15366   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15367   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
15368   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15369   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15370   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
15371   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
15372   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
15373   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
15374   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15375   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15376   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15377   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15378   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15379   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15380   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15381   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15382   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
15383   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
15384   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
15385   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15386   /* FIXME: vget_lane supports more variants than this!  */
15387   { VAR10 (GETLANE, vget_lane,
15388            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15389   { VAR10 (SETLANE, vset_lane,
15390            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15391   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
15392   { VAR10 (DUP, vdup_n,
15393            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15394   { VAR10 (DUPLANE, vdup_lane,
15395            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15396   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
15397   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
15398   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
15399   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
15400   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
15401   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
15402   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
15403   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15404   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15405   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
15406   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
15407   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15408   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
15409   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
15410   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15411   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15412   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
15413   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
15414   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15415   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
15416   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
15417   { VAR10 (BINOP, vext,
15418            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15419   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15420   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
15421   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
15422   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
15423   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
15424   { VAR10 (SELECT, vbsl,
15425            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15426   { VAR1 (VTBL, vtbl1, v8qi) },
15427   { VAR1 (VTBL, vtbl2, v8qi) },
15428   { VAR1 (VTBL, vtbl3, v8qi) },
15429   { VAR1 (VTBL, vtbl4, v8qi) },
15430   { VAR1 (VTBX, vtbx1, v8qi) },
15431   { VAR1 (VTBX, vtbx2, v8qi) },
15432   { VAR1 (VTBX, vtbx3, v8qi) },
15433   { VAR1 (VTBX, vtbx4, v8qi) },
15434   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15435   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15436   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15437   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
15438   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
15439   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
15440   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
15441   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
15442   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
15443   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
15444   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
15445   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
15446   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
15447   { VAR10 (LOAD1, vld1,
15448            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15449   { VAR10 (LOAD1LANE, vld1_lane,
15450            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15451   { VAR10 (LOAD1, vld1_dup,
15452            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15453   { VAR10 (STORE1, vst1,
15454            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15455   { VAR10 (STORE1LANE, vst1_lane,
15456            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15457   { VAR9 (LOADSTRUCT,
15458           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15459   { VAR7 (LOADSTRUCTLANE, vld2_lane,
15460           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15461   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
15462   { VAR9 (STORESTRUCT, vst2,
15463           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15464   { VAR7 (STORESTRUCTLANE, vst2_lane,
15465           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15466   { VAR9 (LOADSTRUCT,
15467           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15468   { VAR7 (LOADSTRUCTLANE, vld3_lane,
15469           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15470   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
15471   { VAR9 (STORESTRUCT, vst3,
15472           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15473   { VAR7 (STORESTRUCTLANE, vst3_lane,
15474           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15475   { VAR9 (LOADSTRUCT, vld4,
15476           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15477   { VAR7 (LOADSTRUCTLANE, vld4_lane,
15478           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15479   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
15480   { VAR9 (STORESTRUCT, vst4,
15481           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15482   { VAR7 (STORESTRUCTLANE, vst4_lane,
15483           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15484   { VAR10 (LOGICBINOP, vand,
15485            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15486   { VAR10 (LOGICBINOP, vorr,
15487            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15488   { VAR10 (BINOP, veor,
15489            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15490   { VAR10 (LOGICBINOP, vbic,
15491            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15492   { VAR10 (LOGICBINOP, vorn,
15493            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
15494 };
15495
15496 #undef CF
15497 #undef VAR1
15498 #undef VAR2
15499 #undef VAR3
15500 #undef VAR4
15501 #undef VAR5
15502 #undef VAR6
15503 #undef VAR7
15504 #undef VAR8
15505 #undef VAR9
15506 #undef VAR10
15507
15508 static void
15509 arm_init_neon_builtins (void)
15510 {
15511   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
15512
15513   tree neon_intQI_type_node;
15514   tree neon_intHI_type_node;
15515   tree neon_polyQI_type_node;
15516   tree neon_polyHI_type_node;
15517   tree neon_intSI_type_node;
15518   tree neon_intDI_type_node;
15519   tree neon_float_type_node;
15520
15521   tree intQI_pointer_node;
15522   tree intHI_pointer_node;
15523   tree intSI_pointer_node;
15524   tree intDI_pointer_node;
15525   tree float_pointer_node;
15526
15527   tree const_intQI_node;
15528   tree const_intHI_node;
15529   tree const_intSI_node;
15530   tree const_intDI_node;
15531   tree const_float_node;
15532
15533   tree const_intQI_pointer_node;
15534   tree const_intHI_pointer_node;
15535   tree const_intSI_pointer_node;
15536   tree const_intDI_pointer_node;
15537   tree const_float_pointer_node;
15538
15539   tree V8QI_type_node;
15540   tree V4HI_type_node;
15541   tree V2SI_type_node;
15542   tree V2SF_type_node;
15543   tree V16QI_type_node;
15544   tree V8HI_type_node;
15545   tree V4SI_type_node;
15546   tree V4SF_type_node;
15547   tree V2DI_type_node;
15548
15549   tree intUQI_type_node;
15550   tree intUHI_type_node;
15551   tree intUSI_type_node;
15552   tree intUDI_type_node;
15553
15554   tree intEI_type_node;
15555   tree intOI_type_node;
15556   tree intCI_type_node;
15557   tree intXI_type_node;
15558
15559   tree V8QI_pointer_node;
15560   tree V4HI_pointer_node;
15561   tree V2SI_pointer_node;
15562   tree V2SF_pointer_node;
15563   tree V16QI_pointer_node;
15564   tree V8HI_pointer_node;
15565   tree V4SI_pointer_node;
15566   tree V4SF_pointer_node;
15567   tree V2DI_pointer_node;
15568
15569   tree void_ftype_pv8qi_v8qi_v8qi;
15570   tree void_ftype_pv4hi_v4hi_v4hi;
15571   tree void_ftype_pv2si_v2si_v2si;
15572   tree void_ftype_pv2sf_v2sf_v2sf;
15573   tree void_ftype_pdi_di_di;
15574   tree void_ftype_pv16qi_v16qi_v16qi;
15575   tree void_ftype_pv8hi_v8hi_v8hi;
15576   tree void_ftype_pv4si_v4si_v4si;
15577   tree void_ftype_pv4sf_v4sf_v4sf;
15578   tree void_ftype_pv2di_v2di_v2di;
15579
15580   tree reinterp_ftype_dreg[5][5];
15581   tree reinterp_ftype_qreg[5][5];
15582   tree dreg_types[5], qreg_types[5];
15583
15584   /* Create distinguished type nodes for NEON vector element types,
15585      and pointers to values of such types, so we can detect them later.  */
15586   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15587   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15588   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15589   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15590   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
15591   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
15592   neon_float_type_node = make_node (REAL_TYPE);
15593   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
15594   layout_type (neon_float_type_node);
15595
15596   /* Define typedefs which exactly correspond to the modes we are basing vector
15597      types on.  If you change these names you'll need to change
15598      the table used by arm_mangle_type too.  */
15599   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
15600                                              "__builtin_neon_qi");
15601   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
15602                                              "__builtin_neon_hi");
15603   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
15604                                              "__builtin_neon_si");
15605   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
15606                                              "__builtin_neon_sf");
15607   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
15608                                              "__builtin_neon_di");
15609   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
15610                                              "__builtin_neon_poly8");
15611   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
15612                                              "__builtin_neon_poly16");
15613
15614   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
15615   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
15616   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
15617   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
15618   float_pointer_node = build_pointer_type (neon_float_type_node);
15619
15620   /* Next create constant-qualified versions of the above types.  */
15621   const_intQI_node = build_qualified_type (neon_intQI_type_node,
15622                                            TYPE_QUAL_CONST);
15623   const_intHI_node = build_qualified_type (neon_intHI_type_node,
15624                                            TYPE_QUAL_CONST);
15625   const_intSI_node = build_qualified_type (neon_intSI_type_node,
15626                                            TYPE_QUAL_CONST);
15627   const_intDI_node = build_qualified_type (neon_intDI_type_node,
15628                                            TYPE_QUAL_CONST);
15629   const_float_node = build_qualified_type (neon_float_type_node,
15630                                            TYPE_QUAL_CONST);
15631
15632   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
15633   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
15634   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
15635   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
15636   const_float_pointer_node = build_pointer_type (const_float_node);
15637
15638   /* Now create vector types based on our NEON element types.  */
15639   /* 64-bit vectors.  */
15640   V8QI_type_node =
15641     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
15642   V4HI_type_node =
15643     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
15644   V2SI_type_node =
15645     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
15646   V2SF_type_node =
15647     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
15648   /* 128-bit vectors.  */
15649   V16QI_type_node =
15650     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
15651   V8HI_type_node =
15652     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
15653   V4SI_type_node =
15654     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
15655   V4SF_type_node =
15656     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
15657   V2DI_type_node =
15658     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
15659
15660   /* Unsigned integer types for various mode sizes.  */
15661   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
15662   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
15663   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
15664   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
15665
15666   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
15667                                              "__builtin_neon_uqi");
15668   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
15669                                              "__builtin_neon_uhi");
15670   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
15671                                              "__builtin_neon_usi");
15672   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
15673                                              "__builtin_neon_udi");
15674
15675   /* Opaque integer types for structures of vectors.  */
15676   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
15677   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
15678   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
15679   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
15680
15681   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
15682                                              "__builtin_neon_ti");
15683   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
15684                                              "__builtin_neon_ei");
15685   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
15686                                              "__builtin_neon_oi");
15687   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
15688                                              "__builtin_neon_ci");
15689   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
15690                                              "__builtin_neon_xi");
15691
15692   /* Pointers to vector types.  */
15693   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
15694   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
15695   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
15696   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
15697   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
15698   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
15699   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
15700   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
15701   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
15702
15703   /* Operations which return results as pairs.  */
15704   void_ftype_pv8qi_v8qi_v8qi =
15705     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
15706                               V8QI_type_node, NULL);
15707   void_ftype_pv4hi_v4hi_v4hi =
15708     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
15709                               V4HI_type_node, NULL);
15710   void_ftype_pv2si_v2si_v2si =
15711     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
15712                               V2SI_type_node, NULL);
15713   void_ftype_pv2sf_v2sf_v2sf =
15714     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
15715                               V2SF_type_node, NULL);
15716   void_ftype_pdi_di_di =
15717     build_function_type_list (void_type_node, intDI_pointer_node,
15718                               neon_intDI_type_node, neon_intDI_type_node, NULL);
15719   void_ftype_pv16qi_v16qi_v16qi =
15720     build_function_type_list (void_type_node, V16QI_pointer_node,
15721                               V16QI_type_node, V16QI_type_node, NULL);
15722   void_ftype_pv8hi_v8hi_v8hi =
15723     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
15724                               V8HI_type_node, NULL);
15725   void_ftype_pv4si_v4si_v4si =
15726     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
15727                               V4SI_type_node, NULL);
15728   void_ftype_pv4sf_v4sf_v4sf =
15729     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
15730                               V4SF_type_node, NULL);
15731   void_ftype_pv2di_v2di_v2di =
15732     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
15733                               V2DI_type_node, NULL);
15734
15735   dreg_types[0] = V8QI_type_node;
15736   dreg_types[1] = V4HI_type_node;
15737   dreg_types[2] = V2SI_type_node;
15738   dreg_types[3] = V2SF_type_node;
15739   dreg_types[4] = neon_intDI_type_node;
15740
15741   qreg_types[0] = V16QI_type_node;
15742   qreg_types[1] = V8HI_type_node;
15743   qreg_types[2] = V4SI_type_node;
15744   qreg_types[3] = V4SF_type_node;
15745   qreg_types[4] = V2DI_type_node;
15746
15747   for (i = 0; i < 5; i++)
15748     {
15749       int j;
15750       for (j = 0; j < 5; j++)
15751         {
15752           reinterp_ftype_dreg[i][j]
15753             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
15754           reinterp_ftype_qreg[i][j]
15755             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
15756         }
15757     }
15758
15759   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
15760     {
15761       neon_builtin_datum *d = &neon_builtin_data[i];
15762       unsigned int j, codeidx = 0;
15763
15764       d->base_fcode = fcode;
15765
15766       for (j = 0; j < T_MAX; j++)
15767         {
15768           const char* const modenames[] = {
15769             "v8qi", "v4hi", "v2si", "v2sf", "di",
15770             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
15771           };
15772           char namebuf[60];
15773           tree ftype = NULL;
15774           enum insn_code icode;
15775           int is_load = 0, is_store = 0;
15776
15777           if ((d->bits & (1 << j)) == 0)
15778             continue;
15779
15780           icode = d->codes[codeidx++];
15781
15782           switch (d->itype)
15783             {
15784             case NEON_LOAD1:
15785             case NEON_LOAD1LANE:
15786             case NEON_LOADSTRUCT:
15787             case NEON_LOADSTRUCTLANE:
15788               is_load = 1;
15789               /* Fall through.  */
15790             case NEON_STORE1:
15791             case NEON_STORE1LANE:
15792             case NEON_STORESTRUCT:
15793             case NEON_STORESTRUCTLANE:
15794               if (!is_load)
15795                 is_store = 1;
15796               /* Fall through.  */
15797             case NEON_UNOP:
15798             case NEON_BINOP:
15799             case NEON_LOGICBINOP:
15800             case NEON_SHIFTINSERT:
15801             case NEON_TERNOP:
15802             case NEON_GETLANE:
15803             case NEON_SETLANE:
15804             case NEON_CREATE:
15805             case NEON_DUP:
15806             case NEON_DUPLANE:
15807             case NEON_SHIFTIMM:
15808             case NEON_SHIFTACC:
15809             case NEON_COMBINE:
15810             case NEON_SPLIT:
15811             case NEON_CONVERT:
15812             case NEON_FIXCONV:
15813             case NEON_LANEMUL:
15814             case NEON_LANEMULL:
15815             case NEON_LANEMULH:
15816             case NEON_LANEMAC:
15817             case NEON_SCALARMUL:
15818             case NEON_SCALARMULL:
15819             case NEON_SCALARMULH:
15820             case NEON_SCALARMAC:
15821             case NEON_SELECT:
15822             case NEON_VTBL:
15823             case NEON_VTBX:
15824               {
15825                 int k;
15826                 tree return_type = void_type_node, args = void_list_node;
15827
15828                 /* Build a function type directly from the insn_data for this
15829                    builtin.  The build_function_type() function takes care of
15830                    removing duplicates for us.  */
15831                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
15832                   {
15833                     tree eltype;
15834
15835                     if (is_load && k == 1)
15836                       {
15837                         /* Neon load patterns always have the memory operand
15838                            (a SImode pointer) in the operand 1 position.  We
15839                            want a const pointer to the element type in that
15840                            position.  */
15841                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
15842
15843                         switch (1 << j)
15844                           {
15845                           case T_V8QI:
15846                           case T_V16QI:
15847                             eltype = const_intQI_pointer_node;
15848                             break;
15849
15850                           case T_V4HI:
15851                           case T_V8HI:
15852                             eltype = const_intHI_pointer_node;
15853                             break;
15854
15855                           case T_V2SI:
15856                           case T_V4SI:
15857                             eltype = const_intSI_pointer_node;
15858                             break;
15859
15860                           case T_V2SF:
15861                           case T_V4SF:
15862                             eltype = const_float_pointer_node;
15863                             break;
15864
15865                           case T_DI:
15866                           case T_V2DI:
15867                             eltype = const_intDI_pointer_node;
15868                             break;
15869
15870                           default: gcc_unreachable ();
15871                           }
15872                       }
15873                     else if (is_store && k == 0)
15874                       {
15875                         /* Similarly, Neon store patterns use operand 0 as
15876                            the memory location to store to (a SImode pointer).
15877                            Use a pointer to the element type of the store in
15878                            that position.  */
15879                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
15880
15881                         switch (1 << j)
15882                           {
15883                           case T_V8QI:
15884                           case T_V16QI:
15885                             eltype = intQI_pointer_node;
15886                             break;
15887
15888                           case T_V4HI:
15889                           case T_V8HI:
15890                             eltype = intHI_pointer_node;
15891                             break;
15892
15893                           case T_V2SI:
15894                           case T_V4SI:
15895                             eltype = intSI_pointer_node;
15896                             break;
15897
15898                           case T_V2SF:
15899                           case T_V4SF:
15900                             eltype = float_pointer_node;
15901                             break;
15902
15903                           case T_DI:
15904                           case T_V2DI:
15905                             eltype = intDI_pointer_node;
15906                             break;
15907
15908                           default: gcc_unreachable ();
15909                           }
15910                       }
15911                     else
15912                       {
15913                         switch (insn_data[icode].operand[k].mode)
15914                           {
15915                           case VOIDmode: eltype = void_type_node; break;
15916                           /* Scalars.  */
15917                           case QImode: eltype = neon_intQI_type_node; break;
15918                           case HImode: eltype = neon_intHI_type_node; break;
15919                           case SImode: eltype = neon_intSI_type_node; break;
15920                           case SFmode: eltype = neon_float_type_node; break;
15921                           case DImode: eltype = neon_intDI_type_node; break;
15922                           case TImode: eltype = intTI_type_node; break;
15923                           case EImode: eltype = intEI_type_node; break;
15924                           case OImode: eltype = intOI_type_node; break;
15925                           case CImode: eltype = intCI_type_node; break;
15926                           case XImode: eltype = intXI_type_node; break;
15927                           /* 64-bit vectors.  */
15928                           case V8QImode: eltype = V8QI_type_node; break;
15929                           case V4HImode: eltype = V4HI_type_node; break;
15930                           case V2SImode: eltype = V2SI_type_node; break;
15931                           case V2SFmode: eltype = V2SF_type_node; break;
15932                           /* 128-bit vectors.  */
15933                           case V16QImode: eltype = V16QI_type_node; break;
15934                           case V8HImode: eltype = V8HI_type_node; break;
15935                           case V4SImode: eltype = V4SI_type_node; break;
15936                           case V4SFmode: eltype = V4SF_type_node; break;
15937                           case V2DImode: eltype = V2DI_type_node; break;
15938                           default: gcc_unreachable ();
15939                           }
15940                       }
15941
15942                     if (k == 0 && !is_store)
15943                       return_type = eltype;
15944                     else
15945                       args = tree_cons (NULL_TREE, eltype, args);
15946                   }
15947
15948                 ftype = build_function_type (return_type, args);
15949               }
15950               break;
15951
15952             case NEON_RESULTPAIR:
15953               {
15954                 switch (insn_data[icode].operand[1].mode)
15955                   {
15956                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
15957                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
15958                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
15959                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
15960                   case DImode: ftype = void_ftype_pdi_di_di; break;
15961                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
15962                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
15963                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
15964                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
15965                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
15966                   default: gcc_unreachable ();
15967                   }
15968               }
15969               break;
15970
15971             case NEON_REINTERP:
15972               {
15973                 /* We iterate over 5 doubleword types, then 5 quadword
15974                    types.  */
15975                 int rhs = j % 5;
15976                 switch (insn_data[icode].operand[0].mode)
15977                   {
15978                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
15979                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
15980                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
15981                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
15982                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
15983                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
15984                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
15985                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
15986                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
15987                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
15988                   default: gcc_unreachable ();
15989                   }
15990               }
15991               break;
15992
15993             default:
15994               gcc_unreachable ();
15995             }
15996
15997           gcc_assert (ftype != NULL);
15998
15999           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
16000
16001           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
16002                                 NULL_TREE);
16003         }
16004     }
16005 }
16006
16007 static void
16008 arm_init_builtins (void)
16009 {
16010   arm_init_tls_builtins ();
16011
16012   if (TARGET_REALLY_IWMMXT)
16013     arm_init_iwmmxt_builtins ();
16014
16015   if (TARGET_NEON)
16016     arm_init_neon_builtins ();
16017 }
16018
16019 /* Errors in the source file can cause expand_expr to return const0_rtx
16020    where we expect a vector.  To avoid crashing, use one of the vector
16021    clear instructions.  */
16022
16023 static rtx
16024 safe_vector_operand (rtx x, enum machine_mode mode)
16025 {
16026   if (x != const0_rtx)
16027     return x;
16028   x = gen_reg_rtx (mode);
16029
16030   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
16031                                : gen_rtx_SUBREG (DImode, x, 0)));
16032   return x;
16033 }
16034
16035 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
16036
16037 static rtx
16038 arm_expand_binop_builtin (enum insn_code icode,
16039                           tree exp, rtx target)
16040 {
16041   rtx pat;
16042   tree arg0 = CALL_EXPR_ARG (exp, 0);
16043   tree arg1 = CALL_EXPR_ARG (exp, 1);
16044   rtx op0 = expand_normal (arg0);
16045   rtx op1 = expand_normal (arg1);
16046   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16047   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16048   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
16049
16050   if (VECTOR_MODE_P (mode0))
16051     op0 = safe_vector_operand (op0, mode0);
16052   if (VECTOR_MODE_P (mode1))
16053     op1 = safe_vector_operand (op1, mode1);
16054
16055   if (! target
16056       || GET_MODE (target) != tmode
16057       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16058     target = gen_reg_rtx (tmode);
16059
16060   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
16061
16062   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16063     op0 = copy_to_mode_reg (mode0, op0);
16064   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16065     op1 = copy_to_mode_reg (mode1, op1);
16066
16067   pat = GEN_FCN (icode) (target, op0, op1);
16068   if (! pat)
16069     return 0;
16070   emit_insn (pat);
16071   return target;
16072 }
16073
16074 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
16075
16076 static rtx
16077 arm_expand_unop_builtin (enum insn_code icode,
16078                          tree exp, rtx target, int do_load)
16079 {
16080   rtx pat;
16081   tree arg0 = CALL_EXPR_ARG (exp, 0);
16082   rtx op0 = expand_normal (arg0);
16083   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16084   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16085
16086   if (! target
16087       || GET_MODE (target) != tmode
16088       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16089     target = gen_reg_rtx (tmode);
16090   if (do_load)
16091     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16092   else
16093     {
16094       if (VECTOR_MODE_P (mode0))
16095         op0 = safe_vector_operand (op0, mode0);
16096
16097       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16098         op0 = copy_to_mode_reg (mode0, op0);
16099     }
16100
16101   pat = GEN_FCN (icode) (target, op0);
16102   if (! pat)
16103     return 0;
16104   emit_insn (pat);
16105   return target;
16106 }
16107
16108 static int
16109 neon_builtin_compare (const void *a, const void *b)
16110 {
16111   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
16112   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
16113   unsigned int soughtcode = key->base_fcode;
16114
16115   if (soughtcode >= memb->base_fcode
16116       && soughtcode < memb->base_fcode + memb->num_vars)
16117     return 0;
16118   else if (soughtcode < memb->base_fcode)
16119     return -1;
16120   else
16121     return 1;
16122 }
16123
16124 static enum insn_code
16125 locate_neon_builtin_icode (int fcode, neon_itype *itype)
16126 {
16127   neon_builtin_datum key, *found;
16128   int idx;
16129
16130   key.base_fcode = fcode;
16131   found = (neon_builtin_datum *)
16132     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
16133                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
16134   gcc_assert (found);
16135   idx = fcode - (int) found->base_fcode;
16136   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
16137
16138   if (itype)
16139     *itype = found->itype;
16140
16141   return found->codes[idx];
16142 }
16143
16144 typedef enum {
16145   NEON_ARG_COPY_TO_REG,
16146   NEON_ARG_CONSTANT,
16147   NEON_ARG_STOP
16148 } builtin_arg;
16149
16150 #define NEON_MAX_BUILTIN_ARGS 5
16151
16152 /* Expand a Neon builtin.  */
16153 static rtx
16154 arm_expand_neon_args (rtx target, int icode, int have_retval,
16155                       tree exp, ...)
16156 {
16157   va_list ap;
16158   rtx pat;
16159   tree arg[NEON_MAX_BUILTIN_ARGS];
16160   rtx op[NEON_MAX_BUILTIN_ARGS];
16161   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16162   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
16163   int argc = 0;
16164
16165   if (have_retval
16166       && (!target
16167           || GET_MODE (target) != tmode
16168           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
16169     target = gen_reg_rtx (tmode);
16170
16171   va_start (ap, exp);
16172
16173   for (;;)
16174     {
16175       builtin_arg thisarg = va_arg (ap, int);
16176
16177       if (thisarg == NEON_ARG_STOP)
16178         break;
16179       else
16180         {
16181           arg[argc] = CALL_EXPR_ARG (exp, argc);
16182           op[argc] = expand_normal (arg[argc]);
16183           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
16184
16185           switch (thisarg)
16186             {
16187             case NEON_ARG_COPY_TO_REG:
16188               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
16189               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
16190                      (op[argc], mode[argc]))
16191                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
16192               break;
16193
16194             case NEON_ARG_CONSTANT:
16195               /* FIXME: This error message is somewhat unhelpful.  */
16196               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
16197                     (op[argc], mode[argc]))
16198                 error ("argument must be a constant");
16199               break;
16200
16201             case NEON_ARG_STOP:
16202               gcc_unreachable ();
16203             }
16204
16205           argc++;
16206         }
16207     }
16208
16209   va_end (ap);
16210
16211   if (have_retval)
16212     switch (argc)
16213       {
16214       case 1:
16215         pat = GEN_FCN (icode) (target, op[0]);
16216         break;
16217
16218       case 2:
16219         pat = GEN_FCN (icode) (target, op[0], op[1]);
16220         break;
16221
16222       case 3:
16223         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
16224         break;
16225
16226       case 4:
16227         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
16228         break;
16229
16230       case 5:
16231         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
16232         break;
16233
16234       default:
16235         gcc_unreachable ();
16236       }
16237   else
16238     switch (argc)
16239       {
16240       case 1:
16241         pat = GEN_FCN (icode) (op[0]);
16242         break;
16243
16244       case 2:
16245         pat = GEN_FCN (icode) (op[0], op[1]);
16246         break;
16247
16248       case 3:
16249         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
16250         break;
16251
16252       case 4:
16253         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
16254         break;
16255
16256       case 5:
16257         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
16258         break;
16259
16260       default:
16261         gcc_unreachable ();
16262       }
16263
16264   if (!pat)
16265     return 0;
16266
16267   emit_insn (pat);
16268
16269   return target;
16270 }
16271
16272 /* Expand a Neon builtin. These are "special" because they don't have symbolic
16273    constants defined per-instruction or per instruction-variant. Instead, the
16274    required info is looked up in the table neon_builtin_data.  */
16275 static rtx
16276 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
16277 {
16278   neon_itype itype;
16279   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
16280
16281   switch (itype)
16282     {
16283     case NEON_UNOP:
16284     case NEON_CONVERT:
16285     case NEON_DUPLANE:
16286       return arm_expand_neon_args (target, icode, 1, exp,
16287         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16288
16289     case NEON_BINOP:
16290     case NEON_SETLANE:
16291     case NEON_SCALARMUL:
16292     case NEON_SCALARMULL:
16293     case NEON_SCALARMULH:
16294     case NEON_SHIFTINSERT:
16295     case NEON_LOGICBINOP:
16296       return arm_expand_neon_args (target, icode, 1, exp,
16297         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16298         NEON_ARG_STOP);
16299
16300     case NEON_TERNOP:
16301       return arm_expand_neon_args (target, icode, 1, exp,
16302         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16303         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16304
16305     case NEON_GETLANE:
16306     case NEON_FIXCONV:
16307     case NEON_SHIFTIMM:
16308       return arm_expand_neon_args (target, icode, 1, exp,
16309         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
16310         NEON_ARG_STOP);
16311
16312     case NEON_CREATE:
16313       return arm_expand_neon_args (target, icode, 1, exp,
16314         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16315
16316     case NEON_DUP:
16317     case NEON_SPLIT:
16318     case NEON_REINTERP:
16319       return arm_expand_neon_args (target, icode, 1, exp,
16320         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16321
16322     case NEON_COMBINE:
16323     case NEON_VTBL:
16324       return arm_expand_neon_args (target, icode, 1, exp,
16325         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16326
16327     case NEON_RESULTPAIR:
16328       return arm_expand_neon_args (target, icode, 0, exp,
16329         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16330         NEON_ARG_STOP);
16331
16332     case NEON_LANEMUL:
16333     case NEON_LANEMULL:
16334     case NEON_LANEMULH:
16335       return arm_expand_neon_args (target, icode, 1, exp,
16336         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16337         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16338
16339     case NEON_LANEMAC:
16340       return arm_expand_neon_args (target, icode, 1, exp,
16341         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16342         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16343
16344     case NEON_SHIFTACC:
16345       return arm_expand_neon_args (target, icode, 1, exp,
16346         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16347         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16348
16349     case NEON_SCALARMAC:
16350       return arm_expand_neon_args (target, icode, 1, exp,
16351         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16352         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16353
16354     case NEON_SELECT:
16355     case NEON_VTBX:
16356       return arm_expand_neon_args (target, icode, 1, exp,
16357         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16358         NEON_ARG_STOP);
16359
16360     case NEON_LOAD1:
16361     case NEON_LOADSTRUCT:
16362       return arm_expand_neon_args (target, icode, 1, exp,
16363         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16364
16365     case NEON_LOAD1LANE:
16366     case NEON_LOADSTRUCTLANE:
16367       return arm_expand_neon_args (target, icode, 1, exp,
16368         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16369         NEON_ARG_STOP);
16370
16371     case NEON_STORE1:
16372     case NEON_STORESTRUCT:
16373       return arm_expand_neon_args (target, icode, 0, exp,
16374         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16375
16376     case NEON_STORE1LANE:
16377     case NEON_STORESTRUCTLANE:
16378       return arm_expand_neon_args (target, icode, 0, exp,
16379         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16380         NEON_ARG_STOP);
16381     }
16382
16383   gcc_unreachable ();
16384 }
16385
16386 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
16387 void
16388 neon_reinterpret (rtx dest, rtx src)
16389 {
16390   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
16391 }
16392
16393 /* Emit code to place a Neon pair result in memory locations (with equal
16394    registers).  */
16395 void
16396 neon_emit_pair_result_insn (enum machine_mode mode,
16397                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
16398                             rtx op1, rtx op2)
16399 {
16400   rtx mem = gen_rtx_MEM (mode, destaddr);
16401   rtx tmp1 = gen_reg_rtx (mode);
16402   rtx tmp2 = gen_reg_rtx (mode);
16403
16404   emit_insn (intfn (tmp1, op1, tmp2, op2));
16405
16406   emit_move_insn (mem, tmp1);
16407   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
16408   emit_move_insn (mem, tmp2);
16409 }
16410
16411 /* Set up operands for a register copy from src to dest, taking care not to
16412    clobber registers in the process.
16413    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
16414    be called with a large N, so that should be OK.  */
16415
16416 void
16417 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
16418 {
16419   unsigned int copied = 0, opctr = 0;
16420   unsigned int done = (1 << count) - 1;
16421   unsigned int i, j;
16422
16423   while (copied != done)
16424     {
16425       for (i = 0; i < count; i++)
16426         {
16427           int good = 1;
16428
16429           for (j = 0; good && j < count; j++)
16430             if (i != j && (copied & (1 << j)) == 0
16431                 && reg_overlap_mentioned_p (src[j], dest[i]))
16432               good = 0;
16433
16434           if (good)
16435             {
16436               operands[opctr++] = dest[i];
16437               operands[opctr++] = src[i];
16438               copied |= 1 << i;
16439             }
16440         }
16441     }
16442
16443   gcc_assert (opctr == count * 2);
16444 }
16445
16446 /* Expand an expression EXP that calls a built-in function,
16447    with result going to TARGET if that's convenient
16448    (and in mode MODE if that's convenient).
16449    SUBTARGET may be used as the target for computing one of EXP's operands.
16450    IGNORE is nonzero if the value is to be ignored.  */
16451
16452 static rtx
16453 arm_expand_builtin (tree exp,
16454                     rtx target,
16455                     rtx subtarget ATTRIBUTE_UNUSED,
16456                     enum machine_mode mode ATTRIBUTE_UNUSED,
16457                     int ignore ATTRIBUTE_UNUSED)
16458 {
16459   const struct builtin_description * d;
16460   enum insn_code    icode;
16461   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16462   tree              arg0;
16463   tree              arg1;
16464   tree              arg2;
16465   rtx               op0;
16466   rtx               op1;
16467   rtx               op2;
16468   rtx               pat;
16469   int               fcode = DECL_FUNCTION_CODE (fndecl);
16470   size_t            i;
16471   enum machine_mode tmode;
16472   enum machine_mode mode0;
16473   enum machine_mode mode1;
16474   enum machine_mode mode2;
16475
16476   if (fcode >= ARM_BUILTIN_NEON_BASE)
16477     return arm_expand_neon_builtin (fcode, exp, target);
16478
16479   switch (fcode)
16480     {
16481     case ARM_BUILTIN_TEXTRMSB:
16482     case ARM_BUILTIN_TEXTRMUB:
16483     case ARM_BUILTIN_TEXTRMSH:
16484     case ARM_BUILTIN_TEXTRMUH:
16485     case ARM_BUILTIN_TEXTRMSW:
16486     case ARM_BUILTIN_TEXTRMUW:
16487       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
16488                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
16489                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
16490                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
16491                : CODE_FOR_iwmmxt_textrmw);
16492
16493       arg0 = CALL_EXPR_ARG (exp, 0);
16494       arg1 = CALL_EXPR_ARG (exp, 1);
16495       op0 = expand_normal (arg0);
16496       op1 = expand_normal (arg1);
16497       tmode = insn_data[icode].operand[0].mode;
16498       mode0 = insn_data[icode].operand[1].mode;
16499       mode1 = insn_data[icode].operand[2].mode;
16500
16501       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16502         op0 = copy_to_mode_reg (mode0, op0);
16503       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16504         {
16505           /* @@@ better error message */
16506           error ("selector must be an immediate");
16507           return gen_reg_rtx (tmode);
16508         }
16509       if (target == 0
16510           || GET_MODE (target) != tmode
16511           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16512         target = gen_reg_rtx (tmode);
16513       pat = GEN_FCN (icode) (target, op0, op1);
16514       if (! pat)
16515         return 0;
16516       emit_insn (pat);
16517       return target;
16518
16519     case ARM_BUILTIN_TINSRB:
16520     case ARM_BUILTIN_TINSRH:
16521     case ARM_BUILTIN_TINSRW:
16522       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
16523                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
16524                : CODE_FOR_iwmmxt_tinsrw);
16525       arg0 = CALL_EXPR_ARG (exp, 0);
16526       arg1 = CALL_EXPR_ARG (exp, 1);
16527       arg2 = CALL_EXPR_ARG (exp, 2);
16528       op0 = expand_normal (arg0);
16529       op1 = expand_normal (arg1);
16530       op2 = expand_normal (arg2);
16531       tmode = insn_data[icode].operand[0].mode;
16532       mode0 = insn_data[icode].operand[1].mode;
16533       mode1 = insn_data[icode].operand[2].mode;
16534       mode2 = insn_data[icode].operand[3].mode;
16535
16536       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16537         op0 = copy_to_mode_reg (mode0, op0);
16538       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16539         op1 = copy_to_mode_reg (mode1, op1);
16540       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16541         {
16542           /* @@@ better error message */
16543           error ("selector must be an immediate");
16544           return const0_rtx;
16545         }
16546       if (target == 0
16547           || GET_MODE (target) != tmode
16548           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16549         target = gen_reg_rtx (tmode);
16550       pat = GEN_FCN (icode) (target, op0, op1, op2);
16551       if (! pat)
16552         return 0;
16553       emit_insn (pat);
16554       return target;
16555
16556     case ARM_BUILTIN_SETWCX:
16557       arg0 = CALL_EXPR_ARG (exp, 0);
16558       arg1 = CALL_EXPR_ARG (exp, 1);
16559       op0 = force_reg (SImode, expand_normal (arg0));
16560       op1 = expand_normal (arg1);
16561       emit_insn (gen_iwmmxt_tmcr (op1, op0));
16562       return 0;
16563
16564     case ARM_BUILTIN_GETWCX:
16565       arg0 = CALL_EXPR_ARG (exp, 0);
16566       op0 = expand_normal (arg0);
16567       target = gen_reg_rtx (SImode);
16568       emit_insn (gen_iwmmxt_tmrc (target, op0));
16569       return target;
16570
16571     case ARM_BUILTIN_WSHUFH:
16572       icode = CODE_FOR_iwmmxt_wshufh;
16573       arg0 = CALL_EXPR_ARG (exp, 0);
16574       arg1 = CALL_EXPR_ARG (exp, 1);
16575       op0 = expand_normal (arg0);
16576       op1 = expand_normal (arg1);
16577       tmode = insn_data[icode].operand[0].mode;
16578       mode1 = insn_data[icode].operand[1].mode;
16579       mode2 = insn_data[icode].operand[2].mode;
16580
16581       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16582         op0 = copy_to_mode_reg (mode1, op0);
16583       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16584         {
16585           /* @@@ better error message */
16586           error ("mask must be an immediate");
16587           return const0_rtx;
16588         }
16589       if (target == 0
16590           || GET_MODE (target) != tmode
16591           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16592         target = gen_reg_rtx (tmode);
16593       pat = GEN_FCN (icode) (target, op0, op1);
16594       if (! pat)
16595         return 0;
16596       emit_insn (pat);
16597       return target;
16598
16599     case ARM_BUILTIN_WSADB:
16600       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
16601     case ARM_BUILTIN_WSADH:
16602       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
16603     case ARM_BUILTIN_WSADBZ:
16604       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
16605     case ARM_BUILTIN_WSADHZ:
16606       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
16607
16608       /* Several three-argument builtins.  */
16609     case ARM_BUILTIN_WMACS:
16610     case ARM_BUILTIN_WMACU:
16611     case ARM_BUILTIN_WALIGN:
16612     case ARM_BUILTIN_TMIA:
16613     case ARM_BUILTIN_TMIAPH:
16614     case ARM_BUILTIN_TMIATT:
16615     case ARM_BUILTIN_TMIATB:
16616     case ARM_BUILTIN_TMIABT:
16617     case ARM_BUILTIN_TMIABB:
16618       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
16619                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
16620                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
16621                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
16622                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
16623                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
16624                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
16625                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
16626                : CODE_FOR_iwmmxt_walign);
16627       arg0 = CALL_EXPR_ARG (exp, 0);
16628       arg1 = CALL_EXPR_ARG (exp, 1);
16629       arg2 = CALL_EXPR_ARG (exp, 2);
16630       op0 = expand_normal (arg0);
16631       op1 = expand_normal (arg1);
16632       op2 = expand_normal (arg2);
16633       tmode = insn_data[icode].operand[0].mode;
16634       mode0 = insn_data[icode].operand[1].mode;
16635       mode1 = insn_data[icode].operand[2].mode;
16636       mode2 = insn_data[icode].operand[3].mode;
16637
16638       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16639         op0 = copy_to_mode_reg (mode0, op0);
16640       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16641         op1 = copy_to_mode_reg (mode1, op1);
16642       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16643         op2 = copy_to_mode_reg (mode2, op2);
16644       if (target == 0
16645           || GET_MODE (target) != tmode
16646           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16647         target = gen_reg_rtx (tmode);
16648       pat = GEN_FCN (icode) (target, op0, op1, op2);
16649       if (! pat)
16650         return 0;
16651       emit_insn (pat);
16652       return target;
16653
16654     case ARM_BUILTIN_WZERO:
16655       target = gen_reg_rtx (DImode);
16656       emit_insn (gen_iwmmxt_clrdi (target));
16657       return target;
16658
16659     case ARM_BUILTIN_THREAD_POINTER:
16660       return arm_load_tp (target);
16661
16662     default:
16663       break;
16664     }
16665
16666   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16667     if (d->code == (const enum arm_builtins) fcode)
16668       return arm_expand_binop_builtin (d->icode, exp, target);
16669
16670   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16671     if (d->code == (const enum arm_builtins) fcode)
16672       return arm_expand_unop_builtin (d->icode, exp, target, 0);
16673
16674   /* @@@ Should really do something sensible here.  */
16675   return NULL_RTX;
16676 }
16677 \f
16678 /* Return the number (counting from 0) of
16679    the least significant set bit in MASK.  */
16680
16681 inline static int
16682 number_of_first_bit_set (unsigned mask)
16683 {
16684   int bit;
16685
16686   for (bit = 0;
16687        (mask & (1 << bit)) == 0;
16688        ++bit)
16689     continue;
16690
16691   return bit;
16692 }
16693
16694 /* Emit code to push or pop registers to or from the stack.  F is the
16695    assembly file.  MASK is the registers to push or pop.  PUSH is
16696    nonzero if we should push, and zero if we should pop.  For debugging
16697    output, if pushing, adjust CFA_OFFSET by the amount of space added
16698    to the stack.  REAL_REGS should have the same number of bits set as
16699    MASK, and will be used instead (in the same order) to describe which
16700    registers were saved - this is used to mark the save slots when we
16701    push high registers after moving them to low registers.  */
16702 static void
16703 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
16704                unsigned long real_regs)
16705 {
16706   int regno;
16707   int lo_mask = mask & 0xFF;
16708   int pushed_words = 0;
16709
16710   gcc_assert (mask);
16711
16712   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
16713     {
16714       /* Special case.  Do not generate a POP PC statement here, do it in
16715          thumb_exit() */
16716       thumb_exit (f, -1);
16717       return;
16718     }
16719
16720   if (ARM_EABI_UNWIND_TABLES && push)
16721     {
16722       fprintf (f, "\t.save\t{");
16723       for (regno = 0; regno < 15; regno++)
16724         {
16725           if (real_regs & (1 << regno))
16726             {
16727               if (real_regs & ((1 << regno) -1))
16728                 fprintf (f, ", ");
16729               asm_fprintf (f, "%r", regno);
16730             }
16731         }
16732       fprintf (f, "}\n");
16733     }
16734
16735   fprintf (f, "\t%s\t{", push ? "push" : "pop");
16736
16737   /* Look at the low registers first.  */
16738   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
16739     {
16740       if (lo_mask & 1)
16741         {
16742           asm_fprintf (f, "%r", regno);
16743
16744           if ((lo_mask & ~1) != 0)
16745             fprintf (f, ", ");
16746
16747           pushed_words++;
16748         }
16749     }
16750
16751   if (push && (mask & (1 << LR_REGNUM)))
16752     {
16753       /* Catch pushing the LR.  */
16754       if (mask & 0xFF)
16755         fprintf (f, ", ");
16756
16757       asm_fprintf (f, "%r", LR_REGNUM);
16758
16759       pushed_words++;
16760     }
16761   else if (!push && (mask & (1 << PC_REGNUM)))
16762     {
16763       /* Catch popping the PC.  */
16764       if (TARGET_INTERWORK || TARGET_BACKTRACE
16765           || crtl->calls_eh_return)
16766         {
16767           /* The PC is never poped directly, instead
16768              it is popped into r3 and then BX is used.  */
16769           fprintf (f, "}\n");
16770
16771           thumb_exit (f, -1);
16772
16773           return;
16774         }
16775       else
16776         {
16777           if (mask & 0xFF)
16778             fprintf (f, ", ");
16779
16780           asm_fprintf (f, "%r", PC_REGNUM);
16781         }
16782     }
16783
16784   fprintf (f, "}\n");
16785
16786   if (push && pushed_words && dwarf2out_do_frame ())
16787     {
16788       char *l = dwarf2out_cfi_label ();
16789       int pushed_mask = real_regs;
16790
16791       *cfa_offset += pushed_words * 4;
16792       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
16793
16794       pushed_words = 0;
16795       pushed_mask = real_regs;
16796       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
16797         {
16798           if (pushed_mask & 1)
16799             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
16800         }
16801     }
16802 }
16803
16804 /* Generate code to return from a thumb function.
16805    If 'reg_containing_return_addr' is -1, then the return address is
16806    actually on the stack, at the stack pointer.  */
16807 static void
16808 thumb_exit (FILE *f, int reg_containing_return_addr)
16809 {
16810   unsigned regs_available_for_popping;
16811   unsigned regs_to_pop;
16812   int pops_needed;
16813   unsigned available;
16814   unsigned required;
16815   int mode;
16816   int size;
16817   int restore_a4 = FALSE;
16818
16819   /* Compute the registers we need to pop.  */
16820   regs_to_pop = 0;
16821   pops_needed = 0;
16822
16823   if (reg_containing_return_addr == -1)
16824     {
16825       regs_to_pop |= 1 << LR_REGNUM;
16826       ++pops_needed;
16827     }
16828
16829   if (TARGET_BACKTRACE)
16830     {
16831       /* Restore the (ARM) frame pointer and stack pointer.  */
16832       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
16833       pops_needed += 2;
16834     }
16835
16836   /* If there is nothing to pop then just emit the BX instruction and
16837      return.  */
16838   if (pops_needed == 0)
16839     {
16840       if (crtl->calls_eh_return)
16841         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
16842
16843       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16844       return;
16845     }
16846   /* Otherwise if we are not supporting interworking and we have not created
16847      a backtrace structure and the function was not entered in ARM mode then
16848      just pop the return address straight into the PC.  */
16849   else if (!TARGET_INTERWORK
16850            && !TARGET_BACKTRACE
16851            && !is_called_in_ARM_mode (current_function_decl)
16852            && !crtl->calls_eh_return)
16853     {
16854       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
16855       return;
16856     }
16857
16858   /* Find out how many of the (return) argument registers we can corrupt.  */
16859   regs_available_for_popping = 0;
16860
16861   /* If returning via __builtin_eh_return, the bottom three registers
16862      all contain information needed for the return.  */
16863   if (crtl->calls_eh_return)
16864     size = 12;
16865   else
16866     {
16867       /* If we can deduce the registers used from the function's
16868          return value.  This is more reliable that examining
16869          df_regs_ever_live_p () because that will be set if the register is
16870          ever used in the function, not just if the register is used
16871          to hold a return value.  */
16872
16873       if (crtl->return_rtx != 0)
16874         mode = GET_MODE (crtl->return_rtx);
16875       else
16876         mode = DECL_MODE (DECL_RESULT (current_function_decl));
16877
16878       size = GET_MODE_SIZE (mode);
16879
16880       if (size == 0)
16881         {
16882           /* In a void function we can use any argument register.
16883              In a function that returns a structure on the stack
16884              we can use the second and third argument registers.  */
16885           if (mode == VOIDmode)
16886             regs_available_for_popping =
16887               (1 << ARG_REGISTER (1))
16888               | (1 << ARG_REGISTER (2))
16889               | (1 << ARG_REGISTER (3));
16890           else
16891             regs_available_for_popping =
16892               (1 << ARG_REGISTER (2))
16893               | (1 << ARG_REGISTER (3));
16894         }
16895       else if (size <= 4)
16896         regs_available_for_popping =
16897           (1 << ARG_REGISTER (2))
16898           | (1 << ARG_REGISTER (3));
16899       else if (size <= 8)
16900         regs_available_for_popping =
16901           (1 << ARG_REGISTER (3));
16902     }
16903
16904   /* Match registers to be popped with registers into which we pop them.  */
16905   for (available = regs_available_for_popping,
16906        required  = regs_to_pop;
16907        required != 0 && available != 0;
16908        available &= ~(available & - available),
16909        required  &= ~(required  & - required))
16910     -- pops_needed;
16911
16912   /* If we have any popping registers left over, remove them.  */
16913   if (available > 0)
16914     regs_available_for_popping &= ~available;
16915
16916   /* Otherwise if we need another popping register we can use
16917      the fourth argument register.  */
16918   else if (pops_needed)
16919     {
16920       /* If we have not found any free argument registers and
16921          reg a4 contains the return address, we must move it.  */
16922       if (regs_available_for_popping == 0
16923           && reg_containing_return_addr == LAST_ARG_REGNUM)
16924         {
16925           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16926           reg_containing_return_addr = LR_REGNUM;
16927         }
16928       else if (size > 12)
16929         {
16930           /* Register a4 is being used to hold part of the return value,
16931              but we have dire need of a free, low register.  */
16932           restore_a4 = TRUE;
16933
16934           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
16935         }
16936
16937       if (reg_containing_return_addr != LAST_ARG_REGNUM)
16938         {
16939           /* The fourth argument register is available.  */
16940           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
16941
16942           --pops_needed;
16943         }
16944     }
16945
16946   /* Pop as many registers as we can.  */
16947   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16948                  regs_available_for_popping);
16949
16950   /* Process the registers we popped.  */
16951   if (reg_containing_return_addr == -1)
16952     {
16953       /* The return address was popped into the lowest numbered register.  */
16954       regs_to_pop &= ~(1 << LR_REGNUM);
16955
16956       reg_containing_return_addr =
16957         number_of_first_bit_set (regs_available_for_popping);
16958
16959       /* Remove this register for the mask of available registers, so that
16960          the return address will not be corrupted by further pops.  */
16961       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
16962     }
16963
16964   /* If we popped other registers then handle them here.  */
16965   if (regs_available_for_popping)
16966     {
16967       int frame_pointer;
16968
16969       /* Work out which register currently contains the frame pointer.  */
16970       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
16971
16972       /* Move it into the correct place.  */
16973       asm_fprintf (f, "\tmov\t%r, %r\n",
16974                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
16975
16976       /* (Temporarily) remove it from the mask of popped registers.  */
16977       regs_available_for_popping &= ~(1 << frame_pointer);
16978       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
16979
16980       if (regs_available_for_popping)
16981         {
16982           int stack_pointer;
16983
16984           /* We popped the stack pointer as well,
16985              find the register that contains it.  */
16986           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
16987
16988           /* Move it into the stack register.  */
16989           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
16990
16991           /* At this point we have popped all necessary registers, so
16992              do not worry about restoring regs_available_for_popping
16993              to its correct value:
16994
16995              assert (pops_needed == 0)
16996              assert (regs_available_for_popping == (1 << frame_pointer))
16997              assert (regs_to_pop == (1 << STACK_POINTER))  */
16998         }
16999       else
17000         {
17001           /* Since we have just move the popped value into the frame
17002              pointer, the popping register is available for reuse, and
17003              we know that we still have the stack pointer left to pop.  */
17004           regs_available_for_popping |= (1 << frame_pointer);
17005         }
17006     }
17007
17008   /* If we still have registers left on the stack, but we no longer have
17009      any registers into which we can pop them, then we must move the return
17010      address into the link register and make available the register that
17011      contained it.  */
17012   if (regs_available_for_popping == 0 && pops_needed > 0)
17013     {
17014       regs_available_for_popping |= 1 << reg_containing_return_addr;
17015
17016       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
17017                    reg_containing_return_addr);
17018
17019       reg_containing_return_addr = LR_REGNUM;
17020     }
17021
17022   /* If we have registers left on the stack then pop some more.
17023      We know that at most we will want to pop FP and SP.  */
17024   if (pops_needed > 0)
17025     {
17026       int  popped_into;
17027       int  move_to;
17028
17029       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17030                      regs_available_for_popping);
17031
17032       /* We have popped either FP or SP.
17033          Move whichever one it is into the correct register.  */
17034       popped_into = number_of_first_bit_set (regs_available_for_popping);
17035       move_to     = number_of_first_bit_set (regs_to_pop);
17036
17037       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
17038
17039       regs_to_pop &= ~(1 << move_to);
17040
17041       --pops_needed;
17042     }
17043
17044   /* If we still have not popped everything then we must have only
17045      had one register available to us and we are now popping the SP.  */
17046   if (pops_needed > 0)
17047     {
17048       int  popped_into;
17049
17050       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
17051                      regs_available_for_popping);
17052
17053       popped_into = number_of_first_bit_set (regs_available_for_popping);
17054
17055       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
17056       /*
17057         assert (regs_to_pop == (1 << STACK_POINTER))
17058         assert (pops_needed == 1)
17059       */
17060     }
17061
17062   /* If necessary restore the a4 register.  */
17063   if (restore_a4)
17064     {
17065       if (reg_containing_return_addr != LR_REGNUM)
17066         {
17067           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
17068           reg_containing_return_addr = LR_REGNUM;
17069         }
17070
17071       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
17072     }
17073
17074   if (crtl->calls_eh_return)
17075     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
17076
17077   /* Return to caller.  */
17078   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
17079 }
17080
17081 \f
17082 void
17083 thumb1_final_prescan_insn (rtx insn)
17084 {
17085   if (flag_print_asm_name)
17086     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
17087                  INSN_ADDRESSES (INSN_UID (insn)));
17088 }
17089
17090 int
17091 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
17092 {
17093   unsigned HOST_WIDE_INT mask = 0xff;
17094   int i;
17095
17096   if (val == 0) /* XXX */
17097     return 0;
17098
17099   for (i = 0; i < 25; i++)
17100     if ((val & (mask << i)) == val)
17101       return 1;
17102
17103   return 0;
17104 }
17105
17106 /* Returns nonzero if the current function contains,
17107    or might contain a far jump.  */
17108 static int
17109 thumb_far_jump_used_p (void)
17110 {
17111   rtx insn;
17112
17113   /* This test is only important for leaf functions.  */
17114   /* assert (!leaf_function_p ()); */
17115
17116   /* If we have already decided that far jumps may be used,
17117      do not bother checking again, and always return true even if
17118      it turns out that they are not being used.  Once we have made
17119      the decision that far jumps are present (and that hence the link
17120      register will be pushed onto the stack) we cannot go back on it.  */
17121   if (cfun->machine->far_jump_used)
17122     return 1;
17123
17124   /* If this function is not being called from the prologue/epilogue
17125      generation code then it must be being called from the
17126      INITIAL_ELIMINATION_OFFSET macro.  */
17127   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
17128     {
17129       /* In this case we know that we are being asked about the elimination
17130          of the arg pointer register.  If that register is not being used,
17131          then there are no arguments on the stack, and we do not have to
17132          worry that a far jump might force the prologue to push the link
17133          register, changing the stack offsets.  In this case we can just
17134          return false, since the presence of far jumps in the function will
17135          not affect stack offsets.
17136
17137          If the arg pointer is live (or if it was live, but has now been
17138          eliminated and so set to dead) then we do have to test to see if
17139          the function might contain a far jump.  This test can lead to some
17140          false negatives, since before reload is completed, then length of
17141          branch instructions is not known, so gcc defaults to returning their
17142          longest length, which in turn sets the far jump attribute to true.
17143
17144          A false negative will not result in bad code being generated, but it
17145          will result in a needless push and pop of the link register.  We
17146          hope that this does not occur too often.
17147
17148          If we need doubleword stack alignment this could affect the other
17149          elimination offsets so we can't risk getting it wrong.  */
17150       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
17151         cfun->machine->arg_pointer_live = 1;
17152       else if (!cfun->machine->arg_pointer_live)
17153         return 0;
17154     }
17155
17156   /* Check to see if the function contains a branch
17157      insn with the far jump attribute set.  */
17158   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17159     {
17160       if (GET_CODE (insn) == JUMP_INSN
17161           /* Ignore tablejump patterns.  */
17162           && GET_CODE (PATTERN (insn)) != ADDR_VEC
17163           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
17164           && get_attr_far_jump (insn) == FAR_JUMP_YES
17165           )
17166         {
17167           /* Record the fact that we have decided that
17168              the function does use far jumps.  */
17169           cfun->machine->far_jump_used = 1;
17170           return 1;
17171         }
17172     }
17173
17174   return 0;
17175 }
17176
17177 /* Return nonzero if FUNC must be entered in ARM mode.  */
17178 int
17179 is_called_in_ARM_mode (tree func)
17180 {
17181   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
17182
17183   /* Ignore the problem about functions whose address is taken.  */
17184   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
17185     return TRUE;
17186
17187 #ifdef ARM_PE
17188   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
17189 #else
17190   return FALSE;
17191 #endif
17192 }
17193
17194 /* The bits which aren't usefully expanded as rtl.  */
17195 const char *
17196 thumb_unexpanded_epilogue (void)
17197 {
17198   arm_stack_offsets *offsets;
17199   int regno;
17200   unsigned long live_regs_mask = 0;
17201   int high_regs_pushed = 0;
17202   int had_to_push_lr;
17203   int size;
17204
17205   if (return_used_this_function)
17206     return "";
17207
17208   if (IS_NAKED (arm_current_func_type ()))
17209     return "";
17210
17211   offsets = arm_get_frame_offsets ();
17212   live_regs_mask = offsets->saved_regs_mask;
17213   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
17214
17215   /* If we can deduce the registers used from the function's return value.
17216      This is more reliable that examining df_regs_ever_live_p () because that
17217      will be set if the register is ever used in the function, not just if
17218      the register is used to hold a return value.  */
17219   size = arm_size_return_regs ();
17220
17221   /* The prolog may have pushed some high registers to use as
17222      work registers.  e.g. the testsuite file:
17223      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
17224      compiles to produce:
17225         push    {r4, r5, r6, r7, lr}
17226         mov     r7, r9
17227         mov     r6, r8
17228         push    {r6, r7}
17229      as part of the prolog.  We have to undo that pushing here.  */
17230
17231   if (high_regs_pushed)
17232     {
17233       unsigned long mask = live_regs_mask & 0xff;
17234       int next_hi_reg;
17235
17236       /* The available low registers depend on the size of the value we are
17237          returning.  */
17238       if (size <= 12)
17239         mask |=  1 << 3;
17240       if (size <= 8)
17241         mask |= 1 << 2;
17242
17243       if (mask == 0)
17244         /* Oh dear!  We have no low registers into which we can pop
17245            high registers!  */
17246         internal_error
17247           ("no low registers available for popping high registers");
17248
17249       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
17250         if (live_regs_mask & (1 << next_hi_reg))
17251           break;
17252
17253       while (high_regs_pushed)
17254         {
17255           /* Find lo register(s) into which the high register(s) can
17256              be popped.  */
17257           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
17258             {
17259               if (mask & (1 << regno))
17260                 high_regs_pushed--;
17261               if (high_regs_pushed == 0)
17262                 break;
17263             }
17264
17265           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
17266
17267           /* Pop the values into the low register(s).  */
17268           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
17269
17270           /* Move the value(s) into the high registers.  */
17271           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
17272             {
17273               if (mask & (1 << regno))
17274                 {
17275                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
17276                                regno);
17277
17278                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
17279                     if (live_regs_mask & (1 << next_hi_reg))
17280                       break;
17281                 }
17282             }
17283         }
17284       live_regs_mask &= ~0x0f00;
17285     }
17286
17287   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
17288   live_regs_mask &= 0xff;
17289
17290   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
17291     {
17292       /* Pop the return address into the PC.  */
17293       if (had_to_push_lr)
17294         live_regs_mask |= 1 << PC_REGNUM;
17295
17296       /* Either no argument registers were pushed or a backtrace
17297          structure was created which includes an adjusted stack
17298          pointer, so just pop everything.  */
17299       if (live_regs_mask)
17300         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
17301                        live_regs_mask);
17302
17303       /* We have either just popped the return address into the
17304          PC or it is was kept in LR for the entire function.  */
17305       if (!had_to_push_lr)
17306         thumb_exit (asm_out_file, LR_REGNUM);
17307     }
17308   else
17309     {
17310       /* Pop everything but the return address.  */
17311       if (live_regs_mask)
17312         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
17313                        live_regs_mask);
17314
17315       if (had_to_push_lr)
17316         {
17317           if (size > 12)
17318             {
17319               /* We have no free low regs, so save one.  */
17320               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
17321                            LAST_ARG_REGNUM);
17322             }
17323
17324           /* Get the return address into a temporary register.  */
17325           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
17326                          1 << LAST_ARG_REGNUM);
17327
17328           if (size > 12)
17329             {
17330               /* Move the return address to lr.  */
17331               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
17332                            LAST_ARG_REGNUM);
17333               /* Restore the low register.  */
17334               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
17335                            IP_REGNUM);
17336               regno = LR_REGNUM;
17337             }
17338           else
17339             regno = LAST_ARG_REGNUM;
17340         }
17341       else
17342         regno = LR_REGNUM;
17343
17344       /* Remove the argument registers that were pushed onto the stack.  */
17345       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
17346                    SP_REGNUM, SP_REGNUM,
17347                    crtl->args.pretend_args_size);
17348
17349       thumb_exit (asm_out_file, regno);
17350     }
17351
17352   return "";
17353 }
17354
17355 /* Functions to save and restore machine-specific function data.  */
17356 static struct machine_function *
17357 arm_init_machine_status (void)
17358 {
17359   struct machine_function *machine;
17360   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
17361
17362 #if ARM_FT_UNKNOWN != 0
17363   machine->func_type = ARM_FT_UNKNOWN;
17364 #endif
17365   return machine;
17366 }
17367
17368 /* Return an RTX indicating where the return address to the
17369    calling function can be found.  */
17370 rtx
17371 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
17372 {
17373   if (count != 0)
17374     return NULL_RTX;
17375
17376   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
17377 }
17378
17379 /* Do anything needed before RTL is emitted for each function.  */
17380 void
17381 arm_init_expanders (void)
17382 {
17383   /* Arrange to initialize and mark the machine per-function status.  */
17384   init_machine_status = arm_init_machine_status;
17385
17386   /* This is to stop the combine pass optimizing away the alignment
17387      adjustment of va_arg.  */
17388   /* ??? It is claimed that this should not be necessary.  */
17389   if (cfun)
17390     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
17391 }
17392
17393
17394 /* Like arm_compute_initial_elimination offset.  Simpler because there
17395    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
17396    to point at the base of the local variables after static stack
17397    space for a function has been allocated.  */
17398
17399 HOST_WIDE_INT
17400 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
17401 {
17402   arm_stack_offsets *offsets;
17403
17404   offsets = arm_get_frame_offsets ();
17405
17406   switch (from)
17407     {
17408     case ARG_POINTER_REGNUM:
17409       switch (to)
17410         {
17411         case STACK_POINTER_REGNUM:
17412           return offsets->outgoing_args - offsets->saved_args;
17413
17414         case FRAME_POINTER_REGNUM:
17415           return offsets->soft_frame - offsets->saved_args;
17416
17417         case ARM_HARD_FRAME_POINTER_REGNUM:
17418           return offsets->saved_regs - offsets->saved_args;
17419
17420         case THUMB_HARD_FRAME_POINTER_REGNUM:
17421           return offsets->locals_base - offsets->saved_args;
17422
17423         default:
17424           gcc_unreachable ();
17425         }
17426       break;
17427
17428     case FRAME_POINTER_REGNUM:
17429       switch (to)
17430         {
17431         case STACK_POINTER_REGNUM:
17432           return offsets->outgoing_args - offsets->soft_frame;
17433
17434         case ARM_HARD_FRAME_POINTER_REGNUM:
17435           return offsets->saved_regs - offsets->soft_frame;
17436
17437         case THUMB_HARD_FRAME_POINTER_REGNUM:
17438           return offsets->locals_base - offsets->soft_frame;
17439
17440         default:
17441           gcc_unreachable ();
17442         }
17443       break;
17444
17445     default:
17446       gcc_unreachable ();
17447     }
17448 }
17449
17450 /* Generate the rest of a function's prologue.  */
17451 void
17452 thumb1_expand_prologue (void)
17453 {
17454   rtx insn, dwarf;
17455
17456   HOST_WIDE_INT amount;
17457   arm_stack_offsets *offsets;
17458   unsigned long func_type;
17459   int regno;
17460   unsigned long live_regs_mask;
17461
17462   func_type = arm_current_func_type ();
17463
17464   /* Naked functions don't have prologues.  */
17465   if (IS_NAKED (func_type))
17466     return;
17467
17468   if (IS_INTERRUPT (func_type))
17469     {
17470       error ("interrupt Service Routines cannot be coded in Thumb mode");
17471       return;
17472     }
17473
17474   offsets = arm_get_frame_offsets ();
17475   live_regs_mask = offsets->saved_regs_mask;
17476   /* Load the pic register before setting the frame pointer,
17477      so we can use r7 as a temporary work register.  */
17478   if (flag_pic && arm_pic_register != INVALID_REGNUM)
17479     arm_load_pic_register (live_regs_mask);
17480
17481   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
17482     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
17483                     stack_pointer_rtx);
17484
17485   amount = offsets->outgoing_args - offsets->saved_regs;
17486   if (amount)
17487     {
17488       if (amount < 512)
17489         {
17490           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17491                                         GEN_INT (- amount)));
17492           RTX_FRAME_RELATED_P (insn) = 1;
17493         }
17494       else
17495         {
17496           rtx reg;
17497
17498           /* The stack decrement is too big for an immediate value in a single
17499              insn.  In theory we could issue multiple subtracts, but after
17500              three of them it becomes more space efficient to place the full
17501              value in the constant pool and load into a register.  (Also the
17502              ARM debugger really likes to see only one stack decrement per
17503              function).  So instead we look for a scratch register into which
17504              we can load the decrement, and then we subtract this from the
17505              stack pointer.  Unfortunately on the thumb the only available
17506              scratch registers are the argument registers, and we cannot use
17507              these as they may hold arguments to the function.  Instead we
17508              attempt to locate a call preserved register which is used by this
17509              function.  If we can find one, then we know that it will have
17510              been pushed at the start of the prologue and so we can corrupt
17511              it now.  */
17512           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
17513             if (live_regs_mask & (1 << regno))
17514               break;
17515
17516           gcc_assert(regno <= LAST_LO_REGNUM);
17517
17518           reg = gen_rtx_REG (SImode, regno);
17519
17520           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
17521
17522           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
17523                                         stack_pointer_rtx, reg));
17524           RTX_FRAME_RELATED_P (insn) = 1;
17525           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17526                                plus_constant (stack_pointer_rtx,
17527                                               -amount));
17528           RTX_FRAME_RELATED_P (dwarf) = 1;
17529           REG_NOTES (insn)
17530             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
17531                                  REG_NOTES (insn));
17532         }
17533     }
17534
17535   if (frame_pointer_needed)
17536     thumb_set_frame_pointer (offsets);
17537
17538   /* If we are profiling, make sure no instructions are scheduled before
17539      the call to mcount.  Similarly if the user has requested no
17540      scheduling in the prolog.  Similarly if we want non-call exceptions
17541      using the EABI unwinder, to prevent faulting instructions from being
17542      swapped with a stack adjustment.  */
17543   if (crtl->profile || !TARGET_SCHED_PROLOG
17544       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
17545     emit_insn (gen_blockage ());
17546
17547   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
17548   if (live_regs_mask & 0xff)
17549     cfun->machine->lr_save_eliminated = 0;
17550 }
17551
17552
17553 void
17554 thumb1_expand_epilogue (void)
17555 {
17556   HOST_WIDE_INT amount;
17557   arm_stack_offsets *offsets;
17558   int regno;
17559
17560   /* Naked functions don't have prologues.  */
17561   if (IS_NAKED (arm_current_func_type ()))
17562     return;
17563
17564   offsets = arm_get_frame_offsets ();
17565   amount = offsets->outgoing_args - offsets->saved_regs;
17566
17567   if (frame_pointer_needed)
17568     {
17569       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
17570       amount = offsets->locals_base - offsets->saved_regs;
17571     }
17572
17573   gcc_assert (amount >= 0);
17574   if (amount)
17575     {
17576       if (amount < 512)
17577         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17578                                GEN_INT (amount)));
17579       else
17580         {
17581           /* r3 is always free in the epilogue.  */
17582           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
17583
17584           emit_insn (gen_movsi (reg, GEN_INT (amount)));
17585           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
17586         }
17587     }
17588
17589   /* Emit a USE (stack_pointer_rtx), so that
17590      the stack adjustment will not be deleted.  */
17591   emit_insn (gen_prologue_use (stack_pointer_rtx));
17592
17593   if (crtl->profile || !TARGET_SCHED_PROLOG)
17594     emit_insn (gen_blockage ());
17595
17596   /* Emit a clobber for each insn that will be restored in the epilogue,
17597      so that flow2 will get register lifetimes correct.  */
17598   for (regno = 0; regno < 13; regno++)
17599     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
17600       emit_clobber (gen_rtx_REG (SImode, regno));
17601
17602   if (! df_regs_ever_live_p (LR_REGNUM))
17603     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
17604 }
17605
17606 static void
17607 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
17608 {
17609   arm_stack_offsets *offsets;
17610   unsigned long live_regs_mask = 0;
17611   unsigned long l_mask;
17612   unsigned high_regs_pushed = 0;
17613   int cfa_offset = 0;
17614   int regno;
17615
17616   if (IS_NAKED (arm_current_func_type ()))
17617     return;
17618
17619   if (is_called_in_ARM_mode (current_function_decl))
17620     {
17621       const char * name;
17622
17623       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
17624       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
17625                   == SYMBOL_REF);
17626       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
17627
17628       /* Generate code sequence to switch us into Thumb mode.  */
17629       /* The .code 32 directive has already been emitted by
17630          ASM_DECLARE_FUNCTION_NAME.  */
17631       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
17632       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
17633
17634       /* Generate a label, so that the debugger will notice the
17635          change in instruction sets.  This label is also used by
17636          the assembler to bypass the ARM code when this function
17637          is called from a Thumb encoded function elsewhere in the
17638          same file.  Hence the definition of STUB_NAME here must
17639          agree with the definition in gas/config/tc-arm.c.  */
17640
17641 #define STUB_NAME ".real_start_of"
17642
17643       fprintf (f, "\t.code\t16\n");
17644 #ifdef ARM_PE
17645       if (arm_dllexport_name_p (name))
17646         name = arm_strip_name_encoding (name);
17647 #endif
17648       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
17649       fprintf (f, "\t.thumb_func\n");
17650       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
17651     }
17652
17653   if (crtl->args.pretend_args_size)
17654     {
17655       /* Output unwind directive for the stack adjustment.  */
17656       if (ARM_EABI_UNWIND_TABLES)
17657         fprintf (f, "\t.pad #%d\n",
17658                  crtl->args.pretend_args_size);
17659
17660       if (cfun->machine->uses_anonymous_args)
17661         {
17662           int num_pushes;
17663
17664           fprintf (f, "\tpush\t{");
17665
17666           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
17667
17668           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
17669                regno <= LAST_ARG_REGNUM;
17670                regno++)
17671             asm_fprintf (f, "%r%s", regno,
17672                          regno == LAST_ARG_REGNUM ? "" : ", ");
17673
17674           fprintf (f, "}\n");
17675         }
17676       else
17677         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
17678                      SP_REGNUM, SP_REGNUM,
17679                      crtl->args.pretend_args_size);
17680
17681       /* We don't need to record the stores for unwinding (would it
17682          help the debugger any if we did?), but record the change in
17683          the stack pointer.  */
17684       if (dwarf2out_do_frame ())
17685         {
17686           char *l = dwarf2out_cfi_label ();
17687
17688           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
17689           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17690         }
17691     }
17692
17693   /* Get the registers we are going to push.  */
17694   offsets = arm_get_frame_offsets ();
17695   live_regs_mask = offsets->saved_regs_mask;
17696   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
17697   l_mask = live_regs_mask & 0x40ff;
17698   /* Then count how many other high registers will need to be pushed.  */
17699   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
17700
17701   if (TARGET_BACKTRACE)
17702     {
17703       unsigned offset;
17704       unsigned work_register;
17705
17706       /* We have been asked to create a stack backtrace structure.
17707          The code looks like this:
17708
17709          0   .align 2
17710          0   func:
17711          0     sub   SP, #16         Reserve space for 4 registers.
17712          2     push  {R7}            Push low registers.
17713          4     add   R7, SP, #20     Get the stack pointer before the push.
17714          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
17715          8     mov   R7, PC          Get hold of the start of this code plus 12.
17716         10     str   R7, [SP, #16]   Store it.
17717         12     mov   R7, FP          Get hold of the current frame pointer.
17718         14     str   R7, [SP, #4]    Store it.
17719         16     mov   R7, LR          Get hold of the current return address.
17720         18     str   R7, [SP, #12]   Store it.
17721         20     add   R7, SP, #16     Point at the start of the backtrace structure.
17722         22     mov   FP, R7          Put this value into the frame pointer.  */
17723
17724       work_register = thumb_find_work_register (live_regs_mask);
17725
17726       if (ARM_EABI_UNWIND_TABLES)
17727         asm_fprintf (f, "\t.pad #16\n");
17728
17729       asm_fprintf
17730         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
17731          SP_REGNUM, SP_REGNUM);
17732
17733       if (dwarf2out_do_frame ())
17734         {
17735           char *l = dwarf2out_cfi_label ();
17736
17737           cfa_offset = cfa_offset + 16;
17738           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17739         }
17740
17741       if (l_mask)
17742         {
17743           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
17744           offset = bit_count (l_mask) * UNITS_PER_WORD;
17745         }
17746       else
17747         offset = 0;
17748
17749       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17750                    offset + 16 + crtl->args.pretend_args_size);
17751
17752       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17753                    offset + 4);
17754
17755       /* Make sure that the instruction fetching the PC is in the right place
17756          to calculate "start of backtrace creation code + 12".  */
17757       if (l_mask)
17758         {
17759           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17760           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17761                        offset + 12);
17762           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17763                        ARM_HARD_FRAME_POINTER_REGNUM);
17764           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17765                        offset);
17766         }
17767       else
17768         {
17769           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17770                        ARM_HARD_FRAME_POINTER_REGNUM);
17771           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17772                        offset);
17773           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17774           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17775                        offset + 12);
17776         }
17777
17778       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
17779       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17780                    offset + 8);
17781       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17782                    offset + 12);
17783       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
17784                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
17785     }
17786   /* Optimization:  If we are not pushing any low registers but we are going
17787      to push some high registers then delay our first push.  This will just
17788      be a push of LR and we can combine it with the push of the first high
17789      register.  */
17790   else if ((l_mask & 0xff) != 0
17791            || (high_regs_pushed == 0 && l_mask))
17792     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
17793
17794   if (high_regs_pushed)
17795     {
17796       unsigned pushable_regs;
17797       unsigned next_hi_reg;
17798
17799       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
17800         if (live_regs_mask & (1 << next_hi_reg))
17801           break;
17802
17803       pushable_regs = l_mask & 0xff;
17804
17805       if (pushable_regs == 0)
17806         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
17807
17808       while (high_regs_pushed > 0)
17809         {
17810           unsigned long real_regs_mask = 0;
17811
17812           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
17813             {
17814               if (pushable_regs & (1 << regno))
17815                 {
17816                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
17817
17818                   high_regs_pushed --;
17819                   real_regs_mask |= (1 << next_hi_reg);
17820
17821                   if (high_regs_pushed)
17822                     {
17823                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
17824                            next_hi_reg --)
17825                         if (live_regs_mask & (1 << next_hi_reg))
17826                           break;
17827                     }
17828                   else
17829                     {
17830                       pushable_regs &= ~((1 << regno) - 1);
17831                       break;
17832                     }
17833                 }
17834             }
17835
17836           /* If we had to find a work register and we have not yet
17837              saved the LR then add it to the list of regs to push.  */
17838           if (l_mask == (1 << LR_REGNUM))
17839             {
17840               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
17841                              1, &cfa_offset,
17842                              real_regs_mask | (1 << LR_REGNUM));
17843               l_mask = 0;
17844             }
17845           else
17846             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
17847         }
17848     }
17849 }
17850
17851 /* Handle the case of a double word load into a low register from
17852    a computed memory address.  The computed address may involve a
17853    register which is overwritten by the load.  */
17854 const char *
17855 thumb_load_double_from_address (rtx *operands)
17856 {
17857   rtx addr;
17858   rtx base;
17859   rtx offset;
17860   rtx arg1;
17861   rtx arg2;
17862
17863   gcc_assert (GET_CODE (operands[0]) == REG);
17864   gcc_assert (GET_CODE (operands[1]) == MEM);
17865
17866   /* Get the memory address.  */
17867   addr = XEXP (operands[1], 0);
17868
17869   /* Work out how the memory address is computed.  */
17870   switch (GET_CODE (addr))
17871     {
17872     case REG:
17873       operands[2] = adjust_address (operands[1], SImode, 4);
17874
17875       if (REGNO (operands[0]) == REGNO (addr))
17876         {
17877           output_asm_insn ("ldr\t%H0, %2", operands);
17878           output_asm_insn ("ldr\t%0, %1", operands);
17879         }
17880       else
17881         {
17882           output_asm_insn ("ldr\t%0, %1", operands);
17883           output_asm_insn ("ldr\t%H0, %2", operands);
17884         }
17885       break;
17886
17887     case CONST:
17888       /* Compute <address> + 4 for the high order load.  */
17889       operands[2] = adjust_address (operands[1], SImode, 4);
17890
17891       output_asm_insn ("ldr\t%0, %1", operands);
17892       output_asm_insn ("ldr\t%H0, %2", operands);
17893       break;
17894
17895     case PLUS:
17896       arg1   = XEXP (addr, 0);
17897       arg2   = XEXP (addr, 1);
17898
17899       if (CONSTANT_P (arg1))
17900         base = arg2, offset = arg1;
17901       else
17902         base = arg1, offset = arg2;
17903
17904       gcc_assert (GET_CODE (base) == REG);
17905
17906       /* Catch the case of <address> = <reg> + <reg> */
17907       if (GET_CODE (offset) == REG)
17908         {
17909           int reg_offset = REGNO (offset);
17910           int reg_base   = REGNO (base);
17911           int reg_dest   = REGNO (operands[0]);
17912
17913           /* Add the base and offset registers together into the
17914              higher destination register.  */
17915           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
17916                        reg_dest + 1, reg_base, reg_offset);
17917
17918           /* Load the lower destination register from the address in
17919              the higher destination register.  */
17920           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
17921                        reg_dest, reg_dest + 1);
17922
17923           /* Load the higher destination register from its own address
17924              plus 4.  */
17925           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
17926                        reg_dest + 1, reg_dest + 1);
17927         }
17928       else
17929         {
17930           /* Compute <address> + 4 for the high order load.  */
17931           operands[2] = adjust_address (operands[1], SImode, 4);
17932
17933           /* If the computed address is held in the low order register
17934              then load the high order register first, otherwise always
17935              load the low order register first.  */
17936           if (REGNO (operands[0]) == REGNO (base))
17937             {
17938               output_asm_insn ("ldr\t%H0, %2", operands);
17939               output_asm_insn ("ldr\t%0, %1", operands);
17940             }
17941           else
17942             {
17943               output_asm_insn ("ldr\t%0, %1", operands);
17944               output_asm_insn ("ldr\t%H0, %2", operands);
17945             }
17946         }
17947       break;
17948
17949     case LABEL_REF:
17950       /* With no registers to worry about we can just load the value
17951          directly.  */
17952       operands[2] = adjust_address (operands[1], SImode, 4);
17953
17954       output_asm_insn ("ldr\t%H0, %2", operands);
17955       output_asm_insn ("ldr\t%0, %1", operands);
17956       break;
17957
17958     default:
17959       gcc_unreachable ();
17960     }
17961
17962   return "";
17963 }
17964
17965 const char *
17966 thumb_output_move_mem_multiple (int n, rtx *operands)
17967 {
17968   rtx tmp;
17969
17970   switch (n)
17971     {
17972     case 2:
17973       if (REGNO (operands[4]) > REGNO (operands[5]))
17974         {
17975           tmp = operands[4];
17976           operands[4] = operands[5];
17977           operands[5] = tmp;
17978         }
17979       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
17980       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
17981       break;
17982
17983     case 3:
17984       if (REGNO (operands[4]) > REGNO (operands[5]))
17985         {
17986           tmp = operands[4];
17987           operands[4] = operands[5];
17988           operands[5] = tmp;
17989         }
17990       if (REGNO (operands[5]) > REGNO (operands[6]))
17991         {
17992           tmp = operands[5];
17993           operands[5] = operands[6];
17994           operands[6] = tmp;
17995         }
17996       if (REGNO (operands[4]) > REGNO (operands[5]))
17997         {
17998           tmp = operands[4];
17999           operands[4] = operands[5];
18000           operands[5] = tmp;
18001         }
18002
18003       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
18004       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
18005       break;
18006
18007     default:
18008       gcc_unreachable ();
18009     }
18010
18011   return "";
18012 }
18013
18014 /* Output a call-via instruction for thumb state.  */
18015 const char *
18016 thumb_call_via_reg (rtx reg)
18017 {
18018   int regno = REGNO (reg);
18019   rtx *labelp;
18020
18021   gcc_assert (regno < LR_REGNUM);
18022
18023   /* If we are in the normal text section we can use a single instance
18024      per compilation unit.  If we are doing function sections, then we need
18025      an entry per section, since we can't rely on reachability.  */
18026   if (in_section == text_section)
18027     {
18028       thumb_call_reg_needed = 1;
18029
18030       if (thumb_call_via_label[regno] == NULL)
18031         thumb_call_via_label[regno] = gen_label_rtx ();
18032       labelp = thumb_call_via_label + regno;
18033     }
18034   else
18035     {
18036       if (cfun->machine->call_via[regno] == NULL)
18037         cfun->machine->call_via[regno] = gen_label_rtx ();
18038       labelp = cfun->machine->call_via + regno;
18039     }
18040
18041   output_asm_insn ("bl\t%a0", labelp);
18042   return "";
18043 }
18044
18045 /* Routines for generating rtl.  */
18046 void
18047 thumb_expand_movmemqi (rtx *operands)
18048 {
18049   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
18050   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
18051   HOST_WIDE_INT len = INTVAL (operands[2]);
18052   HOST_WIDE_INT offset = 0;
18053
18054   while (len >= 12)
18055     {
18056       emit_insn (gen_movmem12b (out, in, out, in));
18057       len -= 12;
18058     }
18059
18060   if (len >= 8)
18061     {
18062       emit_insn (gen_movmem8b (out, in, out, in));
18063       len -= 8;
18064     }
18065
18066   if (len >= 4)
18067     {
18068       rtx reg = gen_reg_rtx (SImode);
18069       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
18070       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
18071       len -= 4;
18072       offset += 4;
18073     }
18074
18075   if (len >= 2)
18076     {
18077       rtx reg = gen_reg_rtx (HImode);
18078       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
18079                                               plus_constant (in, offset))));
18080       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
18081                             reg));
18082       len -= 2;
18083       offset += 2;
18084     }
18085
18086   if (len)
18087     {
18088       rtx reg = gen_reg_rtx (QImode);
18089       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
18090                                               plus_constant (in, offset))));
18091       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
18092                             reg));
18093     }
18094 }
18095
18096 void
18097 thumb_reload_out_hi (rtx *operands)
18098 {
18099   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
18100 }
18101
18102 /* Handle reading a half-word from memory during reload.  */
18103 void
18104 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
18105 {
18106   gcc_unreachable ();
18107 }
18108
18109 /* Return the length of a function name prefix
18110     that starts with the character 'c'.  */
18111 static int
18112 arm_get_strip_length (int c)
18113 {
18114   switch (c)
18115     {
18116     ARM_NAME_ENCODING_LENGTHS
18117       default: return 0;
18118     }
18119 }
18120
18121 /* Return a pointer to a function's name with any
18122    and all prefix encodings stripped from it.  */
18123 const char *
18124 arm_strip_name_encoding (const char *name)
18125 {
18126   int skip;
18127
18128   while ((skip = arm_get_strip_length (* name)))
18129     name += skip;
18130
18131   return name;
18132 }
18133
18134 /* If there is a '*' anywhere in the name's prefix, then
18135    emit the stripped name verbatim, otherwise prepend an
18136    underscore if leading underscores are being used.  */
18137 void
18138 arm_asm_output_labelref (FILE *stream, const char *name)
18139 {
18140   int skip;
18141   int verbatim = 0;
18142
18143   while ((skip = arm_get_strip_length (* name)))
18144     {
18145       verbatim |= (*name == '*');
18146       name += skip;
18147     }
18148
18149   if (verbatim)
18150     fputs (name, stream);
18151   else
18152     asm_fprintf (stream, "%U%s", name);
18153 }
18154
18155 static void
18156 arm_file_start (void)
18157 {
18158   int val;
18159
18160   if (TARGET_UNIFIED_ASM)
18161     asm_fprintf (asm_out_file, "\t.syntax unified\n");
18162
18163   if (TARGET_BPABI)
18164     {
18165       const char *fpu_name;
18166       if (arm_select[0].string)
18167         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
18168       else if (arm_select[1].string)
18169         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
18170       else
18171         asm_fprintf (asm_out_file, "\t.cpu %s\n",
18172                      all_cores[arm_default_cpu].name);
18173
18174       if (TARGET_SOFT_FLOAT)
18175         {
18176           if (TARGET_VFP)
18177             fpu_name = "softvfp";
18178           else
18179             fpu_name = "softfpa";
18180         }
18181       else
18182         {
18183           int set_float_abi_attributes = 0;
18184           switch (arm_fpu_arch)
18185             {
18186             case FPUTYPE_FPA:
18187               fpu_name = "fpa";
18188               break;
18189             case FPUTYPE_FPA_EMU2:
18190               fpu_name = "fpe2";
18191               break;
18192             case FPUTYPE_FPA_EMU3:
18193               fpu_name = "fpe3";
18194               break;
18195             case FPUTYPE_MAVERICK:
18196               fpu_name = "maverick";
18197               break;
18198             case FPUTYPE_VFP:
18199               fpu_name = "vfp";
18200               set_float_abi_attributes = 1;
18201               break;
18202             case FPUTYPE_VFP3D16:
18203               fpu_name = "vfpv3-d16";
18204               set_float_abi_attributes = 1;
18205               break;
18206             case FPUTYPE_VFP3:
18207               fpu_name = "vfpv3";
18208               set_float_abi_attributes = 1;
18209               break;
18210             case FPUTYPE_NEON:
18211               fpu_name = "neon";
18212               set_float_abi_attributes = 1;
18213               break;
18214             default:
18215               abort();
18216             }
18217           if (set_float_abi_attributes)
18218             {
18219               if (TARGET_HARD_FLOAT)
18220                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
18221               if (TARGET_HARD_FLOAT_ABI)
18222                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
18223             }
18224         }
18225       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
18226
18227       /* Some of these attributes only apply when the corresponding features
18228          are used.  However we don't have any easy way of figuring this out.
18229          Conservatively record the setting that would have been used.  */
18230
18231       /* Tag_ABI_FP_rounding.  */
18232       if (flag_rounding_math)
18233         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
18234       if (!flag_unsafe_math_optimizations)
18235         {
18236           /* Tag_ABI_FP_denomal.  */
18237           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
18238           /* Tag_ABI_FP_exceptions.  */
18239           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
18240         }
18241       /* Tag_ABI_FP_user_exceptions.  */
18242       if (flag_signaling_nans)
18243         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
18244       /* Tag_ABI_FP_number_model.  */
18245       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
18246                    flag_finite_math_only ? 1 : 3);
18247
18248       /* Tag_ABI_align8_needed.  */
18249       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
18250       /* Tag_ABI_align8_preserved.  */
18251       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
18252       /* Tag_ABI_enum_size.  */
18253       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
18254                    flag_short_enums ? 1 : 2);
18255
18256       /* Tag_ABI_optimization_goals.  */
18257       if (optimize_size)
18258         val = 4;
18259       else if (optimize >= 2)
18260         val = 2;
18261       else if (optimize)
18262         val = 1;
18263       else
18264         val = 6;
18265       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
18266
18267       if (arm_lang_output_object_attributes_hook)
18268         arm_lang_output_object_attributes_hook();
18269     }
18270   default_file_start();
18271 }
18272
18273 static void
18274 arm_file_end (void)
18275 {
18276   int regno;
18277
18278   if (NEED_INDICATE_EXEC_STACK)
18279     /* Add .note.GNU-stack.  */
18280     file_end_indicate_exec_stack ();
18281
18282   if (! thumb_call_reg_needed)
18283     return;
18284
18285   switch_to_section (text_section);
18286   asm_fprintf (asm_out_file, "\t.code 16\n");
18287   ASM_OUTPUT_ALIGN (asm_out_file, 1);
18288
18289   for (regno = 0; regno < LR_REGNUM; regno++)
18290     {
18291       rtx label = thumb_call_via_label[regno];
18292
18293       if (label != 0)
18294         {
18295           targetm.asm_out.internal_label (asm_out_file, "L",
18296                                           CODE_LABEL_NUMBER (label));
18297           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
18298         }
18299     }
18300 }
18301
18302 #ifndef ARM_PE
18303 /* Symbols in the text segment can be accessed without indirecting via the
18304    constant pool; it may take an extra binary operation, but this is still
18305    faster than indirecting via memory.  Don't do this when not optimizing,
18306    since we won't be calculating al of the offsets necessary to do this
18307    simplification.  */
18308
18309 static void
18310 arm_encode_section_info (tree decl, rtx rtl, int first)
18311 {
18312   if (optimize > 0 && TREE_CONSTANT (decl))
18313     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
18314
18315   default_encode_section_info (decl, rtl, first);
18316 }
18317 #endif /* !ARM_PE */
18318
18319 static void
18320 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
18321 {
18322   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
18323       && !strcmp (prefix, "L"))
18324     {
18325       arm_ccfsm_state = 0;
18326       arm_target_insn = NULL;
18327     }
18328   default_internal_label (stream, prefix, labelno);
18329 }
18330
18331 /* Output code to add DELTA to the first argument, and then jump
18332    to FUNCTION.  Used for C++ multiple inheritance.  */
18333 static void
18334 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
18335                      HOST_WIDE_INT delta,
18336                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
18337                      tree function)
18338 {
18339   static int thunk_label = 0;
18340   char label[256];
18341   char labelpc[256];
18342   int mi_delta = delta;
18343   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
18344   int shift = 0;
18345   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
18346                     ? 1 : 0);
18347   if (mi_delta < 0)
18348     mi_delta = - mi_delta;
18349
18350   if (TARGET_THUMB1)
18351     {
18352       int labelno = thunk_label++;
18353       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
18354       /* Thunks are entered in arm mode when avaiable.  */
18355       if (TARGET_THUMB1_ONLY)
18356         {
18357           /* push r3 so we can use it as a temporary.  */
18358           /* TODO: Omit this save if r3 is not used.  */
18359           fputs ("\tpush {r3}\n", file);
18360           fputs ("\tldr\tr3, ", file);
18361         }
18362       else
18363         {
18364           fputs ("\tldr\tr12, ", file);
18365         }
18366       assemble_name (file, label);
18367       fputc ('\n', file);
18368       if (flag_pic)
18369         {
18370           /* If we are generating PIC, the ldr instruction below loads
18371              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
18372              the address of the add + 8, so we have:
18373
18374              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
18375                  = target + 1.
18376
18377              Note that we have "+ 1" because some versions of GNU ld
18378              don't set the low bit of the result for R_ARM_REL32
18379              relocations against thumb function symbols.
18380              On ARMv6M this is +4, not +8.  */
18381           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
18382           assemble_name (file, labelpc);
18383           fputs (":\n", file);
18384           if (TARGET_THUMB1_ONLY)
18385             {
18386               /* This is 2 insns after the start of the thunk, so we know it
18387                  is 4-byte aligned.  */
18388               fputs ("\tadd\tr3, pc, r3\n", file);
18389               fputs ("\tmov r12, r3\n", file);
18390             }
18391           else
18392             fputs ("\tadd\tr12, pc, r12\n", file);
18393         }
18394       else if (TARGET_THUMB1_ONLY)
18395         fputs ("\tmov r12, r3\n", file);
18396     }
18397   if (TARGET_THUMB1_ONLY)
18398     {
18399       if (mi_delta > 255)
18400         {
18401           fputs ("\tldr\tr3, ", file);
18402           assemble_name (file, label);
18403           fputs ("+4\n", file);
18404           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
18405                        mi_op, this_regno, this_regno);
18406         }
18407       else if (mi_delta != 0)
18408         {
18409           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18410                        mi_op, this_regno, this_regno,
18411                        mi_delta);
18412         }
18413     }
18414   else
18415     {
18416       /* TODO: Use movw/movt for large constants when available.  */
18417       while (mi_delta != 0)
18418         {
18419           if ((mi_delta & (3 << shift)) == 0)
18420             shift += 2;
18421           else
18422             {
18423               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18424                            mi_op, this_regno, this_regno,
18425                            mi_delta & (0xff << shift));
18426               mi_delta &= ~(0xff << shift);
18427               shift += 8;
18428             }
18429         }
18430     }
18431   if (TARGET_THUMB1)
18432     {
18433       if (TARGET_THUMB1_ONLY)
18434         fputs ("\tpop\t{r3}\n", file);
18435
18436       fprintf (file, "\tbx\tr12\n");
18437       ASM_OUTPUT_ALIGN (file, 2);
18438       assemble_name (file, label);
18439       fputs (":\n", file);
18440       if (flag_pic)
18441         {
18442           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
18443           rtx tem = XEXP (DECL_RTL (function), 0);
18444           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
18445           tem = gen_rtx_MINUS (GET_MODE (tem),
18446                                tem,
18447                                gen_rtx_SYMBOL_REF (Pmode,
18448                                                    ggc_strdup (labelpc)));
18449           assemble_integer (tem, 4, BITS_PER_WORD, 1);
18450         }
18451       else
18452         /* Output ".word .LTHUNKn".  */
18453         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
18454
18455       if (TARGET_THUMB1_ONLY && mi_delta > 255)
18456         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
18457     }
18458   else
18459     {
18460       fputs ("\tb\t", file);
18461       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
18462       if (NEED_PLT_RELOC)
18463         fputs ("(PLT)", file);
18464       fputc ('\n', file);
18465     }
18466 }
18467
18468 int
18469 arm_emit_vector_const (FILE *file, rtx x)
18470 {
18471   int i;
18472   const char * pattern;
18473
18474   gcc_assert (GET_CODE (x) == CONST_VECTOR);
18475
18476   switch (GET_MODE (x))
18477     {
18478     case V2SImode: pattern = "%08x"; break;
18479     case V4HImode: pattern = "%04x"; break;
18480     case V8QImode: pattern = "%02x"; break;
18481     default:       gcc_unreachable ();
18482     }
18483
18484   fprintf (file, "0x");
18485   for (i = CONST_VECTOR_NUNITS (x); i--;)
18486     {
18487       rtx element;
18488
18489       element = CONST_VECTOR_ELT (x, i);
18490       fprintf (file, pattern, INTVAL (element));
18491     }
18492
18493   return 1;
18494 }
18495
18496 const char *
18497 arm_output_load_gr (rtx *operands)
18498 {
18499   rtx reg;
18500   rtx offset;
18501   rtx wcgr;
18502   rtx sum;
18503
18504   if (GET_CODE (operands [1]) != MEM
18505       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
18506       || GET_CODE (reg = XEXP (sum, 0)) != REG
18507       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
18508       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
18509     return "wldrw%?\t%0, %1";
18510
18511   /* Fix up an out-of-range load of a GR register.  */
18512   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
18513   wcgr = operands[0];
18514   operands[0] = reg;
18515   output_asm_insn ("ldr%?\t%0, %1", operands);
18516
18517   operands[0] = wcgr;
18518   operands[1] = reg;
18519   output_asm_insn ("tmcr%?\t%0, %1", operands);
18520   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
18521
18522   return "";
18523 }
18524
18525 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
18526
18527    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
18528    named arg and all anonymous args onto the stack.
18529    XXX I know the prologue shouldn't be pushing registers, but it is faster
18530    that way.  */
18531
18532 static void
18533 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
18534                             enum machine_mode mode,
18535                             tree type,
18536                             int *pretend_size,
18537                             int second_time ATTRIBUTE_UNUSED)
18538 {
18539   int nregs = cum->nregs;
18540   if (nregs & 1
18541       && ARM_DOUBLEWORD_ALIGN
18542       && arm_needs_doubleword_align (mode, type))
18543     nregs++;
18544
18545   cfun->machine->uses_anonymous_args = 1;
18546   if (nregs < NUM_ARG_REGS)
18547     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
18548 }
18549
18550 /* Return nonzero if the CONSUMER instruction (a store) does not need
18551    PRODUCER's value to calculate the address.  */
18552
18553 int
18554 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
18555 {
18556   rtx value = PATTERN (producer);
18557   rtx addr = PATTERN (consumer);
18558
18559   if (GET_CODE (value) == COND_EXEC)
18560     value = COND_EXEC_CODE (value);
18561   if (GET_CODE (value) == PARALLEL)
18562     value = XVECEXP (value, 0, 0);
18563   value = XEXP (value, 0);
18564   if (GET_CODE (addr) == COND_EXEC)
18565     addr = COND_EXEC_CODE (addr);
18566   if (GET_CODE (addr) == PARALLEL)
18567     addr = XVECEXP (addr, 0, 0);
18568   addr = XEXP (addr, 0);
18569
18570   return !reg_overlap_mentioned_p (value, addr);
18571 }
18572
18573 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
18574    have an early register shift value or amount dependency on the
18575    result of PRODUCER.  */
18576
18577 int
18578 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
18579 {
18580   rtx value = PATTERN (producer);
18581   rtx op = PATTERN (consumer);
18582   rtx early_op;
18583
18584   if (GET_CODE (value) == COND_EXEC)
18585     value = COND_EXEC_CODE (value);
18586   if (GET_CODE (value) == PARALLEL)
18587     value = XVECEXP (value, 0, 0);
18588   value = XEXP (value, 0);
18589   if (GET_CODE (op) == COND_EXEC)
18590     op = COND_EXEC_CODE (op);
18591   if (GET_CODE (op) == PARALLEL)
18592     op = XVECEXP (op, 0, 0);
18593   op = XEXP (op, 1);
18594
18595   early_op = XEXP (op, 0);
18596   /* This is either an actual independent shift, or a shift applied to
18597      the first operand of another operation.  We want the whole shift
18598      operation.  */
18599   if (GET_CODE (early_op) == REG)
18600     early_op = op;
18601
18602   return !reg_overlap_mentioned_p (value, early_op);
18603 }
18604
18605 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
18606    have an early register shift value dependency on the result of
18607    PRODUCER.  */
18608
18609 int
18610 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
18611 {
18612   rtx value = PATTERN (producer);
18613   rtx op = PATTERN (consumer);
18614   rtx early_op;
18615
18616   if (GET_CODE (value) == COND_EXEC)
18617     value = COND_EXEC_CODE (value);
18618   if (GET_CODE (value) == PARALLEL)
18619     value = XVECEXP (value, 0, 0);
18620   value = XEXP (value, 0);
18621   if (GET_CODE (op) == COND_EXEC)
18622     op = COND_EXEC_CODE (op);
18623   if (GET_CODE (op) == PARALLEL)
18624     op = XVECEXP (op, 0, 0);
18625   op = XEXP (op, 1);
18626
18627   early_op = XEXP (op, 0);
18628
18629   /* This is either an actual independent shift, or a shift applied to
18630      the first operand of another operation.  We want the value being
18631      shifted, in either case.  */
18632   if (GET_CODE (early_op) != REG)
18633     early_op = XEXP (early_op, 0);
18634
18635   return !reg_overlap_mentioned_p (value, early_op);
18636 }
18637
18638 /* Return nonzero if the CONSUMER (a mul or mac op) does not
18639    have an early register mult dependency on the result of
18640    PRODUCER.  */
18641
18642 int
18643 arm_no_early_mul_dep (rtx producer, rtx consumer)
18644 {
18645   rtx value = PATTERN (producer);
18646   rtx op = PATTERN (consumer);
18647
18648   if (GET_CODE (value) == COND_EXEC)
18649     value = COND_EXEC_CODE (value);
18650   if (GET_CODE (value) == PARALLEL)
18651     value = XVECEXP (value, 0, 0);
18652   value = XEXP (value, 0);
18653   if (GET_CODE (op) == COND_EXEC)
18654     op = COND_EXEC_CODE (op);
18655   if (GET_CODE (op) == PARALLEL)
18656     op = XVECEXP (op, 0, 0);
18657   op = XEXP (op, 1);
18658
18659   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
18660     {
18661       if (GET_CODE (XEXP (op, 0)) == MULT)
18662         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
18663       else
18664         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
18665     }
18666
18667   return 0;
18668 }
18669
18670 /* We can't rely on the caller doing the proper promotion when
18671    using APCS or ATPCS.  */
18672
18673 static bool
18674 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
18675 {
18676     return !TARGET_AAPCS_BASED;
18677 }
18678
18679
18680 /* AAPCS based ABIs use short enums by default.  */
18681
18682 static bool
18683 arm_default_short_enums (void)
18684 {
18685   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
18686 }
18687
18688
18689 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
18690
18691 static bool
18692 arm_align_anon_bitfield (void)
18693 {
18694   return TARGET_AAPCS_BASED;
18695 }
18696
18697
18698 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
18699
18700 static tree
18701 arm_cxx_guard_type (void)
18702 {
18703   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
18704 }
18705
18706 /* Return non-zero if the consumer (a multiply-accumulate instruction)
18707    has an accumulator dependency on the result of the producer (a
18708    multiplication instruction) and no other dependency on that result.  */
18709 int
18710 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
18711 {
18712   rtx mul = PATTERN (producer);
18713   rtx mac = PATTERN (consumer);
18714   rtx mul_result;
18715   rtx mac_op0, mac_op1, mac_acc;
18716
18717   if (GET_CODE (mul) == COND_EXEC)
18718     mul = COND_EXEC_CODE (mul);
18719   if (GET_CODE (mac) == COND_EXEC)
18720     mac = COND_EXEC_CODE (mac);
18721
18722   /* Check that mul is of the form (set (...) (mult ...))
18723      and mla is of the form (set (...) (plus (mult ...) (...))).  */
18724   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
18725       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
18726           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
18727     return 0;
18728
18729   mul_result = XEXP (mul, 0);
18730   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
18731   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
18732   mac_acc = XEXP (XEXP (mac, 1), 1);
18733
18734   return (reg_overlap_mentioned_p (mul_result, mac_acc)
18735           && !reg_overlap_mentioned_p (mul_result, mac_op0)
18736           && !reg_overlap_mentioned_p (mul_result, mac_op1));
18737 }
18738
18739
18740 /* The EABI says test the least significant bit of a guard variable.  */
18741
18742 static bool
18743 arm_cxx_guard_mask_bit (void)
18744 {
18745   return TARGET_AAPCS_BASED;
18746 }
18747
18748
18749 /* The EABI specifies that all array cookies are 8 bytes long.  */
18750
18751 static tree
18752 arm_get_cookie_size (tree type)
18753 {
18754   tree size;
18755
18756   if (!TARGET_AAPCS_BASED)
18757     return default_cxx_get_cookie_size (type);
18758
18759   size = build_int_cst (sizetype, 8);
18760   return size;
18761 }
18762
18763
18764 /* The EABI says that array cookies should also contain the element size.  */
18765
18766 static bool
18767 arm_cookie_has_size (void)
18768 {
18769   return TARGET_AAPCS_BASED;
18770 }
18771
18772
18773 /* The EABI says constructors and destructors should return a pointer to
18774    the object constructed/destroyed.  */
18775
18776 static bool
18777 arm_cxx_cdtor_returns_this (void)
18778 {
18779   return TARGET_AAPCS_BASED;
18780 }
18781
18782 /* The EABI says that an inline function may never be the key
18783    method.  */
18784
18785 static bool
18786 arm_cxx_key_method_may_be_inline (void)
18787 {
18788   return !TARGET_AAPCS_BASED;
18789 }
18790
18791 static void
18792 arm_cxx_determine_class_data_visibility (tree decl)
18793 {
18794   if (!TARGET_AAPCS_BASED
18795       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
18796     return;
18797
18798   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
18799      is exported.  However, on systems without dynamic vague linkage,
18800      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
18801   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
18802     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
18803   else
18804     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
18805   DECL_VISIBILITY_SPECIFIED (decl) = 1;
18806 }
18807
18808 static bool
18809 arm_cxx_class_data_always_comdat (void)
18810 {
18811   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
18812      vague linkage if the class has no key function.  */
18813   return !TARGET_AAPCS_BASED;
18814 }
18815
18816
18817 /* The EABI says __aeabi_atexit should be used to register static
18818    destructors.  */
18819
18820 static bool
18821 arm_cxx_use_aeabi_atexit (void)
18822 {
18823   return TARGET_AAPCS_BASED;
18824 }
18825
18826
18827 void
18828 arm_set_return_address (rtx source, rtx scratch)
18829 {
18830   arm_stack_offsets *offsets;
18831   HOST_WIDE_INT delta;
18832   rtx addr;
18833   unsigned long saved_regs;
18834
18835   offsets = arm_get_frame_offsets ();
18836   saved_regs = offsets->saved_regs_mask;
18837
18838   if ((saved_regs & (1 << LR_REGNUM)) == 0)
18839     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18840   else
18841     {
18842       if (frame_pointer_needed)
18843         addr = plus_constant(hard_frame_pointer_rtx, -4);
18844       else
18845         {
18846           /* LR will be the first saved register.  */
18847           delta = offsets->outgoing_args - (offsets->frame + 4);
18848
18849
18850           if (delta >= 4096)
18851             {
18852               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
18853                                      GEN_INT (delta & ~4095)));
18854               addr = scratch;
18855               delta &= 4095;
18856             }
18857           else
18858             addr = stack_pointer_rtx;
18859
18860           addr = plus_constant (addr, delta);
18861         }
18862       emit_move_insn (gen_frame_mem (Pmode, addr), source);
18863     }
18864 }
18865
18866
18867 void
18868 thumb_set_return_address (rtx source, rtx scratch)
18869 {
18870   arm_stack_offsets *offsets;
18871   HOST_WIDE_INT delta;
18872   HOST_WIDE_INT limit;
18873   int reg;
18874   rtx addr;
18875   unsigned long mask;
18876
18877   emit_use (source);
18878
18879   offsets = arm_get_frame_offsets ();
18880   mask = offsets->saved_regs_mask;
18881   if (mask & (1 << LR_REGNUM))
18882     {
18883       limit = 1024;
18884       /* Find the saved regs.  */
18885       if (frame_pointer_needed)
18886         {
18887           delta = offsets->soft_frame - offsets->saved_args;
18888           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
18889           if (TARGET_THUMB1)
18890             limit = 128;
18891         }
18892       else
18893         {
18894           delta = offsets->outgoing_args - offsets->saved_args;
18895           reg = SP_REGNUM;
18896         }
18897       /* Allow for the stack frame.  */
18898       if (TARGET_THUMB1 && TARGET_BACKTRACE)
18899         delta -= 16;
18900       /* The link register is always the first saved register.  */
18901       delta -= 4;
18902
18903       /* Construct the address.  */
18904       addr = gen_rtx_REG (SImode, reg);
18905       if (delta > limit)
18906         {
18907           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
18908           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
18909           addr = scratch;
18910         }
18911       else
18912         addr = plus_constant (addr, delta);
18913
18914       emit_move_insn (gen_frame_mem (Pmode, addr), source);
18915     }
18916   else
18917     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18918 }
18919
18920 /* Implements target hook vector_mode_supported_p.  */
18921 bool
18922 arm_vector_mode_supported_p (enum machine_mode mode)
18923 {
18924   /* Neon also supports V2SImode, etc. listed in the clause below.  */
18925   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
18926       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
18927     return true;
18928
18929   if ((mode == V2SImode)
18930       || (mode == V4HImode)
18931       || (mode == V8QImode))
18932     return true;
18933
18934   return false;
18935 }
18936
18937 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
18938    ARM insns and therefore guarantee that the shift count is modulo 256.
18939    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
18940    guarantee no particular behavior for out-of-range counts.  */
18941
18942 static unsigned HOST_WIDE_INT
18943 arm_shift_truncation_mask (enum machine_mode mode)
18944 {
18945   return mode == SImode ? 255 : 0;
18946 }
18947
18948
18949 /* Map internal gcc register numbers to DWARF2 register numbers.  */
18950
18951 unsigned int
18952 arm_dbx_register_number (unsigned int regno)
18953 {
18954   if (regno < 16)
18955     return regno;
18956
18957   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
18958      compatibility.  The EABI defines them as registers 96-103.  */
18959   if (IS_FPA_REGNUM (regno))
18960     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
18961
18962   /* FIXME: VFPv3 register numbering.  */
18963   if (IS_VFP_REGNUM (regno))
18964     return 64 + regno - FIRST_VFP_REGNUM;
18965
18966   if (IS_IWMMXT_GR_REGNUM (regno))
18967     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
18968
18969   if (IS_IWMMXT_REGNUM (regno))
18970     return 112 + regno - FIRST_IWMMXT_REGNUM;
18971
18972   gcc_unreachable ();
18973 }
18974
18975
18976 #ifdef TARGET_UNWIND_INFO
18977 /* Emit unwind directives for a store-multiple instruction or stack pointer
18978    push during alignment.
18979    These should only ever be generated by the function prologue code, so
18980    expect them to have a particular form.  */
18981
18982 static void
18983 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
18984 {
18985   int i;
18986   HOST_WIDE_INT offset;
18987   HOST_WIDE_INT nregs;
18988   int reg_size;
18989   unsigned reg;
18990   unsigned lastreg;
18991   rtx e;
18992
18993   e = XVECEXP (p, 0, 0);
18994   if (GET_CODE (e) != SET)
18995     abort ();
18996
18997   /* First insn will adjust the stack pointer.  */
18998   if (GET_CODE (e) != SET
18999       || GET_CODE (XEXP (e, 0)) != REG
19000       || REGNO (XEXP (e, 0)) != SP_REGNUM
19001       || GET_CODE (XEXP (e, 1)) != PLUS)
19002     abort ();
19003
19004   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
19005   nregs = XVECLEN (p, 0) - 1;
19006
19007   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
19008   if (reg < 16)
19009     {
19010       /* The function prologue may also push pc, but not annotate it as it is
19011          never restored.  We turn this into a stack pointer adjustment.  */
19012       if (nregs * 4 == offset - 4)
19013         {
19014           fprintf (asm_out_file, "\t.pad #4\n");
19015           offset -= 4;
19016         }
19017       reg_size = 4;
19018       fprintf (asm_out_file, "\t.save {");
19019     }
19020   else if (IS_VFP_REGNUM (reg))
19021     {
19022       reg_size = 8;
19023       fprintf (asm_out_file, "\t.vsave {");
19024     }
19025   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
19026     {
19027       /* FPA registers are done differently.  */
19028       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
19029       return;
19030     }
19031   else
19032     /* Unknown register type.  */
19033     abort ();
19034
19035   /* If the stack increment doesn't match the size of the saved registers,
19036      something has gone horribly wrong.  */
19037   if (offset != nregs * reg_size)
19038     abort ();
19039
19040   offset = 0;
19041   lastreg = 0;
19042   /* The remaining insns will describe the stores.  */
19043   for (i = 1; i <= nregs; i++)
19044     {
19045       /* Expect (set (mem <addr>) (reg)).
19046          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
19047       e = XVECEXP (p, 0, i);
19048       if (GET_CODE (e) != SET
19049           || GET_CODE (XEXP (e, 0)) != MEM
19050           || GET_CODE (XEXP (e, 1)) != REG)
19051         abort ();
19052
19053       reg = REGNO (XEXP (e, 1));
19054       if (reg < lastreg)
19055         abort ();
19056
19057       if (i != 1)
19058         fprintf (asm_out_file, ", ");
19059       /* We can't use %r for vfp because we need to use the
19060          double precision register names.  */
19061       if (IS_VFP_REGNUM (reg))
19062         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
19063       else
19064         asm_fprintf (asm_out_file, "%r", reg);
19065
19066 #ifdef ENABLE_CHECKING
19067       /* Check that the addresses are consecutive.  */
19068       e = XEXP (XEXP (e, 0), 0);
19069       if (GET_CODE (e) == PLUS)
19070         {
19071           offset += reg_size;
19072           if (GET_CODE (XEXP (e, 0)) != REG
19073               || REGNO (XEXP (e, 0)) != SP_REGNUM
19074               || GET_CODE (XEXP (e, 1)) != CONST_INT
19075               || offset != INTVAL (XEXP (e, 1)))
19076             abort ();
19077         }
19078       else if (i != 1
19079                || GET_CODE (e) != REG
19080                || REGNO (e) != SP_REGNUM)
19081         abort ();
19082 #endif
19083     }
19084   fprintf (asm_out_file, "}\n");
19085 }
19086
19087 /*  Emit unwind directives for a SET.  */
19088
19089 static void
19090 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
19091 {
19092   rtx e0;
19093   rtx e1;
19094   unsigned reg;
19095
19096   e0 = XEXP (p, 0);
19097   e1 = XEXP (p, 1);
19098   switch (GET_CODE (e0))
19099     {
19100     case MEM:
19101       /* Pushing a single register.  */
19102       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
19103           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
19104           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
19105         abort ();
19106
19107       asm_fprintf (asm_out_file, "\t.save ");
19108       if (IS_VFP_REGNUM (REGNO (e1)))
19109         asm_fprintf(asm_out_file, "{d%d}\n",
19110                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
19111       else
19112         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
19113       break;
19114
19115     case REG:
19116       if (REGNO (e0) == SP_REGNUM)
19117         {
19118           /* A stack increment.  */
19119           if (GET_CODE (e1) != PLUS
19120               || GET_CODE (XEXP (e1, 0)) != REG
19121               || REGNO (XEXP (e1, 0)) != SP_REGNUM
19122               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
19123             abort ();
19124
19125           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
19126                        -INTVAL (XEXP (e1, 1)));
19127         }
19128       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
19129         {
19130           HOST_WIDE_INT offset;
19131
19132           if (GET_CODE (e1) == PLUS)
19133             {
19134               if (GET_CODE (XEXP (e1, 0)) != REG
19135                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
19136                 abort ();
19137               reg = REGNO (XEXP (e1, 0));
19138               offset = INTVAL (XEXP (e1, 1));
19139               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
19140                            HARD_FRAME_POINTER_REGNUM, reg,
19141                            INTVAL (XEXP (e1, 1)));
19142             }
19143           else if (GET_CODE (e1) == REG)
19144             {
19145               reg = REGNO (e1);
19146               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
19147                            HARD_FRAME_POINTER_REGNUM, reg);
19148             }
19149           else
19150             abort ();
19151         }
19152       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
19153         {
19154           /* Move from sp to reg.  */
19155           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
19156         }
19157      else if (GET_CODE (e1) == PLUS
19158               && GET_CODE (XEXP (e1, 0)) == REG
19159               && REGNO (XEXP (e1, 0)) == SP_REGNUM
19160               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
19161         {
19162           /* Set reg to offset from sp.  */
19163           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
19164                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
19165         }
19166       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
19167         {
19168           /* Stack pointer save before alignment.  */
19169           reg = REGNO (e0);
19170           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
19171                        reg + 0x90, reg);
19172         }
19173       else
19174         abort ();
19175       break;
19176
19177     default:
19178       abort ();
19179     }
19180 }
19181
19182
19183 /* Emit unwind directives for the given insn.  */
19184
19185 static void
19186 arm_unwind_emit (FILE * asm_out_file, rtx insn)
19187 {
19188   rtx pat;
19189
19190   if (!ARM_EABI_UNWIND_TABLES)
19191     return;
19192
19193   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
19194       && (TREE_NOTHROW (current_function_decl)
19195           || crtl->all_throwers_are_sibcalls))
19196     return;
19197
19198   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
19199     return;
19200
19201   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
19202   if (pat)
19203     pat = XEXP (pat, 0);
19204   else
19205     pat = PATTERN (insn);
19206
19207   switch (GET_CODE (pat))
19208     {
19209     case SET:
19210       arm_unwind_emit_set (asm_out_file, pat);
19211       break;
19212
19213     case SEQUENCE:
19214       /* Store multiple.  */
19215       arm_unwind_emit_sequence (asm_out_file, pat);
19216       break;
19217
19218     default:
19219       abort();
19220     }
19221 }
19222
19223
19224 /* Output a reference from a function exception table to the type_info
19225    object X.  The EABI specifies that the symbol should be relocated by
19226    an R_ARM_TARGET2 relocation.  */
19227
19228 static bool
19229 arm_output_ttype (rtx x)
19230 {
19231   fputs ("\t.word\t", asm_out_file);
19232   output_addr_const (asm_out_file, x);
19233   /* Use special relocations for symbol references.  */
19234   if (GET_CODE (x) != CONST_INT)
19235     fputs ("(TARGET2)", asm_out_file);
19236   fputc ('\n', asm_out_file);
19237
19238   return TRUE;
19239 }
19240 #endif /* TARGET_UNWIND_INFO */
19241
19242
19243 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
19244    stack alignment.  */
19245
19246 static void
19247 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
19248 {
19249   rtx unspec = SET_SRC (pattern);
19250   gcc_assert (GET_CODE (unspec) == UNSPEC);
19251
19252   switch (index)
19253     {
19254     case UNSPEC_STACK_ALIGN:
19255       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
19256          put anything on the stack, so hopefully it won't matter.
19257          CFA = SP will be correct after alignment.  */
19258       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
19259                               SET_DEST (pattern));
19260       break;
19261     default:
19262       gcc_unreachable ();
19263     }
19264 }
19265
19266
19267 /* Output unwind directives for the start/end of a function.  */
19268
19269 void
19270 arm_output_fn_unwind (FILE * f, bool prologue)
19271 {
19272   if (!ARM_EABI_UNWIND_TABLES)
19273     return;
19274
19275   if (prologue)
19276     fputs ("\t.fnstart\n", f);
19277   else
19278     {
19279       /* If this function will never be unwound, then mark it as such.
19280          The came condition is used in arm_unwind_emit to suppress
19281          the frame annotations.  */
19282       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
19283           && (TREE_NOTHROW (current_function_decl)
19284               || crtl->all_throwers_are_sibcalls))
19285         fputs("\t.cantunwind\n", f);
19286
19287       fputs ("\t.fnend\n", f);
19288     }
19289 }
19290
19291 static bool
19292 arm_emit_tls_decoration (FILE *fp, rtx x)
19293 {
19294   enum tls_reloc reloc;
19295   rtx val;
19296
19297   val = XVECEXP (x, 0, 0);
19298   reloc = INTVAL (XVECEXP (x, 0, 1));
19299
19300   output_addr_const (fp, val);
19301
19302   switch (reloc)
19303     {
19304     case TLS_GD32:
19305       fputs ("(tlsgd)", fp);
19306       break;
19307     case TLS_LDM32:
19308       fputs ("(tlsldm)", fp);
19309       break;
19310     case TLS_LDO32:
19311       fputs ("(tlsldo)", fp);
19312       break;
19313     case TLS_IE32:
19314       fputs ("(gottpoff)", fp);
19315       break;
19316     case TLS_LE32:
19317       fputs ("(tpoff)", fp);
19318       break;
19319     default:
19320       gcc_unreachable ();
19321     }
19322
19323   switch (reloc)
19324     {
19325     case TLS_GD32:
19326     case TLS_LDM32:
19327     case TLS_IE32:
19328       fputs (" + (. - ", fp);
19329       output_addr_const (fp, XVECEXP (x, 0, 2));
19330       fputs (" - ", fp);
19331       output_addr_const (fp, XVECEXP (x, 0, 3));
19332       fputc (')', fp);
19333       break;
19334     default:
19335       break;
19336     }
19337
19338   return TRUE;
19339 }
19340
19341 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
19342
19343 static void
19344 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
19345 {
19346   gcc_assert (size == 4);
19347   fputs ("\t.word\t", file);
19348   output_addr_const (file, x);
19349   fputs ("(tlsldo)", file);
19350 }
19351
19352 bool
19353 arm_output_addr_const_extra (FILE *fp, rtx x)
19354 {
19355   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
19356     return arm_emit_tls_decoration (fp, x);
19357   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
19358     {
19359       char label[256];
19360       int labelno = INTVAL (XVECEXP (x, 0, 0));
19361
19362       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
19363       assemble_name_raw (fp, label);
19364
19365       return TRUE;
19366     }
19367   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
19368     {
19369       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
19370       if (GOT_PCREL)
19371         fputs ("+.", fp);
19372       fputs ("-(", fp);
19373       output_addr_const (fp, XVECEXP (x, 0, 0));
19374       fputc (')', fp);
19375       return TRUE;
19376     }
19377   else if (GET_CODE (x) == CONST_VECTOR)
19378     return arm_emit_vector_const (fp, x);
19379
19380   return FALSE;
19381 }
19382
19383 /* Output assembly for a shift instruction.
19384    SET_FLAGS determines how the instruction modifies the condition codes.
19385    0 - Do not set condition codes.
19386    1 - Set condition codes.
19387    2 - Use smallest instruction.  */
19388 const char *
19389 arm_output_shift(rtx * operands, int set_flags)
19390 {
19391   char pattern[100];
19392   static const char flag_chars[3] = {'?', '.', '!'};
19393   const char *shift;
19394   HOST_WIDE_INT val;
19395   char c;
19396   
19397   c = flag_chars[set_flags];
19398   if (TARGET_UNIFIED_ASM)
19399     {
19400       shift = shift_op(operands[3], &val);
19401       if (shift)
19402         {
19403           if (val != -1)
19404             operands[2] = GEN_INT(val);
19405           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
19406         }
19407       else
19408         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
19409     }
19410   else
19411     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
19412   output_asm_insn (pattern, operands);
19413   return "";
19414 }
19415
19416 /* Output a Thumb-2 casesi instruction.  */
19417 const char *
19418 thumb2_output_casesi (rtx *operands)
19419 {
19420   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
19421
19422   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
19423
19424   output_asm_insn ("cmp\t%0, %1", operands);
19425   output_asm_insn ("bhi\t%l3", operands);
19426   switch (GET_MODE(diff_vec))
19427     {
19428     case QImode:
19429       return "tbb\t[%|pc, %0]";
19430     case HImode:
19431       return "tbh\t[%|pc, %0, lsl #1]";
19432     case SImode:
19433       if (flag_pic)
19434         {
19435           output_asm_insn ("adr\t%4, %l2", operands);
19436           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
19437           output_asm_insn ("add\t%4, %4, %5", operands);
19438           return "bx\t%4";
19439         }
19440       else
19441         {
19442           output_asm_insn ("adr\t%4, %l2", operands);
19443           return "ldr\t%|pc, [%4, %0, lsl #2]";
19444         }
19445     default:
19446       gcc_unreachable ();
19447     }
19448 }
19449
19450 /* Most ARM cores are single issue, but some newer ones can dual issue.
19451    The scheduler descriptions rely on this being correct.  */
19452 static int
19453 arm_issue_rate (void)
19454 {
19455   switch (arm_tune)
19456     {
19457     case cortexr4:
19458     case cortexr4f:
19459     case cortexa8:
19460     case cortexa9:
19461       return 2;
19462
19463     default:
19464       return 1;
19465     }
19466 }
19467
19468 /* A table and a function to perform ARM-specific name mangling for
19469    NEON vector types in order to conform to the AAPCS (see "Procedure
19470    Call Standard for the ARM Architecture", Appendix A).  To qualify
19471    for emission with the mangled names defined in that document, a
19472    vector type must not only be of the correct mode but also be
19473    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
19474 typedef struct
19475 {
19476   enum machine_mode mode;
19477   const char *element_type_name;
19478   const char *aapcs_name;
19479 } arm_mangle_map_entry;
19480
19481 static arm_mangle_map_entry arm_mangle_map[] = {
19482   /* 64-bit containerized types.  */
19483   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
19484   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
19485   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
19486   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
19487   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
19488   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
19489   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
19490   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
19491   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
19492   /* 128-bit containerized types.  */
19493   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
19494   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
19495   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
19496   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
19497   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
19498   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
19499   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
19500   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
19501   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
19502   { VOIDmode, NULL, NULL }
19503 };
19504
19505 const char *
19506 arm_mangle_type (const_tree type)
19507 {
19508   arm_mangle_map_entry *pos = arm_mangle_map;
19509
19510   if (TREE_CODE (type) != VECTOR_TYPE)
19511     return NULL;
19512
19513   /* Check the mode of the vector type, and the name of the vector
19514      element type, against the table.  */
19515   while (pos->mode != VOIDmode)
19516     {
19517       tree elt_type = TREE_TYPE (type);
19518
19519       if (pos->mode == TYPE_MODE (type)
19520           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
19521           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
19522                       pos->element_type_name))
19523         return pos->aapcs_name;
19524
19525       pos++;
19526     }
19527
19528   /* Use the default mangling for unrecognized (possibly user-defined)
19529      vector types.  */
19530   return NULL;
19531 }
19532
19533 /* Order of allocation of core registers for Thumb: this allocation is
19534    written over the corresponding initial entries of the array
19535    initialized with REG_ALLOC_ORDER.  We allocate all low registers
19536    first.  Saving and restoring a low register is usually cheaper than
19537    using a call-clobbered high register.  */
19538
19539 static const int thumb_core_reg_alloc_order[] =
19540 {
19541    3,  2,  1,  0,  4,  5,  6,  7,
19542   14, 12,  8,  9, 10, 11, 13, 15
19543 };
19544
19545 /* Adjust register allocation order when compiling for Thumb.  */
19546
19547 void
19548 arm_order_regs_for_local_alloc (void)
19549 {
19550   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
19551   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
19552   if (TARGET_THUMB)
19553     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
19554             sizeof (thumb_core_reg_alloc_order));
19555 }
19556
19557 /* Set default optimization options.  */
19558 void
19559 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
19560 {
19561   /* Enable section anchors by default at -O1 or higher.
19562      Use 2 to distinguish from an explicit -fsection-anchors
19563      given on the command line.  */
19564   if (level > 0)
19565     flag_section_anchors = 2;
19566 }
19567
19568 #include "gt-arm.h"