OSDN Git Service

PR target/38695
[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 int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
130 static bool arm_size_rtx_costs (rtx, int, int, int *);
131 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
132 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
133 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
134 static bool arm_9e_rtx_costs (rtx, int, int, int *);
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, int, int, int *);
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       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1887
1888     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1889     case XOR:
1890     case IOR:
1891       return 0;
1892
1893     case AND:
1894       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1895
1896     default:
1897       gcc_unreachable ();
1898     }
1899 }
1900
1901 /* Emit a sequence of insns to handle a large constant.
1902    CODE is the code of the operation required, it can be any of SET, PLUS,
1903    IOR, AND, XOR, MINUS;
1904    MODE is the mode in which the operation is being performed;
1905    VAL is the integer to operate on;
1906    SOURCE is the other operand (a register, or a null-pointer for SET);
1907    SUBTARGETS means it is safe to create scratch registers if that will
1908    either produce a simpler sequence, or we will want to cse the values.
1909    Return value is the number of insns emitted.  */
1910
1911 /* ??? Tweak this for thumb2.  */
1912 int
1913 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1914                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1915 {
1916   rtx cond;
1917
1918   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1919     cond = COND_EXEC_TEST (PATTERN (insn));
1920   else
1921     cond = NULL_RTX;
1922
1923   if (subtargets || code == SET
1924       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1925           && REGNO (target) != REGNO (source)))
1926     {
1927       /* After arm_reorg has been called, we can't fix up expensive
1928          constants by pushing them into memory so we must synthesize
1929          them in-line, regardless of the cost.  This is only likely to
1930          be more costly on chips that have load delay slots and we are
1931          compiling without running the scheduler (so no splitting
1932          occurred before the final instruction emission).
1933
1934          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1935       */
1936       if (!after_arm_reorg
1937           && !cond
1938           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1939                                 1, 0)
1940               > arm_constant_limit + (code != SET)))
1941         {
1942           if (code == SET)
1943             {
1944               /* Currently SET is the only monadic value for CODE, all
1945                  the rest are diadic.  */
1946               if (TARGET_USE_MOVT)
1947                 arm_emit_movpair (target, GEN_INT (val));
1948               else
1949                 emit_set_insn (target, GEN_INT (val));
1950
1951               return 1;
1952             }
1953           else
1954             {
1955               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1956
1957               if (TARGET_USE_MOVT)
1958                 arm_emit_movpair (temp, GEN_INT (val));
1959               else
1960                 emit_set_insn (temp, GEN_INT (val));
1961
1962               /* For MINUS, the value is subtracted from, since we never
1963                  have subtraction of a constant.  */
1964               if (code == MINUS)
1965                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1966               else
1967                 emit_set_insn (target,
1968                                gen_rtx_fmt_ee (code, mode, source, temp));
1969               return 2;
1970             }
1971         }
1972     }
1973
1974   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1975                            1);
1976 }
1977
1978 /* Return the number of ARM instructions required to synthesize the given
1979    constant.  */
1980 static int
1981 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1982 {
1983   HOST_WIDE_INT temp1;
1984   int num_insns = 0;
1985   do
1986     {
1987       int end;
1988
1989       if (i <= 0)
1990         i += 32;
1991       if (remainder & (3 << (i - 2)))
1992         {
1993           end = i - 8;
1994           if (end < 0)
1995             end += 32;
1996           temp1 = remainder & ((0x0ff << end)
1997                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1998           remainder &= ~temp1;
1999           num_insns++;
2000           i -= 6;
2001         }
2002       i -= 2;
2003     } while (remainder);
2004   return num_insns;
2005 }
2006
2007 /* Emit an instruction with the indicated PATTERN.  If COND is
2008    non-NULL, conditionalize the execution of the instruction on COND
2009    being true.  */
2010
2011 static void
2012 emit_constant_insn (rtx cond, rtx pattern)
2013 {
2014   if (cond)
2015     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
2016   emit_insn (pattern);
2017 }
2018
2019 /* As above, but extra parameter GENERATE which, if clear, suppresses
2020    RTL generation.  */
2021 /* ??? This needs more work for thumb2.  */
2022
2023 static int
2024 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
2025                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
2026                   int generate)
2027 {
2028   int can_invert = 0;
2029   int can_negate = 0;
2030   int can_negate_initial = 0;
2031   int can_shift = 0;
2032   int i;
2033   int num_bits_set = 0;
2034   int set_sign_bit_copies = 0;
2035   int clear_sign_bit_copies = 0;
2036   int clear_zero_bit_copies = 0;
2037   int set_zero_bit_copies = 0;
2038   int insns = 0;
2039   unsigned HOST_WIDE_INT temp1, temp2;
2040   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
2041
2042   /* Find out which operations are safe for a given CODE.  Also do a quick
2043      check for degenerate cases; these can occur when DImode operations
2044      are split.  */
2045   switch (code)
2046     {
2047     case SET:
2048       can_invert = 1;
2049       can_shift = 1;
2050       can_negate = 1;
2051       break;
2052
2053     case PLUS:
2054       can_negate = 1;
2055       can_negate_initial = 1;
2056       break;
2057
2058     case IOR:
2059       if (remainder == 0xffffffff)
2060         {
2061           if (generate)
2062             emit_constant_insn (cond,
2063                                 gen_rtx_SET (VOIDmode, target,
2064                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2065           return 1;
2066         }
2067       if (remainder == 0)
2068         {
2069           if (reload_completed && rtx_equal_p (target, source))
2070             return 0;
2071           if (generate)
2072             emit_constant_insn (cond,
2073                                 gen_rtx_SET (VOIDmode, target, source));
2074           return 1;
2075         }
2076       break;
2077
2078     case AND:
2079       if (remainder == 0)
2080         {
2081           if (generate)
2082             emit_constant_insn (cond,
2083                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2084           return 1;
2085         }
2086       if (remainder == 0xffffffff)
2087         {
2088           if (reload_completed && rtx_equal_p (target, source))
2089             return 0;
2090           if (generate)
2091             emit_constant_insn (cond,
2092                                 gen_rtx_SET (VOIDmode, target, source));
2093           return 1;
2094         }
2095       can_invert = 1;
2096       break;
2097
2098     case XOR:
2099       if (remainder == 0)
2100         {
2101           if (reload_completed && rtx_equal_p (target, source))
2102             return 0;
2103           if (generate)
2104             emit_constant_insn (cond,
2105                                 gen_rtx_SET (VOIDmode, target, source));
2106           return 1;
2107         }
2108
2109       /* We don't know how to handle other cases yet.  */
2110       gcc_assert (remainder == 0xffffffff);
2111
2112       if (generate)
2113         emit_constant_insn (cond,
2114                             gen_rtx_SET (VOIDmode, target,
2115                                          gen_rtx_NOT (mode, source)));
2116       return 1;
2117
2118     case MINUS:
2119       /* We treat MINUS as (val - source), since (source - val) is always
2120          passed as (source + (-val)).  */
2121       if (remainder == 0)
2122         {
2123           if (generate)
2124             emit_constant_insn (cond,
2125                                 gen_rtx_SET (VOIDmode, target,
2126                                              gen_rtx_NEG (mode, source)));
2127           return 1;
2128         }
2129       if (const_ok_for_arm (val))
2130         {
2131           if (generate)
2132             emit_constant_insn (cond,
2133                                 gen_rtx_SET (VOIDmode, target,
2134                                              gen_rtx_MINUS (mode, GEN_INT (val),
2135                                                             source)));
2136           return 1;
2137         }
2138       can_negate = 1;
2139
2140       break;
2141
2142     default:
2143       gcc_unreachable ();
2144     }
2145
2146   /* If we can do it in one insn get out quickly.  */
2147   if (const_ok_for_arm (val)
2148       || (can_negate_initial && const_ok_for_arm (-val))
2149       || (can_invert && const_ok_for_arm (~val)))
2150     {
2151       if (generate)
2152         emit_constant_insn (cond,
2153                             gen_rtx_SET (VOIDmode, target,
2154                                          (source
2155                                           ? gen_rtx_fmt_ee (code, mode, source,
2156                                                             GEN_INT (val))
2157                                           : GEN_INT (val))));
2158       return 1;
2159     }
2160
2161   /* Calculate a few attributes that may be useful for specific
2162      optimizations.  */
2163   for (i = 31; i >= 0; i--)
2164     {
2165       if ((remainder & (1 << i)) == 0)
2166         clear_sign_bit_copies++;
2167       else
2168         break;
2169     }
2170
2171   for (i = 31; i >= 0; i--)
2172     {
2173       if ((remainder & (1 << i)) != 0)
2174         set_sign_bit_copies++;
2175       else
2176         break;
2177     }
2178
2179   for (i = 0; i <= 31; i++)
2180     {
2181       if ((remainder & (1 << i)) == 0)
2182         clear_zero_bit_copies++;
2183       else
2184         break;
2185     }
2186
2187   for (i = 0; i <= 31; i++)
2188     {
2189       if ((remainder & (1 << i)) != 0)
2190         set_zero_bit_copies++;
2191       else
2192         break;
2193     }
2194
2195   switch (code)
2196     {
2197     case SET:
2198       /* See if we can use movw.  */
2199       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2200         {
2201           if (generate)
2202             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2203                                                    GEN_INT (val)));
2204           return 1;
2205         }
2206
2207       /* See if we can do this by sign_extending a constant that is known
2208          to be negative.  This is a good, way of doing it, since the shift
2209          may well merge into a subsequent insn.  */
2210       if (set_sign_bit_copies > 1)
2211         {
2212           if (const_ok_for_arm
2213               (temp1 = ARM_SIGN_EXTEND (remainder
2214                                         << (set_sign_bit_copies - 1))))
2215             {
2216               if (generate)
2217                 {
2218                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2219                   emit_constant_insn (cond,
2220                                       gen_rtx_SET (VOIDmode, new_src,
2221                                                    GEN_INT (temp1)));
2222                   emit_constant_insn (cond,
2223                                       gen_ashrsi3 (target, new_src,
2224                                                    GEN_INT (set_sign_bit_copies - 1)));
2225                 }
2226               return 2;
2227             }
2228           /* For an inverted constant, we will need to set the low bits,
2229              these will be shifted out of harm's way.  */
2230           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2231           if (const_ok_for_arm (~temp1))
2232             {
2233               if (generate)
2234                 {
2235                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2236                   emit_constant_insn (cond,
2237                                       gen_rtx_SET (VOIDmode, new_src,
2238                                                    GEN_INT (temp1)));
2239                   emit_constant_insn (cond,
2240                                       gen_ashrsi3 (target, new_src,
2241                                                    GEN_INT (set_sign_bit_copies - 1)));
2242                 }
2243               return 2;
2244             }
2245         }
2246
2247       /* See if we can calculate the value as the difference between two
2248          valid immediates.  */
2249       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2250         {
2251           int topshift = clear_sign_bit_copies & ~1;
2252
2253           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2254                                    & (0xff000000 >> topshift));
2255
2256           /* If temp1 is zero, then that means the 9 most significant
2257              bits of remainder were 1 and we've caused it to overflow.
2258              When topshift is 0 we don't need to do anything since we
2259              can borrow from 'bit 32'.  */
2260           if (temp1 == 0 && topshift != 0)
2261             temp1 = 0x80000000 >> (topshift - 1);
2262
2263           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2264
2265           if (const_ok_for_arm (temp2))
2266             {
2267               if (generate)
2268                 {
2269                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2270                   emit_constant_insn (cond,
2271                                       gen_rtx_SET (VOIDmode, new_src,
2272                                                    GEN_INT (temp1)));
2273                   emit_constant_insn (cond,
2274                                       gen_addsi3 (target, new_src,
2275                                                   GEN_INT (-temp2)));
2276                 }
2277
2278               return 2;
2279             }
2280         }
2281
2282       /* See if we can generate this by setting the bottom (or the top)
2283          16 bits, and then shifting these into the other half of the
2284          word.  We only look for the simplest cases, to do more would cost
2285          too much.  Be careful, however, not to generate this when the
2286          alternative would take fewer insns.  */
2287       if (val & 0xffff0000)
2288         {
2289           temp1 = remainder & 0xffff0000;
2290           temp2 = remainder & 0x0000ffff;
2291
2292           /* Overlaps outside this range are best done using other methods.  */
2293           for (i = 9; i < 24; i++)
2294             {
2295               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2296                   && !const_ok_for_arm (temp2))
2297                 {
2298                   rtx new_src = (subtargets
2299                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2300                                  : target);
2301                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2302                                             source, subtargets, generate);
2303                   source = new_src;
2304                   if (generate)
2305                     emit_constant_insn
2306                       (cond,
2307                        gen_rtx_SET
2308                        (VOIDmode, target,
2309                         gen_rtx_IOR (mode,
2310                                      gen_rtx_ASHIFT (mode, source,
2311                                                      GEN_INT (i)),
2312                                      source)));
2313                   return insns + 1;
2314                 }
2315             }
2316
2317           /* Don't duplicate cases already considered.  */
2318           for (i = 17; i < 24; i++)
2319             {
2320               if (((temp1 | (temp1 >> i)) == remainder)
2321                   && !const_ok_for_arm (temp1))
2322                 {
2323                   rtx new_src = (subtargets
2324                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2325                                  : target);
2326                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2327                                             source, subtargets, generate);
2328                   source = new_src;
2329                   if (generate)
2330                     emit_constant_insn
2331                       (cond,
2332                        gen_rtx_SET (VOIDmode, target,
2333                                     gen_rtx_IOR
2334                                     (mode,
2335                                      gen_rtx_LSHIFTRT (mode, source,
2336                                                        GEN_INT (i)),
2337                                      source)));
2338                   return insns + 1;
2339                 }
2340             }
2341         }
2342       break;
2343
2344     case IOR:
2345     case XOR:
2346       /* If we have IOR or XOR, and the constant can be loaded in a
2347          single instruction, and we can find a temporary to put it in,
2348          then this can be done in two instructions instead of 3-4.  */
2349       if (subtargets
2350           /* TARGET can't be NULL if SUBTARGETS is 0 */
2351           || (reload_completed && !reg_mentioned_p (target, source)))
2352         {
2353           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2354             {
2355               if (generate)
2356                 {
2357                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2358
2359                   emit_constant_insn (cond,
2360                                       gen_rtx_SET (VOIDmode, sub,
2361                                                    GEN_INT (val)));
2362                   emit_constant_insn (cond,
2363                                       gen_rtx_SET (VOIDmode, target,
2364                                                    gen_rtx_fmt_ee (code, mode,
2365                                                                    source, sub)));
2366                 }
2367               return 2;
2368             }
2369         }
2370
2371       if (code == XOR)
2372         break;
2373
2374       if (set_sign_bit_copies > 8
2375           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2376         {
2377           if (generate)
2378             {
2379               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2380               rtx shift = GEN_INT (set_sign_bit_copies);
2381
2382               emit_constant_insn
2383                 (cond,
2384                  gen_rtx_SET (VOIDmode, sub,
2385                               gen_rtx_NOT (mode,
2386                                            gen_rtx_ASHIFT (mode,
2387                                                            source,
2388                                                            shift))));
2389               emit_constant_insn
2390                 (cond,
2391                  gen_rtx_SET (VOIDmode, target,
2392                               gen_rtx_NOT (mode,
2393                                            gen_rtx_LSHIFTRT (mode, sub,
2394                                                              shift))));
2395             }
2396           return 2;
2397         }
2398
2399       if (set_zero_bit_copies > 8
2400           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2401         {
2402           if (generate)
2403             {
2404               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2405               rtx shift = GEN_INT (set_zero_bit_copies);
2406
2407               emit_constant_insn
2408                 (cond,
2409                  gen_rtx_SET (VOIDmode, sub,
2410                               gen_rtx_NOT (mode,
2411                                            gen_rtx_LSHIFTRT (mode,
2412                                                              source,
2413                                                              shift))));
2414               emit_constant_insn
2415                 (cond,
2416                  gen_rtx_SET (VOIDmode, target,
2417                               gen_rtx_NOT (mode,
2418                                            gen_rtx_ASHIFT (mode, sub,
2419                                                            shift))));
2420             }
2421           return 2;
2422         }
2423
2424       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2425         {
2426           if (generate)
2427             {
2428               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2429               emit_constant_insn (cond,
2430                                   gen_rtx_SET (VOIDmode, sub,
2431                                                gen_rtx_NOT (mode, source)));
2432               source = sub;
2433               if (subtargets)
2434                 sub = gen_reg_rtx (mode);
2435               emit_constant_insn (cond,
2436                                   gen_rtx_SET (VOIDmode, sub,
2437                                                gen_rtx_AND (mode, source,
2438                                                             GEN_INT (temp1))));
2439               emit_constant_insn (cond,
2440                                   gen_rtx_SET (VOIDmode, target,
2441                                                gen_rtx_NOT (mode, sub)));
2442             }
2443           return 3;
2444         }
2445       break;
2446
2447     case AND:
2448       /* See if two shifts will do 2 or more insn's worth of work.  */
2449       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2450         {
2451           HOST_WIDE_INT shift_mask = ((0xffffffff
2452                                        << (32 - clear_sign_bit_copies))
2453                                       & 0xffffffff);
2454
2455           if ((remainder | shift_mask) != 0xffffffff)
2456             {
2457               if (generate)
2458                 {
2459                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2460                   insns = arm_gen_constant (AND, mode, cond,
2461                                             remainder | shift_mask,
2462                                             new_src, source, subtargets, 1);
2463                   source = new_src;
2464                 }
2465               else
2466                 {
2467                   rtx targ = subtargets ? NULL_RTX : target;
2468                   insns = arm_gen_constant (AND, mode, cond,
2469                                             remainder | shift_mask,
2470                                             targ, source, subtargets, 0);
2471                 }
2472             }
2473
2474           if (generate)
2475             {
2476               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2477               rtx shift = GEN_INT (clear_sign_bit_copies);
2478
2479               emit_insn (gen_ashlsi3 (new_src, source, shift));
2480               emit_insn (gen_lshrsi3 (target, new_src, shift));
2481             }
2482
2483           return insns + 2;
2484         }
2485
2486       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2487         {
2488           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2489
2490           if ((remainder | shift_mask) != 0xffffffff)
2491             {
2492               if (generate)
2493                 {
2494                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2495
2496                   insns = arm_gen_constant (AND, mode, cond,
2497                                             remainder | shift_mask,
2498                                             new_src, source, subtargets, 1);
2499                   source = new_src;
2500                 }
2501               else
2502                 {
2503                   rtx targ = subtargets ? NULL_RTX : target;
2504
2505                   insns = arm_gen_constant (AND, mode, cond,
2506                                             remainder | shift_mask,
2507                                             targ, source, subtargets, 0);
2508                 }
2509             }
2510
2511           if (generate)
2512             {
2513               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2514               rtx shift = GEN_INT (clear_zero_bit_copies);
2515
2516               emit_insn (gen_lshrsi3 (new_src, source, shift));
2517               emit_insn (gen_ashlsi3 (target, new_src, shift));
2518             }
2519
2520           return insns + 2;
2521         }
2522
2523       break;
2524
2525     default:
2526       break;
2527     }
2528
2529   for (i = 0; i < 32; i++)
2530     if (remainder & (1 << i))
2531       num_bits_set++;
2532
2533   if (code == AND || (can_invert && num_bits_set > 16))
2534     remainder = (~remainder) & 0xffffffff;
2535   else if (code == PLUS && num_bits_set > 16)
2536     remainder = (-remainder) & 0xffffffff;
2537   else
2538     {
2539       can_invert = 0;
2540       can_negate = 0;
2541     }
2542
2543   /* Now try and find a way of doing the job in either two or three
2544      instructions.
2545      We start by looking for the largest block of zeros that are aligned on
2546      a 2-bit boundary, we then fill up the temps, wrapping around to the
2547      top of the word when we drop off the bottom.
2548      In the worst case this code should produce no more than four insns.
2549      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2550      best place to start.  */
2551
2552   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2553      the same.  */
2554   {
2555     int best_start = 0;
2556     if (!TARGET_THUMB2)
2557       {
2558         int best_consecutive_zeros = 0;
2559
2560         for (i = 0; i < 32; i += 2)
2561           {
2562             int consecutive_zeros = 0;
2563
2564             if (!(remainder & (3 << i)))
2565               {
2566                 while ((i < 32) && !(remainder & (3 << i)))
2567                   {
2568                     consecutive_zeros += 2;
2569                     i += 2;
2570                   }
2571                 if (consecutive_zeros > best_consecutive_zeros)
2572                   {
2573                     best_consecutive_zeros = consecutive_zeros;
2574                     best_start = i - consecutive_zeros;
2575                   }
2576                 i -= 2;
2577               }
2578           }
2579
2580         /* So long as it won't require any more insns to do so, it's
2581            desirable to emit a small constant (in bits 0...9) in the last
2582            insn.  This way there is more chance that it can be combined with
2583            a later addressing insn to form a pre-indexed load or store
2584            operation.  Consider:
2585
2586                    *((volatile int *)0xe0000100) = 1;
2587                    *((volatile int *)0xe0000110) = 2;
2588
2589            We want this to wind up as:
2590
2591                     mov rA, #0xe0000000
2592                     mov rB, #1
2593                     str rB, [rA, #0x100]
2594                     mov rB, #2
2595                     str rB, [rA, #0x110]
2596
2597            rather than having to synthesize both large constants from scratch.
2598
2599            Therefore, we calculate how many insns would be required to emit
2600            the constant starting from `best_start', and also starting from
2601            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2602            yield a shorter sequence, we may as well use zero.  */
2603         if (best_start != 0
2604             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2605             && (count_insns_for_constant (remainder, 0) <=
2606                 count_insns_for_constant (remainder, best_start)))
2607           best_start = 0;
2608       }
2609
2610     /* Now start emitting the insns.  */
2611     i = best_start;
2612     do
2613       {
2614         int end;
2615
2616         if (i <= 0)
2617           i += 32;
2618         if (remainder & (3 << (i - 2)))
2619           {
2620             end = i - 8;
2621             if (end < 0)
2622               end += 32;
2623             temp1 = remainder & ((0x0ff << end)
2624                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2625             remainder &= ~temp1;
2626
2627             if (generate)
2628               {
2629                 rtx new_src, temp1_rtx;
2630
2631                 if (code == SET || code == MINUS)
2632                   {
2633                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2634                     if (can_invert && code != MINUS)
2635                       temp1 = ~temp1;
2636                   }
2637                 else
2638                   {
2639                     if (remainder && subtargets)
2640                       new_src = gen_reg_rtx (mode);
2641                     else
2642                       new_src = target;
2643                     if (can_invert)
2644                       temp1 = ~temp1;
2645                     else if (can_negate)
2646                       temp1 = -temp1;
2647                   }
2648
2649                 temp1 = trunc_int_for_mode (temp1, mode);
2650                 temp1_rtx = GEN_INT (temp1);
2651
2652                 if (code == SET)
2653                   ;
2654                 else if (code == MINUS)
2655                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2656                 else
2657                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2658
2659                 emit_constant_insn (cond,
2660                                     gen_rtx_SET (VOIDmode, new_src,
2661                                                  temp1_rtx));
2662                 source = new_src;
2663               }
2664
2665             if (code == SET)
2666               {
2667                 can_invert = 0;
2668                 code = PLUS;
2669               }
2670             else if (code == MINUS)
2671               code = PLUS;
2672
2673             insns++;
2674             if (TARGET_ARM)
2675               i -= 6;
2676             else
2677               i -= 7;
2678           }
2679         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2680            shifts.  */
2681         if (TARGET_ARM)
2682           i -= 2;
2683         else
2684           i--;
2685       }
2686     while (remainder);
2687   }
2688
2689   return insns;
2690 }
2691
2692 /* Canonicalize a comparison so that we are more likely to recognize it.
2693    This can be done for a few constant compares, where we can make the
2694    immediate value easier to load.  */
2695
2696 enum rtx_code
2697 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2698                              rtx * op1)
2699 {
2700   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2701   unsigned HOST_WIDE_INT maxval;
2702   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2703
2704   switch (code)
2705     {
2706     case EQ:
2707     case NE:
2708       return code;
2709
2710     case GT:
2711     case LE:
2712       if (i != maxval
2713           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2714         {
2715           *op1 = GEN_INT (i + 1);
2716           return code == GT ? GE : LT;
2717         }
2718       break;
2719
2720     case GE:
2721     case LT:
2722       if (i != ~maxval
2723           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2724         {
2725           *op1 = GEN_INT (i - 1);
2726           return code == GE ? GT : LE;
2727         }
2728       break;
2729
2730     case GTU:
2731     case LEU:
2732       if (i != ~((unsigned HOST_WIDE_INT) 0)
2733           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2734         {
2735           *op1 = GEN_INT (i + 1);
2736           return code == GTU ? GEU : LTU;
2737         }
2738       break;
2739
2740     case GEU:
2741     case LTU:
2742       if (i != 0
2743           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2744         {
2745           *op1 = GEN_INT (i - 1);
2746           return code == GEU ? GTU : LEU;
2747         }
2748       break;
2749
2750     default:
2751       gcc_unreachable ();
2752     }
2753
2754   return code;
2755 }
2756
2757
2758 /* Define how to find the value returned by a function.  */
2759
2760 rtx
2761 arm_function_value(const_tree type, const_tree func ATTRIBUTE_UNUSED)
2762 {
2763   enum machine_mode mode;
2764   int unsignedp ATTRIBUTE_UNUSED;
2765   rtx r ATTRIBUTE_UNUSED;
2766
2767   mode = TYPE_MODE (type);
2768   /* Promote integer types.  */
2769   if (INTEGRAL_TYPE_P (type))
2770     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2771
2772   /* Promotes small structs returned in a register to full-word size
2773      for big-endian AAPCS.  */
2774   if (arm_return_in_msb (type))
2775     {
2776       HOST_WIDE_INT size = int_size_in_bytes (type);
2777       if (size % UNITS_PER_WORD != 0)
2778         {
2779           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2780           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2781         }
2782     }
2783
2784   return LIBCALL_VALUE(mode);
2785 }
2786
2787 /* Determine the amount of memory needed to store the possible return
2788    registers of an untyped call.  */
2789 int
2790 arm_apply_result_size (void)
2791 {
2792   int size = 16;
2793
2794   if (TARGET_ARM)
2795     {
2796       if (TARGET_HARD_FLOAT_ABI)
2797         {
2798           if (TARGET_FPA)
2799             size += 12;
2800           if (TARGET_MAVERICK)
2801             size += 8;
2802         }
2803       if (TARGET_IWMMXT_ABI)
2804         size += 8;
2805     }
2806
2807   return size;
2808 }
2809
2810 /* Decide whether a type should be returned in memory (true)
2811    or in a register (false).  This is called as the target hook
2812    TARGET_RETURN_IN_MEMORY.  */
2813 static bool
2814 arm_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
2815 {
2816   HOST_WIDE_INT size;
2817
2818   size = int_size_in_bytes (type);
2819
2820   /* Vector values should be returned using ARM registers, not memory (unless
2821      they're over 16 bytes, which will break since we only have four
2822      call-clobbered registers to play with).  */
2823   if (TREE_CODE (type) == VECTOR_TYPE)
2824     return (size < 0 || size > (4 * UNITS_PER_WORD));
2825
2826   if (!AGGREGATE_TYPE_P (type) &&
2827       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2828     /* All simple types are returned in registers.
2829        For AAPCS, complex types are treated the same as aggregates.  */
2830     return 0;
2831
2832   if (arm_abi != ARM_ABI_APCS)
2833     {
2834       /* ATPCS and later return aggregate types in memory only if they are
2835          larger than a word (or are variable size).  */
2836       return (size < 0 || size > UNITS_PER_WORD);
2837     }
2838
2839   /* For the arm-wince targets we choose to be compatible with Microsoft's
2840      ARM and Thumb compilers, which always return aggregates in memory.  */
2841 #ifndef ARM_WINCE
2842   /* All structures/unions bigger than one word are returned in memory.
2843      Also catch the case where int_size_in_bytes returns -1.  In this case
2844      the aggregate is either huge or of variable size, and in either case
2845      we will want to return it via memory and not in a register.  */
2846   if (size < 0 || size > UNITS_PER_WORD)
2847     return 1;
2848
2849   if (TREE_CODE (type) == RECORD_TYPE)
2850     {
2851       tree field;
2852
2853       /* For a struct the APCS says that we only return in a register
2854          if the type is 'integer like' and every addressable element
2855          has an offset of zero.  For practical purposes this means
2856          that the structure can have at most one non bit-field element
2857          and that this element must be the first one in the structure.  */
2858
2859       /* Find the first field, ignoring non FIELD_DECL things which will
2860          have been created by C++.  */
2861       for (field = TYPE_FIELDS (type);
2862            field && TREE_CODE (field) != FIELD_DECL;
2863            field = TREE_CHAIN (field))
2864         continue;
2865
2866       if (field == NULL)
2867         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2868
2869       /* Check that the first field is valid for returning in a register.  */
2870
2871       /* ... Floats are not allowed */
2872       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2873         return 1;
2874
2875       /* ... Aggregates that are not themselves valid for returning in
2876          a register are not allowed.  */
2877       if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
2878         return 1;
2879
2880       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2881          since they are not addressable.  */
2882       for (field = TREE_CHAIN (field);
2883            field;
2884            field = TREE_CHAIN (field))
2885         {
2886           if (TREE_CODE (field) != FIELD_DECL)
2887             continue;
2888
2889           if (!DECL_BIT_FIELD_TYPE (field))
2890             return 1;
2891         }
2892
2893       return 0;
2894     }
2895
2896   if (TREE_CODE (type) == UNION_TYPE)
2897     {
2898       tree field;
2899
2900       /* Unions can be returned in registers if every element is
2901          integral, or can be returned in an integer register.  */
2902       for (field = TYPE_FIELDS (type);
2903            field;
2904            field = TREE_CHAIN (field))
2905         {
2906           if (TREE_CODE (field) != FIELD_DECL)
2907             continue;
2908
2909           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2910             return 1;
2911
2912           if (arm_return_in_memory (TREE_TYPE (field), NULL_TREE))
2913             return 1;
2914         }
2915
2916       return 0;
2917     }
2918 #endif /* not ARM_WINCE */
2919
2920   /* Return all other types in memory.  */
2921   return 1;
2922 }
2923
2924 /* Indicate whether or not words of a double are in big-endian order.  */
2925
2926 int
2927 arm_float_words_big_endian (void)
2928 {
2929   if (TARGET_MAVERICK)
2930     return 0;
2931
2932   /* For FPA, float words are always big-endian.  For VFP, floats words
2933      follow the memory system mode.  */
2934
2935   if (TARGET_FPA)
2936     {
2937       return 1;
2938     }
2939
2940   if (TARGET_VFP)
2941     return (TARGET_BIG_END ? 1 : 0);
2942
2943   return 1;
2944 }
2945
2946 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2947    for a call to a function whose data type is FNTYPE.
2948    For a library call, FNTYPE is NULL.  */
2949 void
2950 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2951                           rtx libname  ATTRIBUTE_UNUSED,
2952                           tree fndecl ATTRIBUTE_UNUSED)
2953 {
2954   /* On the ARM, the offset starts at 0.  */
2955   pcum->nregs = 0;
2956   pcum->iwmmxt_nregs = 0;
2957   pcum->can_split = true;
2958
2959   /* Varargs vectors are treated the same as long long.
2960      named_count avoids having to change the way arm handles 'named' */
2961   pcum->named_count = 0;
2962   pcum->nargs = 0;
2963
2964   if (TARGET_REALLY_IWMMXT && fntype)
2965     {
2966       tree fn_arg;
2967
2968       for (fn_arg = TYPE_ARG_TYPES (fntype);
2969            fn_arg;
2970            fn_arg = TREE_CHAIN (fn_arg))
2971         pcum->named_count += 1;
2972
2973       if (! pcum->named_count)
2974         pcum->named_count = INT_MAX;
2975     }
2976 }
2977
2978
2979 /* Return true if mode/type need doubleword alignment.  */
2980 bool
2981 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2982 {
2983   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2984           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2985 }
2986
2987
2988 /* Determine where to put an argument to a function.
2989    Value is zero to push the argument on the stack,
2990    or a hard register in which to store the argument.
2991
2992    MODE is the argument's machine mode.
2993    TYPE is the data type of the argument (as a tree).
2994     This is null for libcalls where that information may
2995     not be available.
2996    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2997     the preceding args and about the function being called.
2998    NAMED is nonzero if this argument is a named parameter
2999     (otherwise it is an extra parameter matching an ellipsis).  */
3000
3001 rtx
3002 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3003                   tree type, int named)
3004 {
3005   int nregs;
3006
3007   /* Varargs vectors are treated the same as long long.
3008      named_count avoids having to change the way arm handles 'named' */
3009   if (TARGET_IWMMXT_ABI
3010       && arm_vector_mode_supported_p (mode)
3011       && pcum->named_count > pcum->nargs + 1)
3012     {
3013       if (pcum->iwmmxt_nregs <= 9)
3014         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
3015       else
3016         {
3017           pcum->can_split = false;
3018           return NULL_RTX;
3019         }
3020     }
3021
3022   /* Put doubleword aligned quantities in even register pairs.  */
3023   if (pcum->nregs & 1
3024       && ARM_DOUBLEWORD_ALIGN
3025       && arm_needs_doubleword_align (mode, type))
3026     pcum->nregs++;
3027
3028   if (mode == VOIDmode)
3029     /* Pick an arbitrary value for operand 2 of the call insn.  */
3030     return const0_rtx;
3031
3032   /* Only allow splitting an arg between regs and memory if all preceding
3033      args were allocated to regs.  For args passed by reference we only count
3034      the reference pointer.  */
3035   if (pcum->can_split)
3036     nregs = 1;
3037   else
3038     nregs = ARM_NUM_REGS2 (mode, type);
3039
3040   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
3041     return NULL_RTX;
3042
3043   return gen_rtx_REG (mode, pcum->nregs);
3044 }
3045
3046 static int
3047 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
3048                        tree type, bool named ATTRIBUTE_UNUSED)
3049 {
3050   int nregs = pcum->nregs;
3051
3052   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
3053     return 0;
3054
3055   if (NUM_ARG_REGS > nregs
3056       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
3057       && pcum->can_split)
3058     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
3059
3060   return 0;
3061 }
3062
3063 /* Variable sized types are passed by reference.  This is a GCC
3064    extension to the ARM ABI.  */
3065
3066 static bool
3067 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3068                        enum machine_mode mode ATTRIBUTE_UNUSED,
3069                        const_tree type, bool named ATTRIBUTE_UNUSED)
3070 {
3071   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3072 }
3073 \f
3074 /* Encode the current state of the #pragma [no_]long_calls.  */
3075 typedef enum
3076 {
3077   OFF,          /* No #pragma [no_]long_calls is in effect.  */
3078   LONG,         /* #pragma long_calls is in effect.  */
3079   SHORT         /* #pragma no_long_calls is in effect.  */
3080 } arm_pragma_enum;
3081
3082 static arm_pragma_enum arm_pragma_long_calls = OFF;
3083
3084 void
3085 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3086 {
3087   arm_pragma_long_calls = LONG;
3088 }
3089
3090 void
3091 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3092 {
3093   arm_pragma_long_calls = SHORT;
3094 }
3095
3096 void
3097 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3098 {
3099   arm_pragma_long_calls = OFF;
3100 }
3101 \f
3102 /* Table of machine attributes.  */
3103 const struct attribute_spec arm_attribute_table[] =
3104 {
3105   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3106   /* Function calls made to this symbol must be done indirectly, because
3107      it may lie outside of the 26 bit addressing range of a normal function
3108      call.  */
3109   { "long_call",    0, 0, false, true,  true,  NULL },
3110   /* Whereas these functions are always known to reside within the 26 bit
3111      addressing range.  */
3112   { "short_call",   0, 0, false, true,  true,  NULL },
3113   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
3114   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
3115   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
3116   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
3117 #ifdef ARM_PE
3118   /* ARM/PE has three new attributes:
3119      interfacearm - ?
3120      dllexport - for exporting a function/variable that will live in a dll
3121      dllimport - for importing a function/variable from a dll
3122
3123      Microsoft allows multiple declspecs in one __declspec, separating
3124      them with spaces.  We do NOT support this.  Instead, use __declspec
3125      multiple times.
3126   */
3127   { "dllimport",    0, 0, true,  false, false, NULL },
3128   { "dllexport",    0, 0, true,  false, false, NULL },
3129   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
3130 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3131   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
3132   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
3133   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
3134 #endif
3135   { NULL,           0, 0, false, false, false, NULL }
3136 };
3137
3138 /* Handle an attribute requiring a FUNCTION_DECL;
3139    arguments as in struct attribute_spec.handler.  */
3140 static tree
3141 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3142                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3143 {
3144   if (TREE_CODE (*node) != FUNCTION_DECL)
3145     {
3146       warning (OPT_Wattributes, "%qs attribute only applies to functions",
3147                IDENTIFIER_POINTER (name));
3148       *no_add_attrs = true;
3149     }
3150
3151   return NULL_TREE;
3152 }
3153
3154 /* Handle an "interrupt" or "isr" attribute;
3155    arguments as in struct attribute_spec.handler.  */
3156 static tree
3157 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3158                           bool *no_add_attrs)
3159 {
3160   if (DECL_P (*node))
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       /* FIXME: the argument if any is checked for type attributes;
3169          should it be checked for decl ones?  */
3170     }
3171   else
3172     {
3173       if (TREE_CODE (*node) == FUNCTION_TYPE
3174           || TREE_CODE (*node) == METHOD_TYPE)
3175         {
3176           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3177             {
3178               warning (OPT_Wattributes, "%qs attribute ignored",
3179                        IDENTIFIER_POINTER (name));
3180               *no_add_attrs = true;
3181             }
3182         }
3183       else if (TREE_CODE (*node) == POINTER_TYPE
3184                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3185                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3186                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3187         {
3188           *node = build_variant_type_copy (*node);
3189           TREE_TYPE (*node) = build_type_attribute_variant
3190             (TREE_TYPE (*node),
3191              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3192           *no_add_attrs = true;
3193         }
3194       else
3195         {
3196           /* Possibly pass this attribute on from the type to a decl.  */
3197           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3198                        | (int) ATTR_FLAG_FUNCTION_NEXT
3199                        | (int) ATTR_FLAG_ARRAY_NEXT))
3200             {
3201               *no_add_attrs = true;
3202               return tree_cons (name, args, NULL_TREE);
3203             }
3204           else
3205             {
3206               warning (OPT_Wattributes, "%qs attribute ignored",
3207                        IDENTIFIER_POINTER (name));
3208             }
3209         }
3210     }
3211
3212   return NULL_TREE;
3213 }
3214
3215 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3216 /* Handle the "notshared" attribute.  This attribute is another way of
3217    requesting hidden visibility.  ARM's compiler supports
3218    "__declspec(notshared)"; we support the same thing via an
3219    attribute.  */
3220
3221 static tree
3222 arm_handle_notshared_attribute (tree *node,
3223                                 tree name ATTRIBUTE_UNUSED,
3224                                 tree args ATTRIBUTE_UNUSED,
3225                                 int flags ATTRIBUTE_UNUSED,
3226                                 bool *no_add_attrs)
3227 {
3228   tree decl = TYPE_NAME (*node);
3229
3230   if (decl)
3231     {
3232       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3233       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3234       *no_add_attrs = false;
3235     }
3236   return NULL_TREE;
3237 }
3238 #endif
3239
3240 /* Return 0 if the attributes for two types are incompatible, 1 if they
3241    are compatible, and 2 if they are nearly compatible (which causes a
3242    warning to be generated).  */
3243 static int
3244 arm_comp_type_attributes (const_tree type1, const_tree type2)
3245 {
3246   int l1, l2, s1, s2;
3247
3248   /* Check for mismatch of non-default calling convention.  */
3249   if (TREE_CODE (type1) != FUNCTION_TYPE)
3250     return 1;
3251
3252   /* Check for mismatched call attributes.  */
3253   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3254   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3255   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3256   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3257
3258   /* Only bother to check if an attribute is defined.  */
3259   if (l1 | l2 | s1 | s2)
3260     {
3261       /* If one type has an attribute, the other must have the same attribute.  */
3262       if ((l1 != l2) || (s1 != s2))
3263         return 0;
3264
3265       /* Disallow mixed attributes.  */
3266       if ((l1 & s2) || (l2 & s1))
3267         return 0;
3268     }
3269
3270   /* Check for mismatched ISR attribute.  */
3271   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3272   if (! l1)
3273     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3274   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3275   if (! l2)
3276     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3277   if (l1 != l2)
3278     return 0;
3279
3280   return 1;
3281 }
3282
3283 /*  Assigns default attributes to newly defined type.  This is used to
3284     set short_call/long_call attributes for function types of
3285     functions defined inside corresponding #pragma scopes.  */
3286 static void
3287 arm_set_default_type_attributes (tree type)
3288 {
3289   /* Add __attribute__ ((long_call)) to all functions, when
3290      inside #pragma long_calls or __attribute__ ((short_call)),
3291      when inside #pragma no_long_calls.  */
3292   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3293     {
3294       tree type_attr_list, attr_name;
3295       type_attr_list = TYPE_ATTRIBUTES (type);
3296
3297       if (arm_pragma_long_calls == LONG)
3298         attr_name = get_identifier ("long_call");
3299       else if (arm_pragma_long_calls == SHORT)
3300         attr_name = get_identifier ("short_call");
3301       else
3302         return;
3303
3304       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3305       TYPE_ATTRIBUTES (type) = type_attr_list;
3306     }
3307 }
3308 \f
3309 /* Return true if DECL is known to be linked into section SECTION.  */
3310
3311 static bool
3312 arm_function_in_section_p (tree decl, section *section)
3313 {
3314   /* We can only be certain about functions defined in the same
3315      compilation unit.  */
3316   if (!TREE_STATIC (decl))
3317     return false;
3318
3319   /* Make sure that SYMBOL always binds to the definition in this
3320      compilation unit.  */
3321   if (!targetm.binds_local_p (decl))
3322     return false;
3323
3324   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3325   if (!DECL_SECTION_NAME (decl))
3326     {
3327       /* Make sure that we will not create a unique section for DECL.  */
3328       if (flag_function_sections || DECL_ONE_ONLY (decl))
3329         return false;
3330     }
3331
3332   return function_section (decl) == section;
3333 }
3334
3335 /* Return nonzero if a 32-bit "long_call" should be generated for
3336    a call from the current function to DECL.  We generate a long_call
3337    if the function:
3338
3339         a.  has an __attribute__((long call))
3340      or b.  is within the scope of a #pragma long_calls
3341      or c.  the -mlong-calls command line switch has been specified
3342
3343    However we do not generate a long call if the function:
3344
3345         d.  has an __attribute__ ((short_call))
3346      or e.  is inside the scope of a #pragma no_long_calls
3347      or f.  is defined in the same section as the current function.  */
3348
3349 bool
3350 arm_is_long_call_p (tree decl)
3351 {
3352   tree attrs;
3353
3354   if (!decl)
3355     return TARGET_LONG_CALLS;
3356
3357   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3358   if (lookup_attribute ("short_call", attrs))
3359     return false;
3360
3361   /* For "f", be conservative, and only cater for cases in which the
3362      whole of the current function is placed in the same section.  */
3363   if (!flag_reorder_blocks_and_partition
3364       && TREE_CODE (decl) == FUNCTION_DECL
3365       && arm_function_in_section_p (decl, current_function_section ()))
3366     return false;
3367
3368   if (lookup_attribute ("long_call", attrs))
3369     return true;
3370
3371   return TARGET_LONG_CALLS;
3372 }
3373
3374 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3375 static bool
3376 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3377 {
3378   unsigned long func_type;
3379
3380   if (cfun->machine->sibcall_blocked)
3381     return false;
3382
3383   /* Never tailcall something for which we have no decl, or if we
3384      are in Thumb mode.  */
3385   if (decl == NULL || TARGET_THUMB)
3386     return false;
3387
3388   /* The PIC register is live on entry to VxWorks PLT entries, so we
3389      must make the call before restoring the PIC register.  */
3390   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3391     return false;
3392
3393   /* Cannot tail-call to long calls, since these are out of range of
3394      a branch instruction.  */
3395   if (arm_is_long_call_p (decl))
3396     return false;
3397
3398   /* If we are interworking and the function is not declared static
3399      then we can't tail-call it unless we know that it exists in this
3400      compilation unit (since it might be a Thumb routine).  */
3401   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3402     return false;
3403
3404   func_type = arm_current_func_type ();
3405   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3406   if (IS_INTERRUPT (func_type))
3407     return false;
3408
3409   /* Never tailcall if function may be called with a misaligned SP.  */
3410   if (IS_STACKALIGN (func_type))
3411     return false;
3412
3413   /* Everything else is ok.  */
3414   return true;
3415 }
3416
3417 \f
3418 /* Addressing mode support functions.  */
3419
3420 /* Return nonzero if X is a legitimate immediate operand when compiling
3421    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3422 int
3423 legitimate_pic_operand_p (rtx x)
3424 {
3425   if (GET_CODE (x) == SYMBOL_REF
3426       || (GET_CODE (x) == CONST
3427           && GET_CODE (XEXP (x, 0)) == PLUS
3428           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3429     return 0;
3430
3431   return 1;
3432 }
3433
3434 /* Record that the current function needs a PIC register.  Initialize
3435    cfun->machine->pic_reg if we have not already done so.  */
3436
3437 static void
3438 require_pic_register (void)
3439 {
3440   /* A lot of the logic here is made obscure by the fact that this
3441      routine gets called as part of the rtx cost estimation process.
3442      We don't want those calls to affect any assumptions about the real
3443      function; and further, we can't call entry_of_function() until we
3444      start the real expansion process.  */
3445   if (!crtl->uses_pic_offset_table)
3446     {
3447       gcc_assert (can_create_pseudo_p ());
3448       if (arm_pic_register != INVALID_REGNUM)
3449         {
3450           cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3451
3452           /* Play games to avoid marking the function as needing pic
3453              if we are being called as part of the cost-estimation
3454              process.  */
3455           if (current_ir_type () != IR_GIMPLE)
3456             crtl->uses_pic_offset_table = 1;
3457         }
3458       else
3459         {
3460           rtx seq;
3461
3462           cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3463
3464           /* Play games to avoid marking the function as needing pic
3465              if we are being called as part of the cost-estimation
3466              process.  */
3467           if (current_ir_type () != IR_GIMPLE)
3468             {
3469               crtl->uses_pic_offset_table = 1;
3470               start_sequence ();
3471
3472               arm_load_pic_register (0UL);
3473
3474               seq = get_insns ();
3475               end_sequence ();
3476               emit_insn_after (seq, entry_of_function ());
3477             }
3478         }
3479     }
3480 }
3481
3482 rtx
3483 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3484 {
3485   if (GET_CODE (orig) == SYMBOL_REF
3486       || GET_CODE (orig) == LABEL_REF)
3487     {
3488       rtx pic_ref, address;
3489       rtx insn;
3490       int subregs = 0;
3491
3492       /* If this function doesn't have a pic register, create one now.  */
3493       require_pic_register ();
3494
3495       if (reg == 0)
3496         {
3497           gcc_assert (can_create_pseudo_p ());
3498           reg = gen_reg_rtx (Pmode);
3499
3500           subregs = 1;
3501         }
3502
3503       if (subregs)
3504         address = gen_reg_rtx (Pmode);
3505       else
3506         address = reg;
3507
3508       if (TARGET_ARM)
3509         emit_insn (gen_pic_load_addr_arm (address, orig));
3510       else if (TARGET_THUMB2)
3511         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3512       else /* TARGET_THUMB1 */
3513         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3514
3515       /* VxWorks does not impose a fixed gap between segments; the run-time
3516          gap can be different from the object-file gap.  We therefore can't
3517          use GOTOFF unless we are absolutely sure that the symbol is in the
3518          same segment as the GOT.  Unfortunately, the flexibility of linker
3519          scripts means that we can't be sure of that in general, so assume
3520          that GOTOFF is never valid on VxWorks.  */
3521       if ((GET_CODE (orig) == LABEL_REF
3522            || (GET_CODE (orig) == SYMBOL_REF &&
3523                SYMBOL_REF_LOCAL_P (orig)))
3524           && NEED_GOT_RELOC
3525           && !TARGET_VXWORKS_RTP)
3526         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3527       else
3528         {
3529           pic_ref = gen_const_mem (Pmode,
3530                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3531                                                  address));
3532         }
3533
3534       insn = emit_move_insn (reg, pic_ref);
3535
3536       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3537          by loop.  */
3538       set_unique_reg_note (insn, REG_EQUAL, orig);
3539
3540       return reg;
3541     }
3542   else if (GET_CODE (orig) == CONST)
3543     {
3544       rtx base, offset;
3545
3546       if (GET_CODE (XEXP (orig, 0)) == PLUS
3547           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3548         return orig;
3549
3550       /* Handle the case where we have: const (UNSPEC_TLS).  */
3551       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3552           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3553         return orig;
3554
3555       /* Handle the case where we have:
3556          const (plus (UNSPEC_TLS) (ADDEND)).  The ADDEND must be a
3557          CONST_INT.  */
3558       if (GET_CODE (XEXP (orig, 0)) == PLUS
3559           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == UNSPEC
3560           && XINT (XEXP (XEXP (orig, 0), 0), 1) == UNSPEC_TLS)
3561         {
3562           gcc_assert (GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT);
3563           return orig;
3564         }
3565
3566       if (reg == 0)
3567         {
3568           gcc_assert (can_create_pseudo_p ());
3569           reg = gen_reg_rtx (Pmode);
3570         }
3571
3572       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3573
3574       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3575       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3576                                        base == reg ? 0 : reg);
3577
3578       if (GET_CODE (offset) == CONST_INT)
3579         {
3580           /* The base register doesn't really matter, we only want to
3581              test the index for the appropriate mode.  */
3582           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3583             {
3584               gcc_assert (can_create_pseudo_p ());
3585               offset = force_reg (Pmode, offset);
3586             }
3587
3588           if (GET_CODE (offset) == CONST_INT)
3589             return plus_constant (base, INTVAL (offset));
3590         }
3591
3592       if (GET_MODE_SIZE (mode) > 4
3593           && (GET_MODE_CLASS (mode) == MODE_INT
3594               || TARGET_SOFT_FLOAT))
3595         {
3596           emit_insn (gen_addsi3 (reg, base, offset));
3597           return reg;
3598         }
3599
3600       return gen_rtx_PLUS (Pmode, base, offset);
3601     }
3602
3603   return orig;
3604 }
3605
3606
3607 /* Find a spare register to use during the prolog of a function.  */
3608
3609 static int
3610 thumb_find_work_register (unsigned long pushed_regs_mask)
3611 {
3612   int reg;
3613
3614   /* Check the argument registers first as these are call-used.  The
3615      register allocation order means that sometimes r3 might be used
3616      but earlier argument registers might not, so check them all.  */
3617   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3618     if (!df_regs_ever_live_p (reg))
3619       return reg;
3620
3621   /* Before going on to check the call-saved registers we can try a couple
3622      more ways of deducing that r3 is available.  The first is when we are
3623      pushing anonymous arguments onto the stack and we have less than 4
3624      registers worth of fixed arguments(*).  In this case r3 will be part of
3625      the variable argument list and so we can be sure that it will be
3626      pushed right at the start of the function.  Hence it will be available
3627      for the rest of the prologue.
3628      (*): ie crtl->args.pretend_args_size is greater than 0.  */
3629   if (cfun->machine->uses_anonymous_args
3630       && crtl->args.pretend_args_size > 0)
3631     return LAST_ARG_REGNUM;
3632
3633   /* The other case is when we have fixed arguments but less than 4 registers
3634      worth.  In this case r3 might be used in the body of the function, but
3635      it is not being used to convey an argument into the function.  In theory
3636      we could just check crtl->args.size to see how many bytes are
3637      being passed in argument registers, but it seems that it is unreliable.
3638      Sometimes it will have the value 0 when in fact arguments are being
3639      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3640      check the args_info.nregs field as well.  The problem with this field is
3641      that it makes no allowances for arguments that are passed to the
3642      function but which are not used.  Hence we could miss an opportunity
3643      when a function has an unused argument in r3.  But it is better to be
3644      safe than to be sorry.  */
3645   if (! cfun->machine->uses_anonymous_args
3646       && crtl->args.size >= 0
3647       && crtl->args.size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3648       && crtl->args.info.nregs < 4)
3649     return LAST_ARG_REGNUM;
3650
3651   /* Otherwise look for a call-saved register that is going to be pushed.  */
3652   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3653     if (pushed_regs_mask & (1 << reg))
3654       return reg;
3655
3656   if (TARGET_THUMB2)
3657     {
3658       /* Thumb-2 can use high regs.  */
3659       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3660         if (pushed_regs_mask & (1 << reg))
3661           return reg;
3662     }
3663   /* Something went wrong - thumb_compute_save_reg_mask()
3664      should have arranged for a suitable register to be pushed.  */
3665   gcc_unreachable ();
3666 }
3667
3668 static GTY(()) int pic_labelno;
3669
3670 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3671    low register.  */
3672
3673 void
3674 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3675 {
3676   rtx l1, labelno, pic_tmp, pic_rtx, pic_reg;
3677
3678   if (crtl->uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3679     return;
3680
3681   gcc_assert (flag_pic);
3682
3683   pic_reg = cfun->machine->pic_reg;
3684   if (TARGET_VXWORKS_RTP)
3685     {
3686       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3687       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3688       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3689
3690       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
3691
3692       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3693       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
3694     }
3695   else
3696     {
3697       /* We use an UNSPEC rather than a LABEL_REF because this label
3698          never appears in the code stream.  */
3699
3700       labelno = GEN_INT (pic_labelno++);
3701       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3702       l1 = gen_rtx_CONST (VOIDmode, l1);
3703
3704       /* On the ARM the PC register contains 'dot + 8' at the time of the
3705          addition, on the Thumb it is 'dot + 4'.  */
3706       pic_rtx = plus_constant (l1, TARGET_ARM ? 8 : 4);
3707       pic_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, pic_rtx),
3708                                 UNSPEC_GOTSYM_OFF);
3709       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3710
3711       if (TARGET_ARM)
3712         {
3713           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3714           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
3715         }
3716       else if (TARGET_THUMB2)
3717         {
3718           /* Thumb-2 only allows very limited access to the PC.  Calculate the
3719              address in a temporary register.  */
3720           if (arm_pic_register != INVALID_REGNUM)
3721             {
3722               pic_tmp = gen_rtx_REG (SImode,
3723                                      thumb_find_work_register (saved_regs));
3724             }
3725           else
3726             {
3727               gcc_assert (can_create_pseudo_p ());
3728               pic_tmp = gen_reg_rtx (Pmode);
3729             }
3730
3731           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
3732           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3733           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
3734         }
3735       else /* TARGET_THUMB1 */
3736         {
3737           if (arm_pic_register != INVALID_REGNUM
3738               && REGNO (pic_reg) > LAST_LO_REGNUM)
3739             {
3740               /* We will have pushed the pic register, so we should always be
3741                  able to find a work register.  */
3742               pic_tmp = gen_rtx_REG (SImode,
3743                                      thumb_find_work_register (saved_regs));
3744               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3745               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3746             }
3747           else
3748             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
3749           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
3750         }
3751     }
3752
3753   /* Need to emit this whether or not we obey regdecls,
3754      since setjmp/longjmp can cause life info to screw up.  */
3755   emit_use (pic_reg);
3756 }
3757
3758
3759 /* Return nonzero if X is valid as an ARM state addressing register.  */
3760 static int
3761 arm_address_register_rtx_p (rtx x, int strict_p)
3762 {
3763   int regno;
3764
3765   if (GET_CODE (x) != REG)
3766     return 0;
3767
3768   regno = REGNO (x);
3769
3770   if (strict_p)
3771     return ARM_REGNO_OK_FOR_BASE_P (regno);
3772
3773   return (regno <= LAST_ARM_REGNUM
3774           || regno >= FIRST_PSEUDO_REGISTER
3775           || regno == FRAME_POINTER_REGNUM
3776           || regno == ARG_POINTER_REGNUM);
3777 }
3778
3779 /* Return TRUE if this rtx is the difference of a symbol and a label,
3780    and will reduce to a PC-relative relocation in the object file.
3781    Expressions like this can be left alone when generating PIC, rather
3782    than forced through the GOT.  */
3783 static int
3784 pcrel_constant_p (rtx x)
3785 {
3786   if (GET_CODE (x) == MINUS)
3787     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3788
3789   return FALSE;
3790 }
3791
3792 /* Return nonzero if X is a valid ARM state address operand.  */
3793 int
3794 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3795                           int strict_p)
3796 {
3797   bool use_ldrd;
3798   enum rtx_code code = GET_CODE (x);
3799
3800   if (arm_address_register_rtx_p (x, strict_p))
3801     return 1;
3802
3803   use_ldrd = (TARGET_LDRD
3804               && (mode == DImode
3805                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3806
3807   if (code == POST_INC || code == PRE_DEC
3808       || ((code == PRE_INC || code == POST_DEC)
3809           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3810     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3811
3812   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3813            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3814            && GET_CODE (XEXP (x, 1)) == PLUS
3815            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3816     {
3817       rtx addend = XEXP (XEXP (x, 1), 1);
3818
3819       /* Don't allow ldrd post increment by register because it's hard
3820          to fixup invalid register choices.  */
3821       if (use_ldrd
3822           && GET_CODE (x) == POST_MODIFY
3823           && GET_CODE (addend) == REG)
3824         return 0;
3825
3826       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3827               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3828     }
3829
3830   /* After reload constants split into minipools will have addresses
3831      from a LABEL_REF.  */
3832   else if (reload_completed
3833            && (code == LABEL_REF
3834                || (code == CONST
3835                    && GET_CODE (XEXP (x, 0)) == PLUS
3836                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3837                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3838     return 1;
3839
3840   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
3841     return 0;
3842
3843   else if (code == PLUS)
3844     {
3845       rtx xop0 = XEXP (x, 0);
3846       rtx xop1 = XEXP (x, 1);
3847
3848       return ((arm_address_register_rtx_p (xop0, strict_p)
3849                && GET_CODE(xop1) == CONST_INT
3850                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3851               || (arm_address_register_rtx_p (xop1, strict_p)
3852                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3853     }
3854
3855 #if 0
3856   /* Reload currently can't handle MINUS, so disable this for now */
3857   else if (GET_CODE (x) == MINUS)
3858     {
3859       rtx xop0 = XEXP (x, 0);
3860       rtx xop1 = XEXP (x, 1);
3861
3862       return (arm_address_register_rtx_p (xop0, strict_p)
3863               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3864     }
3865 #endif
3866
3867   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3868            && code == SYMBOL_REF
3869            && CONSTANT_POOL_ADDRESS_P (x)
3870            && ! (flag_pic
3871                  && symbol_mentioned_p (get_pool_constant (x))
3872                  && ! pcrel_constant_p (get_pool_constant (x))))
3873     return 1;
3874
3875   return 0;
3876 }
3877
3878 /* Return nonzero if X is a valid Thumb-2 address operand.  */
3879 int
3880 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3881 {
3882   bool use_ldrd;
3883   enum rtx_code code = GET_CODE (x);
3884   
3885   if (arm_address_register_rtx_p (x, strict_p))
3886     return 1;
3887
3888   use_ldrd = (TARGET_LDRD
3889               && (mode == DImode
3890                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3891
3892   if (code == POST_INC || code == PRE_DEC
3893       || ((code == PRE_INC || code == POST_DEC)
3894           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3895     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3896
3897   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3898            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3899            && GET_CODE (XEXP (x, 1)) == PLUS
3900            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3901     {
3902       /* Thumb-2 only has autoincrement by constant.  */
3903       rtx addend = XEXP (XEXP (x, 1), 1);
3904       HOST_WIDE_INT offset;
3905
3906       if (GET_CODE (addend) != CONST_INT)
3907         return 0;
3908
3909       offset = INTVAL(addend);
3910       if (GET_MODE_SIZE (mode) <= 4)
3911         return (offset > -256 && offset < 256);
3912       
3913       return (use_ldrd && offset > -1024 && offset < 1024
3914               && (offset & 3) == 0);
3915     }
3916
3917   /* After reload constants split into minipools will have addresses
3918      from a LABEL_REF.  */
3919   else if (reload_completed
3920            && (code == LABEL_REF
3921                || (code == CONST
3922                    && GET_CODE (XEXP (x, 0)) == PLUS
3923                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3924                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3925     return 1;
3926
3927   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
3928     return 0;
3929
3930   else if (code == PLUS)
3931     {
3932       rtx xop0 = XEXP (x, 0);
3933       rtx xop1 = XEXP (x, 1);
3934
3935       return ((arm_address_register_rtx_p (xop0, strict_p)
3936                && thumb2_legitimate_index_p (mode, xop1, strict_p))
3937               || (arm_address_register_rtx_p (xop1, strict_p)
3938                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3939     }
3940
3941   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3942            && code == SYMBOL_REF
3943            && CONSTANT_POOL_ADDRESS_P (x)
3944            && ! (flag_pic
3945                  && symbol_mentioned_p (get_pool_constant (x))
3946                  && ! pcrel_constant_p (get_pool_constant (x))))
3947     return 1;
3948
3949   return 0;
3950 }
3951
3952 /* Return nonzero if INDEX is valid for an address index operand in
3953    ARM state.  */
3954 static int
3955 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3956                         int strict_p)
3957 {
3958   HOST_WIDE_INT range;
3959   enum rtx_code code = GET_CODE (index);
3960
3961   /* Standard coprocessor addressing modes.  */
3962   if (TARGET_HARD_FLOAT
3963       && (TARGET_FPA || TARGET_MAVERICK)
3964       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3965           || (TARGET_MAVERICK && mode == DImode)))
3966     return (code == CONST_INT && INTVAL (index) < 1024
3967             && INTVAL (index) > -1024
3968             && (INTVAL (index) & 3) == 0);
3969
3970   if (TARGET_NEON
3971       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
3972     return (code == CONST_INT
3973             && INTVAL (index) < 1016
3974             && INTVAL (index) > -1024
3975             && (INTVAL (index) & 3) == 0);
3976
3977   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3978     return (code == CONST_INT
3979             && INTVAL (index) < 1024
3980             && INTVAL (index) > -1024
3981             && (INTVAL (index) & 3) == 0);
3982
3983   if (arm_address_register_rtx_p (index, strict_p)
3984       && (GET_MODE_SIZE (mode) <= 4))
3985     return 1;
3986
3987   if (mode == DImode || mode == DFmode)
3988     {
3989       if (code == CONST_INT)
3990         {
3991           HOST_WIDE_INT val = INTVAL (index);
3992
3993           if (TARGET_LDRD)
3994             return val > -256 && val < 256;
3995           else
3996             return val > -4096 && val < 4092;
3997         }
3998
3999       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
4000     }
4001
4002   if (GET_MODE_SIZE (mode) <= 4
4003       && ! (arm_arch4
4004             && (mode == HImode
4005                 || (mode == QImode && outer == SIGN_EXTEND))))
4006     {
4007       if (code == MULT)
4008         {
4009           rtx xiop0 = XEXP (index, 0);
4010           rtx xiop1 = XEXP (index, 1);
4011
4012           return ((arm_address_register_rtx_p (xiop0, strict_p)
4013                    && power_of_two_operand (xiop1, SImode))
4014                   || (arm_address_register_rtx_p (xiop1, strict_p)
4015                       && power_of_two_operand (xiop0, SImode)));
4016         }
4017       else if (code == LSHIFTRT || code == ASHIFTRT
4018                || code == ASHIFT || code == ROTATERT)
4019         {
4020           rtx op = XEXP (index, 1);
4021
4022           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4023                   && GET_CODE (op) == CONST_INT
4024                   && INTVAL (op) > 0
4025                   && INTVAL (op) <= 31);
4026         }
4027     }
4028
4029   /* For ARM v4 we may be doing a sign-extend operation during the
4030      load.  */
4031   if (arm_arch4)
4032     {
4033       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
4034         range = 256;
4035       else
4036         range = 4096;
4037     }
4038   else
4039     range = (mode == HImode) ? 4095 : 4096;
4040
4041   return (code == CONST_INT
4042           && INTVAL (index) < range
4043           && INTVAL (index) > -range);
4044 }
4045
4046 /* Return true if OP is a valid index scaling factor for Thumb-2 address
4047    index operand.  i.e. 1, 2, 4 or 8.  */
4048 static bool
4049 thumb2_index_mul_operand (rtx op)
4050 {
4051   HOST_WIDE_INT val;
4052   
4053   if (GET_CODE(op) != CONST_INT)
4054     return false;
4055
4056   val = INTVAL(op);
4057   return (val == 1 || val == 2 || val == 4 || val == 8);
4058 }
4059   
4060 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
4061 static int
4062 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4063 {
4064   enum rtx_code code = GET_CODE (index);
4065
4066   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
4067   /* Standard coprocessor addressing modes.  */
4068   if (TARGET_HARD_FLOAT
4069       && (TARGET_FPA || TARGET_MAVERICK)
4070       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4071           || (TARGET_MAVERICK && mode == DImode)))
4072     return (code == CONST_INT && INTVAL (index) < 1024
4073             && INTVAL (index) > -1024
4074             && (INTVAL (index) & 3) == 0);
4075
4076   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4077     {
4078       /* For DImode assume values will usually live in core regs
4079          and only allow LDRD addressing modes.  */
4080       if (!TARGET_LDRD || mode != DImode)
4081         return (code == CONST_INT
4082                 && INTVAL (index) < 1024
4083                 && INTVAL (index) > -1024
4084                 && (INTVAL (index) & 3) == 0);
4085     }
4086
4087   if (TARGET_NEON
4088       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4089     return (code == CONST_INT
4090             && INTVAL (index) < 1016
4091             && INTVAL (index) > -1024
4092             && (INTVAL (index) & 3) == 0);
4093
4094   if (arm_address_register_rtx_p (index, strict_p)
4095       && (GET_MODE_SIZE (mode) <= 4))
4096     return 1;
4097
4098   if (mode == DImode || mode == DFmode)
4099     {
4100       HOST_WIDE_INT val = INTVAL (index);
4101       /* ??? Can we assume ldrd for thumb2?  */
4102       /* Thumb-2 ldrd only has reg+const addressing modes.  */
4103       if (code != CONST_INT)
4104         return 0;
4105
4106       /* ldrd supports offsets of +-1020.
4107          However the ldr fallback does not.  */
4108       return val > -256 && val < 256 && (val & 3) == 0;
4109     }
4110
4111   if (code == MULT)
4112     {
4113       rtx xiop0 = XEXP (index, 0);
4114       rtx xiop1 = XEXP (index, 1);
4115
4116       return ((arm_address_register_rtx_p (xiop0, strict_p)
4117                && thumb2_index_mul_operand (xiop1))
4118               || (arm_address_register_rtx_p (xiop1, strict_p)
4119                   && thumb2_index_mul_operand (xiop0)));
4120     }
4121   else if (code == ASHIFT)
4122     {
4123       rtx op = XEXP (index, 1);
4124
4125       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4126               && GET_CODE (op) == CONST_INT
4127               && INTVAL (op) > 0
4128               && INTVAL (op) <= 3);
4129     }
4130
4131   return (code == CONST_INT
4132           && INTVAL (index) < 4096
4133           && INTVAL (index) > -256);
4134 }
4135
4136 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
4137 static int
4138 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
4139 {
4140   int regno;
4141
4142   if (GET_CODE (x) != REG)
4143     return 0;
4144
4145   regno = REGNO (x);
4146
4147   if (strict_p)
4148     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
4149
4150   return (regno <= LAST_LO_REGNUM
4151           || regno > LAST_VIRTUAL_REGISTER
4152           || regno == FRAME_POINTER_REGNUM
4153           || (GET_MODE_SIZE (mode) >= 4
4154               && (regno == STACK_POINTER_REGNUM
4155                   || regno >= FIRST_PSEUDO_REGISTER
4156                   || x == hard_frame_pointer_rtx
4157                   || x == arg_pointer_rtx)));
4158 }
4159
4160 /* Return nonzero if x is a legitimate index register.  This is the case
4161    for any base register that can access a QImode object.  */
4162 inline static int
4163 thumb1_index_register_rtx_p (rtx x, int strict_p)
4164 {
4165   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4166 }
4167
4168 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4169
4170    The AP may be eliminated to either the SP or the FP, so we use the
4171    least common denominator, e.g. SImode, and offsets from 0 to 64.
4172
4173    ??? Verify whether the above is the right approach.
4174
4175    ??? Also, the FP may be eliminated to the SP, so perhaps that
4176    needs special handling also.
4177
4178    ??? Look at how the mips16 port solves this problem.  It probably uses
4179    better ways to solve some of these problems.
4180
4181    Although it is not incorrect, we don't accept QImode and HImode
4182    addresses based on the frame pointer or arg pointer until the
4183    reload pass starts.  This is so that eliminating such addresses
4184    into stack based ones won't produce impossible code.  */
4185 int
4186 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4187 {
4188   /* ??? Not clear if this is right.  Experiment.  */
4189   if (GET_MODE_SIZE (mode) < 4
4190       && !(reload_in_progress || reload_completed)
4191       && (reg_mentioned_p (frame_pointer_rtx, x)
4192           || reg_mentioned_p (arg_pointer_rtx, x)
4193           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4194           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4195           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4196           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4197     return 0;
4198
4199   /* Accept any base register.  SP only in SImode or larger.  */
4200   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4201     return 1;
4202
4203   /* This is PC relative data before arm_reorg runs.  */
4204   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4205            && GET_CODE (x) == SYMBOL_REF
4206            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4207     return 1;
4208
4209   /* This is PC relative data after arm_reorg runs.  */
4210   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
4211            && (GET_CODE (x) == LABEL_REF
4212                || (GET_CODE (x) == CONST
4213                    && GET_CODE (XEXP (x, 0)) == PLUS
4214                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4215                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4216     return 1;
4217
4218   /* Post-inc indexing only supported for SImode and larger.  */
4219   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4220            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4221     return 1;
4222
4223   else if (GET_CODE (x) == PLUS)
4224     {
4225       /* REG+REG address can be any two index registers.  */
4226       /* We disallow FRAME+REG addressing since we know that FRAME
4227          will be replaced with STACK, and SP relative addressing only
4228          permits SP+OFFSET.  */
4229       if (GET_MODE_SIZE (mode) <= 4
4230           && XEXP (x, 0) != frame_pointer_rtx
4231           && XEXP (x, 1) != frame_pointer_rtx
4232           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4233           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4234         return 1;
4235
4236       /* REG+const has 5-7 bit offset for non-SP registers.  */
4237       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4238                 || XEXP (x, 0) == arg_pointer_rtx)
4239                && GET_CODE (XEXP (x, 1)) == CONST_INT
4240                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4241         return 1;
4242
4243       /* REG+const has 10-bit offset for SP, but only SImode and
4244          larger is supported.  */
4245       /* ??? Should probably check for DI/DFmode overflow here
4246          just like GO_IF_LEGITIMATE_OFFSET does.  */
4247       else if (GET_CODE (XEXP (x, 0)) == REG
4248                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4249                && GET_MODE_SIZE (mode) >= 4
4250                && GET_CODE (XEXP (x, 1)) == CONST_INT
4251                && INTVAL (XEXP (x, 1)) >= 0
4252                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4253                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4254         return 1;
4255
4256       else if (GET_CODE (XEXP (x, 0)) == REG
4257                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4258                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4259                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4260                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4261                && GET_MODE_SIZE (mode) >= 4
4262                && GET_CODE (XEXP (x, 1)) == CONST_INT
4263                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4264         return 1;
4265     }
4266
4267   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4268            && GET_MODE_SIZE (mode) == 4
4269            && GET_CODE (x) == SYMBOL_REF
4270            && CONSTANT_POOL_ADDRESS_P (x)
4271            && ! (flag_pic
4272                  && symbol_mentioned_p (get_pool_constant (x))
4273                  && ! pcrel_constant_p (get_pool_constant (x))))
4274     return 1;
4275
4276   return 0;
4277 }
4278
4279 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4280    instruction of mode MODE.  */
4281 int
4282 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4283 {
4284   switch (GET_MODE_SIZE (mode))
4285     {
4286     case 1:
4287       return val >= 0 && val < 32;
4288
4289     case 2:
4290       return val >= 0 && val < 64 && (val & 1) == 0;
4291
4292     default:
4293       return (val >= 0
4294               && (val + GET_MODE_SIZE (mode)) <= 128
4295               && (val & 3) == 0);
4296     }
4297 }
4298
4299 /* Build the SYMBOL_REF for __tls_get_addr.  */
4300
4301 static GTY(()) rtx tls_get_addr_libfunc;
4302
4303 static rtx
4304 get_tls_get_addr (void)
4305 {
4306   if (!tls_get_addr_libfunc)
4307     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4308   return tls_get_addr_libfunc;
4309 }
4310
4311 static rtx
4312 arm_load_tp (rtx target)
4313 {
4314   if (!target)
4315     target = gen_reg_rtx (SImode);
4316
4317   if (TARGET_HARD_TP)
4318     {
4319       /* Can return in any reg.  */
4320       emit_insn (gen_load_tp_hard (target));
4321     }
4322   else
4323     {
4324       /* Always returned in r0.  Immediately copy the result into a pseudo,
4325          otherwise other uses of r0 (e.g. setting up function arguments) may
4326          clobber the value.  */
4327
4328       rtx tmp;
4329
4330       emit_insn (gen_load_tp_soft ());
4331
4332       tmp = gen_rtx_REG (SImode, 0);
4333       emit_move_insn (target, tmp);
4334     }
4335   return target;
4336 }
4337
4338 static rtx
4339 load_tls_operand (rtx x, rtx reg)
4340 {
4341   rtx tmp;
4342
4343   if (reg == NULL_RTX)
4344     reg = gen_reg_rtx (SImode);
4345
4346   tmp = gen_rtx_CONST (SImode, x);
4347
4348   emit_move_insn (reg, tmp);
4349
4350   return reg;
4351 }
4352
4353 static rtx
4354 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4355 {
4356   rtx insns, label, labelno, sum;
4357
4358   start_sequence ();
4359
4360   labelno = GEN_INT (pic_labelno++);
4361   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4362   label = gen_rtx_CONST (VOIDmode, label);
4363
4364   sum = gen_rtx_UNSPEC (Pmode,
4365                         gen_rtvec (4, x, GEN_INT (reloc), label,
4366                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4367                         UNSPEC_TLS);
4368   reg = load_tls_operand (sum, reg);
4369
4370   if (TARGET_ARM)
4371     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4372   else if (TARGET_THUMB2)
4373     {
4374       rtx tmp;
4375       /* Thumb-2 only allows very limited access to the PC.  Calculate
4376          the address in a temporary register.  */
4377       tmp = gen_reg_rtx (SImode);
4378       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4379       emit_insn (gen_addsi3(reg, reg, tmp));
4380     }
4381   else /* TARGET_THUMB1 */
4382     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4383
4384   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4385                                      Pmode, 1, reg, Pmode);
4386
4387   insns = get_insns ();
4388   end_sequence ();
4389
4390   return insns;
4391 }
4392
4393 rtx
4394 legitimize_tls_address (rtx x, rtx reg)
4395 {
4396   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4397   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4398
4399   switch (model)
4400     {
4401     case TLS_MODEL_GLOBAL_DYNAMIC:
4402       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4403       dest = gen_reg_rtx (Pmode);
4404       emit_libcall_block (insns, dest, ret, x);
4405       return dest;
4406
4407     case TLS_MODEL_LOCAL_DYNAMIC:
4408       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4409
4410       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4411          share the LDM result with other LD model accesses.  */
4412       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4413                             UNSPEC_TLS);
4414       dest = gen_reg_rtx (Pmode);
4415       emit_libcall_block (insns, dest, ret, eqv);
4416
4417       /* Load the addend.  */
4418       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4419                                UNSPEC_TLS);
4420       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4421       return gen_rtx_PLUS (Pmode, dest, addend);
4422
4423     case TLS_MODEL_INITIAL_EXEC:
4424       labelno = GEN_INT (pic_labelno++);
4425       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4426       label = gen_rtx_CONST (VOIDmode, label);
4427       sum = gen_rtx_UNSPEC (Pmode,
4428                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4429                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4430                             UNSPEC_TLS);
4431       reg = load_tls_operand (sum, reg);
4432
4433       if (TARGET_ARM)
4434         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4435       else if (TARGET_THUMB2)
4436         {
4437           rtx tmp;
4438           /* Thumb-2 only allows very limited access to the PC.  Calculate
4439              the address in a temporary register.  */
4440           tmp = gen_reg_rtx (SImode);
4441           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4442           emit_insn (gen_addsi3(reg, reg, tmp));
4443           emit_move_insn (reg, gen_const_mem (SImode, reg));
4444         }
4445       else
4446         {
4447           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4448           emit_move_insn (reg, gen_const_mem (SImode, reg));
4449         }
4450
4451       tp = arm_load_tp (NULL_RTX);
4452
4453       return gen_rtx_PLUS (Pmode, tp, reg);
4454
4455     case TLS_MODEL_LOCAL_EXEC:
4456       tp = arm_load_tp (NULL_RTX);
4457
4458       reg = gen_rtx_UNSPEC (Pmode,
4459                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4460                             UNSPEC_TLS);
4461       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4462
4463       return gen_rtx_PLUS (Pmode, tp, reg);
4464
4465     default:
4466       abort ();
4467     }
4468 }
4469
4470 /* Try machine-dependent ways of modifying an illegitimate address
4471    to be legitimate.  If we find one, return the new, valid address.  */
4472 rtx
4473 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4474 {
4475   if (arm_tls_symbol_p (x))
4476     return legitimize_tls_address (x, NULL_RTX);
4477
4478   if (GET_CODE (x) == PLUS)
4479     {
4480       rtx xop0 = XEXP (x, 0);
4481       rtx xop1 = XEXP (x, 1);
4482
4483       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4484         xop0 = force_reg (SImode, xop0);
4485
4486       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4487         xop1 = force_reg (SImode, xop1);
4488
4489       if (ARM_BASE_REGISTER_RTX_P (xop0)
4490           && GET_CODE (xop1) == CONST_INT)
4491         {
4492           HOST_WIDE_INT n, low_n;
4493           rtx base_reg, val;
4494           n = INTVAL (xop1);
4495
4496           /* VFP addressing modes actually allow greater offsets, but for
4497              now we just stick with the lowest common denominator.  */
4498           if (mode == DImode
4499               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4500             {
4501               low_n = n & 0x0f;
4502               n &= ~0x0f;
4503               if (low_n > 4)
4504                 {
4505                   n += 16;
4506                   low_n -= 16;
4507                 }
4508             }
4509           else
4510             {
4511               low_n = ((mode) == TImode ? 0
4512                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4513               n -= low_n;
4514             }
4515
4516           base_reg = gen_reg_rtx (SImode);
4517           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4518           emit_move_insn (base_reg, val);
4519           x = plus_constant (base_reg, low_n);
4520         }
4521       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4522         x = gen_rtx_PLUS (SImode, xop0, xop1);
4523     }
4524
4525   /* XXX We don't allow MINUS any more -- see comment in
4526      arm_legitimate_address_p ().  */
4527   else if (GET_CODE (x) == MINUS)
4528     {
4529       rtx xop0 = XEXP (x, 0);
4530       rtx xop1 = XEXP (x, 1);
4531
4532       if (CONSTANT_P (xop0))
4533         xop0 = force_reg (SImode, xop0);
4534
4535       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4536         xop1 = force_reg (SImode, xop1);
4537
4538       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4539         x = gen_rtx_MINUS (SImode, xop0, xop1);
4540     }
4541
4542   /* Make sure to take full advantage of the pre-indexed addressing mode
4543      with absolute addresses which often allows for the base register to
4544      be factorized for multiple adjacent memory references, and it might
4545      even allows for the mini pool to be avoided entirely. */
4546   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4547     {
4548       unsigned int bits;
4549       HOST_WIDE_INT mask, base, index;
4550       rtx base_reg;
4551
4552       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4553          use a 8-bit index. So let's use a 12-bit index for SImode only and
4554          hope that arm_gen_constant will enable ldrb to use more bits. */
4555       bits = (mode == SImode) ? 12 : 8;
4556       mask = (1 << bits) - 1;
4557       base = INTVAL (x) & ~mask;
4558       index = INTVAL (x) & mask;
4559       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4560         {
4561           /* It'll most probably be more efficient to generate the base
4562              with more bits set and use a negative index instead. */
4563           base |= mask;
4564           index -= mask;
4565         }
4566       base_reg = force_reg (SImode, GEN_INT (base));
4567       x = plus_constant (base_reg, index);
4568     }
4569
4570   if (flag_pic)
4571     {
4572       /* We need to find and carefully transform any SYMBOL and LABEL
4573          references; so go back to the original address expression.  */
4574       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4575
4576       if (new_x != orig_x)
4577         x = new_x;
4578     }
4579
4580   return x;
4581 }
4582
4583
4584 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4585    to be legitimate.  If we find one, return the new, valid address.  */
4586 rtx
4587 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4588 {
4589   if (arm_tls_symbol_p (x))
4590     return legitimize_tls_address (x, NULL_RTX);
4591
4592   if (GET_CODE (x) == PLUS
4593       && GET_CODE (XEXP (x, 1)) == CONST_INT
4594       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4595           || INTVAL (XEXP (x, 1)) < 0))
4596     {
4597       rtx xop0 = XEXP (x, 0);
4598       rtx xop1 = XEXP (x, 1);
4599       HOST_WIDE_INT offset = INTVAL (xop1);
4600
4601       /* Try and fold the offset into a biasing of the base register and
4602          then offsetting that.  Don't do this when optimizing for space
4603          since it can cause too many CSEs.  */
4604       if (optimize_size && offset >= 0
4605           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4606         {
4607           HOST_WIDE_INT delta;
4608
4609           if (offset >= 256)
4610             delta = offset - (256 - GET_MODE_SIZE (mode));
4611           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4612             delta = 31 * GET_MODE_SIZE (mode);
4613           else
4614             delta = offset & (~31 * GET_MODE_SIZE (mode));
4615
4616           xop0 = force_operand (plus_constant (xop0, offset - delta),
4617                                 NULL_RTX);
4618           x = plus_constant (xop0, delta);
4619         }
4620       else if (offset < 0 && offset > -256)
4621         /* Small negative offsets are best done with a subtract before the
4622            dereference, forcing these into a register normally takes two
4623            instructions.  */
4624         x = force_operand (x, NULL_RTX);
4625       else
4626         {
4627           /* For the remaining cases, force the constant into a register.  */
4628           xop1 = force_reg (SImode, xop1);
4629           x = gen_rtx_PLUS (SImode, xop0, xop1);
4630         }
4631     }
4632   else if (GET_CODE (x) == PLUS
4633            && s_register_operand (XEXP (x, 1), SImode)
4634            && !s_register_operand (XEXP (x, 0), SImode))
4635     {
4636       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4637
4638       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4639     }
4640
4641   if (flag_pic)
4642     {
4643       /* We need to find and carefully transform any SYMBOL and LABEL
4644          references; so go back to the original address expression.  */
4645       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4646
4647       if (new_x != orig_x)
4648         x = new_x;
4649     }
4650
4651   return x;
4652 }
4653
4654 rtx
4655 thumb_legitimize_reload_address (rtx *x_p,
4656                                  enum machine_mode mode,
4657                                  int opnum, int type,
4658                                  int ind_levels ATTRIBUTE_UNUSED)
4659 {
4660   rtx x = *x_p;
4661
4662   if (GET_CODE (x) == PLUS
4663       && GET_MODE_SIZE (mode) < 4
4664       && REG_P (XEXP (x, 0))
4665       && XEXP (x, 0) == stack_pointer_rtx
4666       && GET_CODE (XEXP (x, 1)) == CONST_INT
4667       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4668     {
4669       rtx orig_x = x;
4670
4671       x = copy_rtx (x);
4672       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4673                    Pmode, VOIDmode, 0, 0, opnum, type);
4674       return x;
4675     }
4676
4677   /* If both registers are hi-regs, then it's better to reload the
4678      entire expression rather than each register individually.  That
4679      only requires one reload register rather than two.  */
4680   if (GET_CODE (x) == PLUS
4681       && REG_P (XEXP (x, 0))
4682       && REG_P (XEXP (x, 1))
4683       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4684       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4685     {
4686       rtx orig_x = x;
4687
4688       x = copy_rtx (x);
4689       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4690                    Pmode, VOIDmode, 0, 0, opnum, type);
4691       return x;
4692     }
4693
4694   return NULL;
4695 }
4696
4697 /* Test for various thread-local symbols.  */
4698
4699 /* Return TRUE if X is a thread-local symbol.  */
4700
4701 static bool
4702 arm_tls_symbol_p (rtx x)
4703 {
4704   if (! TARGET_HAVE_TLS)
4705     return false;
4706
4707   if (GET_CODE (x) != SYMBOL_REF)
4708     return false;
4709
4710   return SYMBOL_REF_TLS_MODEL (x) != 0;
4711 }
4712
4713 /* Helper for arm_tls_referenced_p.  */
4714
4715 static int
4716 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4717 {
4718   if (GET_CODE (*x) == SYMBOL_REF)
4719     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4720
4721   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4722      TLS offsets, not real symbol references.  */
4723   if (GET_CODE (*x) == UNSPEC
4724       && XINT (*x, 1) == UNSPEC_TLS)
4725     return -1;
4726
4727   return 0;
4728 }
4729
4730 /* Return TRUE if X contains any TLS symbol references.  */
4731
4732 bool
4733 arm_tls_referenced_p (rtx x)
4734 {
4735   if (! TARGET_HAVE_TLS)
4736     return false;
4737
4738   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4739 }
4740
4741 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
4742
4743 bool
4744 arm_cannot_force_const_mem (rtx x)
4745 {
4746   rtx base, offset;
4747
4748   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
4749     {
4750       split_const (x, &base, &offset);
4751       if (GET_CODE (base) == SYMBOL_REF
4752           && !offset_within_block_p (base, INTVAL (offset)))
4753         return true;
4754     }
4755   return arm_tls_referenced_p (x);
4756 }
4757 \f
4758 #define REG_OR_SUBREG_REG(X)                                            \
4759   (GET_CODE (X) == REG                                                  \
4760    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4761
4762 #define REG_OR_SUBREG_RTX(X)                    \
4763    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4764
4765 #ifndef COSTS_N_INSNS
4766 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4767 #endif
4768 static inline int
4769 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4770 {
4771   enum machine_mode mode = GET_MODE (x);
4772
4773   switch (code)
4774     {
4775     case ASHIFT:
4776     case ASHIFTRT:
4777     case LSHIFTRT:
4778     case ROTATERT:
4779     case PLUS:
4780     case MINUS:
4781     case COMPARE:
4782     case NEG:
4783     case NOT:
4784       return COSTS_N_INSNS (1);
4785
4786     case MULT:
4787       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4788         {
4789           int cycles = 0;
4790           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4791
4792           while (i)
4793             {
4794               i >>= 2;
4795               cycles++;
4796             }
4797           return COSTS_N_INSNS (2) + cycles;
4798         }
4799       return COSTS_N_INSNS (1) + 16;
4800
4801     case SET:
4802       return (COSTS_N_INSNS (1)
4803               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4804                      + GET_CODE (SET_DEST (x)) == MEM));
4805
4806     case CONST_INT:
4807       if (outer == SET)
4808         {
4809           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4810             return 0;
4811           if (thumb_shiftable_const (INTVAL (x)))
4812             return COSTS_N_INSNS (2);
4813           return COSTS_N_INSNS (3);
4814         }
4815       else if ((outer == PLUS || outer == COMPARE)
4816                && INTVAL (x) < 256 && INTVAL (x) > -256)
4817         return 0;
4818       else if (outer == AND
4819                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4820         return COSTS_N_INSNS (1);
4821       else if (outer == ASHIFT || outer == ASHIFTRT
4822                || outer == LSHIFTRT)
4823         return 0;
4824       return COSTS_N_INSNS (2);
4825
4826     case CONST:
4827     case CONST_DOUBLE:
4828     case LABEL_REF:
4829     case SYMBOL_REF:
4830       return COSTS_N_INSNS (3);
4831
4832     case UDIV:
4833     case UMOD:
4834     case DIV:
4835     case MOD:
4836       return 100;
4837
4838     case TRUNCATE:
4839       return 99;
4840
4841     case AND:
4842     case XOR:
4843     case IOR:
4844       /* XXX guess.  */
4845       return 8;
4846
4847     case MEM:
4848       /* XXX another guess.  */
4849       /* Memory costs quite a lot for the first word, but subsequent words
4850          load at the equivalent of a single insn each.  */
4851       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4852               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4853                  ? 4 : 0));
4854
4855     case IF_THEN_ELSE:
4856       /* XXX a guess.  */
4857       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4858         return 14;
4859       return 2;
4860
4861     case ZERO_EXTEND:
4862       /* XXX still guessing.  */
4863       switch (GET_MODE (XEXP (x, 0)))
4864         {
4865         case QImode:
4866           return (1 + (mode == DImode ? 4 : 0)
4867                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4868
4869         case HImode:
4870           return (4 + (mode == DImode ? 4 : 0)
4871                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4872
4873         case SImode:
4874           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4875
4876         default:
4877           return 99;
4878         }
4879
4880     default:
4881       return 99;
4882     }
4883 }
4884
4885
4886 /* Worker routine for arm_rtx_costs.  */
4887 /* ??? This needs updating for thumb2.  */
4888 static inline int
4889 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4890 {
4891   enum machine_mode mode = GET_MODE (x);
4892   enum rtx_code subcode;
4893   int extra_cost;
4894
4895   switch (code)
4896     {
4897     case MEM:
4898       /* Memory costs quite a lot for the first word, but subsequent words
4899          load at the equivalent of a single insn each.  */
4900       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4901               + (GET_CODE (x) == SYMBOL_REF
4902                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4903
4904     case DIV:
4905     case MOD:
4906     case UDIV:
4907     case UMOD:
4908       return optimize_size ? COSTS_N_INSNS (2) : 100;
4909
4910     case ROTATE:
4911       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4912         return 4;
4913       /* Fall through */
4914     case ROTATERT:
4915       if (mode != SImode)
4916         return 8;
4917       /* Fall through */
4918     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4919       if (mode == DImode)
4920         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4921                 + ((GET_CODE (XEXP (x, 0)) == REG
4922                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4923                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4924                    ? 0 : 8));
4925
4926       extra_cost = 1;
4927       /* Increase the cost of complex shifts because they aren't any faster,
4928          and reduce dual issue opportunities.  */
4929       if (arm_tune_cortex_a9
4930           && outer != SET && GET_CODE (XEXP (x, 1)) != CONST_INT)
4931         extra_cost++;
4932
4933       return (extra_cost + ((GET_CODE (XEXP (x, 0)) == REG
4934                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4935                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4936                    ? 0 : 4)
4937               + ((GET_CODE (XEXP (x, 1)) == REG
4938                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4939                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4940                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4941                  ? 0 : 4));
4942
4943     case MINUS:
4944       if (GET_CODE (XEXP (x, 1)) == MULT && mode == SImode && arm_arch_thumb2)
4945         {
4946           extra_cost = rtx_cost (XEXP (x, 1), code, true);
4947           if (!REG_OR_SUBREG_REG (XEXP (x, 0)))
4948             extra_cost += 4 * ARM_NUM_REGS (mode);
4949           return extra_cost;
4950         }
4951
4952       if (mode == DImode)
4953         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4954                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4955                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4956                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4957                    ? 0 : 8));
4958
4959       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4960         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4961                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4962                           && arm_const_double_rtx (XEXP (x, 1))))
4963                      ? 0 : 8)
4964                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4965                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4966                         && arm_const_double_rtx (XEXP (x, 0))))
4967                    ? 0 : 8));
4968
4969       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4970             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4971             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4972           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4973                || subcode == ASHIFTRT || subcode == LSHIFTRT
4974                || subcode == ROTATE || subcode == ROTATERT
4975                || (subcode == MULT
4976                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4977                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4978                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4979               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4980               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4981                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4982               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4983         return 1;
4984       /* Fall through */
4985
4986     case PLUS:
4987       if (arm_arch6 && mode == SImode
4988           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4989               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4990         return 1 + (GET_CODE (XEXP (XEXP (x, 0), 0)) == MEM ? 10 : 0)
4991                  + (GET_CODE (XEXP (x, 1)) == MEM ? 10 : 0);
4992
4993       if (GET_CODE (XEXP (x, 0)) == MULT)
4994         {
4995           extra_cost = rtx_cost (XEXP (x, 0), code, true);
4996           if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4997             extra_cost += 4 * ARM_NUM_REGS (mode);
4998           return extra_cost;
4999         }
5000
5001       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5002         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
5003                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
5004                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
5005                         && arm_const_double_rtx (XEXP (x, 1))))
5006                    ? 0 : 8));
5007
5008       /* Fall through */
5009     case AND: case XOR: case IOR:
5010       extra_cost = 0;
5011
5012       /* Normally the frame registers will be spilt into reg+const during
5013          reload, so it is a bad idea to combine them with other instructions,
5014          since then they might not be moved outside of loops.  As a compromise
5015          we allow integration with ops that have a constant as their second
5016          operand.  */
5017       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
5018            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
5019            && GET_CODE (XEXP (x, 1)) != CONST_INT)
5020           || (REG_OR_SUBREG_REG (XEXP (x, 0))
5021               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
5022         extra_cost = 4;
5023
5024       if (mode == DImode)
5025         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
5026                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
5027                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
5028                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
5029                    ? 0 : 8));
5030
5031       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
5032         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
5033                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
5034                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
5035                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
5036                    ? 0 : 4));
5037
5038       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
5039         return (1 + extra_cost
5040                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
5041                      || subcode == LSHIFTRT || subcode == ASHIFTRT
5042                      || subcode == ROTATE || subcode == ROTATERT
5043                      || (subcode == MULT
5044                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5045                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
5046                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
5047                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
5048                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1))
5049                          && !arm_tune_cortex_a9)
5050                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
5051                    ? 0 : 4));
5052
5053       return 8;
5054
5055     case MULT:
5056       /* This should have been handled by the CPU specific routines.  */
5057       gcc_unreachable ();
5058
5059     case TRUNCATE:
5060       if (arm_arch3m && mode == SImode
5061           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5062           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5063           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
5064               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5065           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
5066               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
5067         return 8;
5068       return 99;
5069
5070     case NEG:
5071       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5072         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
5073       /* Fall through */
5074     case NOT:
5075       if (mode == DImode)
5076         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
5077
5078       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
5079
5080     case IF_THEN_ELSE:
5081       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5082         return 14;
5083       return 2;
5084
5085     case COMPARE:
5086       return 1;
5087
5088     case ABS:
5089       return 4 + (mode == DImode ? 4 : 0);
5090
5091     case SIGN_EXTEND:
5092       if (arm_arch_thumb2 && mode == SImode)
5093         return 1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0);
5094
5095       if (GET_MODE (XEXP (x, 0)) == QImode)
5096         return (4 + (mode == DImode ? 4 : 0)
5097                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5098       /* Fall through */
5099     case ZERO_EXTEND:
5100       if (arm_arch6 && mode == SImode)
5101         return 1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0);
5102
5103       switch (GET_MODE (XEXP (x, 0)))
5104         {
5105         case QImode:
5106           return (1 + (mode == DImode ? 4 : 0)
5107                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5108
5109         case HImode:
5110           return (4 + (mode == DImode ? 4 : 0)
5111                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5112
5113         case SImode:
5114           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5115
5116         case V8QImode:
5117         case V4HImode:
5118         case V2SImode:
5119         case V4QImode:
5120         case V2HImode:
5121             return 1;
5122
5123         default:
5124           gcc_unreachable ();
5125         }
5126       gcc_unreachable ();
5127
5128     case CONST_INT:
5129       if (const_ok_for_arm (INTVAL (x)))
5130         return outer == SET ? 2 : -1;
5131       else if (outer == AND
5132                && const_ok_for_arm (~INTVAL (x)))
5133         return -1;
5134       else if ((outer == COMPARE
5135                 || outer == PLUS || outer == MINUS)
5136                && const_ok_for_arm (-INTVAL (x)))
5137         return -1;
5138       else
5139         return 5;
5140
5141     case CONST:
5142     case LABEL_REF:
5143     case SYMBOL_REF:
5144       return 6;
5145
5146     case HIGH:
5147     case LO_SUM:
5148       return (outer == SET) ? 1 : -1;
5149
5150     case CONST_DOUBLE:
5151       if (arm_const_double_rtx (x) || vfp3_const_double_rtx (x))
5152         return outer == SET ? 2 : -1;
5153       else if ((outer == COMPARE || outer == PLUS)
5154                && neg_const_double_rtx_ok_for_fpa (x))
5155         return -1;
5156       return 7;
5157
5158     default:
5159       return 99;
5160     }
5161 }
5162
5163 /* RTX costs when optimizing for size.  */
5164 static bool
5165 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
5166 {
5167   enum machine_mode mode = GET_MODE (x);
5168
5169   if (TARGET_THUMB1)
5170     {
5171       /* XXX TBD.  For now, use the standard costs.  */
5172       *total = thumb1_rtx_costs (x, code, outer_code);
5173       return true;
5174     }
5175
5176   /* FIXME: This makes no attempt to prefer narrow Thumb-2 instructions.  */
5177   switch (code)
5178     {
5179     case MEM:
5180       /* A memory access costs 1 insn if the mode is small, or the address is
5181          a single register, otherwise it costs one insn per word.  */
5182       if (REG_P (XEXP (x, 0)))
5183         *total = COSTS_N_INSNS (1);
5184       else
5185         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5186       return true;
5187
5188     case DIV:
5189     case MOD:
5190     case UDIV:
5191     case UMOD:
5192       /* Needs a libcall, so it costs about this.  */
5193       *total = COSTS_N_INSNS (2);
5194       return false;
5195
5196     case ROTATE:
5197       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5198         {
5199           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code, false);
5200           return true;
5201         }
5202       /* Fall through */
5203     case ROTATERT:
5204     case ASHIFT:
5205     case LSHIFTRT:
5206     case ASHIFTRT:
5207       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5208         {
5209           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code, false);
5210           return true;
5211         }
5212       else if (mode == SImode)
5213         {
5214           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code, false);
5215           /* Slightly disparage register shifts, but not by much.  */
5216           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5217             *total += 1 + rtx_cost (XEXP (x, 1), code, false);
5218           return true;
5219         }
5220
5221       /* Needs a libcall.  */
5222       *total = COSTS_N_INSNS (2);
5223       return false;
5224
5225     case MINUS:
5226       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5227         {
5228           *total = COSTS_N_INSNS (1);
5229           return false;
5230         }
5231
5232       if (mode == SImode)
5233         {
5234           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5235           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5236
5237           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5238               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5239               || subcode1 == ROTATE || subcode1 == ROTATERT
5240               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5241               || subcode1 == ASHIFTRT)
5242             {
5243               /* It's just the cost of the two operands.  */
5244               *total = 0;
5245               return false;
5246             }
5247
5248           *total = COSTS_N_INSNS (1);
5249           return false;
5250         }
5251
5252       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5253       return false;
5254
5255     case PLUS:
5256       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5257         {
5258           *total = COSTS_N_INSNS (1);
5259           return false;
5260         }
5261
5262       /* Fall through */
5263     case AND: case XOR: case IOR:
5264       if (mode == SImode)
5265         {
5266           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5267
5268           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5269               || subcode == LSHIFTRT || subcode == ASHIFTRT
5270               || (code == AND && subcode == NOT))
5271             {
5272               /* It's just the cost of the two operands.  */
5273               *total = 0;
5274               return false;
5275             }
5276         }
5277
5278       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5279       return false;
5280
5281     case MULT:
5282       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5283       return false;
5284
5285     case NEG:
5286       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5287         {
5288           *total = COSTS_N_INSNS (1);
5289           return false;
5290         }
5291
5292       /* Fall through */
5293     case NOT:
5294       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5295
5296       return false;
5297
5298     case IF_THEN_ELSE:
5299       *total = 0;
5300       return false;
5301
5302     case COMPARE:
5303       if (cc_register (XEXP (x, 0), VOIDmode))
5304         * total = 0;
5305       else
5306         *total = COSTS_N_INSNS (1);
5307       return false;
5308
5309     case ABS:
5310       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5311         *total = COSTS_N_INSNS (1);
5312       else
5313         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5314       return false;
5315
5316     case SIGN_EXTEND:
5317       *total = 0;
5318       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5319         {
5320           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5321             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5322         }
5323       if (mode == DImode)
5324         *total += COSTS_N_INSNS (1);
5325       return false;
5326
5327     case ZERO_EXTEND:
5328       *total = 0;
5329       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5330         {
5331           switch (GET_MODE (XEXP (x, 0)))
5332             {
5333             case QImode:
5334               *total += COSTS_N_INSNS (1);
5335               break;
5336
5337             case HImode:
5338               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5339
5340             case SImode:
5341               break;
5342
5343             default:
5344               *total += COSTS_N_INSNS (2);
5345             }
5346         }
5347
5348       if (mode == DImode)
5349         *total += COSTS_N_INSNS (1);
5350
5351       return false;
5352
5353     case CONST_INT:
5354       if (const_ok_for_arm (INTVAL (x)))
5355         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5356       else if (const_ok_for_arm (~INTVAL (x)))
5357         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5358       else if (const_ok_for_arm (-INTVAL (x)))
5359         {
5360           if (outer_code == COMPARE || outer_code == PLUS
5361               || outer_code == MINUS)
5362             *total = 0;
5363           else
5364             *total = COSTS_N_INSNS (1);
5365         }
5366       else
5367         *total = COSTS_N_INSNS (2);
5368       return true;
5369
5370     case CONST:
5371     case LABEL_REF:
5372     case SYMBOL_REF:
5373       *total = COSTS_N_INSNS (2);
5374       return true;
5375
5376     case CONST_DOUBLE:
5377       *total = COSTS_N_INSNS (4);
5378       return true;
5379
5380     case HIGH:
5381     case LO_SUM:
5382       /* We prefer constant pool entries to MOVW/MOVT pairs, so bump the
5383          cost of these slightly.  */
5384       *total = COSTS_N_INSNS (1) + 1;
5385       return true;
5386
5387     default:
5388       if (mode != VOIDmode)
5389         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5390       else
5391         *total = COSTS_N_INSNS (4); /* How knows?  */
5392       return false;
5393     }
5394 }
5395
5396 /* RTX costs when optimizing for size.  */
5397 static bool
5398 arm_rtx_costs (rtx x, int code, int outer_code, int *total, bool speed)
5399 {
5400   if (!speed)
5401     return arm_size_rtx_costs (x, code, outer_code, total);
5402   else
5403     return all_cores[(int)arm_tune].rtx_costs (x, code, outer_code, total);
5404 }
5405
5406 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
5407    supported on any "slowmul" cores, so it can be ignored.  */
5408
5409 static bool
5410 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5411 {
5412   enum machine_mode mode = GET_MODE (x);
5413
5414   if (TARGET_THUMB)
5415     {
5416       *total = thumb1_rtx_costs (x, code, outer_code);
5417       return true;
5418     }
5419
5420   switch (code)
5421     {
5422     case MULT:
5423       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5424           || mode == DImode)
5425         {
5426           *total = 30;
5427           return true;
5428         }
5429
5430       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5431         {
5432           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5433                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5434           int cost, const_ok = const_ok_for_arm (i);
5435           int j, booth_unit_size;
5436
5437           /* Tune as appropriate.  */
5438           cost = const_ok ? 4 : 8;
5439           booth_unit_size = 2;
5440           for (j = 0; i && j < 32; j += booth_unit_size)
5441             {
5442               i >>= booth_unit_size;
5443               cost += 2;
5444             }
5445
5446           *total = cost;
5447           return true;
5448         }
5449
5450       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5451                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5452       return true;
5453
5454     default:
5455       *total = arm_rtx_costs_1 (x, code, outer_code);
5456       return true;
5457     }
5458 }
5459
5460
5461 /* RTX cost for cores with a fast multiply unit (M variants).  */
5462
5463 static bool
5464 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5465 {
5466   enum machine_mode mode = GET_MODE (x);
5467
5468   if (TARGET_THUMB1)
5469     {
5470       *total = thumb1_rtx_costs (x, code, outer_code);
5471       return true;
5472     }
5473
5474   /* ??? should thumb2 use different costs?  */
5475   switch (code)
5476     {
5477     case MULT:
5478       /* There is no point basing this on the tuning, since it is always the
5479          fast variant if it exists at all.  */
5480       if (mode == DImode
5481           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5482           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5483               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5484         {
5485           *total = 8;
5486           return true;
5487         }
5488
5489
5490       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5491           || mode == DImode)
5492         {
5493           *total = 30;
5494           return true;
5495         }
5496
5497       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5498         {
5499           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5500                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5501           int cost, const_ok = const_ok_for_arm (i);
5502           int j, booth_unit_size;
5503
5504           /* Tune as appropriate.  */
5505           cost = const_ok ? 4 : 8;
5506           booth_unit_size = 8;
5507           for (j = 0; i && j < 32; j += booth_unit_size)
5508             {
5509               i >>= booth_unit_size;
5510               cost += 2;
5511             }
5512
5513           *total = cost;
5514           return true;
5515         }
5516
5517       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5518                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5519       return true;
5520
5521     default:
5522       *total = arm_rtx_costs_1 (x, code, outer_code);
5523       return true;
5524     }
5525 }
5526
5527
5528 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
5529    so it can be ignored.  */
5530
5531 static bool
5532 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
5533 {
5534   enum machine_mode mode = GET_MODE (x);
5535
5536   if (TARGET_THUMB)
5537     {
5538       *total = thumb1_rtx_costs (x, code, outer_code);
5539       return true;
5540     }
5541
5542   switch (code)
5543     {
5544     case MULT:
5545       /* There is no point basing this on the tuning, since it is always the
5546          fast variant if it exists at all.  */
5547       if (mode == DImode
5548           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5549           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5550               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5551         {
5552           *total = 8;
5553           return true;
5554         }
5555
5556
5557       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5558           || mode == DImode)
5559         {
5560           *total = 30;
5561           return true;
5562         }
5563
5564       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5565         {
5566           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5567                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5568           int cost, const_ok = const_ok_for_arm (i);
5569           unsigned HOST_WIDE_INT masked_const;
5570
5571           /* The cost will be related to two insns.
5572              First a load of the constant (MOV or LDR), then a multiply.  */
5573           cost = 2;
5574           if (! const_ok)
5575             cost += 1;      /* LDR is probably more expensive because
5576                                of longer result latency.  */
5577           masked_const = i & 0xffff8000;
5578           if (masked_const != 0 && masked_const != 0xffff8000)
5579             {
5580               masked_const = i & 0xf8000000;
5581               if (masked_const == 0 || masked_const == 0xf8000000)
5582                 cost += 1;
5583               else
5584                 cost += 2;
5585             }
5586           *total = cost;
5587           return true;
5588         }
5589
5590       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5591                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5592       return true;
5593
5594     case COMPARE:
5595       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5596          will stall until the multiplication is complete.  */
5597       if (GET_CODE (XEXP (x, 0)) == MULT)
5598         *total = 4 + rtx_cost (XEXP (x, 0), code, true);
5599       else
5600         *total = arm_rtx_costs_1 (x, code, outer_code);
5601       return true;
5602
5603     default:
5604       *total = arm_rtx_costs_1 (x, code, outer_code);
5605       return true;
5606     }
5607 }
5608
5609
5610 /* RTX costs for 9e (and later) cores.  */
5611
5612 static bool
5613 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5614 {
5615   enum machine_mode mode = GET_MODE (x);
5616   int nonreg_cost;
5617   int cost;
5618
5619   if (TARGET_THUMB1)
5620     {
5621       switch (code)
5622         {
5623         case MULT:
5624           *total = COSTS_N_INSNS (3);
5625           return true;
5626
5627         default:
5628           *total = thumb1_rtx_costs (x, code, outer_code);
5629           return true;
5630         }
5631     }
5632
5633   switch (code)
5634     {
5635     case MULT:
5636       /* There is no point basing this on the tuning, since it is always the
5637          fast variant if it exists at all.  */
5638       if (mode == DImode
5639           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5640           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5641               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5642         {
5643           *total = 3;
5644           return true;
5645         }
5646
5647
5648       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5649         {
5650           *total = 30;
5651           return true;
5652         }
5653       if (mode == DImode)
5654         {
5655           cost = 7;
5656           nonreg_cost = 8;
5657         }
5658       else
5659         {
5660           cost = 2;
5661           nonreg_cost = 4;
5662         }
5663
5664
5665       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5666                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5667       return true;
5668
5669     default:
5670       *total = arm_rtx_costs_1 (x, code, outer_code);
5671       return true;
5672     }
5673 }
5674 /* All address computations that can be done are free, but rtx cost returns
5675    the same for practically all of them.  So we weight the different types
5676    of address here in the order (most pref first):
5677    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5678 static inline int
5679 arm_arm_address_cost (rtx x)
5680 {
5681   enum rtx_code c  = GET_CODE (x);
5682
5683   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5684     return 0;
5685   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5686     return 10;
5687
5688   if (c == PLUS || c == MINUS)
5689     {
5690       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5691         return 2;
5692
5693       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5694         return 3;
5695
5696       return 4;
5697     }
5698
5699   return 6;
5700 }
5701
5702 static inline int
5703 arm_thumb_address_cost (rtx x)
5704 {
5705   enum rtx_code c  = GET_CODE (x);
5706
5707   if (c == REG)
5708     return 1;
5709   if (c == PLUS
5710       && GET_CODE (XEXP (x, 0)) == REG
5711       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5712     return 1;
5713
5714   return 2;
5715 }
5716
5717 static int
5718 arm_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
5719 {
5720   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5721 }
5722
5723 static int
5724 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5725 {
5726   rtx i_pat, d_pat;
5727
5728   /* Some true dependencies can have a higher cost depending
5729      on precisely how certain input operands are used.  */
5730   if (arm_tune_xscale
5731       && REG_NOTE_KIND (link) == 0
5732       && recog_memoized (insn) >= 0
5733       && recog_memoized (dep) >= 0)
5734     {
5735       int shift_opnum = get_attr_shift (insn);
5736       enum attr_type attr_type = get_attr_type (dep);
5737
5738       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5739          operand for INSN.  If we have a shifted input operand and the
5740          instruction we depend on is another ALU instruction, then we may
5741          have to account for an additional stall.  */
5742       if (shift_opnum != 0
5743           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5744         {
5745           rtx shifted_operand;
5746           int opno;
5747
5748           /* Get the shifted operand.  */
5749           extract_insn (insn);
5750           shifted_operand = recog_data.operand[shift_opnum];
5751
5752           /* Iterate over all the operands in DEP.  If we write an operand
5753              that overlaps with SHIFTED_OPERAND, then we have increase the
5754              cost of this dependency.  */
5755           extract_insn (dep);
5756           preprocess_constraints ();
5757           for (opno = 0; opno < recog_data.n_operands; opno++)
5758             {
5759               /* We can ignore strict inputs.  */
5760               if (recog_data.operand_type[opno] == OP_IN)
5761                 continue;
5762
5763               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5764                                            shifted_operand))
5765                 return 2;
5766             }
5767         }
5768     }
5769
5770   /* XXX This is not strictly true for the FPA.  */
5771   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5772       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5773     return 0;
5774
5775   /* Call insns don't incur a stall, even if they follow a load.  */
5776   if (REG_NOTE_KIND (link) == 0
5777       && GET_CODE (insn) == CALL_INSN)
5778     return 1;
5779
5780   if ((i_pat = single_set (insn)) != NULL
5781       && GET_CODE (SET_SRC (i_pat)) == MEM
5782       && (d_pat = single_set (dep)) != NULL
5783       && GET_CODE (SET_DEST (d_pat)) == MEM)
5784     {
5785       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5786       /* This is a load after a store, there is no conflict if the load reads
5787          from a cached area.  Assume that loads from the stack, and from the
5788          constant pool are cached, and that others will miss.  This is a
5789          hack.  */
5790
5791       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5792           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5793           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5794           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5795         return 1;
5796     }
5797
5798   return cost;
5799 }
5800
5801 static int fp_consts_inited = 0;
5802
5803 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5804 static const char * const strings_fp[8] =
5805 {
5806   "0",   "1",   "2",   "3",
5807   "4",   "5",   "0.5", "10"
5808 };
5809
5810 static REAL_VALUE_TYPE values_fp[8];
5811
5812 static void
5813 init_fp_table (void)
5814 {
5815   int i;
5816   REAL_VALUE_TYPE r;
5817
5818   if (TARGET_VFP)
5819     fp_consts_inited = 1;
5820   else
5821     fp_consts_inited = 8;
5822
5823   for (i = 0; i < fp_consts_inited; i++)
5824     {
5825       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5826       values_fp[i] = r;
5827     }
5828 }
5829
5830 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5831 int
5832 arm_const_double_rtx (rtx x)
5833 {
5834   REAL_VALUE_TYPE r;
5835   int i;
5836
5837   if (!fp_consts_inited)
5838     init_fp_table ();
5839
5840   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5841   if (REAL_VALUE_MINUS_ZERO (r))
5842     return 0;
5843
5844   for (i = 0; i < fp_consts_inited; i++)
5845     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5846       return 1;
5847
5848   return 0;
5849 }
5850
5851 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5852 int
5853 neg_const_double_rtx_ok_for_fpa (rtx x)
5854 {
5855   REAL_VALUE_TYPE r;
5856   int i;
5857
5858   if (!fp_consts_inited)
5859     init_fp_table ();
5860
5861   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5862   r = REAL_VALUE_NEGATE (r);
5863   if (REAL_VALUE_MINUS_ZERO (r))
5864     return 0;
5865
5866   for (i = 0; i < 8; i++)
5867     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5868       return 1;
5869
5870   return 0;
5871 }
5872
5873
5874 /* VFPv3 has a fairly wide range of representable immediates, formed from
5875    "quarter-precision" floating-point values. These can be evaluated using this
5876    formula (with ^ for exponentiation):
5877
5878      -1^s * n * 2^-r
5879
5880    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
5881    16 <= n <= 31 and 0 <= r <= 7.
5882
5883    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
5884
5885      - A (most-significant) is the sign bit.
5886      - BCD are the exponent (encoded as r XOR 3).
5887      - EFGH are the mantissa (encoded as n - 16).
5888 */
5889
5890 /* Return an integer index for a VFPv3 immediate operand X suitable for the
5891    fconst[sd] instruction, or -1 if X isn't suitable.  */
5892 static int
5893 vfp3_const_double_index (rtx x)
5894 {
5895   REAL_VALUE_TYPE r, m;
5896   int sign, exponent;
5897   unsigned HOST_WIDE_INT mantissa, mant_hi;
5898   unsigned HOST_WIDE_INT mask;
5899   HOST_WIDE_INT m1, m2;
5900   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
5901
5902   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
5903     return -1;
5904
5905   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5906
5907   /* We can't represent these things, so detect them first.  */
5908   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
5909     return -1;
5910
5911   /* Extract sign, exponent and mantissa.  */
5912   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
5913   r = REAL_VALUE_ABS (r);
5914   exponent = REAL_EXP (&r);
5915   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
5916      highest (sign) bit, with a fixed binary point at bit point_pos.
5917      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
5918      bits for the mantissa, this may fail (low bits would be lost).  */
5919   real_ldexp (&m, &r, point_pos - exponent);
5920   REAL_VALUE_TO_INT (&m1, &m2, m);
5921   mantissa = m1;
5922   mant_hi = m2;
5923
5924   /* If there are bits set in the low part of the mantissa, we can't
5925      represent this value.  */
5926   if (mantissa != 0)
5927     return -1;
5928
5929   /* Now make it so that mantissa contains the most-significant bits, and move
5930      the point_pos to indicate that the least-significant bits have been
5931      discarded.  */
5932   point_pos -= HOST_BITS_PER_WIDE_INT;
5933   mantissa = mant_hi;
5934
5935   /* We can permit four significant bits of mantissa only, plus a high bit
5936      which is always 1.  */
5937   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
5938   if ((mantissa & mask) != 0)
5939     return -1;
5940
5941   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
5942   mantissa >>= point_pos - 5;
5943
5944   /* The mantissa may be zero. Disallow that case. (It's possible to load the
5945      floating-point immediate zero with Neon using an integer-zero load, but
5946      that case is handled elsewhere.)  */
5947   if (mantissa == 0)
5948     return -1;
5949
5950   gcc_assert (mantissa >= 16 && mantissa <= 31);
5951
5952   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
5953      normalized significands are in the range [1, 2). (Our mantissa is shifted
5954      left 4 places at this point relative to normalized IEEE754 values).  GCC
5955      internally uses [0.5, 1) (see real.c), so the exponent returned from
5956      REAL_EXP must be altered.  */
5957   exponent = 5 - exponent;
5958
5959   if (exponent < 0 || exponent > 7)
5960     return -1;
5961
5962   /* Sign, mantissa and exponent are now in the correct form to plug into the
5963      formula described in the comment above.  */
5964   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
5965 }
5966
5967 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
5968 int
5969 vfp3_const_double_rtx (rtx x)
5970 {
5971   if (!TARGET_VFP3)
5972     return 0;
5973
5974   return vfp3_const_double_index (x) != -1;
5975 }
5976
5977 /* Recognize immediates which can be used in various Neon instructions. Legal
5978    immediates are described by the following table (for VMVN variants, the
5979    bitwise inverse of the constant shown is recognized. In either case, VMOV
5980    is output and the correct instruction to use for a given constant is chosen
5981    by the assembler). The constant shown is replicated across all elements of
5982    the destination vector.
5983
5984    insn elems variant constant (binary)
5985    ---- ----- ------- -----------------
5986    vmov  i32     0    00000000 00000000 00000000 abcdefgh
5987    vmov  i32     1    00000000 00000000 abcdefgh 00000000
5988    vmov  i32     2    00000000 abcdefgh 00000000 00000000
5989    vmov  i32     3    abcdefgh 00000000 00000000 00000000
5990    vmov  i16     4    00000000 abcdefgh
5991    vmov  i16     5    abcdefgh 00000000
5992    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
5993    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
5994    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
5995    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
5996    vmvn  i16    10    00000000 abcdefgh
5997    vmvn  i16    11    abcdefgh 00000000
5998    vmov  i32    12    00000000 00000000 abcdefgh 11111111
5999    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
6000    vmov  i32    14    00000000 abcdefgh 11111111 11111111
6001    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
6002    vmov   i8    16    abcdefgh
6003    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
6004                       eeeeeeee ffffffff gggggggg hhhhhhhh
6005    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
6006
6007    For case 18, B = !b. Representable values are exactly those accepted by
6008    vfp3_const_double_index, but are output as floating-point numbers rather
6009    than indices.
6010
6011    Variants 0-5 (inclusive) may also be used as immediates for the second
6012    operand of VORR/VBIC instructions.
6013
6014    The INVERSE argument causes the bitwise inverse of the given operand to be
6015    recognized instead (used for recognizing legal immediates for the VAND/VORN
6016    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
6017    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
6018    output, rather than the real insns vbic/vorr).
6019
6020    INVERSE makes no difference to the recognition of float vectors.
6021
6022    The return value is the variant of immediate as shown in the above table, or
6023    -1 if the given value doesn't match any of the listed patterns.
6024 */
6025 static int
6026 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
6027                       rtx *modconst, int *elementwidth)
6028 {
6029 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
6030   matches = 1;                                  \
6031   for (i = 0; i < idx; i += (STRIDE))           \
6032     if (!(TEST))                                \
6033       matches = 0;                              \
6034   if (matches)                                  \
6035     {                                           \
6036       immtype = (CLASS);                        \
6037       elsize = (ELSIZE);                        \
6038       break;                                    \
6039     }
6040
6041   unsigned int i, elsize, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
6042   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
6043   unsigned char bytes[16];
6044   int immtype = -1, matches;
6045   unsigned int invmask = inverse ? 0xff : 0;
6046
6047   /* Vectors of float constants.  */
6048   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6049     {
6050       rtx el0 = CONST_VECTOR_ELT (op, 0);
6051       REAL_VALUE_TYPE r0;
6052
6053       if (!vfp3_const_double_rtx (el0))
6054         return -1;
6055
6056       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
6057
6058       for (i = 1; i < n_elts; i++)
6059         {
6060           rtx elt = CONST_VECTOR_ELT (op, i);
6061           REAL_VALUE_TYPE re;
6062
6063           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
6064
6065           if (!REAL_VALUES_EQUAL (r0, re))
6066             return -1;
6067         }
6068
6069       if (modconst)
6070         *modconst = CONST_VECTOR_ELT (op, 0);
6071
6072       if (elementwidth)
6073         *elementwidth = 0;
6074
6075       return 18;
6076     }
6077
6078   /* Splat vector constant out into a byte vector.  */
6079   for (i = 0; i < n_elts; i++)
6080     {
6081       rtx el = CONST_VECTOR_ELT (op, i);
6082       unsigned HOST_WIDE_INT elpart;
6083       unsigned int part, parts;
6084
6085       if (GET_CODE (el) == CONST_INT)
6086         {
6087           elpart = INTVAL (el);
6088           parts = 1;
6089         }
6090       else if (GET_CODE (el) == CONST_DOUBLE)
6091         {
6092           elpart = CONST_DOUBLE_LOW (el);
6093           parts = 2;
6094         }
6095       else
6096         gcc_unreachable ();
6097
6098       for (part = 0; part < parts; part++)
6099         {
6100           unsigned int byte;
6101           for (byte = 0; byte < innersize; byte++)
6102             {
6103               bytes[idx++] = (elpart & 0xff) ^ invmask;
6104               elpart >>= BITS_PER_UNIT;
6105             }
6106           if (GET_CODE (el) == CONST_DOUBLE)
6107             elpart = CONST_DOUBLE_HIGH (el);
6108         }
6109     }
6110
6111   /* Sanity check.  */
6112   gcc_assert (idx == GET_MODE_SIZE (mode));
6113
6114   do
6115     {
6116       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
6117                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6118
6119       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6120                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6121
6122       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
6123                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6124
6125       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
6126                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
6127
6128       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
6129
6130       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
6131
6132       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
6133                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6134
6135       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6136                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6137
6138       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
6139                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6140
6141       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
6142                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
6143
6144       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
6145
6146       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
6147
6148       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6149                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6150
6151       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6152                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6153
6154       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
6155                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6156
6157       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
6158                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6159
6160       CHECK (1, 8, 16, bytes[i] == bytes[0]);
6161
6162       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
6163                         && bytes[i] == bytes[(i + 8) % idx]);
6164     }
6165   while (0);
6166
6167   if (immtype == -1)
6168     return -1;
6169
6170   if (elementwidth)
6171     *elementwidth = elsize;
6172
6173   if (modconst)
6174     {
6175       unsigned HOST_WIDE_INT imm = 0;
6176
6177       /* Un-invert bytes of recognized vector, if necessary.  */
6178       if (invmask != 0)
6179         for (i = 0; i < idx; i++)
6180           bytes[i] ^= invmask;
6181
6182       if (immtype == 17)
6183         {
6184           /* FIXME: Broken on 32-bit H_W_I hosts.  */
6185           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
6186
6187           for (i = 0; i < 8; i++)
6188             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
6189                    << (i * BITS_PER_UNIT);
6190
6191           *modconst = GEN_INT (imm);
6192         }
6193       else
6194         {
6195           unsigned HOST_WIDE_INT imm = 0;
6196
6197           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
6198             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
6199
6200           *modconst = GEN_INT (imm);
6201         }
6202     }
6203
6204   return immtype;
6205 #undef CHECK
6206 }
6207
6208 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
6209    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
6210    float elements), and a modified constant (whatever should be output for a
6211    VMOV) in *MODCONST.  */
6212
6213 int
6214 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
6215                                rtx *modconst, int *elementwidth)
6216 {
6217   rtx tmpconst;
6218   int tmpwidth;
6219   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
6220
6221   if (retval == -1)
6222     return 0;
6223
6224   if (modconst)
6225     *modconst = tmpconst;
6226
6227   if (elementwidth)
6228     *elementwidth = tmpwidth;
6229
6230   return 1;
6231 }
6232
6233 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
6234    the immediate is valid, write a constant suitable for using as an operand
6235    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
6236    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
6237
6238 int
6239 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
6240                                 rtx *modconst, int *elementwidth)
6241 {
6242   rtx tmpconst;
6243   int tmpwidth;
6244   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
6245
6246   if (retval < 0 || retval > 5)
6247     return 0;
6248
6249   if (modconst)
6250     *modconst = tmpconst;
6251
6252   if (elementwidth)
6253     *elementwidth = tmpwidth;
6254
6255   return 1;
6256 }
6257
6258 /* Return a string suitable for output of Neon immediate logic operation
6259    MNEM.  */
6260
6261 char *
6262 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
6263                              int inverse, int quad)
6264 {
6265   int width, is_valid;
6266   static char templ[40];
6267
6268   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
6269
6270   gcc_assert (is_valid != 0);
6271
6272   if (quad)
6273     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
6274   else
6275     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
6276
6277   return templ;
6278 }
6279
6280 /* Output a sequence of pairwise operations to implement a reduction.
6281    NOTE: We do "too much work" here, because pairwise operations work on two
6282    registers-worth of operands in one go. Unfortunately we can't exploit those
6283    extra calculations to do the full operation in fewer steps, I don't think.
6284    Although all vector elements of the result but the first are ignored, we
6285    actually calculate the same result in each of the elements. An alternative
6286    such as initially loading a vector with zero to use as each of the second
6287    operands would use up an additional register and take an extra instruction,
6288    for no particular gain.  */
6289
6290 void
6291 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
6292                       rtx (*reduc) (rtx, rtx, rtx))
6293 {
6294   enum machine_mode inner = GET_MODE_INNER (mode);
6295   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
6296   rtx tmpsum = op1;
6297
6298   for (i = parts / 2; i >= 1; i /= 2)
6299     {
6300       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
6301       emit_insn (reduc (dest, tmpsum, tmpsum));
6302       tmpsum = dest;
6303     }
6304 }
6305
6306 /* Initialize a vector with non-constant elements.  FIXME: We can do better
6307    than the current implementation (building a vector on the stack and then
6308    loading it) in many cases.  See rs6000.c.  */
6309
6310 void
6311 neon_expand_vector_init (rtx target, rtx vals)
6312 {
6313   enum machine_mode mode = GET_MODE (target);
6314   enum machine_mode inner = GET_MODE_INNER (mode);
6315   unsigned int i, n_elts = GET_MODE_NUNITS (mode);
6316   rtx mem;
6317
6318   gcc_assert (VECTOR_MODE_P (mode));
6319
6320   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
6321   for (i = 0; i < n_elts; i++)
6322     emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
6323                    XVECEXP (vals, 0, i));
6324
6325   emit_move_insn (target, mem);
6326 }
6327
6328 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
6329    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
6330    reported source locations are bogus.  */
6331
6332 static void
6333 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
6334               const char *err)
6335 {
6336   HOST_WIDE_INT lane;
6337
6338   gcc_assert (GET_CODE (operand) == CONST_INT);
6339
6340   lane = INTVAL (operand);
6341
6342   if (lane < low || lane >= high)
6343     error (err);
6344 }
6345
6346 /* Bounds-check lanes.  */
6347
6348 void
6349 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6350 {
6351   bounds_check (operand, low, high, "lane out of range");
6352 }
6353
6354 /* Bounds-check constants.  */
6355
6356 void
6357 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6358 {
6359   bounds_check (operand, low, high, "constant out of range");
6360 }
6361
6362 HOST_WIDE_INT
6363 neon_element_bits (enum machine_mode mode)
6364 {
6365   if (mode == DImode)
6366     return GET_MODE_BITSIZE (mode);
6367   else
6368     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
6369 }
6370
6371 \f
6372 /* Predicates for `match_operand' and `match_operator'.  */
6373
6374 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
6375 int
6376 cirrus_memory_offset (rtx op)
6377 {
6378   /* Reject eliminable registers.  */
6379   if (! (reload_in_progress || reload_completed)
6380       && (   reg_mentioned_p (frame_pointer_rtx, op)
6381           || reg_mentioned_p (arg_pointer_rtx, op)
6382           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6383           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6384           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6385           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6386     return 0;
6387
6388   if (GET_CODE (op) == MEM)
6389     {
6390       rtx ind;
6391
6392       ind = XEXP (op, 0);
6393
6394       /* Match: (mem (reg)).  */
6395       if (GET_CODE (ind) == REG)
6396         return 1;
6397
6398       /* Match:
6399          (mem (plus (reg)
6400                     (const))).  */
6401       if (GET_CODE (ind) == PLUS
6402           && GET_CODE (XEXP (ind, 0)) == REG
6403           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6404           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
6405         return 1;
6406     }
6407
6408   return 0;
6409 }
6410
6411 /* Return TRUE if OP is a valid coprocessor memory address pattern.
6412    WB is true if full writeback address modes are allowed and is false
6413    if limited writeback address modes (POST_INC and PRE_DEC) are
6414    allowed.  */
6415
6416 int
6417 arm_coproc_mem_operand (rtx op, bool wb)
6418 {
6419   rtx ind;
6420
6421   /* Reject eliminable registers.  */
6422   if (! (reload_in_progress || reload_completed)
6423       && (   reg_mentioned_p (frame_pointer_rtx, op)
6424           || reg_mentioned_p (arg_pointer_rtx, op)
6425           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6426           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6427           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6428           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6429     return FALSE;
6430
6431   /* Constants are converted into offsets from labels.  */
6432   if (GET_CODE (op) != MEM)
6433     return FALSE;
6434
6435   ind = XEXP (op, 0);
6436
6437   if (reload_completed
6438       && (GET_CODE (ind) == LABEL_REF
6439           || (GET_CODE (ind) == CONST
6440               && GET_CODE (XEXP (ind, 0)) == PLUS
6441               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6442               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6443     return TRUE;
6444
6445   /* Match: (mem (reg)).  */
6446   if (GET_CODE (ind) == REG)
6447     return arm_address_register_rtx_p (ind, 0);
6448
6449   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
6450      acceptable in any case (subject to verification by
6451      arm_address_register_rtx_p).  We need WB to be true to accept
6452      PRE_INC and POST_DEC.  */
6453   if (GET_CODE (ind) == POST_INC
6454       || GET_CODE (ind) == PRE_DEC
6455       || (wb
6456           && (GET_CODE (ind) == PRE_INC
6457               || GET_CODE (ind) == POST_DEC)))
6458     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6459
6460   if (wb
6461       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
6462       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6463       && GET_CODE (XEXP (ind, 1)) == PLUS
6464       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6465     ind = XEXP (ind, 1);
6466
6467   /* Match:
6468      (plus (reg)
6469            (const)).  */
6470   if (GET_CODE (ind) == PLUS
6471       && GET_CODE (XEXP (ind, 0)) == REG
6472       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6473       && GET_CODE (XEXP (ind, 1)) == CONST_INT
6474       && INTVAL (XEXP (ind, 1)) > -1024
6475       && INTVAL (XEXP (ind, 1)) <  1024
6476       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6477     return TRUE;
6478
6479   return FALSE;
6480 }
6481
6482 /* Return TRUE if OP is a memory operand which we can load or store a vector
6483    to/from. If CORE is true, we're moving from ARM registers not Neon
6484    registers.  */
6485 int
6486 neon_vector_mem_operand (rtx op, bool core)
6487 {
6488   rtx ind;
6489
6490   /* Reject eliminable registers.  */
6491   if (! (reload_in_progress || reload_completed)
6492       && (   reg_mentioned_p (frame_pointer_rtx, op)
6493           || reg_mentioned_p (arg_pointer_rtx, op)
6494           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6495           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6496           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6497           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6498     return FALSE;
6499
6500   /* Constants are converted into offsets from labels.  */
6501   if (GET_CODE (op) != MEM)
6502     return FALSE;
6503
6504   ind = XEXP (op, 0);
6505
6506   if (reload_completed
6507       && (GET_CODE (ind) == LABEL_REF
6508           || (GET_CODE (ind) == CONST
6509               && GET_CODE (XEXP (ind, 0)) == PLUS
6510               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6511               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6512     return TRUE;
6513
6514   /* Match: (mem (reg)).  */
6515   if (GET_CODE (ind) == REG)
6516     return arm_address_register_rtx_p (ind, 0);
6517
6518   /* Allow post-increment with Neon registers.  */
6519   if (!core && GET_CODE (ind) == POST_INC)
6520     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6521
6522 #if 0
6523   /* FIXME: We can support this too if we use VLD1/VST1.  */
6524   if (!core
6525       && GET_CODE (ind) == POST_MODIFY
6526       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6527       && GET_CODE (XEXP (ind, 1)) == PLUS
6528       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6529     ind = XEXP (ind, 1);
6530 #endif
6531
6532   /* Match:
6533      (plus (reg)
6534           (const)).  */
6535   if (!core
6536       && GET_CODE (ind) == PLUS
6537       && GET_CODE (XEXP (ind, 0)) == REG
6538       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6539       && GET_CODE (XEXP (ind, 1)) == CONST_INT
6540       && INTVAL (XEXP (ind, 1)) > -1024
6541       && INTVAL (XEXP (ind, 1)) < 1016
6542       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6543     return TRUE;
6544
6545   return FALSE;
6546 }
6547
6548 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
6549    type.  */
6550 int
6551 neon_struct_mem_operand (rtx op)
6552 {
6553   rtx ind;
6554
6555   /* Reject eliminable registers.  */
6556   if (! (reload_in_progress || reload_completed)
6557       && (   reg_mentioned_p (frame_pointer_rtx, op)
6558           || reg_mentioned_p (arg_pointer_rtx, op)
6559           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6560           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6561           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6562           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6563     return FALSE;
6564
6565   /* Constants are converted into offsets from labels.  */
6566   if (GET_CODE (op) != MEM)
6567     return FALSE;
6568
6569   ind = XEXP (op, 0);
6570
6571   if (reload_completed
6572       && (GET_CODE (ind) == LABEL_REF
6573           || (GET_CODE (ind) == CONST
6574               && GET_CODE (XEXP (ind, 0)) == PLUS
6575               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6576               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6577     return TRUE;
6578
6579   /* Match: (mem (reg)).  */
6580   if (GET_CODE (ind) == REG)
6581     return arm_address_register_rtx_p (ind, 0);
6582
6583   return FALSE;
6584 }
6585
6586 /* Return true if X is a register that will be eliminated later on.  */
6587 int
6588 arm_eliminable_register (rtx x)
6589 {
6590   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
6591                        || REGNO (x) == ARG_POINTER_REGNUM
6592                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
6593                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
6594 }
6595
6596 /* Return GENERAL_REGS if a scratch register required to reload x to/from
6597    coprocessor registers.  Otherwise return NO_REGS.  */
6598
6599 enum reg_class
6600 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
6601 {
6602   if (TARGET_NEON
6603       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
6604           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6605       && neon_vector_mem_operand (x, FALSE))
6606      return NO_REGS;
6607
6608   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
6609     return NO_REGS;
6610
6611   return GENERAL_REGS;
6612 }
6613
6614 /* Values which must be returned in the most-significant end of the return
6615    register.  */
6616
6617 static bool
6618 arm_return_in_msb (const_tree valtype)
6619 {
6620   return (TARGET_AAPCS_BASED
6621           && BYTES_BIG_ENDIAN
6622           && (AGGREGATE_TYPE_P (valtype)
6623               || TREE_CODE (valtype) == COMPLEX_TYPE));
6624 }
6625
6626 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
6627    Use by the Cirrus Maverick code which has to workaround
6628    a hardware bug triggered by such instructions.  */
6629 static bool
6630 arm_memory_load_p (rtx insn)
6631 {
6632   rtx body, lhs, rhs;;
6633
6634   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
6635     return false;
6636
6637   body = PATTERN (insn);
6638
6639   if (GET_CODE (body) != SET)
6640     return false;
6641
6642   lhs = XEXP (body, 0);
6643   rhs = XEXP (body, 1);
6644
6645   lhs = REG_OR_SUBREG_RTX (lhs);
6646
6647   /* If the destination is not a general purpose
6648      register we do not have to worry.  */
6649   if (GET_CODE (lhs) != REG
6650       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
6651     return false;
6652
6653   /* As well as loads from memory we also have to react
6654      to loads of invalid constants which will be turned
6655      into loads from the minipool.  */
6656   return (GET_CODE (rhs) == MEM
6657           || GET_CODE (rhs) == SYMBOL_REF
6658           || note_invalid_constants (insn, -1, false));
6659 }
6660
6661 /* Return TRUE if INSN is a Cirrus instruction.  */
6662 static bool
6663 arm_cirrus_insn_p (rtx insn)
6664 {
6665   enum attr_cirrus attr;
6666
6667   /* get_attr cannot accept USE or CLOBBER.  */
6668   if (!insn
6669       || GET_CODE (insn) != INSN
6670       || GET_CODE (PATTERN (insn)) == USE
6671       || GET_CODE (PATTERN (insn)) == CLOBBER)
6672     return 0;
6673
6674   attr = get_attr_cirrus (insn);
6675
6676   return attr != CIRRUS_NOT;
6677 }
6678
6679 /* Cirrus reorg for invalid instruction combinations.  */
6680 static void
6681 cirrus_reorg (rtx first)
6682 {
6683   enum attr_cirrus attr;
6684   rtx body = PATTERN (first);
6685   rtx t;
6686   int nops;
6687
6688   /* Any branch must be followed by 2 non Cirrus instructions.  */
6689   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
6690     {
6691       nops = 0;
6692       t = next_nonnote_insn (first);
6693
6694       if (arm_cirrus_insn_p (t))
6695         ++ nops;
6696
6697       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
6698         ++ nops;
6699
6700       while (nops --)
6701         emit_insn_after (gen_nop (), first);
6702
6703       return;
6704     }
6705
6706   /* (float (blah)) is in parallel with a clobber.  */
6707   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
6708     body = XVECEXP (body, 0, 0);
6709
6710   if (GET_CODE (body) == SET)
6711     {
6712       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
6713
6714       /* cfldrd, cfldr64, cfstrd, cfstr64 must
6715          be followed by a non Cirrus insn.  */
6716       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
6717         {
6718           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
6719             emit_insn_after (gen_nop (), first);
6720
6721           return;
6722         }
6723       else if (arm_memory_load_p (first))
6724         {
6725           unsigned int arm_regno;
6726
6727           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
6728              ldr/cfmv64hr combination where the Rd field is the same
6729              in both instructions must be split with a non Cirrus
6730              insn.  Example:
6731
6732              ldr r0, blah
6733              nop
6734              cfmvsr mvf0, r0.  */
6735
6736           /* Get Arm register number for ldr insn.  */
6737           if (GET_CODE (lhs) == REG)
6738             arm_regno = REGNO (lhs);
6739           else
6740             {
6741               gcc_assert (GET_CODE (rhs) == REG);
6742               arm_regno = REGNO (rhs);
6743             }
6744
6745           /* Next insn.  */
6746           first = next_nonnote_insn (first);
6747
6748           if (! arm_cirrus_insn_p (first))
6749             return;
6750
6751           body = PATTERN (first);
6752
6753           /* (float (blah)) is in parallel with a clobber.  */
6754           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
6755             body = XVECEXP (body, 0, 0);
6756
6757           if (GET_CODE (body) == FLOAT)
6758             body = XEXP (body, 0);
6759
6760           if (get_attr_cirrus (first) == CIRRUS_MOVE
6761               && GET_CODE (XEXP (body, 1)) == REG
6762               && arm_regno == REGNO (XEXP (body, 1)))
6763             emit_insn_after (gen_nop (), first);
6764
6765           return;
6766         }
6767     }
6768
6769   /* get_attr cannot accept USE or CLOBBER.  */
6770   if (!first
6771       || GET_CODE (first) != INSN
6772       || GET_CODE (PATTERN (first)) == USE
6773       || GET_CODE (PATTERN (first)) == CLOBBER)
6774     return;
6775
6776   attr = get_attr_cirrus (first);
6777
6778   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
6779      must be followed by a non-coprocessor instruction.  */
6780   if (attr == CIRRUS_COMPARE)
6781     {
6782       nops = 0;
6783
6784       t = next_nonnote_insn (first);
6785
6786       if (arm_cirrus_insn_p (t))
6787         ++ nops;
6788
6789       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
6790         ++ nops;
6791
6792       while (nops --)
6793         emit_insn_after (gen_nop (), first);
6794
6795       return;
6796     }
6797 }
6798
6799 /* Return TRUE if X references a SYMBOL_REF.  */
6800 int
6801 symbol_mentioned_p (rtx x)
6802 {
6803   const char * fmt;
6804   int i;
6805
6806   if (GET_CODE (x) == SYMBOL_REF)
6807     return 1;
6808
6809   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
6810      are constant offsets, not symbols.  */
6811   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6812     return 0;
6813
6814   fmt = GET_RTX_FORMAT (GET_CODE (x));
6815
6816   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6817     {
6818       if (fmt[i] == 'E')
6819         {
6820           int j;
6821
6822           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6823             if (symbol_mentioned_p (XVECEXP (x, i, j)))
6824               return 1;
6825         }
6826       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
6827         return 1;
6828     }
6829
6830   return 0;
6831 }
6832
6833 /* Return TRUE if X references a LABEL_REF.  */
6834 int
6835 label_mentioned_p (rtx x)
6836 {
6837   const char * fmt;
6838   int i;
6839
6840   if (GET_CODE (x) == LABEL_REF)
6841     return 1;
6842
6843   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
6844      instruction, but they are constant offsets, not symbols.  */
6845   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6846     return 0;
6847
6848   fmt = GET_RTX_FORMAT (GET_CODE (x));
6849   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6850     {
6851       if (fmt[i] == 'E')
6852         {
6853           int j;
6854
6855           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6856             if (label_mentioned_p (XVECEXP (x, i, j)))
6857               return 1;
6858         }
6859       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
6860         return 1;
6861     }
6862
6863   return 0;
6864 }
6865
6866 int
6867 tls_mentioned_p (rtx x)
6868 {
6869   switch (GET_CODE (x))
6870     {
6871     case CONST:
6872       return tls_mentioned_p (XEXP (x, 0));
6873
6874     case UNSPEC:
6875       if (XINT (x, 1) == UNSPEC_TLS)
6876         return 1;
6877
6878     default:
6879       return 0;
6880     }
6881 }
6882
6883 /* Must not copy a SET whose source operand is PC-relative.  */
6884
6885 static bool
6886 arm_cannot_copy_insn_p (rtx insn)
6887 {
6888   rtx pat = PATTERN (insn);
6889
6890   if (GET_CODE (pat) == SET)
6891     {
6892       rtx rhs = SET_SRC (pat);
6893
6894       if (GET_CODE (rhs) == UNSPEC
6895           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
6896         return TRUE;
6897
6898       if (GET_CODE (rhs) == MEM
6899           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
6900           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
6901         return TRUE;
6902     }
6903
6904   return FALSE;
6905 }
6906
6907 enum rtx_code
6908 minmax_code (rtx x)
6909 {
6910   enum rtx_code code = GET_CODE (x);
6911
6912   switch (code)
6913     {
6914     case SMAX:
6915       return GE;
6916     case SMIN:
6917       return LE;
6918     case UMIN:
6919       return LEU;
6920     case UMAX:
6921       return GEU;
6922     default:
6923       gcc_unreachable ();
6924     }
6925 }
6926
6927 /* Return 1 if memory locations are adjacent.  */
6928 int
6929 adjacent_mem_locations (rtx a, rtx b)
6930 {
6931   /* We don't guarantee to preserve the order of these memory refs.  */
6932   if (volatile_refs_p (a) || volatile_refs_p (b))
6933     return 0;
6934
6935   if ((GET_CODE (XEXP (a, 0)) == REG
6936        || (GET_CODE (XEXP (a, 0)) == PLUS
6937            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
6938       && (GET_CODE (XEXP (b, 0)) == REG
6939           || (GET_CODE (XEXP (b, 0)) == PLUS
6940               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
6941     {
6942       HOST_WIDE_INT val0 = 0, val1 = 0;
6943       rtx reg0, reg1;
6944       int val_diff;
6945
6946       if (GET_CODE (XEXP (a, 0)) == PLUS)
6947         {
6948           reg0 = XEXP (XEXP (a, 0), 0);
6949           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
6950         }
6951       else
6952         reg0 = XEXP (a, 0);
6953
6954       if (GET_CODE (XEXP (b, 0)) == PLUS)
6955         {
6956           reg1 = XEXP (XEXP (b, 0), 0);
6957           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
6958         }
6959       else
6960         reg1 = XEXP (b, 0);
6961
6962       /* Don't accept any offset that will require multiple
6963          instructions to handle, since this would cause the
6964          arith_adjacentmem pattern to output an overlong sequence.  */
6965       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
6966         return 0;
6967
6968       /* Don't allow an eliminable register: register elimination can make
6969          the offset too large.  */
6970       if (arm_eliminable_register (reg0))
6971         return 0;
6972
6973       val_diff = val1 - val0;
6974
6975       if (arm_ld_sched)
6976         {
6977           /* If the target has load delay slots, then there's no benefit
6978              to using an ldm instruction unless the offset is zero and
6979              we are optimizing for size.  */
6980           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
6981                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
6982                   && (val_diff == 4 || val_diff == -4));
6983         }
6984
6985       return ((REGNO (reg0) == REGNO (reg1))
6986               && (val_diff == 4 || val_diff == -4));
6987     }
6988
6989   return 0;
6990 }
6991
6992 int
6993 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6994                         HOST_WIDE_INT *load_offset)
6995 {
6996   int unsorted_regs[4];
6997   HOST_WIDE_INT unsorted_offsets[4];
6998   int order[4];
6999   int base_reg = -1;
7000   int i;
7001
7002   /* Can only handle 2, 3, or 4 insns at present,
7003      though could be easily extended if required.  */
7004   gcc_assert (nops >= 2 && nops <= 4);
7005
7006   memset (order, 0, 4 * sizeof (int));
7007
7008   /* Loop over the operands and check that the memory references are
7009      suitable (i.e. immediate offsets from the same base register).  At
7010      the same time, extract the target register, and the memory
7011      offsets.  */
7012   for (i = 0; i < nops; i++)
7013     {
7014       rtx reg;
7015       rtx offset;
7016
7017       /* Convert a subreg of a mem into the mem itself.  */
7018       if (GET_CODE (operands[nops + i]) == SUBREG)
7019         operands[nops + i] = alter_subreg (operands + (nops + i));
7020
7021       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7022
7023       /* Don't reorder volatile memory references; it doesn't seem worth
7024          looking for the case where the order is ok anyway.  */
7025       if (MEM_VOLATILE_P (operands[nops + i]))
7026         return 0;
7027
7028       offset = const0_rtx;
7029
7030       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7031            || (GET_CODE (reg) == SUBREG
7032                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7033           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7034               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7035                    == REG)
7036                   || (GET_CODE (reg) == SUBREG
7037                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7038               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7039                   == CONST_INT)))
7040         {
7041           if (i == 0)
7042             {
7043               base_reg = REGNO (reg);
7044               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7045                                   ? REGNO (operands[i])
7046                                   : REGNO (SUBREG_REG (operands[i])));
7047               order[0] = 0;
7048             }
7049           else
7050             {
7051               if (base_reg != (int) REGNO (reg))
7052                 /* Not addressed from the same base register.  */
7053                 return 0;
7054
7055               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7056                                   ? REGNO (operands[i])
7057                                   : REGNO (SUBREG_REG (operands[i])));
7058               if (unsorted_regs[i] < unsorted_regs[order[0]])
7059                 order[0] = i;
7060             }
7061
7062           /* If it isn't an integer register, or if it overwrites the
7063              base register but isn't the last insn in the list, then
7064              we can't do this.  */
7065           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
7066               || (i != nops - 1 && unsorted_regs[i] == base_reg))
7067             return 0;
7068
7069           unsorted_offsets[i] = INTVAL (offset);
7070         }
7071       else
7072         /* Not a suitable memory address.  */
7073         return 0;
7074     }
7075
7076   /* All the useful information has now been extracted from the
7077      operands into unsorted_regs and unsorted_offsets; additionally,
7078      order[0] has been set to the lowest numbered register in the
7079      list.  Sort the registers into order, and check that the memory
7080      offsets are ascending and adjacent.  */
7081
7082   for (i = 1; i < nops; i++)
7083     {
7084       int j;
7085
7086       order[i] = order[i - 1];
7087       for (j = 0; j < nops; j++)
7088         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7089             && (order[i] == order[i - 1]
7090                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7091           order[i] = j;
7092
7093       /* Have we found a suitable register? if not, one must be used more
7094          than once.  */
7095       if (order[i] == order[i - 1])
7096         return 0;
7097
7098       /* Is the memory address adjacent and ascending? */
7099       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7100         return 0;
7101     }
7102
7103   if (base)
7104     {
7105       *base = base_reg;
7106
7107       for (i = 0; i < nops; i++)
7108         regs[i] = unsorted_regs[order[i]];
7109
7110       *load_offset = unsorted_offsets[order[0]];
7111     }
7112
7113   if (unsorted_offsets[order[0]] == 0)
7114     return 1; /* ldmia */
7115
7116   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
7117     return 2; /* ldmib */
7118
7119   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
7120     return 3; /* ldmda */
7121
7122   if (unsorted_offsets[order[nops - 1]] == -4)
7123     return 4; /* ldmdb */
7124
7125   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
7126      if the offset isn't small enough.  The reason 2 ldrs are faster
7127      is because these ARMs are able to do more than one cache access
7128      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
7129      whilst the ARM8 has a double bandwidth cache.  This means that
7130      these cores can do both an instruction fetch and a data fetch in
7131      a single cycle, so the trick of calculating the address into a
7132      scratch register (one of the result regs) and then doing a load
7133      multiple actually becomes slower (and no smaller in code size).
7134      That is the transformation
7135
7136         ldr     rd1, [rbase + offset]
7137         ldr     rd2, [rbase + offset + 4]
7138
7139      to
7140
7141         add     rd1, rbase, offset
7142         ldmia   rd1, {rd1, rd2}
7143
7144      produces worse code -- '3 cycles + any stalls on rd2' instead of
7145      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
7146      access per cycle, the first sequence could never complete in less
7147      than 6 cycles, whereas the ldm sequence would only take 5 and
7148      would make better use of sequential accesses if not hitting the
7149      cache.
7150
7151      We cheat here and test 'arm_ld_sched' which we currently know to
7152      only be true for the ARM8, ARM9 and StrongARM.  If this ever
7153      changes, then the test below needs to be reworked.  */
7154   if (nops == 2 && arm_ld_sched)
7155     return 0;
7156
7157   /* Can't do it without setting up the offset, only do this if it takes
7158      no more than one insn.  */
7159   return (const_ok_for_arm (unsorted_offsets[order[0]])
7160           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
7161 }
7162
7163 const char *
7164 emit_ldm_seq (rtx *operands, int nops)
7165 {
7166   int regs[4];
7167   int base_reg;
7168   HOST_WIDE_INT offset;
7169   char buf[100];
7170   int i;
7171
7172   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7173     {
7174     case 1:
7175       strcpy (buf, "ldm%(ia%)\t");
7176       break;
7177
7178     case 2:
7179       strcpy (buf, "ldm%(ib%)\t");
7180       break;
7181
7182     case 3:
7183       strcpy (buf, "ldm%(da%)\t");
7184       break;
7185
7186     case 4:
7187       strcpy (buf, "ldm%(db%)\t");
7188       break;
7189
7190     case 5:
7191       if (offset >= 0)
7192         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7193                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7194                  (long) offset);
7195       else
7196         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7197                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7198                  (long) -offset);
7199       output_asm_insn (buf, operands);
7200       base_reg = regs[0];
7201       strcpy (buf, "ldm%(ia%)\t");
7202       break;
7203
7204     default:
7205       gcc_unreachable ();
7206     }
7207
7208   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7209            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7210
7211   for (i = 1; i < nops; i++)
7212     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7213              reg_names[regs[i]]);
7214
7215   strcat (buf, "}\t%@ phole ldm");
7216
7217   output_asm_insn (buf, operands);
7218   return "";
7219 }
7220
7221 int
7222 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7223                          HOST_WIDE_INT * load_offset)
7224 {
7225   int unsorted_regs[4];
7226   HOST_WIDE_INT unsorted_offsets[4];
7227   int order[4];
7228   int base_reg = -1;
7229   int i;
7230
7231   /* Can only handle 2, 3, or 4 insns at present, though could be easily
7232      extended if required.  */
7233   gcc_assert (nops >= 2 && nops <= 4);
7234
7235   memset (order, 0, 4 * sizeof (int));
7236
7237   /* Loop over the operands and check that the memory references are
7238      suitable (i.e. immediate offsets from the same base register).  At
7239      the same time, extract the target register, and the memory
7240      offsets.  */
7241   for (i = 0; i < nops; i++)
7242     {
7243       rtx reg;
7244       rtx offset;
7245
7246       /* Convert a subreg of a mem into the mem itself.  */
7247       if (GET_CODE (operands[nops + i]) == SUBREG)
7248         operands[nops + i] = alter_subreg (operands + (nops + i));
7249
7250       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7251
7252       /* Don't reorder volatile memory references; it doesn't seem worth
7253          looking for the case where the order is ok anyway.  */
7254       if (MEM_VOLATILE_P (operands[nops + i]))
7255         return 0;
7256
7257       offset = const0_rtx;
7258
7259       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7260            || (GET_CODE (reg) == SUBREG
7261                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7262           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7263               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7264                    == REG)
7265                   || (GET_CODE (reg) == SUBREG
7266                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7267               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7268                   == CONST_INT)))
7269         {
7270           if (i == 0)
7271             {
7272               base_reg = REGNO (reg);
7273               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7274                                   ? REGNO (operands[i])
7275                                   : REGNO (SUBREG_REG (operands[i])));
7276               order[0] = 0;
7277             }
7278           else
7279             {
7280               if (base_reg != (int) REGNO (reg))
7281                 /* Not addressed from the same base register.  */
7282                 return 0;
7283
7284               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7285                                   ? REGNO (operands[i])
7286                                   : REGNO (SUBREG_REG (operands[i])));
7287               if (unsorted_regs[i] < unsorted_regs[order[0]])
7288                 order[0] = i;
7289             }
7290
7291           /* If it isn't an integer register, then we can't do this.  */
7292           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
7293             return 0;
7294
7295           unsorted_offsets[i] = INTVAL (offset);
7296         }
7297       else
7298         /* Not a suitable memory address.  */
7299         return 0;
7300     }
7301
7302   /* All the useful information has now been extracted from the
7303      operands into unsorted_regs and unsorted_offsets; additionally,
7304      order[0] has been set to the lowest numbered register in the
7305      list.  Sort the registers into order, and check that the memory
7306      offsets are ascending and adjacent.  */
7307
7308   for (i = 1; i < nops; i++)
7309     {
7310       int j;
7311
7312       order[i] = order[i - 1];
7313       for (j = 0; j < nops; j++)
7314         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7315             && (order[i] == order[i - 1]
7316                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7317           order[i] = j;
7318
7319       /* Have we found a suitable register? if not, one must be used more
7320          than once.  */
7321       if (order[i] == order[i - 1])
7322         return 0;
7323
7324       /* Is the memory address adjacent and ascending? */
7325       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7326         return 0;
7327     }
7328
7329   if (base)
7330     {
7331       *base = base_reg;
7332
7333       for (i = 0; i < nops; i++)
7334         regs[i] = unsorted_regs[order[i]];
7335
7336       *load_offset = unsorted_offsets[order[0]];
7337     }
7338
7339   if (unsorted_offsets[order[0]] == 0)
7340     return 1; /* stmia */
7341
7342   if (unsorted_offsets[order[0]] == 4)
7343     return 2; /* stmib */
7344
7345   if (unsorted_offsets[order[nops - 1]] == 0)
7346     return 3; /* stmda */
7347
7348   if (unsorted_offsets[order[nops - 1]] == -4)
7349     return 4; /* stmdb */
7350
7351   return 0;
7352 }
7353
7354 const char *
7355 emit_stm_seq (rtx *operands, int nops)
7356 {
7357   int regs[4];
7358   int base_reg;
7359   HOST_WIDE_INT offset;
7360   char buf[100];
7361   int i;
7362
7363   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7364     {
7365     case 1:
7366       strcpy (buf, "stm%(ia%)\t");
7367       break;
7368
7369     case 2:
7370       strcpy (buf, "stm%(ib%)\t");
7371       break;
7372
7373     case 3:
7374       strcpy (buf, "stm%(da%)\t");
7375       break;
7376
7377     case 4:
7378       strcpy (buf, "stm%(db%)\t");
7379       break;
7380
7381     default:
7382       gcc_unreachable ();
7383     }
7384
7385   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7386            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7387
7388   for (i = 1; i < nops; i++)
7389     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7390              reg_names[regs[i]]);
7391
7392   strcat (buf, "}\t%@ phole stm");
7393
7394   output_asm_insn (buf, operands);
7395   return "";
7396 }
7397 \f
7398 /* Routines for use in generating RTL.  */
7399
7400 rtx
7401 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
7402                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
7403 {
7404   HOST_WIDE_INT offset = *offsetp;
7405   int i = 0, j;
7406   rtx result;
7407   int sign = up ? 1 : -1;
7408   rtx mem, addr;
7409
7410   /* XScale has load-store double instructions, but they have stricter
7411      alignment requirements than load-store multiple, so we cannot
7412      use them.
7413
7414      For XScale ldm requires 2 + NREGS cycles to complete and blocks
7415      the pipeline until completion.
7416
7417         NREGS           CYCLES
7418           1               3
7419           2               4
7420           3               5
7421           4               6
7422
7423      An ldr instruction takes 1-3 cycles, but does not block the
7424      pipeline.
7425
7426         NREGS           CYCLES
7427           1              1-3
7428           2              2-6
7429           3              3-9
7430           4              4-12
7431
7432      Best case ldr will always win.  However, the more ldr instructions
7433      we issue, the less likely we are to be able to schedule them well.
7434      Using ldr instructions also increases code size.
7435
7436      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
7437      for counts of 3 or 4 regs.  */
7438   if (arm_tune_xscale && count <= 2 && ! optimize_size)
7439     {
7440       rtx seq;
7441
7442       start_sequence ();
7443
7444       for (i = 0; i < count; i++)
7445         {
7446           addr = plus_constant (from, i * 4 * sign);
7447           mem = adjust_automodify_address (basemem, SImode, addr, offset);
7448           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
7449           offset += 4 * sign;
7450         }
7451
7452       if (write_back)
7453         {
7454           emit_move_insn (from, plus_constant (from, count * 4 * sign));
7455           *offsetp = offset;
7456         }
7457
7458       seq = get_insns ();
7459       end_sequence ();
7460
7461       return seq;
7462     }
7463
7464   result = gen_rtx_PARALLEL (VOIDmode,
7465                              rtvec_alloc (count + (write_back ? 1 : 0)));
7466   if (write_back)
7467     {
7468       XVECEXP (result, 0, 0)
7469         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
7470       i = 1;
7471       count++;
7472     }
7473
7474   for (j = 0; i < count; i++, j++)
7475     {
7476       addr = plus_constant (from, j * 4 * sign);
7477       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
7478       XVECEXP (result, 0, i)
7479         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
7480       offset += 4 * sign;
7481     }
7482
7483   if (write_back)
7484     *offsetp = offset;
7485
7486   return result;
7487 }
7488
7489 rtx
7490 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
7491                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
7492 {
7493   HOST_WIDE_INT offset = *offsetp;
7494   int i = 0, j;
7495   rtx result;
7496   int sign = up ? 1 : -1;
7497   rtx mem, addr;
7498
7499   /* See arm_gen_load_multiple for discussion of
7500      the pros/cons of ldm/stm usage for XScale.  */
7501   if (arm_tune_xscale && count <= 2 && ! optimize_size)
7502     {
7503       rtx seq;
7504
7505       start_sequence ();
7506
7507       for (i = 0; i < count; i++)
7508         {
7509           addr = plus_constant (to, i * 4 * sign);
7510           mem = adjust_automodify_address (basemem, SImode, addr, offset);
7511           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
7512           offset += 4 * sign;
7513         }
7514
7515       if (write_back)
7516         {
7517           emit_move_insn (to, plus_constant (to, count * 4 * sign));
7518           *offsetp = offset;
7519         }
7520
7521       seq = get_insns ();
7522       end_sequence ();
7523
7524       return seq;
7525     }
7526
7527   result = gen_rtx_PARALLEL (VOIDmode,
7528                              rtvec_alloc (count + (write_back ? 1 : 0)));
7529   if (write_back)
7530     {
7531       XVECEXP (result, 0, 0)
7532         = gen_rtx_SET (VOIDmode, to,
7533                        plus_constant (to, count * 4 * sign));
7534       i = 1;
7535       count++;
7536     }
7537
7538   for (j = 0; i < count; i++, j++)
7539     {
7540       addr = plus_constant (to, j * 4 * sign);
7541       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
7542       XVECEXP (result, 0, i)
7543         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
7544       offset += 4 * sign;
7545     }
7546
7547   if (write_back)
7548     *offsetp = offset;
7549
7550   return result;
7551 }
7552
7553 int
7554 arm_gen_movmemqi (rtx *operands)
7555 {
7556   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
7557   HOST_WIDE_INT srcoffset, dstoffset;
7558   int i;
7559   rtx src, dst, srcbase, dstbase;
7560   rtx part_bytes_reg = NULL;
7561   rtx mem;
7562
7563   if (GET_CODE (operands[2]) != CONST_INT
7564       || GET_CODE (operands[3]) != CONST_INT
7565       || INTVAL (operands[2]) > 64
7566       || INTVAL (operands[3]) & 3)
7567     return 0;
7568
7569   dstbase = operands[0];
7570   srcbase = operands[1];
7571
7572   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
7573   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
7574
7575   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
7576   out_words_to_go = INTVAL (operands[2]) / 4;
7577   last_bytes = INTVAL (operands[2]) & 3;
7578   dstoffset = srcoffset = 0;
7579
7580   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
7581     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
7582
7583   for (i = 0; in_words_to_go >= 2; i+=4)
7584     {
7585       if (in_words_to_go > 4)
7586         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
7587                                           srcbase, &srcoffset));
7588       else
7589         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
7590                                           FALSE, srcbase, &srcoffset));
7591
7592       if (out_words_to_go)
7593         {
7594           if (out_words_to_go > 4)
7595             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
7596                                                dstbase, &dstoffset));
7597           else if (out_words_to_go != 1)
7598             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
7599                                                dst, TRUE,
7600                                                (last_bytes == 0
7601                                                 ? FALSE : TRUE),
7602                                                dstbase, &dstoffset));
7603           else
7604             {
7605               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
7606               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
7607               if (last_bytes != 0)
7608                 {
7609                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
7610                   dstoffset += 4;
7611                 }
7612             }
7613         }
7614
7615       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
7616       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
7617     }
7618
7619   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
7620   if (out_words_to_go)
7621     {
7622       rtx sreg;
7623
7624       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7625       sreg = copy_to_reg (mem);
7626
7627       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
7628       emit_move_insn (mem, sreg);
7629       in_words_to_go--;
7630
7631       gcc_assert (!in_words_to_go);     /* Sanity check */
7632     }
7633
7634   if (in_words_to_go)
7635     {
7636       gcc_assert (in_words_to_go > 0);
7637
7638       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7639       part_bytes_reg = copy_to_mode_reg (SImode, mem);
7640     }
7641
7642   gcc_assert (!last_bytes || part_bytes_reg);
7643
7644   if (BYTES_BIG_ENDIAN && last_bytes)
7645     {
7646       rtx tmp = gen_reg_rtx (SImode);
7647
7648       /* The bytes we want are in the top end of the word.  */
7649       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
7650                               GEN_INT (8 * (4 - last_bytes))));
7651       part_bytes_reg = tmp;
7652
7653       while (last_bytes)
7654         {
7655           mem = adjust_automodify_address (dstbase, QImode,
7656                                            plus_constant (dst, last_bytes - 1),
7657                                            dstoffset + last_bytes - 1);
7658           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7659
7660           if (--last_bytes)
7661             {
7662               tmp = gen_reg_rtx (SImode);
7663               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
7664               part_bytes_reg = tmp;
7665             }
7666         }
7667
7668     }
7669   else
7670     {
7671       if (last_bytes > 1)
7672         {
7673           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
7674           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
7675           last_bytes -= 2;
7676           if (last_bytes)
7677             {
7678               rtx tmp = gen_reg_rtx (SImode);
7679               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
7680               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
7681               part_bytes_reg = tmp;
7682               dstoffset += 2;
7683             }
7684         }
7685
7686       if (last_bytes)
7687         {
7688           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
7689           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7690         }
7691     }
7692
7693   return 1;
7694 }
7695
7696 /* Select a dominance comparison mode if possible for a test of the general
7697    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
7698    COND_OR == DOM_CC_X_AND_Y => (X && Y)
7699    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
7700    COND_OR == DOM_CC_X_OR_Y => (X || Y)
7701    In all cases OP will be either EQ or NE, but we don't need to know which
7702    here.  If we are unable to support a dominance comparison we return
7703    CC mode.  This will then fail to match for the RTL expressions that
7704    generate this call.  */
7705 enum machine_mode
7706 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
7707 {
7708   enum rtx_code cond1, cond2;
7709   int swapped = 0;
7710
7711   /* Currently we will probably get the wrong result if the individual
7712      comparisons are not simple.  This also ensures that it is safe to
7713      reverse a comparison if necessary.  */
7714   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
7715        != CCmode)
7716       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
7717           != CCmode))
7718     return CCmode;
7719
7720   /* The if_then_else variant of this tests the second condition if the
7721      first passes, but is true if the first fails.  Reverse the first
7722      condition to get a true "inclusive-or" expression.  */
7723   if (cond_or == DOM_CC_NX_OR_Y)
7724     cond1 = reverse_condition (cond1);
7725
7726   /* If the comparisons are not equal, and one doesn't dominate the other,
7727      then we can't do this.  */
7728   if (cond1 != cond2
7729       && !comparison_dominates_p (cond1, cond2)
7730       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
7731     return CCmode;
7732
7733   if (swapped)
7734     {
7735       enum rtx_code temp = cond1;
7736       cond1 = cond2;
7737       cond2 = temp;
7738     }
7739
7740   switch (cond1)
7741     {
7742     case EQ:
7743       if (cond_or == DOM_CC_X_AND_Y)
7744         return CC_DEQmode;
7745
7746       switch (cond2)
7747         {
7748         case EQ: return CC_DEQmode;
7749         case LE: return CC_DLEmode;
7750         case LEU: return CC_DLEUmode;
7751         case GE: return CC_DGEmode;
7752         case GEU: return CC_DGEUmode;
7753         default: gcc_unreachable ();
7754         }
7755
7756     case LT:
7757       if (cond_or == DOM_CC_X_AND_Y)
7758         return CC_DLTmode;
7759
7760       switch (cond2)
7761         {
7762         case  LT:
7763             return CC_DLTmode;
7764         case LE:
7765           return CC_DLEmode;
7766         case NE:
7767           return CC_DNEmode;
7768         default:
7769           gcc_unreachable ();
7770         }
7771
7772     case GT:
7773       if (cond_or == DOM_CC_X_AND_Y)
7774         return CC_DGTmode;
7775
7776       switch (cond2)
7777         {
7778         case GT:
7779           return CC_DGTmode;
7780         case GE:
7781           return CC_DGEmode;
7782         case NE:
7783           return CC_DNEmode;
7784         default:
7785           gcc_unreachable ();
7786         }
7787
7788     case LTU:
7789       if (cond_or == DOM_CC_X_AND_Y)
7790         return CC_DLTUmode;
7791
7792       switch (cond2)
7793         {
7794         case LTU:
7795           return CC_DLTUmode;
7796         case LEU:
7797           return CC_DLEUmode;
7798         case NE:
7799           return CC_DNEmode;
7800         default:
7801           gcc_unreachable ();
7802         }
7803
7804     case GTU:
7805       if (cond_or == DOM_CC_X_AND_Y)
7806         return CC_DGTUmode;
7807
7808       switch (cond2)
7809         {
7810         case GTU:
7811           return CC_DGTUmode;
7812         case GEU:
7813           return CC_DGEUmode;
7814         case NE:
7815           return CC_DNEmode;
7816         default:
7817           gcc_unreachable ();
7818         }
7819
7820     /* The remaining cases only occur when both comparisons are the
7821        same.  */
7822     case NE:
7823       gcc_assert (cond1 == cond2);
7824       return CC_DNEmode;
7825
7826     case LE:
7827       gcc_assert (cond1 == cond2);
7828       return CC_DLEmode;
7829
7830     case GE:
7831       gcc_assert (cond1 == cond2);
7832       return CC_DGEmode;
7833
7834     case LEU:
7835       gcc_assert (cond1 == cond2);
7836       return CC_DLEUmode;
7837
7838     case GEU:
7839       gcc_assert (cond1 == cond2);
7840       return CC_DGEUmode;
7841
7842     default:
7843       gcc_unreachable ();
7844     }
7845 }
7846
7847 enum machine_mode
7848 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
7849 {
7850   /* All floating point compares return CCFP if it is an equality
7851      comparison, and CCFPE otherwise.  */
7852   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
7853     {
7854       switch (op)
7855         {
7856         case EQ:
7857         case NE:
7858         case UNORDERED:
7859         case ORDERED:
7860         case UNLT:
7861         case UNLE:
7862         case UNGT:
7863         case UNGE:
7864         case UNEQ:
7865         case LTGT:
7866           return CCFPmode;
7867
7868         case LT:
7869         case LE:
7870         case GT:
7871         case GE:
7872           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
7873             return CCFPmode;
7874           return CCFPEmode;
7875
7876         default:
7877           gcc_unreachable ();
7878         }
7879     }
7880
7881   /* A compare with a shifted operand.  Because of canonicalization, the
7882      comparison will have to be swapped when we emit the assembler.  */
7883   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
7884       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7885           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
7886           || GET_CODE (x) == ROTATERT))
7887     return CC_SWPmode;
7888
7889   /* This operation is performed swapped, but since we only rely on the Z
7890      flag we don't need an additional mode.  */
7891   if (GET_MODE (y) == SImode && REG_P (y)
7892       && GET_CODE (x) == NEG
7893       && (op == EQ || op == NE))
7894     return CC_Zmode;
7895
7896   /* This is a special case that is used by combine to allow a
7897      comparison of a shifted byte load to be split into a zero-extend
7898      followed by a comparison of the shifted integer (only valid for
7899      equalities and unsigned inequalities).  */
7900   if (GET_MODE (x) == SImode
7901       && GET_CODE (x) == ASHIFT
7902       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
7903       && GET_CODE (XEXP (x, 0)) == SUBREG
7904       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
7905       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
7906       && (op == EQ || op == NE
7907           || op == GEU || op == GTU || op == LTU || op == LEU)
7908       && GET_CODE (y) == CONST_INT)
7909     return CC_Zmode;
7910
7911   /* A construct for a conditional compare, if the false arm contains
7912      0, then both conditions must be true, otherwise either condition
7913      must be true.  Not all conditions are possible, so CCmode is
7914      returned if it can't be done.  */
7915   if (GET_CODE (x) == IF_THEN_ELSE
7916       && (XEXP (x, 2) == const0_rtx
7917           || XEXP (x, 2) == const1_rtx)
7918       && COMPARISON_P (XEXP (x, 0))
7919       && COMPARISON_P (XEXP (x, 1)))
7920     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7921                                          INTVAL (XEXP (x, 2)));
7922
7923   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
7924   if (GET_CODE (x) == AND
7925       && COMPARISON_P (XEXP (x, 0))
7926       && COMPARISON_P (XEXP (x, 1)))
7927     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7928                                          DOM_CC_X_AND_Y);
7929
7930   if (GET_CODE (x) == IOR
7931       && COMPARISON_P (XEXP (x, 0))
7932       && COMPARISON_P (XEXP (x, 1)))
7933     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7934                                          DOM_CC_X_OR_Y);
7935
7936   /* An operation (on Thumb) where we want to test for a single bit.
7937      This is done by shifting that bit up into the top bit of a
7938      scratch register; we can then branch on the sign bit.  */
7939   if (TARGET_THUMB1
7940       && GET_MODE (x) == SImode
7941       && (op == EQ || op == NE)
7942       && GET_CODE (x) == ZERO_EXTRACT
7943       && XEXP (x, 1) == const1_rtx)
7944     return CC_Nmode;
7945
7946   /* An operation that sets the condition codes as a side-effect, the
7947      V flag is not set correctly, so we can only use comparisons where
7948      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
7949      instead.)  */
7950   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
7951   if (GET_MODE (x) == SImode
7952       && y == const0_rtx
7953       && (op == EQ || op == NE || op == LT || op == GE)
7954       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
7955           || GET_CODE (x) == AND || GET_CODE (x) == IOR
7956           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
7957           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
7958           || GET_CODE (x) == LSHIFTRT
7959           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7960           || GET_CODE (x) == ROTATERT
7961           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
7962     return CC_NOOVmode;
7963
7964   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
7965     return CC_Zmode;
7966
7967   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
7968       && GET_CODE (x) == PLUS
7969       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
7970     return CC_Cmode;
7971
7972   return CCmode;
7973 }
7974
7975 /* X and Y are two things to compare using CODE.  Emit the compare insn and
7976    return the rtx for register 0 in the proper mode.  FP means this is a
7977    floating point compare: I don't think that it is needed on the arm.  */
7978 rtx
7979 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
7980 {
7981   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
7982   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
7983
7984   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
7985
7986   return cc_reg;
7987 }
7988
7989 /* Generate a sequence of insns that will generate the correct return
7990    address mask depending on the physical architecture that the program
7991    is running on.  */
7992 rtx
7993 arm_gen_return_addr_mask (void)
7994 {
7995   rtx reg = gen_reg_rtx (Pmode);
7996
7997   emit_insn (gen_return_addr_mask (reg));
7998   return reg;
7999 }
8000
8001 void
8002 arm_reload_in_hi (rtx *operands)
8003 {
8004   rtx ref = operands[1];
8005   rtx base, scratch;
8006   HOST_WIDE_INT offset = 0;
8007
8008   if (GET_CODE (ref) == SUBREG)
8009     {
8010       offset = SUBREG_BYTE (ref);
8011       ref = SUBREG_REG (ref);
8012     }
8013
8014   if (GET_CODE (ref) == REG)
8015     {
8016       /* We have a pseudo which has been spilt onto the stack; there
8017          are two cases here: the first where there is a simple
8018          stack-slot replacement and a second where the stack-slot is
8019          out of range, or is used as a subreg.  */
8020       if (reg_equiv_mem[REGNO (ref)])
8021         {
8022           ref = reg_equiv_mem[REGNO (ref)];
8023           base = find_replacement (&XEXP (ref, 0));
8024         }
8025       else
8026         /* The slot is out of range, or was dressed up in a SUBREG.  */
8027         base = reg_equiv_address[REGNO (ref)];
8028     }
8029   else
8030     base = find_replacement (&XEXP (ref, 0));
8031
8032   /* Handle the case where the address is too complex to be offset by 1.  */
8033   if (GET_CODE (base) == MINUS
8034       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8035     {
8036       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8037
8038       emit_set_insn (base_plus, base);
8039       base = base_plus;
8040     }
8041   else if (GET_CODE (base) == PLUS)
8042     {
8043       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8044       HOST_WIDE_INT hi, lo;
8045
8046       offset += INTVAL (XEXP (base, 1));
8047       base = XEXP (base, 0);
8048
8049       /* Rework the address into a legal sequence of insns.  */
8050       /* Valid range for lo is -4095 -> 4095 */
8051       lo = (offset >= 0
8052             ? (offset & 0xfff)
8053             : -((-offset) & 0xfff));
8054
8055       /* Corner case, if lo is the max offset then we would be out of range
8056          once we have added the additional 1 below, so bump the msb into the
8057          pre-loading insn(s).  */
8058       if (lo == 4095)
8059         lo &= 0x7ff;
8060
8061       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8062              ^ (HOST_WIDE_INT) 0x80000000)
8063             - (HOST_WIDE_INT) 0x80000000);
8064
8065       gcc_assert (hi + lo == offset);
8066
8067       if (hi != 0)
8068         {
8069           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8070
8071           /* Get the base address; addsi3 knows how to handle constants
8072              that require more than one insn.  */
8073           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8074           base = base_plus;
8075           offset = lo;
8076         }
8077     }
8078
8079   /* Operands[2] may overlap operands[0] (though it won't overlap
8080      operands[1]), that's why we asked for a DImode reg -- so we can
8081      use the bit that does not overlap.  */
8082   if (REGNO (operands[2]) == REGNO (operands[0]))
8083     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8084   else
8085     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8086
8087   emit_insn (gen_zero_extendqisi2 (scratch,
8088                                    gen_rtx_MEM (QImode,
8089                                                 plus_constant (base,
8090                                                                offset))));
8091   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
8092                                    gen_rtx_MEM (QImode,
8093                                                 plus_constant (base,
8094                                                                offset + 1))));
8095   if (!BYTES_BIG_ENDIAN)
8096     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8097                    gen_rtx_IOR (SImode,
8098                                 gen_rtx_ASHIFT
8099                                 (SImode,
8100                                  gen_rtx_SUBREG (SImode, operands[0], 0),
8101                                  GEN_INT (8)),
8102                                 scratch));
8103   else
8104     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8105                    gen_rtx_IOR (SImode,
8106                                 gen_rtx_ASHIFT (SImode, scratch,
8107                                                 GEN_INT (8)),
8108                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
8109 }
8110
8111 /* Handle storing a half-word to memory during reload by synthesizing as two
8112    byte stores.  Take care not to clobber the input values until after we
8113    have moved them somewhere safe.  This code assumes that if the DImode
8114    scratch in operands[2] overlaps either the input value or output address
8115    in some way, then that value must die in this insn (we absolutely need
8116    two scratch registers for some corner cases).  */
8117 void
8118 arm_reload_out_hi (rtx *operands)
8119 {
8120   rtx ref = operands[0];
8121   rtx outval = operands[1];
8122   rtx base, scratch;
8123   HOST_WIDE_INT offset = 0;
8124
8125   if (GET_CODE (ref) == SUBREG)
8126     {
8127       offset = SUBREG_BYTE (ref);
8128       ref = SUBREG_REG (ref);
8129     }
8130
8131   if (GET_CODE (ref) == REG)
8132     {
8133       /* We have a pseudo which has been spilt onto the stack; there
8134          are two cases here: the first where there is a simple
8135          stack-slot replacement and a second where the stack-slot is
8136          out of range, or is used as a subreg.  */
8137       if (reg_equiv_mem[REGNO (ref)])
8138         {
8139           ref = reg_equiv_mem[REGNO (ref)];
8140           base = find_replacement (&XEXP (ref, 0));
8141         }
8142       else
8143         /* The slot is out of range, or was dressed up in a SUBREG.  */
8144         base = reg_equiv_address[REGNO (ref)];
8145     }
8146   else
8147     base = find_replacement (&XEXP (ref, 0));
8148
8149   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8150
8151   /* Handle the case where the address is too complex to be offset by 1.  */
8152   if (GET_CODE (base) == MINUS
8153       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8154     {
8155       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8156
8157       /* Be careful not to destroy OUTVAL.  */
8158       if (reg_overlap_mentioned_p (base_plus, outval))
8159         {
8160           /* Updating base_plus might destroy outval, see if we can
8161              swap the scratch and base_plus.  */
8162           if (!reg_overlap_mentioned_p (scratch, outval))
8163             {
8164               rtx tmp = scratch;
8165               scratch = base_plus;
8166               base_plus = tmp;
8167             }
8168           else
8169             {
8170               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8171
8172               /* Be conservative and copy OUTVAL into the scratch now,
8173                  this should only be necessary if outval is a subreg
8174                  of something larger than a word.  */
8175               /* XXX Might this clobber base?  I can't see how it can,
8176                  since scratch is known to overlap with OUTVAL, and
8177                  must be wider than a word.  */
8178               emit_insn (gen_movhi (scratch_hi, outval));
8179               outval = scratch_hi;
8180             }
8181         }
8182
8183       emit_set_insn (base_plus, base);
8184       base = base_plus;
8185     }
8186   else if (GET_CODE (base) == PLUS)
8187     {
8188       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8189       HOST_WIDE_INT hi, lo;
8190
8191       offset += INTVAL (XEXP (base, 1));
8192       base = XEXP (base, 0);
8193
8194       /* Rework the address into a legal sequence of insns.  */
8195       /* Valid range for lo is -4095 -> 4095 */
8196       lo = (offset >= 0
8197             ? (offset & 0xfff)
8198             : -((-offset) & 0xfff));
8199
8200       /* Corner case, if lo is the max offset then we would be out of range
8201          once we have added the additional 1 below, so bump the msb into the
8202          pre-loading insn(s).  */
8203       if (lo == 4095)
8204         lo &= 0x7ff;
8205
8206       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8207              ^ (HOST_WIDE_INT) 0x80000000)
8208             - (HOST_WIDE_INT) 0x80000000);
8209
8210       gcc_assert (hi + lo == offset);
8211
8212       if (hi != 0)
8213         {
8214           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8215
8216           /* Be careful not to destroy OUTVAL.  */
8217           if (reg_overlap_mentioned_p (base_plus, outval))
8218             {
8219               /* Updating base_plus might destroy outval, see if we
8220                  can swap the scratch and base_plus.  */
8221               if (!reg_overlap_mentioned_p (scratch, outval))
8222                 {
8223                   rtx tmp = scratch;
8224                   scratch = base_plus;
8225                   base_plus = tmp;
8226                 }
8227               else
8228                 {
8229                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8230
8231                   /* Be conservative and copy outval into scratch now,
8232                      this should only be necessary if outval is a
8233                      subreg of something larger than a word.  */
8234                   /* XXX Might this clobber base?  I can't see how it
8235                      can, since scratch is known to overlap with
8236                      outval.  */
8237                   emit_insn (gen_movhi (scratch_hi, outval));
8238                   outval = scratch_hi;
8239                 }
8240             }
8241
8242           /* Get the base address; addsi3 knows how to handle constants
8243              that require more than one insn.  */
8244           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8245           base = base_plus;
8246           offset = lo;
8247         }
8248     }
8249
8250   if (BYTES_BIG_ENDIAN)
8251     {
8252       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8253                                          plus_constant (base, offset + 1)),
8254                             gen_lowpart (QImode, outval)));
8255       emit_insn (gen_lshrsi3 (scratch,
8256                               gen_rtx_SUBREG (SImode, outval, 0),
8257                               GEN_INT (8)));
8258       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8259                             gen_lowpart (QImode, scratch)));
8260     }
8261   else
8262     {
8263       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8264                             gen_lowpart (QImode, outval)));
8265       emit_insn (gen_lshrsi3 (scratch,
8266                               gen_rtx_SUBREG (SImode, outval, 0),
8267                               GEN_INT (8)));
8268       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8269                                          plus_constant (base, offset + 1)),
8270                             gen_lowpart (QImode, scratch)));
8271     }
8272 }
8273
8274 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
8275    (padded to the size of a word) should be passed in a register.  */
8276
8277 static bool
8278 arm_must_pass_in_stack (enum machine_mode mode, const_tree type)
8279 {
8280   if (TARGET_AAPCS_BASED)
8281     return must_pass_in_stack_var_size (mode, type);
8282   else
8283     return must_pass_in_stack_var_size_or_pad (mode, type);
8284 }
8285
8286
8287 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
8288    Return true if an argument passed on the stack should be padded upwards,
8289    i.e. if the least-significant byte has useful data.
8290    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
8291    aggregate types are placed in the lowest memory address.  */
8292
8293 bool
8294 arm_pad_arg_upward (enum machine_mode mode, const_tree type)
8295 {
8296   if (!TARGET_AAPCS_BASED)
8297     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
8298
8299   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
8300     return false;
8301
8302   return true;
8303 }
8304
8305
8306 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
8307    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
8308    byte of the register has useful data, and return the opposite if the
8309    most significant byte does.
8310    For AAPCS, small aggregates and small complex types are always padded
8311    upwards.  */
8312
8313 bool
8314 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
8315                     tree type, int first ATTRIBUTE_UNUSED)
8316 {
8317   if (TARGET_AAPCS_BASED
8318       && BYTES_BIG_ENDIAN
8319       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
8320       && int_size_in_bytes (type) <= 4)
8321     return true;
8322
8323   /* Otherwise, use default padding.  */
8324   return !BYTES_BIG_ENDIAN;
8325 }
8326
8327 \f
8328 /* Print a symbolic form of X to the debug file, F.  */
8329 static void
8330 arm_print_value (FILE *f, rtx x)
8331 {
8332   switch (GET_CODE (x))
8333     {
8334     case CONST_INT:
8335       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
8336       return;
8337
8338     case CONST_DOUBLE:
8339       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
8340       return;
8341
8342     case CONST_VECTOR:
8343       {
8344         int i;
8345
8346         fprintf (f, "<");
8347         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
8348           {
8349             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
8350             if (i < (CONST_VECTOR_NUNITS (x) - 1))
8351               fputc (',', f);
8352           }
8353         fprintf (f, ">");
8354       }
8355       return;
8356
8357     case CONST_STRING:
8358       fprintf (f, "\"%s\"", XSTR (x, 0));
8359       return;
8360
8361     case SYMBOL_REF:
8362       fprintf (f, "`%s'", XSTR (x, 0));
8363       return;
8364
8365     case LABEL_REF:
8366       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
8367       return;
8368
8369     case CONST:
8370       arm_print_value (f, XEXP (x, 0));
8371       return;
8372
8373     case PLUS:
8374       arm_print_value (f, XEXP (x, 0));
8375       fprintf (f, "+");
8376       arm_print_value (f, XEXP (x, 1));
8377       return;
8378
8379     case PC:
8380       fprintf (f, "pc");
8381       return;
8382
8383     default:
8384       fprintf (f, "????");
8385       return;
8386     }
8387 }
8388 \f
8389 /* Routines for manipulation of the constant pool.  */
8390
8391 /* Arm instructions cannot load a large constant directly into a
8392    register; they have to come from a pc relative load.  The constant
8393    must therefore be placed in the addressable range of the pc
8394    relative load.  Depending on the precise pc relative load
8395    instruction the range is somewhere between 256 bytes and 4k.  This
8396    means that we often have to dump a constant inside a function, and
8397    generate code to branch around it.
8398
8399    It is important to minimize this, since the branches will slow
8400    things down and make the code larger.
8401
8402    Normally we can hide the table after an existing unconditional
8403    branch so that there is no interruption of the flow, but in the
8404    worst case the code looks like this:
8405
8406         ldr     rn, L1
8407         ...
8408         b       L2
8409         align
8410         L1:     .long value
8411         L2:
8412         ...
8413
8414         ldr     rn, L3
8415         ...
8416         b       L4
8417         align
8418         L3:     .long value
8419         L4:
8420         ...
8421
8422    We fix this by performing a scan after scheduling, which notices
8423    which instructions need to have their operands fetched from the
8424    constant table and builds the table.
8425
8426    The algorithm starts by building a table of all the constants that
8427    need fixing up and all the natural barriers in the function (places
8428    where a constant table can be dropped without breaking the flow).
8429    For each fixup we note how far the pc-relative replacement will be
8430    able to reach and the offset of the instruction into the function.
8431
8432    Having built the table we then group the fixes together to form
8433    tables that are as large as possible (subject to addressing
8434    constraints) and emit each table of constants after the last
8435    barrier that is within range of all the instructions in the group.
8436    If a group does not contain a barrier, then we forcibly create one
8437    by inserting a jump instruction into the flow.  Once the table has
8438    been inserted, the insns are then modified to reference the
8439    relevant entry in the pool.
8440
8441    Possible enhancements to the algorithm (not implemented) are:
8442
8443    1) For some processors and object formats, there may be benefit in
8444    aligning the pools to the start of cache lines; this alignment
8445    would need to be taken into account when calculating addressability
8446    of a pool.  */
8447
8448 /* These typedefs are located at the start of this file, so that
8449    they can be used in the prototypes there.  This comment is to
8450    remind readers of that fact so that the following structures
8451    can be understood more easily.
8452
8453      typedef struct minipool_node    Mnode;
8454      typedef struct minipool_fixup   Mfix;  */
8455
8456 struct minipool_node
8457 {
8458   /* Doubly linked chain of entries.  */
8459   Mnode * next;
8460   Mnode * prev;
8461   /* The maximum offset into the code that this entry can be placed.  While
8462      pushing fixes for forward references, all entries are sorted in order
8463      of increasing max_address.  */
8464   HOST_WIDE_INT max_address;
8465   /* Similarly for an entry inserted for a backwards ref.  */
8466   HOST_WIDE_INT min_address;
8467   /* The number of fixes referencing this entry.  This can become zero
8468      if we "unpush" an entry.  In this case we ignore the entry when we
8469      come to emit the code.  */
8470   int refcount;
8471   /* The offset from the start of the minipool.  */
8472   HOST_WIDE_INT offset;
8473   /* The value in table.  */
8474   rtx value;
8475   /* The mode of value.  */
8476   enum machine_mode mode;
8477   /* The size of the value.  With iWMMXt enabled
8478      sizes > 4 also imply an alignment of 8-bytes.  */
8479   int fix_size;
8480 };
8481
8482 struct minipool_fixup
8483 {
8484   Mfix *            next;
8485   rtx               insn;
8486   HOST_WIDE_INT     address;
8487   rtx *             loc;
8488   enum machine_mode mode;
8489   int               fix_size;
8490   rtx               value;
8491   Mnode *           minipool;
8492   HOST_WIDE_INT     forwards;
8493   HOST_WIDE_INT     backwards;
8494 };
8495
8496 /* Fixes less than a word need padding out to a word boundary.  */
8497 #define MINIPOOL_FIX_SIZE(mode) \
8498   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
8499
8500 static Mnode *  minipool_vector_head;
8501 static Mnode *  minipool_vector_tail;
8502 static rtx      minipool_vector_label;
8503 static int      minipool_pad;
8504
8505 /* The linked list of all minipool fixes required for this function.  */
8506 Mfix *          minipool_fix_head;
8507 Mfix *          minipool_fix_tail;
8508 /* The fix entry for the current minipool, once it has been placed.  */
8509 Mfix *          minipool_barrier;
8510
8511 /* Determines if INSN is the start of a jump table.  Returns the end
8512    of the TABLE or NULL_RTX.  */
8513 static rtx
8514 is_jump_table (rtx insn)
8515 {
8516   rtx table;
8517
8518   if (GET_CODE (insn) == JUMP_INSN
8519       && JUMP_LABEL (insn) != NULL
8520       && ((table = next_real_insn (JUMP_LABEL (insn)))
8521           == next_real_insn (insn))
8522       && table != NULL
8523       && GET_CODE (table) == JUMP_INSN
8524       && (GET_CODE (PATTERN (table)) == ADDR_VEC
8525           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
8526     return table;
8527
8528   return NULL_RTX;
8529 }
8530
8531 #ifndef JUMP_TABLES_IN_TEXT_SECTION
8532 #define JUMP_TABLES_IN_TEXT_SECTION 0
8533 #endif
8534
8535 static HOST_WIDE_INT
8536 get_jump_table_size (rtx insn)
8537 {
8538   /* ADDR_VECs only take room if read-only data does into the text
8539      section.  */
8540   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
8541     {
8542       rtx body = PATTERN (insn);
8543       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
8544       HOST_WIDE_INT size;
8545       HOST_WIDE_INT modesize;
8546
8547       modesize = GET_MODE_SIZE (GET_MODE (body));
8548       size = modesize * XVECLEN (body, elt);
8549       switch (modesize)
8550         {
8551         case 1:
8552           /* Round up size  of TBB table to a halfword boundary.  */
8553           size = (size + 1) & ~(HOST_WIDE_INT)1;
8554           break;
8555         case 2:
8556           /* No padding necessary for TBH.  */
8557           break;
8558         case 4:
8559           /* Add two bytes for alignment on Thumb.  */
8560           if (TARGET_THUMB)
8561             size += 2;
8562           break;
8563         default:
8564           gcc_unreachable ();
8565         }
8566       return size;
8567     }
8568
8569   return 0;
8570 }
8571
8572 /* Move a minipool fix MP from its current location to before MAX_MP.
8573    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
8574    constraints may need updating.  */
8575 static Mnode *
8576 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
8577                                HOST_WIDE_INT max_address)
8578 {
8579   /* The code below assumes these are different.  */
8580   gcc_assert (mp != max_mp);
8581
8582   if (max_mp == NULL)
8583     {
8584       if (max_address < mp->max_address)
8585         mp->max_address = max_address;
8586     }
8587   else
8588     {
8589       if (max_address > max_mp->max_address - mp->fix_size)
8590         mp->max_address = max_mp->max_address - mp->fix_size;
8591       else
8592         mp->max_address = max_address;
8593
8594       /* Unlink MP from its current position.  Since max_mp is non-null,
8595        mp->prev must be non-null.  */
8596       mp->prev->next = mp->next;
8597       if (mp->next != NULL)
8598         mp->next->prev = mp->prev;
8599       else
8600         minipool_vector_tail = mp->prev;
8601
8602       /* Re-insert it before MAX_MP.  */
8603       mp->next = max_mp;
8604       mp->prev = max_mp->prev;
8605       max_mp->prev = mp;
8606
8607       if (mp->prev != NULL)
8608         mp->prev->next = mp;
8609       else
8610         minipool_vector_head = mp;
8611     }
8612
8613   /* Save the new entry.  */
8614   max_mp = mp;
8615
8616   /* Scan over the preceding entries and adjust their addresses as
8617      required.  */
8618   while (mp->prev != NULL
8619          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8620     {
8621       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8622       mp = mp->prev;
8623     }
8624
8625   return max_mp;
8626 }
8627
8628 /* Add a constant to the minipool for a forward reference.  Returns the
8629    node added or NULL if the constant will not fit in this pool.  */
8630 static Mnode *
8631 add_minipool_forward_ref (Mfix *fix)
8632 {
8633   /* If set, max_mp is the first pool_entry that has a lower
8634      constraint than the one we are trying to add.  */
8635   Mnode *       max_mp = NULL;
8636   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
8637   Mnode *       mp;
8638
8639   /* If the minipool starts before the end of FIX->INSN then this FIX
8640      can not be placed into the current pool.  Furthermore, adding the
8641      new constant pool entry may cause the pool to start FIX_SIZE bytes
8642      earlier.  */
8643   if (minipool_vector_head &&
8644       (fix->address + get_attr_length (fix->insn)
8645        >= minipool_vector_head->max_address - fix->fix_size))
8646     return NULL;
8647
8648   /* Scan the pool to see if a constant with the same value has
8649      already been added.  While we are doing this, also note the
8650      location where we must insert the constant if it doesn't already
8651      exist.  */
8652   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8653     {
8654       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8655           && fix->mode == mp->mode
8656           && (GET_CODE (fix->value) != CODE_LABEL
8657               || (CODE_LABEL_NUMBER (fix->value)
8658                   == CODE_LABEL_NUMBER (mp->value)))
8659           && rtx_equal_p (fix->value, mp->value))
8660         {
8661           /* More than one fix references this entry.  */
8662           mp->refcount++;
8663           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
8664         }
8665
8666       /* Note the insertion point if necessary.  */
8667       if (max_mp == NULL
8668           && mp->max_address > max_address)
8669         max_mp = mp;
8670
8671       /* If we are inserting an 8-bytes aligned quantity and
8672          we have not already found an insertion point, then
8673          make sure that all such 8-byte aligned quantities are
8674          placed at the start of the pool.  */
8675       if (ARM_DOUBLEWORD_ALIGN
8676           && max_mp == NULL
8677           && fix->fix_size >= 8
8678           && mp->fix_size < 8)
8679         {
8680           max_mp = mp;
8681           max_address = mp->max_address;
8682         }
8683     }
8684
8685   /* The value is not currently in the minipool, so we need to create
8686      a new entry for it.  If MAX_MP is NULL, the entry will be put on
8687      the end of the list since the placement is less constrained than
8688      any existing entry.  Otherwise, we insert the new fix before
8689      MAX_MP and, if necessary, adjust the constraints on the other
8690      entries.  */
8691   mp = XNEW (Mnode);
8692   mp->fix_size = fix->fix_size;
8693   mp->mode = fix->mode;
8694   mp->value = fix->value;
8695   mp->refcount = 1;
8696   /* Not yet required for a backwards ref.  */
8697   mp->min_address = -65536;
8698
8699   if (max_mp == NULL)
8700     {
8701       mp->max_address = max_address;
8702       mp->next = NULL;
8703       mp->prev = minipool_vector_tail;
8704
8705       if (mp->prev == NULL)
8706         {
8707           minipool_vector_head = mp;
8708           minipool_vector_label = gen_label_rtx ();
8709         }
8710       else
8711         mp->prev->next = mp;
8712
8713       minipool_vector_tail = mp;
8714     }
8715   else
8716     {
8717       if (max_address > max_mp->max_address - mp->fix_size)
8718         mp->max_address = max_mp->max_address - mp->fix_size;
8719       else
8720         mp->max_address = max_address;
8721
8722       mp->next = max_mp;
8723       mp->prev = max_mp->prev;
8724       max_mp->prev = mp;
8725       if (mp->prev != NULL)
8726         mp->prev->next = mp;
8727       else
8728         minipool_vector_head = mp;
8729     }
8730
8731   /* Save the new entry.  */
8732   max_mp = mp;
8733
8734   /* Scan over the preceding entries and adjust their addresses as
8735      required.  */
8736   while (mp->prev != NULL
8737          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8738     {
8739       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8740       mp = mp->prev;
8741     }
8742
8743   return max_mp;
8744 }
8745
8746 static Mnode *
8747 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
8748                                 HOST_WIDE_INT  min_address)
8749 {
8750   HOST_WIDE_INT offset;
8751
8752   /* The code below assumes these are different.  */
8753   gcc_assert (mp != min_mp);
8754
8755   if (min_mp == NULL)
8756     {
8757       if (min_address > mp->min_address)
8758         mp->min_address = min_address;
8759     }
8760   else
8761     {
8762       /* We will adjust this below if it is too loose.  */
8763       mp->min_address = min_address;
8764
8765       /* Unlink MP from its current position.  Since min_mp is non-null,
8766          mp->next must be non-null.  */
8767       mp->next->prev = mp->prev;
8768       if (mp->prev != NULL)
8769         mp->prev->next = mp->next;
8770       else
8771         minipool_vector_head = mp->next;
8772
8773       /* Reinsert it after MIN_MP.  */
8774       mp->prev = min_mp;
8775       mp->next = min_mp->next;
8776       min_mp->next = mp;
8777       if (mp->next != NULL)
8778         mp->next->prev = mp;
8779       else
8780         minipool_vector_tail = mp;
8781     }
8782
8783   min_mp = mp;
8784
8785   offset = 0;
8786   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8787     {
8788       mp->offset = offset;
8789       if (mp->refcount > 0)
8790         offset += mp->fix_size;
8791
8792       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
8793         mp->next->min_address = mp->min_address + mp->fix_size;
8794     }
8795
8796   return min_mp;
8797 }
8798
8799 /* Add a constant to the minipool for a backward reference.  Returns the
8800    node added or NULL if the constant will not fit in this pool.
8801
8802    Note that the code for insertion for a backwards reference can be
8803    somewhat confusing because the calculated offsets for each fix do
8804    not take into account the size of the pool (which is still under
8805    construction.  */
8806 static Mnode *
8807 add_minipool_backward_ref (Mfix *fix)
8808 {
8809   /* If set, min_mp is the last pool_entry that has a lower constraint
8810      than the one we are trying to add.  */
8811   Mnode *min_mp = NULL;
8812   /* This can be negative, since it is only a constraint.  */
8813   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
8814   Mnode *mp;
8815
8816   /* If we can't reach the current pool from this insn, or if we can't
8817      insert this entry at the end of the pool without pushing other
8818      fixes out of range, then we don't try.  This ensures that we
8819      can't fail later on.  */
8820   if (min_address >= minipool_barrier->address
8821       || (minipool_vector_tail->min_address + fix->fix_size
8822           >= minipool_barrier->address))
8823     return NULL;
8824
8825   /* Scan the pool to see if a constant with the same value has
8826      already been added.  While we are doing this, also note the
8827      location where we must insert the constant if it doesn't already
8828      exist.  */
8829   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
8830     {
8831       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8832           && fix->mode == mp->mode
8833           && (GET_CODE (fix->value) != CODE_LABEL
8834               || (CODE_LABEL_NUMBER (fix->value)
8835                   == CODE_LABEL_NUMBER (mp->value)))
8836           && rtx_equal_p (fix->value, mp->value)
8837           /* Check that there is enough slack to move this entry to the
8838              end of the table (this is conservative).  */
8839           && (mp->max_address
8840               > (minipool_barrier->address
8841                  + minipool_vector_tail->offset
8842                  + minipool_vector_tail->fix_size)))
8843         {
8844           mp->refcount++;
8845           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
8846         }
8847
8848       if (min_mp != NULL)
8849         mp->min_address += fix->fix_size;
8850       else
8851         {
8852           /* Note the insertion point if necessary.  */
8853           if (mp->min_address < min_address)
8854             {
8855               /* For now, we do not allow the insertion of 8-byte alignment
8856                  requiring nodes anywhere but at the start of the pool.  */
8857               if (ARM_DOUBLEWORD_ALIGN
8858                   && fix->fix_size >= 8 && mp->fix_size < 8)
8859                 return NULL;
8860               else
8861                 min_mp = mp;
8862             }
8863           else if (mp->max_address
8864                    < minipool_barrier->address + mp->offset + fix->fix_size)
8865             {
8866               /* Inserting before this entry would push the fix beyond
8867                  its maximum address (which can happen if we have
8868                  re-located a forwards fix); force the new fix to come
8869                  after it.  */
8870               if (ARM_DOUBLEWORD_ALIGN
8871                   && fix->fix_size >= 8 && mp->fix_size < 8)
8872                 return NULL;
8873               else
8874                 {
8875                   min_mp = mp;
8876                   min_address = mp->min_address + fix->fix_size;
8877                 }
8878             }
8879           /* Do not insert a non-8-byte aligned quantity before 8-byte
8880              aligned quantities.  */
8881           else if (ARM_DOUBLEWORD_ALIGN
8882                    && fix->fix_size < 8
8883                    && mp->fix_size >= 8)
8884             {
8885               min_mp = mp;
8886               min_address = mp->min_address + fix->fix_size;
8887             }
8888         }
8889     }
8890
8891   /* We need to create a new entry.  */
8892   mp = XNEW (Mnode);
8893   mp->fix_size = fix->fix_size;
8894   mp->mode = fix->mode;
8895   mp->value = fix->value;
8896   mp->refcount = 1;
8897   mp->max_address = minipool_barrier->address + 65536;
8898
8899   mp->min_address = min_address;
8900
8901   if (min_mp == NULL)
8902     {
8903       mp->prev = NULL;
8904       mp->next = minipool_vector_head;
8905
8906       if (mp->next == NULL)
8907         {
8908           minipool_vector_tail = mp;
8909           minipool_vector_label = gen_label_rtx ();
8910         }
8911       else
8912         mp->next->prev = mp;
8913
8914       minipool_vector_head = mp;
8915     }
8916   else
8917     {
8918       mp->next = min_mp->next;
8919       mp->prev = min_mp;
8920       min_mp->next = mp;
8921
8922       if (mp->next != NULL)
8923         mp->next->prev = mp;
8924       else
8925         minipool_vector_tail = mp;
8926     }
8927
8928   /* Save the new entry.  */
8929   min_mp = mp;
8930
8931   if (mp->prev)
8932     mp = mp->prev;
8933   else
8934     mp->offset = 0;
8935
8936   /* Scan over the following entries and adjust their offsets.  */
8937   while (mp->next != NULL)
8938     {
8939       if (mp->next->min_address < mp->min_address + mp->fix_size)
8940         mp->next->min_address = mp->min_address + mp->fix_size;
8941
8942       if (mp->refcount)
8943         mp->next->offset = mp->offset + mp->fix_size;
8944       else
8945         mp->next->offset = mp->offset;
8946
8947       mp = mp->next;
8948     }
8949
8950   return min_mp;
8951 }
8952
8953 static void
8954 assign_minipool_offsets (Mfix *barrier)
8955 {
8956   HOST_WIDE_INT offset = 0;
8957   Mnode *mp;
8958
8959   minipool_barrier = barrier;
8960
8961   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8962     {
8963       mp->offset = offset;
8964
8965       if (mp->refcount > 0)
8966         offset += mp->fix_size;
8967     }
8968 }
8969
8970 /* Output the literal table */
8971 static void
8972 dump_minipool (rtx scan)
8973 {
8974   Mnode * mp;
8975   Mnode * nmp;
8976   int align64 = 0;
8977
8978   if (ARM_DOUBLEWORD_ALIGN)
8979     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8980       if (mp->refcount > 0 && mp->fix_size >= 8)
8981         {
8982           align64 = 1;
8983           break;
8984         }
8985
8986   if (dump_file)
8987     fprintf (dump_file,
8988              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
8989              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
8990
8991   scan = emit_label_after (gen_label_rtx (), scan);
8992   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
8993   scan = emit_label_after (minipool_vector_label, scan);
8994
8995   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
8996     {
8997       if (mp->refcount > 0)
8998         {
8999           if (dump_file)
9000             {
9001               fprintf (dump_file,
9002                        ";;  Offset %u, min %ld, max %ld ",
9003                        (unsigned) mp->offset, (unsigned long) mp->min_address,
9004                        (unsigned long) mp->max_address);
9005               arm_print_value (dump_file, mp->value);
9006               fputc ('\n', dump_file);
9007             }
9008
9009           switch (mp->fix_size)
9010             {
9011 #ifdef HAVE_consttable_1
9012             case 1:
9013               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
9014               break;
9015
9016 #endif
9017 #ifdef HAVE_consttable_2
9018             case 2:
9019               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
9020               break;
9021
9022 #endif
9023 #ifdef HAVE_consttable_4
9024             case 4:
9025               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
9026               break;
9027
9028 #endif
9029 #ifdef HAVE_consttable_8
9030             case 8:
9031               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
9032               break;
9033
9034 #endif
9035 #ifdef HAVE_consttable_16
9036             case 16:
9037               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
9038               break;
9039
9040 #endif
9041             default:
9042               gcc_unreachable ();
9043             }
9044         }
9045
9046       nmp = mp->next;
9047       free (mp);
9048     }
9049
9050   minipool_vector_head = minipool_vector_tail = NULL;
9051   scan = emit_insn_after (gen_consttable_end (), scan);
9052   scan = emit_barrier_after (scan);
9053 }
9054
9055 /* Return the cost of forcibly inserting a barrier after INSN.  */
9056 static int
9057 arm_barrier_cost (rtx insn)
9058 {
9059   /* Basing the location of the pool on the loop depth is preferable,
9060      but at the moment, the basic block information seems to be
9061      corrupt by this stage of the compilation.  */
9062   int base_cost = 50;
9063   rtx next = next_nonnote_insn (insn);
9064
9065   if (next != NULL && GET_CODE (next) == CODE_LABEL)
9066     base_cost -= 20;
9067
9068   switch (GET_CODE (insn))
9069     {
9070     case CODE_LABEL:
9071       /* It will always be better to place the table before the label, rather
9072          than after it.  */
9073       return 50;
9074
9075     case INSN:
9076     case CALL_INSN:
9077       return base_cost;
9078
9079     case JUMP_INSN:
9080       return base_cost - 10;
9081
9082     default:
9083       return base_cost + 10;
9084     }
9085 }
9086
9087 /* Find the best place in the insn stream in the range
9088    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
9089    Create the barrier by inserting a jump and add a new fix entry for
9090    it.  */
9091 static Mfix *
9092 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
9093 {
9094   HOST_WIDE_INT count = 0;
9095   rtx barrier;
9096   rtx from = fix->insn;
9097   /* The instruction after which we will insert the jump.  */
9098   rtx selected = NULL;
9099   int selected_cost;
9100   /* The address at which the jump instruction will be placed.  */
9101   HOST_WIDE_INT selected_address;
9102   Mfix * new_fix;
9103   HOST_WIDE_INT max_count = max_address - fix->address;
9104   rtx label = gen_label_rtx ();
9105
9106   selected_cost = arm_barrier_cost (from);
9107   selected_address = fix->address;
9108
9109   while (from && count < max_count)
9110     {
9111       rtx tmp;
9112       int new_cost;
9113
9114       /* This code shouldn't have been called if there was a natural barrier
9115          within range.  */
9116       gcc_assert (GET_CODE (from) != BARRIER);
9117
9118       /* Count the length of this insn.  */
9119       count += get_attr_length (from);
9120
9121       /* If there is a jump table, add its length.  */
9122       tmp = is_jump_table (from);
9123       if (tmp != NULL)
9124         {
9125           count += get_jump_table_size (tmp);
9126
9127           /* Jump tables aren't in a basic block, so base the cost on
9128              the dispatch insn.  If we select this location, we will
9129              still put the pool after the table.  */
9130           new_cost = arm_barrier_cost (from);
9131
9132           if (count < max_count 
9133               && (!selected || new_cost <= selected_cost))
9134             {
9135               selected = tmp;
9136               selected_cost = new_cost;
9137               selected_address = fix->address + count;
9138             }
9139
9140           /* Continue after the dispatch table.  */
9141           from = NEXT_INSN (tmp);
9142           continue;
9143         }
9144
9145       new_cost = arm_barrier_cost (from);
9146
9147       if (count < max_count
9148           && (!selected || new_cost <= selected_cost))
9149         {
9150           selected = from;
9151           selected_cost = new_cost;
9152           selected_address = fix->address + count;
9153         }
9154
9155       from = NEXT_INSN (from);
9156     }
9157
9158   /* Make sure that we found a place to insert the jump.  */
9159   gcc_assert (selected);
9160
9161   /* Create a new JUMP_INSN that branches around a barrier.  */
9162   from = emit_jump_insn_after (gen_jump (label), selected);
9163   JUMP_LABEL (from) = label;
9164   barrier = emit_barrier_after (from);
9165   emit_label_after (label, barrier);
9166
9167   /* Create a minipool barrier entry for the new barrier.  */
9168   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
9169   new_fix->insn = barrier;
9170   new_fix->address = selected_address;
9171   new_fix->next = fix->next;
9172   fix->next = new_fix;
9173
9174   return new_fix;
9175 }
9176
9177 /* Record that there is a natural barrier in the insn stream at
9178    ADDRESS.  */
9179 static void
9180 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
9181 {
9182   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9183
9184   fix->insn = insn;
9185   fix->address = address;
9186
9187   fix->next = NULL;
9188   if (minipool_fix_head != NULL)
9189     minipool_fix_tail->next = fix;
9190   else
9191     minipool_fix_head = fix;
9192
9193   minipool_fix_tail = fix;
9194 }
9195
9196 /* Record INSN, which will need fixing up to load a value from the
9197    minipool.  ADDRESS is the offset of the insn since the start of the
9198    function; LOC is a pointer to the part of the insn which requires
9199    fixing; VALUE is the constant that must be loaded, which is of type
9200    MODE.  */
9201 static void
9202 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
9203                    enum machine_mode mode, rtx value)
9204 {
9205   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9206
9207   fix->insn = insn;
9208   fix->address = address;
9209   fix->loc = loc;
9210   fix->mode = mode;
9211   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
9212   fix->value = value;
9213   fix->forwards = get_attr_pool_range (insn);
9214   fix->backwards = get_attr_neg_pool_range (insn);
9215   fix->minipool = NULL;
9216
9217   /* If an insn doesn't have a range defined for it, then it isn't
9218      expecting to be reworked by this code.  Better to stop now than
9219      to generate duff assembly code.  */
9220   gcc_assert (fix->forwards || fix->backwards);
9221
9222   /* If an entry requires 8-byte alignment then assume all constant pools
9223      require 4 bytes of padding.  Trying to do this later on a per-pool
9224      basis is awkward because existing pool entries have to be modified.  */
9225   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
9226     minipool_pad = 4;
9227
9228   if (dump_file)
9229     {
9230       fprintf (dump_file,
9231                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
9232                GET_MODE_NAME (mode),
9233                INSN_UID (insn), (unsigned long) address,
9234                -1 * (long)fix->backwards, (long)fix->forwards);
9235       arm_print_value (dump_file, fix->value);
9236       fprintf (dump_file, "\n");
9237     }
9238
9239   /* Add it to the chain of fixes.  */
9240   fix->next = NULL;
9241
9242   if (minipool_fix_head != NULL)
9243     minipool_fix_tail->next = fix;
9244   else
9245     minipool_fix_head = fix;
9246
9247   minipool_fix_tail = fix;
9248 }
9249
9250 /* Return the cost of synthesizing a 64-bit constant VAL inline.
9251    Returns the number of insns needed, or 99 if we don't know how to
9252    do it.  */
9253 int
9254 arm_const_double_inline_cost (rtx val)
9255 {
9256   rtx lowpart, highpart;
9257   enum machine_mode mode;
9258
9259   mode = GET_MODE (val);
9260
9261   if (mode == VOIDmode)
9262     mode = DImode;
9263
9264   gcc_assert (GET_MODE_SIZE (mode) == 8);
9265
9266   lowpart = gen_lowpart (SImode, val);
9267   highpart = gen_highpart_mode (SImode, mode, val);
9268
9269   gcc_assert (GET_CODE (lowpart) == CONST_INT);
9270   gcc_assert (GET_CODE (highpart) == CONST_INT);
9271
9272   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
9273                             NULL_RTX, NULL_RTX, 0, 0)
9274           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
9275                               NULL_RTX, NULL_RTX, 0, 0));
9276 }
9277
9278 /* Return true if it is worthwhile to split a 64-bit constant into two
9279    32-bit operations.  This is the case if optimizing for size, or
9280    if we have load delay slots, or if one 32-bit part can be done with
9281    a single data operation.  */
9282 bool
9283 arm_const_double_by_parts (rtx val)
9284 {
9285   enum machine_mode mode = GET_MODE (val);
9286   rtx part;
9287
9288   if (optimize_size || arm_ld_sched)
9289     return true;
9290
9291   if (mode == VOIDmode)
9292     mode = DImode;
9293
9294   part = gen_highpart_mode (SImode, mode, val);
9295
9296   gcc_assert (GET_CODE (part) == CONST_INT);
9297
9298   if (const_ok_for_arm (INTVAL (part))
9299       || const_ok_for_arm (~INTVAL (part)))
9300     return true;
9301
9302   part = gen_lowpart (SImode, val);
9303
9304   gcc_assert (GET_CODE (part) == CONST_INT);
9305
9306   if (const_ok_for_arm (INTVAL (part))
9307       || const_ok_for_arm (~INTVAL (part)))
9308     return true;
9309
9310   return false;
9311 }
9312
9313 /* Scan INSN and note any of its operands that need fixing.
9314    If DO_PUSHES is false we do not actually push any of the fixups
9315    needed.  The function returns TRUE if any fixups were needed/pushed.
9316    This is used by arm_memory_load_p() which needs to know about loads
9317    of constants that will be converted into minipool loads.  */
9318 static bool
9319 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
9320 {
9321   bool result = false;
9322   int opno;
9323
9324   extract_insn (insn);
9325
9326   if (!constrain_operands (1))
9327     fatal_insn_not_found (insn);
9328
9329   if (recog_data.n_alternatives == 0)
9330     return false;
9331
9332   /* Fill in recog_op_alt with information about the constraints of
9333      this insn.  */
9334   preprocess_constraints ();
9335
9336   for (opno = 0; opno < recog_data.n_operands; opno++)
9337     {
9338       /* Things we need to fix can only occur in inputs.  */
9339       if (recog_data.operand_type[opno] != OP_IN)
9340         continue;
9341
9342       /* If this alternative is a memory reference, then any mention
9343          of constants in this alternative is really to fool reload
9344          into allowing us to accept one there.  We need to fix them up
9345          now so that we output the right code.  */
9346       if (recog_op_alt[opno][which_alternative].memory_ok)
9347         {
9348           rtx op = recog_data.operand[opno];
9349
9350           if (CONSTANT_P (op))
9351             {
9352               if (do_pushes)
9353                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
9354                                    recog_data.operand_mode[opno], op);
9355               result = true;
9356             }
9357           else if (GET_CODE (op) == MEM
9358                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
9359                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
9360             {
9361               if (do_pushes)
9362                 {
9363                   rtx cop = avoid_constant_pool_reference (op);
9364
9365                   /* Casting the address of something to a mode narrower
9366                      than a word can cause avoid_constant_pool_reference()
9367                      to return the pool reference itself.  That's no good to
9368                      us here.  Lets just hope that we can use the
9369                      constant pool value directly.  */
9370                   if (op == cop)
9371                     cop = get_pool_constant (XEXP (op, 0));
9372
9373                   push_minipool_fix (insn, address,
9374                                      recog_data.operand_loc[opno],
9375                                      recog_data.operand_mode[opno], cop);
9376                 }
9377
9378               result = true;
9379             }
9380         }
9381     }
9382
9383   return result;
9384 }
9385
9386 /* Gcc puts the pool in the wrong place for ARM, since we can only
9387    load addresses a limited distance around the pc.  We do some
9388    special munging to move the constant pool values to the correct
9389    point in the code.  */
9390 static void
9391 arm_reorg (void)
9392 {
9393   rtx insn;
9394   HOST_WIDE_INT address = 0;
9395   Mfix * fix;
9396
9397   minipool_fix_head = minipool_fix_tail = NULL;
9398
9399   /* The first insn must always be a note, or the code below won't
9400      scan it properly.  */
9401   insn = get_insns ();
9402   gcc_assert (GET_CODE (insn) == NOTE);
9403   minipool_pad = 0;
9404
9405   /* Scan all the insns and record the operands that will need fixing.  */
9406   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
9407     {
9408       if (TARGET_CIRRUS_FIX_INVALID_INSNS
9409           && (arm_cirrus_insn_p (insn)
9410               || GET_CODE (insn) == JUMP_INSN
9411               || arm_memory_load_p (insn)))
9412         cirrus_reorg (insn);
9413
9414       if (GET_CODE (insn) == BARRIER)
9415         push_minipool_barrier (insn, address);
9416       else if (INSN_P (insn))
9417         {
9418           rtx table;
9419
9420           note_invalid_constants (insn, address, true);
9421           address += get_attr_length (insn);
9422
9423           /* If the insn is a vector jump, add the size of the table
9424              and skip the table.  */
9425           if ((table = is_jump_table (insn)) != NULL)
9426             {
9427               address += get_jump_table_size (table);
9428               insn = table;
9429             }
9430         }
9431     }
9432
9433   fix = minipool_fix_head;
9434
9435   /* Now scan the fixups and perform the required changes.  */
9436   while (fix)
9437     {
9438       Mfix * ftmp;
9439       Mfix * fdel;
9440       Mfix *  last_added_fix;
9441       Mfix * last_barrier = NULL;
9442       Mfix * this_fix;
9443
9444       /* Skip any further barriers before the next fix.  */
9445       while (fix && GET_CODE (fix->insn) == BARRIER)
9446         fix = fix->next;
9447
9448       /* No more fixes.  */
9449       if (fix == NULL)
9450         break;
9451
9452       last_added_fix = NULL;
9453
9454       for (ftmp = fix; ftmp; ftmp = ftmp->next)
9455         {
9456           if (GET_CODE (ftmp->insn) == BARRIER)
9457             {
9458               if (ftmp->address >= minipool_vector_head->max_address)
9459                 break;
9460
9461               last_barrier = ftmp;
9462             }
9463           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
9464             break;
9465
9466           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
9467         }
9468
9469       /* If we found a barrier, drop back to that; any fixes that we
9470          could have reached but come after the barrier will now go in
9471          the next mini-pool.  */
9472       if (last_barrier != NULL)
9473         {
9474           /* Reduce the refcount for those fixes that won't go into this
9475              pool after all.  */
9476           for (fdel = last_barrier->next;
9477                fdel && fdel != ftmp;
9478                fdel = fdel->next)
9479             {
9480               fdel->minipool->refcount--;
9481               fdel->minipool = NULL;
9482             }
9483
9484           ftmp = last_barrier;
9485         }
9486       else
9487         {
9488           /* ftmp is first fix that we can't fit into this pool and
9489              there no natural barriers that we could use.  Insert a
9490              new barrier in the code somewhere between the previous
9491              fix and this one, and arrange to jump around it.  */
9492           HOST_WIDE_INT max_address;
9493
9494           /* The last item on the list of fixes must be a barrier, so
9495              we can never run off the end of the list of fixes without
9496              last_barrier being set.  */
9497           gcc_assert (ftmp);
9498
9499           max_address = minipool_vector_head->max_address;
9500           /* Check that there isn't another fix that is in range that
9501              we couldn't fit into this pool because the pool was
9502              already too large: we need to put the pool before such an
9503              instruction.  The pool itself may come just after the
9504              fix because create_fix_barrier also allows space for a
9505              jump instruction.  */
9506           if (ftmp->address < max_address)
9507             max_address = ftmp->address + 1;
9508
9509           last_barrier = create_fix_barrier (last_added_fix, max_address);
9510         }
9511
9512       assign_minipool_offsets (last_barrier);
9513
9514       while (ftmp)
9515         {
9516           if (GET_CODE (ftmp->insn) != BARRIER
9517               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
9518                   == NULL))
9519             break;
9520
9521           ftmp = ftmp->next;
9522         }
9523
9524       /* Scan over the fixes we have identified for this pool, fixing them
9525          up and adding the constants to the pool itself.  */
9526       for (this_fix = fix; this_fix && ftmp != this_fix;
9527            this_fix = this_fix->next)
9528         if (GET_CODE (this_fix->insn) != BARRIER)
9529           {
9530             rtx addr
9531               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
9532                                                   minipool_vector_label),
9533                                this_fix->minipool->offset);
9534             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
9535           }
9536
9537       dump_minipool (last_barrier->insn);
9538       fix = ftmp;
9539     }
9540
9541   /* From now on we must synthesize any constants that we can't handle
9542      directly.  This can happen if the RTL gets split during final
9543      instruction generation.  */
9544   after_arm_reorg = 1;
9545
9546   /* Free the minipool memory.  */
9547   obstack_free (&minipool_obstack, minipool_startobj);
9548 }
9549 \f
9550 /* Routines to output assembly language.  */
9551
9552 /* If the rtx is the correct value then return the string of the number.
9553    In this way we can ensure that valid double constants are generated even
9554    when cross compiling.  */
9555 const char *
9556 fp_immediate_constant (rtx x)
9557 {
9558   REAL_VALUE_TYPE r;
9559   int i;
9560
9561   if (!fp_consts_inited)
9562     init_fp_table ();
9563
9564   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9565   for (i = 0; i < 8; i++)
9566     if (REAL_VALUES_EQUAL (r, values_fp[i]))
9567       return strings_fp[i];
9568
9569   gcc_unreachable ();
9570 }
9571
9572 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
9573 static const char *
9574 fp_const_from_val (REAL_VALUE_TYPE *r)
9575 {
9576   int i;
9577
9578   if (!fp_consts_inited)
9579     init_fp_table ();
9580
9581   for (i = 0; i < 8; i++)
9582     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
9583       return strings_fp[i];
9584
9585   gcc_unreachable ();
9586 }
9587
9588 /* Output the operands of a LDM/STM instruction to STREAM.
9589    MASK is the ARM register set mask of which only bits 0-15 are important.
9590    REG is the base register, either the frame pointer or the stack pointer,
9591    INSTR is the possibly suffixed load or store instruction.
9592    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
9593
9594 static void
9595 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
9596                  unsigned long mask, int rfe)
9597 {
9598   unsigned i;
9599   bool not_first = FALSE;
9600
9601   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
9602   fputc ('\t', stream);
9603   asm_fprintf (stream, instr, reg);
9604   fputc ('{', stream);
9605
9606   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9607     if (mask & (1 << i))
9608       {
9609         if (not_first)
9610           fprintf (stream, ", ");
9611
9612         asm_fprintf (stream, "%r", i);
9613         not_first = TRUE;
9614       }
9615
9616   if (rfe)
9617     fprintf (stream, "}^\n");
9618   else
9619     fprintf (stream, "}\n");
9620 }
9621
9622
9623 /* Output a FLDMD instruction to STREAM.
9624    BASE if the register containing the address.
9625    REG and COUNT specify the register range.
9626    Extra registers may be added to avoid hardware bugs.
9627
9628    We output FLDMD even for ARMv5 VFP implementations.  Although
9629    FLDMD is technically not supported until ARMv6, it is believed
9630    that all VFP implementations support its use in this context.  */
9631
9632 static void
9633 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
9634 {
9635   int i;
9636
9637   /* Workaround ARM10 VFPr1 bug.  */
9638   if (count == 2 && !arm_arch6)
9639     {
9640       if (reg == 15)
9641         reg--;
9642       count++;
9643     }
9644
9645   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
9646      load into multiple parts if we have to handle more than 16 registers.  */
9647   if (count > 16)
9648     {
9649       vfp_output_fldmd (stream, base, reg, 16);
9650       vfp_output_fldmd (stream, base, reg + 16, count - 16);
9651       return;
9652     }
9653
9654   fputc ('\t', stream);
9655   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9656
9657   for (i = reg; i < reg + count; i++)
9658     {
9659       if (i > reg)
9660         fputs (", ", stream);
9661       asm_fprintf (stream, "d%d", i);
9662     }
9663   fputs ("}\n", stream);
9664
9665 }
9666
9667
9668 /* Output the assembly for a store multiple.  */
9669
9670 const char *
9671 vfp_output_fstmd (rtx * operands)
9672 {
9673   char pattern[100];
9674   int p;
9675   int base;
9676   int i;
9677
9678   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
9679   p = strlen (pattern);
9680
9681   gcc_assert (GET_CODE (operands[1]) == REG);
9682
9683   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
9684   for (i = 1; i < XVECLEN (operands[2], 0); i++)
9685     {
9686       p += sprintf (&pattern[p], ", d%d", base + i);
9687     }
9688   strcpy (&pattern[p], "}");
9689
9690   output_asm_insn (pattern, operands);
9691   return "";
9692 }
9693
9694
9695 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
9696    number of bytes pushed.  */
9697
9698 static int
9699 vfp_emit_fstmd (int base_reg, int count)
9700 {
9701   rtx par;
9702   rtx dwarf;
9703   rtx tmp, reg;
9704   int i;
9705
9706   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
9707      register pairs are stored by a store multiple insn.  We avoid this
9708      by pushing an extra pair.  */
9709   if (count == 2 && !arm_arch6)
9710     {
9711       if (base_reg == LAST_VFP_REGNUM - 3)
9712         base_reg -= 2;
9713       count++;
9714     }
9715
9716   /* FSTMD may not store more than 16 doubleword registers at once.  Split
9717      larger stores into multiple parts (up to a maximum of two, in
9718      practice).  */
9719   if (count > 16)
9720     {
9721       int saved;
9722       /* NOTE: base_reg is an internal register number, so each D register
9723          counts as 2.  */
9724       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
9725       saved += vfp_emit_fstmd (base_reg, 16);
9726       return saved;
9727     }
9728
9729   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9730   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9731
9732   reg = gen_rtx_REG (DFmode, base_reg);
9733   base_reg += 2;
9734
9735   XVECEXP (par, 0, 0)
9736     = gen_rtx_SET (VOIDmode,
9737                    gen_frame_mem (BLKmode,
9738                                   gen_rtx_PRE_DEC (BLKmode,
9739                                                    stack_pointer_rtx)),
9740                    gen_rtx_UNSPEC (BLKmode,
9741                                    gen_rtvec (1, reg),
9742                                    UNSPEC_PUSH_MULT));
9743
9744   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9745                      plus_constant (stack_pointer_rtx, -(count * 8)));
9746   RTX_FRAME_RELATED_P (tmp) = 1;
9747   XVECEXP (dwarf, 0, 0) = tmp;
9748
9749   tmp = gen_rtx_SET (VOIDmode,
9750                      gen_frame_mem (DFmode, stack_pointer_rtx),
9751                      reg);
9752   RTX_FRAME_RELATED_P (tmp) = 1;
9753   XVECEXP (dwarf, 0, 1) = tmp;
9754
9755   for (i = 1; i < count; i++)
9756     {
9757       reg = gen_rtx_REG (DFmode, base_reg);
9758       base_reg += 2;
9759       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9760
9761       tmp = gen_rtx_SET (VOIDmode,
9762                          gen_frame_mem (DFmode,
9763                                         plus_constant (stack_pointer_rtx,
9764                                                        i * 8)),
9765                          reg);
9766       RTX_FRAME_RELATED_P (tmp) = 1;
9767       XVECEXP (dwarf, 0, i + 1) = tmp;
9768     }
9769
9770   par = emit_insn (par);
9771   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9772                                        REG_NOTES (par));
9773   RTX_FRAME_RELATED_P (par) = 1;
9774
9775   return count * 8;
9776 }
9777
9778 /* Emit a call instruction with pattern PAT.  ADDR is the address of
9779    the call target.  */
9780
9781 void
9782 arm_emit_call_insn (rtx pat, rtx addr)
9783 {
9784   rtx insn;
9785
9786   insn = emit_call_insn (pat);
9787
9788   /* The PIC register is live on entry to VxWorks PIC PLT entries.
9789      If the call might use such an entry, add a use of the PIC register
9790      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
9791   if (TARGET_VXWORKS_RTP
9792       && flag_pic
9793       && GET_CODE (addr) == SYMBOL_REF
9794       && (SYMBOL_REF_DECL (addr)
9795           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
9796           : !SYMBOL_REF_LOCAL_P (addr)))
9797     {
9798       require_pic_register ();
9799       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
9800     }
9801 }
9802
9803 /* Output a 'call' insn.  */
9804 const char *
9805 output_call (rtx *operands)
9806 {
9807   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
9808
9809   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
9810   if (REGNO (operands[0]) == LR_REGNUM)
9811     {
9812       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
9813       output_asm_insn ("mov%?\t%0, %|lr", operands);
9814     }
9815
9816   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9817
9818   if (TARGET_INTERWORK || arm_arch4t)
9819     output_asm_insn ("bx%?\t%0", operands);
9820   else
9821     output_asm_insn ("mov%?\t%|pc, %0", operands);
9822
9823   return "";
9824 }
9825
9826 /* Output a 'call' insn that is a reference in memory.  */
9827 const char *
9828 output_call_mem (rtx *operands)
9829 {
9830   if (TARGET_INTERWORK && !arm_arch5)
9831     {
9832       output_asm_insn ("ldr%?\t%|ip, %0", operands);
9833       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9834       output_asm_insn ("bx%?\t%|ip", operands);
9835     }
9836   else if (regno_use_in (LR_REGNUM, operands[0]))
9837     {
9838       /* LR is used in the memory address.  We load the address in the
9839          first instruction.  It's safe to use IP as the target of the
9840          load since the call will kill it anyway.  */
9841       output_asm_insn ("ldr%?\t%|ip, %0", operands);
9842       if (arm_arch5)
9843         output_asm_insn ("blx%?\t%|ip", operands);
9844       else
9845         {
9846           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9847           if (arm_arch4t)
9848             output_asm_insn ("bx%?\t%|ip", operands);
9849           else
9850             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
9851         }
9852     }
9853   else
9854     {
9855       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9856       output_asm_insn ("ldr%?\t%|pc, %0", operands);
9857     }
9858
9859   return "";
9860 }
9861
9862
9863 /* Output a move from arm registers to an fpa registers.
9864    OPERANDS[0] is an fpa register.
9865    OPERANDS[1] is the first registers of an arm register pair.  */
9866 const char *
9867 output_mov_long_double_fpa_from_arm (rtx *operands)
9868 {
9869   int arm_reg0 = REGNO (operands[1]);
9870   rtx ops[3];
9871
9872   gcc_assert (arm_reg0 != IP_REGNUM);
9873
9874   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9875   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9876   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9877
9878   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9879   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
9880
9881   return "";
9882 }
9883
9884 /* Output a move from an fpa register to arm registers.
9885    OPERANDS[0] is the first registers of an arm register pair.
9886    OPERANDS[1] is an fpa register.  */
9887 const char *
9888 output_mov_long_double_arm_from_fpa (rtx *operands)
9889 {
9890   int arm_reg0 = REGNO (operands[0]);
9891   rtx ops[3];
9892
9893   gcc_assert (arm_reg0 != IP_REGNUM);
9894
9895   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9896   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9897   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9898
9899   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
9900   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9901   return "";
9902 }
9903
9904 /* Output a move from arm registers to arm registers of a long double
9905    OPERANDS[0] is the destination.
9906    OPERANDS[1] is the source.  */
9907 const char *
9908 output_mov_long_double_arm_from_arm (rtx *operands)
9909 {
9910   /* We have to be careful here because the two might overlap.  */
9911   int dest_start = REGNO (operands[0]);
9912   int src_start = REGNO (operands[1]);
9913   rtx ops[2];
9914   int i;
9915
9916   if (dest_start < src_start)
9917     {
9918       for (i = 0; i < 3; i++)
9919         {
9920           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9921           ops[1] = gen_rtx_REG (SImode, src_start + i);
9922           output_asm_insn ("mov%?\t%0, %1", ops);
9923         }
9924     }
9925   else
9926     {
9927       for (i = 2; i >= 0; i--)
9928         {
9929           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9930           ops[1] = gen_rtx_REG (SImode, src_start + i);
9931           output_asm_insn ("mov%?\t%0, %1", ops);
9932         }
9933     }
9934
9935   return "";
9936 }
9937
9938
9939 /* Emit a MOVW/MOVT pair.  */
9940 void arm_emit_movpair (rtx dest, rtx src)
9941 {
9942   emit_set_insn (dest, gen_rtx_HIGH (SImode, src));
9943   emit_set_insn (dest, gen_rtx_LO_SUM (SImode, dest, src));
9944 }
9945
9946
9947 /* Output a move from arm registers to an fpa registers.
9948    OPERANDS[0] is an fpa register.
9949    OPERANDS[1] is the first registers of an arm register pair.  */
9950 const char *
9951 output_mov_double_fpa_from_arm (rtx *operands)
9952 {
9953   int arm_reg0 = REGNO (operands[1]);
9954   rtx ops[2];
9955
9956   gcc_assert (arm_reg0 != IP_REGNUM);
9957
9958   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9959   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9960   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
9961   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
9962   return "";
9963 }
9964
9965 /* Output a move from an fpa register to arm registers.
9966    OPERANDS[0] is the first registers of an arm register pair.
9967    OPERANDS[1] is an fpa register.  */
9968 const char *
9969 output_mov_double_arm_from_fpa (rtx *operands)
9970 {
9971   int arm_reg0 = REGNO (operands[0]);
9972   rtx ops[2];
9973
9974   gcc_assert (arm_reg0 != IP_REGNUM);
9975
9976   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9977   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9978   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
9979   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
9980   return "";
9981 }
9982
9983 /* Output a move between double words.
9984    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
9985    or MEM<-REG and all MEMs must be offsettable addresses.  */
9986 const char *
9987 output_move_double (rtx *operands)
9988 {
9989   enum rtx_code code0 = GET_CODE (operands[0]);
9990   enum rtx_code code1 = GET_CODE (operands[1]);
9991   rtx otherops[3];
9992
9993   if (code0 == REG)
9994     {
9995       unsigned int reg0 = REGNO (operands[0]);
9996
9997       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9998
9999       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
10000
10001       switch (GET_CODE (XEXP (operands[1], 0)))
10002         {
10003         case REG:
10004           if (TARGET_LDRD
10005               && !(fix_cm3_ldrd && reg0 == REGNO(XEXP (operands[1], 0))))
10006             output_asm_insn ("ldr%(d%)\t%0, [%m1]", operands);
10007           else
10008             output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10009           break;
10010
10011         case PRE_INC:
10012           gcc_assert (TARGET_LDRD);
10013           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
10014           break;
10015
10016         case PRE_DEC:
10017           if (TARGET_LDRD)
10018             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
10019           else
10020             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
10021           break;
10022
10023         case POST_INC:
10024           if (TARGET_LDRD)
10025             output_asm_insn ("ldr%(d%)\t%0, [%m1], #8", operands);
10026           else
10027             output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
10028           break;
10029
10030         case POST_DEC:
10031           gcc_assert (TARGET_LDRD);
10032           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
10033           break;
10034
10035         case PRE_MODIFY:
10036         case POST_MODIFY:
10037           /* Autoicrement addressing modes should never have overlapping
10038              base and destination registers, and overlapping index registers
10039              are already prohibited, so this doesn't need to worry about
10040              fix_cm3_ldrd.  */
10041           otherops[0] = operands[0];
10042           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
10043           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
10044
10045           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
10046             {
10047               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
10048                 {
10049                   /* Registers overlap so split out the increment.  */
10050                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
10051                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
10052                 }
10053               else
10054                 {
10055                   /* IWMMXT allows offsets larger than ldrd can handle,
10056                      fix these up with a pair of ldr.  */
10057                   if (GET_CODE (otherops[2]) == CONST_INT
10058                       && (INTVAL(otherops[2]) <= -256
10059                           || INTVAL(otherops[2]) >= 256))
10060                     {
10061                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10062                       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10063                       output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10064                     }
10065                   else
10066                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
10067                 }
10068             }
10069           else
10070             {
10071               /* IWMMXT allows offsets larger than ldrd can handle,
10072                  fix these up with a pair of ldr.  */
10073               if (GET_CODE (otherops[2]) == CONST_INT
10074                   && (INTVAL(otherops[2]) <= -256
10075                       || INTVAL(otherops[2]) >= 256))
10076                 {
10077                   otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
10078                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10079                   otherops[0] = operands[0];
10080                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10081                 }
10082               else
10083                 /* We only allow constant increments, so this is safe.  */
10084                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
10085             }
10086           break;
10087
10088         case LABEL_REF:
10089         case CONST:
10090           /* We might be able to use ldrd %0, %1 here.  However the range is
10091              different to ldr/adr, and it is broken on some ARMv7-M
10092              implementations.  */
10093           /* Use the second register of the pair to avoid problematic
10094              overlap.  */
10095           otherops[1] = operands[1];
10096           output_asm_insn ("adr%?\t%0, %1", otherops);
10097           operands[1] = otherops[0];
10098           if (TARGET_LDRD)
10099             output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10100           else
10101             output_asm_insn ("ldm%(ia%)\t%1, %M0", operands);
10102           break;
10103
10104           /* ??? This needs checking for thumb2.  */
10105         default:
10106           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
10107                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
10108             {
10109               otherops[0] = operands[0];
10110               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
10111               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
10112
10113               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
10114                 {
10115                   if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10116                     {
10117                       switch ((int) INTVAL (otherops[2]))
10118                         {
10119                         case -8:
10120                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
10121                           return "";
10122                         case -4:
10123                           if (TARGET_THUMB2)
10124                             break;
10125                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
10126                           return "";
10127                         case 4:
10128                           if (TARGET_THUMB2)
10129                             break;
10130                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
10131                           return "";
10132                         }
10133                     }
10134                   otherops[0] = gen_rtx_REG(SImode, REGNO(operands[0]) + 1);
10135                   operands[1] = otherops[0];
10136                   if (TARGET_LDRD
10137                       && (GET_CODE (otherops[2]) == REG
10138                           || (GET_CODE (otherops[2]) == CONST_INT
10139                               && INTVAL (otherops[2]) > -256
10140                               && INTVAL (otherops[2]) < 256)))
10141                     {
10142                       if (reg_overlap_mentioned_p (operands[0],
10143                                                    otherops[2]))
10144                         {
10145                           rtx tmp;
10146                           /* Swap base and index registers over to
10147                              avoid a conflict.  */
10148                           tmp = otherops[1];
10149                           otherops[1] = otherops[2];
10150                           otherops[2] = tmp;
10151                         }
10152                       /* If both registers conflict, it will usually
10153                          have been fixed by a splitter.  */
10154                       if (reg_overlap_mentioned_p (operands[0], otherops[2])
10155                           || (fix_cm3_ldrd && reg0 == REGNO (otherops[1])))
10156                         {
10157                           output_asm_insn ("add%?\t%0, %1, %2", otherops);
10158                           output_asm_insn ("ldr%(d%)\t%0, [%1]", operands);
10159                         }
10160                       else
10161                         {
10162                           otherops[0] = operands[0];
10163                           output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
10164                         }
10165                       return "";
10166                     }
10167
10168                   if (GET_CODE (otherops[2]) == CONST_INT)
10169                     {
10170                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
10171                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
10172                       else
10173                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
10174                     }
10175                   else
10176                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
10177                 }
10178               else
10179                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
10180
10181               if (TARGET_LDRD)
10182                 return "ldr%(d%)\t%0, [%1]";
10183
10184               return "ldm%(ia%)\t%1, %M0";
10185             }
10186           else
10187             {
10188               otherops[1] = adjust_address (operands[1], SImode, 4);
10189               /* Take care of overlapping base/data reg.  */
10190               if (reg_mentioned_p (operands[0], operands[1]))
10191                 {
10192                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10193                   output_asm_insn ("ldr%?\t%0, %1", operands);
10194                 }
10195               else
10196                 {
10197                   output_asm_insn ("ldr%?\t%0, %1", operands);
10198                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10199                 }
10200             }
10201         }
10202     }
10203   else
10204     {
10205       /* Constraints should ensure this.  */
10206       gcc_assert (code0 == MEM && code1 == REG);
10207       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
10208
10209       switch (GET_CODE (XEXP (operands[0], 0)))
10210         {
10211         case REG:
10212           if (TARGET_LDRD)
10213             output_asm_insn ("str%(d%)\t%1, [%m0]", operands);
10214           else
10215             output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10216           break;
10217
10218         case PRE_INC:
10219           gcc_assert (TARGET_LDRD);
10220           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
10221           break;
10222
10223         case PRE_DEC:
10224           if (TARGET_LDRD)
10225             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
10226           else
10227             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
10228           break;
10229
10230         case POST_INC:
10231           if (TARGET_LDRD)
10232             output_asm_insn ("str%(d%)\t%1, [%m0], #8", operands);
10233           else
10234             output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
10235           break;
10236
10237         case POST_DEC:
10238           gcc_assert (TARGET_LDRD);
10239           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
10240           break;
10241
10242         case PRE_MODIFY:
10243         case POST_MODIFY:
10244           otherops[0] = operands[1];
10245           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
10246           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
10247
10248           /* IWMMXT allows offsets larger than ldrd can handle,
10249              fix these up with a pair of ldr.  */
10250           if (GET_CODE (otherops[2]) == CONST_INT
10251               && (INTVAL(otherops[2]) <= -256
10252                   || INTVAL(otherops[2]) >= 256))
10253             {
10254               rtx reg1;
10255               reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10256               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10257                 {
10258                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10259                   otherops[0] = reg1;
10260                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10261                 }
10262               else
10263                 {
10264                   otherops[0] = reg1;
10265                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10266                   otherops[0] = operands[1];
10267                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10268                 }
10269             }
10270           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10271             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
10272           else
10273             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
10274           break;
10275
10276         case PLUS:
10277           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
10278           if (GET_CODE (otherops[2]) == CONST_INT && !TARGET_LDRD)
10279             {
10280               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
10281                 {
10282                 case -8:
10283                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
10284                   return "";
10285
10286                 case -4:
10287                   if (TARGET_THUMB2)
10288                     break;
10289                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
10290                   return "";
10291
10292                 case 4:
10293                   if (TARGET_THUMB2)
10294                     break;
10295                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
10296                   return "";
10297                 }
10298             }
10299           if (TARGET_LDRD
10300               && (GET_CODE (otherops[2]) == REG
10301                   || (GET_CODE (otherops[2]) == CONST_INT
10302                       && INTVAL (otherops[2]) > -256
10303                       && INTVAL (otherops[2]) < 256)))
10304             {
10305               otherops[0] = operands[1];
10306               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
10307               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
10308               return "";
10309             }
10310           /* Fall through */
10311
10312         default:
10313           otherops[0] = adjust_address (operands[0], SImode, 4);
10314           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10315           output_asm_insn ("str%?\t%1, %0", operands);
10316           output_asm_insn ("str%?\t%1, %0", otherops);
10317         }
10318     }
10319
10320   return "";
10321 }
10322
10323 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
10324    handles MEMs accepted by neon_vector_mem_operand with CORE=true.  */
10325
10326 const char *
10327 output_move_quad (rtx *operands)
10328 {
10329   if (REG_P (operands[0]))
10330     {
10331       /* Load, or reg->reg move.  */
10332
10333       if (MEM_P (operands[1]))
10334         {
10335           switch (GET_CODE (XEXP (operands[1], 0)))
10336             {
10337             case REG:
10338               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10339               break;
10340
10341             case LABEL_REF:
10342             case CONST:
10343               output_asm_insn ("adr%?\t%0, %1", operands);
10344               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
10345               break;
10346
10347             default:
10348               gcc_unreachable ();
10349             }
10350         }
10351       else
10352         {
10353           rtx ops[2];
10354           int dest, src, i;
10355
10356           gcc_assert (REG_P (operands[1]));
10357
10358           dest = REGNO (operands[0]);
10359           src = REGNO (operands[1]);
10360
10361           /* This seems pretty dumb, but hopefully GCC won't try to do it
10362              very often.  */
10363           if (dest < src)
10364             for (i = 0; i < 4; i++)
10365               {
10366                 ops[0] = gen_rtx_REG (SImode, dest + i);
10367                 ops[1] = gen_rtx_REG (SImode, src + i);
10368                 output_asm_insn ("mov%?\t%0, %1", ops);
10369               }
10370           else
10371             for (i = 3; i >= 0; i--)
10372               {
10373                 ops[0] = gen_rtx_REG (SImode, dest + i);
10374                 ops[1] = gen_rtx_REG (SImode, src + i);
10375                 output_asm_insn ("mov%?\t%0, %1", ops);
10376               }
10377         }
10378     }
10379   else
10380     {
10381       gcc_assert (MEM_P (operands[0]));
10382       gcc_assert (REG_P (operands[1]));
10383       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
10384
10385       switch (GET_CODE (XEXP (operands[0], 0)))
10386         {
10387         case REG:
10388           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10389           break;
10390
10391         default:
10392           gcc_unreachable ();
10393         }
10394     }
10395
10396   return "";
10397 }
10398
10399 /* Output a VFP load or store instruction.  */
10400
10401 const char *
10402 output_move_vfp (rtx *operands)
10403 {
10404   rtx reg, mem, addr, ops[2];
10405   int load = REG_P (operands[0]);
10406   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
10407   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
10408   const char *templ;
10409   char buff[50];
10410   enum machine_mode mode;
10411
10412   reg = operands[!load];
10413   mem = operands[load];
10414
10415   mode = GET_MODE (reg);
10416
10417   gcc_assert (REG_P (reg));
10418   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
10419   gcc_assert (mode == SFmode
10420               || mode == DFmode
10421               || mode == SImode
10422               || mode == DImode
10423               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
10424   gcc_assert (MEM_P (mem));
10425
10426   addr = XEXP (mem, 0);
10427
10428   switch (GET_CODE (addr))
10429     {
10430     case PRE_DEC:
10431       templ = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
10432       ops[0] = XEXP (addr, 0);
10433       ops[1] = reg;
10434       break;
10435
10436     case POST_INC:
10437       templ = "f%smia%c%%?\t%%0!, {%%%s1}%s";
10438       ops[0] = XEXP (addr, 0);
10439       ops[1] = reg;
10440       break;
10441
10442     default:
10443       templ = "f%s%c%%?\t%%%s0, %%1%s";
10444       ops[0] = reg;
10445       ops[1] = mem;
10446       break;
10447     }
10448
10449   sprintf (buff, templ,
10450            load ? "ld" : "st",
10451            dp ? 'd' : 's',
10452            dp ? "P" : "",
10453            integer_p ? "\t%@ int" : "");
10454   output_asm_insn (buff, ops);
10455
10456   return "";
10457 }
10458
10459 /* Output a Neon quad-word load or store, or a load or store for
10460    larger structure modes.
10461
10462    WARNING: The ordering of elements is weird in big-endian mode,
10463    because we use VSTM, as required by the EABI.  GCC RTL defines
10464    element ordering based on in-memory order.  This can be differ
10465    from the architectural ordering of elements within a NEON register.
10466    The intrinsics defined in arm_neon.h use the NEON register element
10467    ordering, not the GCC RTL element ordering.
10468
10469    For example, the in-memory ordering of a big-endian a quadword
10470    vector with 16-bit elements when stored from register pair {d0,d1}
10471    will be (lowest address first, d0[N] is NEON register element N):
10472
10473      [d0[3], d0[2], d0[1], d0[0], d1[7], d1[6], d1[5], d1[4]]
10474
10475    When necessary, quadword registers (dN, dN+1) are moved to ARM
10476    registers from rN in the order:
10477
10478      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
10479
10480    So that STM/LDM can be used on vectors in ARM registers, and the
10481    same memory layout will result as if VSTM/VLDM were used.  */
10482
10483 const char *
10484 output_move_neon (rtx *operands)
10485 {
10486   rtx reg, mem, addr, ops[2];
10487   int regno, load = REG_P (operands[0]);
10488   const char *templ;
10489   char buff[50];
10490   enum machine_mode mode;
10491
10492   reg = operands[!load];
10493   mem = operands[load];
10494
10495   mode = GET_MODE (reg);
10496
10497   gcc_assert (REG_P (reg));
10498   regno = REGNO (reg);
10499   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
10500               || NEON_REGNO_OK_FOR_QUAD (regno));
10501   gcc_assert (VALID_NEON_DREG_MODE (mode)
10502               || VALID_NEON_QREG_MODE (mode)
10503               || VALID_NEON_STRUCT_MODE (mode));
10504   gcc_assert (MEM_P (mem));
10505
10506   addr = XEXP (mem, 0);
10507
10508   /* Strip off const from addresses like (const (plus (...))).  */
10509   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
10510     addr = XEXP (addr, 0);
10511
10512   switch (GET_CODE (addr))
10513     {
10514     case POST_INC:
10515       templ = "v%smia%%?\t%%0!, %%h1";
10516       ops[0] = XEXP (addr, 0);
10517       ops[1] = reg;
10518       break;
10519
10520     case POST_MODIFY:
10521       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
10522       gcc_unreachable ();
10523
10524     case LABEL_REF:
10525     case PLUS:
10526       {
10527         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
10528         int i;
10529         int overlap = -1;
10530         for (i = 0; i < nregs; i++)
10531           {
10532             /* We're only using DImode here because it's a convenient size.  */
10533             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
10534             ops[1] = adjust_address (mem, SImode, 8 * i);
10535             if (reg_overlap_mentioned_p (ops[0], mem))
10536               {
10537                 gcc_assert (overlap == -1);
10538                 overlap = i;
10539               }
10540             else
10541               {
10542                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10543                 output_asm_insn (buff, ops);
10544               }
10545           }
10546         if (overlap != -1)
10547           {
10548             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
10549             ops[1] = adjust_address (mem, SImode, 8 * overlap);
10550             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10551             output_asm_insn (buff, ops);
10552           }
10553
10554         return "";
10555       }
10556
10557     default:
10558       templ = "v%smia%%?\t%%m0, %%h1";
10559       ops[0] = mem;
10560       ops[1] = reg;
10561     }
10562
10563   sprintf (buff, templ, load ? "ld" : "st");
10564   output_asm_insn (buff, ops);
10565
10566   return "";
10567 }
10568
10569 /* Output an ADD r, s, #n where n may be too big for one instruction.
10570    If adding zero to one register, output nothing.  */
10571 const char *
10572 output_add_immediate (rtx *operands)
10573 {
10574   HOST_WIDE_INT n = INTVAL (operands[2]);
10575
10576   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
10577     {
10578       if (n < 0)
10579         output_multi_immediate (operands,
10580                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
10581                                 -n);
10582       else
10583         output_multi_immediate (operands,
10584                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
10585                                 n);
10586     }
10587
10588   return "";
10589 }
10590
10591 /* Output a multiple immediate operation.
10592    OPERANDS is the vector of operands referred to in the output patterns.
10593    INSTR1 is the output pattern to use for the first constant.
10594    INSTR2 is the output pattern to use for subsequent constants.
10595    IMMED_OP is the index of the constant slot in OPERANDS.
10596    N is the constant value.  */
10597 static const char *
10598 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
10599                         int immed_op, HOST_WIDE_INT n)
10600 {
10601 #if HOST_BITS_PER_WIDE_INT > 32
10602   n &= 0xffffffff;
10603 #endif
10604
10605   if (n == 0)
10606     {
10607       /* Quick and easy output.  */
10608       operands[immed_op] = const0_rtx;
10609       output_asm_insn (instr1, operands);
10610     }
10611   else
10612     {
10613       int i;
10614       const char * instr = instr1;
10615
10616       /* Note that n is never zero here (which would give no output).  */
10617       for (i = 0; i < 32; i += 2)
10618         {
10619           if (n & (3 << i))
10620             {
10621               operands[immed_op] = GEN_INT (n & (255 << i));
10622               output_asm_insn (instr, operands);
10623               instr = instr2;
10624               i += 6;
10625             }
10626         }
10627     }
10628
10629   return "";
10630 }
10631
10632 /* Return the name of a shifter operation.  */
10633 static const char *
10634 arm_shift_nmem(enum rtx_code code)
10635 {
10636   switch (code)
10637     {
10638     case ASHIFT:
10639       return ARM_LSL_NAME;
10640
10641     case ASHIFTRT:
10642       return "asr";
10643
10644     case LSHIFTRT:
10645       return "lsr";
10646
10647     case ROTATERT:
10648       return "ror";
10649
10650     default:
10651       abort();
10652     }
10653 }
10654
10655 /* Return the appropriate ARM instruction for the operation code.
10656    The returned result should not be overwritten.  OP is the rtx of the
10657    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
10658    was shifted.  */
10659 const char *
10660 arithmetic_instr (rtx op, int shift_first_arg)
10661 {
10662   switch (GET_CODE (op))
10663     {
10664     case PLUS:
10665       return "add";
10666
10667     case MINUS:
10668       return shift_first_arg ? "rsb" : "sub";
10669
10670     case IOR:
10671       return "orr";
10672
10673     case XOR:
10674       return "eor";
10675
10676     case AND:
10677       return "and";
10678
10679     case ASHIFT:
10680     case ASHIFTRT:
10681     case LSHIFTRT:
10682     case ROTATERT:
10683       return arm_shift_nmem(GET_CODE(op));
10684
10685     default:
10686       gcc_unreachable ();
10687     }
10688 }
10689
10690 /* Ensure valid constant shifts and return the appropriate shift mnemonic
10691    for the operation code.  The returned result should not be overwritten.
10692    OP is the rtx code of the shift.
10693    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
10694    shift.  */
10695 static const char *
10696 shift_op (rtx op, HOST_WIDE_INT *amountp)
10697 {
10698   const char * mnem;
10699   enum rtx_code code = GET_CODE (op);
10700
10701   switch (GET_CODE (XEXP (op, 1)))
10702     {
10703     case REG:
10704     case SUBREG:
10705       *amountp = -1;
10706       break;
10707
10708     case CONST_INT:
10709       *amountp = INTVAL (XEXP (op, 1));
10710       break;
10711
10712     default:
10713       gcc_unreachable ();
10714     }
10715
10716   switch (code)
10717     {
10718     case ROTATE:
10719       gcc_assert (*amountp != -1);
10720       *amountp = 32 - *amountp;
10721       code = ROTATERT;
10722
10723       /* Fall through.  */
10724
10725     case ASHIFT:
10726     case ASHIFTRT:
10727     case LSHIFTRT:
10728     case ROTATERT:
10729       mnem = arm_shift_nmem(code);
10730       break;
10731
10732     case MULT:
10733       /* We never have to worry about the amount being other than a
10734          power of 2, since this case can never be reloaded from a reg.  */
10735       gcc_assert (*amountp != -1);
10736       *amountp = int_log2 (*amountp);
10737       return ARM_LSL_NAME;
10738
10739     default:
10740       gcc_unreachable ();
10741     }
10742
10743   if (*amountp != -1)
10744     {
10745       /* This is not 100% correct, but follows from the desire to merge
10746          multiplication by a power of 2 with the recognizer for a
10747          shift.  >=32 is not a valid shift for "lsl", so we must try and
10748          output a shift that produces the correct arithmetical result.
10749          Using lsr #32 is identical except for the fact that the carry bit
10750          is not set correctly if we set the flags; but we never use the
10751          carry bit from such an operation, so we can ignore that.  */
10752       if (code == ROTATERT)
10753         /* Rotate is just modulo 32.  */
10754         *amountp &= 31;
10755       else if (*amountp != (*amountp & 31))
10756         {
10757           if (code == ASHIFT)
10758             mnem = "lsr";
10759           *amountp = 32;
10760         }
10761
10762       /* Shifts of 0 are no-ops.  */
10763       if (*amountp == 0)
10764         return NULL;
10765     }
10766
10767   return mnem;
10768 }
10769
10770 /* Obtain the shift from the POWER of two.  */
10771
10772 static HOST_WIDE_INT
10773 int_log2 (HOST_WIDE_INT power)
10774 {
10775   HOST_WIDE_INT shift = 0;
10776
10777   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
10778     {
10779       gcc_assert (shift <= 31);
10780       shift++;
10781     }
10782
10783   return shift;
10784 }
10785
10786 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
10787    because /bin/as is horribly restrictive.  The judgement about
10788    whether or not each character is 'printable' (and can be output as
10789    is) or not (and must be printed with an octal escape) must be made
10790    with reference to the *host* character set -- the situation is
10791    similar to that discussed in the comments above pp_c_char in
10792    c-pretty-print.c.  */
10793
10794 #define MAX_ASCII_LEN 51
10795
10796 void
10797 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
10798 {
10799   int i;
10800   int len_so_far = 0;
10801
10802   fputs ("\t.ascii\t\"", stream);
10803
10804   for (i = 0; i < len; i++)
10805     {
10806       int c = p[i];
10807
10808       if (len_so_far >= MAX_ASCII_LEN)
10809         {
10810           fputs ("\"\n\t.ascii\t\"", stream);
10811           len_so_far = 0;
10812         }
10813
10814       if (ISPRINT (c))
10815         {
10816           if (c == '\\' || c == '\"')
10817             {
10818               putc ('\\', stream);
10819               len_so_far++;
10820             }
10821           putc (c, stream);
10822           len_so_far++;
10823         }
10824       else
10825         {
10826           fprintf (stream, "\\%03o", c);
10827           len_so_far += 4;
10828         }
10829     }
10830
10831   fputs ("\"\n", stream);
10832 }
10833 \f
10834 /* Compute the register save mask for registers 0 through 12
10835    inclusive.  This code is used by arm_compute_save_reg_mask.  */
10836
10837 static unsigned long
10838 arm_compute_save_reg0_reg12_mask (void)
10839 {
10840   unsigned long func_type = arm_current_func_type ();
10841   unsigned long save_reg_mask = 0;
10842   unsigned int reg;
10843
10844   if (IS_INTERRUPT (func_type))
10845     {
10846       unsigned int max_reg;
10847       /* Interrupt functions must not corrupt any registers,
10848          even call clobbered ones.  If this is a leaf function
10849          we can just examine the registers used by the RTL, but
10850          otherwise we have to assume that whatever function is
10851          called might clobber anything, and so we have to save
10852          all the call-clobbered registers as well.  */
10853       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
10854         /* FIQ handlers have registers r8 - r12 banked, so
10855            we only need to check r0 - r7, Normal ISRs only
10856            bank r14 and r15, so we must check up to r12.
10857            r13 is the stack pointer which is always preserved,
10858            so we do not need to consider it here.  */
10859         max_reg = 7;
10860       else
10861         max_reg = 12;
10862
10863       for (reg = 0; reg <= max_reg; reg++)
10864         if (df_regs_ever_live_p (reg)
10865             || (! current_function_is_leaf && call_used_regs[reg]))
10866           save_reg_mask |= (1 << reg);
10867
10868       /* Also save the pic base register if necessary.  */
10869       if (flag_pic
10870           && !TARGET_SINGLE_PIC_BASE
10871           && arm_pic_register != INVALID_REGNUM
10872           && crtl->uses_pic_offset_table)
10873         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10874     }
10875   else
10876     {
10877       /* In the normal case we only need to save those registers
10878          which are call saved and which are used by this function.  */
10879       for (reg = 0; reg <= 11; reg++)
10880         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
10881           save_reg_mask |= (1 << reg);
10882
10883       /* Handle the frame pointer as a special case.  */
10884       if (frame_pointer_needed)
10885         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
10886
10887       /* If we aren't loading the PIC register,
10888          don't stack it even though it may be live.  */
10889       if (flag_pic
10890           && !TARGET_SINGLE_PIC_BASE
10891           && arm_pic_register != INVALID_REGNUM
10892           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
10893               || crtl->uses_pic_offset_table))
10894         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10895
10896       /* The prologue will copy SP into R0, so save it.  */
10897       if (IS_STACKALIGN (func_type))
10898         save_reg_mask |= 1;
10899     }
10900
10901   /* Save registers so the exception handler can modify them.  */
10902   if (crtl->calls_eh_return)
10903     {
10904       unsigned int i;
10905
10906       for (i = 0; ; i++)
10907         {
10908           reg = EH_RETURN_DATA_REGNO (i);
10909           if (reg == INVALID_REGNUM)
10910             break;
10911           save_reg_mask |= 1 << reg;
10912         }
10913     }
10914
10915   return save_reg_mask;
10916 }
10917
10918
10919 /* Compute the number of bytes used to store the static chain register on the 
10920    stack, above the stack frame. We need to know this accurately to get the
10921    alignment of the rest of the stack frame correct. */
10922
10923 static int arm_compute_static_chain_stack_bytes (void)
10924 {
10925   unsigned long func_type = arm_current_func_type ();
10926   int static_chain_stack_bytes = 0;
10927
10928   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM &&
10929       IS_NESTED (func_type) &&
10930       df_regs_ever_live_p (3) && crtl->args.pretend_args_size == 0)
10931     static_chain_stack_bytes = 4;
10932
10933   return static_chain_stack_bytes;
10934 }
10935
10936
10937 /* Compute a bit mask of which registers need to be
10938    saved on the stack for the current function.
10939    This is used by arm_get_frame_offsets, which may add extra registers.  */
10940
10941 static unsigned long
10942 arm_compute_save_reg_mask (void)
10943 {
10944   unsigned int save_reg_mask = 0;
10945   unsigned long func_type = arm_current_func_type ();
10946   unsigned int reg;
10947
10948   if (IS_NAKED (func_type))
10949     /* This should never really happen.  */
10950     return 0;
10951
10952   /* If we are creating a stack frame, then we must save the frame pointer,
10953      IP (which will hold the old stack pointer), LR and the PC.  */
10954   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
10955     save_reg_mask |=
10956       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
10957       | (1 << IP_REGNUM)
10958       | (1 << LR_REGNUM)
10959       | (1 << PC_REGNUM);
10960
10961   /* Volatile functions do not return, so there
10962      is no need to save any other registers.  */
10963   if (IS_VOLATILE (func_type))
10964     return save_reg_mask;
10965
10966   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
10967
10968   /* Decide if we need to save the link register.
10969      Interrupt routines have their own banked link register,
10970      so they never need to save it.
10971      Otherwise if we do not use the link register we do not need to save
10972      it.  If we are pushing other registers onto the stack however, we
10973      can save an instruction in the epilogue by pushing the link register
10974      now and then popping it back into the PC.  This incurs extra memory
10975      accesses though, so we only do it when optimizing for size, and only
10976      if we know that we will not need a fancy return sequence.  */
10977   if (df_regs_ever_live_p (LR_REGNUM)
10978       || (save_reg_mask
10979           && optimize_size
10980           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10981           && !crtl->calls_eh_return))
10982     save_reg_mask |= 1 << LR_REGNUM;
10983
10984   if (cfun->machine->lr_save_eliminated)
10985     save_reg_mask &= ~ (1 << LR_REGNUM);
10986
10987   if (TARGET_REALLY_IWMMXT
10988       && ((bit_count (save_reg_mask)
10989            + ARM_NUM_INTS (crtl->args.pretend_args_size +
10990                            arm_compute_static_chain_stack_bytes())
10991            ) % 2) != 0)
10992     {
10993       /* The total number of registers that are going to be pushed
10994          onto the stack is odd.  We need to ensure that the stack
10995          is 64-bit aligned before we start to save iWMMXt registers,
10996          and also before we start to create locals.  (A local variable
10997          might be a double or long long which we will load/store using
10998          an iWMMXt instruction).  Therefore we need to push another
10999          ARM register, so that the stack will be 64-bit aligned.  We
11000          try to avoid using the arg registers (r0 -r3) as they might be
11001          used to pass values in a tail call.  */
11002       for (reg = 4; reg <= 12; reg++)
11003         if ((save_reg_mask & (1 << reg)) == 0)
11004           break;
11005
11006       if (reg <= 12)
11007         save_reg_mask |= (1 << reg);
11008       else
11009         {
11010           cfun->machine->sibcall_blocked = 1;
11011           save_reg_mask |= (1 << 3);
11012         }
11013     }
11014
11015   /* We may need to push an additional register for use initializing the
11016      PIC base register.  */
11017   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
11018       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
11019     {
11020       reg = thumb_find_work_register (1 << 4);
11021       if (!call_used_regs[reg])
11022         save_reg_mask |= (1 << reg);
11023     }
11024
11025   return save_reg_mask;
11026 }
11027
11028
11029 /* Compute a bit mask of which registers need to be
11030    saved on the stack for the current function.  */
11031 static unsigned long
11032 thumb1_compute_save_reg_mask (void)
11033 {
11034   unsigned long mask;
11035   unsigned reg;
11036
11037   mask = 0;
11038   for (reg = 0; reg < 12; reg ++)
11039     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11040       mask |= 1 << reg;
11041
11042   if (flag_pic
11043       && !TARGET_SINGLE_PIC_BASE
11044       && arm_pic_register != INVALID_REGNUM
11045       && crtl->uses_pic_offset_table)
11046     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
11047
11048   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
11049   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
11050     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
11051
11052   /* LR will also be pushed if any lo regs are pushed.  */
11053   if (mask & 0xff || thumb_force_lr_save ())
11054     mask |= (1 << LR_REGNUM);
11055
11056   /* Make sure we have a low work register if we need one.
11057      We will need one if we are going to push a high register,
11058      but we are not currently intending to push a low register.  */
11059   if ((mask & 0xff) == 0
11060       && ((mask & 0x0f00) || TARGET_BACKTRACE))
11061     {
11062       /* Use thumb_find_work_register to choose which register
11063          we will use.  If the register is live then we will
11064          have to push it.  Use LAST_LO_REGNUM as our fallback
11065          choice for the register to select.  */
11066       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
11067       /* Make sure the register returned by thumb_find_work_register is
11068          not part of the return value.  */
11069       if (reg * UNITS_PER_WORD <= (unsigned) arm_size_return_regs ())
11070         reg = LAST_LO_REGNUM;
11071
11072       if (! call_used_regs[reg])
11073         mask |= 1 << reg;
11074     }
11075
11076   /* The 504 below is 8 bytes less than 512 because there are two possible
11077      alignment words.  We can't tell here if they will be present or not so we
11078      have to play it safe and assume that they are. */
11079   if ((CALLER_INTERWORKING_SLOT_SIZE +
11080        ROUND_UP_WORD (get_frame_size ()) +
11081        crtl->outgoing_args_size) >= 504)
11082     {
11083       /* This is the same as the code in thumb1_expand_prologue() which
11084          determines which register to use for stack decrement. */
11085       for (reg = LAST_ARG_REGNUM + 1; reg <= LAST_LO_REGNUM; reg++)
11086         if (mask & (1 << reg))
11087           break;
11088
11089       if (reg > LAST_LO_REGNUM)
11090         {
11091           /* Make sure we have a register available for stack decrement. */
11092           mask |= 1 << LAST_LO_REGNUM;
11093         }
11094     }
11095
11096   return mask;
11097 }
11098
11099
11100 /* Return the number of bytes required to save VFP registers.  */
11101 static int
11102 arm_get_vfp_saved_size (void)
11103 {
11104   unsigned int regno;
11105   int count;
11106   int saved;
11107
11108   saved = 0;
11109   /* Space for saved VFP registers.  */
11110   if (TARGET_HARD_FLOAT && TARGET_VFP)
11111     {
11112       count = 0;
11113       for (regno = FIRST_VFP_REGNUM;
11114            regno < LAST_VFP_REGNUM;
11115            regno += 2)
11116         {
11117           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
11118               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
11119             {
11120               if (count > 0)
11121                 {
11122                   /* Workaround ARM10 VFPr1 bug.  */
11123                   if (count == 2 && !arm_arch6)
11124                     count++;
11125                   saved += count * 8;
11126                 }
11127               count = 0;
11128             }
11129           else
11130             count++;
11131         }
11132       if (count > 0)
11133         {
11134           if (count == 2 && !arm_arch6)
11135             count++;
11136           saved += count * 8;
11137         }
11138     }
11139   return saved;
11140 }
11141
11142
11143 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
11144    everything bar the final return instruction.  */
11145 const char *
11146 output_return_instruction (rtx operand, int really_return, int reverse)
11147 {
11148   char conditional[10];
11149   char instr[100];
11150   unsigned reg;
11151   unsigned long live_regs_mask;
11152   unsigned long func_type;
11153   arm_stack_offsets *offsets;
11154
11155   func_type = arm_current_func_type ();
11156
11157   if (IS_NAKED (func_type))
11158     return "";
11159
11160   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11161     {
11162       /* If this function was declared non-returning, and we have
11163          found a tail call, then we have to trust that the called
11164          function won't return.  */
11165       if (really_return)
11166         {
11167           rtx ops[2];
11168
11169           /* Otherwise, trap an attempted return by aborting.  */
11170           ops[0] = operand;
11171           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
11172                                        : "abort");
11173           assemble_external_libcall (ops[1]);
11174           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
11175         }
11176
11177       return "";
11178     }
11179
11180   gcc_assert (!cfun->calls_alloca || really_return);
11181
11182   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
11183
11184   return_used_this_function = 1;
11185
11186   offsets = arm_get_frame_offsets ();
11187   live_regs_mask = offsets->saved_regs_mask;
11188
11189   if (live_regs_mask)
11190     {
11191       const char * return_reg;
11192
11193       /* If we do not have any special requirements for function exit
11194          (e.g. interworking) then we can load the return address
11195          directly into the PC.  Otherwise we must load it into LR.  */
11196       if (really_return
11197           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
11198         return_reg = reg_names[PC_REGNUM];
11199       else
11200         return_reg = reg_names[LR_REGNUM];
11201
11202       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
11203         {
11204           /* There are three possible reasons for the IP register
11205              being saved.  1) a stack frame was created, in which case
11206              IP contains the old stack pointer, or 2) an ISR routine
11207              corrupted it, or 3) it was saved to align the stack on
11208              iWMMXt.  In case 1, restore IP into SP, otherwise just
11209              restore IP.  */
11210           if (frame_pointer_needed)
11211             {
11212               live_regs_mask &= ~ (1 << IP_REGNUM);
11213               live_regs_mask |=   (1 << SP_REGNUM);
11214             }
11215           else
11216             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
11217         }
11218
11219       /* On some ARM architectures it is faster to use LDR rather than
11220          LDM to load a single register.  On other architectures, the
11221          cost is the same.  In 26 bit mode, or for exception handlers,
11222          we have to use LDM to load the PC so that the CPSR is also
11223          restored.  */
11224       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11225         if (live_regs_mask == (1U << reg))
11226           break;
11227
11228       if (reg <= LAST_ARM_REGNUM
11229           && (reg != LR_REGNUM
11230               || ! really_return
11231               || ! IS_INTERRUPT (func_type)))
11232         {
11233           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
11234                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
11235         }
11236       else
11237         {
11238           char *p;
11239           int first = 1;
11240
11241           /* Generate the load multiple instruction to restore the
11242              registers.  Note we can get here, even if
11243              frame_pointer_needed is true, but only if sp already
11244              points to the base of the saved core registers.  */
11245           if (live_regs_mask & (1 << SP_REGNUM))
11246             {
11247               unsigned HOST_WIDE_INT stack_adjust;
11248
11249               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
11250               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
11251
11252               if (stack_adjust && arm_arch5 && TARGET_ARM)
11253                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
11254               else
11255                 {
11256                   /* If we can't use ldmib (SA110 bug),
11257                      then try to pop r3 instead.  */
11258                   if (stack_adjust)
11259                     live_regs_mask |= 1 << 3;
11260                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
11261                 }
11262             }
11263           else
11264             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
11265
11266           p = instr + strlen (instr);
11267
11268           for (reg = 0; reg <= SP_REGNUM; reg++)
11269             if (live_regs_mask & (1 << reg))
11270               {
11271                 int l = strlen (reg_names[reg]);
11272
11273                 if (first)
11274                   first = 0;
11275                 else
11276                   {
11277                     memcpy (p, ", ", 2);
11278                     p += 2;
11279                   }
11280
11281                 memcpy (p, "%|", 2);
11282                 memcpy (p + 2, reg_names[reg], l);
11283                 p += l + 2;
11284               }
11285
11286           if (live_regs_mask & (1 << LR_REGNUM))
11287             {
11288               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
11289               /* If returning from an interrupt, restore the CPSR.  */
11290               if (IS_INTERRUPT (func_type))
11291                 strcat (p, "^");
11292             }
11293           else
11294             strcpy (p, "}");
11295         }
11296
11297       output_asm_insn (instr, & operand);
11298
11299       /* See if we need to generate an extra instruction to
11300          perform the actual function return.  */
11301       if (really_return
11302           && func_type != ARM_FT_INTERWORKED
11303           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
11304         {
11305           /* The return has already been handled
11306              by loading the LR into the PC.  */
11307           really_return = 0;
11308         }
11309     }
11310
11311   if (really_return)
11312     {
11313       switch ((int) ARM_FUNC_TYPE (func_type))
11314         {
11315         case ARM_FT_ISR:
11316         case ARM_FT_FIQ:
11317           /* ??? This is wrong for unified assembly syntax.  */
11318           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
11319           break;
11320
11321         case ARM_FT_INTERWORKED:
11322           sprintf (instr, "bx%s\t%%|lr", conditional);
11323           break;
11324
11325         case ARM_FT_EXCEPTION:
11326           /* ??? This is wrong for unified assembly syntax.  */
11327           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
11328           break;
11329
11330         default:
11331           /* Use bx if it's available.  */
11332           if (arm_arch5 || arm_arch4t)
11333             sprintf (instr, "bx%s\t%%|lr", conditional);
11334           else
11335             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
11336           break;
11337         }
11338
11339       output_asm_insn (instr, & operand);
11340     }
11341
11342   return "";
11343 }
11344
11345 /* Write the function name into the code section, directly preceding
11346    the function prologue.
11347
11348    Code will be output similar to this:
11349      t0
11350          .ascii "arm_poke_function_name", 0
11351          .align
11352      t1
11353          .word 0xff000000 + (t1 - t0)
11354      arm_poke_function_name
11355          mov     ip, sp
11356          stmfd   sp!, {fp, ip, lr, pc}
11357          sub     fp, ip, #4
11358
11359    When performing a stack backtrace, code can inspect the value
11360    of 'pc' stored at 'fp' + 0.  If the trace function then looks
11361    at location pc - 12 and the top 8 bits are set, then we know
11362    that there is a function name embedded immediately preceding this
11363    location and has length ((pc[-3]) & 0xff000000).
11364
11365    We assume that pc is declared as a pointer to an unsigned long.
11366
11367    It is of no benefit to output the function name if we are assembling
11368    a leaf function.  These function types will not contain a stack
11369    backtrace structure, therefore it is not possible to determine the
11370    function name.  */
11371 void
11372 arm_poke_function_name (FILE *stream, const char *name)
11373 {
11374   unsigned long alignlength;
11375   unsigned long length;
11376   rtx           x;
11377
11378   length      = strlen (name) + 1;
11379   alignlength = ROUND_UP_WORD (length);
11380
11381   ASM_OUTPUT_ASCII (stream, name, length);
11382   ASM_OUTPUT_ALIGN (stream, 2);
11383   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
11384   assemble_aligned_integer (UNITS_PER_WORD, x);
11385 }
11386
11387 /* Place some comments into the assembler stream
11388    describing the current function.  */
11389 static void
11390 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
11391 {
11392   unsigned long func_type;
11393
11394   if (TARGET_THUMB1)
11395     {
11396       thumb1_output_function_prologue (f, frame_size);
11397       return;
11398     }
11399
11400   /* Sanity check.  */
11401   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
11402
11403   func_type = arm_current_func_type ();
11404
11405   switch ((int) ARM_FUNC_TYPE (func_type))
11406     {
11407     default:
11408     case ARM_FT_NORMAL:
11409       break;
11410     case ARM_FT_INTERWORKED:
11411       asm_fprintf (f, "\t%@ Function supports interworking.\n");
11412       break;
11413     case ARM_FT_ISR:
11414       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
11415       break;
11416     case ARM_FT_FIQ:
11417       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
11418       break;
11419     case ARM_FT_EXCEPTION:
11420       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
11421       break;
11422     }
11423
11424   if (IS_NAKED (func_type))
11425     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
11426
11427   if (IS_VOLATILE (func_type))
11428     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
11429
11430   if (IS_NESTED (func_type))
11431     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
11432   if (IS_STACKALIGN (func_type))
11433     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
11434
11435   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
11436                crtl->args.size,
11437                crtl->args.pretend_args_size, frame_size);
11438
11439   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
11440                frame_pointer_needed,
11441                cfun->machine->uses_anonymous_args);
11442
11443   if (cfun->machine->lr_save_eliminated)
11444     asm_fprintf (f, "\t%@ link register save eliminated.\n");
11445
11446   if (crtl->calls_eh_return)
11447     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
11448
11449   return_used_this_function = 0;
11450 }
11451
11452 const char *
11453 arm_output_epilogue (rtx sibling)
11454 {
11455   int reg;
11456   unsigned long saved_regs_mask;
11457   unsigned long func_type;
11458   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
11459      frame that is $fp + 4 for a non-variadic function.  */
11460   int floats_offset = 0;
11461   rtx operands[3];
11462   FILE * f = asm_out_file;
11463   unsigned int lrm_count = 0;
11464   int really_return = (sibling == NULL);
11465   int start_reg;
11466   arm_stack_offsets *offsets;
11467
11468   /* If we have already generated the return instruction
11469      then it is futile to generate anything else.  */
11470   if (use_return_insn (FALSE, sibling) && return_used_this_function)
11471     return "";
11472
11473   func_type = arm_current_func_type ();
11474
11475   if (IS_NAKED (func_type))
11476     /* Naked functions don't have epilogues.  */
11477     return "";
11478
11479   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11480     {
11481       rtx op;
11482
11483       /* A volatile function should never return.  Call abort.  */
11484       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
11485       assemble_external_libcall (op);
11486       output_asm_insn ("bl\t%a0", &op);
11487
11488       return "";
11489     }
11490
11491   /* If we are throwing an exception, then we really must be doing a
11492      return, so we can't tail-call.  */
11493   gcc_assert (!crtl->calls_eh_return || really_return);
11494
11495   offsets = arm_get_frame_offsets ();
11496   saved_regs_mask = offsets->saved_regs_mask;
11497
11498   if (TARGET_IWMMXT)
11499     lrm_count = bit_count (saved_regs_mask);
11500
11501   floats_offset = offsets->saved_args;
11502   /* Compute how far away the floats will be.  */
11503   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11504     if (saved_regs_mask & (1 << reg))
11505       floats_offset += 4;
11506
11507   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
11508     {
11509       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
11510       int vfp_offset = offsets->frame;
11511
11512       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11513         {
11514           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11515             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11516               {
11517                 floats_offset += 12;
11518                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
11519                              reg, FP_REGNUM, floats_offset - vfp_offset);
11520               }
11521         }
11522       else
11523         {
11524           start_reg = LAST_FPA_REGNUM;
11525
11526           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11527             {
11528               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11529                 {
11530                   floats_offset += 12;
11531
11532                   /* We can't unstack more than four registers at once.  */
11533                   if (start_reg - reg == 3)
11534                     {
11535                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
11536                                    reg, FP_REGNUM, floats_offset - vfp_offset);
11537                       start_reg = reg - 1;
11538                     }
11539                 }
11540               else
11541                 {
11542                   if (reg != start_reg)
11543                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11544                                  reg + 1, start_reg - reg,
11545                                  FP_REGNUM, floats_offset - vfp_offset);
11546                   start_reg = reg - 1;
11547                 }
11548             }
11549
11550           /* Just in case the last register checked also needs unstacking.  */
11551           if (reg != start_reg)
11552             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11553                          reg + 1, start_reg - reg,
11554                          FP_REGNUM, floats_offset - vfp_offset);
11555         }
11556
11557       if (TARGET_HARD_FLOAT && TARGET_VFP)
11558         {
11559           int saved_size;
11560
11561           /* The fldmd insns do not have base+offset addressing
11562              modes, so we use IP to hold the address.  */
11563           saved_size = arm_get_vfp_saved_size ();
11564
11565           if (saved_size > 0)
11566             {
11567               floats_offset += saved_size;
11568               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
11569                            FP_REGNUM, floats_offset - vfp_offset);
11570             }
11571           start_reg = FIRST_VFP_REGNUM;
11572           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11573             {
11574               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11575                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
11576                 {
11577                   if (start_reg != reg)
11578                     vfp_output_fldmd (f, IP_REGNUM,
11579                                       (start_reg - FIRST_VFP_REGNUM) / 2,
11580                                       (reg - start_reg) / 2);
11581                   start_reg = reg + 2;
11582                 }
11583             }
11584           if (start_reg != reg)
11585             vfp_output_fldmd (f, IP_REGNUM,
11586                               (start_reg - FIRST_VFP_REGNUM) / 2,
11587                               (reg - start_reg) / 2);
11588         }
11589
11590       if (TARGET_IWMMXT)
11591         {
11592           /* The frame pointer is guaranteed to be non-double-word aligned.
11593              This is because it is set to (old_stack_pointer - 4) and the
11594              old_stack_pointer was double word aligned.  Thus the offset to
11595              the iWMMXt registers to be loaded must also be non-double-word
11596              sized, so that the resultant address *is* double-word aligned.
11597              We can ignore floats_offset since that was already included in
11598              the live_regs_mask.  */
11599           lrm_count += (lrm_count % 2 ? 2 : 1);
11600
11601           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
11602             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11603               {
11604                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
11605                              reg, FP_REGNUM, lrm_count * 4);
11606                 lrm_count += 2;
11607               }
11608         }
11609
11610       /* saved_regs_mask should contain the IP, which at the time of stack
11611          frame generation actually contains the old stack pointer.  So a
11612          quick way to unwind the stack is just pop the IP register directly
11613          into the stack pointer.  */
11614       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
11615       saved_regs_mask &= ~ (1 << IP_REGNUM);
11616       saved_regs_mask |=   (1 << SP_REGNUM);
11617
11618       /* There are two registers left in saved_regs_mask - LR and PC.  We
11619          only need to restore the LR register (the return address), but to
11620          save time we can load it directly into the PC, unless we need a
11621          special function exit sequence, or we are not really returning.  */
11622       if (really_return
11623           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
11624           && !crtl->calls_eh_return)
11625         /* Delete the LR from the register mask, so that the LR on
11626            the stack is loaded into the PC in the register mask.  */
11627         saved_regs_mask &= ~ (1 << LR_REGNUM);
11628       else
11629         saved_regs_mask &= ~ (1 << PC_REGNUM);
11630
11631       /* We must use SP as the base register, because SP is one of the
11632          registers being restored.  If an interrupt or page fault
11633          happens in the ldm instruction, the SP might or might not
11634          have been restored.  That would be bad, as then SP will no
11635          longer indicate the safe area of stack, and we can get stack
11636          corruption.  Using SP as the base register means that it will
11637          be reset correctly to the original value, should an interrupt
11638          occur.  If the stack pointer already points at the right
11639          place, then omit the subtraction.  */
11640       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
11641           || cfun->calls_alloca)
11642         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
11643                      4 * bit_count (saved_regs_mask));
11644       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
11645
11646       if (IS_INTERRUPT (func_type))
11647         /* Interrupt handlers will have pushed the
11648            IP onto the stack, so restore it now.  */
11649         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
11650     }
11651   else
11652     {
11653       /* This branch is executed for ARM mode (non-apcs frames) and
11654          Thumb-2 mode. Frame layout is essentially the same for those
11655          cases, except that in ARM mode frame pointer points to the
11656          first saved register, while in Thumb-2 mode the frame pointer points
11657          to the last saved register.
11658
11659          It is possible to make frame pointer point to last saved
11660          register in both cases, and remove some conditionals below.
11661          That means that fp setup in prologue would be just "mov fp, sp"
11662          and sp restore in epilogue would be just "mov sp, fp", whereas
11663          now we have to use add/sub in those cases. However, the value
11664          of that would be marginal, as both mov and add/sub are 32-bit
11665          in ARM mode, and it would require extra conditionals
11666          in arm_expand_prologue to distingish ARM-apcs-frame case
11667          (where frame pointer is required to point at first register)
11668          and ARM-non-apcs-frame. Therefore, such change is postponed
11669          until real need arise.  */
11670       unsigned HOST_WIDE_INT amount;
11671       int rfe;
11672       /* Restore stack pointer if necessary.  */
11673       if (TARGET_ARM && frame_pointer_needed)
11674         {
11675           operands[0] = stack_pointer_rtx;
11676           operands[1] = hard_frame_pointer_rtx;
11677           
11678           operands[2] = GEN_INT (offsets->frame - offsets->saved_regs);
11679           output_add_immediate (operands);
11680         }
11681       else
11682         {
11683           if (frame_pointer_needed)
11684             {
11685               /* For Thumb-2 restore sp from the frame pointer.
11686                  Operand restrictions mean we have to incrememnt FP, then copy
11687                  to SP.  */
11688               amount = offsets->locals_base - offsets->saved_regs;
11689               operands[0] = hard_frame_pointer_rtx;
11690             }
11691           else
11692             {
11693               unsigned long count;
11694               operands[0] = stack_pointer_rtx;
11695               amount = offsets->outgoing_args - offsets->saved_regs;
11696               /* pop call clobbered registers if it avoids a
11697                  separate stack adjustment.  */
11698               count = offsets->saved_regs - offsets->saved_args;
11699               if (optimize_size
11700                   && count != 0
11701                   && !crtl->calls_eh_return
11702                   && bit_count(saved_regs_mask) * 4 == count
11703                   && !IS_INTERRUPT (func_type)
11704                   && !crtl->tail_call_emit)
11705                 {
11706                   unsigned long mask;
11707                   mask = (1 << (arm_size_return_regs() / 4)) - 1;
11708                   mask ^= 0xf;
11709                   mask &= ~saved_regs_mask;
11710                   reg = 0;
11711                   while (bit_count (mask) * 4 > amount)
11712                     {
11713                       while ((mask & (1 << reg)) == 0)
11714                         reg++;
11715                       mask &= ~(1 << reg);
11716                     }
11717                   if (bit_count (mask) * 4 == amount) {
11718                       amount = 0;
11719                       saved_regs_mask |= mask;
11720                   }
11721                 }
11722             }
11723           
11724           if (amount)
11725             {
11726               operands[1] = operands[0];
11727               operands[2] = GEN_INT (amount);
11728               output_add_immediate (operands);
11729             }
11730           if (frame_pointer_needed)
11731             asm_fprintf (f, "\tmov\t%r, %r\n",
11732                          SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
11733         }
11734
11735       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11736         {
11737           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
11738             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11739               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
11740                            reg, SP_REGNUM);
11741         }
11742       else
11743         {
11744           start_reg = FIRST_FPA_REGNUM;
11745
11746           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
11747             {
11748               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11749                 {
11750                   if (reg - start_reg == 3)
11751                     {
11752                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
11753                                    start_reg, SP_REGNUM);
11754                       start_reg = reg + 1;
11755                     }
11756                 }
11757               else
11758                 {
11759                   if (reg != start_reg)
11760                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
11761                                  start_reg, reg - start_reg,
11762                                  SP_REGNUM);
11763
11764                   start_reg = reg + 1;
11765                 }
11766             }
11767
11768           /* Just in case the last register checked also needs unstacking.  */
11769           if (reg != start_reg)
11770             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
11771                          start_reg, reg - start_reg, SP_REGNUM);
11772         }
11773
11774       if (TARGET_HARD_FLOAT && TARGET_VFP)
11775         {
11776           start_reg = FIRST_VFP_REGNUM;
11777           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11778             {
11779               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11780                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
11781                 {
11782                   if (start_reg != reg)
11783                     vfp_output_fldmd (f, SP_REGNUM,
11784                                       (start_reg - FIRST_VFP_REGNUM) / 2,
11785                                       (reg - start_reg) / 2);
11786                   start_reg = reg + 2;
11787                 }
11788             }
11789           if (start_reg != reg)
11790             vfp_output_fldmd (f, SP_REGNUM,
11791                               (start_reg - FIRST_VFP_REGNUM) / 2,
11792                               (reg - start_reg) / 2);
11793         }
11794       if (TARGET_IWMMXT)
11795         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
11796           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11797             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
11798
11799       /* If we can, restore the LR into the PC.  */
11800       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
11801           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
11802           && !IS_STACKALIGN (func_type)
11803           && really_return
11804           && crtl->args.pretend_args_size == 0
11805           && saved_regs_mask & (1 << LR_REGNUM)
11806           && !crtl->calls_eh_return)
11807         {
11808           saved_regs_mask &= ~ (1 << LR_REGNUM);
11809           saved_regs_mask |=   (1 << PC_REGNUM);
11810           rfe = IS_INTERRUPT (func_type);
11811         }
11812       else
11813         rfe = 0;
11814
11815       /* Load the registers off the stack.  If we only have one register
11816          to load use the LDR instruction - it is faster.  For Thumb-2
11817          always use pop and the assembler will pick the best instruction.*/
11818       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
11819           && !IS_INTERRUPT(func_type))
11820         {
11821           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
11822         }
11823       else if (saved_regs_mask)
11824         {
11825           if (saved_regs_mask & (1 << SP_REGNUM))
11826             /* Note - write back to the stack register is not enabled
11827                (i.e. "ldmfd sp!...").  We know that the stack pointer is
11828                in the list of registers and if we add writeback the
11829                instruction becomes UNPREDICTABLE.  */
11830             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
11831                              rfe);
11832           else if (TARGET_ARM)
11833             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
11834                              rfe);
11835           else
11836             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
11837         }
11838
11839       if (crtl->args.pretend_args_size)
11840         {
11841           /* Unwind the pre-pushed regs.  */
11842           operands[0] = operands[1] = stack_pointer_rtx;
11843           operands[2] = GEN_INT (crtl->args.pretend_args_size);
11844           output_add_immediate (operands);
11845         }
11846     }
11847
11848   /* We may have already restored PC directly from the stack.  */
11849   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
11850     return "";
11851
11852   /* Stack adjustment for exception handler.  */
11853   if (crtl->calls_eh_return)
11854     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
11855                  ARM_EH_STACKADJ_REGNUM);
11856
11857   /* Generate the return instruction.  */
11858   switch ((int) ARM_FUNC_TYPE (func_type))
11859     {
11860     case ARM_FT_ISR:
11861     case ARM_FT_FIQ:
11862       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
11863       break;
11864
11865     case ARM_FT_EXCEPTION:
11866       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
11867       break;
11868
11869     case ARM_FT_INTERWORKED:
11870       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
11871       break;
11872
11873     default:
11874       if (IS_STACKALIGN (func_type))
11875         {
11876           /* See comment in arm_expand_prologue.  */
11877           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
11878         }
11879       if (arm_arch5 || arm_arch4t)
11880         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
11881       else
11882         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
11883       break;
11884     }
11885
11886   return "";
11887 }
11888
11889 static void
11890 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11891                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
11892 {
11893   arm_stack_offsets *offsets;
11894
11895   if (TARGET_THUMB1)
11896     {
11897       int regno;
11898
11899       /* Emit any call-via-reg trampolines that are needed for v4t support
11900          of call_reg and call_value_reg type insns.  */
11901       for (regno = 0; regno < LR_REGNUM; regno++)
11902         {
11903           rtx label = cfun->machine->call_via[regno];
11904
11905           if (label != NULL)
11906             {
11907               switch_to_section (function_section (current_function_decl));
11908               targetm.asm_out.internal_label (asm_out_file, "L",
11909                                               CODE_LABEL_NUMBER (label));
11910               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
11911             }
11912         }
11913
11914       /* ??? Probably not safe to set this here, since it assumes that a
11915          function will be emitted as assembly immediately after we generate
11916          RTL for it.  This does not happen for inline functions.  */
11917       return_used_this_function = 0;
11918     }
11919   else /* TARGET_32BIT */
11920     {
11921       /* We need to take into account any stack-frame rounding.  */
11922       offsets = arm_get_frame_offsets ();
11923
11924       gcc_assert (!use_return_insn (FALSE, NULL)
11925                   || !return_used_this_function
11926                   || offsets->saved_regs == offsets->outgoing_args
11927                   || frame_pointer_needed);
11928
11929       /* Reset the ARM-specific per-function variables.  */
11930       after_arm_reorg = 0;
11931     }
11932 }
11933
11934 /* Generate and emit an insn that we will recognize as a push_multi.
11935    Unfortunately, since this insn does not reflect very well the actual
11936    semantics of the operation, we need to annotate the insn for the benefit
11937    of DWARF2 frame unwind information.  */
11938 static rtx
11939 emit_multi_reg_push (unsigned long mask)
11940 {
11941   int num_regs = 0;
11942   int num_dwarf_regs;
11943   int i, j;
11944   rtx par;
11945   rtx dwarf;
11946   int dwarf_par_index;
11947   rtx tmp, reg;
11948
11949   for (i = 0; i <= LAST_ARM_REGNUM; i++)
11950     if (mask & (1 << i))
11951       num_regs++;
11952
11953   gcc_assert (num_regs && num_regs <= 16);
11954
11955   /* We don't record the PC in the dwarf frame information.  */
11956   num_dwarf_regs = num_regs;
11957   if (mask & (1 << PC_REGNUM))
11958     num_dwarf_regs--;
11959
11960   /* For the body of the insn we are going to generate an UNSPEC in
11961      parallel with several USEs.  This allows the insn to be recognized
11962      by the push_multi pattern in the arm.md file.  The insn looks
11963      something like this:
11964
11965        (parallel [
11966            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
11967                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
11968            (use (reg:SI 11 fp))
11969            (use (reg:SI 12 ip))
11970            (use (reg:SI 14 lr))
11971            (use (reg:SI 15 pc))
11972         ])
11973
11974      For the frame note however, we try to be more explicit and actually
11975      show each register being stored into the stack frame, plus a (single)
11976      decrement of the stack pointer.  We do it this way in order to be
11977      friendly to the stack unwinding code, which only wants to see a single
11978      stack decrement per instruction.  The RTL we generate for the note looks
11979      something like this:
11980
11981       (sequence [
11982            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
11983            (set (mem:SI (reg:SI sp)) (reg:SI r4))
11984            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
11985            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
11986            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
11987         ])
11988
11989       This sequence is used both by the code to support stack unwinding for
11990       exceptions handlers and the code to generate dwarf2 frame debugging.  */
11991
11992   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
11993   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
11994   dwarf_par_index = 1;
11995
11996   for (i = 0; i <= LAST_ARM_REGNUM; i++)
11997     {
11998       if (mask & (1 << i))
11999         {
12000           reg = gen_rtx_REG (SImode, i);
12001
12002           XVECEXP (par, 0, 0)
12003             = gen_rtx_SET (VOIDmode,
12004                            gen_frame_mem (BLKmode,
12005                                           gen_rtx_PRE_DEC (BLKmode,
12006                                                            stack_pointer_rtx)),
12007                            gen_rtx_UNSPEC (BLKmode,
12008                                            gen_rtvec (1, reg),
12009                                            UNSPEC_PUSH_MULT));
12010
12011           if (i != PC_REGNUM)
12012             {
12013               tmp = gen_rtx_SET (VOIDmode,
12014                                  gen_frame_mem (SImode, stack_pointer_rtx),
12015                                  reg);
12016               RTX_FRAME_RELATED_P (tmp) = 1;
12017               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
12018               dwarf_par_index++;
12019             }
12020
12021           break;
12022         }
12023     }
12024
12025   for (j = 1, i++; j < num_regs; i++)
12026     {
12027       if (mask & (1 << i))
12028         {
12029           reg = gen_rtx_REG (SImode, i);
12030
12031           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
12032
12033           if (i != PC_REGNUM)
12034             {
12035               tmp
12036                 = gen_rtx_SET (VOIDmode,
12037                                gen_frame_mem (SImode,
12038                                               plus_constant (stack_pointer_rtx,
12039                                                              4 * j)),
12040                                reg);
12041               RTX_FRAME_RELATED_P (tmp) = 1;
12042               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
12043             }
12044
12045           j++;
12046         }
12047     }
12048
12049   par = emit_insn (par);
12050
12051   tmp = gen_rtx_SET (VOIDmode,
12052                      stack_pointer_rtx,
12053                      plus_constant (stack_pointer_rtx, -4 * num_regs));
12054   RTX_FRAME_RELATED_P (tmp) = 1;
12055   XVECEXP (dwarf, 0, 0) = tmp;
12056
12057   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12058                                        REG_NOTES (par));
12059   return par;
12060 }
12061
12062 /* Calculate the size of the return value that is passed in registers.  */
12063 static unsigned
12064 arm_size_return_regs (void)
12065 {
12066   enum machine_mode mode;
12067
12068   if (crtl->return_rtx != 0)
12069     mode = GET_MODE (crtl->return_rtx);
12070   else
12071     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12072
12073   return GET_MODE_SIZE (mode);
12074 }
12075
12076 static rtx
12077 emit_sfm (int base_reg, int count)
12078 {
12079   rtx par;
12080   rtx dwarf;
12081   rtx tmp, reg;
12082   int i;
12083
12084   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
12085   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
12086
12087   reg = gen_rtx_REG (XFmode, base_reg++);
12088
12089   XVECEXP (par, 0, 0)
12090     = gen_rtx_SET (VOIDmode,
12091                    gen_frame_mem (BLKmode,
12092                                   gen_rtx_PRE_DEC (BLKmode,
12093                                                    stack_pointer_rtx)),
12094                    gen_rtx_UNSPEC (BLKmode,
12095                                    gen_rtvec (1, reg),
12096                                    UNSPEC_PUSH_MULT));
12097   tmp = gen_rtx_SET (VOIDmode,
12098                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
12099   RTX_FRAME_RELATED_P (tmp) = 1;
12100   XVECEXP (dwarf, 0, 1) = tmp;
12101
12102   for (i = 1; i < count; i++)
12103     {
12104       reg = gen_rtx_REG (XFmode, base_reg++);
12105       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
12106
12107       tmp = gen_rtx_SET (VOIDmode,
12108                          gen_frame_mem (XFmode,
12109                                         plus_constant (stack_pointer_rtx,
12110                                                        i * 12)),
12111                          reg);
12112       RTX_FRAME_RELATED_P (tmp) = 1;
12113       XVECEXP (dwarf, 0, i + 1) = tmp;
12114     }
12115
12116   tmp = gen_rtx_SET (VOIDmode,
12117                      stack_pointer_rtx,
12118                      plus_constant (stack_pointer_rtx, -12 * count));
12119
12120   RTX_FRAME_RELATED_P (tmp) = 1;
12121   XVECEXP (dwarf, 0, 0) = tmp;
12122
12123   par = emit_insn (par);
12124   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12125                                        REG_NOTES (par));
12126   return par;
12127 }
12128
12129
12130 /* Return true if the current function needs to save/restore LR.  */
12131
12132 static bool
12133 thumb_force_lr_save (void)
12134 {
12135   return !cfun->machine->lr_save_eliminated
12136          && (!leaf_function_p ()
12137              || thumb_far_jump_used_p ()
12138              || df_regs_ever_live_p (LR_REGNUM));
12139 }
12140
12141
12142 /* Compute the distance from register FROM to register TO.
12143    These can be the arg pointer (26), the soft frame pointer (25),
12144    the stack pointer (13) or the hard frame pointer (11).
12145    In thumb mode r7 is used as the soft frame pointer, if needed.
12146    Typical stack layout looks like this:
12147
12148        old stack pointer -> |    |
12149                              ----
12150                             |    | \
12151                             |    |   saved arguments for
12152                             |    |   vararg functions
12153                             |    | /
12154                               --
12155    hard FP & arg pointer -> |    | \
12156                             |    |   stack
12157                             |    |   frame
12158                             |    | /
12159                               --
12160                             |    | \
12161                             |    |   call saved
12162                             |    |   registers
12163       soft frame pointer -> |    | /
12164                               --
12165                             |    | \
12166                             |    |   local
12167                             |    |   variables
12168      locals base pointer -> |    | /
12169                               --
12170                             |    | \
12171                             |    |   outgoing
12172                             |    |   arguments
12173    current stack pointer -> |    | /
12174                               --
12175
12176   For a given function some or all of these stack components
12177   may not be needed, giving rise to the possibility of
12178   eliminating some of the registers.
12179
12180   The values returned by this function must reflect the behavior
12181   of arm_expand_prologue() and arm_compute_save_reg_mask().
12182
12183   The sign of the number returned reflects the direction of stack
12184   growth, so the values are positive for all eliminations except
12185   from the soft frame pointer to the hard frame pointer.
12186
12187   SFP may point just inside the local variables block to ensure correct
12188   alignment.  */
12189
12190
12191 /* Calculate stack offsets.  These are used to calculate register elimination
12192    offsets and in prologue/epilogue code.  Also calculates which registers
12193    should be saved.  */
12194
12195 static arm_stack_offsets *
12196 arm_get_frame_offsets (void)
12197 {
12198   struct arm_stack_offsets *offsets;
12199   unsigned long func_type;
12200   int leaf;
12201   int saved;
12202   int core_saved;
12203   HOST_WIDE_INT frame_size;
12204   int i;
12205
12206   offsets = &cfun->machine->stack_offsets;
12207
12208   /* We need to know if we are a leaf function.  Unfortunately, it
12209      is possible to be called after start_sequence has been called,
12210      which causes get_insns to return the insns for the sequence,
12211      not the function, which will cause leaf_function_p to return
12212      the incorrect result.
12213
12214      to know about leaf functions once reload has completed, and the
12215      frame size cannot be changed after that time, so we can safely
12216      use the cached value.  */
12217
12218   if (reload_completed)
12219     return offsets;
12220
12221   /* Initially this is the size of the local variables.  It will translated
12222      into an offset once we have determined the size of preceding data.  */
12223   frame_size = ROUND_UP_WORD (get_frame_size ());
12224
12225   leaf = leaf_function_p ();
12226
12227   /* Space for variadic functions.  */
12228   offsets->saved_args = crtl->args.pretend_args_size;
12229
12230   /* In Thumb mode this is incorrect, but never used.  */
12231   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0) +
12232                    arm_compute_static_chain_stack_bytes();
12233
12234   if (TARGET_32BIT)
12235     {
12236       unsigned int regno;
12237
12238       offsets->saved_regs_mask = arm_compute_save_reg_mask ();
12239       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12240       saved = core_saved;
12241
12242       /* We know that SP will be doubleword aligned on entry, and we must
12243          preserve that condition at any subroutine call.  We also require the
12244          soft frame pointer to be doubleword aligned.  */
12245
12246       if (TARGET_REALLY_IWMMXT)
12247         {
12248           /* Check for the call-saved iWMMXt registers.  */
12249           for (regno = FIRST_IWMMXT_REGNUM;
12250                regno <= LAST_IWMMXT_REGNUM;
12251                regno++)
12252             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12253               saved += 8;
12254         }
12255
12256       func_type = arm_current_func_type ();
12257       if (! IS_VOLATILE (func_type))
12258         {
12259           /* Space for saved FPA registers.  */
12260           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
12261             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12262             saved += 12;
12263
12264           /* Space for saved VFP registers.  */
12265           if (TARGET_HARD_FLOAT && TARGET_VFP)
12266             saved += arm_get_vfp_saved_size ();
12267         }
12268     }
12269   else /* TARGET_THUMB1 */
12270     {
12271       offsets->saved_regs_mask = thumb1_compute_save_reg_mask ();
12272       core_saved = bit_count (offsets->saved_regs_mask) * 4;
12273       saved = core_saved;
12274       if (TARGET_BACKTRACE)
12275         saved += 16;
12276     }
12277
12278   /* Saved registers include the stack frame.  */
12279   offsets->saved_regs = offsets->saved_args + saved +
12280                         arm_compute_static_chain_stack_bytes();
12281   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
12282   /* A leaf function does not need any stack alignment if it has nothing
12283      on the stack.  */
12284   if (leaf && frame_size == 0)
12285     {
12286       offsets->outgoing_args = offsets->soft_frame;
12287       offsets->locals_base = offsets->soft_frame;
12288       return offsets;
12289     }
12290
12291   /* Ensure SFP has the correct alignment.  */
12292   if (ARM_DOUBLEWORD_ALIGN
12293       && (offsets->soft_frame & 7))
12294     {
12295       offsets->soft_frame += 4;
12296       /* Try to align stack by pushing an extra reg.  Don't bother doing this
12297          when there is a stack frame as the alignment will be rolled into
12298          the normal stack adjustment.  */
12299       if (frame_size + crtl->outgoing_args_size == 0)
12300         {
12301           int reg = -1;
12302
12303           for (i = 4; i <= (TARGET_THUMB1 ? LAST_LO_REGNUM : 11); i++)
12304             {
12305               if ((offsets->saved_regs_mask & (1 << i)) == 0)
12306                 {
12307                   reg = i;
12308                   break;
12309                 }
12310             }
12311
12312           if (reg == -1 && arm_size_return_regs () <= 12
12313               && !crtl->tail_call_emit)
12314             {
12315               /* Push/pop an argument register (r3) if all callee saved
12316                  registers are already being pushed.  */
12317               reg = 3;
12318             }
12319
12320           if (reg != -1)
12321             {
12322               offsets->saved_regs += 4;
12323               offsets->saved_regs_mask |= (1 << reg);
12324             }
12325         }
12326     }
12327
12328   offsets->locals_base = offsets->soft_frame + frame_size;
12329   offsets->outgoing_args = (offsets->locals_base
12330                             + crtl->outgoing_args_size);
12331
12332   if (ARM_DOUBLEWORD_ALIGN)
12333     {
12334       /* Ensure SP remains doubleword aligned.  */
12335       if (offsets->outgoing_args & 7)
12336         offsets->outgoing_args += 4;
12337       gcc_assert (!(offsets->outgoing_args & 7));
12338     }
12339
12340   return offsets;
12341 }
12342
12343
12344 /* Calculate the relative offsets for the different stack pointers.  Positive
12345    offsets are in the direction of stack growth.  */
12346
12347 HOST_WIDE_INT
12348 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12349 {
12350   arm_stack_offsets *offsets;
12351
12352   offsets = arm_get_frame_offsets ();
12353
12354   /* OK, now we have enough information to compute the distances.
12355      There must be an entry in these switch tables for each pair
12356      of registers in ELIMINABLE_REGS, even if some of the entries
12357      seem to be redundant or useless.  */
12358   switch (from)
12359     {
12360     case ARG_POINTER_REGNUM:
12361       switch (to)
12362         {
12363         case THUMB_HARD_FRAME_POINTER_REGNUM:
12364           return 0;
12365
12366         case FRAME_POINTER_REGNUM:
12367           /* This is the reverse of the soft frame pointer
12368              to hard frame pointer elimination below.  */
12369           return offsets->soft_frame - offsets->saved_args;
12370
12371         case ARM_HARD_FRAME_POINTER_REGNUM:
12372           /* This is only non-zero in the case where the static chain register
12373              is stored above the frame.  */
12374           return offsets->frame - offsets->saved_args - 4;
12375
12376         case STACK_POINTER_REGNUM:
12377           /* If nothing has been pushed on the stack at all
12378              then this will return -4.  This *is* correct!  */
12379           return offsets->outgoing_args - (offsets->saved_args + 4);
12380
12381         default:
12382           gcc_unreachable ();
12383         }
12384       gcc_unreachable ();
12385
12386     case FRAME_POINTER_REGNUM:
12387       switch (to)
12388         {
12389         case THUMB_HARD_FRAME_POINTER_REGNUM:
12390           return 0;
12391
12392         case ARM_HARD_FRAME_POINTER_REGNUM:
12393           /* The hard frame pointer points to the top entry in the
12394              stack frame.  The soft frame pointer to the bottom entry
12395              in the stack frame.  If there is no stack frame at all,
12396              then they are identical.  */
12397
12398           return offsets->frame - offsets->soft_frame;
12399
12400         case STACK_POINTER_REGNUM:
12401           return offsets->outgoing_args - offsets->soft_frame;
12402
12403         default:
12404           gcc_unreachable ();
12405         }
12406       gcc_unreachable ();
12407
12408     default:
12409       /* You cannot eliminate from the stack pointer.
12410          In theory you could eliminate from the hard frame
12411          pointer to the stack pointer, but this will never
12412          happen, since if a stack frame is not needed the
12413          hard frame pointer will never be used.  */
12414       gcc_unreachable ();
12415     }
12416 }
12417
12418
12419 /* Emit RTL to save coprocessor registers on function entry.  Returns the
12420    number of bytes pushed.  */
12421
12422 static int
12423 arm_save_coproc_regs(void)
12424 {
12425   int saved_size = 0;
12426   unsigned reg;
12427   unsigned start_reg;
12428   rtx insn;
12429
12430   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
12431     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
12432       {
12433         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
12434         insn = gen_rtx_MEM (V2SImode, insn);
12435         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
12436         RTX_FRAME_RELATED_P (insn) = 1;
12437         saved_size += 8;
12438       }
12439
12440   /* Save any floating point call-saved registers used by this
12441      function.  */
12442   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12443     {
12444       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12445         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12446           {
12447             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
12448             insn = gen_rtx_MEM (XFmode, insn);
12449             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
12450             RTX_FRAME_RELATED_P (insn) = 1;
12451             saved_size += 12;
12452           }
12453     }
12454   else
12455     {
12456       start_reg = LAST_FPA_REGNUM;
12457
12458       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12459         {
12460           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12461             {
12462               if (start_reg - reg == 3)
12463                 {
12464                   insn = emit_sfm (reg, 4);
12465                   RTX_FRAME_RELATED_P (insn) = 1;
12466                   saved_size += 48;
12467                   start_reg = reg - 1;
12468                 }
12469             }
12470           else
12471             {
12472               if (start_reg != reg)
12473                 {
12474                   insn = emit_sfm (reg + 1, start_reg - reg);
12475                   RTX_FRAME_RELATED_P (insn) = 1;
12476                   saved_size += (start_reg - reg) * 12;
12477                 }
12478               start_reg = reg - 1;
12479             }
12480         }
12481
12482       if (start_reg != reg)
12483         {
12484           insn = emit_sfm (reg + 1, start_reg - reg);
12485           saved_size += (start_reg - reg) * 12;
12486           RTX_FRAME_RELATED_P (insn) = 1;
12487         }
12488     }
12489   if (TARGET_HARD_FLOAT && TARGET_VFP)
12490     {
12491       start_reg = FIRST_VFP_REGNUM;
12492
12493       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12494         {
12495           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12496               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12497             {
12498               if (start_reg != reg)
12499                 saved_size += vfp_emit_fstmd (start_reg,
12500                                               (reg - start_reg) / 2);
12501               start_reg = reg + 2;
12502             }
12503         }
12504       if (start_reg != reg)
12505         saved_size += vfp_emit_fstmd (start_reg,
12506                                       (reg - start_reg) / 2);
12507     }
12508   return saved_size;
12509 }
12510
12511
12512 /* Set the Thumb frame pointer from the stack pointer.  */
12513
12514 static void
12515 thumb_set_frame_pointer (arm_stack_offsets *offsets)
12516 {
12517   HOST_WIDE_INT amount;
12518   rtx insn, dwarf;
12519
12520   amount = offsets->outgoing_args - offsets->locals_base;
12521   if (amount < 1024)
12522     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12523                                   stack_pointer_rtx, GEN_INT (amount)));
12524   else
12525     {
12526       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
12527       /* Thumb-2 RTL patterns expect sp as the first input.  Thumb-1
12528          expects the first two operands to be the same.  */
12529       if (TARGET_THUMB2)
12530         {
12531           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12532                                         stack_pointer_rtx,
12533                                         hard_frame_pointer_rtx));
12534         }
12535       else
12536         {
12537           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12538                                         hard_frame_pointer_rtx,
12539                                         stack_pointer_rtx));
12540         }
12541       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
12542                            plus_constant (stack_pointer_rtx, amount));
12543       RTX_FRAME_RELATED_P (dwarf) = 1;
12544       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12545                                             REG_NOTES (insn));
12546     }
12547
12548   RTX_FRAME_RELATED_P (insn) = 1;
12549 }
12550
12551 /* Generate the prologue instructions for entry into an ARM or Thumb-2
12552    function.  */
12553 void
12554 arm_expand_prologue (void)
12555 {
12556   rtx amount;
12557   rtx insn;
12558   rtx ip_rtx;
12559   unsigned long live_regs_mask;
12560   unsigned long func_type;
12561   int fp_offset = 0;
12562   int saved_pretend_args = 0;
12563   int saved_regs = 0;
12564   unsigned HOST_WIDE_INT args_to_push;
12565   arm_stack_offsets *offsets;
12566
12567   func_type = arm_current_func_type ();
12568
12569   /* Naked functions don't have prologues.  */
12570   if (IS_NAKED (func_type))
12571     return;
12572
12573   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
12574   args_to_push = crtl->args.pretend_args_size;
12575
12576   /* Compute which register we will have to save onto the stack.  */
12577   offsets = arm_get_frame_offsets ();
12578   live_regs_mask = offsets->saved_regs_mask;
12579
12580   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
12581
12582   if (IS_STACKALIGN (func_type))
12583     {
12584       rtx dwarf;
12585       rtx r0;
12586       rtx r1;
12587       /* Handle a word-aligned stack pointer.  We generate the following:
12588
12589           mov r0, sp
12590           bic r1, r0, #7
12591           mov sp, r1
12592           <save and restore r0 in normal prologue/epilogue>
12593           mov sp, r0
12594           bx lr
12595
12596          The unwinder doesn't need to know about the stack realignment.
12597          Just tell it we saved SP in r0.  */
12598       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
12599
12600       r0 = gen_rtx_REG (SImode, 0);
12601       r1 = gen_rtx_REG (SImode, 1);
12602       /* Use a real rtvec rather than NULL_RTVEC so the rest of the
12603          compiler won't choke.  */
12604       dwarf = gen_rtx_UNSPEC (SImode, rtvec_alloc (0), UNSPEC_STACK_ALIGN);
12605       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
12606       insn = gen_movsi (r0, stack_pointer_rtx);
12607       RTX_FRAME_RELATED_P (insn) = 1;
12608       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12609                                             dwarf, REG_NOTES (insn));
12610       emit_insn (insn);
12611       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
12612       emit_insn (gen_movsi (stack_pointer_rtx, r1));
12613     }
12614
12615   /* For APCS frames, if IP register is clobbered
12616      when creating frame, save that register in a special
12617      way.  */
12618   if (TARGET_APCS_FRAME && frame_pointer_needed && TARGET_ARM)
12619     {
12620       if (IS_INTERRUPT (func_type))
12621         {
12622           /* Interrupt functions must not corrupt any registers.
12623              Creating a frame pointer however, corrupts the IP
12624              register, so we must push it first.  */
12625           insn = emit_multi_reg_push (1 << IP_REGNUM);
12626
12627           /* Do not set RTX_FRAME_RELATED_P on this insn.
12628              The dwarf stack unwinding code only wants to see one
12629              stack decrement per function, and this is not it.  If
12630              this instruction is labeled as being part of the frame
12631              creation sequence then dwarf2out_frame_debug_expr will
12632              die when it encounters the assignment of IP to FP
12633              later on, since the use of SP here establishes SP as
12634              the CFA register and not IP.
12635
12636              Anyway this instruction is not really part of the stack
12637              frame creation although it is part of the prologue.  */
12638         }
12639       else if (IS_NESTED (func_type))
12640         {
12641           /* The Static chain register is the same as the IP register
12642              used as a scratch register during stack frame creation.
12643              To get around this need to find somewhere to store IP
12644              whilst the frame is being created.  We try the following
12645              places in order:
12646
12647                1. The last argument register.
12648                2. A slot on the stack above the frame.  (This only
12649                   works if the function is not a varargs function).
12650                3. Register r3, after pushing the argument registers
12651                   onto the stack.
12652
12653              Note - we only need to tell the dwarf2 backend about the SP
12654              adjustment in the second variant; the static chain register
12655              doesn't need to be unwound, as it doesn't contain a value
12656              inherited from the caller.  */
12657
12658           if (df_regs_ever_live_p (3) == false)
12659             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
12660           else if (args_to_push == 0)
12661             {
12662               rtx dwarf;
12663
12664               gcc_assert(arm_compute_static_chain_stack_bytes() == 4);
12665               saved_regs += 4;
12666
12667               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
12668               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
12669               fp_offset = 4;
12670
12671               /* Just tell the dwarf backend that we adjusted SP.  */
12672               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12673                                    plus_constant (stack_pointer_rtx,
12674                                                   -fp_offset));
12675               RTX_FRAME_RELATED_P (insn) = 1;
12676               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12677                                                     dwarf, REG_NOTES (insn));
12678             }
12679           else
12680             {
12681               /* Store the args on the stack.  */
12682               if (cfun->machine->uses_anonymous_args)
12683                 insn = emit_multi_reg_push
12684                   ((0xf0 >> (args_to_push / 4)) & 0xf);
12685               else
12686                 insn = emit_insn
12687                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12688                                GEN_INT (- args_to_push)));
12689
12690               RTX_FRAME_RELATED_P (insn) = 1;
12691
12692               saved_pretend_args = 1;
12693               fp_offset = args_to_push;
12694               args_to_push = 0;
12695
12696               /* Now reuse r3 to preserve IP.  */
12697               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
12698             }
12699         }
12700
12701       insn = emit_set_insn (ip_rtx,
12702                             plus_constant (stack_pointer_rtx, fp_offset));
12703       RTX_FRAME_RELATED_P (insn) = 1;
12704     }
12705
12706   if (args_to_push)
12707     {
12708       /* Push the argument registers, or reserve space for them.  */
12709       if (cfun->machine->uses_anonymous_args)
12710         insn = emit_multi_reg_push
12711           ((0xf0 >> (args_to_push / 4)) & 0xf);
12712       else
12713         insn = emit_insn
12714           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12715                        GEN_INT (- args_to_push)));
12716       RTX_FRAME_RELATED_P (insn) = 1;
12717     }
12718
12719   /* If this is an interrupt service routine, and the link register
12720      is going to be pushed, and we're not generating extra
12721      push of IP (needed when frame is needed and frame layout if apcs),
12722      subtracting four from LR now will mean that the function return
12723      can be done with a single instruction.  */
12724   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
12725       && (live_regs_mask & (1 << LR_REGNUM)) != 0
12726       && !(frame_pointer_needed && TARGET_APCS_FRAME)
12727       && TARGET_ARM)
12728     {
12729       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
12730       
12731       emit_set_insn (lr, plus_constant (lr, -4));
12732     }
12733
12734   if (live_regs_mask)
12735     {
12736       saved_regs += bit_count (live_regs_mask) * 4;
12737       if (optimize_size && !frame_pointer_needed
12738           && saved_regs == offsets->saved_regs - offsets->saved_args)
12739         {
12740           /* If no coprocessor registers are being pushed and we don't have
12741              to worry about a frame pointer then push extra registers to
12742              create the stack frame.  This is done is a way that does not
12743              alter the frame layout, so is independent of the epilogue.  */
12744           int n;
12745           int frame;
12746           n = 0;
12747           while (n < 8 && (live_regs_mask & (1 << n)) == 0)
12748             n++;
12749           frame = offsets->outgoing_args - (offsets->saved_args + saved_regs);
12750           if (frame && n * 4 >= frame)
12751             {
12752               n = frame / 4;
12753               live_regs_mask |= (1 << n) - 1;
12754               saved_regs += frame;
12755             }
12756         }
12757       insn = emit_multi_reg_push (live_regs_mask);
12758       RTX_FRAME_RELATED_P (insn) = 1;
12759     }
12760
12761   if (! IS_VOLATILE (func_type))
12762     saved_regs += arm_save_coproc_regs ();
12763
12764   if (frame_pointer_needed && TARGET_ARM)
12765     {
12766       /* Create the new frame pointer.  */
12767       if (TARGET_APCS_FRAME)
12768         {
12769           insn = GEN_INT (-(4 + args_to_push + fp_offset));
12770           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
12771           RTX_FRAME_RELATED_P (insn) = 1;
12772
12773           if (IS_NESTED (func_type))
12774             {
12775               /* Recover the static chain register.  */
12776               if (!df_regs_ever_live_p (3)
12777                   || saved_pretend_args)
12778                 insn = gen_rtx_REG (SImode, 3);
12779               else /* if (crtl->args.pretend_args_size == 0) */
12780                 {
12781                   insn = plus_constant (hard_frame_pointer_rtx, 4);
12782                   insn = gen_frame_mem (SImode, insn);
12783                 }
12784               emit_set_insn (ip_rtx, insn);
12785               /* Add a USE to stop propagate_one_insn() from barfing.  */
12786               emit_insn (gen_prologue_use (ip_rtx));
12787             }
12788         }
12789       else
12790         {
12791           insn = GEN_INT (saved_regs - 4);
12792           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12793                                         stack_pointer_rtx, insn));
12794           RTX_FRAME_RELATED_P (insn) = 1;
12795         }
12796     }
12797
12798   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
12799     {
12800       /* This add can produce multiple insns for a large constant, so we
12801          need to get tricky.  */
12802       rtx last = get_last_insn ();
12803
12804       amount = GEN_INT (offsets->saved_args + saved_regs
12805                         - offsets->outgoing_args);
12806
12807       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12808                                     amount));
12809       do
12810         {
12811           last = last ? NEXT_INSN (last) : get_insns ();
12812           RTX_FRAME_RELATED_P (last) = 1;
12813         }
12814       while (last != insn);
12815
12816       /* If the frame pointer is needed, emit a special barrier that
12817          will prevent the scheduler from moving stores to the frame
12818          before the stack adjustment.  */
12819       if (frame_pointer_needed)
12820         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
12821                                          hard_frame_pointer_rtx));
12822     }
12823
12824
12825   if (frame_pointer_needed && TARGET_THUMB2)
12826     thumb_set_frame_pointer (offsets);
12827
12828   if (flag_pic && arm_pic_register != INVALID_REGNUM)
12829     {
12830       unsigned long mask;
12831
12832       mask = live_regs_mask;
12833       mask &= THUMB2_WORK_REGS;
12834       if (!IS_NESTED (func_type))
12835         mask |= (1 << IP_REGNUM);
12836       arm_load_pic_register (mask);
12837     }
12838
12839   /* If we are profiling, make sure no instructions are scheduled before
12840      the call to mcount.  Similarly if the user has requested no
12841      scheduling in the prolog.  Similarly if we want non-call exceptions
12842      using the EABI unwinder, to prevent faulting instructions from being
12843      swapped with a stack adjustment.  */
12844   if (crtl->profile || !TARGET_SCHED_PROLOG
12845       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
12846     emit_insn (gen_blockage ());
12847
12848   /* If the link register is being kept alive, with the return address in it,
12849      then make sure that it does not get reused by the ce2 pass.  */
12850   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
12851     cfun->machine->lr_save_eliminated = 1;
12852 }
12853 \f
12854 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
12855 static void
12856 arm_print_condition (FILE *stream)
12857 {
12858   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
12859     {
12860       /* Branch conversion is not implemented for Thumb-2.  */
12861       if (TARGET_THUMB)
12862         {
12863           output_operand_lossage ("predicated Thumb instruction");
12864           return;
12865         }
12866       if (current_insn_predicate != NULL)
12867         {
12868           output_operand_lossage
12869             ("predicated instruction in conditional sequence");
12870           return;
12871         }
12872
12873       fputs (arm_condition_codes[arm_current_cc], stream);
12874     }
12875   else if (current_insn_predicate)
12876     {
12877       enum arm_cond_code code;
12878
12879       if (TARGET_THUMB1)
12880         {
12881           output_operand_lossage ("predicated Thumb instruction");
12882           return;
12883         }
12884
12885       code = get_arm_condition_code (current_insn_predicate);
12886       fputs (arm_condition_codes[code], stream);
12887     }
12888 }
12889
12890
12891 /* If CODE is 'd', then the X is a condition operand and the instruction
12892    should only be executed if the condition is true.
12893    if CODE is 'D', then the X is a condition operand and the instruction
12894    should only be executed if the condition is false: however, if the mode
12895    of the comparison is CCFPEmode, then always execute the instruction -- we
12896    do this because in these circumstances !GE does not necessarily imply LT;
12897    in these cases the instruction pattern will take care to make sure that
12898    an instruction containing %d will follow, thereby undoing the effects of
12899    doing this instruction unconditionally.
12900    If CODE is 'N' then X is a floating point operand that must be negated
12901    before output.
12902    If CODE is 'B' then output a bitwise inverted value of X (a const int).
12903    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
12904 void
12905 arm_print_operand (FILE *stream, rtx x, int code)
12906 {
12907   switch (code)
12908     {
12909     case '@':
12910       fputs (ASM_COMMENT_START, stream);
12911       return;
12912
12913     case '_':
12914       fputs (user_label_prefix, stream);
12915       return;
12916
12917     case '|':
12918       fputs (REGISTER_PREFIX, stream);
12919       return;
12920
12921     case '?':
12922       arm_print_condition (stream);
12923       return;
12924
12925     case '(':
12926       /* Nothing in unified syntax, otherwise the current condition code.  */
12927       if (!TARGET_UNIFIED_ASM)
12928         arm_print_condition (stream);
12929       break;
12930
12931     case ')':
12932       /* The current condition code in unified syntax, otherwise nothing.  */
12933       if (TARGET_UNIFIED_ASM)
12934         arm_print_condition (stream);
12935       break;
12936   
12937     case '.':
12938       /* The current condition code for a condition code setting instruction.
12939          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
12940       if (TARGET_UNIFIED_ASM)
12941         {
12942           fputc('s', stream);
12943           arm_print_condition (stream);
12944         }
12945       else
12946         {
12947           arm_print_condition (stream);
12948           fputc('s', stream);
12949         }
12950       return;
12951
12952     case '!':
12953       /* If the instruction is conditionally executed then print
12954          the current condition code, otherwise print 's'.  */
12955       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
12956       if (current_insn_predicate)
12957         arm_print_condition (stream);
12958       else
12959         fputc('s', stream);
12960       break;
12961
12962     /* %# is a "break" sequence. It doesn't output anything, but is used to
12963        separate e.g. operand numbers from following text, if that text consists
12964        of further digits which we don't want to be part of the operand
12965        number.  */
12966     case '#':
12967       return;
12968
12969     case 'N':
12970       {
12971         REAL_VALUE_TYPE r;
12972         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12973         r = REAL_VALUE_NEGATE (r);
12974         fprintf (stream, "%s", fp_const_from_val (&r));
12975       }
12976       return;
12977
12978     /* An integer or symbol address without a preceding # sign.  */
12979     case 'c':
12980       switch (GET_CODE (x))
12981         {
12982         case CONST_INT:
12983           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12984           break;
12985
12986         case SYMBOL_REF:
12987           output_addr_const (stream, x);
12988           break;
12989
12990         default:
12991           gcc_unreachable ();
12992         }
12993       return;
12994
12995     case 'B':
12996       if (GET_CODE (x) == CONST_INT)
12997         {
12998           HOST_WIDE_INT val;
12999           val = ARM_SIGN_EXTEND (~INTVAL (x));
13000           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
13001         }
13002       else
13003         {
13004           putc ('~', stream);
13005           output_addr_const (stream, x);
13006         }
13007       return;
13008
13009     case 'L':
13010       /* The low 16 bits of an immediate constant.  */
13011       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
13012       return;
13013
13014     case 'i':
13015       fprintf (stream, "%s", arithmetic_instr (x, 1));
13016       return;
13017
13018     /* Truncate Cirrus shift counts.  */
13019     case 's':
13020       if (GET_CODE (x) == CONST_INT)
13021         {
13022           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
13023           return;
13024         }
13025       arm_print_operand (stream, x, 0);
13026       return;
13027
13028     case 'I':
13029       fprintf (stream, "%s", arithmetic_instr (x, 0));
13030       return;
13031
13032     case 'S':
13033       {
13034         HOST_WIDE_INT val;
13035         const char *shift;
13036
13037         if (!shift_operator (x, SImode))
13038           {
13039             output_operand_lossage ("invalid shift operand");
13040             break;
13041           }
13042
13043         shift = shift_op (x, &val);
13044
13045         if (shift)
13046           {
13047             fprintf (stream, ", %s ", shift);
13048             if (val == -1)
13049               arm_print_operand (stream, XEXP (x, 1), 0);
13050             else
13051               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
13052           }
13053       }
13054       return;
13055
13056       /* An explanation of the 'Q', 'R' and 'H' register operands:
13057
13058          In a pair of registers containing a DI or DF value the 'Q'
13059          operand returns the register number of the register containing
13060          the least significant part of the value.  The 'R' operand returns
13061          the register number of the register containing the most
13062          significant part of the value.
13063
13064          The 'H' operand returns the higher of the two register numbers.
13065          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
13066          same as the 'Q' operand, since the most significant part of the
13067          value is held in the lower number register.  The reverse is true
13068          on systems where WORDS_BIG_ENDIAN is false.
13069
13070          The purpose of these operands is to distinguish between cases
13071          where the endian-ness of the values is important (for example
13072          when they are added together), and cases where the endian-ness
13073          is irrelevant, but the order of register operations is important.
13074          For example when loading a value from memory into a register
13075          pair, the endian-ness does not matter.  Provided that the value
13076          from the lower memory address is put into the lower numbered
13077          register, and the value from the higher address is put into the
13078          higher numbered register, the load will work regardless of whether
13079          the value being loaded is big-wordian or little-wordian.  The
13080          order of the two register loads can matter however, if the address
13081          of the memory location is actually held in one of the registers
13082          being overwritten by the load.  */
13083     case 'Q':
13084       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13085         {
13086           output_operand_lossage ("invalid operand for code '%c'", code);
13087           return;
13088         }
13089
13090       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
13091       return;
13092
13093     case 'R':
13094       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13095         {
13096           output_operand_lossage ("invalid operand for code '%c'", code);
13097           return;
13098         }
13099
13100       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
13101       return;
13102
13103     case 'H':
13104       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13105         {
13106           output_operand_lossage ("invalid operand for code '%c'", code);
13107           return;
13108         }
13109
13110       asm_fprintf (stream, "%r", REGNO (x) + 1);
13111       return;
13112
13113     case 'J':
13114       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13115         {
13116           output_operand_lossage ("invalid operand for code '%c'", code);
13117           return;
13118         }
13119
13120       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
13121       return;
13122
13123     case 'K':
13124       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
13125         {
13126           output_operand_lossage ("invalid operand for code '%c'", code);
13127           return;
13128         }
13129
13130       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
13131       return;
13132
13133     case 'm':
13134       asm_fprintf (stream, "%r",
13135                    GET_CODE (XEXP (x, 0)) == REG
13136                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
13137       return;
13138
13139     case 'M':
13140       asm_fprintf (stream, "{%r-%r}",
13141                    REGNO (x),
13142                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
13143       return;
13144
13145     /* Like 'M', but writing doubleword vector registers, for use by Neon
13146        insns.  */
13147     case 'h':
13148       {
13149         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
13150         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
13151         if (numregs == 1)
13152           asm_fprintf (stream, "{d%d}", regno);
13153         else
13154           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
13155       }
13156       return;
13157
13158     case 'd':
13159       /* CONST_TRUE_RTX means always -- that's the default.  */
13160       if (x == const_true_rtx)
13161         return;
13162
13163       if (!COMPARISON_P (x))
13164         {
13165           output_operand_lossage ("invalid operand for code '%c'", code);
13166           return;
13167         }
13168
13169       fputs (arm_condition_codes[get_arm_condition_code (x)],
13170              stream);
13171       return;
13172
13173     case 'D':
13174       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
13175          want to do that.  */
13176       if (x == const_true_rtx)
13177         {
13178           output_operand_lossage ("instruction never executed");
13179           return;
13180         }
13181       if (!COMPARISON_P (x))
13182         {
13183           output_operand_lossage ("invalid operand for code '%c'", code);
13184           return;
13185         }
13186
13187       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
13188                                  (get_arm_condition_code (x))],
13189              stream);
13190       return;
13191
13192     /* Cirrus registers can be accessed in a variety of ways:
13193          single floating point (f)
13194          double floating point (d)
13195          32bit integer         (fx)
13196          64bit integer         (dx).  */
13197     case 'W':                   /* Cirrus register in F mode.  */
13198     case 'X':                   /* Cirrus register in D mode.  */
13199     case 'Y':                   /* Cirrus register in FX mode.  */
13200     case 'Z':                   /* Cirrus register in DX mode.  */
13201       gcc_assert (GET_CODE (x) == REG
13202                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
13203
13204       fprintf (stream, "mv%s%s",
13205                code == 'W' ? "f"
13206                : code == 'X' ? "d"
13207                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
13208
13209       return;
13210
13211     /* Print cirrus register in the mode specified by the register's mode.  */
13212     case 'V':
13213       {
13214         int mode = GET_MODE (x);
13215
13216         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
13217           {
13218             output_operand_lossage ("invalid operand for code '%c'", code);
13219             return;
13220           }
13221
13222         fprintf (stream, "mv%s%s",
13223                  mode == DFmode ? "d"
13224                  : mode == SImode ? "fx"
13225                  : mode == DImode ? "dx"
13226                  : "f", reg_names[REGNO (x)] + 2);
13227
13228         return;
13229       }
13230
13231     case 'U':
13232       if (GET_CODE (x) != REG
13233           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
13234           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
13235         /* Bad value for wCG register number.  */
13236         {
13237           output_operand_lossage ("invalid operand for code '%c'", code);
13238           return;
13239         }
13240
13241       else
13242         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
13243       return;
13244
13245       /* Print an iWMMXt control register name.  */
13246     case 'w':
13247       if (GET_CODE (x) != CONST_INT
13248           || INTVAL (x) < 0
13249           || INTVAL (x) >= 16)
13250         /* Bad value for wC register number.  */
13251         {
13252           output_operand_lossage ("invalid operand for code '%c'", code);
13253           return;
13254         }
13255
13256       else
13257         {
13258           static const char * wc_reg_names [16] =
13259             {
13260               "wCID",  "wCon",  "wCSSF", "wCASF",
13261               "wC4",   "wC5",   "wC6",   "wC7",
13262               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
13263               "wC12",  "wC13",  "wC14",  "wC15"
13264             };
13265
13266           fprintf (stream, wc_reg_names [INTVAL (x)]);
13267         }
13268       return;
13269
13270     /* Print a VFP/Neon double precision or quad precision register name.  */
13271     case 'P':
13272     case 'q':
13273       {
13274         int mode = GET_MODE (x);
13275         int is_quad = (code == 'q');
13276         int regno;
13277
13278         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
13279           {
13280             output_operand_lossage ("invalid operand for code '%c'", code);
13281             return;
13282           }
13283
13284         if (GET_CODE (x) != REG
13285             || !IS_VFP_REGNUM (REGNO (x)))
13286           {
13287             output_operand_lossage ("invalid operand for code '%c'", code);
13288             return;
13289           }
13290
13291         regno = REGNO (x);
13292         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
13293             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
13294           {
13295             output_operand_lossage ("invalid operand for code '%c'", code);
13296             return;
13297           }
13298
13299         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
13300           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
13301       }
13302       return;
13303
13304     /* These two codes print the low/high doubleword register of a Neon quad
13305        register, respectively.  For pair-structure types, can also print
13306        low/high quadword registers.  */
13307     case 'e':
13308     case 'f':
13309       {
13310         int mode = GET_MODE (x);
13311         int regno;
13312
13313         if ((GET_MODE_SIZE (mode) != 16
13314              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
13315           {
13316             output_operand_lossage ("invalid operand for code '%c'", code);
13317             return;
13318           }
13319
13320         regno = REGNO (x);
13321         if (!NEON_REGNO_OK_FOR_QUAD (regno))
13322           {
13323             output_operand_lossage ("invalid operand for code '%c'", code);
13324             return;
13325           }
13326
13327         if (GET_MODE_SIZE (mode) == 16)
13328           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
13329                                   + (code == 'f' ? 1 : 0));
13330         else
13331           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
13332                                   + (code == 'f' ? 1 : 0));
13333       }
13334       return;
13335
13336     /* Print a VFPv3 floating-point constant, represented as an integer
13337        index.  */
13338     case 'G':
13339       {
13340         int index = vfp3_const_double_index (x);
13341         gcc_assert (index != -1);
13342         fprintf (stream, "%d", index);
13343       }
13344       return;
13345
13346     /* Print bits representing opcode features for Neon.
13347
13348        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
13349        and polynomials as unsigned.
13350
13351        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
13352
13353        Bit 2 is 1 for rounding functions, 0 otherwise.  */
13354
13355     /* Identify the type as 's', 'u', 'p' or 'f'.  */
13356     case 'T':
13357       {
13358         HOST_WIDE_INT bits = INTVAL (x);
13359         fputc ("uspf"[bits & 3], stream);
13360       }
13361       return;
13362
13363     /* Likewise, but signed and unsigned integers are both 'i'.  */
13364     case 'F':
13365       {
13366         HOST_WIDE_INT bits = INTVAL (x);
13367         fputc ("iipf"[bits & 3], stream);
13368       }
13369       return;
13370
13371     /* As for 'T', but emit 'u' instead of 'p'.  */
13372     case 't':
13373       {
13374         HOST_WIDE_INT bits = INTVAL (x);
13375         fputc ("usuf"[bits & 3], stream);
13376       }
13377       return;
13378
13379     /* Bit 2: rounding (vs none).  */
13380     case 'O':
13381       {
13382         HOST_WIDE_INT bits = INTVAL (x);
13383         fputs ((bits & 4) != 0 ? "r" : "", stream);
13384       }
13385       return;
13386
13387     default:
13388       if (x == 0)
13389         {
13390           output_operand_lossage ("missing operand");
13391           return;
13392         }
13393
13394       switch (GET_CODE (x))
13395         {
13396         case REG:
13397           asm_fprintf (stream, "%r", REGNO (x));
13398           break;
13399
13400         case MEM:
13401           output_memory_reference_mode = GET_MODE (x);
13402           output_address (XEXP (x, 0));
13403           break;
13404
13405         case CONST_DOUBLE:
13406           if (TARGET_NEON)
13407             {
13408               char fpstr[20];
13409               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
13410                                sizeof (fpstr), 0, 1);
13411               fprintf (stream, "#%s", fpstr);
13412             }
13413           else
13414             fprintf (stream, "#%s", fp_immediate_constant (x));
13415           break;
13416
13417         default:
13418           gcc_assert (GET_CODE (x) != NEG);
13419           fputc ('#', stream);
13420           output_addr_const (stream, x);
13421           break;
13422         }
13423     }
13424 }
13425 \f
13426 /* Target hook for assembling integer objects.  The ARM version needs to
13427    handle word-sized values specially.  */
13428 static bool
13429 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
13430 {
13431   enum machine_mode mode;
13432
13433   if (size == UNITS_PER_WORD && aligned_p)
13434     {
13435       fputs ("\t.word\t", asm_out_file);
13436       output_addr_const (asm_out_file, x);
13437
13438       /* Mark symbols as position independent.  We only do this in the
13439          .text segment, not in the .data segment.  */
13440       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
13441           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
13442         {
13443           /* See legitimize_pic_address for an explanation of the
13444              TARGET_VXWORKS_RTP check.  */
13445           if (TARGET_VXWORKS_RTP
13446               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
13447             fputs ("(GOT)", asm_out_file);
13448           else
13449             fputs ("(GOTOFF)", asm_out_file);
13450         }
13451       fputc ('\n', asm_out_file);
13452       return true;
13453     }
13454
13455   mode = GET_MODE (x);
13456
13457   if (arm_vector_mode_supported_p (mode))
13458     {
13459       int i, units;
13460
13461       gcc_assert (GET_CODE (x) == CONST_VECTOR);
13462
13463       units = CONST_VECTOR_NUNITS (x);
13464       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
13465
13466       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13467         for (i = 0; i < units; i++)
13468           {
13469             rtx elt = CONST_VECTOR_ELT (x, i);
13470             assemble_integer
13471               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
13472           }
13473       else
13474         for (i = 0; i < units; i++)
13475           {
13476             rtx elt = CONST_VECTOR_ELT (x, i);
13477             REAL_VALUE_TYPE rval;
13478
13479             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
13480
13481             assemble_real
13482               (rval, GET_MODE_INNER (mode),
13483               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
13484           }
13485
13486       return true;
13487     }
13488
13489   return default_assemble_integer (x, size, aligned_p);
13490 }
13491
13492 static void
13493 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
13494 {
13495   section *s;
13496
13497   if (!TARGET_AAPCS_BASED)
13498     {
13499       (is_ctor ? 
13500        default_named_section_asm_out_constructor 
13501        : default_named_section_asm_out_destructor) (symbol, priority);
13502       return;
13503     }
13504
13505   /* Put these in the .init_array section, using a special relocation.  */
13506   if (priority != DEFAULT_INIT_PRIORITY)
13507     {
13508       char buf[18];
13509       sprintf (buf, "%s.%.5u", 
13510                is_ctor ? ".init_array" : ".fini_array",
13511                priority);
13512       s = get_section (buf, SECTION_WRITE, NULL_TREE);
13513     }
13514   else if (is_ctor)
13515     s = ctors_section;
13516   else
13517     s = dtors_section;
13518
13519   switch_to_section (s);
13520   assemble_align (POINTER_SIZE);
13521   fputs ("\t.word\t", asm_out_file);
13522   output_addr_const (asm_out_file, symbol);
13523   fputs ("(target1)\n", asm_out_file);
13524 }
13525
13526 /* Add a function to the list of static constructors.  */
13527
13528 static void
13529 arm_elf_asm_constructor (rtx symbol, int priority)
13530 {
13531   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
13532 }
13533
13534 /* Add a function to the list of static destructors.  */
13535
13536 static void
13537 arm_elf_asm_destructor (rtx symbol, int priority)
13538 {
13539   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
13540 }
13541 \f
13542 /* A finite state machine takes care of noticing whether or not instructions
13543    can be conditionally executed, and thus decrease execution time and code
13544    size by deleting branch instructions.  The fsm is controlled by
13545    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
13546
13547 /* The state of the fsm controlling condition codes are:
13548    0: normal, do nothing special
13549    1: make ASM_OUTPUT_OPCODE not output this instruction
13550    2: make ASM_OUTPUT_OPCODE not output this instruction
13551    3: make instructions conditional
13552    4: make instructions conditional
13553
13554    State transitions (state->state by whom under condition):
13555    0 -> 1 final_prescan_insn if the `target' is a label
13556    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
13557    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
13558    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
13559    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
13560           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
13561    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
13562           (the target insn is arm_target_insn).
13563
13564    If the jump clobbers the conditions then we use states 2 and 4.
13565
13566    A similar thing can be done with conditional return insns.
13567
13568    XXX In case the `target' is an unconditional branch, this conditionalising
13569    of the instructions always reduces code size, but not always execution
13570    time.  But then, I want to reduce the code size to somewhere near what
13571    /bin/cc produces.  */
13572
13573 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
13574    instructions.  When a COND_EXEC instruction is seen the subsequent
13575    instructions are scanned so that multiple conditional instructions can be
13576    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
13577    specify the length and true/false mask for the IT block.  These will be
13578    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
13579
13580 /* Returns the index of the ARM condition code string in
13581    `arm_condition_codes'.  COMPARISON should be an rtx like
13582    `(eq (...) (...))'.  */
13583 static enum arm_cond_code
13584 get_arm_condition_code (rtx comparison)
13585 {
13586   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
13587   int code;
13588   enum rtx_code comp_code = GET_CODE (comparison);
13589
13590   if (GET_MODE_CLASS (mode) != MODE_CC)
13591     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
13592                            XEXP (comparison, 1));
13593
13594   switch (mode)
13595     {
13596     case CC_DNEmode: code = ARM_NE; goto dominance;
13597     case CC_DEQmode: code = ARM_EQ; goto dominance;
13598     case CC_DGEmode: code = ARM_GE; goto dominance;
13599     case CC_DGTmode: code = ARM_GT; goto dominance;
13600     case CC_DLEmode: code = ARM_LE; goto dominance;
13601     case CC_DLTmode: code = ARM_LT; goto dominance;
13602     case CC_DGEUmode: code = ARM_CS; goto dominance;
13603     case CC_DGTUmode: code = ARM_HI; goto dominance;
13604     case CC_DLEUmode: code = ARM_LS; goto dominance;
13605     case CC_DLTUmode: code = ARM_CC;
13606
13607     dominance:
13608       gcc_assert (comp_code == EQ || comp_code == NE);
13609
13610       if (comp_code == EQ)
13611         return ARM_INVERSE_CONDITION_CODE (code);
13612       return code;
13613
13614     case CC_NOOVmode:
13615       switch (comp_code)
13616         {
13617         case NE: return ARM_NE;
13618         case EQ: return ARM_EQ;
13619         case GE: return ARM_PL;
13620         case LT: return ARM_MI;
13621         default: gcc_unreachable ();
13622         }
13623
13624     case CC_Zmode:
13625       switch (comp_code)
13626         {
13627         case NE: return ARM_NE;
13628         case EQ: return ARM_EQ;
13629         default: gcc_unreachable ();
13630         }
13631
13632     case CC_Nmode:
13633       switch (comp_code)
13634         {
13635         case NE: return ARM_MI;
13636         case EQ: return ARM_PL;
13637         default: gcc_unreachable ();
13638         }
13639
13640     case CCFPEmode:
13641     case CCFPmode:
13642       /* These encodings assume that AC=1 in the FPA system control
13643          byte.  This allows us to handle all cases except UNEQ and
13644          LTGT.  */
13645       switch (comp_code)
13646         {
13647         case GE: return ARM_GE;
13648         case GT: return ARM_GT;
13649         case LE: return ARM_LS;
13650         case LT: return ARM_MI;
13651         case NE: return ARM_NE;
13652         case EQ: return ARM_EQ;
13653         case ORDERED: return ARM_VC;
13654         case UNORDERED: return ARM_VS;
13655         case UNLT: return ARM_LT;
13656         case UNLE: return ARM_LE;
13657         case UNGT: return ARM_HI;
13658         case UNGE: return ARM_PL;
13659           /* UNEQ and LTGT do not have a representation.  */
13660         case UNEQ: /* Fall through.  */
13661         case LTGT: /* Fall through.  */
13662         default: gcc_unreachable ();
13663         }
13664
13665     case CC_SWPmode:
13666       switch (comp_code)
13667         {
13668         case NE: return ARM_NE;
13669         case EQ: return ARM_EQ;
13670         case GE: return ARM_LE;
13671         case GT: return ARM_LT;
13672         case LE: return ARM_GE;
13673         case LT: return ARM_GT;
13674         case GEU: return ARM_LS;
13675         case GTU: return ARM_CC;
13676         case LEU: return ARM_CS;
13677         case LTU: return ARM_HI;
13678         default: gcc_unreachable ();
13679         }
13680
13681     case CC_Cmode:
13682       switch (comp_code)
13683       {
13684       case LTU: return ARM_CS;
13685       case GEU: return ARM_CC;
13686       default: gcc_unreachable ();
13687       }
13688
13689     case CCmode:
13690       switch (comp_code)
13691         {
13692         case NE: return ARM_NE;
13693         case EQ: return ARM_EQ;
13694         case GE: return ARM_GE;
13695         case GT: return ARM_GT;
13696         case LE: return ARM_LE;
13697         case LT: return ARM_LT;
13698         case GEU: return ARM_CS;
13699         case GTU: return ARM_HI;
13700         case LEU: return ARM_LS;
13701         case LTU: return ARM_CC;
13702         default: gcc_unreachable ();
13703         }
13704
13705     default: gcc_unreachable ();
13706     }
13707 }
13708
13709 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
13710    instructions.  */
13711 void
13712 thumb2_final_prescan_insn (rtx insn)
13713 {
13714   rtx first_insn = insn;
13715   rtx body = PATTERN (insn);
13716   rtx predicate;
13717   enum arm_cond_code code;
13718   int n;
13719   int mask;
13720
13721   /* Remove the previous insn from the count of insns to be output.  */
13722   if (arm_condexec_count)
13723       arm_condexec_count--;
13724
13725   /* Nothing to do if we are already inside a conditional block.  */
13726   if (arm_condexec_count)
13727     return;
13728
13729   if (GET_CODE (body) != COND_EXEC)
13730     return;
13731
13732   /* Conditional jumps are implemented directly.  */
13733   if (GET_CODE (insn) == JUMP_INSN)
13734     return;
13735
13736   predicate = COND_EXEC_TEST (body);
13737   arm_current_cc = get_arm_condition_code (predicate);
13738
13739   n = get_attr_ce_count (insn);
13740   arm_condexec_count = 1;
13741   arm_condexec_mask = (1 << n) - 1;
13742   arm_condexec_masklen = n;
13743   /* See if subsequent instructions can be combined into the same block.  */
13744   for (;;)
13745     {
13746       insn = next_nonnote_insn (insn);
13747
13748       /* Jumping into the middle of an IT block is illegal, so a label or
13749          barrier terminates the block.  */
13750       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
13751         break;
13752
13753       body = PATTERN (insn);
13754       /* USE and CLOBBER aren't really insns, so just skip them.  */
13755       if (GET_CODE (body) == USE
13756           || GET_CODE (body) == CLOBBER)
13757         continue;
13758
13759       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
13760       if (GET_CODE (body) != COND_EXEC)
13761         break;
13762       /* Allow up to 4 conditionally executed instructions in a block.  */
13763       n = get_attr_ce_count (insn);
13764       if (arm_condexec_masklen + n > 4)
13765         break;
13766
13767       predicate = COND_EXEC_TEST (body);
13768       code = get_arm_condition_code (predicate);
13769       mask = (1 << n) - 1;
13770       if (arm_current_cc == code)
13771         arm_condexec_mask |= (mask << arm_condexec_masklen);
13772       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
13773         break;
13774
13775       arm_condexec_count++;
13776       arm_condexec_masklen += n;
13777
13778       /* A jump must be the last instruction in a conditional block.  */
13779       if (GET_CODE(insn) == JUMP_INSN)
13780         break;
13781     }
13782   /* Restore recog_data (getting the attributes of other insns can
13783      destroy this array, but final.c assumes that it remains intact
13784      across this call).  */
13785   extract_constrain_insn_cached (first_insn);
13786 }
13787
13788 void
13789 arm_final_prescan_insn (rtx insn)
13790 {
13791   /* BODY will hold the body of INSN.  */
13792   rtx body = PATTERN (insn);
13793
13794   /* This will be 1 if trying to repeat the trick, and things need to be
13795      reversed if it appears to fail.  */
13796   int reverse = 0;
13797
13798   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
13799      taken are clobbered, even if the rtl suggests otherwise.  It also
13800      means that we have to grub around within the jump expression to find
13801      out what the conditions are when the jump isn't taken.  */
13802   int jump_clobbers = 0;
13803
13804   /* If we start with a return insn, we only succeed if we find another one.  */
13805   int seeking_return = 0;
13806
13807   /* START_INSN will hold the insn from where we start looking.  This is the
13808      first insn after the following code_label if REVERSE is true.  */
13809   rtx start_insn = insn;
13810
13811   /* If in state 4, check if the target branch is reached, in order to
13812      change back to state 0.  */
13813   if (arm_ccfsm_state == 4)
13814     {
13815       if (insn == arm_target_insn)
13816         {
13817           arm_target_insn = NULL;
13818           arm_ccfsm_state = 0;
13819         }
13820       return;
13821     }
13822
13823   /* If in state 3, it is possible to repeat the trick, if this insn is an
13824      unconditional branch to a label, and immediately following this branch
13825      is the previous target label which is only used once, and the label this
13826      branch jumps to is not too far off.  */
13827   if (arm_ccfsm_state == 3)
13828     {
13829       if (simplejump_p (insn))
13830         {
13831           start_insn = next_nonnote_insn (start_insn);
13832           if (GET_CODE (start_insn) == BARRIER)
13833             {
13834               /* XXX Isn't this always a barrier?  */
13835               start_insn = next_nonnote_insn (start_insn);
13836             }
13837           if (GET_CODE (start_insn) == CODE_LABEL
13838               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
13839               && LABEL_NUSES (start_insn) == 1)
13840             reverse = TRUE;
13841           else
13842             return;
13843         }
13844       else if (GET_CODE (body) == RETURN)
13845         {
13846           start_insn = next_nonnote_insn (start_insn);
13847           if (GET_CODE (start_insn) == BARRIER)
13848             start_insn = next_nonnote_insn (start_insn);
13849           if (GET_CODE (start_insn) == CODE_LABEL
13850               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
13851               && LABEL_NUSES (start_insn) == 1)
13852             {
13853               reverse = TRUE;
13854               seeking_return = 1;
13855             }
13856           else
13857             return;
13858         }
13859       else
13860         return;
13861     }
13862
13863   gcc_assert (!arm_ccfsm_state || reverse);
13864   if (GET_CODE (insn) != JUMP_INSN)
13865     return;
13866
13867   /* This jump might be paralleled with a clobber of the condition codes
13868      the jump should always come first */
13869   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
13870     body = XVECEXP (body, 0, 0);
13871
13872   if (reverse
13873       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
13874           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
13875     {
13876       int insns_skipped;
13877       int fail = FALSE, succeed = FALSE;
13878       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
13879       int then_not_else = TRUE;
13880       rtx this_insn = start_insn, label = 0;
13881
13882       /* If the jump cannot be done with one instruction, we cannot
13883          conditionally execute the instruction in the inverse case.  */
13884       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
13885         {
13886           jump_clobbers = 1;
13887           return;
13888         }
13889
13890       /* Register the insn jumped to.  */
13891       if (reverse)
13892         {
13893           if (!seeking_return)
13894             label = XEXP (SET_SRC (body), 0);
13895         }
13896       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
13897         label = XEXP (XEXP (SET_SRC (body), 1), 0);
13898       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
13899         {
13900           label = XEXP (XEXP (SET_SRC (body), 2), 0);
13901           then_not_else = FALSE;
13902         }
13903       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
13904         seeking_return = 1;
13905       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
13906         {
13907           seeking_return = 1;
13908           then_not_else = FALSE;
13909         }
13910       else
13911         gcc_unreachable ();
13912
13913       /* See how many insns this branch skips, and what kind of insns.  If all
13914          insns are okay, and the label or unconditional branch to the same
13915          label is not too far away, succeed.  */
13916       for (insns_skipped = 0;
13917            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
13918         {
13919           rtx scanbody;
13920
13921           this_insn = next_nonnote_insn (this_insn);
13922           if (!this_insn)
13923             break;
13924
13925           switch (GET_CODE (this_insn))
13926             {
13927             case CODE_LABEL:
13928               /* Succeed if it is the target label, otherwise fail since
13929                  control falls in from somewhere else.  */
13930               if (this_insn == label)
13931                 {
13932                   if (jump_clobbers)
13933                     {
13934                       arm_ccfsm_state = 2;
13935                       this_insn = next_nonnote_insn (this_insn);
13936                     }
13937                   else
13938                     arm_ccfsm_state = 1;
13939                   succeed = TRUE;
13940                 }
13941               else
13942                 fail = TRUE;
13943               break;
13944
13945             case BARRIER:
13946               /* Succeed if the following insn is the target label.
13947                  Otherwise fail.
13948                  If return insns are used then the last insn in a function
13949                  will be a barrier.  */
13950               this_insn = next_nonnote_insn (this_insn);
13951               if (this_insn && this_insn == label)
13952                 {
13953                   if (jump_clobbers)
13954                     {
13955                       arm_ccfsm_state = 2;
13956                       this_insn = next_nonnote_insn (this_insn);
13957                     }
13958                   else
13959                     arm_ccfsm_state = 1;
13960                   succeed = TRUE;
13961                 }
13962               else
13963                 fail = TRUE;
13964               break;
13965
13966             case CALL_INSN:
13967               /* The AAPCS says that conditional calls should not be
13968                  used since they make interworking inefficient (the
13969                  linker can't transform BL<cond> into BLX).  That's
13970                  only a problem if the machine has BLX.  */
13971               if (arm_arch5)
13972                 {
13973                   fail = TRUE;
13974                   break;
13975                 }
13976
13977               /* Succeed if the following insn is the target label, or
13978                  if the following two insns are a barrier and the
13979                  target label.  */
13980               this_insn = next_nonnote_insn (this_insn);
13981               if (this_insn && GET_CODE (this_insn) == BARRIER)
13982                 this_insn = next_nonnote_insn (this_insn);
13983
13984               if (this_insn && this_insn == label
13985                   && insns_skipped < max_insns_skipped)
13986                 {
13987                   if (jump_clobbers)
13988                     {
13989                       arm_ccfsm_state = 2;
13990                       this_insn = next_nonnote_insn (this_insn);
13991                     }
13992                   else
13993                     arm_ccfsm_state = 1;
13994                   succeed = TRUE;
13995                 }
13996               else
13997                 fail = TRUE;
13998               break;
13999
14000             case JUMP_INSN:
14001               /* If this is an unconditional branch to the same label, succeed.
14002                  If it is to another label, do nothing.  If it is conditional,
14003                  fail.  */
14004               /* XXX Probably, the tests for SET and the PC are
14005                  unnecessary.  */
14006
14007               scanbody = PATTERN (this_insn);
14008               if (GET_CODE (scanbody) == SET
14009                   && GET_CODE (SET_DEST (scanbody)) == PC)
14010                 {
14011                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
14012                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
14013                     {
14014                       arm_ccfsm_state = 2;
14015                       succeed = TRUE;
14016                     }
14017                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
14018                     fail = TRUE;
14019                 }
14020               /* Fail if a conditional return is undesirable (e.g. on a
14021                  StrongARM), but still allow this if optimizing for size.  */
14022               else if (GET_CODE (scanbody) == RETURN
14023                        && !use_return_insn (TRUE, NULL)
14024                        && !optimize_size)
14025                 fail = TRUE;
14026               else if (GET_CODE (scanbody) == RETURN
14027                        && seeking_return)
14028                 {
14029                   arm_ccfsm_state = 2;
14030                   succeed = TRUE;
14031                 }
14032               else if (GET_CODE (scanbody) == PARALLEL)
14033                 {
14034                   switch (get_attr_conds (this_insn))
14035                     {
14036                     case CONDS_NOCOND:
14037                       break;
14038                     default:
14039                       fail = TRUE;
14040                       break;
14041                     }
14042                 }
14043               else
14044                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
14045
14046               break;
14047
14048             case INSN:
14049               /* Instructions using or affecting the condition codes make it
14050                  fail.  */
14051               scanbody = PATTERN (this_insn);
14052               if (!(GET_CODE (scanbody) == SET
14053                     || GET_CODE (scanbody) == PARALLEL)
14054                   || get_attr_conds (this_insn) != CONDS_NOCOND)
14055                 fail = TRUE;
14056
14057               /* A conditional cirrus instruction must be followed by
14058                  a non Cirrus instruction.  However, since we
14059                  conditionalize instructions in this function and by
14060                  the time we get here we can't add instructions
14061                  (nops), because shorten_branches() has already been
14062                  called, we will disable conditionalizing Cirrus
14063                  instructions to be safe.  */
14064               if (GET_CODE (scanbody) != USE
14065                   && GET_CODE (scanbody) != CLOBBER
14066                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
14067                 fail = TRUE;
14068               break;
14069
14070             default:
14071               break;
14072             }
14073         }
14074       if (succeed)
14075         {
14076           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
14077             arm_target_label = CODE_LABEL_NUMBER (label);
14078           else
14079             {
14080               gcc_assert (seeking_return || arm_ccfsm_state == 2);
14081
14082               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
14083                 {
14084                   this_insn = next_nonnote_insn (this_insn);
14085                   gcc_assert (!this_insn
14086                               || (GET_CODE (this_insn) != BARRIER
14087                                   && GET_CODE (this_insn) != CODE_LABEL));
14088                 }
14089               if (!this_insn)
14090                 {
14091                   /* Oh, dear! we ran off the end.. give up.  */
14092                   extract_constrain_insn_cached (insn);
14093                   arm_ccfsm_state = 0;
14094                   arm_target_insn = NULL;
14095                   return;
14096                 }
14097               arm_target_insn = this_insn;
14098             }
14099           if (jump_clobbers)
14100             {
14101               gcc_assert (!reverse);
14102               arm_current_cc =
14103                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
14104                                                             0), 0), 1));
14105               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
14106                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14107               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
14108                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14109             }
14110           else
14111             {
14112               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
14113                  what it was.  */
14114               if (!reverse)
14115                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
14116                                                                0));
14117             }
14118
14119           if (reverse || then_not_else)
14120             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
14121         }
14122
14123       /* Restore recog_data (getting the attributes of other insns can
14124          destroy this array, but final.c assumes that it remains intact
14125          across this call.  */
14126       extract_constrain_insn_cached (insn);
14127     }
14128 }
14129
14130 /* Output IT instructions.  */
14131 void
14132 thumb2_asm_output_opcode (FILE * stream)
14133 {
14134   char buff[5];
14135   int n;
14136
14137   if (arm_condexec_mask)
14138     {
14139       for (n = 0; n < arm_condexec_masklen; n++)
14140         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
14141       buff[n] = 0;
14142       asm_fprintf(stream, "i%s\t%s\n\t", buff,
14143                   arm_condition_codes[arm_current_cc]);
14144       arm_condexec_mask = 0;
14145     }
14146 }
14147
14148 /* Returns true if REGNO is a valid register
14149    for holding a quantity of type MODE.  */
14150 int
14151 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
14152 {
14153   if (GET_MODE_CLASS (mode) == MODE_CC)
14154     return (regno == CC_REGNUM
14155             || (TARGET_HARD_FLOAT && TARGET_VFP
14156                 && regno == VFPCC_REGNUM));
14157
14158   if (TARGET_THUMB1)
14159     /* For the Thumb we only allow values bigger than SImode in
14160        registers 0 - 6, so that there is always a second low
14161        register available to hold the upper part of the value.
14162        We probably we ought to ensure that the register is the
14163        start of an even numbered register pair.  */
14164     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
14165
14166   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
14167       && IS_CIRRUS_REGNUM (regno))
14168     /* We have outlawed SI values in Cirrus registers because they
14169        reside in the lower 32 bits, but SF values reside in the
14170        upper 32 bits.  This causes gcc all sorts of grief.  We can't
14171        even split the registers into pairs because Cirrus SI values
14172        get sign extended to 64bits-- aldyh.  */
14173     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
14174
14175   if (TARGET_HARD_FLOAT && TARGET_VFP
14176       && IS_VFP_REGNUM (regno))
14177     {
14178       if (mode == SFmode || mode == SImode)
14179         return VFP_REGNO_OK_FOR_SINGLE (regno);
14180
14181       if (mode == DFmode)
14182         return VFP_REGNO_OK_FOR_DOUBLE (regno);
14183
14184       if (TARGET_NEON)
14185         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
14186                || (VALID_NEON_QREG_MODE (mode)
14187                    && NEON_REGNO_OK_FOR_QUAD (regno))
14188                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
14189                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
14190                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
14191                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
14192                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
14193
14194       return FALSE;
14195     }
14196
14197   if (TARGET_REALLY_IWMMXT)
14198     {
14199       if (IS_IWMMXT_GR_REGNUM (regno))
14200         return mode == SImode;
14201
14202       if (IS_IWMMXT_REGNUM (regno))
14203         return VALID_IWMMXT_REG_MODE (mode);
14204     }
14205   
14206   /* We allow any value to be stored in the general registers.
14207      Restrict doubleword quantities to even register pairs so that we can
14208      use ldrd.  Do not allow Neon structure opaque modes in general registers;
14209      they would use too many.  */
14210   if (regno <= LAST_ARM_REGNUM)
14211     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
14212       && !VALID_NEON_STRUCT_MODE (mode);
14213
14214   if (regno == FRAME_POINTER_REGNUM
14215       || regno == ARG_POINTER_REGNUM)
14216     /* We only allow integers in the fake hard registers.  */
14217     return GET_MODE_CLASS (mode) == MODE_INT;
14218
14219   /* The only registers left are the FPA registers
14220      which we only allow to hold FP values.  */
14221   return (TARGET_HARD_FLOAT && TARGET_FPA
14222           && GET_MODE_CLASS (mode) == MODE_FLOAT
14223           && regno >= FIRST_FPA_REGNUM
14224           && regno <= LAST_FPA_REGNUM);
14225 }
14226
14227 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
14228    not used in arm mode.  */
14229 int
14230 arm_regno_class (int regno)
14231 {
14232   if (TARGET_THUMB1)
14233     {
14234       if (regno == STACK_POINTER_REGNUM)
14235         return STACK_REG;
14236       if (regno == CC_REGNUM)
14237         return CC_REG;
14238       if (regno < 8)
14239         return LO_REGS;
14240       return HI_REGS;
14241     }
14242
14243   if (TARGET_THUMB2 && regno < 8)
14244     return LO_REGS;
14245
14246   if (   regno <= LAST_ARM_REGNUM
14247       || regno == FRAME_POINTER_REGNUM
14248       || regno == ARG_POINTER_REGNUM)
14249     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
14250
14251   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
14252     return TARGET_THUMB2 ? CC_REG : NO_REGS;
14253
14254   if (IS_CIRRUS_REGNUM (regno))
14255     return CIRRUS_REGS;
14256
14257   if (IS_VFP_REGNUM (regno))
14258     {
14259       if (regno <= D7_VFP_REGNUM)
14260         return VFP_D0_D7_REGS;
14261       else if (regno <= LAST_LO_VFP_REGNUM)
14262         return VFP_LO_REGS;
14263       else
14264         return VFP_HI_REGS;
14265     }
14266
14267   if (IS_IWMMXT_REGNUM (regno))
14268     return IWMMXT_REGS;
14269
14270   if (IS_IWMMXT_GR_REGNUM (regno))
14271     return IWMMXT_GR_REGS;
14272
14273   return FPA_REGS;
14274 }
14275
14276 /* Handle a special case when computing the offset
14277    of an argument from the frame pointer.  */
14278 int
14279 arm_debugger_arg_offset (int value, rtx addr)
14280 {
14281   rtx insn;
14282
14283   /* We are only interested if dbxout_parms() failed to compute the offset.  */
14284   if (value != 0)
14285     return 0;
14286
14287   /* We can only cope with the case where the address is held in a register.  */
14288   if (GET_CODE (addr) != REG)
14289     return 0;
14290
14291   /* If we are using the frame pointer to point at the argument, then
14292      an offset of 0 is correct.  */
14293   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
14294     return 0;
14295
14296   /* If we are using the stack pointer to point at the
14297      argument, then an offset of 0 is correct.  */
14298   /* ??? Check this is consistent with thumb2 frame layout.  */
14299   if ((TARGET_THUMB || !frame_pointer_needed)
14300       && REGNO (addr) == SP_REGNUM)
14301     return 0;
14302
14303   /* Oh dear.  The argument is pointed to by a register rather
14304      than being held in a register, or being stored at a known
14305      offset from the frame pointer.  Since GDB only understands
14306      those two kinds of argument we must translate the address
14307      held in the register into an offset from the frame pointer.
14308      We do this by searching through the insns for the function
14309      looking to see where this register gets its value.  If the
14310      register is initialized from the frame pointer plus an offset
14311      then we are in luck and we can continue, otherwise we give up.
14312
14313      This code is exercised by producing debugging information
14314      for a function with arguments like this:
14315
14316            double func (double a, double b, int c, double d) {return d;}
14317
14318      Without this code the stab for parameter 'd' will be set to
14319      an offset of 0 from the frame pointer, rather than 8.  */
14320
14321   /* The if() statement says:
14322
14323      If the insn is a normal instruction
14324      and if the insn is setting the value in a register
14325      and if the register being set is the register holding the address of the argument
14326      and if the address is computing by an addition
14327      that involves adding to a register
14328      which is the frame pointer
14329      a constant integer
14330
14331      then...  */
14332
14333   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14334     {
14335       if (   GET_CODE (insn) == INSN
14336           && GET_CODE (PATTERN (insn)) == SET
14337           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
14338           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
14339           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
14340           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
14341           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
14342              )
14343         {
14344           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
14345
14346           break;
14347         }
14348     }
14349
14350   if (value == 0)
14351     {
14352       debug_rtx (addr);
14353       warning (0, "unable to compute real location of stacked parameter");
14354       value = 8; /* XXX magic hack */
14355     }
14356
14357   return value;
14358 }
14359 \f
14360 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
14361   do                                                                    \
14362     {                                                                   \
14363       if ((MASK) & insn_flags)                                          \
14364         add_builtin_function ((NAME), (TYPE), (CODE),                   \
14365                              BUILT_IN_MD, NULL, NULL_TREE);             \
14366     }                                                                   \
14367   while (0)
14368
14369 struct builtin_description
14370 {
14371   const unsigned int       mask;
14372   const enum insn_code     icode;
14373   const char * const       name;
14374   const enum arm_builtins  code;
14375   const enum rtx_code      comparison;
14376   const unsigned int       flag;
14377 };
14378
14379 static const struct builtin_description bdesc_2arg[] =
14380 {
14381 #define IWMMXT_BUILTIN(code, string, builtin) \
14382   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
14383     ARM_BUILTIN_##builtin, 0, 0 },
14384
14385   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
14386   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
14387   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
14388   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
14389   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
14390   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
14391   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
14392   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
14393   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
14394   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
14395   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
14396   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
14397   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
14398   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
14399   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
14400   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
14401   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
14402   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
14403   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
14404   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
14405   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
14406   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
14407   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
14408   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
14409   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
14410   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
14411   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
14412   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
14413   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
14414   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
14415   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
14416   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
14417   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
14418   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
14419   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
14420   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
14421   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
14422   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
14423   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
14424   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
14425   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
14426   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
14427   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
14428   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
14429   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
14430   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
14431   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
14432   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
14433   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
14434   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
14435   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
14436   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
14437   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
14438   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
14439   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
14440   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
14441   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
14442   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
14443
14444 #define IWMMXT_BUILTIN2(code, builtin) \
14445   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
14446
14447   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
14448   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
14449   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
14450   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
14451   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
14452   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
14453   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
14454   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
14455   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
14456   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
14457   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
14458   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
14459   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
14460   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
14461   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
14462   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
14463   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
14464   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
14465   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
14466   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
14467   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
14468   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
14469   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
14470   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
14471   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
14472   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
14473   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
14474   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
14475   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
14476   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
14477   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
14478   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
14479 };
14480
14481 static const struct builtin_description bdesc_1arg[] =
14482 {
14483   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
14484   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
14485   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
14486   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
14487   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
14488   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
14489   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
14490   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
14491   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
14492   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
14493   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
14494   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
14495   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
14496   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
14497   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
14498   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
14499   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
14500   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
14501 };
14502
14503 /* Set up all the iWMMXt builtins.  This is
14504    not called if TARGET_IWMMXT is zero.  */
14505
14506 static void
14507 arm_init_iwmmxt_builtins (void)
14508 {
14509   const struct builtin_description * d;
14510   size_t i;
14511   tree endlink = void_list_node;
14512
14513   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14514   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14515   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14516
14517   tree int_ftype_int
14518     = build_function_type (integer_type_node,
14519                            tree_cons (NULL_TREE, integer_type_node, endlink));
14520   tree v8qi_ftype_v8qi_v8qi_int
14521     = build_function_type (V8QI_type_node,
14522                            tree_cons (NULL_TREE, V8QI_type_node,
14523                                       tree_cons (NULL_TREE, V8QI_type_node,
14524                                                  tree_cons (NULL_TREE,
14525                                                             integer_type_node,
14526                                                             endlink))));
14527   tree v4hi_ftype_v4hi_int
14528     = build_function_type (V4HI_type_node,
14529                            tree_cons (NULL_TREE, V4HI_type_node,
14530                                       tree_cons (NULL_TREE, integer_type_node,
14531                                                  endlink)));
14532   tree v2si_ftype_v2si_int
14533     = build_function_type (V2SI_type_node,
14534                            tree_cons (NULL_TREE, V2SI_type_node,
14535                                       tree_cons (NULL_TREE, integer_type_node,
14536                                                  endlink)));
14537   tree v2si_ftype_di_di
14538     = build_function_type (V2SI_type_node,
14539                            tree_cons (NULL_TREE, long_long_integer_type_node,
14540                                       tree_cons (NULL_TREE, long_long_integer_type_node,
14541                                                  endlink)));
14542   tree di_ftype_di_int
14543     = build_function_type (long_long_integer_type_node,
14544                            tree_cons (NULL_TREE, long_long_integer_type_node,
14545                                       tree_cons (NULL_TREE, integer_type_node,
14546                                                  endlink)));
14547   tree di_ftype_di_int_int
14548     = build_function_type (long_long_integer_type_node,
14549                            tree_cons (NULL_TREE, long_long_integer_type_node,
14550                                       tree_cons (NULL_TREE, integer_type_node,
14551                                                  tree_cons (NULL_TREE,
14552                                                             integer_type_node,
14553                                                             endlink))));
14554   tree int_ftype_v8qi
14555     = build_function_type (integer_type_node,
14556                            tree_cons (NULL_TREE, V8QI_type_node,
14557                                       endlink));
14558   tree int_ftype_v4hi
14559     = build_function_type (integer_type_node,
14560                            tree_cons (NULL_TREE, V4HI_type_node,
14561                                       endlink));
14562   tree int_ftype_v2si
14563     = build_function_type (integer_type_node,
14564                            tree_cons (NULL_TREE, V2SI_type_node,
14565                                       endlink));
14566   tree int_ftype_v8qi_int
14567     = build_function_type (integer_type_node,
14568                            tree_cons (NULL_TREE, V8QI_type_node,
14569                                       tree_cons (NULL_TREE, integer_type_node,
14570                                                  endlink)));
14571   tree int_ftype_v4hi_int
14572     = build_function_type (integer_type_node,
14573                            tree_cons (NULL_TREE, V4HI_type_node,
14574                                       tree_cons (NULL_TREE, integer_type_node,
14575                                                  endlink)));
14576   tree int_ftype_v2si_int
14577     = build_function_type (integer_type_node,
14578                            tree_cons (NULL_TREE, V2SI_type_node,
14579                                       tree_cons (NULL_TREE, integer_type_node,
14580                                                  endlink)));
14581   tree v8qi_ftype_v8qi_int_int
14582     = build_function_type (V8QI_type_node,
14583                            tree_cons (NULL_TREE, V8QI_type_node,
14584                                       tree_cons (NULL_TREE, integer_type_node,
14585                                                  tree_cons (NULL_TREE,
14586                                                             integer_type_node,
14587                                                             endlink))));
14588   tree v4hi_ftype_v4hi_int_int
14589     = build_function_type (V4HI_type_node,
14590                            tree_cons (NULL_TREE, V4HI_type_node,
14591                                       tree_cons (NULL_TREE, integer_type_node,
14592                                                  tree_cons (NULL_TREE,
14593                                                             integer_type_node,
14594                                                             endlink))));
14595   tree v2si_ftype_v2si_int_int
14596     = build_function_type (V2SI_type_node,
14597                            tree_cons (NULL_TREE, V2SI_type_node,
14598                                       tree_cons (NULL_TREE, integer_type_node,
14599                                                  tree_cons (NULL_TREE,
14600                                                             integer_type_node,
14601                                                             endlink))));
14602   /* Miscellaneous.  */
14603   tree v8qi_ftype_v4hi_v4hi
14604     = build_function_type (V8QI_type_node,
14605                            tree_cons (NULL_TREE, V4HI_type_node,
14606                                       tree_cons (NULL_TREE, V4HI_type_node,
14607                                                  endlink)));
14608   tree v4hi_ftype_v2si_v2si
14609     = build_function_type (V4HI_type_node,
14610                            tree_cons (NULL_TREE, V2SI_type_node,
14611                                       tree_cons (NULL_TREE, V2SI_type_node,
14612                                                  endlink)));
14613   tree v2si_ftype_v4hi_v4hi
14614     = build_function_type (V2SI_type_node,
14615                            tree_cons (NULL_TREE, V4HI_type_node,
14616                                       tree_cons (NULL_TREE, V4HI_type_node,
14617                                                  endlink)));
14618   tree v2si_ftype_v8qi_v8qi
14619     = build_function_type (V2SI_type_node,
14620                            tree_cons (NULL_TREE, V8QI_type_node,
14621                                       tree_cons (NULL_TREE, V8QI_type_node,
14622                                                  endlink)));
14623   tree v4hi_ftype_v4hi_di
14624     = build_function_type (V4HI_type_node,
14625                            tree_cons (NULL_TREE, V4HI_type_node,
14626                                       tree_cons (NULL_TREE,
14627                                                  long_long_integer_type_node,
14628                                                  endlink)));
14629   tree v2si_ftype_v2si_di
14630     = build_function_type (V2SI_type_node,
14631                            tree_cons (NULL_TREE, V2SI_type_node,
14632                                       tree_cons (NULL_TREE,
14633                                                  long_long_integer_type_node,
14634                                                  endlink)));
14635   tree void_ftype_int_int
14636     = build_function_type (void_type_node,
14637                            tree_cons (NULL_TREE, integer_type_node,
14638                                       tree_cons (NULL_TREE, integer_type_node,
14639                                                  endlink)));
14640   tree di_ftype_void
14641     = build_function_type (long_long_unsigned_type_node, endlink);
14642   tree di_ftype_v8qi
14643     = build_function_type (long_long_integer_type_node,
14644                            tree_cons (NULL_TREE, V8QI_type_node,
14645                                       endlink));
14646   tree di_ftype_v4hi
14647     = build_function_type (long_long_integer_type_node,
14648                            tree_cons (NULL_TREE, V4HI_type_node,
14649                                       endlink));
14650   tree di_ftype_v2si
14651     = build_function_type (long_long_integer_type_node,
14652                            tree_cons (NULL_TREE, V2SI_type_node,
14653                                       endlink));
14654   tree v2si_ftype_v4hi
14655     = build_function_type (V2SI_type_node,
14656                            tree_cons (NULL_TREE, V4HI_type_node,
14657                                       endlink));
14658   tree v4hi_ftype_v8qi
14659     = build_function_type (V4HI_type_node,
14660                            tree_cons (NULL_TREE, V8QI_type_node,
14661                                       endlink));
14662
14663   tree di_ftype_di_v4hi_v4hi
14664     = build_function_type (long_long_unsigned_type_node,
14665                            tree_cons (NULL_TREE,
14666                                       long_long_unsigned_type_node,
14667                                       tree_cons (NULL_TREE, V4HI_type_node,
14668                                                  tree_cons (NULL_TREE,
14669                                                             V4HI_type_node,
14670                                                             endlink))));
14671
14672   tree di_ftype_v4hi_v4hi
14673     = build_function_type (long_long_unsigned_type_node,
14674                            tree_cons (NULL_TREE, V4HI_type_node,
14675                                       tree_cons (NULL_TREE, V4HI_type_node,
14676                                                  endlink)));
14677
14678   /* Normal vector binops.  */
14679   tree v8qi_ftype_v8qi_v8qi
14680     = build_function_type (V8QI_type_node,
14681                            tree_cons (NULL_TREE, V8QI_type_node,
14682                                       tree_cons (NULL_TREE, V8QI_type_node,
14683                                                  endlink)));
14684   tree v4hi_ftype_v4hi_v4hi
14685     = build_function_type (V4HI_type_node,
14686                            tree_cons (NULL_TREE, V4HI_type_node,
14687                                       tree_cons (NULL_TREE, V4HI_type_node,
14688                                                  endlink)));
14689   tree v2si_ftype_v2si_v2si
14690     = build_function_type (V2SI_type_node,
14691                            tree_cons (NULL_TREE, V2SI_type_node,
14692                                       tree_cons (NULL_TREE, V2SI_type_node,
14693                                                  endlink)));
14694   tree di_ftype_di_di
14695     = build_function_type (long_long_unsigned_type_node,
14696                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
14697                                       tree_cons (NULL_TREE,
14698                                                  long_long_unsigned_type_node,
14699                                                  endlink)));
14700
14701   /* Add all builtins that are more or less simple operations on two
14702      operands.  */
14703   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14704     {
14705       /* Use one of the operands; the target can have a different mode for
14706          mask-generating compares.  */
14707       enum machine_mode mode;
14708       tree type;
14709
14710       if (d->name == 0)
14711         continue;
14712
14713       mode = insn_data[d->icode].operand[1].mode;
14714
14715       switch (mode)
14716         {
14717         case V8QImode:
14718           type = v8qi_ftype_v8qi_v8qi;
14719           break;
14720         case V4HImode:
14721           type = v4hi_ftype_v4hi_v4hi;
14722           break;
14723         case V2SImode:
14724           type = v2si_ftype_v2si_v2si;
14725           break;
14726         case DImode:
14727           type = di_ftype_di_di;
14728           break;
14729
14730         default:
14731           gcc_unreachable ();
14732         }
14733
14734       def_mbuiltin (d->mask, d->name, type, d->code);
14735     }
14736
14737   /* Add the remaining MMX insns with somewhat more complicated types.  */
14738   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
14739   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
14740   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
14741
14742   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
14743   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
14744   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
14745   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
14746   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
14747   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
14748
14749   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
14750   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
14751   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
14752   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
14753   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
14754   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
14755
14756   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
14757   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
14758   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
14759   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
14760   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
14761   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
14762
14763   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
14764   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
14765   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
14766   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
14767   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
14768   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
14769
14770   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
14771
14772   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
14773   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
14774   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
14775   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
14776
14777   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
14778   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
14779   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
14780   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
14781   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
14782   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
14783   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
14784   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
14785   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
14786
14787   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
14788   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
14789   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
14790
14791   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
14792   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
14793   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
14794
14795   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
14796   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
14797   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
14798   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
14799   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
14800   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
14801
14802   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
14803   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
14804   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
14805   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
14806   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
14807   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
14808   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
14809   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
14810   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
14811   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
14812   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
14813   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
14814
14815   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
14816   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
14817   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
14818   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
14819
14820   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
14821   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
14822   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
14823   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
14824   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
14825   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
14826   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
14827 }
14828
14829 static void
14830 arm_init_tls_builtins (void)
14831 {
14832   tree ftype, decl;
14833
14834   ftype = build_function_type (ptr_type_node, void_list_node);
14835   decl = add_builtin_function ("__builtin_thread_pointer", ftype,
14836                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
14837                                NULL, NULL_TREE);
14838   TREE_NOTHROW (decl) = 1;
14839   TREE_READONLY (decl) = 1;
14840 }
14841
14842 typedef enum {
14843   T_V8QI  = 0x0001,
14844   T_V4HI  = 0x0002,
14845   T_V2SI  = 0x0004,
14846   T_V2SF  = 0x0008,
14847   T_DI    = 0x0010,
14848   T_V16QI = 0x0020,
14849   T_V8HI  = 0x0040,
14850   T_V4SI  = 0x0080,
14851   T_V4SF  = 0x0100,
14852   T_V2DI  = 0x0200,
14853   T_TI    = 0x0400,
14854   T_EI    = 0x0800,
14855   T_OI    = 0x1000
14856 } neon_builtin_type_bits;
14857
14858 #define v8qi_UP  T_V8QI
14859 #define v4hi_UP  T_V4HI
14860 #define v2si_UP  T_V2SI
14861 #define v2sf_UP  T_V2SF
14862 #define di_UP    T_DI
14863 #define v16qi_UP T_V16QI
14864 #define v8hi_UP  T_V8HI
14865 #define v4si_UP  T_V4SI
14866 #define v4sf_UP  T_V4SF
14867 #define v2di_UP  T_V2DI
14868 #define ti_UP    T_TI
14869 #define ei_UP    T_EI
14870 #define oi_UP    T_OI
14871
14872 #define UP(X) X##_UP
14873
14874 #define T_MAX 13
14875
14876 typedef enum {
14877   NEON_BINOP,
14878   NEON_TERNOP,
14879   NEON_UNOP,
14880   NEON_GETLANE,
14881   NEON_SETLANE,
14882   NEON_CREATE,
14883   NEON_DUP,
14884   NEON_DUPLANE,
14885   NEON_COMBINE,
14886   NEON_SPLIT,
14887   NEON_LANEMUL,
14888   NEON_LANEMULL,
14889   NEON_LANEMULH,
14890   NEON_LANEMAC,
14891   NEON_SCALARMUL,
14892   NEON_SCALARMULL,
14893   NEON_SCALARMULH,
14894   NEON_SCALARMAC,
14895   NEON_CONVERT,
14896   NEON_FIXCONV,
14897   NEON_SELECT,
14898   NEON_RESULTPAIR,
14899   NEON_REINTERP,
14900   NEON_VTBL,
14901   NEON_VTBX,
14902   NEON_LOAD1,
14903   NEON_LOAD1LANE,
14904   NEON_STORE1,
14905   NEON_STORE1LANE,
14906   NEON_LOADSTRUCT,
14907   NEON_LOADSTRUCTLANE,
14908   NEON_STORESTRUCT,
14909   NEON_STORESTRUCTLANE,
14910   NEON_LOGICBINOP,
14911   NEON_SHIFTINSERT,
14912   NEON_SHIFTIMM,
14913   NEON_SHIFTACC
14914 } neon_itype;
14915
14916 typedef struct {
14917   const char *name;
14918   const neon_itype itype;
14919   const neon_builtin_type_bits bits;
14920   const enum insn_code codes[T_MAX];
14921   const unsigned int num_vars;
14922   unsigned int base_fcode;
14923 } neon_builtin_datum;
14924
14925 #define CF(N,X) CODE_FOR_neon_##N##X
14926
14927 #define VAR1(T, N, A) \
14928   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
14929 #define VAR2(T, N, A, B) \
14930   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
14931 #define VAR3(T, N, A, B, C) \
14932   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
14933   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
14934 #define VAR4(T, N, A, B, C, D) \
14935   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
14936   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
14937 #define VAR5(T, N, A, B, C, D, E) \
14938   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
14939   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
14940 #define VAR6(T, N, A, B, C, D, E, F) \
14941   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
14942   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
14943 #define VAR7(T, N, A, B, C, D, E, F, G) \
14944   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
14945   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14946     CF (N, G) }, 7, 0
14947 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
14948   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14949                 | UP (H), \
14950   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14951     CF (N, G), CF (N, H) }, 8, 0
14952 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
14953   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14954                 | UP (H) | UP (I), \
14955   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14956     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
14957 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
14958   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14959                 | UP (H) | UP (I) | UP (J), \
14960   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14961     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
14962
14963 /* The mode entries in the following table correspond to the "key" type of the
14964    instruction variant, i.e. equivalent to that which would be specified after
14965    the assembler mnemonic, which usually refers to the last vector operand.
14966    (Signed/unsigned/polynomial types are not differentiated between though, and
14967    are all mapped onto the same mode for a given element size.) The modes
14968    listed per instruction should be the same as those defined for that
14969    instruction's pattern in neon.md.
14970    WARNING: Variants should be listed in the same increasing order as
14971    neon_builtin_type_bits.  */
14972
14973 static neon_builtin_datum neon_builtin_data[] =
14974 {
14975   { VAR10 (BINOP, vadd,
14976            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14977   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
14978   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
14979   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14980   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14981   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
14982   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14983   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14984   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
14985   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14986   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
14987   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
14988   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
14989   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
14990   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
14991   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
14992   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
14993   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
14994   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
14995   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
14996   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
14997   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
14998   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14999   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15000   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15001   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
15002   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
15003   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
15004   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15005   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15006   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15007   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
15008   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15009   { VAR10 (BINOP, vsub,
15010            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15011   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
15012   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
15013   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15014   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15015   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
15016   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15017   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15018   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15019   { VAR2 (BINOP, vcage, v2sf, v4sf) },
15020   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
15021   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15022   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15023   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
15024   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15025   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
15026   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15027   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15028   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
15029   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15030   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15031   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
15032   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
15033   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
15034   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
15035   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15036   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
15037   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15038   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15039   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15040   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15041   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15042   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15043   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
15044   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
15045   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
15046   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
15047   /* FIXME: vget_lane supports more variants than this!  */
15048   { VAR10 (GETLANE, vget_lane,
15049            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15050   { VAR10 (SETLANE, vset_lane,
15051            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15052   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
15053   { VAR10 (DUP, vdup_n,
15054            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15055   { VAR10 (DUPLANE, vdup_lane,
15056            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15057   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
15058   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
15059   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
15060   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
15061   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
15062   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
15063   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
15064   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15065   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15066   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
15067   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
15068   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15069   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
15070   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
15071   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15072   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15073   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
15074   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
15075   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15076   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
15077   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
15078   { VAR10 (BINOP, vext,
15079            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15080   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15081   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
15082   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
15083   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
15084   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
15085   { VAR10 (SELECT, vbsl,
15086            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15087   { VAR1 (VTBL, vtbl1, v8qi) },
15088   { VAR1 (VTBL, vtbl2, v8qi) },
15089   { VAR1 (VTBL, vtbl3, v8qi) },
15090   { VAR1 (VTBL, vtbl4, v8qi) },
15091   { VAR1 (VTBX, vtbx1, v8qi) },
15092   { VAR1 (VTBX, vtbx2, v8qi) },
15093   { VAR1 (VTBX, vtbx3, v8qi) },
15094   { VAR1 (VTBX, vtbx4, v8qi) },
15095   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15096   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15097   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
15098   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
15099   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
15100   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
15101   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
15102   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
15103   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
15104   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
15105   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
15106   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
15107   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
15108   { VAR10 (LOAD1, vld1,
15109            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15110   { VAR10 (LOAD1LANE, vld1_lane,
15111            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15112   { VAR10 (LOAD1, vld1_dup,
15113            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15114   { VAR10 (STORE1, vst1,
15115            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15116   { VAR10 (STORE1LANE, vst1_lane,
15117            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15118   { VAR9 (LOADSTRUCT,
15119           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15120   { VAR7 (LOADSTRUCTLANE, vld2_lane,
15121           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15122   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
15123   { VAR9 (STORESTRUCT, vst2,
15124           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15125   { VAR7 (STORESTRUCTLANE, vst2_lane,
15126           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15127   { VAR9 (LOADSTRUCT,
15128           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15129   { VAR7 (LOADSTRUCTLANE, vld3_lane,
15130           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15131   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
15132   { VAR9 (STORESTRUCT, vst3,
15133           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15134   { VAR7 (STORESTRUCTLANE, vst3_lane,
15135           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15136   { VAR9 (LOADSTRUCT, vld4,
15137           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15138   { VAR7 (LOADSTRUCTLANE, vld4_lane,
15139           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15140   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
15141   { VAR9 (STORESTRUCT, vst4,
15142           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
15143   { VAR7 (STORESTRUCTLANE, vst4_lane,
15144           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
15145   { VAR10 (LOGICBINOP, vand,
15146            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15147   { VAR10 (LOGICBINOP, vorr,
15148            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15149   { VAR10 (BINOP, veor,
15150            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15151   { VAR10 (LOGICBINOP, vbic,
15152            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
15153   { VAR10 (LOGICBINOP, vorn,
15154            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
15155 };
15156
15157 #undef CF
15158 #undef VAR1
15159 #undef VAR2
15160 #undef VAR3
15161 #undef VAR4
15162 #undef VAR5
15163 #undef VAR6
15164 #undef VAR7
15165 #undef VAR8
15166 #undef VAR9
15167 #undef VAR10
15168
15169 static void
15170 arm_init_neon_builtins (void)
15171 {
15172   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
15173
15174   tree neon_intQI_type_node;
15175   tree neon_intHI_type_node;
15176   tree neon_polyQI_type_node;
15177   tree neon_polyHI_type_node;
15178   tree neon_intSI_type_node;
15179   tree neon_intDI_type_node;
15180   tree neon_float_type_node;
15181
15182   tree intQI_pointer_node;
15183   tree intHI_pointer_node;
15184   tree intSI_pointer_node;
15185   tree intDI_pointer_node;
15186   tree float_pointer_node;
15187
15188   tree const_intQI_node;
15189   tree const_intHI_node;
15190   tree const_intSI_node;
15191   tree const_intDI_node;
15192   tree const_float_node;
15193
15194   tree const_intQI_pointer_node;
15195   tree const_intHI_pointer_node;
15196   tree const_intSI_pointer_node;
15197   tree const_intDI_pointer_node;
15198   tree const_float_pointer_node;
15199
15200   tree V8QI_type_node;
15201   tree V4HI_type_node;
15202   tree V2SI_type_node;
15203   tree V2SF_type_node;
15204   tree V16QI_type_node;
15205   tree V8HI_type_node;
15206   tree V4SI_type_node;
15207   tree V4SF_type_node;
15208   tree V2DI_type_node;
15209
15210   tree intUQI_type_node;
15211   tree intUHI_type_node;
15212   tree intUSI_type_node;
15213   tree intUDI_type_node;
15214
15215   tree intEI_type_node;
15216   tree intOI_type_node;
15217   tree intCI_type_node;
15218   tree intXI_type_node;
15219
15220   tree V8QI_pointer_node;
15221   tree V4HI_pointer_node;
15222   tree V2SI_pointer_node;
15223   tree V2SF_pointer_node;
15224   tree V16QI_pointer_node;
15225   tree V8HI_pointer_node;
15226   tree V4SI_pointer_node;
15227   tree V4SF_pointer_node;
15228   tree V2DI_pointer_node;
15229
15230   tree void_ftype_pv8qi_v8qi_v8qi;
15231   tree void_ftype_pv4hi_v4hi_v4hi;
15232   tree void_ftype_pv2si_v2si_v2si;
15233   tree void_ftype_pv2sf_v2sf_v2sf;
15234   tree void_ftype_pdi_di_di;
15235   tree void_ftype_pv16qi_v16qi_v16qi;
15236   tree void_ftype_pv8hi_v8hi_v8hi;
15237   tree void_ftype_pv4si_v4si_v4si;
15238   tree void_ftype_pv4sf_v4sf_v4sf;
15239   tree void_ftype_pv2di_v2di_v2di;
15240
15241   tree reinterp_ftype_dreg[5][5];
15242   tree reinterp_ftype_qreg[5][5];
15243   tree dreg_types[5], qreg_types[5];
15244
15245   /* Create distinguished type nodes for NEON vector element types,
15246      and pointers to values of such types, so we can detect them later.  */
15247   neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15248   neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15249   neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
15250   neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
15251   neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
15252   neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
15253   neon_float_type_node = make_node (REAL_TYPE);
15254   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
15255   layout_type (neon_float_type_node);
15256
15257   /* Define typedefs which exactly correspond to the modes we are basing vector
15258      types on.  If you change these names you'll need to change
15259      the table used by arm_mangle_type too.  */
15260   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
15261                                              "__builtin_neon_qi");
15262   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
15263                                              "__builtin_neon_hi");
15264   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
15265                                              "__builtin_neon_si");
15266   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
15267                                              "__builtin_neon_sf");
15268   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
15269                                              "__builtin_neon_di");
15270   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
15271                                              "__builtin_neon_poly8");
15272   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
15273                                              "__builtin_neon_poly16");
15274
15275   intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
15276   intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
15277   intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
15278   intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
15279   float_pointer_node = build_pointer_type (neon_float_type_node);
15280
15281   /* Next create constant-qualified versions of the above types.  */
15282   const_intQI_node = build_qualified_type (neon_intQI_type_node,
15283                                            TYPE_QUAL_CONST);
15284   const_intHI_node = build_qualified_type (neon_intHI_type_node,
15285                                            TYPE_QUAL_CONST);
15286   const_intSI_node = build_qualified_type (neon_intSI_type_node,
15287                                            TYPE_QUAL_CONST);
15288   const_intDI_node = build_qualified_type (neon_intDI_type_node,
15289                                            TYPE_QUAL_CONST);
15290   const_float_node = build_qualified_type (neon_float_type_node,
15291                                            TYPE_QUAL_CONST);
15292
15293   const_intQI_pointer_node = build_pointer_type (const_intQI_node);
15294   const_intHI_pointer_node = build_pointer_type (const_intHI_node);
15295   const_intSI_pointer_node = build_pointer_type (const_intSI_node);
15296   const_intDI_pointer_node = build_pointer_type (const_intDI_node);
15297   const_float_pointer_node = build_pointer_type (const_float_node);
15298
15299   /* Now create vector types based on our NEON element types.  */
15300   /* 64-bit vectors.  */
15301   V8QI_type_node =
15302     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
15303   V4HI_type_node =
15304     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
15305   V2SI_type_node =
15306     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
15307   V2SF_type_node =
15308     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
15309   /* 128-bit vectors.  */
15310   V16QI_type_node =
15311     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
15312   V8HI_type_node =
15313     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
15314   V4SI_type_node =
15315     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
15316   V4SF_type_node =
15317     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
15318   V2DI_type_node =
15319     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
15320
15321   /* Unsigned integer types for various mode sizes.  */
15322   intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
15323   intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
15324   intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
15325   intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
15326
15327   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
15328                                              "__builtin_neon_uqi");
15329   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
15330                                              "__builtin_neon_uhi");
15331   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
15332                                              "__builtin_neon_usi");
15333   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
15334                                              "__builtin_neon_udi");
15335
15336   /* Opaque integer types for structures of vectors.  */
15337   intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
15338   intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
15339   intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
15340   intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
15341
15342   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
15343                                              "__builtin_neon_ti");
15344   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
15345                                              "__builtin_neon_ei");
15346   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
15347                                              "__builtin_neon_oi");
15348   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
15349                                              "__builtin_neon_ci");
15350   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
15351                                              "__builtin_neon_xi");
15352
15353   /* Pointers to vector types.  */
15354   V8QI_pointer_node = build_pointer_type (V8QI_type_node);
15355   V4HI_pointer_node = build_pointer_type (V4HI_type_node);
15356   V2SI_pointer_node = build_pointer_type (V2SI_type_node);
15357   V2SF_pointer_node = build_pointer_type (V2SF_type_node);
15358   V16QI_pointer_node = build_pointer_type (V16QI_type_node);
15359   V8HI_pointer_node = build_pointer_type (V8HI_type_node);
15360   V4SI_pointer_node = build_pointer_type (V4SI_type_node);
15361   V4SF_pointer_node = build_pointer_type (V4SF_type_node);
15362   V2DI_pointer_node = build_pointer_type (V2DI_type_node);
15363
15364   /* Operations which return results as pairs.  */
15365   void_ftype_pv8qi_v8qi_v8qi =
15366     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
15367                               V8QI_type_node, NULL);
15368   void_ftype_pv4hi_v4hi_v4hi =
15369     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
15370                               V4HI_type_node, NULL);
15371   void_ftype_pv2si_v2si_v2si =
15372     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
15373                               V2SI_type_node, NULL);
15374   void_ftype_pv2sf_v2sf_v2sf =
15375     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
15376                               V2SF_type_node, NULL);
15377   void_ftype_pdi_di_di =
15378     build_function_type_list (void_type_node, intDI_pointer_node,
15379                               neon_intDI_type_node, neon_intDI_type_node, NULL);
15380   void_ftype_pv16qi_v16qi_v16qi =
15381     build_function_type_list (void_type_node, V16QI_pointer_node,
15382                               V16QI_type_node, V16QI_type_node, NULL);
15383   void_ftype_pv8hi_v8hi_v8hi =
15384     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
15385                               V8HI_type_node, NULL);
15386   void_ftype_pv4si_v4si_v4si =
15387     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
15388                               V4SI_type_node, NULL);
15389   void_ftype_pv4sf_v4sf_v4sf =
15390     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
15391                               V4SF_type_node, NULL);
15392   void_ftype_pv2di_v2di_v2di =
15393     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
15394                               V2DI_type_node, NULL);
15395
15396   dreg_types[0] = V8QI_type_node;
15397   dreg_types[1] = V4HI_type_node;
15398   dreg_types[2] = V2SI_type_node;
15399   dreg_types[3] = V2SF_type_node;
15400   dreg_types[4] = neon_intDI_type_node;
15401
15402   qreg_types[0] = V16QI_type_node;
15403   qreg_types[1] = V8HI_type_node;
15404   qreg_types[2] = V4SI_type_node;
15405   qreg_types[3] = V4SF_type_node;
15406   qreg_types[4] = V2DI_type_node;
15407
15408   for (i = 0; i < 5; i++)
15409     {
15410       int j;
15411       for (j = 0; j < 5; j++)
15412         {
15413           reinterp_ftype_dreg[i][j]
15414             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
15415           reinterp_ftype_qreg[i][j]
15416             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
15417         }
15418     }
15419
15420   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
15421     {
15422       neon_builtin_datum *d = &neon_builtin_data[i];
15423       unsigned int j, codeidx = 0;
15424
15425       d->base_fcode = fcode;
15426
15427       for (j = 0; j < T_MAX; j++)
15428         {
15429           const char* const modenames[] = {
15430             "v8qi", "v4hi", "v2si", "v2sf", "di",
15431             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
15432           };
15433           char namebuf[60];
15434           tree ftype = NULL;
15435           enum insn_code icode;
15436           int is_load = 0, is_store = 0;
15437
15438           if ((d->bits & (1 << j)) == 0)
15439             continue;
15440
15441           icode = d->codes[codeidx++];
15442
15443           switch (d->itype)
15444             {
15445             case NEON_LOAD1:
15446             case NEON_LOAD1LANE:
15447             case NEON_LOADSTRUCT:
15448             case NEON_LOADSTRUCTLANE:
15449               is_load = 1;
15450               /* Fall through.  */
15451             case NEON_STORE1:
15452             case NEON_STORE1LANE:
15453             case NEON_STORESTRUCT:
15454             case NEON_STORESTRUCTLANE:
15455               if (!is_load)
15456                 is_store = 1;
15457               /* Fall through.  */
15458             case NEON_UNOP:
15459             case NEON_BINOP:
15460             case NEON_LOGICBINOP:
15461             case NEON_SHIFTINSERT:
15462             case NEON_TERNOP:
15463             case NEON_GETLANE:
15464             case NEON_SETLANE:
15465             case NEON_CREATE:
15466             case NEON_DUP:
15467             case NEON_DUPLANE:
15468             case NEON_SHIFTIMM:
15469             case NEON_SHIFTACC:
15470             case NEON_COMBINE:
15471             case NEON_SPLIT:
15472             case NEON_CONVERT:
15473             case NEON_FIXCONV:
15474             case NEON_LANEMUL:
15475             case NEON_LANEMULL:
15476             case NEON_LANEMULH:
15477             case NEON_LANEMAC:
15478             case NEON_SCALARMUL:
15479             case NEON_SCALARMULL:
15480             case NEON_SCALARMULH:
15481             case NEON_SCALARMAC:
15482             case NEON_SELECT:
15483             case NEON_VTBL:
15484             case NEON_VTBX:
15485               {
15486                 int k;
15487                 tree return_type = void_type_node, args = void_list_node;
15488
15489                 /* Build a function type directly from the insn_data for this
15490                    builtin.  The build_function_type() function takes care of
15491                    removing duplicates for us.  */
15492                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
15493                   {
15494                     tree eltype;
15495
15496                     if (is_load && k == 1)
15497                       {
15498                         /* Neon load patterns always have the memory operand
15499                            (a SImode pointer) in the operand 1 position.  We
15500                            want a const pointer to the element type in that
15501                            position.  */
15502                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
15503
15504                         switch (1 << j)
15505                           {
15506                           case T_V8QI:
15507                           case T_V16QI:
15508                             eltype = const_intQI_pointer_node;
15509                             break;
15510
15511                           case T_V4HI:
15512                           case T_V8HI:
15513                             eltype = const_intHI_pointer_node;
15514                             break;
15515
15516                           case T_V2SI:
15517                           case T_V4SI:
15518                             eltype = const_intSI_pointer_node;
15519                             break;
15520
15521                           case T_V2SF:
15522                           case T_V4SF:
15523                             eltype = const_float_pointer_node;
15524                             break;
15525
15526                           case T_DI:
15527                           case T_V2DI:
15528                             eltype = const_intDI_pointer_node;
15529                             break;
15530
15531                           default: gcc_unreachable ();
15532                           }
15533                       }
15534                     else if (is_store && k == 0)
15535                       {
15536                         /* Similarly, Neon store patterns use operand 0 as
15537                            the memory location to store to (a SImode pointer).
15538                            Use a pointer to the element type of the store in
15539                            that position.  */
15540                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
15541
15542                         switch (1 << j)
15543                           {
15544                           case T_V8QI:
15545                           case T_V16QI:
15546                             eltype = intQI_pointer_node;
15547                             break;
15548
15549                           case T_V4HI:
15550                           case T_V8HI:
15551                             eltype = intHI_pointer_node;
15552                             break;
15553
15554                           case T_V2SI:
15555                           case T_V4SI:
15556                             eltype = intSI_pointer_node;
15557                             break;
15558
15559                           case T_V2SF:
15560                           case T_V4SF:
15561                             eltype = float_pointer_node;
15562                             break;
15563
15564                           case T_DI:
15565                           case T_V2DI:
15566                             eltype = intDI_pointer_node;
15567                             break;
15568
15569                           default: gcc_unreachable ();
15570                           }
15571                       }
15572                     else
15573                       {
15574                         switch (insn_data[icode].operand[k].mode)
15575                           {
15576                           case VOIDmode: eltype = void_type_node; break;
15577                           /* Scalars.  */
15578                           case QImode: eltype = neon_intQI_type_node; break;
15579                           case HImode: eltype = neon_intHI_type_node; break;
15580                           case SImode: eltype = neon_intSI_type_node; break;
15581                           case SFmode: eltype = neon_float_type_node; break;
15582                           case DImode: eltype = neon_intDI_type_node; break;
15583                           case TImode: eltype = intTI_type_node; break;
15584                           case EImode: eltype = intEI_type_node; break;
15585                           case OImode: eltype = intOI_type_node; break;
15586                           case CImode: eltype = intCI_type_node; break;
15587                           case XImode: eltype = intXI_type_node; break;
15588                           /* 64-bit vectors.  */
15589                           case V8QImode: eltype = V8QI_type_node; break;
15590                           case V4HImode: eltype = V4HI_type_node; break;
15591                           case V2SImode: eltype = V2SI_type_node; break;
15592                           case V2SFmode: eltype = V2SF_type_node; break;
15593                           /* 128-bit vectors.  */
15594                           case V16QImode: eltype = V16QI_type_node; break;
15595                           case V8HImode: eltype = V8HI_type_node; break;
15596                           case V4SImode: eltype = V4SI_type_node; break;
15597                           case V4SFmode: eltype = V4SF_type_node; break;
15598                           case V2DImode: eltype = V2DI_type_node; break;
15599                           default: gcc_unreachable ();
15600                           }
15601                       }
15602
15603                     if (k == 0 && !is_store)
15604                       return_type = eltype;
15605                     else
15606                       args = tree_cons (NULL_TREE, eltype, args);
15607                   }
15608
15609                 ftype = build_function_type (return_type, args);
15610               }
15611               break;
15612
15613             case NEON_RESULTPAIR:
15614               {
15615                 switch (insn_data[icode].operand[1].mode)
15616                   {
15617                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
15618                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
15619                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
15620                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
15621                   case DImode: ftype = void_ftype_pdi_di_di; break;
15622                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
15623                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
15624                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
15625                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
15626                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
15627                   default: gcc_unreachable ();
15628                   }
15629               }
15630               break;
15631
15632             case NEON_REINTERP:
15633               {
15634                 /* We iterate over 5 doubleword types, then 5 quadword
15635                    types.  */
15636                 int rhs = j % 5;
15637                 switch (insn_data[icode].operand[0].mode)
15638                   {
15639                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
15640                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
15641                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
15642                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
15643                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
15644                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
15645                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
15646                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
15647                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
15648                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
15649                   default: gcc_unreachable ();
15650                   }
15651               }
15652               break;
15653
15654             default:
15655               gcc_unreachable ();
15656             }
15657
15658           gcc_assert (ftype != NULL);
15659
15660           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
15661
15662           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
15663                                 NULL_TREE);
15664         }
15665     }
15666 }
15667
15668 static void
15669 arm_init_builtins (void)
15670 {
15671   arm_init_tls_builtins ();
15672
15673   if (TARGET_REALLY_IWMMXT)
15674     arm_init_iwmmxt_builtins ();
15675
15676   if (TARGET_NEON)
15677     arm_init_neon_builtins ();
15678 }
15679
15680 /* Errors in the source file can cause expand_expr to return const0_rtx
15681    where we expect a vector.  To avoid crashing, use one of the vector
15682    clear instructions.  */
15683
15684 static rtx
15685 safe_vector_operand (rtx x, enum machine_mode mode)
15686 {
15687   if (x != const0_rtx)
15688     return x;
15689   x = gen_reg_rtx (mode);
15690
15691   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
15692                                : gen_rtx_SUBREG (DImode, x, 0)));
15693   return x;
15694 }
15695
15696 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
15697
15698 static rtx
15699 arm_expand_binop_builtin (enum insn_code icode,
15700                           tree exp, rtx target)
15701 {
15702   rtx pat;
15703   tree arg0 = CALL_EXPR_ARG (exp, 0);
15704   tree arg1 = CALL_EXPR_ARG (exp, 1);
15705   rtx op0 = expand_normal (arg0);
15706   rtx op1 = expand_normal (arg1);
15707   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15708   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15709   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
15710
15711   if (VECTOR_MODE_P (mode0))
15712     op0 = safe_vector_operand (op0, mode0);
15713   if (VECTOR_MODE_P (mode1))
15714     op1 = safe_vector_operand (op1, mode1);
15715
15716   if (! target
15717       || GET_MODE (target) != tmode
15718       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15719     target = gen_reg_rtx (tmode);
15720
15721   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
15722
15723   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15724     op0 = copy_to_mode_reg (mode0, op0);
15725   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15726     op1 = copy_to_mode_reg (mode1, op1);
15727
15728   pat = GEN_FCN (icode) (target, op0, op1);
15729   if (! pat)
15730     return 0;
15731   emit_insn (pat);
15732   return target;
15733 }
15734
15735 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
15736
15737 static rtx
15738 arm_expand_unop_builtin (enum insn_code icode,
15739                          tree exp, rtx target, int do_load)
15740 {
15741   rtx pat;
15742   tree arg0 = CALL_EXPR_ARG (exp, 0);
15743   rtx op0 = expand_normal (arg0);
15744   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15745   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15746
15747   if (! target
15748       || GET_MODE (target) != tmode
15749       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15750     target = gen_reg_rtx (tmode);
15751   if (do_load)
15752     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15753   else
15754     {
15755       if (VECTOR_MODE_P (mode0))
15756         op0 = safe_vector_operand (op0, mode0);
15757
15758       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15759         op0 = copy_to_mode_reg (mode0, op0);
15760     }
15761
15762   pat = GEN_FCN (icode) (target, op0);
15763   if (! pat)
15764     return 0;
15765   emit_insn (pat);
15766   return target;
15767 }
15768
15769 static int
15770 neon_builtin_compare (const void *a, const void *b)
15771 {
15772   const neon_builtin_datum *const key = (const neon_builtin_datum *) a;
15773   const neon_builtin_datum *const memb = (const neon_builtin_datum *) b;
15774   unsigned int soughtcode = key->base_fcode;
15775
15776   if (soughtcode >= memb->base_fcode
15777       && soughtcode < memb->base_fcode + memb->num_vars)
15778     return 0;
15779   else if (soughtcode < memb->base_fcode)
15780     return -1;
15781   else
15782     return 1;
15783 }
15784
15785 static enum insn_code
15786 locate_neon_builtin_icode (int fcode, neon_itype *itype)
15787 {
15788   neon_builtin_datum key, *found;
15789   int idx;
15790
15791   key.base_fcode = fcode;
15792   found = (neon_builtin_datum *)
15793     bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
15794                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
15795   gcc_assert (found);
15796   idx = fcode - (int) found->base_fcode;
15797   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
15798
15799   if (itype)
15800     *itype = found->itype;
15801
15802   return found->codes[idx];
15803 }
15804
15805 typedef enum {
15806   NEON_ARG_COPY_TO_REG,
15807   NEON_ARG_CONSTANT,
15808   NEON_ARG_STOP
15809 } builtin_arg;
15810
15811 #define NEON_MAX_BUILTIN_ARGS 5
15812
15813 /* Expand a Neon builtin.  */
15814 static rtx
15815 arm_expand_neon_args (rtx target, int icode, int have_retval,
15816                       tree exp, ...)
15817 {
15818   va_list ap;
15819   rtx pat;
15820   tree arg[NEON_MAX_BUILTIN_ARGS];
15821   rtx op[NEON_MAX_BUILTIN_ARGS];
15822   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15823   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
15824   int argc = 0;
15825
15826   if (have_retval
15827       && (!target
15828           || GET_MODE (target) != tmode
15829           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
15830     target = gen_reg_rtx (tmode);
15831
15832   va_start (ap, exp);
15833
15834   for (;;)
15835     {
15836       builtin_arg thisarg = va_arg (ap, int);
15837
15838       if (thisarg == NEON_ARG_STOP)
15839         break;
15840       else
15841         {
15842           arg[argc] = CALL_EXPR_ARG (exp, argc);
15843           op[argc] = expand_normal (arg[argc]);
15844           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
15845
15846           switch (thisarg)
15847             {
15848             case NEON_ARG_COPY_TO_REG:
15849               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
15850               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
15851                      (op[argc], mode[argc]))
15852                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
15853               break;
15854
15855             case NEON_ARG_CONSTANT:
15856               /* FIXME: This error message is somewhat unhelpful.  */
15857               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
15858                     (op[argc], mode[argc]))
15859                 error ("argument must be a constant");
15860               break;
15861
15862             case NEON_ARG_STOP:
15863               gcc_unreachable ();
15864             }
15865
15866           argc++;
15867         }
15868     }
15869
15870   va_end (ap);
15871
15872   if (have_retval)
15873     switch (argc)
15874       {
15875       case 1:
15876         pat = GEN_FCN (icode) (target, op[0]);
15877         break;
15878
15879       case 2:
15880         pat = GEN_FCN (icode) (target, op[0], op[1]);
15881         break;
15882
15883       case 3:
15884         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
15885         break;
15886
15887       case 4:
15888         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
15889         break;
15890
15891       case 5:
15892         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
15893         break;
15894
15895       default:
15896         gcc_unreachable ();
15897       }
15898   else
15899     switch (argc)
15900       {
15901       case 1:
15902         pat = GEN_FCN (icode) (op[0]);
15903         break;
15904
15905       case 2:
15906         pat = GEN_FCN (icode) (op[0], op[1]);
15907         break;
15908
15909       case 3:
15910         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
15911         break;
15912
15913       case 4:
15914         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
15915         break;
15916
15917       case 5:
15918         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
15919         break;
15920
15921       default:
15922         gcc_unreachable ();
15923       }
15924
15925   if (!pat)
15926     return 0;
15927
15928   emit_insn (pat);
15929
15930   return target;
15931 }
15932
15933 /* Expand a Neon builtin. These are "special" because they don't have symbolic
15934    constants defined per-instruction or per instruction-variant. Instead, the
15935    required info is looked up in the table neon_builtin_data.  */
15936 static rtx
15937 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
15938 {
15939   neon_itype itype;
15940   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
15941
15942   switch (itype)
15943     {
15944     case NEON_UNOP:
15945     case NEON_CONVERT:
15946     case NEON_DUPLANE:
15947       return arm_expand_neon_args (target, icode, 1, exp,
15948         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
15949
15950     case NEON_BINOP:
15951     case NEON_SETLANE:
15952     case NEON_SCALARMUL:
15953     case NEON_SCALARMULL:
15954     case NEON_SCALARMULH:
15955     case NEON_SHIFTINSERT:
15956     case NEON_LOGICBINOP:
15957       return arm_expand_neon_args (target, icode, 1, exp,
15958         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15959         NEON_ARG_STOP);
15960
15961     case NEON_TERNOP:
15962       return arm_expand_neon_args (target, icode, 1, exp,
15963         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15964         NEON_ARG_CONSTANT, NEON_ARG_STOP);
15965
15966     case NEON_GETLANE:
15967     case NEON_FIXCONV:
15968     case NEON_SHIFTIMM:
15969       return arm_expand_neon_args (target, icode, 1, exp,
15970         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
15971         NEON_ARG_STOP);
15972
15973     case NEON_CREATE:
15974       return arm_expand_neon_args (target, icode, 1, exp,
15975         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15976
15977     case NEON_DUP:
15978     case NEON_SPLIT:
15979     case NEON_REINTERP:
15980       return arm_expand_neon_args (target, icode, 1, exp,
15981         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15982
15983     case NEON_COMBINE:
15984     case NEON_VTBL:
15985       return arm_expand_neon_args (target, icode, 1, exp,
15986         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15987
15988     case NEON_RESULTPAIR:
15989       return arm_expand_neon_args (target, icode, 0, exp,
15990         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15991         NEON_ARG_STOP);
15992
15993     case NEON_LANEMUL:
15994     case NEON_LANEMULL:
15995     case NEON_LANEMULH:
15996       return arm_expand_neon_args (target, icode, 1, exp,
15997         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15998         NEON_ARG_CONSTANT, NEON_ARG_STOP);
15999
16000     case NEON_LANEMAC:
16001       return arm_expand_neon_args (target, icode, 1, exp,
16002         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16003         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
16004
16005     case NEON_SHIFTACC:
16006       return arm_expand_neon_args (target, icode, 1, exp,
16007         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16008         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16009
16010     case NEON_SCALARMAC:
16011       return arm_expand_neon_args (target, icode, 1, exp,
16012         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16013         NEON_ARG_CONSTANT, NEON_ARG_STOP);
16014
16015     case NEON_SELECT:
16016     case NEON_VTBX:
16017       return arm_expand_neon_args (target, icode, 1, exp,
16018         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
16019         NEON_ARG_STOP);
16020
16021     case NEON_LOAD1:
16022     case NEON_LOADSTRUCT:
16023       return arm_expand_neon_args (target, icode, 1, exp,
16024         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16025
16026     case NEON_LOAD1LANE:
16027     case NEON_LOADSTRUCTLANE:
16028       return arm_expand_neon_args (target, icode, 1, exp,
16029         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16030         NEON_ARG_STOP);
16031
16032     case NEON_STORE1:
16033     case NEON_STORESTRUCT:
16034       return arm_expand_neon_args (target, icode, 0, exp,
16035         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
16036
16037     case NEON_STORE1LANE:
16038     case NEON_STORESTRUCTLANE:
16039       return arm_expand_neon_args (target, icode, 0, exp,
16040         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
16041         NEON_ARG_STOP);
16042     }
16043
16044   gcc_unreachable ();
16045 }
16046
16047 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
16048 void
16049 neon_reinterpret (rtx dest, rtx src)
16050 {
16051   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
16052 }
16053
16054 /* Emit code to place a Neon pair result in memory locations (with equal
16055    registers).  */
16056 void
16057 neon_emit_pair_result_insn (enum machine_mode mode,
16058                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
16059                             rtx op1, rtx op2)
16060 {
16061   rtx mem = gen_rtx_MEM (mode, destaddr);
16062   rtx tmp1 = gen_reg_rtx (mode);
16063   rtx tmp2 = gen_reg_rtx (mode);
16064
16065   emit_insn (intfn (tmp1, op1, tmp2, op2));
16066
16067   emit_move_insn (mem, tmp1);
16068   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
16069   emit_move_insn (mem, tmp2);
16070 }
16071
16072 /* Set up operands for a register copy from src to dest, taking care not to
16073    clobber registers in the process.
16074    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
16075    be called with a large N, so that should be OK.  */
16076
16077 void
16078 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
16079 {
16080   unsigned int copied = 0, opctr = 0;
16081   unsigned int done = (1 << count) - 1;
16082   unsigned int i, j;
16083
16084   while (copied != done)
16085     {
16086       for (i = 0; i < count; i++)
16087         {
16088           int good = 1;
16089
16090           for (j = 0; good && j < count; j++)
16091             if (i != j && (copied & (1 << j)) == 0
16092                 && reg_overlap_mentioned_p (src[j], dest[i]))
16093               good = 0;
16094
16095           if (good)
16096             {
16097               operands[opctr++] = dest[i];
16098               operands[opctr++] = src[i];
16099               copied |= 1 << i;
16100             }
16101         }
16102     }
16103
16104   gcc_assert (opctr == count * 2);
16105 }
16106
16107 /* Expand an expression EXP that calls a built-in function,
16108    with result going to TARGET if that's convenient
16109    (and in mode MODE if that's convenient).
16110    SUBTARGET may be used as the target for computing one of EXP's operands.
16111    IGNORE is nonzero if the value is to be ignored.  */
16112
16113 static rtx
16114 arm_expand_builtin (tree exp,
16115                     rtx target,
16116                     rtx subtarget ATTRIBUTE_UNUSED,
16117                     enum machine_mode mode ATTRIBUTE_UNUSED,
16118                     int ignore ATTRIBUTE_UNUSED)
16119 {
16120   const struct builtin_description * d;
16121   enum insn_code    icode;
16122   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
16123   tree              arg0;
16124   tree              arg1;
16125   tree              arg2;
16126   rtx               op0;
16127   rtx               op1;
16128   rtx               op2;
16129   rtx               pat;
16130   int               fcode = DECL_FUNCTION_CODE (fndecl);
16131   size_t            i;
16132   enum machine_mode tmode;
16133   enum machine_mode mode0;
16134   enum machine_mode mode1;
16135   enum machine_mode mode2;
16136
16137   if (fcode >= ARM_BUILTIN_NEON_BASE)
16138     return arm_expand_neon_builtin (fcode, exp, target);
16139
16140   switch (fcode)
16141     {
16142     case ARM_BUILTIN_TEXTRMSB:
16143     case ARM_BUILTIN_TEXTRMUB:
16144     case ARM_BUILTIN_TEXTRMSH:
16145     case ARM_BUILTIN_TEXTRMUH:
16146     case ARM_BUILTIN_TEXTRMSW:
16147     case ARM_BUILTIN_TEXTRMUW:
16148       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
16149                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
16150                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
16151                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
16152                : CODE_FOR_iwmmxt_textrmw);
16153
16154       arg0 = CALL_EXPR_ARG (exp, 0);
16155       arg1 = CALL_EXPR_ARG (exp, 1);
16156       op0 = expand_normal (arg0);
16157       op1 = expand_normal (arg1);
16158       tmode = insn_data[icode].operand[0].mode;
16159       mode0 = insn_data[icode].operand[1].mode;
16160       mode1 = insn_data[icode].operand[2].mode;
16161
16162       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16163         op0 = copy_to_mode_reg (mode0, op0);
16164       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16165         {
16166           /* @@@ better error message */
16167           error ("selector must be an immediate");
16168           return gen_reg_rtx (tmode);
16169         }
16170       if (target == 0
16171           || GET_MODE (target) != tmode
16172           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16173         target = gen_reg_rtx (tmode);
16174       pat = GEN_FCN (icode) (target, op0, op1);
16175       if (! pat)
16176         return 0;
16177       emit_insn (pat);
16178       return target;
16179
16180     case ARM_BUILTIN_TINSRB:
16181     case ARM_BUILTIN_TINSRH:
16182     case ARM_BUILTIN_TINSRW:
16183       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
16184                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
16185                : CODE_FOR_iwmmxt_tinsrw);
16186       arg0 = CALL_EXPR_ARG (exp, 0);
16187       arg1 = CALL_EXPR_ARG (exp, 1);
16188       arg2 = CALL_EXPR_ARG (exp, 2);
16189       op0 = expand_normal (arg0);
16190       op1 = expand_normal (arg1);
16191       op2 = expand_normal (arg2);
16192       tmode = insn_data[icode].operand[0].mode;
16193       mode0 = insn_data[icode].operand[1].mode;
16194       mode1 = insn_data[icode].operand[2].mode;
16195       mode2 = insn_data[icode].operand[3].mode;
16196
16197       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16198         op0 = copy_to_mode_reg (mode0, op0);
16199       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16200         op1 = copy_to_mode_reg (mode1, op1);
16201       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16202         {
16203           /* @@@ better error message */
16204           error ("selector must be an immediate");
16205           return const0_rtx;
16206         }
16207       if (target == 0
16208           || GET_MODE (target) != tmode
16209           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16210         target = gen_reg_rtx (tmode);
16211       pat = GEN_FCN (icode) (target, op0, op1, op2);
16212       if (! pat)
16213         return 0;
16214       emit_insn (pat);
16215       return target;
16216
16217     case ARM_BUILTIN_SETWCX:
16218       arg0 = CALL_EXPR_ARG (exp, 0);
16219       arg1 = CALL_EXPR_ARG (exp, 1);
16220       op0 = force_reg (SImode, expand_normal (arg0));
16221       op1 = expand_normal (arg1);
16222       emit_insn (gen_iwmmxt_tmcr (op1, op0));
16223       return 0;
16224
16225     case ARM_BUILTIN_GETWCX:
16226       arg0 = CALL_EXPR_ARG (exp, 0);
16227       op0 = expand_normal (arg0);
16228       target = gen_reg_rtx (SImode);
16229       emit_insn (gen_iwmmxt_tmrc (target, op0));
16230       return target;
16231
16232     case ARM_BUILTIN_WSHUFH:
16233       icode = CODE_FOR_iwmmxt_wshufh;
16234       arg0 = CALL_EXPR_ARG (exp, 0);
16235       arg1 = CALL_EXPR_ARG (exp, 1);
16236       op0 = expand_normal (arg0);
16237       op1 = expand_normal (arg1);
16238       tmode = insn_data[icode].operand[0].mode;
16239       mode1 = insn_data[icode].operand[1].mode;
16240       mode2 = insn_data[icode].operand[2].mode;
16241
16242       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16243         op0 = copy_to_mode_reg (mode1, op0);
16244       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16245         {
16246           /* @@@ better error message */
16247           error ("mask must be an immediate");
16248           return const0_rtx;
16249         }
16250       if (target == 0
16251           || GET_MODE (target) != tmode
16252           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16253         target = gen_reg_rtx (tmode);
16254       pat = GEN_FCN (icode) (target, op0, op1);
16255       if (! pat)
16256         return 0;
16257       emit_insn (pat);
16258       return target;
16259
16260     case ARM_BUILTIN_WSADB:
16261       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
16262     case ARM_BUILTIN_WSADH:
16263       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
16264     case ARM_BUILTIN_WSADBZ:
16265       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
16266     case ARM_BUILTIN_WSADHZ:
16267       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
16268
16269       /* Several three-argument builtins.  */
16270     case ARM_BUILTIN_WMACS:
16271     case ARM_BUILTIN_WMACU:
16272     case ARM_BUILTIN_WALIGN:
16273     case ARM_BUILTIN_TMIA:
16274     case ARM_BUILTIN_TMIAPH:
16275     case ARM_BUILTIN_TMIATT:
16276     case ARM_BUILTIN_TMIATB:
16277     case ARM_BUILTIN_TMIABT:
16278     case ARM_BUILTIN_TMIABB:
16279       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
16280                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
16281                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
16282                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
16283                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
16284                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
16285                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
16286                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
16287                : CODE_FOR_iwmmxt_walign);
16288       arg0 = CALL_EXPR_ARG (exp, 0);
16289       arg1 = CALL_EXPR_ARG (exp, 1);
16290       arg2 = CALL_EXPR_ARG (exp, 2);
16291       op0 = expand_normal (arg0);
16292       op1 = expand_normal (arg1);
16293       op2 = expand_normal (arg2);
16294       tmode = insn_data[icode].operand[0].mode;
16295       mode0 = insn_data[icode].operand[1].mode;
16296       mode1 = insn_data[icode].operand[2].mode;
16297       mode2 = insn_data[icode].operand[3].mode;
16298
16299       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16300         op0 = copy_to_mode_reg (mode0, op0);
16301       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
16302         op1 = copy_to_mode_reg (mode1, op1);
16303       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16304         op2 = copy_to_mode_reg (mode2, op2);
16305       if (target == 0
16306           || GET_MODE (target) != tmode
16307           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16308         target = gen_reg_rtx (tmode);
16309       pat = GEN_FCN (icode) (target, op0, op1, op2);
16310       if (! pat)
16311         return 0;
16312       emit_insn (pat);
16313       return target;
16314
16315     case ARM_BUILTIN_WZERO:
16316       target = gen_reg_rtx (DImode);
16317       emit_insn (gen_iwmmxt_clrdi (target));
16318       return target;
16319
16320     case ARM_BUILTIN_THREAD_POINTER:
16321       return arm_load_tp (target);
16322
16323     default:
16324       break;
16325     }
16326
16327   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16328     if (d->code == (const enum arm_builtins) fcode)
16329       return arm_expand_binop_builtin (d->icode, exp, target);
16330
16331   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16332     if (d->code == (const enum arm_builtins) fcode)
16333       return arm_expand_unop_builtin (d->icode, exp, target, 0);
16334
16335   /* @@@ Should really do something sensible here.  */
16336   return NULL_RTX;
16337 }
16338 \f
16339 /* Return the number (counting from 0) of
16340    the least significant set bit in MASK.  */
16341
16342 inline static int
16343 number_of_first_bit_set (unsigned mask)
16344 {
16345   int bit;
16346
16347   for (bit = 0;
16348        (mask & (1 << bit)) == 0;
16349        ++bit)
16350     continue;
16351
16352   return bit;
16353 }
16354
16355 /* Emit code to push or pop registers to or from the stack.  F is the
16356    assembly file.  MASK is the registers to push or pop.  PUSH is
16357    nonzero if we should push, and zero if we should pop.  For debugging
16358    output, if pushing, adjust CFA_OFFSET by the amount of space added
16359    to the stack.  REAL_REGS should have the same number of bits set as
16360    MASK, and will be used instead (in the same order) to describe which
16361    registers were saved - this is used to mark the save slots when we
16362    push high registers after moving them to low registers.  */
16363 static void
16364 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
16365                unsigned long real_regs)
16366 {
16367   int regno;
16368   int lo_mask = mask & 0xFF;
16369   int pushed_words = 0;
16370
16371   gcc_assert (mask);
16372
16373   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
16374     {
16375       /* Special case.  Do not generate a POP PC statement here, do it in
16376          thumb_exit() */
16377       thumb_exit (f, -1);
16378       return;
16379     }
16380
16381   if (ARM_EABI_UNWIND_TABLES && push)
16382     {
16383       fprintf (f, "\t.save\t{");
16384       for (regno = 0; regno < 15; regno++)
16385         {
16386           if (real_regs & (1 << regno))
16387             {
16388               if (real_regs & ((1 << regno) -1))
16389                 fprintf (f, ", ");
16390               asm_fprintf (f, "%r", regno);
16391             }
16392         }
16393       fprintf (f, "}\n");
16394     }
16395
16396   fprintf (f, "\t%s\t{", push ? "push" : "pop");
16397
16398   /* Look at the low registers first.  */
16399   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
16400     {
16401       if (lo_mask & 1)
16402         {
16403           asm_fprintf (f, "%r", regno);
16404
16405           if ((lo_mask & ~1) != 0)
16406             fprintf (f, ", ");
16407
16408           pushed_words++;
16409         }
16410     }
16411
16412   if (push && (mask & (1 << LR_REGNUM)))
16413     {
16414       /* Catch pushing the LR.  */
16415       if (mask & 0xFF)
16416         fprintf (f, ", ");
16417
16418       asm_fprintf (f, "%r", LR_REGNUM);
16419
16420       pushed_words++;
16421     }
16422   else if (!push && (mask & (1 << PC_REGNUM)))
16423     {
16424       /* Catch popping the PC.  */
16425       if (TARGET_INTERWORK || TARGET_BACKTRACE
16426           || crtl->calls_eh_return)
16427         {
16428           /* The PC is never poped directly, instead
16429              it is popped into r3 and then BX is used.  */
16430           fprintf (f, "}\n");
16431
16432           thumb_exit (f, -1);
16433
16434           return;
16435         }
16436       else
16437         {
16438           if (mask & 0xFF)
16439             fprintf (f, ", ");
16440
16441           asm_fprintf (f, "%r", PC_REGNUM);
16442         }
16443     }
16444
16445   fprintf (f, "}\n");
16446
16447   if (push && pushed_words && dwarf2out_do_frame ())
16448     {
16449       char *l = dwarf2out_cfi_label ();
16450       int pushed_mask = real_regs;
16451
16452       *cfa_offset += pushed_words * 4;
16453       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
16454
16455       pushed_words = 0;
16456       pushed_mask = real_regs;
16457       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
16458         {
16459           if (pushed_mask & 1)
16460             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
16461         }
16462     }
16463 }
16464
16465 /* Generate code to return from a thumb function.
16466    If 'reg_containing_return_addr' is -1, then the return address is
16467    actually on the stack, at the stack pointer.  */
16468 static void
16469 thumb_exit (FILE *f, int reg_containing_return_addr)
16470 {
16471   unsigned regs_available_for_popping;
16472   unsigned regs_to_pop;
16473   int pops_needed;
16474   unsigned available;
16475   unsigned required;
16476   int mode;
16477   int size;
16478   int restore_a4 = FALSE;
16479
16480   /* Compute the registers we need to pop.  */
16481   regs_to_pop = 0;
16482   pops_needed = 0;
16483
16484   if (reg_containing_return_addr == -1)
16485     {
16486       regs_to_pop |= 1 << LR_REGNUM;
16487       ++pops_needed;
16488     }
16489
16490   if (TARGET_BACKTRACE)
16491     {
16492       /* Restore the (ARM) frame pointer and stack pointer.  */
16493       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
16494       pops_needed += 2;
16495     }
16496
16497   /* If there is nothing to pop then just emit the BX instruction and
16498      return.  */
16499   if (pops_needed == 0)
16500     {
16501       if (crtl->calls_eh_return)
16502         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
16503
16504       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16505       return;
16506     }
16507   /* Otherwise if we are not supporting interworking and we have not created
16508      a backtrace structure and the function was not entered in ARM mode then
16509      just pop the return address straight into the PC.  */
16510   else if (!TARGET_INTERWORK
16511            && !TARGET_BACKTRACE
16512            && !is_called_in_ARM_mode (current_function_decl)
16513            && !crtl->calls_eh_return)
16514     {
16515       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
16516       return;
16517     }
16518
16519   /* Find out how many of the (return) argument registers we can corrupt.  */
16520   regs_available_for_popping = 0;
16521
16522   /* If returning via __builtin_eh_return, the bottom three registers
16523      all contain information needed for the return.  */
16524   if (crtl->calls_eh_return)
16525     size = 12;
16526   else
16527     {
16528       /* If we can deduce the registers used from the function's
16529          return value.  This is more reliable that examining
16530          df_regs_ever_live_p () because that will be set if the register is
16531          ever used in the function, not just if the register is used
16532          to hold a return value.  */
16533
16534       if (crtl->return_rtx != 0)
16535         mode = GET_MODE (crtl->return_rtx);
16536       else
16537         mode = DECL_MODE (DECL_RESULT (current_function_decl));
16538
16539       size = GET_MODE_SIZE (mode);
16540
16541       if (size == 0)
16542         {
16543           /* In a void function we can use any argument register.
16544              In a function that returns a structure on the stack
16545              we can use the second and third argument registers.  */
16546           if (mode == VOIDmode)
16547             regs_available_for_popping =
16548               (1 << ARG_REGISTER (1))
16549               | (1 << ARG_REGISTER (2))
16550               | (1 << ARG_REGISTER (3));
16551           else
16552             regs_available_for_popping =
16553               (1 << ARG_REGISTER (2))
16554               | (1 << ARG_REGISTER (3));
16555         }
16556       else if (size <= 4)
16557         regs_available_for_popping =
16558           (1 << ARG_REGISTER (2))
16559           | (1 << ARG_REGISTER (3));
16560       else if (size <= 8)
16561         regs_available_for_popping =
16562           (1 << ARG_REGISTER (3));
16563     }
16564
16565   /* Match registers to be popped with registers into which we pop them.  */
16566   for (available = regs_available_for_popping,
16567        required  = regs_to_pop;
16568        required != 0 && available != 0;
16569        available &= ~(available & - available),
16570        required  &= ~(required  & - required))
16571     -- pops_needed;
16572
16573   /* If we have any popping registers left over, remove them.  */
16574   if (available > 0)
16575     regs_available_for_popping &= ~available;
16576
16577   /* Otherwise if we need another popping register we can use
16578      the fourth argument register.  */
16579   else if (pops_needed)
16580     {
16581       /* If we have not found any free argument registers and
16582          reg a4 contains the return address, we must move it.  */
16583       if (regs_available_for_popping == 0
16584           && reg_containing_return_addr == LAST_ARG_REGNUM)
16585         {
16586           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16587           reg_containing_return_addr = LR_REGNUM;
16588         }
16589       else if (size > 12)
16590         {
16591           /* Register a4 is being used to hold part of the return value,
16592              but we have dire need of a free, low register.  */
16593           restore_a4 = TRUE;
16594
16595           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
16596         }
16597
16598       if (reg_containing_return_addr != LAST_ARG_REGNUM)
16599         {
16600           /* The fourth argument register is available.  */
16601           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
16602
16603           --pops_needed;
16604         }
16605     }
16606
16607   /* Pop as many registers as we can.  */
16608   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16609                  regs_available_for_popping);
16610
16611   /* Process the registers we popped.  */
16612   if (reg_containing_return_addr == -1)
16613     {
16614       /* The return address was popped into the lowest numbered register.  */
16615       regs_to_pop &= ~(1 << LR_REGNUM);
16616
16617       reg_containing_return_addr =
16618         number_of_first_bit_set (regs_available_for_popping);
16619
16620       /* Remove this register for the mask of available registers, so that
16621          the return address will not be corrupted by further pops.  */
16622       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
16623     }
16624
16625   /* If we popped other registers then handle them here.  */
16626   if (regs_available_for_popping)
16627     {
16628       int frame_pointer;
16629
16630       /* Work out which register currently contains the frame pointer.  */
16631       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
16632
16633       /* Move it into the correct place.  */
16634       asm_fprintf (f, "\tmov\t%r, %r\n",
16635                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
16636
16637       /* (Temporarily) remove it from the mask of popped registers.  */
16638       regs_available_for_popping &= ~(1 << frame_pointer);
16639       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
16640
16641       if (regs_available_for_popping)
16642         {
16643           int stack_pointer;
16644
16645           /* We popped the stack pointer as well,
16646              find the register that contains it.  */
16647           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
16648
16649           /* Move it into the stack register.  */
16650           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
16651
16652           /* At this point we have popped all necessary registers, so
16653              do not worry about restoring regs_available_for_popping
16654              to its correct value:
16655
16656              assert (pops_needed == 0)
16657              assert (regs_available_for_popping == (1 << frame_pointer))
16658              assert (regs_to_pop == (1 << STACK_POINTER))  */
16659         }
16660       else
16661         {
16662           /* Since we have just move the popped value into the frame
16663              pointer, the popping register is available for reuse, and
16664              we know that we still have the stack pointer left to pop.  */
16665           regs_available_for_popping |= (1 << frame_pointer);
16666         }
16667     }
16668
16669   /* If we still have registers left on the stack, but we no longer have
16670      any registers into which we can pop them, then we must move the return
16671      address into the link register and make available the register that
16672      contained it.  */
16673   if (regs_available_for_popping == 0 && pops_needed > 0)
16674     {
16675       regs_available_for_popping |= 1 << reg_containing_return_addr;
16676
16677       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
16678                    reg_containing_return_addr);
16679
16680       reg_containing_return_addr = LR_REGNUM;
16681     }
16682
16683   /* If we have registers left on the stack then pop some more.
16684      We know that at most we will want to pop FP and SP.  */
16685   if (pops_needed > 0)
16686     {
16687       int  popped_into;
16688       int  move_to;
16689
16690       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16691                      regs_available_for_popping);
16692
16693       /* We have popped either FP or SP.
16694          Move whichever one it is into the correct register.  */
16695       popped_into = number_of_first_bit_set (regs_available_for_popping);
16696       move_to     = number_of_first_bit_set (regs_to_pop);
16697
16698       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
16699
16700       regs_to_pop &= ~(1 << move_to);
16701
16702       --pops_needed;
16703     }
16704
16705   /* If we still have not popped everything then we must have only
16706      had one register available to us and we are now popping the SP.  */
16707   if (pops_needed > 0)
16708     {
16709       int  popped_into;
16710
16711       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16712                      regs_available_for_popping);
16713
16714       popped_into = number_of_first_bit_set (regs_available_for_popping);
16715
16716       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
16717       /*
16718         assert (regs_to_pop == (1 << STACK_POINTER))
16719         assert (pops_needed == 1)
16720       */
16721     }
16722
16723   /* If necessary restore the a4 register.  */
16724   if (restore_a4)
16725     {
16726       if (reg_containing_return_addr != LR_REGNUM)
16727         {
16728           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16729           reg_containing_return_addr = LR_REGNUM;
16730         }
16731
16732       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
16733     }
16734
16735   if (crtl->calls_eh_return)
16736     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
16737
16738   /* Return to caller.  */
16739   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16740 }
16741
16742 \f
16743 void
16744 thumb1_final_prescan_insn (rtx insn)
16745 {
16746   if (flag_print_asm_name)
16747     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
16748                  INSN_ADDRESSES (INSN_UID (insn)));
16749 }
16750
16751 int
16752 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
16753 {
16754   unsigned HOST_WIDE_INT mask = 0xff;
16755   int i;
16756
16757   if (val == 0) /* XXX */
16758     return 0;
16759
16760   for (i = 0; i < 25; i++)
16761     if ((val & (mask << i)) == val)
16762       return 1;
16763
16764   return 0;
16765 }
16766
16767 /* Returns nonzero if the current function contains,
16768    or might contain a far jump.  */
16769 static int
16770 thumb_far_jump_used_p (void)
16771 {
16772   rtx insn;
16773
16774   /* This test is only important for leaf functions.  */
16775   /* assert (!leaf_function_p ()); */
16776
16777   /* If we have already decided that far jumps may be used,
16778      do not bother checking again, and always return true even if
16779      it turns out that they are not being used.  Once we have made
16780      the decision that far jumps are present (and that hence the link
16781      register will be pushed onto the stack) we cannot go back on it.  */
16782   if (cfun->machine->far_jump_used)
16783     return 1;
16784
16785   /* If this function is not being called from the prologue/epilogue
16786      generation code then it must be being called from the
16787      INITIAL_ELIMINATION_OFFSET macro.  */
16788   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
16789     {
16790       /* In this case we know that we are being asked about the elimination
16791          of the arg pointer register.  If that register is not being used,
16792          then there are no arguments on the stack, and we do not have to
16793          worry that a far jump might force the prologue to push the link
16794          register, changing the stack offsets.  In this case we can just
16795          return false, since the presence of far jumps in the function will
16796          not affect stack offsets.
16797
16798          If the arg pointer is live (or if it was live, but has now been
16799          eliminated and so set to dead) then we do have to test to see if
16800          the function might contain a far jump.  This test can lead to some
16801          false negatives, since before reload is completed, then length of
16802          branch instructions is not known, so gcc defaults to returning their
16803          longest length, which in turn sets the far jump attribute to true.
16804
16805          A false negative will not result in bad code being generated, but it
16806          will result in a needless push and pop of the link register.  We
16807          hope that this does not occur too often.
16808
16809          If we need doubleword stack alignment this could affect the other
16810          elimination offsets so we can't risk getting it wrong.  */
16811       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
16812         cfun->machine->arg_pointer_live = 1;
16813       else if (!cfun->machine->arg_pointer_live)
16814         return 0;
16815     }
16816
16817   /* Check to see if the function contains a branch
16818      insn with the far jump attribute set.  */
16819   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16820     {
16821       if (GET_CODE (insn) == JUMP_INSN
16822           /* Ignore tablejump patterns.  */
16823           && GET_CODE (PATTERN (insn)) != ADDR_VEC
16824           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
16825           && get_attr_far_jump (insn) == FAR_JUMP_YES
16826           )
16827         {
16828           /* Record the fact that we have decided that
16829              the function does use far jumps.  */
16830           cfun->machine->far_jump_used = 1;
16831           return 1;
16832         }
16833     }
16834
16835   return 0;
16836 }
16837
16838 /* Return nonzero if FUNC must be entered in ARM mode.  */
16839 int
16840 is_called_in_ARM_mode (tree func)
16841 {
16842   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
16843
16844   /* Ignore the problem about functions whose address is taken.  */
16845   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
16846     return TRUE;
16847
16848 #ifdef ARM_PE
16849   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
16850 #else
16851   return FALSE;
16852 #endif
16853 }
16854
16855 /* The bits which aren't usefully expanded as rtl.  */
16856 const char *
16857 thumb_unexpanded_epilogue (void)
16858 {
16859   arm_stack_offsets *offsets;
16860   int regno;
16861   unsigned long live_regs_mask = 0;
16862   int high_regs_pushed = 0;
16863   int had_to_push_lr;
16864   int size;
16865
16866   if (return_used_this_function)
16867     return "";
16868
16869   if (IS_NAKED (arm_current_func_type ()))
16870     return "";
16871
16872   offsets = arm_get_frame_offsets ();
16873   live_regs_mask = offsets->saved_regs_mask;
16874   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
16875
16876   /* If we can deduce the registers used from the function's return value.
16877      This is more reliable that examining df_regs_ever_live_p () because that
16878      will be set if the register is ever used in the function, not just if
16879      the register is used to hold a return value.  */
16880   size = arm_size_return_regs ();
16881
16882   /* The prolog may have pushed some high registers to use as
16883      work registers.  e.g. the testsuite file:
16884      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
16885      compiles to produce:
16886         push    {r4, r5, r6, r7, lr}
16887         mov     r7, r9
16888         mov     r6, r8
16889         push    {r6, r7}
16890      as part of the prolog.  We have to undo that pushing here.  */
16891
16892   if (high_regs_pushed)
16893     {
16894       unsigned long mask = live_regs_mask & 0xff;
16895       int next_hi_reg;
16896
16897       /* The available low registers depend on the size of the value we are
16898          returning.  */
16899       if (size <= 12)
16900         mask |=  1 << 3;
16901       if (size <= 8)
16902         mask |= 1 << 2;
16903
16904       if (mask == 0)
16905         /* Oh dear!  We have no low registers into which we can pop
16906            high registers!  */
16907         internal_error
16908           ("no low registers available for popping high registers");
16909
16910       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
16911         if (live_regs_mask & (1 << next_hi_reg))
16912           break;
16913
16914       while (high_regs_pushed)
16915         {
16916           /* Find lo register(s) into which the high register(s) can
16917              be popped.  */
16918           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
16919             {
16920               if (mask & (1 << regno))
16921                 high_regs_pushed--;
16922               if (high_regs_pushed == 0)
16923                 break;
16924             }
16925
16926           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
16927
16928           /* Pop the values into the low register(s).  */
16929           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
16930
16931           /* Move the value(s) into the high registers.  */
16932           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
16933             {
16934               if (mask & (1 << regno))
16935                 {
16936                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
16937                                regno);
16938
16939                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
16940                     if (live_regs_mask & (1 << next_hi_reg))
16941                       break;
16942                 }
16943             }
16944         }
16945       live_regs_mask &= ~0x0f00;
16946     }
16947
16948   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
16949   live_regs_mask &= 0xff;
16950
16951   if (crtl->args.pretend_args_size == 0 || TARGET_BACKTRACE)
16952     {
16953       /* Pop the return address into the PC.  */
16954       if (had_to_push_lr)
16955         live_regs_mask |= 1 << PC_REGNUM;
16956
16957       /* Either no argument registers were pushed or a backtrace
16958          structure was created which includes an adjusted stack
16959          pointer, so just pop everything.  */
16960       if (live_regs_mask)
16961         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
16962                        live_regs_mask);
16963
16964       /* We have either just popped the return address into the
16965          PC or it is was kept in LR for the entire function.  */
16966       if (!had_to_push_lr)
16967         thumb_exit (asm_out_file, LR_REGNUM);
16968     }
16969   else
16970     {
16971       /* Pop everything but the return address.  */
16972       if (live_regs_mask)
16973         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
16974                        live_regs_mask);
16975
16976       if (had_to_push_lr)
16977         {
16978           if (size > 12)
16979             {
16980               /* We have no free low regs, so save one.  */
16981               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
16982                            LAST_ARG_REGNUM);
16983             }
16984
16985           /* Get the return address into a temporary register.  */
16986           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
16987                          1 << LAST_ARG_REGNUM);
16988
16989           if (size > 12)
16990             {
16991               /* Move the return address to lr.  */
16992               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
16993                            LAST_ARG_REGNUM);
16994               /* Restore the low register.  */
16995               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
16996                            IP_REGNUM);
16997               regno = LR_REGNUM;
16998             }
16999           else
17000             regno = LAST_ARG_REGNUM;
17001         }
17002       else
17003         regno = LR_REGNUM;
17004
17005       /* Remove the argument registers that were pushed onto the stack.  */
17006       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
17007                    SP_REGNUM, SP_REGNUM,
17008                    crtl->args.pretend_args_size);
17009
17010       thumb_exit (asm_out_file, regno);
17011     }
17012
17013   return "";
17014 }
17015
17016 /* Functions to save and restore machine-specific function data.  */
17017 static struct machine_function *
17018 arm_init_machine_status (void)
17019 {
17020   struct machine_function *machine;
17021   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
17022
17023 #if ARM_FT_UNKNOWN != 0
17024   machine->func_type = ARM_FT_UNKNOWN;
17025 #endif
17026   return machine;
17027 }
17028
17029 /* Return an RTX indicating where the return address to the
17030    calling function can be found.  */
17031 rtx
17032 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
17033 {
17034   if (count != 0)
17035     return NULL_RTX;
17036
17037   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
17038 }
17039
17040 /* Do anything needed before RTL is emitted for each function.  */
17041 void
17042 arm_init_expanders (void)
17043 {
17044   /* Arrange to initialize and mark the machine per-function status.  */
17045   init_machine_status = arm_init_machine_status;
17046
17047   /* This is to stop the combine pass optimizing away the alignment
17048      adjustment of va_arg.  */
17049   /* ??? It is claimed that this should not be necessary.  */
17050   if (cfun)
17051     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
17052 }
17053
17054
17055 /* Like arm_compute_initial_elimination offset.  Simpler because there
17056    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
17057    to point at the base of the local variables after static stack
17058    space for a function has been allocated.  */
17059
17060 HOST_WIDE_INT
17061 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
17062 {
17063   arm_stack_offsets *offsets;
17064
17065   offsets = arm_get_frame_offsets ();
17066
17067   switch (from)
17068     {
17069     case ARG_POINTER_REGNUM:
17070       switch (to)
17071         {
17072         case STACK_POINTER_REGNUM:
17073           return offsets->outgoing_args - offsets->saved_args;
17074
17075         case FRAME_POINTER_REGNUM:
17076           return offsets->soft_frame - offsets->saved_args;
17077
17078         case ARM_HARD_FRAME_POINTER_REGNUM:
17079           return offsets->saved_regs - offsets->saved_args;
17080
17081         case THUMB_HARD_FRAME_POINTER_REGNUM:
17082           return offsets->locals_base - offsets->saved_args;
17083
17084         default:
17085           gcc_unreachable ();
17086         }
17087       break;
17088
17089     case FRAME_POINTER_REGNUM:
17090       switch (to)
17091         {
17092         case STACK_POINTER_REGNUM:
17093           return offsets->outgoing_args - offsets->soft_frame;
17094
17095         case ARM_HARD_FRAME_POINTER_REGNUM:
17096           return offsets->saved_regs - offsets->soft_frame;
17097
17098         case THUMB_HARD_FRAME_POINTER_REGNUM:
17099           return offsets->locals_base - offsets->soft_frame;
17100
17101         default:
17102           gcc_unreachable ();
17103         }
17104       break;
17105
17106     default:
17107       gcc_unreachable ();
17108     }
17109 }
17110
17111 /* Generate the rest of a function's prologue.  */
17112 void
17113 thumb1_expand_prologue (void)
17114 {
17115   rtx insn, dwarf;
17116
17117   HOST_WIDE_INT amount;
17118   arm_stack_offsets *offsets;
17119   unsigned long func_type;
17120   int regno;
17121   unsigned long live_regs_mask;
17122
17123   func_type = arm_current_func_type ();
17124
17125   /* Naked functions don't have prologues.  */
17126   if (IS_NAKED (func_type))
17127     return;
17128
17129   if (IS_INTERRUPT (func_type))
17130     {
17131       error ("interrupt Service Routines cannot be coded in Thumb mode");
17132       return;
17133     }
17134
17135   offsets = arm_get_frame_offsets ();
17136   live_regs_mask = offsets->saved_regs_mask;
17137   /* Load the pic register before setting the frame pointer,
17138      so we can use r7 as a temporary work register.  */
17139   if (flag_pic && arm_pic_register != INVALID_REGNUM)
17140     arm_load_pic_register (live_regs_mask);
17141
17142   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
17143     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
17144                     stack_pointer_rtx);
17145
17146   amount = offsets->outgoing_args - offsets->saved_regs;
17147   if (amount)
17148     {
17149       if (amount < 512)
17150         {
17151           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17152                                         GEN_INT (- amount)));
17153           RTX_FRAME_RELATED_P (insn) = 1;
17154         }
17155       else
17156         {
17157           rtx reg;
17158
17159           /* The stack decrement is too big for an immediate value in a single
17160              insn.  In theory we could issue multiple subtracts, but after
17161              three of them it becomes more space efficient to place the full
17162              value in the constant pool and load into a register.  (Also the
17163              ARM debugger really likes to see only one stack decrement per
17164              function).  So instead we look for a scratch register into which
17165              we can load the decrement, and then we subtract this from the
17166              stack pointer.  Unfortunately on the thumb the only available
17167              scratch registers are the argument registers, and we cannot use
17168              these as they may hold arguments to the function.  Instead we
17169              attempt to locate a call preserved register which is used by this
17170              function.  If we can find one, then we know that it will have
17171              been pushed at the start of the prologue and so we can corrupt
17172              it now.  */
17173           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
17174             if (live_regs_mask & (1 << regno))
17175               break;
17176
17177           gcc_assert(regno <= LAST_LO_REGNUM);
17178
17179           reg = gen_rtx_REG (SImode, regno);
17180
17181           emit_insn (gen_movsi (reg, GEN_INT (- amount)));
17182
17183           insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
17184                                         stack_pointer_rtx, reg));
17185           RTX_FRAME_RELATED_P (insn) = 1;
17186           dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17187                                plus_constant (stack_pointer_rtx,
17188                                               -amount));
17189           RTX_FRAME_RELATED_P (dwarf) = 1;
17190           REG_NOTES (insn)
17191             = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
17192                                  REG_NOTES (insn));
17193         }
17194     }
17195
17196   if (frame_pointer_needed)
17197     thumb_set_frame_pointer (offsets);
17198
17199   /* If we are profiling, make sure no instructions are scheduled before
17200      the call to mcount.  Similarly if the user has requested no
17201      scheduling in the prolog.  Similarly if we want non-call exceptions
17202      using the EABI unwinder, to prevent faulting instructions from being
17203      swapped with a stack adjustment.  */
17204   if (crtl->profile || !TARGET_SCHED_PROLOG
17205       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
17206     emit_insn (gen_blockage ());
17207
17208   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
17209   if (live_regs_mask & 0xff)
17210     cfun->machine->lr_save_eliminated = 0;
17211 }
17212
17213
17214 void
17215 thumb1_expand_epilogue (void)
17216 {
17217   HOST_WIDE_INT amount;
17218   arm_stack_offsets *offsets;
17219   int regno;
17220
17221   /* Naked functions don't have prologues.  */
17222   if (IS_NAKED (arm_current_func_type ()))
17223     return;
17224
17225   offsets = arm_get_frame_offsets ();
17226   amount = offsets->outgoing_args - offsets->saved_regs;
17227
17228   if (frame_pointer_needed)
17229     {
17230       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
17231       amount = offsets->locals_base - offsets->saved_regs;
17232     }
17233
17234   gcc_assert (amount >= 0);
17235   if (amount)
17236     {
17237       if (amount < 512)
17238         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
17239                                GEN_INT (amount)));
17240       else
17241         {
17242           /* r3 is always free in the epilogue.  */
17243           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
17244
17245           emit_insn (gen_movsi (reg, GEN_INT (amount)));
17246           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
17247         }
17248     }
17249
17250   /* Emit a USE (stack_pointer_rtx), so that
17251      the stack adjustment will not be deleted.  */
17252   emit_insn (gen_prologue_use (stack_pointer_rtx));
17253
17254   if (crtl->profile || !TARGET_SCHED_PROLOG)
17255     emit_insn (gen_blockage ());
17256
17257   /* Emit a clobber for each insn that will be restored in the epilogue,
17258      so that flow2 will get register lifetimes correct.  */
17259   for (regno = 0; regno < 13; regno++)
17260     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
17261       emit_clobber (gen_rtx_REG (SImode, regno));
17262
17263   if (! df_regs_ever_live_p (LR_REGNUM))
17264     emit_use (gen_rtx_REG (SImode, LR_REGNUM));
17265 }
17266
17267 static void
17268 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
17269 {
17270   arm_stack_offsets *offsets;
17271   unsigned long live_regs_mask = 0;
17272   unsigned long l_mask;
17273   unsigned high_regs_pushed = 0;
17274   int cfa_offset = 0;
17275   int regno;
17276
17277   if (IS_NAKED (arm_current_func_type ()))
17278     return;
17279
17280   if (is_called_in_ARM_mode (current_function_decl))
17281     {
17282       const char * name;
17283
17284       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
17285       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
17286                   == SYMBOL_REF);
17287       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
17288
17289       /* Generate code sequence to switch us into Thumb mode.  */
17290       /* The .code 32 directive has already been emitted by
17291          ASM_DECLARE_FUNCTION_NAME.  */
17292       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
17293       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
17294
17295       /* Generate a label, so that the debugger will notice the
17296          change in instruction sets.  This label is also used by
17297          the assembler to bypass the ARM code when this function
17298          is called from a Thumb encoded function elsewhere in the
17299          same file.  Hence the definition of STUB_NAME here must
17300          agree with the definition in gas/config/tc-arm.c.  */
17301
17302 #define STUB_NAME ".real_start_of"
17303
17304       fprintf (f, "\t.code\t16\n");
17305 #ifdef ARM_PE
17306       if (arm_dllexport_name_p (name))
17307         name = arm_strip_name_encoding (name);
17308 #endif
17309       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
17310       fprintf (f, "\t.thumb_func\n");
17311       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
17312     }
17313
17314   if (crtl->args.pretend_args_size)
17315     {
17316       /* Output unwind directive for the stack adjustment.  */
17317       if (ARM_EABI_UNWIND_TABLES)
17318         fprintf (f, "\t.pad #%d\n",
17319                  crtl->args.pretend_args_size);
17320
17321       if (cfun->machine->uses_anonymous_args)
17322         {
17323           int num_pushes;
17324
17325           fprintf (f, "\tpush\t{");
17326
17327           num_pushes = ARM_NUM_INTS (crtl->args.pretend_args_size);
17328
17329           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
17330                regno <= LAST_ARG_REGNUM;
17331                regno++)
17332             asm_fprintf (f, "%r%s", regno,
17333                          regno == LAST_ARG_REGNUM ? "" : ", ");
17334
17335           fprintf (f, "}\n");
17336         }
17337       else
17338         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
17339                      SP_REGNUM, SP_REGNUM,
17340                      crtl->args.pretend_args_size);
17341
17342       /* We don't need to record the stores for unwinding (would it
17343          help the debugger any if we did?), but record the change in
17344          the stack pointer.  */
17345       if (dwarf2out_do_frame ())
17346         {
17347           char *l = dwarf2out_cfi_label ();
17348
17349           cfa_offset = cfa_offset + crtl->args.pretend_args_size;
17350           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17351         }
17352     }
17353
17354   /* Get the registers we are going to push.  */
17355   offsets = arm_get_frame_offsets ();
17356   live_regs_mask = offsets->saved_regs_mask;
17357   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
17358   l_mask = live_regs_mask & 0x40ff;
17359   /* Then count how many other high registers will need to be pushed.  */
17360   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
17361
17362   if (TARGET_BACKTRACE)
17363     {
17364       unsigned offset;
17365       unsigned work_register;
17366
17367       /* We have been asked to create a stack backtrace structure.
17368          The code looks like this:
17369
17370          0   .align 2
17371          0   func:
17372          0     sub   SP, #16         Reserve space for 4 registers.
17373          2     push  {R7}            Push low registers.
17374          4     add   R7, SP, #20     Get the stack pointer before the push.
17375          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
17376          8     mov   R7, PC          Get hold of the start of this code plus 12.
17377         10     str   R7, [SP, #16]   Store it.
17378         12     mov   R7, FP          Get hold of the current frame pointer.
17379         14     str   R7, [SP, #4]    Store it.
17380         16     mov   R7, LR          Get hold of the current return address.
17381         18     str   R7, [SP, #12]   Store it.
17382         20     add   R7, SP, #16     Point at the start of the backtrace structure.
17383         22     mov   FP, R7          Put this value into the frame pointer.  */
17384
17385       work_register = thumb_find_work_register (live_regs_mask);
17386
17387       if (ARM_EABI_UNWIND_TABLES)
17388         asm_fprintf (f, "\t.pad #16\n");
17389
17390       asm_fprintf
17391         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
17392          SP_REGNUM, SP_REGNUM);
17393
17394       if (dwarf2out_do_frame ())
17395         {
17396           char *l = dwarf2out_cfi_label ();
17397
17398           cfa_offset = cfa_offset + 16;
17399           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17400         }
17401
17402       if (l_mask)
17403         {
17404           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
17405           offset = bit_count (l_mask) * UNITS_PER_WORD;
17406         }
17407       else
17408         offset = 0;
17409
17410       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17411                    offset + 16 + crtl->args.pretend_args_size);
17412
17413       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17414                    offset + 4);
17415
17416       /* Make sure that the instruction fetching the PC is in the right place
17417          to calculate "start of backtrace creation code + 12".  */
17418       if (l_mask)
17419         {
17420           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17421           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17422                        offset + 12);
17423           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17424                        ARM_HARD_FRAME_POINTER_REGNUM);
17425           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17426                        offset);
17427         }
17428       else
17429         {
17430           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17431                        ARM_HARD_FRAME_POINTER_REGNUM);
17432           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17433                        offset);
17434           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17435           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17436                        offset + 12);
17437         }
17438
17439       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
17440       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17441                    offset + 8);
17442       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17443                    offset + 12);
17444       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
17445                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
17446     }
17447   /* Optimization:  If we are not pushing any low registers but we are going
17448      to push some high registers then delay our first push.  This will just
17449      be a push of LR and we can combine it with the push of the first high
17450      register.  */
17451   else if ((l_mask & 0xff) != 0
17452            || (high_regs_pushed == 0 && l_mask))
17453     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
17454
17455   if (high_regs_pushed)
17456     {
17457       unsigned pushable_regs;
17458       unsigned next_hi_reg;
17459
17460       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
17461         if (live_regs_mask & (1 << next_hi_reg))
17462           break;
17463
17464       pushable_regs = l_mask & 0xff;
17465
17466       if (pushable_regs == 0)
17467         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
17468
17469       while (high_regs_pushed > 0)
17470         {
17471           unsigned long real_regs_mask = 0;
17472
17473           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
17474             {
17475               if (pushable_regs & (1 << regno))
17476                 {
17477                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
17478
17479                   high_regs_pushed --;
17480                   real_regs_mask |= (1 << next_hi_reg);
17481
17482                   if (high_regs_pushed)
17483                     {
17484                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
17485                            next_hi_reg --)
17486                         if (live_regs_mask & (1 << next_hi_reg))
17487                           break;
17488                     }
17489                   else
17490                     {
17491                       pushable_regs &= ~((1 << regno) - 1);
17492                       break;
17493                     }
17494                 }
17495             }
17496
17497           /* If we had to find a work register and we have not yet
17498              saved the LR then add it to the list of regs to push.  */
17499           if (l_mask == (1 << LR_REGNUM))
17500             {
17501               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
17502                              1, &cfa_offset,
17503                              real_regs_mask | (1 << LR_REGNUM));
17504               l_mask = 0;
17505             }
17506           else
17507             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
17508         }
17509     }
17510 }
17511
17512 /* Handle the case of a double word load into a low register from
17513    a computed memory address.  The computed address may involve a
17514    register which is overwritten by the load.  */
17515 const char *
17516 thumb_load_double_from_address (rtx *operands)
17517 {
17518   rtx addr;
17519   rtx base;
17520   rtx offset;
17521   rtx arg1;
17522   rtx arg2;
17523
17524   gcc_assert (GET_CODE (operands[0]) == REG);
17525   gcc_assert (GET_CODE (operands[1]) == MEM);
17526
17527   /* Get the memory address.  */
17528   addr = XEXP (operands[1], 0);
17529
17530   /* Work out how the memory address is computed.  */
17531   switch (GET_CODE (addr))
17532     {
17533     case REG:
17534       operands[2] = adjust_address (operands[1], SImode, 4);
17535
17536       if (REGNO (operands[0]) == REGNO (addr))
17537         {
17538           output_asm_insn ("ldr\t%H0, %2", operands);
17539           output_asm_insn ("ldr\t%0, %1", operands);
17540         }
17541       else
17542         {
17543           output_asm_insn ("ldr\t%0, %1", operands);
17544           output_asm_insn ("ldr\t%H0, %2", operands);
17545         }
17546       break;
17547
17548     case CONST:
17549       /* Compute <address> + 4 for the high order load.  */
17550       operands[2] = adjust_address (operands[1], SImode, 4);
17551
17552       output_asm_insn ("ldr\t%0, %1", operands);
17553       output_asm_insn ("ldr\t%H0, %2", operands);
17554       break;
17555
17556     case PLUS:
17557       arg1   = XEXP (addr, 0);
17558       arg2   = XEXP (addr, 1);
17559
17560       if (CONSTANT_P (arg1))
17561         base = arg2, offset = arg1;
17562       else
17563         base = arg1, offset = arg2;
17564
17565       gcc_assert (GET_CODE (base) == REG);
17566
17567       /* Catch the case of <address> = <reg> + <reg> */
17568       if (GET_CODE (offset) == REG)
17569         {
17570           int reg_offset = REGNO (offset);
17571           int reg_base   = REGNO (base);
17572           int reg_dest   = REGNO (operands[0]);
17573
17574           /* Add the base and offset registers together into the
17575              higher destination register.  */
17576           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
17577                        reg_dest + 1, reg_base, reg_offset);
17578
17579           /* Load the lower destination register from the address in
17580              the higher destination register.  */
17581           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
17582                        reg_dest, reg_dest + 1);
17583
17584           /* Load the higher destination register from its own address
17585              plus 4.  */
17586           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
17587                        reg_dest + 1, reg_dest + 1);
17588         }
17589       else
17590         {
17591           /* Compute <address> + 4 for the high order load.  */
17592           operands[2] = adjust_address (operands[1], SImode, 4);
17593
17594           /* If the computed address is held in the low order register
17595              then load the high order register first, otherwise always
17596              load the low order register first.  */
17597           if (REGNO (operands[0]) == REGNO (base))
17598             {
17599               output_asm_insn ("ldr\t%H0, %2", operands);
17600               output_asm_insn ("ldr\t%0, %1", operands);
17601             }
17602           else
17603             {
17604               output_asm_insn ("ldr\t%0, %1", operands);
17605               output_asm_insn ("ldr\t%H0, %2", operands);
17606             }
17607         }
17608       break;
17609
17610     case LABEL_REF:
17611       /* With no registers to worry about we can just load the value
17612          directly.  */
17613       operands[2] = adjust_address (operands[1], SImode, 4);
17614
17615       output_asm_insn ("ldr\t%H0, %2", operands);
17616       output_asm_insn ("ldr\t%0, %1", operands);
17617       break;
17618
17619     default:
17620       gcc_unreachable ();
17621     }
17622
17623   return "";
17624 }
17625
17626 const char *
17627 thumb_output_move_mem_multiple (int n, rtx *operands)
17628 {
17629   rtx tmp;
17630
17631   switch (n)
17632     {
17633     case 2:
17634       if (REGNO (operands[4]) > REGNO (operands[5]))
17635         {
17636           tmp = operands[4];
17637           operands[4] = operands[5];
17638           operands[5] = tmp;
17639         }
17640       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
17641       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
17642       break;
17643
17644     case 3:
17645       if (REGNO (operands[4]) > REGNO (operands[5]))
17646         {
17647           tmp = operands[4];
17648           operands[4] = operands[5];
17649           operands[5] = tmp;
17650         }
17651       if (REGNO (operands[5]) > REGNO (operands[6]))
17652         {
17653           tmp = operands[5];
17654           operands[5] = operands[6];
17655           operands[6] = tmp;
17656         }
17657       if (REGNO (operands[4]) > REGNO (operands[5]))
17658         {
17659           tmp = operands[4];
17660           operands[4] = operands[5];
17661           operands[5] = tmp;
17662         }
17663
17664       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
17665       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
17666       break;
17667
17668     default:
17669       gcc_unreachable ();
17670     }
17671
17672   return "";
17673 }
17674
17675 /* Output a call-via instruction for thumb state.  */
17676 const char *
17677 thumb_call_via_reg (rtx reg)
17678 {
17679   int regno = REGNO (reg);
17680   rtx *labelp;
17681
17682   gcc_assert (regno < LR_REGNUM);
17683
17684   /* If we are in the normal text section we can use a single instance
17685      per compilation unit.  If we are doing function sections, then we need
17686      an entry per section, since we can't rely on reachability.  */
17687   if (in_section == text_section)
17688     {
17689       thumb_call_reg_needed = 1;
17690
17691       if (thumb_call_via_label[regno] == NULL)
17692         thumb_call_via_label[regno] = gen_label_rtx ();
17693       labelp = thumb_call_via_label + regno;
17694     }
17695   else
17696     {
17697       if (cfun->machine->call_via[regno] == NULL)
17698         cfun->machine->call_via[regno] = gen_label_rtx ();
17699       labelp = cfun->machine->call_via + regno;
17700     }
17701
17702   output_asm_insn ("bl\t%a0", labelp);
17703   return "";
17704 }
17705
17706 /* Routines for generating rtl.  */
17707 void
17708 thumb_expand_movmemqi (rtx *operands)
17709 {
17710   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
17711   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
17712   HOST_WIDE_INT len = INTVAL (operands[2]);
17713   HOST_WIDE_INT offset = 0;
17714
17715   while (len >= 12)
17716     {
17717       emit_insn (gen_movmem12b (out, in, out, in));
17718       len -= 12;
17719     }
17720
17721   if (len >= 8)
17722     {
17723       emit_insn (gen_movmem8b (out, in, out, in));
17724       len -= 8;
17725     }
17726
17727   if (len >= 4)
17728     {
17729       rtx reg = gen_reg_rtx (SImode);
17730       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
17731       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
17732       len -= 4;
17733       offset += 4;
17734     }
17735
17736   if (len >= 2)
17737     {
17738       rtx reg = gen_reg_rtx (HImode);
17739       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
17740                                               plus_constant (in, offset))));
17741       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
17742                             reg));
17743       len -= 2;
17744       offset += 2;
17745     }
17746
17747   if (len)
17748     {
17749       rtx reg = gen_reg_rtx (QImode);
17750       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
17751                                               plus_constant (in, offset))));
17752       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
17753                             reg));
17754     }
17755 }
17756
17757 void
17758 thumb_reload_out_hi (rtx *operands)
17759 {
17760   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
17761 }
17762
17763 /* Handle reading a half-word from memory during reload.  */
17764 void
17765 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
17766 {
17767   gcc_unreachable ();
17768 }
17769
17770 /* Return the length of a function name prefix
17771     that starts with the character 'c'.  */
17772 static int
17773 arm_get_strip_length (int c)
17774 {
17775   switch (c)
17776     {
17777     ARM_NAME_ENCODING_LENGTHS
17778       default: return 0;
17779     }
17780 }
17781
17782 /* Return a pointer to a function's name with any
17783    and all prefix encodings stripped from it.  */
17784 const char *
17785 arm_strip_name_encoding (const char *name)
17786 {
17787   int skip;
17788
17789   while ((skip = arm_get_strip_length (* name)))
17790     name += skip;
17791
17792   return name;
17793 }
17794
17795 /* If there is a '*' anywhere in the name's prefix, then
17796    emit the stripped name verbatim, otherwise prepend an
17797    underscore if leading underscores are being used.  */
17798 void
17799 arm_asm_output_labelref (FILE *stream, const char *name)
17800 {
17801   int skip;
17802   int verbatim = 0;
17803
17804   while ((skip = arm_get_strip_length (* name)))
17805     {
17806       verbatim |= (*name == '*');
17807       name += skip;
17808     }
17809
17810   if (verbatim)
17811     fputs (name, stream);
17812   else
17813     asm_fprintf (stream, "%U%s", name);
17814 }
17815
17816 static void
17817 arm_file_start (void)
17818 {
17819   int val;
17820
17821   if (TARGET_UNIFIED_ASM)
17822     asm_fprintf (asm_out_file, "\t.syntax unified\n");
17823
17824   if (TARGET_BPABI)
17825     {
17826       const char *fpu_name;
17827       if (arm_select[0].string)
17828         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
17829       else if (arm_select[1].string)
17830         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
17831       else
17832         asm_fprintf (asm_out_file, "\t.cpu %s\n",
17833                      all_cores[arm_default_cpu].name);
17834
17835       if (TARGET_SOFT_FLOAT)
17836         {
17837           if (TARGET_VFP)
17838             fpu_name = "softvfp";
17839           else
17840             fpu_name = "softfpa";
17841         }
17842       else
17843         {
17844           int set_float_abi_attributes = 0;
17845           switch (arm_fpu_arch)
17846             {
17847             case FPUTYPE_FPA:
17848               fpu_name = "fpa";
17849               break;
17850             case FPUTYPE_FPA_EMU2:
17851               fpu_name = "fpe2";
17852               break;
17853             case FPUTYPE_FPA_EMU3:
17854               fpu_name = "fpe3";
17855               break;
17856             case FPUTYPE_MAVERICK:
17857               fpu_name = "maverick";
17858               break;
17859             case FPUTYPE_VFP:
17860               fpu_name = "vfp";
17861               set_float_abi_attributes = 1;
17862               break;
17863             case FPUTYPE_VFP3D16:
17864               fpu_name = "vfpv3-d16";
17865               set_float_abi_attributes = 1;
17866               break;
17867             case FPUTYPE_VFP3:
17868               fpu_name = "vfpv3";
17869               set_float_abi_attributes = 1;
17870               break;
17871             case FPUTYPE_NEON:
17872               fpu_name = "neon";
17873               set_float_abi_attributes = 1;
17874               break;
17875             default:
17876               abort();
17877             }
17878           if (set_float_abi_attributes)
17879             {
17880               if (TARGET_HARD_FLOAT)
17881                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
17882               if (TARGET_HARD_FLOAT_ABI)
17883                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
17884             }
17885         }
17886       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
17887
17888       /* Some of these attributes only apply when the corresponding features
17889          are used.  However we don't have any easy way of figuring this out.
17890          Conservatively record the setting that would have been used.  */
17891
17892       /* Tag_ABI_FP_rounding.  */
17893       if (flag_rounding_math)
17894         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
17895       if (!flag_unsafe_math_optimizations)
17896         {
17897           /* Tag_ABI_FP_denomal.  */
17898           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
17899           /* Tag_ABI_FP_exceptions.  */
17900           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
17901         }
17902       /* Tag_ABI_FP_user_exceptions.  */
17903       if (flag_signaling_nans)
17904         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
17905       /* Tag_ABI_FP_number_model.  */
17906       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
17907                    flag_finite_math_only ? 1 : 3);
17908
17909       /* Tag_ABI_align8_needed.  */
17910       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
17911       /* Tag_ABI_align8_preserved.  */
17912       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
17913       /* Tag_ABI_enum_size.  */
17914       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
17915                    flag_short_enums ? 1 : 2);
17916
17917       /* Tag_ABI_optimization_goals.  */
17918       if (optimize_size)
17919         val = 4;
17920       else if (optimize >= 2)
17921         val = 2;
17922       else if (optimize)
17923         val = 1;
17924       else
17925         val = 6;
17926       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
17927
17928       if (arm_lang_output_object_attributes_hook)
17929         arm_lang_output_object_attributes_hook();
17930     }
17931   default_file_start();
17932 }
17933
17934 static void
17935 arm_file_end (void)
17936 {
17937   int regno;
17938
17939   if (NEED_INDICATE_EXEC_STACK)
17940     /* Add .note.GNU-stack.  */
17941     file_end_indicate_exec_stack ();
17942
17943   if (! thumb_call_reg_needed)
17944     return;
17945
17946   switch_to_section (text_section);
17947   asm_fprintf (asm_out_file, "\t.code 16\n");
17948   ASM_OUTPUT_ALIGN (asm_out_file, 1);
17949
17950   for (regno = 0; regno < LR_REGNUM; regno++)
17951     {
17952       rtx label = thumb_call_via_label[regno];
17953
17954       if (label != 0)
17955         {
17956           targetm.asm_out.internal_label (asm_out_file, "L",
17957                                           CODE_LABEL_NUMBER (label));
17958           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
17959         }
17960     }
17961 }
17962
17963 #ifndef ARM_PE
17964 /* Symbols in the text segment can be accessed without indirecting via the
17965    constant pool; it may take an extra binary operation, but this is still
17966    faster than indirecting via memory.  Don't do this when not optimizing,
17967    since we won't be calculating al of the offsets necessary to do this
17968    simplification.  */
17969
17970 static void
17971 arm_encode_section_info (tree decl, rtx rtl, int first)
17972 {
17973   if (optimize > 0 && TREE_CONSTANT (decl))
17974     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
17975
17976   default_encode_section_info (decl, rtl, first);
17977 }
17978 #endif /* !ARM_PE */
17979
17980 static void
17981 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
17982 {
17983   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
17984       && !strcmp (prefix, "L"))
17985     {
17986       arm_ccfsm_state = 0;
17987       arm_target_insn = NULL;
17988     }
17989   default_internal_label (stream, prefix, labelno);
17990 }
17991
17992 /* Output code to add DELTA to the first argument, and then jump
17993    to FUNCTION.  Used for C++ multiple inheritance.  */
17994 static void
17995 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
17996                      HOST_WIDE_INT delta,
17997                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
17998                      tree function)
17999 {
18000   static int thunk_label = 0;
18001   char label[256];
18002   char labelpc[256];
18003   int mi_delta = delta;
18004   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
18005   int shift = 0;
18006   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
18007                     ? 1 : 0);
18008   if (mi_delta < 0)
18009     mi_delta = - mi_delta;
18010
18011   if (TARGET_THUMB1)
18012     {
18013       int labelno = thunk_label++;
18014       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
18015       /* Thunks are entered in arm mode when avaiable.  */
18016       if (TARGET_THUMB1_ONLY)
18017         {
18018           /* push r3 so we can use it as a temporary.  */
18019           /* TODO: Omit this save if r3 is not used.  */
18020           fputs ("\tpush {r3}\n", file);
18021           fputs ("\tldr\tr3, ", file);
18022         }
18023       else
18024         {
18025           fputs ("\tldr\tr12, ", file);
18026         }
18027       assemble_name (file, label);
18028       fputc ('\n', file);
18029       if (flag_pic)
18030         {
18031           /* If we are generating PIC, the ldr instruction below loads
18032              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
18033              the address of the add + 8, so we have:
18034
18035              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
18036                  = target + 1.
18037
18038              Note that we have "+ 1" because some versions of GNU ld
18039              don't set the low bit of the result for R_ARM_REL32
18040              relocations against thumb function symbols.
18041              On ARMv6M this is +4, not +8.  */
18042           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
18043           assemble_name (file, labelpc);
18044           fputs (":\n", file);
18045           if (TARGET_THUMB1_ONLY)
18046             {
18047               /* This is 2 insns after the start of the thunk, so we know it
18048                  is 4-byte aligned.  */
18049               fputs ("\tadd\tr3, pc, r3\n", file);
18050               fputs ("\tmov r12, r3\n", file);
18051             }
18052           else
18053             fputs ("\tadd\tr12, pc, r12\n", file);
18054         }
18055       else if (TARGET_THUMB1_ONLY)
18056         fputs ("\tmov r12, r3\n", file);
18057     }
18058   if (TARGET_THUMB1_ONLY)
18059     {
18060       if (mi_delta > 255)
18061         {
18062           fputs ("\tldr\tr3, ", file);
18063           assemble_name (file, label);
18064           fputs ("+4\n", file);
18065           asm_fprintf (file, "\t%s\t%r, %r, r3\n",
18066                        mi_op, this_regno, this_regno);
18067         }
18068       else if (mi_delta != 0)
18069         {
18070           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18071                        mi_op, this_regno, this_regno,
18072                        mi_delta);
18073         }
18074     }
18075   else
18076     {
18077       /* TODO: Use movw/movt for large constants when available.  */
18078       while (mi_delta != 0)
18079         {
18080           if ((mi_delta & (3 << shift)) == 0)
18081             shift += 2;
18082           else
18083             {
18084               asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
18085                            mi_op, this_regno, this_regno,
18086                            mi_delta & (0xff << shift));
18087               mi_delta &= ~(0xff << shift);
18088               shift += 8;
18089             }
18090         }
18091     }
18092   if (TARGET_THUMB1)
18093     {
18094       if (TARGET_THUMB1_ONLY)
18095         fputs ("\tpop\t{r3}\n", file);
18096
18097       fprintf (file, "\tbx\tr12\n");
18098       ASM_OUTPUT_ALIGN (file, 2);
18099       assemble_name (file, label);
18100       fputs (":\n", file);
18101       if (flag_pic)
18102         {
18103           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
18104           rtx tem = XEXP (DECL_RTL (function), 0);
18105           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
18106           tem = gen_rtx_MINUS (GET_MODE (tem),
18107                                tem,
18108                                gen_rtx_SYMBOL_REF (Pmode,
18109                                                    ggc_strdup (labelpc)));
18110           assemble_integer (tem, 4, BITS_PER_WORD, 1);
18111         }
18112       else
18113         /* Output ".word .LTHUNKn".  */
18114         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
18115
18116       if (TARGET_THUMB1_ONLY && mi_delta > 255)
18117         assemble_integer (GEN_INT(mi_delta), 4, BITS_PER_WORD, 1);
18118     }
18119   else
18120     {
18121       fputs ("\tb\t", file);
18122       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
18123       if (NEED_PLT_RELOC)
18124         fputs ("(PLT)", file);
18125       fputc ('\n', file);
18126     }
18127 }
18128
18129 int
18130 arm_emit_vector_const (FILE *file, rtx x)
18131 {
18132   int i;
18133   const char * pattern;
18134
18135   gcc_assert (GET_CODE (x) == CONST_VECTOR);
18136
18137   switch (GET_MODE (x))
18138     {
18139     case V2SImode: pattern = "%08x"; break;
18140     case V4HImode: pattern = "%04x"; break;
18141     case V8QImode: pattern = "%02x"; break;
18142     default:       gcc_unreachable ();
18143     }
18144
18145   fprintf (file, "0x");
18146   for (i = CONST_VECTOR_NUNITS (x); i--;)
18147     {
18148       rtx element;
18149
18150       element = CONST_VECTOR_ELT (x, i);
18151       fprintf (file, pattern, INTVAL (element));
18152     }
18153
18154   return 1;
18155 }
18156
18157 const char *
18158 arm_output_load_gr (rtx *operands)
18159 {
18160   rtx reg;
18161   rtx offset;
18162   rtx wcgr;
18163   rtx sum;
18164
18165   if (GET_CODE (operands [1]) != MEM
18166       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
18167       || GET_CODE (reg = XEXP (sum, 0)) != REG
18168       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
18169       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
18170     return "wldrw%?\t%0, %1";
18171
18172   /* Fix up an out-of-range load of a GR register.  */
18173   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
18174   wcgr = operands[0];
18175   operands[0] = reg;
18176   output_asm_insn ("ldr%?\t%0, %1", operands);
18177
18178   operands[0] = wcgr;
18179   operands[1] = reg;
18180   output_asm_insn ("tmcr%?\t%0, %1", operands);
18181   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
18182
18183   return "";
18184 }
18185
18186 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
18187
18188    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
18189    named arg and all anonymous args onto the stack.
18190    XXX I know the prologue shouldn't be pushing registers, but it is faster
18191    that way.  */
18192
18193 static void
18194 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
18195                             enum machine_mode mode,
18196                             tree type,
18197                             int *pretend_size,
18198                             int second_time ATTRIBUTE_UNUSED)
18199 {
18200   int nregs = cum->nregs;
18201   if (nregs & 1
18202       && ARM_DOUBLEWORD_ALIGN
18203       && arm_needs_doubleword_align (mode, type))
18204     nregs++;
18205
18206   cfun->machine->uses_anonymous_args = 1;
18207   if (nregs < NUM_ARG_REGS)
18208     *pretend_size = (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
18209 }
18210
18211 /* Return nonzero if the CONSUMER instruction (a store) does not need
18212    PRODUCER's value to calculate the address.  */
18213
18214 int
18215 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
18216 {
18217   rtx value = PATTERN (producer);
18218   rtx addr = PATTERN (consumer);
18219
18220   if (GET_CODE (value) == COND_EXEC)
18221     value = COND_EXEC_CODE (value);
18222   if (GET_CODE (value) == PARALLEL)
18223     value = XVECEXP (value, 0, 0);
18224   value = XEXP (value, 0);
18225   if (GET_CODE (addr) == COND_EXEC)
18226     addr = COND_EXEC_CODE (addr);
18227   if (GET_CODE (addr) == PARALLEL)
18228     addr = XVECEXP (addr, 0, 0);
18229   addr = XEXP (addr, 0);
18230
18231   return !reg_overlap_mentioned_p (value, addr);
18232 }
18233
18234 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
18235    have an early register shift value or amount dependency on the
18236    result of PRODUCER.  */
18237
18238 int
18239 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
18240 {
18241   rtx value = PATTERN (producer);
18242   rtx op = PATTERN (consumer);
18243   rtx early_op;
18244
18245   if (GET_CODE (value) == COND_EXEC)
18246     value = COND_EXEC_CODE (value);
18247   if (GET_CODE (value) == PARALLEL)
18248     value = XVECEXP (value, 0, 0);
18249   value = XEXP (value, 0);
18250   if (GET_CODE (op) == COND_EXEC)
18251     op = COND_EXEC_CODE (op);
18252   if (GET_CODE (op) == PARALLEL)
18253     op = XVECEXP (op, 0, 0);
18254   op = XEXP (op, 1);
18255
18256   early_op = XEXP (op, 0);
18257   /* This is either an actual independent shift, or a shift applied to
18258      the first operand of another operation.  We want the whole shift
18259      operation.  */
18260   if (GET_CODE (early_op) == REG)
18261     early_op = op;
18262
18263   return !reg_overlap_mentioned_p (value, early_op);
18264 }
18265
18266 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
18267    have an early register shift value dependency on the result of
18268    PRODUCER.  */
18269
18270 int
18271 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
18272 {
18273   rtx value = PATTERN (producer);
18274   rtx op = PATTERN (consumer);
18275   rtx early_op;
18276
18277   if (GET_CODE (value) == COND_EXEC)
18278     value = COND_EXEC_CODE (value);
18279   if (GET_CODE (value) == PARALLEL)
18280     value = XVECEXP (value, 0, 0);
18281   value = XEXP (value, 0);
18282   if (GET_CODE (op) == COND_EXEC)
18283     op = COND_EXEC_CODE (op);
18284   if (GET_CODE (op) == PARALLEL)
18285     op = XVECEXP (op, 0, 0);
18286   op = XEXP (op, 1);
18287
18288   early_op = XEXP (op, 0);
18289
18290   /* This is either an actual independent shift, or a shift applied to
18291      the first operand of another operation.  We want the value being
18292      shifted, in either case.  */
18293   if (GET_CODE (early_op) != REG)
18294     early_op = XEXP (early_op, 0);
18295
18296   return !reg_overlap_mentioned_p (value, early_op);
18297 }
18298
18299 /* Return nonzero if the CONSUMER (a mul or mac op) does not
18300    have an early register mult dependency on the result of
18301    PRODUCER.  */
18302
18303 int
18304 arm_no_early_mul_dep (rtx producer, rtx consumer)
18305 {
18306   rtx value = PATTERN (producer);
18307   rtx op = PATTERN (consumer);
18308
18309   if (GET_CODE (value) == COND_EXEC)
18310     value = COND_EXEC_CODE (value);
18311   if (GET_CODE (value) == PARALLEL)
18312     value = XVECEXP (value, 0, 0);
18313   value = XEXP (value, 0);
18314   if (GET_CODE (op) == COND_EXEC)
18315     op = COND_EXEC_CODE (op);
18316   if (GET_CODE (op) == PARALLEL)
18317     op = XVECEXP (op, 0, 0);
18318   op = XEXP (op, 1);
18319
18320   if (GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
18321     {
18322       if (GET_CODE (XEXP (op, 0)) == MULT)
18323         return !reg_overlap_mentioned_p (value, XEXP (op, 0));
18324       else
18325         return !reg_overlap_mentioned_p (value, XEXP (op, 1));
18326     }
18327
18328   return 0;
18329 }
18330
18331 /* We can't rely on the caller doing the proper promotion when
18332    using APCS or ATPCS.  */
18333
18334 static bool
18335 arm_promote_prototypes (const_tree t ATTRIBUTE_UNUSED)
18336 {
18337     return !TARGET_AAPCS_BASED;
18338 }
18339
18340
18341 /* AAPCS based ABIs use short enums by default.  */
18342
18343 static bool
18344 arm_default_short_enums (void)
18345 {
18346   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
18347 }
18348
18349
18350 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
18351
18352 static bool
18353 arm_align_anon_bitfield (void)
18354 {
18355   return TARGET_AAPCS_BASED;
18356 }
18357
18358
18359 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
18360
18361 static tree
18362 arm_cxx_guard_type (void)
18363 {
18364   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
18365 }
18366
18367 /* Return non-zero if the consumer (a multiply-accumulate instruction)
18368    has an accumulator dependency on the result of the producer (a
18369    multiplication instruction) and no other dependency on that result.  */
18370 int
18371 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
18372 {
18373   rtx mul = PATTERN (producer);
18374   rtx mac = PATTERN (consumer);
18375   rtx mul_result;
18376   rtx mac_op0, mac_op1, mac_acc;
18377
18378   if (GET_CODE (mul) == COND_EXEC)
18379     mul = COND_EXEC_CODE (mul);
18380   if (GET_CODE (mac) == COND_EXEC)
18381     mac = COND_EXEC_CODE (mac);
18382
18383   /* Check that mul is of the form (set (...) (mult ...))
18384      and mla is of the form (set (...) (plus (mult ...) (...))).  */
18385   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
18386       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
18387           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
18388     return 0;
18389
18390   mul_result = XEXP (mul, 0);
18391   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
18392   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
18393   mac_acc = XEXP (XEXP (mac, 1), 1);
18394
18395   return (reg_overlap_mentioned_p (mul_result, mac_acc)
18396           && !reg_overlap_mentioned_p (mul_result, mac_op0)
18397           && !reg_overlap_mentioned_p (mul_result, mac_op1));
18398 }
18399
18400
18401 /* The EABI says test the least significant bit of a guard variable.  */
18402
18403 static bool
18404 arm_cxx_guard_mask_bit (void)
18405 {
18406   return TARGET_AAPCS_BASED;
18407 }
18408
18409
18410 /* The EABI specifies that all array cookies are 8 bytes long.  */
18411
18412 static tree
18413 arm_get_cookie_size (tree type)
18414 {
18415   tree size;
18416
18417   if (!TARGET_AAPCS_BASED)
18418     return default_cxx_get_cookie_size (type);
18419
18420   size = build_int_cst (sizetype, 8);
18421   return size;
18422 }
18423
18424
18425 /* The EABI says that array cookies should also contain the element size.  */
18426
18427 static bool
18428 arm_cookie_has_size (void)
18429 {
18430   return TARGET_AAPCS_BASED;
18431 }
18432
18433
18434 /* The EABI says constructors and destructors should return a pointer to
18435    the object constructed/destroyed.  */
18436
18437 static bool
18438 arm_cxx_cdtor_returns_this (void)
18439 {
18440   return TARGET_AAPCS_BASED;
18441 }
18442
18443 /* The EABI says that an inline function may never be the key
18444    method.  */
18445
18446 static bool
18447 arm_cxx_key_method_may_be_inline (void)
18448 {
18449   return !TARGET_AAPCS_BASED;
18450 }
18451
18452 static void
18453 arm_cxx_determine_class_data_visibility (tree decl)
18454 {
18455   if (!TARGET_AAPCS_BASED
18456       || !TARGET_DLLIMPORT_DECL_ATTRIBUTES)
18457     return;
18458
18459   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
18460      is exported.  However, on systems without dynamic vague linkage,
18461      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
18462   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
18463     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
18464   else
18465     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
18466   DECL_VISIBILITY_SPECIFIED (decl) = 1;
18467 }
18468
18469 static bool
18470 arm_cxx_class_data_always_comdat (void)
18471 {
18472   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
18473      vague linkage if the class has no key function.  */
18474   return !TARGET_AAPCS_BASED;
18475 }
18476
18477
18478 /* The EABI says __aeabi_atexit should be used to register static
18479    destructors.  */
18480
18481 static bool
18482 arm_cxx_use_aeabi_atexit (void)
18483 {
18484   return TARGET_AAPCS_BASED;
18485 }
18486
18487
18488 void
18489 arm_set_return_address (rtx source, rtx scratch)
18490 {
18491   arm_stack_offsets *offsets;
18492   HOST_WIDE_INT delta;
18493   rtx addr;
18494   unsigned long saved_regs;
18495
18496   offsets = arm_get_frame_offsets ();
18497   saved_regs = offsets->saved_regs_mask;
18498
18499   if ((saved_regs & (1 << LR_REGNUM)) == 0)
18500     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18501   else
18502     {
18503       if (frame_pointer_needed)
18504         addr = plus_constant(hard_frame_pointer_rtx, -4);
18505       else
18506         {
18507           /* LR will be the first saved register.  */
18508           delta = offsets->outgoing_args - (offsets->frame + 4);
18509
18510
18511           if (delta >= 4096)
18512             {
18513               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
18514                                      GEN_INT (delta & ~4095)));
18515               addr = scratch;
18516               delta &= 4095;
18517             }
18518           else
18519             addr = stack_pointer_rtx;
18520
18521           addr = plus_constant (addr, delta);
18522         }
18523       emit_move_insn (gen_frame_mem (Pmode, addr), source);
18524     }
18525 }
18526
18527
18528 void
18529 thumb_set_return_address (rtx source, rtx scratch)
18530 {
18531   arm_stack_offsets *offsets;
18532   HOST_WIDE_INT delta;
18533   HOST_WIDE_INT limit;
18534   int reg;
18535   rtx addr;
18536   unsigned long mask;
18537
18538   emit_use (source);
18539
18540   offsets = arm_get_frame_offsets ();
18541   mask = offsets->saved_regs_mask;
18542   if (mask & (1 << LR_REGNUM))
18543     {
18544       limit = 1024;
18545       /* Find the saved regs.  */
18546       if (frame_pointer_needed)
18547         {
18548           delta = offsets->soft_frame - offsets->saved_args;
18549           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
18550           if (TARGET_THUMB1)
18551             limit = 128;
18552         }
18553       else
18554         {
18555           delta = offsets->outgoing_args - offsets->saved_args;
18556           reg = SP_REGNUM;
18557         }
18558       /* Allow for the stack frame.  */
18559       if (TARGET_THUMB1 && TARGET_BACKTRACE)
18560         delta -= 16;
18561       /* The link register is always the first saved register.  */
18562       delta -= 4;
18563
18564       /* Construct the address.  */
18565       addr = gen_rtx_REG (SImode, reg);
18566       if (delta > limit)
18567         {
18568           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
18569           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
18570           addr = scratch;
18571         }
18572       else
18573         addr = plus_constant (addr, delta);
18574
18575       emit_move_insn (gen_frame_mem (Pmode, addr), source);
18576     }
18577   else
18578     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18579 }
18580
18581 /* Implements target hook vector_mode_supported_p.  */
18582 bool
18583 arm_vector_mode_supported_p (enum machine_mode mode)
18584 {
18585   /* Neon also supports V2SImode, etc. listed in the clause below.  */
18586   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
18587       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
18588     return true;
18589
18590   if ((mode == V2SImode)
18591       || (mode == V4HImode)
18592       || (mode == V8QImode))
18593     return true;
18594
18595   return false;
18596 }
18597
18598 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
18599    ARM insns and therefore guarantee that the shift count is modulo 256.
18600    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
18601    guarantee no particular behavior for out-of-range counts.  */
18602
18603 static unsigned HOST_WIDE_INT
18604 arm_shift_truncation_mask (enum machine_mode mode)
18605 {
18606   return mode == SImode ? 255 : 0;
18607 }
18608
18609
18610 /* Map internal gcc register numbers to DWARF2 register numbers.  */
18611
18612 unsigned int
18613 arm_dbx_register_number (unsigned int regno)
18614 {
18615   if (regno < 16)
18616     return regno;
18617
18618   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
18619      compatibility.  The EABI defines them as registers 96-103.  */
18620   if (IS_FPA_REGNUM (regno))
18621     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
18622
18623   /* FIXME: VFPv3 register numbering.  */
18624   if (IS_VFP_REGNUM (regno))
18625     return 64 + regno - FIRST_VFP_REGNUM;
18626
18627   if (IS_IWMMXT_GR_REGNUM (regno))
18628     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
18629
18630   if (IS_IWMMXT_REGNUM (regno))
18631     return 112 + regno - FIRST_IWMMXT_REGNUM;
18632
18633   gcc_unreachable ();
18634 }
18635
18636
18637 #ifdef TARGET_UNWIND_INFO
18638 /* Emit unwind directives for a store-multiple instruction or stack pointer
18639    push during alignment.
18640    These should only ever be generated by the function prologue code, so
18641    expect them to have a particular form.  */
18642
18643 static void
18644 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
18645 {
18646   int i;
18647   HOST_WIDE_INT offset;
18648   HOST_WIDE_INT nregs;
18649   int reg_size;
18650   unsigned reg;
18651   unsigned lastreg;
18652   rtx e;
18653
18654   e = XVECEXP (p, 0, 0);
18655   if (GET_CODE (e) != SET)
18656     abort ();
18657
18658   /* First insn will adjust the stack pointer.  */
18659   if (GET_CODE (e) != SET
18660       || GET_CODE (XEXP (e, 0)) != REG
18661       || REGNO (XEXP (e, 0)) != SP_REGNUM
18662       || GET_CODE (XEXP (e, 1)) != PLUS)
18663     abort ();
18664
18665   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
18666   nregs = XVECLEN (p, 0) - 1;
18667
18668   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
18669   if (reg < 16)
18670     {
18671       /* The function prologue may also push pc, but not annotate it as it is
18672          never restored.  We turn this into a stack pointer adjustment.  */
18673       if (nregs * 4 == offset - 4)
18674         {
18675           fprintf (asm_out_file, "\t.pad #4\n");
18676           offset -= 4;
18677         }
18678       reg_size = 4;
18679       fprintf (asm_out_file, "\t.save {");
18680     }
18681   else if (IS_VFP_REGNUM (reg))
18682     {
18683       reg_size = 8;
18684       fprintf (asm_out_file, "\t.vsave {");
18685     }
18686   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
18687     {
18688       /* FPA registers are done differently.  */
18689       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
18690       return;
18691     }
18692   else
18693     /* Unknown register type.  */
18694     abort ();
18695
18696   /* If the stack increment doesn't match the size of the saved registers,
18697      something has gone horribly wrong.  */
18698   if (offset != nregs * reg_size)
18699     abort ();
18700
18701   offset = 0;
18702   lastreg = 0;
18703   /* The remaining insns will describe the stores.  */
18704   for (i = 1; i <= nregs; i++)
18705     {
18706       /* Expect (set (mem <addr>) (reg)).
18707          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
18708       e = XVECEXP (p, 0, i);
18709       if (GET_CODE (e) != SET
18710           || GET_CODE (XEXP (e, 0)) != MEM
18711           || GET_CODE (XEXP (e, 1)) != REG)
18712         abort ();
18713
18714       reg = REGNO (XEXP (e, 1));
18715       if (reg < lastreg)
18716         abort ();
18717
18718       if (i != 1)
18719         fprintf (asm_out_file, ", ");
18720       /* We can't use %r for vfp because we need to use the
18721          double precision register names.  */
18722       if (IS_VFP_REGNUM (reg))
18723         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
18724       else
18725         asm_fprintf (asm_out_file, "%r", reg);
18726
18727 #ifdef ENABLE_CHECKING
18728       /* Check that the addresses are consecutive.  */
18729       e = XEXP (XEXP (e, 0), 0);
18730       if (GET_CODE (e) == PLUS)
18731         {
18732           offset += reg_size;
18733           if (GET_CODE (XEXP (e, 0)) != REG
18734               || REGNO (XEXP (e, 0)) != SP_REGNUM
18735               || GET_CODE (XEXP (e, 1)) != CONST_INT
18736               || offset != INTVAL (XEXP (e, 1)))
18737             abort ();
18738         }
18739       else if (i != 1
18740                || GET_CODE (e) != REG
18741                || REGNO (e) != SP_REGNUM)
18742         abort ();
18743 #endif
18744     }
18745   fprintf (asm_out_file, "}\n");
18746 }
18747
18748 /*  Emit unwind directives for a SET.  */
18749
18750 static void
18751 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
18752 {
18753   rtx e0;
18754   rtx e1;
18755   unsigned reg;
18756
18757   e0 = XEXP (p, 0);
18758   e1 = XEXP (p, 1);
18759   switch (GET_CODE (e0))
18760     {
18761     case MEM:
18762       /* Pushing a single register.  */
18763       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
18764           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
18765           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
18766         abort ();
18767
18768       asm_fprintf (asm_out_file, "\t.save ");
18769       if (IS_VFP_REGNUM (REGNO (e1)))
18770         asm_fprintf(asm_out_file, "{d%d}\n",
18771                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
18772       else
18773         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
18774       break;
18775
18776     case REG:
18777       if (REGNO (e0) == SP_REGNUM)
18778         {
18779           /* A stack increment.  */
18780           if (GET_CODE (e1) != PLUS
18781               || GET_CODE (XEXP (e1, 0)) != REG
18782               || REGNO (XEXP (e1, 0)) != SP_REGNUM
18783               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
18784             abort ();
18785
18786           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
18787                        -INTVAL (XEXP (e1, 1)));
18788         }
18789       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
18790         {
18791           HOST_WIDE_INT offset;
18792
18793           if (GET_CODE (e1) == PLUS)
18794             {
18795               if (GET_CODE (XEXP (e1, 0)) != REG
18796                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
18797                 abort ();
18798               reg = REGNO (XEXP (e1, 0));
18799               offset = INTVAL (XEXP (e1, 1));
18800               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
18801                            HARD_FRAME_POINTER_REGNUM, reg,
18802                            INTVAL (XEXP (e1, 1)));
18803             }
18804           else if (GET_CODE (e1) == REG)
18805             {
18806               reg = REGNO (e1);
18807               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
18808                            HARD_FRAME_POINTER_REGNUM, reg);
18809             }
18810           else
18811             abort ();
18812         }
18813       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
18814         {
18815           /* Move from sp to reg.  */
18816           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
18817         }
18818      else if (GET_CODE (e1) == PLUS
18819               && GET_CODE (XEXP (e1, 0)) == REG
18820               && REGNO (XEXP (e1, 0)) == SP_REGNUM
18821               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
18822         {
18823           /* Set reg to offset from sp.  */
18824           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
18825                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
18826         }
18827       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
18828         {
18829           /* Stack pointer save before alignment.  */
18830           reg = REGNO (e0);
18831           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
18832                        reg + 0x90, reg);
18833         }
18834       else
18835         abort ();
18836       break;
18837
18838     default:
18839       abort ();
18840     }
18841 }
18842
18843
18844 /* Emit unwind directives for the given insn.  */
18845
18846 static void
18847 arm_unwind_emit (FILE * asm_out_file, rtx insn)
18848 {
18849   rtx pat;
18850
18851   if (!ARM_EABI_UNWIND_TABLES)
18852     return;
18853
18854   if (!(flag_unwind_tables || crtl->uses_eh_lsda)
18855       && (TREE_NOTHROW (current_function_decl)
18856           || crtl->all_throwers_are_sibcalls))
18857     return;
18858
18859   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
18860     return;
18861
18862   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
18863   if (pat)
18864     pat = XEXP (pat, 0);
18865   else
18866     pat = PATTERN (insn);
18867
18868   switch (GET_CODE (pat))
18869     {
18870     case SET:
18871       arm_unwind_emit_set (asm_out_file, pat);
18872       break;
18873
18874     case SEQUENCE:
18875       /* Store multiple.  */
18876       arm_unwind_emit_sequence (asm_out_file, pat);
18877       break;
18878
18879     default:
18880       abort();
18881     }
18882 }
18883
18884
18885 /* Output a reference from a function exception table to the type_info
18886    object X.  The EABI specifies that the symbol should be relocated by
18887    an R_ARM_TARGET2 relocation.  */
18888
18889 static bool
18890 arm_output_ttype (rtx x)
18891 {
18892   fputs ("\t.word\t", asm_out_file);
18893   output_addr_const (asm_out_file, x);
18894   /* Use special relocations for symbol references.  */
18895   if (GET_CODE (x) != CONST_INT)
18896     fputs ("(TARGET2)", asm_out_file);
18897   fputc ('\n', asm_out_file);
18898
18899   return TRUE;
18900 }
18901 #endif /* TARGET_UNWIND_INFO */
18902
18903
18904 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
18905    stack alignment.  */
18906
18907 static void
18908 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
18909 {
18910   rtx unspec = SET_SRC (pattern);
18911   gcc_assert (GET_CODE (unspec) == UNSPEC);
18912
18913   switch (index)
18914     {
18915     case UNSPEC_STACK_ALIGN:
18916       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
18917          put anything on the stack, so hopefully it won't matter.
18918          CFA = SP will be correct after alignment.  */
18919       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
18920                               SET_DEST (pattern));
18921       break;
18922     default:
18923       gcc_unreachable ();
18924     }
18925 }
18926
18927
18928 /* Output unwind directives for the start/end of a function.  */
18929
18930 void
18931 arm_output_fn_unwind (FILE * f, bool prologue)
18932 {
18933   if (!ARM_EABI_UNWIND_TABLES)
18934     return;
18935
18936   if (prologue)
18937     fputs ("\t.fnstart\n", f);
18938   else
18939     {
18940       /* If this function will never be unwound, then mark it as such.
18941          The came condition is used in arm_unwind_emit to suppress
18942          the frame annotations.  */
18943       if (!(flag_unwind_tables || crtl->uses_eh_lsda)
18944           && (TREE_NOTHROW (current_function_decl)
18945               || crtl->all_throwers_are_sibcalls))
18946         fputs("\t.cantunwind\n", f);
18947
18948       fputs ("\t.fnend\n", f);
18949     }
18950 }
18951
18952 static bool
18953 arm_emit_tls_decoration (FILE *fp, rtx x)
18954 {
18955   enum tls_reloc reloc;
18956   rtx val;
18957
18958   val = XVECEXP (x, 0, 0);
18959   reloc = INTVAL (XVECEXP (x, 0, 1));
18960
18961   output_addr_const (fp, val);
18962
18963   switch (reloc)
18964     {
18965     case TLS_GD32:
18966       fputs ("(tlsgd)", fp);
18967       break;
18968     case TLS_LDM32:
18969       fputs ("(tlsldm)", fp);
18970       break;
18971     case TLS_LDO32:
18972       fputs ("(tlsldo)", fp);
18973       break;
18974     case TLS_IE32:
18975       fputs ("(gottpoff)", fp);
18976       break;
18977     case TLS_LE32:
18978       fputs ("(tpoff)", fp);
18979       break;
18980     default:
18981       gcc_unreachable ();
18982     }
18983
18984   switch (reloc)
18985     {
18986     case TLS_GD32:
18987     case TLS_LDM32:
18988     case TLS_IE32:
18989       fputs (" + (. - ", fp);
18990       output_addr_const (fp, XVECEXP (x, 0, 2));
18991       fputs (" - ", fp);
18992       output_addr_const (fp, XVECEXP (x, 0, 3));
18993       fputc (')', fp);
18994       break;
18995     default:
18996       break;
18997     }
18998
18999   return TRUE;
19000 }
19001
19002 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
19003
19004 static void
19005 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
19006 {
19007   gcc_assert (size == 4);
19008   fputs ("\t.word\t", file);
19009   output_addr_const (file, x);
19010   fputs ("(tlsldo)", file);
19011 }
19012
19013 bool
19014 arm_output_addr_const_extra (FILE *fp, rtx x)
19015 {
19016   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
19017     return arm_emit_tls_decoration (fp, x);
19018   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
19019     {
19020       char label[256];
19021       int labelno = INTVAL (XVECEXP (x, 0, 0));
19022
19023       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
19024       assemble_name_raw (fp, label);
19025
19026       return TRUE;
19027     }
19028   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_GOTSYM_OFF)
19029     {
19030       assemble_name (fp, "_GLOBAL_OFFSET_TABLE_");
19031       if (GOT_PCREL)
19032         fputs ("+.", fp);
19033       fputs ("-(", fp);
19034       output_addr_const (fp, XVECEXP (x, 0, 0));
19035       fputc (')', fp);
19036       return TRUE;
19037     }
19038   else if (GET_CODE (x) == CONST_VECTOR)
19039     return arm_emit_vector_const (fp, x);
19040
19041   return FALSE;
19042 }
19043
19044 /* Output assembly for a shift instruction.
19045    SET_FLAGS determines how the instruction modifies the condition codes.
19046    0 - Do not set condition codes.
19047    1 - Set condition codes.
19048    2 - Use smallest instruction.  */
19049 const char *
19050 arm_output_shift(rtx * operands, int set_flags)
19051 {
19052   char pattern[100];
19053   static const char flag_chars[3] = {'?', '.', '!'};
19054   const char *shift;
19055   HOST_WIDE_INT val;
19056   char c;
19057   
19058   c = flag_chars[set_flags];
19059   if (TARGET_UNIFIED_ASM)
19060     {
19061       shift = shift_op(operands[3], &val);
19062       if (shift)
19063         {
19064           if (val != -1)
19065             operands[2] = GEN_INT(val);
19066           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
19067         }
19068       else
19069         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
19070     }
19071   else
19072     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
19073   output_asm_insn (pattern, operands);
19074   return "";
19075 }
19076
19077 /* Output a Thumb-2 casesi instruction.  */
19078 const char *
19079 thumb2_output_casesi (rtx *operands)
19080 {
19081   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
19082
19083   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
19084
19085   output_asm_insn ("cmp\t%0, %1", operands);
19086   output_asm_insn ("bhi\t%l3", operands);
19087   switch (GET_MODE(diff_vec))
19088     {
19089     case QImode:
19090       return "tbb\t[%|pc, %0]";
19091     case HImode:
19092       return "tbh\t[%|pc, %0, lsl #1]";
19093     case SImode:
19094       if (flag_pic)
19095         {
19096           output_asm_insn ("adr\t%4, %l2", operands);
19097           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
19098           output_asm_insn ("add\t%4, %4, %5", operands);
19099           return "bx\t%4";
19100         }
19101       else
19102         {
19103           output_asm_insn ("adr\t%4, %l2", operands);
19104           return "ldr\t%|pc, [%4, %0, lsl #2]";
19105         }
19106     default:
19107       gcc_unreachable ();
19108     }
19109 }
19110
19111 /* Most ARM cores are single issue, but some newer ones can dual issue.
19112    The scheduler descriptions rely on this being correct.  */
19113 static int
19114 arm_issue_rate (void)
19115 {
19116   switch (arm_tune)
19117     {
19118     case cortexr4:
19119     case cortexr4f:
19120     case cortexa8:
19121     case cortexa9:
19122       return 2;
19123
19124     default:
19125       return 1;
19126     }
19127 }
19128
19129 /* A table and a function to perform ARM-specific name mangling for
19130    NEON vector types in order to conform to the AAPCS (see "Procedure
19131    Call Standard for the ARM Architecture", Appendix A).  To qualify
19132    for emission with the mangled names defined in that document, a
19133    vector type must not only be of the correct mode but also be
19134    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
19135 typedef struct
19136 {
19137   enum machine_mode mode;
19138   const char *element_type_name;
19139   const char *aapcs_name;
19140 } arm_mangle_map_entry;
19141
19142 static arm_mangle_map_entry arm_mangle_map[] = {
19143   /* 64-bit containerized types.  */
19144   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
19145   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
19146   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
19147   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
19148   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
19149   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
19150   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
19151   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
19152   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
19153   /* 128-bit containerized types.  */
19154   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
19155   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
19156   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
19157   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
19158   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
19159   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
19160   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
19161   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
19162   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
19163   { VOIDmode, NULL, NULL }
19164 };
19165
19166 const char *
19167 arm_mangle_type (const_tree type)
19168 {
19169   arm_mangle_map_entry *pos = arm_mangle_map;
19170
19171   if (TREE_CODE (type) != VECTOR_TYPE)
19172     return NULL;
19173
19174   /* Check the mode of the vector type, and the name of the vector
19175      element type, against the table.  */
19176   while (pos->mode != VOIDmode)
19177     {
19178       tree elt_type = TREE_TYPE (type);
19179
19180       if (pos->mode == TYPE_MODE (type)
19181           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
19182           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
19183                       pos->element_type_name))
19184         return pos->aapcs_name;
19185
19186       pos++;
19187     }
19188
19189   /* Use the default mangling for unrecognized (possibly user-defined)
19190      vector types.  */
19191   return NULL;
19192 }
19193
19194 /* Order of allocation of core registers for Thumb: this allocation is
19195    written over the corresponding initial entries of the array
19196    initialized with REG_ALLOC_ORDER.  We allocate all low registers
19197    first.  Saving and restoring a low register is usually cheaper than
19198    using a call-clobbered high register.  */
19199
19200 static const int thumb_core_reg_alloc_order[] =
19201 {
19202    3,  2,  1,  0,  4,  5,  6,  7,
19203   14, 12,  8,  9, 10, 11, 13, 15
19204 };
19205
19206 /* Adjust register allocation order when compiling for Thumb.  */
19207
19208 void
19209 arm_order_regs_for_local_alloc (void)
19210 {
19211   const int arm_reg_alloc_order[] = REG_ALLOC_ORDER;
19212   memcpy(reg_alloc_order, arm_reg_alloc_order, sizeof (reg_alloc_order));
19213   if (TARGET_THUMB)
19214     memcpy (reg_alloc_order, thumb_core_reg_alloc_order,
19215             sizeof (thumb_core_reg_alloc_order));
19216 }
19217
19218 /* Set default optimization options.  */
19219 void
19220 arm_optimization_options (int level, int size ATTRIBUTE_UNUSED)
19221 {
19222   /* Enable section anchors by default at -O1 or higher.
19223      Use 2 to distinguish from an explicit -fsection-anchors
19224      given on the command line.  */
19225   if (level > 0)
19226     flag_section_anchors = 2;
19227 }
19228
19229 #include "gt-arm.h"