OSDN Git Service

gcc/
[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  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23    Boston, MA 02110-1301, USA.  */
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 /* Forward function declarations.  */
64 static arm_stack_offsets *arm_get_frame_offsets (void);
65 static void arm_add_gc_roots (void);
66 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
67                              HOST_WIDE_INT, rtx, rtx, int, int);
68 static unsigned bit_count (unsigned long);
69 static int arm_address_register_rtx_p (rtx, int);
70 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
71 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
72 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
73 inline static int thumb1_index_register_rtx_p (rtx, int);
74 static int thumb_far_jump_used_p (void);
75 static bool thumb_force_lr_save (void);
76 static unsigned long thumb1_compute_save_reg_mask (void);
77 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
78 static rtx emit_sfm (int, int);
79 static int arm_size_return_regs (void);
80 #ifndef AOF_ASSEMBLER
81 static bool arm_assemble_integer (rtx, unsigned int, int);
82 #endif
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 (tree, 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 int arm_address_cost (rtx);
136 static bool arm_memory_load_p (rtx);
137 static bool arm_cirrus_insn_p (rtx);
138 static void cirrus_reorg (rtx);
139 static void arm_init_builtins (void);
140 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
141 static void arm_init_iwmmxt_builtins (void);
142 static rtx safe_vector_operand (rtx, enum machine_mode);
143 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
144 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
145 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
146 static void emit_constant_insn (rtx cond, rtx pattern);
147 static rtx emit_set_insn (rtx, rtx);
148 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
149                                   tree, bool);
150
151 #ifdef OBJECT_FORMAT_ELF
152 static void arm_elf_asm_constructor (rtx, int) ATTRIBUTE_UNUSED;
153 static void arm_elf_asm_destructor (rtx, int) ATTRIBUTE_UNUSED;
154 #endif
155 #ifndef ARM_PE
156 static void arm_encode_section_info (tree, rtx, int);
157 #endif
158
159 static void arm_file_end (void);
160 static void arm_file_start (void);
161
162 #ifdef AOF_ASSEMBLER
163 static void aof_globalize_label (FILE *, const char *);
164 static void aof_dump_imports (FILE *);
165 static void aof_dump_pic_table (FILE *);
166 static void aof_file_start (void);
167 static void aof_file_end (void);
168 static void aof_asm_init_sections (void);
169 #endif
170 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
171                                         tree, int *, int);
172 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
173                                    enum machine_mode, tree, bool);
174 static bool arm_promote_prototypes (tree);
175 static bool arm_default_short_enums (void);
176 static bool arm_align_anon_bitfield (void);
177 static bool arm_return_in_msb (tree);
178 static bool arm_must_pass_in_stack (enum machine_mode, tree);
179 #ifdef TARGET_UNWIND_INFO
180 static void arm_unwind_emit (FILE *, rtx);
181 static bool arm_output_ttype (rtx);
182 #endif
183 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
184
185 static tree arm_cxx_guard_type (void);
186 static bool arm_cxx_guard_mask_bit (void);
187 static tree arm_get_cookie_size (tree);
188 static bool arm_cookie_has_size (void);
189 static bool arm_cxx_cdtor_returns_this (void);
190 static bool arm_cxx_key_method_may_be_inline (void);
191 static void arm_cxx_determine_class_data_visibility (tree);
192 static bool arm_cxx_class_data_always_comdat (void);
193 static bool arm_cxx_use_aeabi_atexit (void);
194 static void arm_init_libfuncs (void);
195 static bool arm_handle_option (size_t, const char *, int);
196 static void arm_target_help (void);
197 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
198 static bool arm_cannot_copy_insn_p (rtx);
199 static bool arm_tls_symbol_p (rtx x);
200 static void arm_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
201
202 \f
203 /* Initialize the GCC target structure.  */
204 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
205 #undef  TARGET_MERGE_DECL_ATTRIBUTES
206 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
207 #endif
208
209 #undef  TARGET_ATTRIBUTE_TABLE
210 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
211
212 #undef TARGET_ASM_FILE_START
213 #define TARGET_ASM_FILE_START arm_file_start
214 #undef TARGET_ASM_FILE_END
215 #define TARGET_ASM_FILE_END arm_file_end
216
217 #ifdef AOF_ASSEMBLER
218 #undef  TARGET_ASM_BYTE_OP
219 #define TARGET_ASM_BYTE_OP "\tDCB\t"
220 #undef  TARGET_ASM_ALIGNED_HI_OP
221 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
222 #undef  TARGET_ASM_ALIGNED_SI_OP
223 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
224 #undef TARGET_ASM_GLOBALIZE_LABEL
225 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
226 #undef TARGET_ASM_FILE_START
227 #define TARGET_ASM_FILE_START aof_file_start
228 #undef TARGET_ASM_FILE_END
229 #define TARGET_ASM_FILE_END aof_file_end
230 #else
231 #undef  TARGET_ASM_ALIGNED_SI_OP
232 #define TARGET_ASM_ALIGNED_SI_OP NULL
233 #undef  TARGET_ASM_INTEGER
234 #define TARGET_ASM_INTEGER arm_assemble_integer
235 #endif
236
237 #undef  TARGET_ASM_FUNCTION_PROLOGUE
238 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
239
240 #undef  TARGET_ASM_FUNCTION_EPILOGUE
241 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
242
243 #undef  TARGET_DEFAULT_TARGET_FLAGS
244 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
245 #undef  TARGET_HANDLE_OPTION
246 #define TARGET_HANDLE_OPTION arm_handle_option
247 #undef  TARGET_HELP
248 #define TARGET_HELP arm_target_help
249
250 #undef  TARGET_COMP_TYPE_ATTRIBUTES
251 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
252
253 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
254 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
255
256 #undef  TARGET_SCHED_ADJUST_COST
257 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
258
259 #undef TARGET_ENCODE_SECTION_INFO
260 #ifdef ARM_PE
261 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
262 #else
263 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
264 #endif
265
266 #undef  TARGET_STRIP_NAME_ENCODING
267 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
268
269 #undef  TARGET_ASM_INTERNAL_LABEL
270 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
271
272 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
273 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
274
275 #undef  TARGET_ASM_OUTPUT_MI_THUNK
276 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
277 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
278 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
279
280 /* This will be overridden in arm_override_options.  */
281 #undef  TARGET_RTX_COSTS
282 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
283 #undef  TARGET_ADDRESS_COST
284 #define TARGET_ADDRESS_COST arm_address_cost
285
286 #undef TARGET_SHIFT_TRUNCATION_MASK
287 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
288 #undef TARGET_VECTOR_MODE_SUPPORTED_P
289 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
290
291 #undef  TARGET_MACHINE_DEPENDENT_REORG
292 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
293
294 #undef  TARGET_INIT_BUILTINS
295 #define TARGET_INIT_BUILTINS  arm_init_builtins
296 #undef  TARGET_EXPAND_BUILTIN
297 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
298
299 #undef TARGET_INIT_LIBFUNCS
300 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
301
302 #undef TARGET_PROMOTE_FUNCTION_ARGS
303 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
304 #undef TARGET_PROMOTE_FUNCTION_RETURN
305 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
306 #undef TARGET_PROMOTE_PROTOTYPES
307 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
308 #undef TARGET_PASS_BY_REFERENCE
309 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
310 #undef TARGET_ARG_PARTIAL_BYTES
311 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
312
313 #undef  TARGET_SETUP_INCOMING_VARARGS
314 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
315
316 #undef TARGET_DEFAULT_SHORT_ENUMS
317 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
318
319 #undef TARGET_ALIGN_ANON_BITFIELD
320 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
321
322 #undef TARGET_NARROW_VOLATILE_BITFIELD
323 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
324
325 #undef TARGET_CXX_GUARD_TYPE
326 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
327
328 #undef TARGET_CXX_GUARD_MASK_BIT
329 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
330
331 #undef TARGET_CXX_GET_COOKIE_SIZE
332 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
333
334 #undef TARGET_CXX_COOKIE_HAS_SIZE
335 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
336
337 #undef TARGET_CXX_CDTOR_RETURNS_THIS
338 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
339
340 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
341 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
342
343 #undef TARGET_CXX_USE_AEABI_ATEXIT
344 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
345
346 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
347 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
348   arm_cxx_determine_class_data_visibility
349
350 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
351 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
352
353 #undef TARGET_RETURN_IN_MSB
354 #define TARGET_RETURN_IN_MSB arm_return_in_msb
355
356 #undef TARGET_MUST_PASS_IN_STACK
357 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
358
359 #ifdef TARGET_UNWIND_INFO
360 #undef TARGET_UNWIND_EMIT
361 #define TARGET_UNWIND_EMIT arm_unwind_emit
362
363 /* EABI unwinding tables use a different format for the typeinfo tables.  */
364 #undef TARGET_ASM_TTYPE
365 #define TARGET_ASM_TTYPE arm_output_ttype
366
367 #undef TARGET_ARM_EABI_UNWINDER
368 #define TARGET_ARM_EABI_UNWINDER true
369 #endif /* TARGET_UNWIND_INFO */
370
371 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
372 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
373
374 #undef  TARGET_CANNOT_COPY_INSN_P
375 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
376
377 #ifdef HAVE_AS_TLS
378 #undef TARGET_HAVE_TLS
379 #define TARGET_HAVE_TLS true
380 #endif
381
382 #undef TARGET_CANNOT_FORCE_CONST_MEM
383 #define TARGET_CANNOT_FORCE_CONST_MEM arm_cannot_force_const_mem
384
385 #undef TARGET_MANGLE_TYPE
386 #define TARGET_MANGLE_TYPE arm_mangle_type
387
388 #ifdef HAVE_AS_TLS
389 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
390 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
391 #endif
392
393 struct gcc_target targetm = TARGET_INITIALIZER;
394 \f
395 /* Obstack for minipool constant handling.  */
396 static struct obstack minipool_obstack;
397 static char *         minipool_startobj;
398
399 /* The maximum number of insns skipped which
400    will be conditionalised if possible.  */
401 static int max_insns_skipped = 5;
402
403 extern FILE * asm_out_file;
404
405 /* True if we are currently building a constant table.  */
406 int making_const_table;
407
408 /* Define the information needed to generate branch insns.  This is
409    stored from the compare operation.  */
410 rtx arm_compare_op0, arm_compare_op1;
411
412 /* The processor for which instructions should be scheduled.  */
413 enum processor_type arm_tune = arm_none;
414
415 /* The default processor used if not overridden by commandline.  */
416 static enum processor_type arm_default_cpu = arm_none;
417
418 /* Which floating point model to use.  */
419 enum arm_fp_model arm_fp_model;
420
421 /* Which floating point hardware is available.  */
422 enum fputype arm_fpu_arch;
423
424 /* Which floating point hardware to schedule for.  */
425 enum fputype arm_fpu_tune;
426
427 /* Whether to use floating point hardware.  */
428 enum float_abi_type arm_float_abi;
429
430 /* Which ABI to use.  */
431 enum arm_abi_type arm_abi;
432
433 /* Which thread pointer model to use.  */
434 enum arm_tp_type target_thread_pointer = TP_AUTO;
435
436 /* Used to parse -mstructure_size_boundary command line option.  */
437 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
438
439 /* Used for Thumb call_via trampolines.  */
440 rtx thumb_call_via_label[14];
441 static int thumb_call_reg_needed;
442
443 /* Bit values used to identify processor capabilities.  */
444 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
445 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
446 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
447 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
448 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
449 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
450 #define FL_THUMB      (1 << 6)        /* Thumb aware */
451 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
452 #define FL_STRONG     (1 << 8)        /* StrongARM */
453 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
454 #define FL_XSCALE     (1 << 10)       /* XScale */
455 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
456 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
457                                          media instructions.  */
458 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
459 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
460                                          Note: ARM6 & 7 derivatives only.  */
461 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
462 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
463 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
464                                          profile.  */
465 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
466 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
467 #define FL_NEON       (1 << 20)       /* Neon instructions.  */
468
469 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
470
471 #define FL_FOR_ARCH2    FL_NOTM
472 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
473 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
474 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
475 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
476 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
477 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
478 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
479 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
480 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
481 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
482 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
483 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
484 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
485 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
486 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
487 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
488 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
489 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
490 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
491
492 /* The bits in this mask specify which
493    instructions we are allowed to generate.  */
494 static unsigned long insn_flags = 0;
495
496 /* The bits in this mask specify which instruction scheduling options should
497    be used.  */
498 static unsigned long tune_flags = 0;
499
500 /* The following are used in the arm.md file as equivalents to bits
501    in the above two flag variables.  */
502
503 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
504 int arm_arch3m = 0;
505
506 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
507 int arm_arch4 = 0;
508
509 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
510 int arm_arch4t = 0;
511
512 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
513 int arm_arch5 = 0;
514
515 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
516 int arm_arch5e = 0;
517
518 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
519 int arm_arch6 = 0;
520
521 /* Nonzero if this chip supports the ARM 6K extensions.  */
522 int arm_arch6k = 0;
523
524 /* Nonzero if instructions not present in the 'M' profile can be used.  */
525 int arm_arch_notm = 0;
526
527 /* Nonzero if this chip can benefit from load scheduling.  */
528 int arm_ld_sched = 0;
529
530 /* Nonzero if this chip is a StrongARM.  */
531 int arm_tune_strongarm = 0;
532
533 /* Nonzero if this chip is a Cirrus variant.  */
534 int arm_arch_cirrus = 0;
535
536 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
537 int arm_arch_iwmmxt = 0;
538
539 /* Nonzero if this chip is an XScale.  */
540 int arm_arch_xscale = 0;
541
542 /* Nonzero if tuning for XScale  */
543 int arm_tune_xscale = 0;
544
545 /* Nonzero if we want to tune for stores that access the write-buffer.
546    This typically means an ARM6 or ARM7 with MMU or MPU.  */
547 int arm_tune_wbuf = 0;
548
549 /* Nonzero if generating Thumb instructions.  */
550 int thumb_code = 0;
551
552 /* Nonzero if we should define __THUMB_INTERWORK__ in the
553    preprocessor.
554    XXX This is a bit of a hack, it's intended to help work around
555    problems in GLD which doesn't understand that armv5t code is
556    interworking clean.  */
557 int arm_cpp_interwork = 0;
558
559 /* Nonzero if chip supports Thumb 2.  */
560 int arm_arch_thumb2;
561
562 /* Nonzero if chip supports integer division instruction.  */
563 int arm_arch_hwdiv;
564
565 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
566    must report the mode of the memory reference from PRINT_OPERAND to
567    PRINT_OPERAND_ADDRESS.  */
568 enum machine_mode output_memory_reference_mode;
569
570 /* The register number to be used for the PIC offset register.  */
571 unsigned arm_pic_register = INVALID_REGNUM;
572
573 /* Set to 1 when a return insn is output, this means that the epilogue
574    is not needed.  */
575 int return_used_this_function;
576
577 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
578    the next function.  */
579 static int after_arm_reorg = 0;
580
581 /* The maximum number of insns to be used when loading a constant.  */
582 static int arm_constant_limit = 3;
583
584 /* For an explanation of these variables, see final_prescan_insn below.  */
585 int arm_ccfsm_state;
586 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
587 enum arm_cond_code arm_current_cc;
588 rtx arm_target_insn;
589 int arm_target_label;
590 /* The number of conditionally executed insns, including the current insn.  */
591 int arm_condexec_count = 0;
592 /* A bitmask specifying the patterns for the IT block.
593    Zero means do not output an IT block before this insn. */
594 int arm_condexec_mask = 0;
595 /* The number of bits used in arm_condexec_mask.  */
596 int arm_condexec_masklen = 0;
597
598 /* The condition codes of the ARM, and the inverse function.  */
599 static const char * const arm_condition_codes[] =
600 {
601   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
602   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
603 };
604
605 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
606 #define streq(string1, string2) (strcmp (string1, string2) == 0)
607
608 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
609                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
610                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
611 \f
612 /* Initialization code.  */
613
614 struct processors
615 {
616   const char *const name;
617   enum processor_type core;
618   const char *arch;
619   const unsigned long flags;
620   bool (* rtx_costs) (rtx, int, int, int *);
621 };
622
623 /* Not all of these give usefully different compilation alternatives,
624    but there is no simple way of generalizing them.  */
625 static const struct processors all_cores[] =
626 {
627   /* ARM Cores */
628 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
629   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
630 #include "arm-cores.def"
631 #undef ARM_CORE
632   {NULL, arm_none, NULL, 0, NULL}
633 };
634
635 static const struct processors all_architectures[] =
636 {
637   /* ARM Architectures */
638   /* We don't specify rtx_costs here as it will be figured out
639      from the core.  */
640
641   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
642   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
643   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
644   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
645   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
646   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
647      implementations that support it, so we will leave it out for now.  */
648   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
649   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
650   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
651   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
652   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
653   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
654   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
655   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
656   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
657   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
658   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
659   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
660   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
661   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
662   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
663   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
664   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
665   {NULL, arm_none, NULL, 0 , NULL}
666 };
667
668 struct arm_cpu_select
669 {
670   const char *              string;
671   const char *              name;
672   const struct processors * processors;
673 };
674
675 /* This is a magic structure.  The 'string' field is magically filled in
676    with a pointer to the value specified by the user on the command line
677    assuming that the user has specified such a value.  */
678
679 static struct arm_cpu_select arm_select[] =
680 {
681   /* string       name            processors  */
682   { NULL,       "-mcpu=",       all_cores  },
683   { NULL,       "-march=",      all_architectures },
684   { NULL,       "-mtune=",      all_cores }
685 };
686
687 /* Defines representing the indexes into the above table.  */
688 #define ARM_OPT_SET_CPU 0
689 #define ARM_OPT_SET_ARCH 1
690 #define ARM_OPT_SET_TUNE 2
691
692 /* The name of the preprocessor macro to define for this architecture.  */
693
694 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
695
696 struct fpu_desc
697 {
698   const char * name;
699   enum fputype fpu;
700 };
701
702
703 /* Available values for -mfpu=.  */
704
705 static const struct fpu_desc all_fpus[] =
706 {
707   {"fpa",       FPUTYPE_FPA},
708   {"fpe2",      FPUTYPE_FPA_EMU2},
709   {"fpe3",      FPUTYPE_FPA_EMU2},
710   {"maverick",  FPUTYPE_MAVERICK},
711   {"vfp",       FPUTYPE_VFP},
712   {"vfp3",      FPUTYPE_VFP3},
713   {"neon",      FPUTYPE_NEON}
714 };
715
716
717 /* Floating point models used by the different hardware.
718    See fputype in arm.h.  */
719
720 static const enum fputype fp_model_for_fpu[] =
721 {
722   /* No FP hardware.  */
723   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
724   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
725   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
726   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
727   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
728   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP  */
729   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP3  */
730   ARM_FP_MODEL_VFP              /* FPUTYPE_NEON  */
731 };
732
733
734 struct float_abi
735 {
736   const char * name;
737   enum float_abi_type abi_type;
738 };
739
740
741 /* Available values for -mfloat-abi=.  */
742
743 static const struct float_abi all_float_abis[] =
744 {
745   {"soft",      ARM_FLOAT_ABI_SOFT},
746   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
747   {"hard",      ARM_FLOAT_ABI_HARD}
748 };
749
750
751 struct abi_name
752 {
753   const char *name;
754   enum arm_abi_type abi_type;
755 };
756
757
758 /* Available values for -mabi=.  */
759
760 static const struct abi_name arm_all_abis[] =
761 {
762   {"apcs-gnu",    ARM_ABI_APCS},
763   {"atpcs",   ARM_ABI_ATPCS},
764   {"aapcs",   ARM_ABI_AAPCS},
765   {"iwmmxt",  ARM_ABI_IWMMXT},
766   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
767 };
768
769 /* Supported TLS relocations.  */
770
771 enum tls_reloc {
772   TLS_GD32,
773   TLS_LDM32,
774   TLS_LDO32,
775   TLS_IE32,
776   TLS_LE32
777 };
778
779 /* Emit an insn that's a simple single-set.  Both the operands must be known
780    to be valid.  */
781 inline static rtx
782 emit_set_insn (rtx x, rtx y)
783 {
784   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
785 }
786
787 /* Return the number of bits set in VALUE.  */
788 static unsigned
789 bit_count (unsigned long value)
790 {
791   unsigned long count = 0;
792
793   while (value)
794     {
795       count++;
796       value &= value - 1;  /* Clear the least-significant set bit.  */
797     }
798
799   return count;
800 }
801
802 /* Set up library functions unique to ARM.  */
803
804 static void
805 arm_init_libfuncs (void)
806 {
807   /* There are no special library functions unless we are using the
808      ARM BPABI.  */
809   if (!TARGET_BPABI)
810     return;
811
812   /* The functions below are described in Section 4 of the "Run-Time
813      ABI for the ARM architecture", Version 1.0.  */
814
815   /* Double-precision floating-point arithmetic.  Table 2.  */
816   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
817   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
818   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
819   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
820   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
821
822   /* Double-precision comparisons.  Table 3.  */
823   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
824   set_optab_libfunc (ne_optab, DFmode, NULL);
825   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
826   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
827   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
828   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
829   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
830
831   /* Single-precision floating-point arithmetic.  Table 4.  */
832   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
833   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
834   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
835   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
836   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
837
838   /* Single-precision comparisons.  Table 5.  */
839   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
840   set_optab_libfunc (ne_optab, SFmode, NULL);
841   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
842   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
843   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
844   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
845   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
846
847   /* Floating-point to integer conversions.  Table 6.  */
848   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
849   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
850   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
851   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
852   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
853   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
854   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
855   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
856
857   /* Conversions between floating types.  Table 7.  */
858   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
859   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
860
861   /* Integer to floating-point conversions.  Table 8.  */
862   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
863   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
864   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
865   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
866   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
867   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
868   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
869   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
870
871   /* Long long.  Table 9.  */
872   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
873   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
874   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
875   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
876   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
877   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
878   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
879   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
880
881   /* Integer (32/32->32) division.  \S 4.3.1.  */
882   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
883   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
884
885   /* The divmod functions are designed so that they can be used for
886      plain division, even though they return both the quotient and the
887      remainder.  The quotient is returned in the usual location (i.e.,
888      r0 for SImode, {r0, r1} for DImode), just as would be expected
889      for an ordinary division routine.  Because the AAPCS calling
890      conventions specify that all of { r0, r1, r2, r3 } are
891      callee-saved registers, there is no need to tell the compiler
892      explicitly that those registers are clobbered by these
893      routines.  */
894   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
895   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
896
897   /* For SImode division the ABI provides div-without-mod routines,
898      which are faster.  */
899   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
900   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
901
902   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
903      divmod libcalls instead.  */
904   set_optab_libfunc (smod_optab, DImode, NULL);
905   set_optab_libfunc (umod_optab, DImode, NULL);
906   set_optab_libfunc (smod_optab, SImode, NULL);
907   set_optab_libfunc (umod_optab, SImode, NULL);
908 }
909
910 /* Implement TARGET_HANDLE_OPTION.  */
911
912 static bool
913 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
914 {
915   switch (code)
916     {
917     case OPT_march_:
918       arm_select[1].string = arg;
919       return true;
920
921     case OPT_mcpu_:
922       arm_select[0].string = arg;
923       return true;
924
925     case OPT_mhard_float:
926       target_float_abi_name = "hard";
927       return true;
928
929     case OPT_msoft_float:
930       target_float_abi_name = "soft";
931       return true;
932
933     case OPT_mtune_:
934       arm_select[2].string = arg;
935       return true;
936
937     default:
938       return true;
939     }
940 }
941
942 static void
943 arm_target_help (void)
944 {
945   int i;
946   static int columns = 0;
947   int remaining;
948
949   /* If we have not done so already, obtain the desired maximum width of
950      the output.  Note - this is a duplication of the code at the start of
951      gcc/opts.c:print_specific_help() - the two copies should probably be
952      replaced by a single function.  */
953   if (columns == 0)
954     {
955       const char *p;
956
957       GET_ENVIRONMENT (p, "COLUMNS");
958       if (p != NULL)
959         {
960           int value = atoi (p);
961
962           if (value > 0)
963             columns = value;
964         }
965
966       if (columns == 0)
967         /* Use a reasonable default.  */
968         columns = 80;
969     }
970
971   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
972
973   /* The - 2 is because we know that the last entry in the array is NULL.  */
974   i = ARRAY_SIZE (all_cores) - 2;
975   gcc_assert (i > 0);
976   printf ("    %s", all_cores[i].name);
977   remaining = columns - (strlen (all_cores[i].name) + 4);
978   gcc_assert (remaining >= 0);
979
980   while (i--)
981     {
982       int len = strlen (all_cores[i].name);
983
984       if (remaining > len + 2)
985         {
986           printf (", %s", all_cores[i].name);
987           remaining -= len + 2;
988         }
989       else
990         {
991           if (remaining > 0)
992             printf (",");
993           printf ("\n    %s", all_cores[i].name);
994           remaining = columns - (len + 4);
995         }
996     }
997
998   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
999
1000   i = ARRAY_SIZE (all_architectures) - 2;
1001   gcc_assert (i > 0);
1002   
1003   printf ("    %s", all_architectures[i].name);
1004   remaining = columns - (strlen (all_architectures[i].name) + 4);
1005   gcc_assert (remaining >= 0);
1006
1007   while (i--)
1008     {
1009       int len = strlen (all_architectures[i].name);
1010
1011       if (remaining > len + 2)
1012         {
1013           printf (", %s", all_architectures[i].name);
1014           remaining -= len + 2;
1015         }
1016       else
1017         {
1018           if (remaining > 0)
1019             printf (",");
1020           printf ("\n    %s", all_architectures[i].name);
1021           remaining = columns - (len + 4);
1022         }
1023     }
1024   printf ("\n");
1025
1026 }
1027
1028 /* Fix up any incompatible options that the user has specified.
1029    This has now turned into a maze.  */
1030 void
1031 arm_override_options (void)
1032 {
1033   unsigned i;
1034   enum processor_type target_arch_cpu = arm_none;
1035
1036   /* Set up the flags based on the cpu/architecture selected by the user.  */
1037   for (i = ARRAY_SIZE (arm_select); i--;)
1038     {
1039       struct arm_cpu_select * ptr = arm_select + i;
1040
1041       if (ptr->string != NULL && ptr->string[0] != '\0')
1042         {
1043           const struct processors * sel;
1044
1045           for (sel = ptr->processors; sel->name != NULL; sel++)
1046             if (streq (ptr->string, sel->name))
1047               {
1048                 /* Set the architecture define.  */
1049                 if (i != ARM_OPT_SET_TUNE)
1050                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1051
1052                 /* Determine the processor core for which we should
1053                    tune code-generation.  */
1054                 if (/* -mcpu= is a sensible default.  */
1055                     i == ARM_OPT_SET_CPU
1056                     /* -mtune= overrides -mcpu= and -march=.  */
1057                     || i == ARM_OPT_SET_TUNE)
1058                   arm_tune = (enum processor_type) (sel - ptr->processors);
1059
1060                 /* Remember the CPU associated with this architecture.
1061                    If no other option is used to set the CPU type,
1062                    we'll use this to guess the most suitable tuning
1063                    options.  */
1064                 if (i == ARM_OPT_SET_ARCH)
1065                   target_arch_cpu = sel->core;
1066
1067                 if (i != ARM_OPT_SET_TUNE)
1068                   {
1069                     /* If we have been given an architecture and a processor
1070                        make sure that they are compatible.  We only generate
1071                        a warning though, and we prefer the CPU over the
1072                        architecture.  */
1073                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
1074                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
1075                                ptr->string);
1076
1077                     insn_flags = sel->flags;
1078                   }
1079
1080                 break;
1081               }
1082
1083           if (sel->name == NULL)
1084             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
1085         }
1086     }
1087
1088   /* Guess the tuning options from the architecture if necessary.  */
1089   if (arm_tune == arm_none)
1090     arm_tune = target_arch_cpu;
1091
1092   /* If the user did not specify a processor, choose one for them.  */
1093   if (insn_flags == 0)
1094     {
1095       const struct processors * sel;
1096       unsigned int        sought;
1097       enum processor_type cpu;
1098
1099       cpu = TARGET_CPU_DEFAULT;
1100       if (cpu == arm_none)
1101         {
1102 #ifdef SUBTARGET_CPU_DEFAULT
1103           /* Use the subtarget default CPU if none was specified by
1104              configure.  */
1105           cpu = SUBTARGET_CPU_DEFAULT;
1106 #endif
1107           /* Default to ARM6.  */
1108           if (cpu == arm_none)
1109             cpu = arm6;
1110         }
1111       sel = &all_cores[cpu];
1112
1113       insn_flags = sel->flags;
1114
1115       /* Now check to see if the user has specified some command line
1116          switch that require certain abilities from the cpu.  */
1117       sought = 0;
1118
1119       if (TARGET_INTERWORK || TARGET_THUMB)
1120         {
1121           sought |= (FL_THUMB | FL_MODE32);
1122
1123           /* There are no ARM processors that support both APCS-26 and
1124              interworking.  Therefore we force FL_MODE26 to be removed
1125              from insn_flags here (if it was set), so that the search
1126              below will always be able to find a compatible processor.  */
1127           insn_flags &= ~FL_MODE26;
1128         }
1129
1130       if (sought != 0 && ((sought & insn_flags) != sought))
1131         {
1132           /* Try to locate a CPU type that supports all of the abilities
1133              of the default CPU, plus the extra abilities requested by
1134              the user.  */
1135           for (sel = all_cores; sel->name != NULL; sel++)
1136             if ((sel->flags & sought) == (sought | insn_flags))
1137               break;
1138
1139           if (sel->name == NULL)
1140             {
1141               unsigned current_bit_count = 0;
1142               const struct processors * best_fit = NULL;
1143
1144               /* Ideally we would like to issue an error message here
1145                  saying that it was not possible to find a CPU compatible
1146                  with the default CPU, but which also supports the command
1147                  line options specified by the programmer, and so they
1148                  ought to use the -mcpu=<name> command line option to
1149                  override the default CPU type.
1150
1151                  If we cannot find a cpu that has both the
1152                  characteristics of the default cpu and the given
1153                  command line options we scan the array again looking
1154                  for a best match.  */
1155               for (sel = all_cores; sel->name != NULL; sel++)
1156                 if ((sel->flags & sought) == sought)
1157                   {
1158                     unsigned count;
1159
1160                     count = bit_count (sel->flags & insn_flags);
1161
1162                     if (count >= current_bit_count)
1163                       {
1164                         best_fit = sel;
1165                         current_bit_count = count;
1166                       }
1167                   }
1168
1169               gcc_assert (best_fit);
1170               sel = best_fit;
1171             }
1172
1173           insn_flags = sel->flags;
1174         }
1175       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1176       arm_default_cpu = (enum processor_type) (sel - all_cores);
1177       if (arm_tune == arm_none)
1178         arm_tune = arm_default_cpu;
1179     }
1180
1181   /* The processor for which we should tune should now have been
1182      chosen.  */
1183   gcc_assert (arm_tune != arm_none);
1184
1185   tune_flags = all_cores[(int)arm_tune].flags;
1186   if (optimize_size)
1187     targetm.rtx_costs = arm_size_rtx_costs;
1188   else
1189     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1190
1191   /* Make sure that the processor choice does not conflict with any of the
1192      other command line choices.  */
1193   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1194     error ("target CPU does not support ARM mode");
1195
1196   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1197     {
1198       warning (0, "target CPU does not support interworking" );
1199       target_flags &= ~MASK_INTERWORK;
1200     }
1201
1202   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1203     {
1204       warning (0, "target CPU does not support THUMB instructions");
1205       target_flags &= ~MASK_THUMB;
1206     }
1207
1208   if (TARGET_APCS_FRAME && TARGET_THUMB)
1209     {
1210       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1211       target_flags &= ~MASK_APCS_FRAME;
1212     }
1213
1214   /* Callee super interworking implies thumb interworking.  Adding
1215      this to the flags here simplifies the logic elsewhere.  */
1216   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1217       target_flags |= MASK_INTERWORK;
1218
1219   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1220      from here where no function is being compiled currently.  */
1221   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1222     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1223
1224   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1225     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1226
1227   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1228     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1229
1230   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1231     {
1232       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1233       target_flags |= MASK_APCS_FRAME;
1234     }
1235
1236   if (TARGET_POKE_FUNCTION_NAME)
1237     target_flags |= MASK_APCS_FRAME;
1238
1239   if (TARGET_APCS_REENT && flag_pic)
1240     error ("-fpic and -mapcs-reent are incompatible");
1241
1242   if (TARGET_APCS_REENT)
1243     warning (0, "APCS reentrant code not supported.  Ignored");
1244
1245   /* If this target is normally configured to use APCS frames, warn if they
1246      are turned off and debugging is turned on.  */
1247   if (TARGET_ARM
1248       && write_symbols != NO_DEBUG
1249       && !TARGET_APCS_FRAME
1250       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1251     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1252
1253   if (TARGET_APCS_FLOAT)
1254     warning (0, "passing floating point arguments in fp regs not yet supported");
1255
1256   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1257   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1258   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1259   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1260   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1261   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1262   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1263   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1264   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1265   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1266   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1267   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1268
1269   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1270   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1271   thumb_code = (TARGET_ARM == 0);
1272   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1273   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1274   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1275   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1276
1277   /* V5 code we generate is completely interworking capable, so we turn off
1278      TARGET_INTERWORK here to avoid many tests later on.  */
1279
1280   /* XXX However, we must pass the right pre-processor defines to CPP
1281      or GLD can get confused.  This is a hack.  */
1282   if (TARGET_INTERWORK)
1283     arm_cpp_interwork = 1;
1284
1285   if (arm_arch5)
1286     target_flags &= ~MASK_INTERWORK;
1287
1288   if (target_abi_name)
1289     {
1290       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1291         {
1292           if (streq (arm_all_abis[i].name, target_abi_name))
1293             {
1294               arm_abi = arm_all_abis[i].abi_type;
1295               break;
1296             }
1297         }
1298       if (i == ARRAY_SIZE (arm_all_abis))
1299         error ("invalid ABI option: -mabi=%s", target_abi_name);
1300     }
1301   else
1302     arm_abi = ARM_DEFAULT_ABI;
1303
1304   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1305     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1306
1307   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1308     error ("iwmmxt abi requires an iwmmxt capable cpu");
1309
1310   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1311   if (target_fpu_name == NULL && target_fpe_name != NULL)
1312     {
1313       if (streq (target_fpe_name, "2"))
1314         target_fpu_name = "fpe2";
1315       else if (streq (target_fpe_name, "3"))
1316         target_fpu_name = "fpe3";
1317       else
1318         error ("invalid floating point emulation option: -mfpe=%s",
1319                target_fpe_name);
1320     }
1321   if (target_fpu_name != NULL)
1322     {
1323       /* The user specified a FPU.  */
1324       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1325         {
1326           if (streq (all_fpus[i].name, target_fpu_name))
1327             {
1328               arm_fpu_arch = all_fpus[i].fpu;
1329               arm_fpu_tune = arm_fpu_arch;
1330               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1331               break;
1332             }
1333         }
1334       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1335         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1336     }
1337   else
1338     {
1339 #ifdef FPUTYPE_DEFAULT
1340       /* Use the default if it is specified for this platform.  */
1341       arm_fpu_arch = FPUTYPE_DEFAULT;
1342       arm_fpu_tune = FPUTYPE_DEFAULT;
1343 #else
1344       /* Pick one based on CPU type.  */
1345       /* ??? Some targets assume FPA is the default.
1346       if ((insn_flags & FL_VFP) != 0)
1347         arm_fpu_arch = FPUTYPE_VFP;
1348       else
1349       */
1350       if (arm_arch_cirrus)
1351         arm_fpu_arch = FPUTYPE_MAVERICK;
1352       else
1353         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1354 #endif
1355       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1356         arm_fpu_tune = FPUTYPE_FPA;
1357       else
1358         arm_fpu_tune = arm_fpu_arch;
1359       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1360       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1361     }
1362
1363   if (target_float_abi_name != NULL)
1364     {
1365       /* The user specified a FP ABI.  */
1366       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1367         {
1368           if (streq (all_float_abis[i].name, target_float_abi_name))
1369             {
1370               arm_float_abi = all_float_abis[i].abi_type;
1371               break;
1372             }
1373         }
1374       if (i == ARRAY_SIZE (all_float_abis))
1375         error ("invalid floating point abi: -mfloat-abi=%s",
1376                target_float_abi_name);
1377     }
1378   else
1379     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1380
1381   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1382     sorry ("-mfloat-abi=hard and VFP");
1383
1384   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1385      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1386      will ever exist.  GCC makes no attempt to support this combination.  */
1387   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1388     sorry ("iWMMXt and hardware floating point");
1389
1390   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1391   if (TARGET_THUMB2 && TARGET_IWMMXT)
1392     sorry ("Thumb-2 iWMMXt");
1393
1394   /* If soft-float is specified then don't use FPU.  */
1395   if (TARGET_SOFT_FLOAT)
1396     arm_fpu_arch = FPUTYPE_NONE;
1397
1398   /* For arm2/3 there is no need to do any scheduling if there is only
1399      a floating point emulator, or we are doing software floating-point.  */
1400   if ((TARGET_SOFT_FLOAT
1401        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1402        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1403       && (tune_flags & FL_MODE32) == 0)
1404     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1405
1406   if (target_thread_switch)
1407     {
1408       if (strcmp (target_thread_switch, "soft") == 0)
1409         target_thread_pointer = TP_SOFT;
1410       else if (strcmp (target_thread_switch, "auto") == 0)
1411         target_thread_pointer = TP_AUTO;
1412       else if (strcmp (target_thread_switch, "cp15") == 0)
1413         target_thread_pointer = TP_CP15;
1414       else
1415         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1416     }
1417
1418   /* Use the cp15 method if it is available.  */
1419   if (target_thread_pointer == TP_AUTO)
1420     {
1421       if (arm_arch6k && !TARGET_THUMB)
1422         target_thread_pointer = TP_CP15;
1423       else
1424         target_thread_pointer = TP_SOFT;
1425     }
1426
1427   if (TARGET_HARD_TP && TARGET_THUMB1)
1428     error ("can not use -mtp=cp15 with 16-bit Thumb");
1429
1430   /* Override the default structure alignment for AAPCS ABI.  */
1431   if (TARGET_AAPCS_BASED)
1432     arm_structure_size_boundary = 8;
1433
1434   if (structure_size_string != NULL)
1435     {
1436       int size = strtol (structure_size_string, NULL, 0);
1437
1438       if (size == 8 || size == 32
1439           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1440         arm_structure_size_boundary = size;
1441       else
1442         warning (0, "structure size boundary can only be set to %s",
1443                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1444     }
1445
1446   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1447     {
1448       error ("RTP PIC is incompatible with Thumb");
1449       flag_pic = 0;
1450     }
1451
1452   /* If stack checking is disabled, we can use r10 as the PIC register,
1453      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1454   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1455     {
1456       if (TARGET_VXWORKS_RTP)
1457         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1458       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1459     }
1460
1461   if (flag_pic && TARGET_VXWORKS_RTP)
1462     arm_pic_register = 9;
1463
1464   if (arm_pic_register_string != NULL)
1465     {
1466       int pic_register = decode_reg_name (arm_pic_register_string);
1467
1468       if (!flag_pic)
1469         warning (0, "-mpic-register= is useless without -fpic");
1470
1471       /* Prevent the user from choosing an obviously stupid PIC register.  */
1472       else if (pic_register < 0 || call_used_regs[pic_register]
1473                || pic_register == HARD_FRAME_POINTER_REGNUM
1474                || pic_register == STACK_POINTER_REGNUM
1475                || pic_register >= PC_REGNUM
1476                || (TARGET_VXWORKS_RTP
1477                    && (unsigned int) pic_register != arm_pic_register))
1478         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1479       else
1480         arm_pic_register = pic_register;
1481     }
1482
1483   /* ??? We might want scheduling for thumb2.  */
1484   if (TARGET_THUMB && flag_schedule_insns)
1485     {
1486       /* Don't warn since it's on by default in -O2.  */
1487       flag_schedule_insns = 0;
1488     }
1489
1490   if (optimize_size)
1491     {
1492       arm_constant_limit = 1;
1493
1494       /* If optimizing for size, bump the number of instructions that we
1495          are prepared to conditionally execute (even on a StrongARM).  */
1496       max_insns_skipped = 6;
1497     }
1498   else
1499     {
1500       /* For processors with load scheduling, it never costs more than
1501          2 cycles to load a constant, and the load scheduler may well
1502          reduce that to 1.  */
1503       if (arm_ld_sched)
1504         arm_constant_limit = 1;
1505
1506       /* On XScale the longer latency of a load makes it more difficult
1507          to achieve a good schedule, so it's faster to synthesize
1508          constants that can be done in two insns.  */
1509       if (arm_tune_xscale)
1510         arm_constant_limit = 2;
1511
1512       /* StrongARM has early execution of branches, so a sequence
1513          that is worth skipping is shorter.  */
1514       if (arm_tune_strongarm)
1515         max_insns_skipped = 3;
1516     }
1517
1518   /* Register global variables with the garbage collector.  */
1519   arm_add_gc_roots ();
1520 }
1521
1522 static void
1523 arm_add_gc_roots (void)
1524 {
1525   gcc_obstack_init(&minipool_obstack);
1526   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1527 }
1528 \f
1529 /* A table of known ARM exception types.
1530    For use with the interrupt function attribute.  */
1531
1532 typedef struct
1533 {
1534   const char *const arg;
1535   const unsigned long return_value;
1536 }
1537 isr_attribute_arg;
1538
1539 static const isr_attribute_arg isr_attribute_args [] =
1540 {
1541   { "IRQ",   ARM_FT_ISR },
1542   { "irq",   ARM_FT_ISR },
1543   { "FIQ",   ARM_FT_FIQ },
1544   { "fiq",   ARM_FT_FIQ },
1545   { "ABORT", ARM_FT_ISR },
1546   { "abort", ARM_FT_ISR },
1547   { "ABORT", ARM_FT_ISR },
1548   { "abort", ARM_FT_ISR },
1549   { "UNDEF", ARM_FT_EXCEPTION },
1550   { "undef", ARM_FT_EXCEPTION },
1551   { "SWI",   ARM_FT_EXCEPTION },
1552   { "swi",   ARM_FT_EXCEPTION },
1553   { NULL,    ARM_FT_NORMAL }
1554 };
1555
1556 /* Returns the (interrupt) function type of the current
1557    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1558
1559 static unsigned long
1560 arm_isr_value (tree argument)
1561 {
1562   const isr_attribute_arg * ptr;
1563   const char *              arg;
1564
1565   if (!arm_arch_notm)
1566     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1567
1568   /* No argument - default to IRQ.  */
1569   if (argument == NULL_TREE)
1570     return ARM_FT_ISR;
1571
1572   /* Get the value of the argument.  */
1573   if (TREE_VALUE (argument) == NULL_TREE
1574       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1575     return ARM_FT_UNKNOWN;
1576
1577   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1578
1579   /* Check it against the list of known arguments.  */
1580   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1581     if (streq (arg, ptr->arg))
1582       return ptr->return_value;
1583
1584   /* An unrecognized interrupt type.  */
1585   return ARM_FT_UNKNOWN;
1586 }
1587
1588 /* Computes the type of the current function.  */
1589
1590 static unsigned long
1591 arm_compute_func_type (void)
1592 {
1593   unsigned long type = ARM_FT_UNKNOWN;
1594   tree a;
1595   tree attr;
1596
1597   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1598
1599   /* Decide if the current function is volatile.  Such functions
1600      never return, and many memory cycles can be saved by not storing
1601      register values that will never be needed again.  This optimization
1602      was added to speed up context switching in a kernel application.  */
1603   if (optimize > 0
1604       && (TREE_NOTHROW (current_function_decl)
1605           || !(flag_unwind_tables
1606                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1607       && TREE_THIS_VOLATILE (current_function_decl))
1608     type |= ARM_FT_VOLATILE;
1609
1610   if (cfun->static_chain_decl != NULL)
1611     type |= ARM_FT_NESTED;
1612
1613   attr = DECL_ATTRIBUTES (current_function_decl);
1614
1615   a = lookup_attribute ("naked", attr);
1616   if (a != NULL_TREE)
1617     type |= ARM_FT_NAKED;
1618
1619   a = lookup_attribute ("isr", attr);
1620   if (a == NULL_TREE)
1621     a = lookup_attribute ("interrupt", attr);
1622
1623   if (a == NULL_TREE)
1624     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1625   else
1626     type |= arm_isr_value (TREE_VALUE (a));
1627
1628   return type;
1629 }
1630
1631 /* Returns the type of the current function.  */
1632
1633 unsigned long
1634 arm_current_func_type (void)
1635 {
1636   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1637     cfun->machine->func_type = arm_compute_func_type ();
1638
1639   return cfun->machine->func_type;
1640 }
1641 \f
1642 /* Return 1 if it is possible to return using a single instruction.
1643    If SIBLING is non-null, this is a test for a return before a sibling
1644    call.  SIBLING is the call insn, so we can examine its register usage.  */
1645
1646 int
1647 use_return_insn (int iscond, rtx sibling)
1648 {
1649   int regno;
1650   unsigned int func_type;
1651   unsigned long saved_int_regs;
1652   unsigned HOST_WIDE_INT stack_adjust;
1653   arm_stack_offsets *offsets;
1654
1655   /* Never use a return instruction before reload has run.  */
1656   if (!reload_completed)
1657     return 0;
1658
1659   func_type = arm_current_func_type ();
1660
1661   /* Naked, volatile and stack alignment functions need special
1662      consideration.  */
1663   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1664     return 0;
1665
1666   /* So do interrupt functions that use the frame pointer and Thumb
1667      interrupt functions.  */
1668   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1669     return 0;
1670
1671   offsets = arm_get_frame_offsets ();
1672   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1673
1674   /* As do variadic functions.  */
1675   if (current_function_pretend_args_size
1676       || cfun->machine->uses_anonymous_args
1677       /* Or if the function calls __builtin_eh_return () */
1678       || current_function_calls_eh_return
1679       /* Or if the function calls alloca */
1680       || current_function_calls_alloca
1681       /* Or if there is a stack adjustment.  However, if the stack pointer
1682          is saved on the stack, we can use a pre-incrementing stack load.  */
1683       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1684     return 0;
1685
1686   saved_int_regs = arm_compute_save_reg_mask ();
1687
1688   /* Unfortunately, the insn
1689
1690        ldmib sp, {..., sp, ...}
1691
1692      triggers a bug on most SA-110 based devices, such that the stack
1693      pointer won't be correctly restored if the instruction takes a
1694      page fault.  We work around this problem by popping r3 along with
1695      the other registers, since that is never slower than executing
1696      another instruction.
1697
1698      We test for !arm_arch5 here, because code for any architecture
1699      less than this could potentially be run on one of the buggy
1700      chips.  */
1701   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1702     {
1703       /* Validate that r3 is a call-clobbered register (always true in
1704          the default abi) ...  */
1705       if (!call_used_regs[3])
1706         return 0;
1707
1708       /* ... that it isn't being used for a return value ... */
1709       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1710         return 0;
1711
1712       /* ... or for a tail-call argument ...  */
1713       if (sibling)
1714         {
1715           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1716
1717           if (find_regno_fusage (sibling, USE, 3))
1718             return 0;
1719         }
1720
1721       /* ... and that there are no call-saved registers in r0-r2
1722          (always true in the default ABI).  */
1723       if (saved_int_regs & 0x7)
1724         return 0;
1725     }
1726
1727   /* Can't be done if interworking with Thumb, and any registers have been
1728      stacked.  */
1729   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1730     return 0;
1731
1732   /* On StrongARM, conditional returns are expensive if they aren't
1733      taken and multiple registers have been stacked.  */
1734   if (iscond && arm_tune_strongarm)
1735     {
1736       /* Conditional return when just the LR is stored is a simple
1737          conditional-load instruction, that's not expensive.  */
1738       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1739         return 0;
1740
1741       if (flag_pic 
1742           && arm_pic_register != INVALID_REGNUM
1743           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1744         return 0;
1745     }
1746
1747   /* If there are saved registers but the LR isn't saved, then we need
1748      two instructions for the return.  */
1749   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1750     return 0;
1751
1752   /* Can't be done if any of the FPA regs are pushed,
1753      since this also requires an insn.  */
1754   if (TARGET_HARD_FLOAT && TARGET_FPA)
1755     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1756       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
1757         return 0;
1758
1759   /* Likewise VFP regs.  */
1760   if (TARGET_HARD_FLOAT && TARGET_VFP)
1761     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1762       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
1763         return 0;
1764
1765   if (TARGET_REALLY_IWMMXT)
1766     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1767       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
1768         return 0;
1769
1770   return 1;
1771 }
1772
1773 /* Return TRUE if int I is a valid immediate ARM constant.  */
1774
1775 int
1776 const_ok_for_arm (HOST_WIDE_INT i)
1777 {
1778   int lowbit;
1779
1780   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1781      be all zero, or all one.  */
1782   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1783       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1784           != ((~(unsigned HOST_WIDE_INT) 0)
1785               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1786     return FALSE;
1787
1788   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1789
1790   /* Fast return for 0 and small values.  We must do this for zero, since
1791      the code below can't handle that one case.  */
1792   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1793     return TRUE;
1794
1795   /* Get the number of trailing zeros.  */
1796   lowbit = ffs((int) i) - 1;
1797   
1798   /* Only even shifts are allowed in ARM mode so round down to the
1799      nearest even number.  */
1800   if (TARGET_ARM)
1801     lowbit &= ~1;
1802
1803   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1804     return TRUE;
1805
1806   if (TARGET_ARM)
1807     {
1808       /* Allow rotated constants in ARM mode.  */
1809       if (lowbit <= 4
1810            && ((i & ~0xc000003f) == 0
1811                || (i & ~0xf000000f) == 0
1812                || (i & ~0xfc000003) == 0))
1813         return TRUE;
1814     }
1815   else
1816     {
1817       HOST_WIDE_INT v;
1818
1819       /* Allow repeated pattern.  */
1820       v = i & 0xff;
1821       v |= v << 16;
1822       if (i == v || i == (v | (v << 8)))
1823         return TRUE;
1824     }
1825
1826   return FALSE;
1827 }
1828
1829 /* Return true if I is a valid constant for the operation CODE.  */
1830 static int
1831 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1832 {
1833   if (const_ok_for_arm (i))
1834     return 1;
1835
1836   switch (code)
1837     {
1838     case PLUS:
1839       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1840
1841     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1842     case XOR:
1843     case IOR:
1844       return 0;
1845
1846     case AND:
1847       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1848
1849     default:
1850       gcc_unreachable ();
1851     }
1852 }
1853
1854 /* Emit a sequence of insns to handle a large constant.
1855    CODE is the code of the operation required, it can be any of SET, PLUS,
1856    IOR, AND, XOR, MINUS;
1857    MODE is the mode in which the operation is being performed;
1858    VAL is the integer to operate on;
1859    SOURCE is the other operand (a register, or a null-pointer for SET);
1860    SUBTARGETS means it is safe to create scratch registers if that will
1861    either produce a simpler sequence, or we will want to cse the values.
1862    Return value is the number of insns emitted.  */
1863
1864 /* ??? Tweak this for thumb2.  */
1865 int
1866 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1867                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1868 {
1869   rtx cond;
1870
1871   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1872     cond = COND_EXEC_TEST (PATTERN (insn));
1873   else
1874     cond = NULL_RTX;
1875
1876   if (subtargets || code == SET
1877       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1878           && REGNO (target) != REGNO (source)))
1879     {
1880       /* After arm_reorg has been called, we can't fix up expensive
1881          constants by pushing them into memory so we must synthesize
1882          them in-line, regardless of the cost.  This is only likely to
1883          be more costly on chips that have load delay slots and we are
1884          compiling without running the scheduler (so no splitting
1885          occurred before the final instruction emission).
1886
1887          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1888       */
1889       if (!after_arm_reorg
1890           && !cond
1891           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1892                                 1, 0)
1893               > arm_constant_limit + (code != SET)))
1894         {
1895           if (code == SET)
1896             {
1897               /* Currently SET is the only monadic value for CODE, all
1898                  the rest are diadic.  */
1899               emit_set_insn (target, GEN_INT (val));
1900               return 1;
1901             }
1902           else
1903             {
1904               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1905
1906               emit_set_insn (temp, GEN_INT (val));
1907               /* For MINUS, the value is subtracted from, since we never
1908                  have subtraction of a constant.  */
1909               if (code == MINUS)
1910                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1911               else
1912                 emit_set_insn (target,
1913                                gen_rtx_fmt_ee (code, mode, source, temp));
1914               return 2;
1915             }
1916         }
1917     }
1918
1919   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1920                            1);
1921 }
1922
1923 /* Return the number of ARM instructions required to synthesize the given
1924    constant.  */
1925 static int
1926 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1927 {
1928   HOST_WIDE_INT temp1;
1929   int num_insns = 0;
1930   do
1931     {
1932       int end;
1933
1934       if (i <= 0)
1935         i += 32;
1936       if (remainder & (3 << (i - 2)))
1937         {
1938           end = i - 8;
1939           if (end < 0)
1940             end += 32;
1941           temp1 = remainder & ((0x0ff << end)
1942                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1943           remainder &= ~temp1;
1944           num_insns++;
1945           i -= 6;
1946         }
1947       i -= 2;
1948     } while (remainder);
1949   return num_insns;
1950 }
1951
1952 /* Emit an instruction with the indicated PATTERN.  If COND is
1953    non-NULL, conditionalize the execution of the instruction on COND
1954    being true.  */
1955
1956 static void
1957 emit_constant_insn (rtx cond, rtx pattern)
1958 {
1959   if (cond)
1960     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1961   emit_insn (pattern);
1962 }
1963
1964 /* As above, but extra parameter GENERATE which, if clear, suppresses
1965    RTL generation.  */
1966 /* ??? This needs more work for thumb2.  */
1967
1968 static int
1969 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1970                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1971                   int generate)
1972 {
1973   int can_invert = 0;
1974   int can_negate = 0;
1975   int can_negate_initial = 0;
1976   int can_shift = 0;
1977   int i;
1978   int num_bits_set = 0;
1979   int set_sign_bit_copies = 0;
1980   int clear_sign_bit_copies = 0;
1981   int clear_zero_bit_copies = 0;
1982   int set_zero_bit_copies = 0;
1983   int insns = 0;
1984   unsigned HOST_WIDE_INT temp1, temp2;
1985   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1986
1987   /* Find out which operations are safe for a given CODE.  Also do a quick
1988      check for degenerate cases; these can occur when DImode operations
1989      are split.  */
1990   switch (code)
1991     {
1992     case SET:
1993       can_invert = 1;
1994       can_shift = 1;
1995       can_negate = 1;
1996       break;
1997
1998     case PLUS:
1999       can_negate = 1;
2000       can_negate_initial = 1;
2001       break;
2002
2003     case IOR:
2004       if (remainder == 0xffffffff)
2005         {
2006           if (generate)
2007             emit_constant_insn (cond,
2008                                 gen_rtx_SET (VOIDmode, target,
2009                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2010           return 1;
2011         }
2012       if (remainder == 0)
2013         {
2014           if (reload_completed && rtx_equal_p (target, source))
2015             return 0;
2016           if (generate)
2017             emit_constant_insn (cond,
2018                                 gen_rtx_SET (VOIDmode, target, source));
2019           return 1;
2020         }
2021       break;
2022
2023     case AND:
2024       if (remainder == 0)
2025         {
2026           if (generate)
2027             emit_constant_insn (cond,
2028                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2029           return 1;
2030         }
2031       if (remainder == 0xffffffff)
2032         {
2033           if (reload_completed && rtx_equal_p (target, source))
2034             return 0;
2035           if (generate)
2036             emit_constant_insn (cond,
2037                                 gen_rtx_SET (VOIDmode, target, source));
2038           return 1;
2039         }
2040       can_invert = 1;
2041       break;
2042
2043     case XOR:
2044       if (remainder == 0)
2045         {
2046           if (reload_completed && rtx_equal_p (target, source))
2047             return 0;
2048           if (generate)
2049             emit_constant_insn (cond,
2050                                 gen_rtx_SET (VOIDmode, target, source));
2051           return 1;
2052         }
2053
2054       /* We don't know how to handle other cases yet.  */
2055       gcc_assert (remainder == 0xffffffff);
2056
2057       if (generate)
2058         emit_constant_insn (cond,
2059                             gen_rtx_SET (VOIDmode, target,
2060                                          gen_rtx_NOT (mode, source)));
2061       return 1;
2062
2063     case MINUS:
2064       /* We treat MINUS as (val - source), since (source - val) is always
2065          passed as (source + (-val)).  */
2066       if (remainder == 0)
2067         {
2068           if (generate)
2069             emit_constant_insn (cond,
2070                                 gen_rtx_SET (VOIDmode, target,
2071                                              gen_rtx_NEG (mode, source)));
2072           return 1;
2073         }
2074       if (const_ok_for_arm (val))
2075         {
2076           if (generate)
2077             emit_constant_insn (cond,
2078                                 gen_rtx_SET (VOIDmode, target,
2079                                              gen_rtx_MINUS (mode, GEN_INT (val),
2080                                                             source)));
2081           return 1;
2082         }
2083       can_negate = 1;
2084
2085       break;
2086
2087     default:
2088       gcc_unreachable ();
2089     }
2090
2091   /* If we can do it in one insn get out quickly.  */
2092   if (const_ok_for_arm (val)
2093       || (can_negate_initial && const_ok_for_arm (-val))
2094       || (can_invert && const_ok_for_arm (~val)))
2095     {
2096       if (generate)
2097         emit_constant_insn (cond,
2098                             gen_rtx_SET (VOIDmode, target,
2099                                          (source
2100                                           ? gen_rtx_fmt_ee (code, mode, source,
2101                                                             GEN_INT (val))
2102                                           : GEN_INT (val))));
2103       return 1;
2104     }
2105
2106   /* Calculate a few attributes that may be useful for specific
2107      optimizations.  */
2108   for (i = 31; i >= 0; i--)
2109     {
2110       if ((remainder & (1 << i)) == 0)
2111         clear_sign_bit_copies++;
2112       else
2113         break;
2114     }
2115
2116   for (i = 31; i >= 0; i--)
2117     {
2118       if ((remainder & (1 << i)) != 0)
2119         set_sign_bit_copies++;
2120       else
2121         break;
2122     }
2123
2124   for (i = 0; i <= 31; i++)
2125     {
2126       if ((remainder & (1 << i)) == 0)
2127         clear_zero_bit_copies++;
2128       else
2129         break;
2130     }
2131
2132   for (i = 0; i <= 31; i++)
2133     {
2134       if ((remainder & (1 << i)) != 0)
2135         set_zero_bit_copies++;
2136       else
2137         break;
2138     }
2139
2140   switch (code)
2141     {
2142     case SET:
2143       /* See if we can use movw.  */
2144       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2145         {
2146           if (generate)
2147             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2148                                                    GEN_INT (val)));
2149           return 1;
2150         }
2151
2152       /* See if we can do this by sign_extending a constant that is known
2153          to be negative.  This is a good, way of doing it, since the shift
2154          may well merge into a subsequent insn.  */
2155       if (set_sign_bit_copies > 1)
2156         {
2157           if (const_ok_for_arm
2158               (temp1 = ARM_SIGN_EXTEND (remainder
2159                                         << (set_sign_bit_copies - 1))))
2160             {
2161               if (generate)
2162                 {
2163                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2164                   emit_constant_insn (cond,
2165                                       gen_rtx_SET (VOIDmode, new_src,
2166                                                    GEN_INT (temp1)));
2167                   emit_constant_insn (cond,
2168                                       gen_ashrsi3 (target, new_src,
2169                                                    GEN_INT (set_sign_bit_copies - 1)));
2170                 }
2171               return 2;
2172             }
2173           /* For an inverted constant, we will need to set the low bits,
2174              these will be shifted out of harm's way.  */
2175           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2176           if (const_ok_for_arm (~temp1))
2177             {
2178               if (generate)
2179                 {
2180                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2181                   emit_constant_insn (cond,
2182                                       gen_rtx_SET (VOIDmode, new_src,
2183                                                    GEN_INT (temp1)));
2184                   emit_constant_insn (cond,
2185                                       gen_ashrsi3 (target, new_src,
2186                                                    GEN_INT (set_sign_bit_copies - 1)));
2187                 }
2188               return 2;
2189             }
2190         }
2191
2192       /* See if we can calculate the value as the difference between two
2193          valid immediates.  */
2194       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2195         {
2196           int topshift = clear_sign_bit_copies & ~1;
2197
2198           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2199                                    & (0xff000000 >> topshift));
2200
2201           /* If temp1 is zero, then that means the 9 most significant
2202              bits of remainder were 1 and we've caused it to overflow.
2203              When topshift is 0 we don't need to do anything since we
2204              can borrow from 'bit 32'.  */
2205           if (temp1 == 0 && topshift != 0)
2206             temp1 = 0x80000000 >> (topshift - 1);
2207
2208           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2209
2210           if (const_ok_for_arm (temp2))
2211             {
2212               if (generate)
2213                 {
2214                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2215                   emit_constant_insn (cond,
2216                                       gen_rtx_SET (VOIDmode, new_src,
2217                                                    GEN_INT (temp1)));
2218                   emit_constant_insn (cond,
2219                                       gen_addsi3 (target, new_src,
2220                                                   GEN_INT (-temp2)));
2221                 }
2222
2223               return 2;
2224             }
2225         }
2226
2227       /* See if we can generate this by setting the bottom (or the top)
2228          16 bits, and then shifting these into the other half of the
2229          word.  We only look for the simplest cases, to do more would cost
2230          too much.  Be careful, however, not to generate this when the
2231          alternative would take fewer insns.  */
2232       if (val & 0xffff0000)
2233         {
2234           temp1 = remainder & 0xffff0000;
2235           temp2 = remainder & 0x0000ffff;
2236
2237           /* Overlaps outside this range are best done using other methods.  */
2238           for (i = 9; i < 24; i++)
2239             {
2240               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2241                   && !const_ok_for_arm (temp2))
2242                 {
2243                   rtx new_src = (subtargets
2244                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2245                                  : target);
2246                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2247                                             source, subtargets, generate);
2248                   source = new_src;
2249                   if (generate)
2250                     emit_constant_insn
2251                       (cond,
2252                        gen_rtx_SET
2253                        (VOIDmode, target,
2254                         gen_rtx_IOR (mode,
2255                                      gen_rtx_ASHIFT (mode, source,
2256                                                      GEN_INT (i)),
2257                                      source)));
2258                   return insns + 1;
2259                 }
2260             }
2261
2262           /* Don't duplicate cases already considered.  */
2263           for (i = 17; i < 24; i++)
2264             {
2265               if (((temp1 | (temp1 >> i)) == remainder)
2266                   && !const_ok_for_arm (temp1))
2267                 {
2268                   rtx new_src = (subtargets
2269                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2270                                  : target);
2271                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2272                                             source, subtargets, generate);
2273                   source = new_src;
2274                   if (generate)
2275                     emit_constant_insn
2276                       (cond,
2277                        gen_rtx_SET (VOIDmode, target,
2278                                     gen_rtx_IOR
2279                                     (mode,
2280                                      gen_rtx_LSHIFTRT (mode, source,
2281                                                        GEN_INT (i)),
2282                                      source)));
2283                   return insns + 1;
2284                 }
2285             }
2286         }
2287       break;
2288
2289     case IOR:
2290     case XOR:
2291       /* If we have IOR or XOR, and the constant can be loaded in a
2292          single instruction, and we can find a temporary to put it in,
2293          then this can be done in two instructions instead of 3-4.  */
2294       if (subtargets
2295           /* TARGET can't be NULL if SUBTARGETS is 0 */
2296           || (reload_completed && !reg_mentioned_p (target, source)))
2297         {
2298           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2299             {
2300               if (generate)
2301                 {
2302                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2303
2304                   emit_constant_insn (cond,
2305                                       gen_rtx_SET (VOIDmode, sub,
2306                                                    GEN_INT (val)));
2307                   emit_constant_insn (cond,
2308                                       gen_rtx_SET (VOIDmode, target,
2309                                                    gen_rtx_fmt_ee (code, mode,
2310                                                                    source, sub)));
2311                 }
2312               return 2;
2313             }
2314         }
2315
2316       if (code == XOR)
2317         break;
2318
2319       if (set_sign_bit_copies > 8
2320           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2321         {
2322           if (generate)
2323             {
2324               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2325               rtx shift = GEN_INT (set_sign_bit_copies);
2326
2327               emit_constant_insn
2328                 (cond,
2329                  gen_rtx_SET (VOIDmode, sub,
2330                               gen_rtx_NOT (mode,
2331                                            gen_rtx_ASHIFT (mode,
2332                                                            source,
2333                                                            shift))));
2334               emit_constant_insn
2335                 (cond,
2336                  gen_rtx_SET (VOIDmode, target,
2337                               gen_rtx_NOT (mode,
2338                                            gen_rtx_LSHIFTRT (mode, sub,
2339                                                              shift))));
2340             }
2341           return 2;
2342         }
2343
2344       if (set_zero_bit_copies > 8
2345           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2346         {
2347           if (generate)
2348             {
2349               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2350               rtx shift = GEN_INT (set_zero_bit_copies);
2351
2352               emit_constant_insn
2353                 (cond,
2354                  gen_rtx_SET (VOIDmode, sub,
2355                               gen_rtx_NOT (mode,
2356                                            gen_rtx_LSHIFTRT (mode,
2357                                                              source,
2358                                                              shift))));
2359               emit_constant_insn
2360                 (cond,
2361                  gen_rtx_SET (VOIDmode, target,
2362                               gen_rtx_NOT (mode,
2363                                            gen_rtx_ASHIFT (mode, sub,
2364                                                            shift))));
2365             }
2366           return 2;
2367         }
2368
2369       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2370         {
2371           if (generate)
2372             {
2373               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2374               emit_constant_insn (cond,
2375                                   gen_rtx_SET (VOIDmode, sub,
2376                                                gen_rtx_NOT (mode, source)));
2377               source = sub;
2378               if (subtargets)
2379                 sub = gen_reg_rtx (mode);
2380               emit_constant_insn (cond,
2381                                   gen_rtx_SET (VOIDmode, sub,
2382                                                gen_rtx_AND (mode, source,
2383                                                             GEN_INT (temp1))));
2384               emit_constant_insn (cond,
2385                                   gen_rtx_SET (VOIDmode, target,
2386                                                gen_rtx_NOT (mode, sub)));
2387             }
2388           return 3;
2389         }
2390       break;
2391
2392     case AND:
2393       /* See if two shifts will do 2 or more insn's worth of work.  */
2394       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2395         {
2396           HOST_WIDE_INT shift_mask = ((0xffffffff
2397                                        << (32 - clear_sign_bit_copies))
2398                                       & 0xffffffff);
2399
2400           if ((remainder | shift_mask) != 0xffffffff)
2401             {
2402               if (generate)
2403                 {
2404                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2405                   insns = arm_gen_constant (AND, mode, cond,
2406                                             remainder | shift_mask,
2407                                             new_src, source, subtargets, 1);
2408                   source = new_src;
2409                 }
2410               else
2411                 {
2412                   rtx targ = subtargets ? NULL_RTX : target;
2413                   insns = arm_gen_constant (AND, mode, cond,
2414                                             remainder | shift_mask,
2415                                             targ, source, subtargets, 0);
2416                 }
2417             }
2418
2419           if (generate)
2420             {
2421               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2422               rtx shift = GEN_INT (clear_sign_bit_copies);
2423
2424               emit_insn (gen_ashlsi3 (new_src, source, shift));
2425               emit_insn (gen_lshrsi3 (target, new_src, shift));
2426             }
2427
2428           return insns + 2;
2429         }
2430
2431       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2432         {
2433           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2434
2435           if ((remainder | shift_mask) != 0xffffffff)
2436             {
2437               if (generate)
2438                 {
2439                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2440
2441                   insns = arm_gen_constant (AND, mode, cond,
2442                                             remainder | shift_mask,
2443                                             new_src, source, subtargets, 1);
2444                   source = new_src;
2445                 }
2446               else
2447                 {
2448                   rtx targ = subtargets ? NULL_RTX : target;
2449
2450                   insns = arm_gen_constant (AND, mode, cond,
2451                                             remainder | shift_mask,
2452                                             targ, source, subtargets, 0);
2453                 }
2454             }
2455
2456           if (generate)
2457             {
2458               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2459               rtx shift = GEN_INT (clear_zero_bit_copies);
2460
2461               emit_insn (gen_lshrsi3 (new_src, source, shift));
2462               emit_insn (gen_ashlsi3 (target, new_src, shift));
2463             }
2464
2465           return insns + 2;
2466         }
2467
2468       break;
2469
2470     default:
2471       break;
2472     }
2473
2474   for (i = 0; i < 32; i++)
2475     if (remainder & (1 << i))
2476       num_bits_set++;
2477
2478   if (code == AND || (can_invert && num_bits_set > 16))
2479     remainder = (~remainder) & 0xffffffff;
2480   else if (code == PLUS && num_bits_set > 16)
2481     remainder = (-remainder) & 0xffffffff;
2482   else
2483     {
2484       can_invert = 0;
2485       can_negate = 0;
2486     }
2487
2488   /* Now try and find a way of doing the job in either two or three
2489      instructions.
2490      We start by looking for the largest block of zeros that are aligned on
2491      a 2-bit boundary, we then fill up the temps, wrapping around to the
2492      top of the word when we drop off the bottom.
2493      In the worst case this code should produce no more than four insns.
2494      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2495      best place to start.  */
2496
2497   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2498      the same.  */
2499   {
2500     int best_start = 0;
2501     if (!TARGET_THUMB2)
2502       {
2503         int best_consecutive_zeros = 0;
2504
2505         for (i = 0; i < 32; i += 2)
2506           {
2507             int consecutive_zeros = 0;
2508
2509             if (!(remainder & (3 << i)))
2510               {
2511                 while ((i < 32) && !(remainder & (3 << i)))
2512                   {
2513                     consecutive_zeros += 2;
2514                     i += 2;
2515                   }
2516                 if (consecutive_zeros > best_consecutive_zeros)
2517                   {
2518                     best_consecutive_zeros = consecutive_zeros;
2519                     best_start = i - consecutive_zeros;
2520                   }
2521                 i -= 2;
2522               }
2523           }
2524
2525         /* So long as it won't require any more insns to do so, it's
2526            desirable to emit a small constant (in bits 0...9) in the last
2527            insn.  This way there is more chance that it can be combined with
2528            a later addressing insn to form a pre-indexed load or store
2529            operation.  Consider:
2530
2531                    *((volatile int *)0xe0000100) = 1;
2532                    *((volatile int *)0xe0000110) = 2;
2533
2534            We want this to wind up as:
2535
2536                     mov rA, #0xe0000000
2537                     mov rB, #1
2538                     str rB, [rA, #0x100]
2539                     mov rB, #2
2540                     str rB, [rA, #0x110]
2541
2542            rather than having to synthesize both large constants from scratch.
2543
2544            Therefore, we calculate how many insns would be required to emit
2545            the constant starting from `best_start', and also starting from
2546            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2547            yield a shorter sequence, we may as well use zero.  */
2548         if (best_start != 0
2549             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2550             && (count_insns_for_constant (remainder, 0) <=
2551                 count_insns_for_constant (remainder, best_start)))
2552           best_start = 0;
2553       }
2554
2555     /* Now start emitting the insns.  */
2556     i = best_start;
2557     do
2558       {
2559         int end;
2560
2561         if (i <= 0)
2562           i += 32;
2563         if (remainder & (3 << (i - 2)))
2564           {
2565             end = i - 8;
2566             if (end < 0)
2567               end += 32;
2568             temp1 = remainder & ((0x0ff << end)
2569                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2570             remainder &= ~temp1;
2571
2572             if (generate)
2573               {
2574                 rtx new_src, temp1_rtx;
2575
2576                 if (code == SET || code == MINUS)
2577                   {
2578                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2579                     if (can_invert && code != MINUS)
2580                       temp1 = ~temp1;
2581                   }
2582                 else
2583                   {
2584                     if (remainder && subtargets)
2585                       new_src = gen_reg_rtx (mode);
2586                     else
2587                       new_src = target;
2588                     if (can_invert)
2589                       temp1 = ~temp1;
2590                     else if (can_negate)
2591                       temp1 = -temp1;
2592                   }
2593
2594                 temp1 = trunc_int_for_mode (temp1, mode);
2595                 temp1_rtx = GEN_INT (temp1);
2596
2597                 if (code == SET)
2598                   ;
2599                 else if (code == MINUS)
2600                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2601                 else
2602                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2603
2604                 emit_constant_insn (cond,
2605                                     gen_rtx_SET (VOIDmode, new_src,
2606                                                  temp1_rtx));
2607                 source = new_src;
2608               }
2609
2610             if (code == SET)
2611               {
2612                 can_invert = 0;
2613                 code = PLUS;
2614               }
2615             else if (code == MINUS)
2616               code = PLUS;
2617
2618             insns++;
2619             if (TARGET_ARM)
2620               i -= 6;
2621             else
2622               i -= 7;
2623           }
2624         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2625            shifts.  */
2626         if (TARGET_ARM)
2627           i -= 2;
2628         else
2629           i--;
2630       }
2631     while (remainder);
2632   }
2633
2634   return insns;
2635 }
2636
2637 /* Canonicalize a comparison so that we are more likely to recognize it.
2638    This can be done for a few constant compares, where we can make the
2639    immediate value easier to load.  */
2640
2641 enum rtx_code
2642 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2643                              rtx * op1)
2644 {
2645   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2646   unsigned HOST_WIDE_INT maxval;
2647   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2648
2649   switch (code)
2650     {
2651     case EQ:
2652     case NE:
2653       return code;
2654
2655     case GT:
2656     case LE:
2657       if (i != maxval
2658           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2659         {
2660           *op1 = GEN_INT (i + 1);
2661           return code == GT ? GE : LT;
2662         }
2663       break;
2664
2665     case GE:
2666     case LT:
2667       if (i != ~maxval
2668           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2669         {
2670           *op1 = GEN_INT (i - 1);
2671           return code == GE ? GT : LE;
2672         }
2673       break;
2674
2675     case GTU:
2676     case LEU:
2677       if (i != ~((unsigned HOST_WIDE_INT) 0)
2678           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2679         {
2680           *op1 = GEN_INT (i + 1);
2681           return code == GTU ? GEU : LTU;
2682         }
2683       break;
2684
2685     case GEU:
2686     case LTU:
2687       if (i != 0
2688           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2689         {
2690           *op1 = GEN_INT (i - 1);
2691           return code == GEU ? GTU : LEU;
2692         }
2693       break;
2694
2695     default:
2696       gcc_unreachable ();
2697     }
2698
2699   return code;
2700 }
2701
2702
2703 /* Define how to find the value returned by a function.  */
2704
2705 rtx
2706 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2707 {
2708   enum machine_mode mode;
2709   int unsignedp ATTRIBUTE_UNUSED;
2710   rtx r ATTRIBUTE_UNUSED;
2711
2712   mode = TYPE_MODE (type);
2713   /* Promote integer types.  */
2714   if (INTEGRAL_TYPE_P (type))
2715     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2716
2717   /* Promotes small structs returned in a register to full-word size
2718      for big-endian AAPCS.  */
2719   if (arm_return_in_msb (type))
2720     {
2721       HOST_WIDE_INT size = int_size_in_bytes (type);
2722       if (size % UNITS_PER_WORD != 0)
2723         {
2724           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2725           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2726         }
2727     }
2728
2729   return LIBCALL_VALUE(mode);
2730 }
2731
2732 /* Determine the amount of memory needed to store the possible return
2733    registers of an untyped call.  */
2734 int
2735 arm_apply_result_size (void)
2736 {
2737   int size = 16;
2738
2739   if (TARGET_ARM)
2740     {
2741       if (TARGET_HARD_FLOAT_ABI)
2742         {
2743           if (TARGET_FPA)
2744             size += 12;
2745           if (TARGET_MAVERICK)
2746             size += 8;
2747         }
2748       if (TARGET_IWMMXT_ABI)
2749         size += 8;
2750     }
2751
2752   return size;
2753 }
2754
2755 /* Decide whether a type should be returned in memory (true)
2756    or in a register (false).  This is called by the macro
2757    RETURN_IN_MEMORY.  */
2758 int
2759 arm_return_in_memory (tree type)
2760 {
2761   HOST_WIDE_INT size;
2762
2763   size = int_size_in_bytes (type);
2764
2765   /* Vector values should be returned using ARM registers, not memory (unless
2766      they're over 16 bytes, which will break since we only have four
2767      call-clobbered registers to play with).  */
2768   if (TREE_CODE (type) == VECTOR_TYPE)
2769     return (size < 0 || size > (4 * UNITS_PER_WORD));
2770
2771   if (!AGGREGATE_TYPE_P (type) &&
2772       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2773     /* All simple types are returned in registers.
2774        For AAPCS, complex types are treated the same as aggregates.  */
2775     return 0;
2776
2777   if (arm_abi != ARM_ABI_APCS)
2778     {
2779       /* ATPCS and later return aggregate types in memory only if they are
2780          larger than a word (or are variable size).  */
2781       return (size < 0 || size > UNITS_PER_WORD);
2782     }
2783
2784   /* For the arm-wince targets we choose to be compatible with Microsoft's
2785      ARM and Thumb compilers, which always return aggregates in memory.  */
2786 #ifndef ARM_WINCE
2787   /* All structures/unions bigger than one word are returned in memory.
2788      Also catch the case where int_size_in_bytes returns -1.  In this case
2789      the aggregate is either huge or of variable size, and in either case
2790      we will want to return it via memory and not in a register.  */
2791   if (size < 0 || size > UNITS_PER_WORD)
2792     return 1;
2793
2794   if (TREE_CODE (type) == RECORD_TYPE)
2795     {
2796       tree field;
2797
2798       /* For a struct the APCS says that we only return in a register
2799          if the type is 'integer like' and every addressable element
2800          has an offset of zero.  For practical purposes this means
2801          that the structure can have at most one non bit-field element
2802          and that this element must be the first one in the structure.  */
2803
2804       /* Find the first field, ignoring non FIELD_DECL things which will
2805          have been created by C++.  */
2806       for (field = TYPE_FIELDS (type);
2807            field && TREE_CODE (field) != FIELD_DECL;
2808            field = TREE_CHAIN (field))
2809         continue;
2810
2811       if (field == NULL)
2812         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2813
2814       /* Check that the first field is valid for returning in a register.  */
2815
2816       /* ... Floats are not allowed */
2817       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2818         return 1;
2819
2820       /* ... Aggregates that are not themselves valid for returning in
2821          a register are not allowed.  */
2822       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2823         return 1;
2824
2825       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2826          since they are not addressable.  */
2827       for (field = TREE_CHAIN (field);
2828            field;
2829            field = TREE_CHAIN (field))
2830         {
2831           if (TREE_CODE (field) != FIELD_DECL)
2832             continue;
2833
2834           if (!DECL_BIT_FIELD_TYPE (field))
2835             return 1;
2836         }
2837
2838       return 0;
2839     }
2840
2841   if (TREE_CODE (type) == UNION_TYPE)
2842     {
2843       tree field;
2844
2845       /* Unions can be returned in registers if every element is
2846          integral, or can be returned in an integer register.  */
2847       for (field = TYPE_FIELDS (type);
2848            field;
2849            field = TREE_CHAIN (field))
2850         {
2851           if (TREE_CODE (field) != FIELD_DECL)
2852             continue;
2853
2854           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2855             return 1;
2856
2857           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2858             return 1;
2859         }
2860
2861       return 0;
2862     }
2863 #endif /* not ARM_WINCE */
2864
2865   /* Return all other types in memory.  */
2866   return 1;
2867 }
2868
2869 /* Indicate whether or not words of a double are in big-endian order.  */
2870
2871 int
2872 arm_float_words_big_endian (void)
2873 {
2874   if (TARGET_MAVERICK)
2875     return 0;
2876
2877   /* For FPA, float words are always big-endian.  For VFP, floats words
2878      follow the memory system mode.  */
2879
2880   if (TARGET_FPA)
2881     {
2882       return 1;
2883     }
2884
2885   if (TARGET_VFP)
2886     return (TARGET_BIG_END ? 1 : 0);
2887
2888   return 1;
2889 }
2890
2891 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2892    for a call to a function whose data type is FNTYPE.
2893    For a library call, FNTYPE is NULL.  */
2894 void
2895 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2896                           rtx libname  ATTRIBUTE_UNUSED,
2897                           tree fndecl ATTRIBUTE_UNUSED)
2898 {
2899   /* On the ARM, the offset starts at 0.  */
2900   pcum->nregs = 0;
2901   pcum->iwmmxt_nregs = 0;
2902   pcum->can_split = true;
2903
2904   /* Varargs vectors are treated the same as long long.
2905      named_count avoids having to change the way arm handles 'named' */
2906   pcum->named_count = 0;
2907   pcum->nargs = 0;
2908
2909   if (TARGET_REALLY_IWMMXT && fntype)
2910     {
2911       tree fn_arg;
2912
2913       for (fn_arg = TYPE_ARG_TYPES (fntype);
2914            fn_arg;
2915            fn_arg = TREE_CHAIN (fn_arg))
2916         pcum->named_count += 1;
2917
2918       if (! pcum->named_count)
2919         pcum->named_count = INT_MAX;
2920     }
2921 }
2922
2923
2924 /* Return true if mode/type need doubleword alignment.  */
2925 bool
2926 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2927 {
2928   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2929           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2930 }
2931
2932
2933 /* Determine where to put an argument to a function.
2934    Value is zero to push the argument on the stack,
2935    or a hard register in which to store the argument.
2936
2937    MODE is the argument's machine mode.
2938    TYPE is the data type of the argument (as a tree).
2939     This is null for libcalls where that information may
2940     not be available.
2941    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2942     the preceding args and about the function being called.
2943    NAMED is nonzero if this argument is a named parameter
2944     (otherwise it is an extra parameter matching an ellipsis).  */
2945
2946 rtx
2947 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2948                   tree type, int named)
2949 {
2950   int nregs;
2951
2952   /* Varargs vectors are treated the same as long long.
2953      named_count avoids having to change the way arm handles 'named' */
2954   if (TARGET_IWMMXT_ABI
2955       && arm_vector_mode_supported_p (mode)
2956       && pcum->named_count > pcum->nargs + 1)
2957     {
2958       if (pcum->iwmmxt_nregs <= 9)
2959         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2960       else
2961         {
2962           pcum->can_split = false;
2963           return NULL_RTX;
2964         }
2965     }
2966
2967   /* Put doubleword aligned quantities in even register pairs.  */
2968   if (pcum->nregs & 1
2969       && ARM_DOUBLEWORD_ALIGN
2970       && arm_needs_doubleword_align (mode, type))
2971     pcum->nregs++;
2972
2973   if (mode == VOIDmode)
2974     /* Pick an arbitrary value for operand 2 of the call insn.  */
2975     return const0_rtx;
2976
2977   /* Only allow splitting an arg between regs and memory if all preceding
2978      args were allocated to regs.  For args passed by reference we only count
2979      the reference pointer.  */
2980   if (pcum->can_split)
2981     nregs = 1;
2982   else
2983     nregs = ARM_NUM_REGS2 (mode, type);
2984
2985   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2986     return NULL_RTX;
2987
2988   return gen_rtx_REG (mode, pcum->nregs);
2989 }
2990
2991 static int
2992 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2993                        tree type, bool named ATTRIBUTE_UNUSED)
2994 {
2995   int nregs = pcum->nregs;
2996
2997   if (TARGET_IWMMXT_ABI && arm_vector_mode_supported_p (mode))
2998     return 0;
2999
3000   if (NUM_ARG_REGS > nregs
3001       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
3002       && pcum->can_split)
3003     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
3004
3005   return 0;
3006 }
3007
3008 /* Variable sized types are passed by reference.  This is a GCC
3009    extension to the ARM ABI.  */
3010
3011 static bool
3012 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3013                        enum machine_mode mode ATTRIBUTE_UNUSED,
3014                        tree type, bool named ATTRIBUTE_UNUSED)
3015 {
3016   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3017 }
3018 \f
3019 /* Encode the current state of the #pragma [no_]long_calls.  */
3020 typedef enum
3021 {
3022   OFF,          /* No #pragma [no_]long_calls is in effect.  */
3023   LONG,         /* #pragma long_calls is in effect.  */
3024   SHORT         /* #pragma no_long_calls is in effect.  */
3025 } arm_pragma_enum;
3026
3027 static arm_pragma_enum arm_pragma_long_calls = OFF;
3028
3029 void
3030 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3031 {
3032   arm_pragma_long_calls = LONG;
3033 }
3034
3035 void
3036 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3037 {
3038   arm_pragma_long_calls = SHORT;
3039 }
3040
3041 void
3042 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3043 {
3044   arm_pragma_long_calls = OFF;
3045 }
3046 \f
3047 /* Table of machine attributes.  */
3048 const struct attribute_spec arm_attribute_table[] =
3049 {
3050   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3051   /* Function calls made to this symbol must be done indirectly, because
3052      it may lie outside of the 26 bit addressing range of a normal function
3053      call.  */
3054   { "long_call",    0, 0, false, true,  true,  NULL },
3055   /* Whereas these functions are always known to reside within the 26 bit
3056      addressing range.  */
3057   { "short_call",   0, 0, false, true,  true,  NULL },
3058   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
3059   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
3060   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
3061   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
3062 #ifdef ARM_PE
3063   /* ARM/PE has three new attributes:
3064      interfacearm - ?
3065      dllexport - for exporting a function/variable that will live in a dll
3066      dllimport - for importing a function/variable from a dll
3067
3068      Microsoft allows multiple declspecs in one __declspec, separating
3069      them with spaces.  We do NOT support this.  Instead, use __declspec
3070      multiple times.
3071   */
3072   { "dllimport",    0, 0, true,  false, false, NULL },
3073   { "dllexport",    0, 0, true,  false, false, NULL },
3074   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
3075 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3076   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
3077   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
3078   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
3079 #endif
3080   { NULL,           0, 0, false, false, false, NULL }
3081 };
3082
3083 /* Handle an attribute requiring a FUNCTION_DECL;
3084    arguments as in struct attribute_spec.handler.  */
3085 static tree
3086 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3087                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3088 {
3089   if (TREE_CODE (*node) != FUNCTION_DECL)
3090     {
3091       warning (OPT_Wattributes, "%qs attribute only applies to functions",
3092                IDENTIFIER_POINTER (name));
3093       *no_add_attrs = true;
3094     }
3095
3096   return NULL_TREE;
3097 }
3098
3099 /* Handle an "interrupt" or "isr" attribute;
3100    arguments as in struct attribute_spec.handler.  */
3101 static tree
3102 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3103                           bool *no_add_attrs)
3104 {
3105   if (DECL_P (*node))
3106     {
3107       if (TREE_CODE (*node) != FUNCTION_DECL)
3108         {
3109           warning (OPT_Wattributes, "%qs attribute only applies to functions",
3110                    IDENTIFIER_POINTER (name));
3111           *no_add_attrs = true;
3112         }
3113       /* FIXME: the argument if any is checked for type attributes;
3114          should it be checked for decl ones?  */
3115     }
3116   else
3117     {
3118       if (TREE_CODE (*node) == FUNCTION_TYPE
3119           || TREE_CODE (*node) == METHOD_TYPE)
3120         {
3121           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3122             {
3123               warning (OPT_Wattributes, "%qs attribute ignored",
3124                        IDENTIFIER_POINTER (name));
3125               *no_add_attrs = true;
3126             }
3127         }
3128       else if (TREE_CODE (*node) == POINTER_TYPE
3129                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3130                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3131                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3132         {
3133           *node = build_variant_type_copy (*node);
3134           TREE_TYPE (*node) = build_type_attribute_variant
3135             (TREE_TYPE (*node),
3136              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3137           *no_add_attrs = true;
3138         }
3139       else
3140         {
3141           /* Possibly pass this attribute on from the type to a decl.  */
3142           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3143                        | (int) ATTR_FLAG_FUNCTION_NEXT
3144                        | (int) ATTR_FLAG_ARRAY_NEXT))
3145             {
3146               *no_add_attrs = true;
3147               return tree_cons (name, args, NULL_TREE);
3148             }
3149           else
3150             {
3151               warning (OPT_Wattributes, "%qs attribute ignored",
3152                        IDENTIFIER_POINTER (name));
3153             }
3154         }
3155     }
3156
3157   return NULL_TREE;
3158 }
3159
3160 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3161 /* Handle the "notshared" attribute.  This attribute is another way of
3162    requesting hidden visibility.  ARM's compiler supports
3163    "__declspec(notshared)"; we support the same thing via an
3164    attribute.  */
3165
3166 static tree
3167 arm_handle_notshared_attribute (tree *node,
3168                                 tree name ATTRIBUTE_UNUSED,
3169                                 tree args ATTRIBUTE_UNUSED,
3170                                 int flags ATTRIBUTE_UNUSED,
3171                                 bool *no_add_attrs)
3172 {
3173   tree decl = TYPE_NAME (*node);
3174
3175   if (decl)
3176     {
3177       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3178       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3179       *no_add_attrs = false;
3180     }
3181   return NULL_TREE;
3182 }
3183 #endif
3184
3185 /* Return 0 if the attributes for two types are incompatible, 1 if they
3186    are compatible, and 2 if they are nearly compatible (which causes a
3187    warning to be generated).  */
3188 static int
3189 arm_comp_type_attributes (tree type1, tree type2)
3190 {
3191   int l1, l2, s1, s2;
3192
3193   /* Check for mismatch of non-default calling convention.  */
3194   if (TREE_CODE (type1) != FUNCTION_TYPE)
3195     return 1;
3196
3197   /* Check for mismatched call attributes.  */
3198   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3199   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3200   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3201   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3202
3203   /* Only bother to check if an attribute is defined.  */
3204   if (l1 | l2 | s1 | s2)
3205     {
3206       /* If one type has an attribute, the other must have the same attribute.  */
3207       if ((l1 != l2) || (s1 != s2))
3208         return 0;
3209
3210       /* Disallow mixed attributes.  */
3211       if ((l1 & s2) || (l2 & s1))
3212         return 0;
3213     }
3214
3215   /* Check for mismatched ISR attribute.  */
3216   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3217   if (! l1)
3218     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3219   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3220   if (! l2)
3221     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3222   if (l1 != l2)
3223     return 0;
3224
3225   return 1;
3226 }
3227
3228 /*  Assigns default attributes to newly defined type.  This is used to
3229     set short_call/long_call attributes for function types of
3230     functions defined inside corresponding #pragma scopes.  */
3231 static void
3232 arm_set_default_type_attributes (tree type)
3233 {
3234   /* Add __attribute__ ((long_call)) to all functions, when
3235      inside #pragma long_calls or __attribute__ ((short_call)),
3236      when inside #pragma no_long_calls.  */
3237   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3238     {
3239       tree type_attr_list, attr_name;
3240       type_attr_list = TYPE_ATTRIBUTES (type);
3241
3242       if (arm_pragma_long_calls == LONG)
3243         attr_name = get_identifier ("long_call");
3244       else if (arm_pragma_long_calls == SHORT)
3245         attr_name = get_identifier ("short_call");
3246       else
3247         return;
3248
3249       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3250       TYPE_ATTRIBUTES (type) = type_attr_list;
3251     }
3252 }
3253 \f
3254 /* Return true if DECL is known to be linked into section SECTION.  */
3255
3256 static bool
3257 arm_function_in_section_p (tree decl, section *section)
3258 {
3259   /* We can only be certain about functions defined in the same
3260      compilation unit.  */
3261   if (!TREE_STATIC (decl))
3262     return false;
3263
3264   /* Make sure that SYMBOL always binds to the definition in this
3265      compilation unit.  */
3266   if (!targetm.binds_local_p (decl))
3267     return false;
3268
3269   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3270   if (!DECL_SECTION_NAME (decl))
3271     {
3272       /* Only cater for unit-at-a-time mode, where we know that the user
3273          cannot later specify a section for DECL.  */
3274       if (!flag_unit_at_a_time)
3275         return false;
3276
3277       /* Make sure that we will not create a unique section for DECL.  */
3278       if (flag_function_sections || DECL_ONE_ONLY (decl))
3279         return false;
3280     }
3281
3282   return function_section (decl) == section;
3283 }
3284
3285 /* Return nonzero if a 32-bit "long_call" should be generated for
3286    a call from the current function to DECL.  We generate a long_call
3287    if the function:
3288
3289         a.  has an __attribute__((long call))
3290      or b.  is within the scope of a #pragma long_calls
3291      or c.  the -mlong-calls command line switch has been specified
3292
3293    However we do not generate a long call if the function:
3294
3295         d.  has an __attribute__ ((short_call))
3296      or e.  is inside the scope of a #pragma no_long_calls
3297      or f.  is defined in the same section as the current function.  */
3298
3299 bool
3300 arm_is_long_call_p (tree decl)
3301 {
3302   tree attrs;
3303
3304   if (!decl)
3305     return TARGET_LONG_CALLS;
3306
3307   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3308   if (lookup_attribute ("short_call", attrs))
3309     return false;
3310
3311   /* For "f", be conservative, and only cater for cases in which the
3312      whole of the current function is placed in the same section.  */
3313   if (!flag_reorder_blocks_and_partition
3314       && arm_function_in_section_p (decl, current_function_section ()))
3315     return false;
3316
3317   if (lookup_attribute ("long_call", attrs))
3318     return true;
3319
3320   return TARGET_LONG_CALLS;
3321 }
3322
3323 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3324 static bool
3325 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3326 {
3327   unsigned long func_type;
3328
3329   if (cfun->machine->sibcall_blocked)
3330     return false;
3331
3332   /* Never tailcall something for which we have no decl, or if we
3333      are in Thumb mode.  */
3334   if (decl == NULL || TARGET_THUMB)
3335     return false;
3336
3337   /* The PIC register is live on entry to VxWorks PLT entries, so we
3338      must make the call before restoring the PIC register.  */
3339   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3340     return false;
3341
3342   /* Cannot tail-call to long calls, since these are out of range of
3343      a branch instruction.  */
3344   if (arm_is_long_call_p (decl))
3345     return false;
3346
3347   /* If we are interworking and the function is not declared static
3348      then we can't tail-call it unless we know that it exists in this
3349      compilation unit (since it might be a Thumb routine).  */
3350   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3351     return false;
3352
3353   func_type = arm_current_func_type ();
3354   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3355   if (IS_INTERRUPT (func_type))
3356     return false;
3357
3358   /* Never tailcall if function may be called with a misaligned SP.  */
3359   if (IS_STACKALIGN (func_type))
3360     return false;
3361
3362   /* Everything else is ok.  */
3363   return true;
3364 }
3365
3366 \f
3367 /* Addressing mode support functions.  */
3368
3369 /* Return nonzero if X is a legitimate immediate operand when compiling
3370    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3371 int
3372 legitimate_pic_operand_p (rtx x)
3373 {
3374   if (GET_CODE (x) == SYMBOL_REF
3375       || (GET_CODE (x) == CONST
3376           && GET_CODE (XEXP (x, 0)) == PLUS
3377           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3378     return 0;
3379
3380   return 1;
3381 }
3382
3383 /* Record that the current function needs a PIC register.  Initialize
3384    cfun->machine->pic_reg if we have not already done so.  */
3385
3386 static void
3387 require_pic_register (void)
3388 {
3389   /* A lot of the logic here is made obscure by the fact that this
3390      routine gets called as part of the rtx cost estimation process.
3391      We don't want those calls to affect any assumptions about the real
3392      function; and further, we can't call entry_of_function() until we
3393      start the real expansion process.  */
3394   if (!current_function_uses_pic_offset_table)
3395     {
3396       gcc_assert (can_create_pseudo_p ());
3397       if (arm_pic_register != INVALID_REGNUM)
3398         {
3399           cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3400
3401           /* Play games to avoid marking the function as needing pic
3402              if we are being called as part of the cost-estimation
3403              process.  */
3404           if (current_ir_type () != IR_GIMPLE)
3405             current_function_uses_pic_offset_table = 1;
3406         }
3407       else
3408         {
3409           rtx seq;
3410
3411           cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3412
3413           /* Play games to avoid marking the function as needing pic
3414              if we are being called as part of the cost-estimation
3415              process.  */
3416           if (current_ir_type () != IR_GIMPLE)
3417             {
3418               current_function_uses_pic_offset_table = 1;
3419               start_sequence ();
3420
3421               arm_load_pic_register (0UL);
3422
3423               seq = get_insns ();
3424               end_sequence ();
3425               emit_insn_after (seq, entry_of_function ());
3426             }
3427         }
3428     }
3429 }
3430
3431 rtx
3432 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3433 {
3434   if (GET_CODE (orig) == SYMBOL_REF
3435       || GET_CODE (orig) == LABEL_REF)
3436     {
3437 #ifndef AOF_ASSEMBLER
3438       rtx pic_ref, address;
3439 #endif
3440       rtx insn;
3441       int subregs = 0;
3442
3443       /* If this function doesn't have a pic register, create one now.  */
3444       require_pic_register ();
3445
3446       if (reg == 0)
3447         {
3448           gcc_assert (can_create_pseudo_p ());
3449           reg = gen_reg_rtx (Pmode);
3450
3451           subregs = 1;
3452         }
3453
3454 #ifdef AOF_ASSEMBLER
3455       /* The AOF assembler can generate relocations for these directly, and
3456          understands that the PIC register has to be added into the offset.  */
3457       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3458 #else
3459       if (subregs)
3460         address = gen_reg_rtx (Pmode);
3461       else
3462         address = reg;
3463
3464       if (TARGET_ARM)
3465         emit_insn (gen_pic_load_addr_arm (address, orig));
3466       else if (TARGET_THUMB2)
3467         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3468       else /* TARGET_THUMB1 */
3469         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3470
3471       /* VxWorks does not impose a fixed gap between segments; the run-time
3472          gap can be different from the object-file gap.  We therefore can't
3473          use GOTOFF unless we are absolutely sure that the symbol is in the
3474          same segment as the GOT.  Unfortunately, the flexibility of linker
3475          scripts means that we can't be sure of that in general, so assume
3476          that GOTOFF is never valid on VxWorks.  */
3477       if ((GET_CODE (orig) == LABEL_REF
3478            || (GET_CODE (orig) == SYMBOL_REF &&
3479                SYMBOL_REF_LOCAL_P (orig)))
3480           && NEED_GOT_RELOC
3481           && !TARGET_VXWORKS_RTP)
3482         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3483       else
3484         {
3485           pic_ref = gen_const_mem (Pmode,
3486                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3487                                                  address));
3488         }
3489
3490       insn = emit_move_insn (reg, pic_ref);
3491 #endif
3492       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3493          by loop.  */
3494       set_unique_reg_note (insn, REG_EQUAL, orig);
3495
3496       return reg;
3497     }
3498   else if (GET_CODE (orig) == CONST)
3499     {
3500       rtx base, offset;
3501
3502       if (GET_CODE (XEXP (orig, 0)) == PLUS
3503           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3504         return orig;
3505
3506       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3507           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3508         return orig;
3509
3510       if (reg == 0)
3511         {
3512           gcc_assert (can_create_pseudo_p ());
3513           reg = gen_reg_rtx (Pmode);
3514         }
3515
3516       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3517
3518       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3519       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3520                                        base == reg ? 0 : reg);
3521
3522       if (GET_CODE (offset) == CONST_INT)
3523         {
3524           /* The base register doesn't really matter, we only want to
3525              test the index for the appropriate mode.  */
3526           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3527             {
3528               gcc_assert (can_create_pseudo_p ());
3529               offset = force_reg (Pmode, offset);
3530             }
3531
3532           if (GET_CODE (offset) == CONST_INT)
3533             return plus_constant (base, INTVAL (offset));
3534         }
3535
3536       if (GET_MODE_SIZE (mode) > 4
3537           && (GET_MODE_CLASS (mode) == MODE_INT
3538               || TARGET_SOFT_FLOAT))
3539         {
3540           emit_insn (gen_addsi3 (reg, base, offset));
3541           return reg;
3542         }
3543
3544       return gen_rtx_PLUS (Pmode, base, offset);
3545     }
3546
3547   return orig;
3548 }
3549
3550
3551 /* Find a spare register to use during the prolog of a function.  */
3552
3553 static int
3554 thumb_find_work_register (unsigned long pushed_regs_mask)
3555 {
3556   int reg;
3557
3558   /* Check the argument registers first as these are call-used.  The
3559      register allocation order means that sometimes r3 might be used
3560      but earlier argument registers might not, so check them all.  */
3561   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3562     if (!df_regs_ever_live_p (reg))
3563       return reg;
3564
3565   /* Before going on to check the call-saved registers we can try a couple
3566      more ways of deducing that r3 is available.  The first is when we are
3567      pushing anonymous arguments onto the stack and we have less than 4
3568      registers worth of fixed arguments(*).  In this case r3 will be part of
3569      the variable argument list and so we can be sure that it will be
3570      pushed right at the start of the function.  Hence it will be available
3571      for the rest of the prologue.
3572      (*): ie current_function_pretend_args_size is greater than 0.  */
3573   if (cfun->machine->uses_anonymous_args
3574       && current_function_pretend_args_size > 0)
3575     return LAST_ARG_REGNUM;
3576
3577   /* The other case is when we have fixed arguments but less than 4 registers
3578      worth.  In this case r3 might be used in the body of the function, but
3579      it is not being used to convey an argument into the function.  In theory
3580      we could just check current_function_args_size to see how many bytes are
3581      being passed in argument registers, but it seems that it is unreliable.
3582      Sometimes it will have the value 0 when in fact arguments are being
3583      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3584      check the args_info.nregs field as well.  The problem with this field is
3585      that it makes no allowances for arguments that are passed to the
3586      function but which are not used.  Hence we could miss an opportunity
3587      when a function has an unused argument in r3.  But it is better to be
3588      safe than to be sorry.  */
3589   if (! cfun->machine->uses_anonymous_args
3590       && current_function_args_size >= 0
3591       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3592       && cfun->args_info.nregs < 4)
3593     return LAST_ARG_REGNUM;
3594
3595   /* Otherwise look for a call-saved register that is going to be pushed.  */
3596   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3597     if (pushed_regs_mask & (1 << reg))
3598       return reg;
3599
3600   if (TARGET_THUMB2)
3601     {
3602       /* Thumb-2 can use high regs.  */
3603       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3604         if (pushed_regs_mask & (1 << reg))
3605           return reg;
3606     }
3607   /* Something went wrong - thumb_compute_save_reg_mask()
3608      should have arranged for a suitable register to be pushed.  */
3609   gcc_unreachable ();
3610 }
3611
3612 static GTY(()) int pic_labelno;
3613
3614 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3615    low register.  */
3616
3617 void
3618 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3619 {
3620 #ifndef AOF_ASSEMBLER
3621   rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx, pic_reg;
3622   rtx global_offset_table;
3623
3624   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3625     return;
3626
3627   gcc_assert (flag_pic);
3628
3629   pic_reg = cfun->machine->pic_reg;
3630   if (TARGET_VXWORKS_RTP)
3631     {
3632       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3633       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3634       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3635
3636       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
3637
3638       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3639       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
3640     }
3641   else
3642     {
3643       /* We use an UNSPEC rather than a LABEL_REF because this label
3644          never appears in the code stream.  */
3645
3646       labelno = GEN_INT (pic_labelno++);
3647       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3648       l1 = gen_rtx_CONST (VOIDmode, l1);
3649
3650       global_offset_table
3651         = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3652       /* On the ARM the PC register contains 'dot + 8' at the time of the
3653          addition, on the Thumb it is 'dot + 4'.  */
3654       pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3655       if (GOT_PCREL)
3656         {
3657           pic_tmp2 = gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx);
3658           pic_tmp2 = gen_rtx_CONST (VOIDmode, pic_tmp2);
3659         }
3660       else
3661         pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3662
3663       pic_rtx = gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp);
3664       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3665
3666       if (TARGET_ARM)
3667         {
3668           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3669           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
3670         }
3671       else if (TARGET_THUMB2)
3672         {
3673           /* Thumb-2 only allows very limited access to the PC.  Calculate the
3674              address in a temporary register.  */
3675           if (arm_pic_register != INVALID_REGNUM)
3676             {
3677               pic_tmp = gen_rtx_REG (SImode,
3678                                      thumb_find_work_register (saved_regs));
3679             }
3680           else
3681             {
3682               gcc_assert (can_create_pseudo_p ());
3683               pic_tmp = gen_reg_rtx (Pmode);
3684             }
3685
3686           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
3687           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3688           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
3689         }
3690       else /* TARGET_THUMB1 */
3691         {
3692           if (arm_pic_register != INVALID_REGNUM
3693               && REGNO (pic_reg) > LAST_LO_REGNUM)
3694             {
3695               /* We will have pushed the pic register, so we should always be
3696                  able to find a work register.  */
3697               pic_tmp = gen_rtx_REG (SImode,
3698                                      thumb_find_work_register (saved_regs));
3699               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3700               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3701             }
3702           else
3703             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
3704           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
3705         }
3706     }
3707
3708   /* Need to emit this whether or not we obey regdecls,
3709      since setjmp/longjmp can cause life info to screw up.  */
3710   emit_insn (gen_rtx_USE (VOIDmode, pic_reg));
3711 #endif /* AOF_ASSEMBLER */
3712 }
3713
3714
3715 /* Return nonzero if X is valid as an ARM state addressing register.  */
3716 static int
3717 arm_address_register_rtx_p (rtx x, int strict_p)
3718 {
3719   int regno;
3720
3721   if (GET_CODE (x) != REG)
3722     return 0;
3723
3724   regno = REGNO (x);
3725
3726   if (strict_p)
3727     return ARM_REGNO_OK_FOR_BASE_P (regno);
3728
3729   return (regno <= LAST_ARM_REGNUM
3730           || regno >= FIRST_PSEUDO_REGISTER
3731           || regno == FRAME_POINTER_REGNUM
3732           || regno == ARG_POINTER_REGNUM);
3733 }
3734
3735 /* Return TRUE if this rtx is the difference of a symbol and a label,
3736    and will reduce to a PC-relative relocation in the object file.
3737    Expressions like this can be left alone when generating PIC, rather
3738    than forced through the GOT.  */
3739 static int
3740 pcrel_constant_p (rtx x)
3741 {
3742   if (GET_CODE (x) == MINUS)
3743     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3744
3745   return FALSE;
3746 }
3747
3748 /* Return nonzero if X is a valid ARM state address operand.  */
3749 int
3750 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3751                           int strict_p)
3752 {
3753   bool use_ldrd;
3754   enum rtx_code code = GET_CODE (x);
3755
3756   if (arm_address_register_rtx_p (x, strict_p))
3757     return 1;
3758
3759   use_ldrd = (TARGET_LDRD
3760               && (mode == DImode
3761                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3762
3763   if (code == POST_INC || code == PRE_DEC
3764       || ((code == PRE_INC || code == POST_DEC)
3765           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3766     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3767
3768   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3769            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3770            && GET_CODE (XEXP (x, 1)) == PLUS
3771            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3772     {
3773       rtx addend = XEXP (XEXP (x, 1), 1);
3774
3775       /* Don't allow ldrd post increment by register because it's hard
3776          to fixup invalid register choices.  */
3777       if (use_ldrd
3778           && GET_CODE (x) == POST_MODIFY
3779           && GET_CODE (addend) == REG)
3780         return 0;
3781
3782       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3783               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3784     }
3785
3786   /* After reload constants split into minipools will have addresses
3787      from a LABEL_REF.  */
3788   else if (reload_completed
3789            && (code == LABEL_REF
3790                || (code == CONST
3791                    && GET_CODE (XEXP (x, 0)) == PLUS
3792                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3793                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3794     return 1;
3795
3796   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
3797     return 0;
3798
3799   else if (code == PLUS)
3800     {
3801       rtx xop0 = XEXP (x, 0);
3802       rtx xop1 = XEXP (x, 1);
3803
3804       return ((arm_address_register_rtx_p (xop0, strict_p)
3805                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3806               || (arm_address_register_rtx_p (xop1, strict_p)
3807                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3808     }
3809
3810 #if 0
3811   /* Reload currently can't handle MINUS, so disable this for now */
3812   else if (GET_CODE (x) == MINUS)
3813     {
3814       rtx xop0 = XEXP (x, 0);
3815       rtx xop1 = XEXP (x, 1);
3816
3817       return (arm_address_register_rtx_p (xop0, strict_p)
3818               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3819     }
3820 #endif
3821
3822   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3823            && code == SYMBOL_REF
3824            && CONSTANT_POOL_ADDRESS_P (x)
3825            && ! (flag_pic
3826                  && symbol_mentioned_p (get_pool_constant (x))
3827                  && ! pcrel_constant_p (get_pool_constant (x))))
3828     return 1;
3829
3830   return 0;
3831 }
3832
3833 /* Return nonzero if X is a valid Thumb-2 address operand.  */
3834 int
3835 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3836 {
3837   bool use_ldrd;
3838   enum rtx_code code = GET_CODE (x);
3839   
3840   if (arm_address_register_rtx_p (x, strict_p))
3841     return 1;
3842
3843   use_ldrd = (TARGET_LDRD
3844               && (mode == DImode
3845                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3846
3847   if (code == POST_INC || code == PRE_DEC
3848       || ((code == PRE_INC || code == POST_DEC)
3849           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3850     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3851
3852   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3853            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3854            && GET_CODE (XEXP (x, 1)) == PLUS
3855            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3856     {
3857       /* Thumb-2 only has autoincrement by constant.  */
3858       rtx addend = XEXP (XEXP (x, 1), 1);
3859       HOST_WIDE_INT offset;
3860
3861       if (GET_CODE (addend) != CONST_INT)
3862         return 0;
3863
3864       offset = INTVAL(addend);
3865       if (GET_MODE_SIZE (mode) <= 4)
3866         return (offset > -256 && offset < 256);
3867       
3868       return (use_ldrd && offset > -1024 && offset < 1024
3869               && (offset & 3) == 0);
3870     }
3871
3872   /* After reload constants split into minipools will have addresses
3873      from a LABEL_REF.  */
3874   else if (reload_completed
3875            && (code == LABEL_REF
3876                || (code == CONST
3877                    && GET_CODE (XEXP (x, 0)) == PLUS
3878                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3879                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3880     return 1;
3881
3882   else if (mode == TImode || (TARGET_NEON && VALID_NEON_STRUCT_MODE (mode)))
3883     return 0;
3884
3885   else if (code == PLUS)
3886     {
3887       rtx xop0 = XEXP (x, 0);
3888       rtx xop1 = XEXP (x, 1);
3889
3890       return ((arm_address_register_rtx_p (xop0, strict_p)
3891                && thumb2_legitimate_index_p (mode, xop1, strict_p))
3892               || (arm_address_register_rtx_p (xop1, strict_p)
3893                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3894     }
3895
3896   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3897            && code == SYMBOL_REF
3898            && CONSTANT_POOL_ADDRESS_P (x)
3899            && ! (flag_pic
3900                  && symbol_mentioned_p (get_pool_constant (x))
3901                  && ! pcrel_constant_p (get_pool_constant (x))))
3902     return 1;
3903
3904   return 0;
3905 }
3906
3907 /* Return nonzero if INDEX is valid for an address index operand in
3908    ARM state.  */
3909 static int
3910 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3911                         int strict_p)
3912 {
3913   HOST_WIDE_INT range;
3914   enum rtx_code code = GET_CODE (index);
3915
3916   /* Standard coprocessor addressing modes.  */
3917   if (TARGET_HARD_FLOAT
3918       && (TARGET_FPA || TARGET_MAVERICK)
3919       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3920           || (TARGET_MAVERICK && mode == DImode)))
3921     return (code == CONST_INT && INTVAL (index) < 1024
3922             && INTVAL (index) > -1024
3923             && (INTVAL (index) & 3) == 0);
3924
3925   if (TARGET_NEON
3926       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
3927     return (code == CONST_INT
3928             && INTVAL (index) < 1016
3929             && INTVAL (index) > -1024
3930             && (INTVAL (index) & 3) == 0);
3931
3932   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3933     return (code == CONST_INT
3934             && INTVAL (index) < 1024
3935             && INTVAL (index) > -1024
3936             && (INTVAL (index) & 3) == 0);
3937
3938   if (arm_address_register_rtx_p (index, strict_p)
3939       && (GET_MODE_SIZE (mode) <= 4))
3940     return 1;
3941
3942   if (mode == DImode || mode == DFmode)
3943     {
3944       if (code == CONST_INT)
3945         {
3946           HOST_WIDE_INT val = INTVAL (index);
3947
3948           if (TARGET_LDRD)
3949             return val > -256 && val < 256;
3950           else
3951             return val > -4096 && val < 4092;
3952         }
3953
3954       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3955     }
3956
3957   if (GET_MODE_SIZE (mode) <= 4
3958       && ! (arm_arch4
3959             && (mode == HImode
3960                 || (mode == QImode && outer == SIGN_EXTEND))))
3961     {
3962       if (code == MULT)
3963         {
3964           rtx xiop0 = XEXP (index, 0);
3965           rtx xiop1 = XEXP (index, 1);
3966
3967           return ((arm_address_register_rtx_p (xiop0, strict_p)
3968                    && power_of_two_operand (xiop1, SImode))
3969                   || (arm_address_register_rtx_p (xiop1, strict_p)
3970                       && power_of_two_operand (xiop0, SImode)));
3971         }
3972       else if (code == LSHIFTRT || code == ASHIFTRT
3973                || code == ASHIFT || code == ROTATERT)
3974         {
3975           rtx op = XEXP (index, 1);
3976
3977           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3978                   && GET_CODE (op) == CONST_INT
3979                   && INTVAL (op) > 0
3980                   && INTVAL (op) <= 31);
3981         }
3982     }
3983
3984   /* For ARM v4 we may be doing a sign-extend operation during the
3985      load.  */
3986   if (arm_arch4)
3987     {
3988       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3989         range = 256;
3990       else
3991         range = 4096;
3992     }
3993   else
3994     range = (mode == HImode) ? 4095 : 4096;
3995
3996   return (code == CONST_INT
3997           && INTVAL (index) < range
3998           && INTVAL (index) > -range);
3999 }
4000
4001 /* Return true if OP is a valid index scaling factor for Thumb-2 address
4002    index operand.  i.e. 1, 2, 4 or 8.  */
4003 static bool
4004 thumb2_index_mul_operand (rtx op)
4005 {
4006   HOST_WIDE_INT val;
4007   
4008   if (GET_CODE(op) != CONST_INT)
4009     return false;
4010
4011   val = INTVAL(op);
4012   return (val == 1 || val == 2 || val == 4 || val == 8);
4013 }
4014   
4015 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
4016 static int
4017 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4018 {
4019   enum rtx_code code = GET_CODE (index);
4020
4021   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
4022   /* Standard coprocessor addressing modes.  */
4023   if (TARGET_HARD_FLOAT
4024       && (TARGET_FPA || TARGET_MAVERICK)
4025       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4026           || (TARGET_MAVERICK && mode == DImode)))
4027     return (code == CONST_INT && INTVAL (index) < 1024
4028             && INTVAL (index) > -1024
4029             && (INTVAL (index) & 3) == 0);
4030
4031   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4032     {
4033       /* For DImode assume values will usually live in core regs
4034          and only allow LDRD addressing modes.  */
4035       if (!TARGET_LDRD || mode != DImode)
4036         return (code == CONST_INT
4037                 && INTVAL (index) < 1024
4038                 && INTVAL (index) > -1024
4039                 && (INTVAL (index) & 3) == 0);
4040     }
4041
4042   if (TARGET_NEON
4043       && (VALID_NEON_DREG_MODE (mode) || VALID_NEON_QREG_MODE (mode)))
4044     return (code == CONST_INT
4045             && INTVAL (index) < 1016
4046             && INTVAL (index) > -1024
4047             && (INTVAL (index) & 3) == 0);
4048
4049   if (arm_address_register_rtx_p (index, strict_p)
4050       && (GET_MODE_SIZE (mode) <= 4))
4051     return 1;
4052
4053   if (mode == DImode || mode == DFmode)
4054     {
4055       HOST_WIDE_INT val = INTVAL (index);
4056       /* ??? Can we assume ldrd for thumb2?  */
4057       /* Thumb-2 ldrd only has reg+const addressing modes.  */
4058       if (code != CONST_INT)
4059         return 0;
4060
4061       /* ldrd supports offsets of +-1020.
4062          However the ldr fallback does not.  */
4063       return val > -256 && val < 256 && (val & 3) == 0;
4064     }
4065
4066   if (code == MULT)
4067     {
4068       rtx xiop0 = XEXP (index, 0);
4069       rtx xiop1 = XEXP (index, 1);
4070
4071       return ((arm_address_register_rtx_p (xiop0, strict_p)
4072                && thumb2_index_mul_operand (xiop1))
4073               || (arm_address_register_rtx_p (xiop1, strict_p)
4074                   && thumb2_index_mul_operand (xiop0)));
4075     }
4076   else if (code == ASHIFT)
4077     {
4078       rtx op = XEXP (index, 1);
4079
4080       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4081               && GET_CODE (op) == CONST_INT
4082               && INTVAL (op) > 0
4083               && INTVAL (op) <= 3);
4084     }
4085
4086   return (code == CONST_INT
4087           && INTVAL (index) < 4096
4088           && INTVAL (index) > -256);
4089 }
4090
4091 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
4092 static int
4093 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
4094 {
4095   int regno;
4096
4097   if (GET_CODE (x) != REG)
4098     return 0;
4099
4100   regno = REGNO (x);
4101
4102   if (strict_p)
4103     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
4104
4105   return (regno <= LAST_LO_REGNUM
4106           || regno > LAST_VIRTUAL_REGISTER
4107           || regno == FRAME_POINTER_REGNUM
4108           || (GET_MODE_SIZE (mode) >= 4
4109               && (regno == STACK_POINTER_REGNUM
4110                   || regno >= FIRST_PSEUDO_REGISTER
4111                   || x == hard_frame_pointer_rtx
4112                   || x == arg_pointer_rtx)));
4113 }
4114
4115 /* Return nonzero if x is a legitimate index register.  This is the case
4116    for any base register that can access a QImode object.  */
4117 inline static int
4118 thumb1_index_register_rtx_p (rtx x, int strict_p)
4119 {
4120   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4121 }
4122
4123 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4124
4125    The AP may be eliminated to either the SP or the FP, so we use the
4126    least common denominator, e.g. SImode, and offsets from 0 to 64.
4127
4128    ??? Verify whether the above is the right approach.
4129
4130    ??? Also, the FP may be eliminated to the SP, so perhaps that
4131    needs special handling also.
4132
4133    ??? Look at how the mips16 port solves this problem.  It probably uses
4134    better ways to solve some of these problems.
4135
4136    Although it is not incorrect, we don't accept QImode and HImode
4137    addresses based on the frame pointer or arg pointer until the
4138    reload pass starts.  This is so that eliminating such addresses
4139    into stack based ones won't produce impossible code.  */
4140 int
4141 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4142 {
4143   /* ??? Not clear if this is right.  Experiment.  */
4144   if (GET_MODE_SIZE (mode) < 4
4145       && !(reload_in_progress || reload_completed)
4146       && (reg_mentioned_p (frame_pointer_rtx, x)
4147           || reg_mentioned_p (arg_pointer_rtx, x)
4148           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4149           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4150           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4151           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4152     return 0;
4153
4154   /* Accept any base register.  SP only in SImode or larger.  */
4155   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4156     return 1;
4157
4158   /* This is PC relative data before arm_reorg runs.  */
4159   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4160            && GET_CODE (x) == SYMBOL_REF
4161            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4162     return 1;
4163
4164   /* This is PC relative data after arm_reorg runs.  */
4165   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
4166            && (GET_CODE (x) == LABEL_REF
4167                || (GET_CODE (x) == CONST
4168                    && GET_CODE (XEXP (x, 0)) == PLUS
4169                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4170                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4171     return 1;
4172
4173   /* Post-inc indexing only supported for SImode and larger.  */
4174   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4175            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4176     return 1;
4177
4178   else if (GET_CODE (x) == PLUS)
4179     {
4180       /* REG+REG address can be any two index registers.  */
4181       /* We disallow FRAME+REG addressing since we know that FRAME
4182          will be replaced with STACK, and SP relative addressing only
4183          permits SP+OFFSET.  */
4184       if (GET_MODE_SIZE (mode) <= 4
4185           && XEXP (x, 0) != frame_pointer_rtx
4186           && XEXP (x, 1) != frame_pointer_rtx
4187           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4188           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4189         return 1;
4190
4191       /* REG+const has 5-7 bit offset for non-SP registers.  */
4192       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4193                 || XEXP (x, 0) == arg_pointer_rtx)
4194                && GET_CODE (XEXP (x, 1)) == CONST_INT
4195                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4196         return 1;
4197
4198       /* REG+const has 10-bit offset for SP, but only SImode and
4199          larger is supported.  */
4200       /* ??? Should probably check for DI/DFmode overflow here
4201          just like GO_IF_LEGITIMATE_OFFSET does.  */
4202       else if (GET_CODE (XEXP (x, 0)) == REG
4203                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4204                && GET_MODE_SIZE (mode) >= 4
4205                && GET_CODE (XEXP (x, 1)) == CONST_INT
4206                && INTVAL (XEXP (x, 1)) >= 0
4207                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4208                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4209         return 1;
4210
4211       else if (GET_CODE (XEXP (x, 0)) == REG
4212                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4213                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4214                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4215                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4216                && GET_MODE_SIZE (mode) >= 4
4217                && GET_CODE (XEXP (x, 1)) == CONST_INT
4218                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4219         return 1;
4220     }
4221
4222   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4223            && GET_MODE_SIZE (mode) == 4
4224            && GET_CODE (x) == SYMBOL_REF
4225            && CONSTANT_POOL_ADDRESS_P (x)
4226            && ! (flag_pic
4227                  && symbol_mentioned_p (get_pool_constant (x))
4228                  && ! pcrel_constant_p (get_pool_constant (x))))
4229     return 1;
4230
4231   return 0;
4232 }
4233
4234 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4235    instruction of mode MODE.  */
4236 int
4237 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4238 {
4239   switch (GET_MODE_SIZE (mode))
4240     {
4241     case 1:
4242       return val >= 0 && val < 32;
4243
4244     case 2:
4245       return val >= 0 && val < 64 && (val & 1) == 0;
4246
4247     default:
4248       return (val >= 0
4249               && (val + GET_MODE_SIZE (mode)) <= 128
4250               && (val & 3) == 0);
4251     }
4252 }
4253
4254 /* Build the SYMBOL_REF for __tls_get_addr.  */
4255
4256 static GTY(()) rtx tls_get_addr_libfunc;
4257
4258 static rtx
4259 get_tls_get_addr (void)
4260 {
4261   if (!tls_get_addr_libfunc)
4262     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4263   return tls_get_addr_libfunc;
4264 }
4265
4266 static rtx
4267 arm_load_tp (rtx target)
4268 {
4269   if (!target)
4270     target = gen_reg_rtx (SImode);
4271
4272   if (TARGET_HARD_TP)
4273     {
4274       /* Can return in any reg.  */
4275       emit_insn (gen_load_tp_hard (target));
4276     }
4277   else
4278     {
4279       /* Always returned in r0.  Immediately copy the result into a pseudo,
4280          otherwise other uses of r0 (e.g. setting up function arguments) may
4281          clobber the value.  */
4282
4283       rtx tmp;
4284
4285       emit_insn (gen_load_tp_soft ());
4286
4287       tmp = gen_rtx_REG (SImode, 0);
4288       emit_move_insn (target, tmp);
4289     }
4290   return target;
4291 }
4292
4293 static rtx
4294 load_tls_operand (rtx x, rtx reg)
4295 {
4296   rtx tmp;
4297
4298   if (reg == NULL_RTX)
4299     reg = gen_reg_rtx (SImode);
4300
4301   tmp = gen_rtx_CONST (SImode, x);
4302
4303   emit_move_insn (reg, tmp);
4304
4305   return reg;
4306 }
4307
4308 static rtx
4309 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4310 {
4311   rtx insns, label, labelno, sum;
4312
4313   start_sequence ();
4314
4315   labelno = GEN_INT (pic_labelno++);
4316   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4317   label = gen_rtx_CONST (VOIDmode, label);
4318
4319   sum = gen_rtx_UNSPEC (Pmode,
4320                         gen_rtvec (4, x, GEN_INT (reloc), label,
4321                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4322                         UNSPEC_TLS);
4323   reg = load_tls_operand (sum, reg);
4324
4325   if (TARGET_ARM)
4326     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4327   else if (TARGET_THUMB2)
4328     {
4329       rtx tmp;
4330       /* Thumb-2 only allows very limited access to the PC.  Calculate
4331          the address in a temporary register.  */
4332       tmp = gen_reg_rtx (SImode);
4333       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4334       emit_insn (gen_addsi3(reg, reg, tmp));
4335     }
4336   else /* TARGET_THUMB1 */
4337     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4338
4339   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4340                                      Pmode, 1, reg, Pmode);
4341
4342   insns = get_insns ();
4343   end_sequence ();
4344
4345   return insns;
4346 }
4347
4348 rtx
4349 legitimize_tls_address (rtx x, rtx reg)
4350 {
4351   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4352   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4353
4354   switch (model)
4355     {
4356     case TLS_MODEL_GLOBAL_DYNAMIC:
4357       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4358       dest = gen_reg_rtx (Pmode);
4359       emit_libcall_block (insns, dest, ret, x);
4360       return dest;
4361
4362     case TLS_MODEL_LOCAL_DYNAMIC:
4363       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4364
4365       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4366          share the LDM result with other LD model accesses.  */
4367       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4368                             UNSPEC_TLS);
4369       dest = gen_reg_rtx (Pmode);
4370       emit_libcall_block (insns, dest, ret, eqv);
4371
4372       /* Load the addend.  */
4373       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4374                                UNSPEC_TLS);
4375       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4376       return gen_rtx_PLUS (Pmode, dest, addend);
4377
4378     case TLS_MODEL_INITIAL_EXEC:
4379       labelno = GEN_INT (pic_labelno++);
4380       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4381       label = gen_rtx_CONST (VOIDmode, label);
4382       sum = gen_rtx_UNSPEC (Pmode,
4383                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4384                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4385                             UNSPEC_TLS);
4386       reg = load_tls_operand (sum, reg);
4387
4388       if (TARGET_ARM)
4389         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4390       else if (TARGET_THUMB2)
4391         {
4392           rtx tmp;
4393           /* Thumb-2 only allows very limited access to the PC.  Calculate
4394              the address in a temporary register.  */
4395           tmp = gen_reg_rtx (SImode);
4396           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4397           emit_insn (gen_addsi3(reg, reg, tmp));
4398           emit_move_insn (reg, gen_const_mem (SImode, reg));
4399         }
4400       else
4401         {
4402           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4403           emit_move_insn (reg, gen_const_mem (SImode, reg));
4404         }
4405
4406       tp = arm_load_tp (NULL_RTX);
4407
4408       return gen_rtx_PLUS (Pmode, tp, reg);
4409
4410     case TLS_MODEL_LOCAL_EXEC:
4411       tp = arm_load_tp (NULL_RTX);
4412
4413       reg = gen_rtx_UNSPEC (Pmode,
4414                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4415                             UNSPEC_TLS);
4416       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4417
4418       return gen_rtx_PLUS (Pmode, tp, reg);
4419
4420     default:
4421       abort ();
4422     }
4423 }
4424
4425 /* Try machine-dependent ways of modifying an illegitimate address
4426    to be legitimate.  If we find one, return the new, valid address.  */
4427 rtx
4428 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4429 {
4430   if (arm_tls_symbol_p (x))
4431     return legitimize_tls_address (x, NULL_RTX);
4432
4433   if (GET_CODE (x) == PLUS)
4434     {
4435       rtx xop0 = XEXP (x, 0);
4436       rtx xop1 = XEXP (x, 1);
4437
4438       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4439         xop0 = force_reg (SImode, xop0);
4440
4441       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4442         xop1 = force_reg (SImode, xop1);
4443
4444       if (ARM_BASE_REGISTER_RTX_P (xop0)
4445           && GET_CODE (xop1) == CONST_INT)
4446         {
4447           HOST_WIDE_INT n, low_n;
4448           rtx base_reg, val;
4449           n = INTVAL (xop1);
4450
4451           /* VFP addressing modes actually allow greater offsets, but for
4452              now we just stick with the lowest common denominator.  */
4453           if (mode == DImode
4454               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4455             {
4456               low_n = n & 0x0f;
4457               n &= ~0x0f;
4458               if (low_n > 4)
4459                 {
4460                   n += 16;
4461                   low_n -= 16;
4462                 }
4463             }
4464           else
4465             {
4466               low_n = ((mode) == TImode ? 0
4467                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4468               n -= low_n;
4469             }
4470
4471           base_reg = gen_reg_rtx (SImode);
4472           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4473           emit_move_insn (base_reg, val);
4474           x = plus_constant (base_reg, low_n);
4475         }
4476       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4477         x = gen_rtx_PLUS (SImode, xop0, xop1);
4478     }
4479
4480   /* XXX We don't allow MINUS any more -- see comment in
4481      arm_legitimate_address_p ().  */
4482   else if (GET_CODE (x) == MINUS)
4483     {
4484       rtx xop0 = XEXP (x, 0);
4485       rtx xop1 = XEXP (x, 1);
4486
4487       if (CONSTANT_P (xop0))
4488         xop0 = force_reg (SImode, xop0);
4489
4490       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4491         xop1 = force_reg (SImode, xop1);
4492
4493       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4494         x = gen_rtx_MINUS (SImode, xop0, xop1);
4495     }
4496
4497   /* Make sure to take full advantage of the pre-indexed addressing mode
4498      with absolute addresses which often allows for the base register to
4499      be factorized for multiple adjacent memory references, and it might
4500      even allows for the mini pool to be avoided entirely. */
4501   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4502     {
4503       unsigned int bits;
4504       HOST_WIDE_INT mask, base, index;
4505       rtx base_reg;
4506
4507       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4508          use a 8-bit index. So let's use a 12-bit index for SImode only and
4509          hope that arm_gen_constant will enable ldrb to use more bits. */
4510       bits = (mode == SImode) ? 12 : 8;
4511       mask = (1 << bits) - 1;
4512       base = INTVAL (x) & ~mask;
4513       index = INTVAL (x) & mask;
4514       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4515         {
4516           /* It'll most probably be more efficient to generate the base
4517              with more bits set and use a negative index instead. */
4518           base |= mask;
4519           index -= mask;
4520         }
4521       base_reg = force_reg (SImode, GEN_INT (base));
4522       x = plus_constant (base_reg, index);
4523     }
4524
4525   if (flag_pic)
4526     {
4527       /* We need to find and carefully transform any SYMBOL and LABEL
4528          references; so go back to the original address expression.  */
4529       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4530
4531       if (new_x != orig_x)
4532         x = new_x;
4533     }
4534
4535   return x;
4536 }
4537
4538
4539 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4540    to be legitimate.  If we find one, return the new, valid address.  */
4541 rtx
4542 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4543 {
4544   if (arm_tls_symbol_p (x))
4545     return legitimize_tls_address (x, NULL_RTX);
4546
4547   if (GET_CODE (x) == PLUS
4548       && GET_CODE (XEXP (x, 1)) == CONST_INT
4549       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4550           || INTVAL (XEXP (x, 1)) < 0))
4551     {
4552       rtx xop0 = XEXP (x, 0);
4553       rtx xop1 = XEXP (x, 1);
4554       HOST_WIDE_INT offset = INTVAL (xop1);
4555
4556       /* Try and fold the offset into a biasing of the base register and
4557          then offsetting that.  Don't do this when optimizing for space
4558          since it can cause too many CSEs.  */
4559       if (optimize_size && offset >= 0
4560           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4561         {
4562           HOST_WIDE_INT delta;
4563
4564           if (offset >= 256)
4565             delta = offset - (256 - GET_MODE_SIZE (mode));
4566           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4567             delta = 31 * GET_MODE_SIZE (mode);
4568           else
4569             delta = offset & (~31 * GET_MODE_SIZE (mode));
4570
4571           xop0 = force_operand (plus_constant (xop0, offset - delta),
4572                                 NULL_RTX);
4573           x = plus_constant (xop0, delta);
4574         }
4575       else if (offset < 0 && offset > -256)
4576         /* Small negative offsets are best done with a subtract before the
4577            dereference, forcing these into a register normally takes two
4578            instructions.  */
4579         x = force_operand (x, NULL_RTX);
4580       else
4581         {
4582           /* For the remaining cases, force the constant into a register.  */
4583           xop1 = force_reg (SImode, xop1);
4584           x = gen_rtx_PLUS (SImode, xop0, xop1);
4585         }
4586     }
4587   else if (GET_CODE (x) == PLUS
4588            && s_register_operand (XEXP (x, 1), SImode)
4589            && !s_register_operand (XEXP (x, 0), SImode))
4590     {
4591       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4592
4593       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4594     }
4595
4596   if (flag_pic)
4597     {
4598       /* We need to find and carefully transform any SYMBOL and LABEL
4599          references; so go back to the original address expression.  */
4600       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4601
4602       if (new_x != orig_x)
4603         x = new_x;
4604     }
4605
4606   return x;
4607 }
4608
4609 rtx
4610 thumb_legitimize_reload_address (rtx *x_p,
4611                                  enum machine_mode mode,
4612                                  int opnum, int type,
4613                                  int ind_levels ATTRIBUTE_UNUSED)
4614 {
4615   rtx x = *x_p;
4616
4617   if (GET_CODE (x) == PLUS
4618       && GET_MODE_SIZE (mode) < 4
4619       && REG_P (XEXP (x, 0))
4620       && XEXP (x, 0) == stack_pointer_rtx
4621       && GET_CODE (XEXP (x, 1)) == CONST_INT
4622       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4623     {
4624       rtx orig_x = x;
4625
4626       x = copy_rtx (x);
4627       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4628                    Pmode, VOIDmode, 0, 0, opnum, type);
4629       return x;
4630     }
4631
4632   /* If both registers are hi-regs, then it's better to reload the
4633      entire expression rather than each register individually.  That
4634      only requires one reload register rather than two.  */
4635   if (GET_CODE (x) == PLUS
4636       && REG_P (XEXP (x, 0))
4637       && REG_P (XEXP (x, 1))
4638       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4639       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4640     {
4641       rtx orig_x = x;
4642
4643       x = copy_rtx (x);
4644       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4645                    Pmode, VOIDmode, 0, 0, opnum, type);
4646       return x;
4647     }
4648
4649   return NULL;
4650 }
4651
4652 /* Test for various thread-local symbols.  */
4653
4654 /* Return TRUE if X is a thread-local symbol.  */
4655
4656 static bool
4657 arm_tls_symbol_p (rtx x)
4658 {
4659   if (! TARGET_HAVE_TLS)
4660     return false;
4661
4662   if (GET_CODE (x) != SYMBOL_REF)
4663     return false;
4664
4665   return SYMBOL_REF_TLS_MODEL (x) != 0;
4666 }
4667
4668 /* Helper for arm_tls_referenced_p.  */
4669
4670 static int
4671 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4672 {
4673   if (GET_CODE (*x) == SYMBOL_REF)
4674     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4675
4676   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4677      TLS offsets, not real symbol references.  */
4678   if (GET_CODE (*x) == UNSPEC
4679       && XINT (*x, 1) == UNSPEC_TLS)
4680     return -1;
4681
4682   return 0;
4683 }
4684
4685 /* Return TRUE if X contains any TLS symbol references.  */
4686
4687 bool
4688 arm_tls_referenced_p (rtx x)
4689 {
4690   if (! TARGET_HAVE_TLS)
4691     return false;
4692
4693   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4694 }
4695
4696 /* Implement TARGET_CANNOT_FORCE_CONST_MEM.  */
4697
4698 bool
4699 arm_cannot_force_const_mem (rtx x)
4700 {
4701   rtx base, offset;
4702
4703   if (ARM_OFFSETS_MUST_BE_WITHIN_SECTIONS_P)
4704     {
4705       split_const (x, &base, &offset);
4706       if (GET_CODE (base) == SYMBOL_REF
4707           && !offset_within_block_p (base, INTVAL (offset)))
4708         return true;
4709     }
4710   return arm_tls_referenced_p (x);
4711 }
4712 \f
4713 #define REG_OR_SUBREG_REG(X)                                            \
4714   (GET_CODE (X) == REG                                                  \
4715    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4716
4717 #define REG_OR_SUBREG_RTX(X)                    \
4718    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4719
4720 #ifndef COSTS_N_INSNS
4721 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4722 #endif
4723 static inline int
4724 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4725 {
4726   enum machine_mode mode = GET_MODE (x);
4727
4728   switch (code)
4729     {
4730     case ASHIFT:
4731     case ASHIFTRT:
4732     case LSHIFTRT:
4733     case ROTATERT:
4734     case PLUS:
4735     case MINUS:
4736     case COMPARE:
4737     case NEG:
4738     case NOT:
4739       return COSTS_N_INSNS (1);
4740
4741     case MULT:
4742       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4743         {
4744           int cycles = 0;
4745           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4746
4747           while (i)
4748             {
4749               i >>= 2;
4750               cycles++;
4751             }
4752           return COSTS_N_INSNS (2) + cycles;
4753         }
4754       return COSTS_N_INSNS (1) + 16;
4755
4756     case SET:
4757       return (COSTS_N_INSNS (1)
4758               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4759                      + GET_CODE (SET_DEST (x)) == MEM));
4760
4761     case CONST_INT:
4762       if (outer == SET)
4763         {
4764           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4765             return 0;
4766           if (thumb_shiftable_const (INTVAL (x)))
4767             return COSTS_N_INSNS (2);
4768           return COSTS_N_INSNS (3);
4769         }
4770       else if ((outer == PLUS || outer == COMPARE)
4771                && INTVAL (x) < 256 && INTVAL (x) > -256)
4772         return 0;
4773       else if (outer == AND
4774                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4775         return COSTS_N_INSNS (1);
4776       else if (outer == ASHIFT || outer == ASHIFTRT
4777                || outer == LSHIFTRT)
4778         return 0;
4779       return COSTS_N_INSNS (2);
4780
4781     case CONST:
4782     case CONST_DOUBLE:
4783     case LABEL_REF:
4784     case SYMBOL_REF:
4785       return COSTS_N_INSNS (3);
4786
4787     case UDIV:
4788     case UMOD:
4789     case DIV:
4790     case MOD:
4791       return 100;
4792
4793     case TRUNCATE:
4794       return 99;
4795
4796     case AND:
4797     case XOR:
4798     case IOR:
4799       /* XXX guess.  */
4800       return 8;
4801
4802     case MEM:
4803       /* XXX another guess.  */
4804       /* Memory costs quite a lot for the first word, but subsequent words
4805          load at the equivalent of a single insn each.  */
4806       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4807               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4808                  ? 4 : 0));
4809
4810     case IF_THEN_ELSE:
4811       /* XXX a guess.  */
4812       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4813         return 14;
4814       return 2;
4815
4816     case ZERO_EXTEND:
4817       /* XXX still guessing.  */
4818       switch (GET_MODE (XEXP (x, 0)))
4819         {
4820         case QImode:
4821           return (1 + (mode == DImode ? 4 : 0)
4822                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4823
4824         case HImode:
4825           return (4 + (mode == DImode ? 4 : 0)
4826                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4827
4828         case SImode:
4829           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4830
4831         default:
4832           return 99;
4833         }
4834
4835     default:
4836       return 99;
4837     }
4838 }
4839
4840
4841 /* Worker routine for arm_rtx_costs.  */
4842 /* ??? This needs updating for thumb2.  */
4843 static inline int
4844 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4845 {
4846   enum machine_mode mode = GET_MODE (x);
4847   enum rtx_code subcode;
4848   int extra_cost;
4849
4850   switch (code)
4851     {
4852     case MEM:
4853       /* Memory costs quite a lot for the first word, but subsequent words
4854          load at the equivalent of a single insn each.  */
4855       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4856               + (GET_CODE (x) == SYMBOL_REF
4857                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4858
4859     case DIV:
4860     case MOD:
4861     case UDIV:
4862     case UMOD:
4863       return optimize_size ? COSTS_N_INSNS (2) : 100;
4864
4865     case ROTATE:
4866       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4867         return 4;
4868       /* Fall through */
4869     case ROTATERT:
4870       if (mode != SImode)
4871         return 8;
4872       /* Fall through */
4873     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4874       if (mode == DImode)
4875         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4876                 + ((GET_CODE (XEXP (x, 0)) == REG
4877                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4878                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4879                    ? 0 : 8));
4880       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4881                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4882                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4883                    ? 0 : 4)
4884               + ((GET_CODE (XEXP (x, 1)) == REG
4885                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4886                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4887                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4888                  ? 0 : 4));
4889
4890     case MINUS:
4891       if (GET_CODE (XEXP (x, 1)) == MULT && mode == SImode && arm_arch_thumb2)
4892         {
4893           extra_cost = rtx_cost (XEXP (x, 1), code);
4894           if (!REG_OR_SUBREG_REG (XEXP (x, 0)))
4895             extra_cost += 4 * ARM_NUM_REGS (mode);
4896           return extra_cost;
4897         }
4898
4899       if (mode == DImode)
4900         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4901                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4902                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4903                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4904                    ? 0 : 8));
4905
4906       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4907         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4908                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4909                           && arm_const_double_rtx (XEXP (x, 1))))
4910                      ? 0 : 8)
4911                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4912                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4913                         && arm_const_double_rtx (XEXP (x, 0))))
4914                    ? 0 : 8));
4915
4916       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4917             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4918             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4919           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4920                || subcode == ASHIFTRT || subcode == LSHIFTRT
4921                || subcode == ROTATE || subcode == ROTATERT
4922                || (subcode == MULT
4923                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4924                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4925                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4926               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4927               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4928                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4929               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4930         return 1;
4931       /* Fall through */
4932
4933     case PLUS:
4934       if (GET_CODE (XEXP (x, 0)) == MULT)
4935         {
4936           extra_cost = rtx_cost (XEXP (x, 0), code);
4937           if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4938             extra_cost += 4 * ARM_NUM_REGS (mode);
4939           return extra_cost;
4940         }
4941
4942       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4943         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4944                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4945                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4946                         && arm_const_double_rtx (XEXP (x, 1))))
4947                    ? 0 : 8));
4948
4949       /* Fall through */
4950     case AND: case XOR: case IOR:
4951       extra_cost = 0;
4952
4953       /* Normally the frame registers will be spilt into reg+const during
4954          reload, so it is a bad idea to combine them with other instructions,
4955          since then they might not be moved outside of loops.  As a compromise
4956          we allow integration with ops that have a constant as their second
4957          operand.  */
4958       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4959            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4960            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4961           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4962               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4963         extra_cost = 4;
4964
4965       if (mode == DImode)
4966         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4967                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4968                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4969                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4970                    ? 0 : 8));
4971
4972       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4973         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4974                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4975                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4976                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4977                    ? 0 : 4));
4978
4979       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4980         return (1 + extra_cost
4981                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4982                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4983                      || subcode == ROTATE || subcode == ROTATERT
4984                      || (subcode == MULT
4985                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4986                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4987                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4988                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4989                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4990                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4991                    ? 0 : 4));
4992
4993       return 8;
4994
4995     case MULT:
4996       /* This should have been handled by the CPU specific routines.  */
4997       gcc_unreachable ();
4998
4999     case TRUNCATE:
5000       if (arm_arch3m && mode == SImode
5001           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5002           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5003           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
5004               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
5005           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
5006               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
5007         return 8;
5008       return 99;
5009
5010     case NEG:
5011       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5012         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
5013       /* Fall through */
5014     case NOT:
5015       if (mode == DImode)
5016         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
5017
5018       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
5019
5020     case IF_THEN_ELSE:
5021       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
5022         return 14;
5023       return 2;
5024
5025     case COMPARE:
5026       return 1;
5027
5028     case ABS:
5029       return 4 + (mode == DImode ? 4 : 0);
5030
5031     case SIGN_EXTEND:
5032       /* ??? value extensions are cheaper on armv6. */
5033       if (GET_MODE (XEXP (x, 0)) == QImode)
5034         return (4 + (mode == DImode ? 4 : 0)
5035                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5036       /* Fall through */
5037     case ZERO_EXTEND:
5038       switch (GET_MODE (XEXP (x, 0)))
5039         {
5040         case QImode:
5041           return (1 + (mode == DImode ? 4 : 0)
5042                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5043
5044         case HImode:
5045           return (4 + (mode == DImode ? 4 : 0)
5046                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5047
5048         case SImode:
5049           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5050
5051         case V8QImode:
5052         case V4HImode:
5053         case V2SImode:
5054         case V4QImode:
5055         case V2HImode:
5056             return 1;
5057
5058         default:
5059           gcc_unreachable ();
5060         }
5061       gcc_unreachable ();
5062
5063     case CONST_INT:
5064       if (const_ok_for_arm (INTVAL (x)))
5065         return outer == SET ? 2 : -1;
5066       else if (outer == AND
5067                && const_ok_for_arm (~INTVAL (x)))
5068         return -1;
5069       else if ((outer == COMPARE
5070                 || outer == PLUS || outer == MINUS)
5071                && const_ok_for_arm (-INTVAL (x)))
5072         return -1;
5073       else
5074         return 5;
5075
5076     case CONST:
5077     case LABEL_REF:
5078     case SYMBOL_REF:
5079       return 6;
5080
5081     case CONST_DOUBLE:
5082       if (arm_const_double_rtx (x) || vfp3_const_double_rtx (x))
5083         return outer == SET ? 2 : -1;
5084       else if ((outer == COMPARE || outer == PLUS)
5085                && neg_const_double_rtx_ok_for_fpa (x))
5086         return -1;
5087       return 7;
5088
5089     default:
5090       return 99;
5091     }
5092 }
5093
5094 /* RTX costs when optimizing for size.  */
5095 static bool
5096 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
5097 {
5098   enum machine_mode mode = GET_MODE (x);
5099
5100   if (TARGET_THUMB)
5101     {
5102       /* XXX TBD.  For now, use the standard costs.  */
5103       *total = thumb1_rtx_costs (x, code, outer_code);
5104       return true;
5105     }
5106
5107   switch (code)
5108     {
5109     case MEM:
5110       /* A memory access costs 1 insn if the mode is small, or the address is
5111          a single register, otherwise it costs one insn per word.  */
5112       if (REG_P (XEXP (x, 0)))
5113         *total = COSTS_N_INSNS (1);
5114       else
5115         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5116       return true;
5117
5118     case DIV:
5119     case MOD:
5120     case UDIV:
5121     case UMOD:
5122       /* Needs a libcall, so it costs about this.  */
5123       *total = COSTS_N_INSNS (2);
5124       return false;
5125
5126     case ROTATE:
5127       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5128         {
5129           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
5130           return true;
5131         }
5132       /* Fall through */
5133     case ROTATERT:
5134     case ASHIFT:
5135     case LSHIFTRT:
5136     case ASHIFTRT:
5137       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5138         {
5139           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
5140           return true;
5141         }
5142       else if (mode == SImode)
5143         {
5144           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
5145           /* Slightly disparage register shifts, but not by much.  */
5146           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5147             *total += 1 + rtx_cost (XEXP (x, 1), code);
5148           return true;
5149         }
5150
5151       /* Needs a libcall.  */
5152       *total = COSTS_N_INSNS (2);
5153       return false;
5154
5155     case MINUS:
5156       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5157         {
5158           *total = COSTS_N_INSNS (1);
5159           return false;
5160         }
5161
5162       if (mode == SImode)
5163         {
5164           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5165           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5166
5167           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5168               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5169               || subcode1 == ROTATE || subcode1 == ROTATERT
5170               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5171               || subcode1 == ASHIFTRT)
5172             {
5173               /* It's just the cost of the two operands.  */
5174               *total = 0;
5175               return false;
5176             }
5177
5178           *total = COSTS_N_INSNS (1);
5179           return false;
5180         }
5181
5182       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5183       return false;
5184
5185     case PLUS:
5186       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5187         {
5188           *total = COSTS_N_INSNS (1);
5189           return false;
5190         }
5191
5192       /* Fall through */
5193     case AND: case XOR: case IOR:
5194       if (mode == SImode)
5195         {
5196           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5197
5198           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5199               || subcode == LSHIFTRT || subcode == ASHIFTRT
5200               || (code == AND && subcode == NOT))
5201             {
5202               /* It's just the cost of the two operands.  */
5203               *total = 0;
5204               return false;
5205             }
5206         }
5207
5208       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5209       return false;
5210
5211     case MULT:
5212       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5213       return false;
5214
5215     case NEG:
5216       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5217         *total = COSTS_N_INSNS (1);
5218       /* Fall through */
5219     case NOT:
5220       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5221
5222       return false;
5223
5224     case IF_THEN_ELSE:
5225       *total = 0;
5226       return false;
5227
5228     case COMPARE:
5229       if (cc_register (XEXP (x, 0), VOIDmode))
5230         * total = 0;
5231       else
5232         *total = COSTS_N_INSNS (1);
5233       return false;
5234
5235     case ABS:
5236       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5237         *total = COSTS_N_INSNS (1);
5238       else
5239         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5240       return false;
5241
5242     case SIGN_EXTEND:
5243       *total = 0;
5244       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5245         {
5246           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5247             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5248         }
5249       if (mode == DImode)
5250         *total += COSTS_N_INSNS (1);
5251       return false;
5252
5253     case ZERO_EXTEND:
5254       *total = 0;
5255       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5256         {
5257           switch (GET_MODE (XEXP (x, 0)))
5258             {
5259             case QImode:
5260               *total += COSTS_N_INSNS (1);
5261               break;
5262
5263             case HImode:
5264               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5265
5266             case SImode:
5267               break;
5268
5269             default:
5270               *total += COSTS_N_INSNS (2);
5271             }
5272         }
5273
5274       if (mode == DImode)
5275         *total += COSTS_N_INSNS (1);
5276
5277       return false;
5278
5279     case CONST_INT:
5280       if (const_ok_for_arm (INTVAL (x)))
5281         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5282       else if (const_ok_for_arm (~INTVAL (x)))
5283         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5284       else if (const_ok_for_arm (-INTVAL (x)))
5285         {
5286           if (outer_code == COMPARE || outer_code == PLUS
5287               || outer_code == MINUS)
5288             *total = 0;
5289           else
5290             *total = COSTS_N_INSNS (1);
5291         }
5292       else
5293         *total = COSTS_N_INSNS (2);
5294       return true;
5295
5296     case CONST:
5297     case LABEL_REF:
5298     case SYMBOL_REF:
5299       *total = COSTS_N_INSNS (2);
5300       return true;
5301
5302     case CONST_DOUBLE:
5303       *total = COSTS_N_INSNS (4);
5304       return true;
5305
5306     default:
5307       if (mode != VOIDmode)
5308         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5309       else
5310         *total = COSTS_N_INSNS (4); /* How knows?  */
5311       return false;
5312     }
5313 }
5314
5315 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
5316    supported on any "slowmul" cores, so it can be ignored.  */
5317
5318 static bool
5319 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5320 {
5321   enum machine_mode mode = GET_MODE (x);
5322
5323   if (TARGET_THUMB)
5324     {
5325       *total = thumb1_rtx_costs (x, code, outer_code);
5326       return true;
5327     }
5328
5329   switch (code)
5330     {
5331     case MULT:
5332       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5333           || mode == DImode)
5334         {
5335           *total = 30;
5336           return true;
5337         }
5338
5339       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5340         {
5341           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5342                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5343           int cost, const_ok = const_ok_for_arm (i);
5344           int j, booth_unit_size;
5345
5346           /* Tune as appropriate.  */
5347           cost = const_ok ? 4 : 8;
5348           booth_unit_size = 2;
5349           for (j = 0; i && j < 32; j += booth_unit_size)
5350             {
5351               i >>= booth_unit_size;
5352               cost += 2;
5353             }
5354
5355           *total = cost;
5356           return true;
5357         }
5358
5359       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5360                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5361       return true;
5362
5363     default:
5364       *total = arm_rtx_costs_1 (x, code, outer_code);
5365       return true;
5366     }
5367 }
5368
5369
5370 /* RTX cost for cores with a fast multiply unit (M variants).  */
5371
5372 static bool
5373 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5374 {
5375   enum machine_mode mode = GET_MODE (x);
5376
5377   if (TARGET_THUMB1)
5378     {
5379       *total = thumb1_rtx_costs (x, code, outer_code);
5380       return true;
5381     }
5382
5383   /* ??? should thumb2 use different costs?  */
5384   switch (code)
5385     {
5386     case MULT:
5387       /* There is no point basing this on the tuning, since it is always the
5388          fast variant if it exists at all.  */
5389       if (mode == DImode
5390           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5391           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5392               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5393         {
5394           *total = 8;
5395           return true;
5396         }
5397
5398
5399       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5400           || mode == DImode)
5401         {
5402           *total = 30;
5403           return true;
5404         }
5405
5406       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5407         {
5408           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5409                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5410           int cost, const_ok = const_ok_for_arm (i);
5411           int j, booth_unit_size;
5412
5413           /* Tune as appropriate.  */
5414           cost = const_ok ? 4 : 8;
5415           booth_unit_size = 8;
5416           for (j = 0; i && j < 32; j += booth_unit_size)
5417             {
5418               i >>= booth_unit_size;
5419               cost += 2;
5420             }
5421
5422           *total = cost;
5423           return true;
5424         }
5425
5426       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5427                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5428       return true;
5429
5430     default:
5431       *total = arm_rtx_costs_1 (x, code, outer_code);
5432       return true;
5433     }
5434 }
5435
5436
5437 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
5438    so it can be ignored.  */
5439
5440 static bool
5441 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
5442 {
5443   enum machine_mode mode = GET_MODE (x);
5444
5445   if (TARGET_THUMB)
5446     {
5447       *total = thumb1_rtx_costs (x, code, outer_code);
5448       return true;
5449     }
5450
5451   switch (code)
5452     {
5453     case MULT:
5454       /* There is no point basing this on the tuning, since it is always the
5455          fast variant if it exists at all.  */
5456       if (mode == DImode
5457           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5458           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5459               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5460         {
5461           *total = 8;
5462           return true;
5463         }
5464
5465
5466       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5467           || mode == DImode)
5468         {
5469           *total = 30;
5470           return true;
5471         }
5472
5473       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5474         {
5475           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5476                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5477           int cost, const_ok = const_ok_for_arm (i);
5478           unsigned HOST_WIDE_INT masked_const;
5479
5480           /* The cost will be related to two insns.
5481              First a load of the constant (MOV or LDR), then a multiply.  */
5482           cost = 2;
5483           if (! const_ok)
5484             cost += 1;      /* LDR is probably more expensive because
5485                                of longer result latency.  */
5486           masked_const = i & 0xffff8000;
5487           if (masked_const != 0 && masked_const != 0xffff8000)
5488             {
5489               masked_const = i & 0xf8000000;
5490               if (masked_const == 0 || masked_const == 0xf8000000)
5491                 cost += 1;
5492               else
5493                 cost += 2;
5494             }
5495           *total = cost;
5496           return true;
5497         }
5498
5499       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5500                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5501       return true;
5502
5503     case COMPARE:
5504       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5505          will stall until the multiplication is complete.  */
5506       if (GET_CODE (XEXP (x, 0)) == MULT)
5507         *total = 4 + rtx_cost (XEXP (x, 0), code);
5508       else
5509         *total = arm_rtx_costs_1 (x, code, outer_code);
5510       return true;
5511
5512     default:
5513       *total = arm_rtx_costs_1 (x, code, outer_code);
5514       return true;
5515     }
5516 }
5517
5518
5519 /* RTX costs for 9e (and later) cores.  */
5520
5521 static bool
5522 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5523 {
5524   enum machine_mode mode = GET_MODE (x);
5525   int nonreg_cost;
5526   int cost;
5527
5528   if (TARGET_THUMB1)
5529     {
5530       switch (code)
5531         {
5532         case MULT:
5533           *total = COSTS_N_INSNS (3);
5534           return true;
5535
5536         default:
5537           *total = thumb1_rtx_costs (x, code, outer_code);
5538           return true;
5539         }
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 = 3;
5553           return true;
5554         }
5555
5556
5557       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5558         {
5559           *total = 30;
5560           return true;
5561         }
5562       if (mode == DImode)
5563         {
5564           cost = 7;
5565           nonreg_cost = 8;
5566         }
5567       else
5568         {
5569           cost = 2;
5570           nonreg_cost = 4;
5571         }
5572
5573
5574       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5575                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5576       return true;
5577
5578     default:
5579       *total = arm_rtx_costs_1 (x, code, outer_code);
5580       return true;
5581     }
5582 }
5583 /* All address computations that can be done are free, but rtx cost returns
5584    the same for practically all of them.  So we weight the different types
5585    of address here in the order (most pref first):
5586    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5587 static inline int
5588 arm_arm_address_cost (rtx x)
5589 {
5590   enum rtx_code c  = GET_CODE (x);
5591
5592   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5593     return 0;
5594   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5595     return 10;
5596
5597   if (c == PLUS || c == MINUS)
5598     {
5599       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5600         return 2;
5601
5602       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5603         return 3;
5604
5605       return 4;
5606     }
5607
5608   return 6;
5609 }
5610
5611 static inline int
5612 arm_thumb_address_cost (rtx x)
5613 {
5614   enum rtx_code c  = GET_CODE (x);
5615
5616   if (c == REG)
5617     return 1;
5618   if (c == PLUS
5619       && GET_CODE (XEXP (x, 0)) == REG
5620       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5621     return 1;
5622
5623   return 2;
5624 }
5625
5626 static int
5627 arm_address_cost (rtx x)
5628 {
5629   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5630 }
5631
5632 static int
5633 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5634 {
5635   rtx i_pat, d_pat;
5636
5637   /* Some true dependencies can have a higher cost depending
5638      on precisely how certain input operands are used.  */
5639   if (arm_tune_xscale
5640       && REG_NOTE_KIND (link) == 0
5641       && recog_memoized (insn) >= 0
5642       && recog_memoized (dep) >= 0)
5643     {
5644       int shift_opnum = get_attr_shift (insn);
5645       enum attr_type attr_type = get_attr_type (dep);
5646
5647       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5648          operand for INSN.  If we have a shifted input operand and the
5649          instruction we depend on is another ALU instruction, then we may
5650          have to account for an additional stall.  */
5651       if (shift_opnum != 0
5652           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5653         {
5654           rtx shifted_operand;
5655           int opno;
5656
5657           /* Get the shifted operand.  */
5658           extract_insn (insn);
5659           shifted_operand = recog_data.operand[shift_opnum];
5660
5661           /* Iterate over all the operands in DEP.  If we write an operand
5662              that overlaps with SHIFTED_OPERAND, then we have increase the
5663              cost of this dependency.  */
5664           extract_insn (dep);
5665           preprocess_constraints ();
5666           for (opno = 0; opno < recog_data.n_operands; opno++)
5667             {
5668               /* We can ignore strict inputs.  */
5669               if (recog_data.operand_type[opno] == OP_IN)
5670                 continue;
5671
5672               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5673                                            shifted_operand))
5674                 return 2;
5675             }
5676         }
5677     }
5678
5679   /* XXX This is not strictly true for the FPA.  */
5680   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5681       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5682     return 0;
5683
5684   /* Call insns don't incur a stall, even if they follow a load.  */
5685   if (REG_NOTE_KIND (link) == 0
5686       && GET_CODE (insn) == CALL_INSN)
5687     return 1;
5688
5689   if ((i_pat = single_set (insn)) != NULL
5690       && GET_CODE (SET_SRC (i_pat)) == MEM
5691       && (d_pat = single_set (dep)) != NULL
5692       && GET_CODE (SET_DEST (d_pat)) == MEM)
5693     {
5694       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5695       /* This is a load after a store, there is no conflict if the load reads
5696          from a cached area.  Assume that loads from the stack, and from the
5697          constant pool are cached, and that others will miss.  This is a
5698          hack.  */
5699
5700       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5701           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5702           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5703           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5704         return 1;
5705     }
5706
5707   return cost;
5708 }
5709
5710 static int fp_consts_inited = 0;
5711
5712 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5713 static const char * const strings_fp[8] =
5714 {
5715   "0",   "1",   "2",   "3",
5716   "4",   "5",   "0.5", "10"
5717 };
5718
5719 static REAL_VALUE_TYPE values_fp[8];
5720
5721 static void
5722 init_fp_table (void)
5723 {
5724   int i;
5725   REAL_VALUE_TYPE r;
5726
5727   if (TARGET_VFP)
5728     fp_consts_inited = 1;
5729   else
5730     fp_consts_inited = 8;
5731
5732   for (i = 0; i < fp_consts_inited; i++)
5733     {
5734       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5735       values_fp[i] = r;
5736     }
5737 }
5738
5739 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5740 int
5741 arm_const_double_rtx (rtx x)
5742 {
5743   REAL_VALUE_TYPE r;
5744   int i;
5745
5746   if (!fp_consts_inited)
5747     init_fp_table ();
5748
5749   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5750   if (REAL_VALUE_MINUS_ZERO (r))
5751     return 0;
5752
5753   for (i = 0; i < fp_consts_inited; i++)
5754     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5755       return 1;
5756
5757   return 0;
5758 }
5759
5760 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5761 int
5762 neg_const_double_rtx_ok_for_fpa (rtx x)
5763 {
5764   REAL_VALUE_TYPE r;
5765   int i;
5766
5767   if (!fp_consts_inited)
5768     init_fp_table ();
5769
5770   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5771   r = REAL_VALUE_NEGATE (r);
5772   if (REAL_VALUE_MINUS_ZERO (r))
5773     return 0;
5774
5775   for (i = 0; i < 8; i++)
5776     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5777       return 1;
5778
5779   return 0;
5780 }
5781
5782
5783 /* VFPv3 has a fairly wide range of representable immediates, formed from
5784    "quarter-precision" floating-point values. These can be evaluated using this
5785    formula (with ^ for exponentiation):
5786
5787      -1^s * n * 2^-r
5788
5789    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
5790    16 <= n <= 31 and 0 <= r <= 7.
5791
5792    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
5793
5794      - A (most-significant) is the sign bit.
5795      - BCD are the exponent (encoded as r XOR 3).
5796      - EFGH are the mantissa (encoded as n - 16).
5797 */
5798
5799 /* Return an integer index for a VFPv3 immediate operand X suitable for the
5800    fconst[sd] instruction, or -1 if X isn't suitable.  */
5801 static int
5802 vfp3_const_double_index (rtx x)
5803 {
5804   REAL_VALUE_TYPE r, m;
5805   int sign, exponent;
5806   unsigned HOST_WIDE_INT mantissa, mant_hi;
5807   unsigned HOST_WIDE_INT mask;
5808   HOST_WIDE_INT m1, m2;
5809   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
5810
5811   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
5812     return -1;
5813
5814   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5815
5816   /* We can't represent these things, so detect them first.  */
5817   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
5818     return -1;
5819
5820   /* Extract sign, exponent and mantissa.  */
5821   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
5822   r = REAL_VALUE_ABS (r);
5823   exponent = REAL_EXP (&r);
5824   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
5825      highest (sign) bit, with a fixed binary point at bit point_pos.
5826      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
5827      bits for the mantissa, this may fail (low bits would be lost).  */
5828   real_ldexp (&m, &r, point_pos - exponent);
5829   REAL_VALUE_TO_INT (&m1, &m2, m);
5830   mantissa = m1;
5831   mant_hi = m2;
5832
5833   /* If there are bits set in the low part of the mantissa, we can't
5834      represent this value.  */
5835   if (mantissa != 0)
5836     return -1;
5837
5838   /* Now make it so that mantissa contains the most-significant bits, and move
5839      the point_pos to indicate that the least-significant bits have been
5840      discarded.  */
5841   point_pos -= HOST_BITS_PER_WIDE_INT;
5842   mantissa = mant_hi;
5843
5844   /* We can permit four significant bits of mantissa only, plus a high bit
5845      which is always 1.  */
5846   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
5847   if ((mantissa & mask) != 0)
5848     return -1;
5849
5850   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
5851   mantissa >>= point_pos - 5;
5852
5853   /* The mantissa may be zero. Disallow that case. (It's possible to load the
5854      floating-point immediate zero with Neon using an integer-zero load, but
5855      that case is handled elsewhere.)  */
5856   if (mantissa == 0)
5857     return -1;
5858
5859   gcc_assert (mantissa >= 16 && mantissa <= 31);
5860
5861   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
5862      normalized significands are in the range [1, 2). (Our mantissa is shifted
5863      left 4 places at this point relative to normalized IEEE754 values).  GCC
5864      internally uses [0.5, 1) (see real.c), so the exponent returned from
5865      REAL_EXP must be altered.  */
5866   exponent = 5 - exponent;
5867
5868   if (exponent < 0 || exponent > 7)
5869     return -1;
5870
5871   /* Sign, mantissa and exponent are now in the correct form to plug into the
5872      formulae described in the comment above.  */
5873   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
5874 }
5875
5876 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
5877 int
5878 vfp3_const_double_rtx (rtx x)
5879 {
5880   if (!TARGET_VFP3)
5881     return 0;
5882
5883   return vfp3_const_double_index (x) != -1;
5884 }
5885
5886 /* Recognize immediates which can be used in various Neon instructions. Legal
5887    immediates are described by the following table (for VMVN variants, the
5888    bitwise inverse of the constant shown is recognized. In either case, VMOV
5889    is output and the correct instruction to use for a given constant is chosen
5890    by the assembler). The constant shown is replicated across all elements of
5891    the destination vector.
5892
5893    insn elems variant constant (binary)
5894    ---- ----- ------- -----------------
5895    vmov  i32     0    00000000 00000000 00000000 abcdefgh
5896    vmov  i32     1    00000000 00000000 abcdefgh 00000000
5897    vmov  i32     2    00000000 abcdefgh 00000000 00000000
5898    vmov  i32     3    abcdefgh 00000000 00000000 00000000
5899    vmov  i16     4    00000000 abcdefgh
5900    vmov  i16     5    abcdefgh 00000000
5901    vmvn  i32     6    00000000 00000000 00000000 abcdefgh
5902    vmvn  i32     7    00000000 00000000 abcdefgh 00000000
5903    vmvn  i32     8    00000000 abcdefgh 00000000 00000000
5904    vmvn  i32     9    abcdefgh 00000000 00000000 00000000
5905    vmvn  i16    10    00000000 abcdefgh
5906    vmvn  i16    11    abcdefgh 00000000
5907    vmov  i32    12    00000000 00000000 abcdefgh 11111111
5908    vmvn  i32    13    00000000 00000000 abcdefgh 11111111
5909    vmov  i32    14    00000000 abcdefgh 11111111 11111111
5910    vmvn  i32    15    00000000 abcdefgh 11111111 11111111
5911    vmov   i8    16    abcdefgh
5912    vmov  i64    17    aaaaaaaa bbbbbbbb cccccccc dddddddd
5913                       eeeeeeee ffffffff gggggggg hhhhhhhh
5914    vmov  f32    18    aBbbbbbc defgh000 00000000 00000000
5915
5916    For case 18, B = !b. Representable values are exactly those accepted by
5917    vfp3_const_double_index, but are output as floating-point numbers rather
5918    than indices.
5919
5920    Variants 0-5 (inclusive) may also be used as immediates for the second
5921    operand of VORR/VBIC instructions.
5922
5923    The INVERSE argument causes the bitwise inverse of the given operand to be
5924    recognized instead (used for recognizing legal immediates for the VAND/VORN
5925    pseudo-instructions). If INVERSE is true, the value placed in *MODCONST is
5926    *not* inverted (i.e. the pseudo-instruction forms vand/vorn should still be
5927    output, rather than the real insns vbic/vorr).
5928
5929    INVERSE makes no difference to the recognition of float vectors.
5930
5931    The return value is the variant of immediate as shown in the above table, or
5932    -1 if the given value doesn't match any of the listed patterns.
5933 */
5934 static int
5935 neon_valid_immediate (rtx op, enum machine_mode mode, int inverse,
5936                       rtx *modconst, int *elementwidth)
5937 {
5938 #define CHECK(STRIDE, ELSIZE, CLASS, TEST)      \
5939   matches = 1;                                  \
5940   for (i = 0; i < idx; i += (STRIDE))           \
5941     if (!(TEST))                                \
5942       matches = 0;                              \
5943   if (matches)                                  \
5944     {                                           \
5945       immtype = (CLASS);                        \
5946       elsize = (ELSIZE);                        \
5947       break;                                    \
5948     }
5949
5950   unsigned int i, elsize, idx = 0, n_elts = CONST_VECTOR_NUNITS (op);
5951   unsigned int innersize = GET_MODE_SIZE (GET_MODE_INNER (mode));
5952   unsigned char bytes[16];
5953   int immtype = -1, matches;
5954   unsigned int invmask = inverse ? 0xff : 0;
5955
5956   /* Vectors of float constants.  */
5957   if (GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
5958     {
5959       rtx el0 = CONST_VECTOR_ELT (op, 0);
5960       REAL_VALUE_TYPE r0;
5961
5962       if (!vfp3_const_double_rtx (el0))
5963         return -1;
5964
5965       REAL_VALUE_FROM_CONST_DOUBLE (r0, el0);
5966
5967       for (i = 1; i < n_elts; i++)
5968         {
5969           rtx elt = CONST_VECTOR_ELT (op, i);
5970           REAL_VALUE_TYPE re;
5971
5972           REAL_VALUE_FROM_CONST_DOUBLE (re, elt);
5973
5974           if (!REAL_VALUES_EQUAL (r0, re))
5975             return -1;
5976         }
5977
5978       if (modconst)
5979         *modconst = CONST_VECTOR_ELT (op, 0);
5980
5981       if (elementwidth)
5982         *elementwidth = 0;
5983
5984       return 18;
5985     }
5986
5987   /* Splat vector constant out into a byte vector.  */
5988   for (i = 0; i < n_elts; i++)
5989     {
5990       rtx el = CONST_VECTOR_ELT (op, i);
5991       unsigned HOST_WIDE_INT elpart;
5992       unsigned int part, parts;
5993
5994       if (GET_CODE (el) == CONST_INT)
5995         {
5996           elpart = INTVAL (el);
5997           parts = 1;
5998         }
5999       else if (GET_CODE (el) == CONST_DOUBLE)
6000         {
6001           elpart = CONST_DOUBLE_LOW (el);
6002           parts = 2;
6003         }
6004       else
6005         gcc_unreachable ();
6006
6007       for (part = 0; part < parts; part++)
6008         {
6009           unsigned int byte;
6010           for (byte = 0; byte < innersize; byte++)
6011             {
6012               bytes[idx++] = (elpart & 0xff) ^ invmask;
6013               elpart >>= BITS_PER_UNIT;
6014             }
6015           if (GET_CODE (el) == CONST_DOUBLE)
6016             elpart = CONST_DOUBLE_HIGH (el);
6017         }
6018     }
6019
6020   /* Sanity check.  */
6021   gcc_assert (idx == GET_MODE_SIZE (mode));
6022
6023   do
6024     {
6025       CHECK (4, 32, 0, bytes[i] == bytes[0] && bytes[i + 1] == 0
6026                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6027
6028       CHECK (4, 32, 1, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6029                        && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6030
6031       CHECK (4, 32, 2, bytes[i] == 0 && bytes[i + 1] == 0
6032                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6033
6034       CHECK (4, 32, 3, bytes[i] == 0 && bytes[i + 1] == 0
6035                        && bytes[i + 2] == 0 && bytes[i + 3] == bytes[3]);
6036
6037       CHECK (2, 16, 4, bytes[i] == bytes[0] && bytes[i + 1] == 0);
6038
6039       CHECK (2, 16, 5, bytes[i] == 0 && bytes[i + 1] == bytes[1]);
6040
6041       CHECK (4, 32, 6, bytes[i] == bytes[0] && bytes[i + 1] == 0xff
6042                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6043
6044       CHECK (4, 32, 7, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6045                        && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6046
6047       CHECK (4, 32, 8, bytes[i] == 0xff && bytes[i + 1] == 0xff
6048                        && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6049
6050       CHECK (4, 32, 9, bytes[i] == 0xff && bytes[i + 1] == 0xff
6051                        && bytes[i + 2] == 0xff && bytes[i + 3] == bytes[3]);
6052
6053       CHECK (2, 16, 10, bytes[i] == bytes[0] && bytes[i + 1] == 0xff);
6054
6055       CHECK (2, 16, 11, bytes[i] == 0xff && bytes[i + 1] == bytes[1]);
6056
6057       CHECK (4, 32, 12, bytes[i] == 0xff && bytes[i + 1] == bytes[1]
6058                         && bytes[i + 2] == 0 && bytes[i + 3] == 0);
6059
6060       CHECK (4, 32, 13, bytes[i] == 0 && bytes[i + 1] == bytes[1]
6061                         && bytes[i + 2] == 0xff && bytes[i + 3] == 0xff);
6062
6063       CHECK (4, 32, 14, bytes[i] == 0xff && bytes[i + 1] == 0xff
6064                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0);
6065
6066       CHECK (4, 32, 15, bytes[i] == 0 && bytes[i + 1] == 0
6067                         && bytes[i + 2] == bytes[2] && bytes[i + 3] == 0xff);
6068
6069       CHECK (1, 8, 16, bytes[i] == bytes[0]);
6070
6071       CHECK (1, 64, 17, (bytes[i] == 0 || bytes[i] == 0xff)
6072                         && bytes[i] == bytes[(i + 8) % idx]);
6073     }
6074   while (0);
6075
6076   if (immtype == -1)
6077     return -1;
6078
6079   if (elementwidth)
6080     *elementwidth = elsize;
6081
6082   if (modconst)
6083     {
6084       unsigned HOST_WIDE_INT imm = 0;
6085
6086       /* Un-invert bytes of recognized vector, if neccessary.  */
6087       if (invmask != 0)
6088         for (i = 0; i < idx; i++)
6089           bytes[i] ^= invmask;
6090
6091       if (immtype == 17)
6092         {
6093           /* FIXME: Broken on 32-bit H_W_I hosts.  */
6094           gcc_assert (sizeof (HOST_WIDE_INT) == 8);
6095
6096           for (i = 0; i < 8; i++)
6097             imm |= (unsigned HOST_WIDE_INT) (bytes[i] ? 0xff : 0)
6098                    << (i * BITS_PER_UNIT);
6099
6100           *modconst = GEN_INT (imm);
6101         }
6102       else
6103         {
6104           unsigned HOST_WIDE_INT imm = 0;
6105
6106           for (i = 0; i < elsize / BITS_PER_UNIT; i++)
6107             imm |= (unsigned HOST_WIDE_INT) bytes[i] << (i * BITS_PER_UNIT);
6108
6109           *modconst = GEN_INT (imm);
6110         }
6111     }
6112
6113   return immtype;
6114 #undef CHECK
6115 }
6116
6117 /* Return TRUE if rtx X is legal for use as either a Neon VMOV (or, implicitly,
6118    VMVN) immediate. Write back width per element to *ELEMENTWIDTH (or zero for
6119    float elements), and a modified constant (whatever should be output for a
6120    VMOV) in *MODCONST.  */
6121
6122 int
6123 neon_immediate_valid_for_move (rtx op, enum machine_mode mode,
6124                                rtx *modconst, int *elementwidth)
6125 {
6126   rtx tmpconst;
6127   int tmpwidth;
6128   int retval = neon_valid_immediate (op, mode, 0, &tmpconst, &tmpwidth);
6129
6130   if (retval == -1)
6131     return 0;
6132
6133   if (modconst)
6134     *modconst = tmpconst;
6135
6136   if (elementwidth)
6137     *elementwidth = tmpwidth;
6138
6139   return 1;
6140 }
6141
6142 /* Return TRUE if rtx X is legal for use in a VORR or VBIC instruction.  If
6143    the immediate is valid, write a constant suitable for using as an operand
6144    to VORR/VBIC/VAND/VORN to *MODCONST and the corresponding element width to
6145    *ELEMENTWIDTH. See neon_valid_immediate for description of INVERSE.  */
6146
6147 int
6148 neon_immediate_valid_for_logic (rtx op, enum machine_mode mode, int inverse,
6149                                 rtx *modconst, int *elementwidth)
6150 {
6151   rtx tmpconst;
6152   int tmpwidth;
6153   int retval = neon_valid_immediate (op, mode, inverse, &tmpconst, &tmpwidth);
6154
6155   if (retval < 0 || retval > 5)
6156     return 0;
6157
6158   if (modconst)
6159     *modconst = tmpconst;
6160
6161   if (elementwidth)
6162     *elementwidth = tmpwidth;
6163
6164   return 1;
6165 }
6166
6167 /* Return a string suitable for output of Neon immediate logic operation
6168    MNEM.  */
6169
6170 char *
6171 neon_output_logic_immediate (const char *mnem, rtx *op2, enum machine_mode mode,
6172                              int inverse, int quad)
6173 {
6174   int width, is_valid;
6175   static char templ[40];
6176
6177   is_valid = neon_immediate_valid_for_logic (*op2, mode, inverse, op2, &width);
6178
6179   gcc_assert (is_valid != 0);
6180
6181   if (quad)
6182     sprintf (templ, "%s.i%d\t%%q0, %%2", mnem, width);
6183   else
6184     sprintf (templ, "%s.i%d\t%%P0, %%2", mnem, width);
6185
6186   return templ;
6187 }
6188
6189 /* Output a sequence of pairwise operations to implement a reduction.
6190    NOTE: We do "too much work" here, because pairwise operations work on two
6191    registers-worth of operands in one go. Unfortunately we can't exploit those
6192    extra calculations to do the full operation in fewer steps, I don't think.
6193    Although all vector elements of the result but the first are ignored, we
6194    actually calculate the same result in each of the elements. An alternative
6195    such as initially loading a vector with zero to use as each of the second
6196    operands would use up an additional register and take an extra instruction,
6197    for no particular gain.  */
6198
6199 void
6200 neon_pairwise_reduce (rtx op0, rtx op1, enum machine_mode mode,
6201                       rtx (*reduc) (rtx, rtx, rtx))
6202 {
6203   enum machine_mode inner = GET_MODE_INNER (mode);
6204   unsigned int i, parts = GET_MODE_SIZE (mode) / GET_MODE_SIZE (inner);
6205   rtx tmpsum = op1;
6206
6207   for (i = parts / 2; i >= 1; i /= 2)
6208     {
6209       rtx dest = (i == 1) ? op0 : gen_reg_rtx (mode);
6210       emit_insn (reduc (dest, tmpsum, tmpsum));
6211       tmpsum = dest;
6212     }
6213 }
6214
6215 /* Initialise a vector with non-constant elements.  FIXME: We can do better
6216    than the current implementation (building a vector on the stack and then
6217    loading it) in many cases.  See rs6000.c.  */
6218
6219 void
6220 neon_expand_vector_init (rtx target, rtx vals)
6221 {
6222   enum machine_mode mode = GET_MODE (target);
6223   enum machine_mode inner = GET_MODE_INNER (mode);
6224   unsigned int i, n_elts = GET_MODE_NUNITS (mode);
6225   rtx mem;
6226
6227   gcc_assert (VECTOR_MODE_P (mode));
6228
6229   mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), 0);
6230   for (i = 0; i < n_elts; i++)
6231     emit_move_insn (adjust_address_nv (mem, inner, i * GET_MODE_SIZE (inner)),
6232                    XVECEXP (vals, 0, i));
6233
6234   emit_move_insn (target, mem);
6235 }
6236
6237 /* Ensure OPERAND lies between LOW (inclusive) and HIGH (exclusive).  Raise
6238    ERR if it doesn't.  FIXME: NEON bounds checks occur late in compilation, so
6239    reported source locations are bogus.  */
6240
6241 static void
6242 bounds_check (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high,
6243               const char *err)
6244 {
6245   HOST_WIDE_INT lane;
6246
6247   gcc_assert (GET_CODE (operand) == CONST_INT);
6248
6249   lane = INTVAL (operand);
6250
6251   if (lane < low || lane >= high)
6252     error (err);
6253 }
6254
6255 /* Bounds-check lanes.  */
6256
6257 void
6258 neon_lane_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6259 {
6260   bounds_check (operand, low, high, "lane out of range");
6261 }
6262
6263 /* Bounds-check constants.  */
6264
6265 void
6266 neon_const_bounds (rtx operand, HOST_WIDE_INT low, HOST_WIDE_INT high)
6267 {
6268   bounds_check (operand, low, high, "constant out of range");
6269 }
6270
6271 HOST_WIDE_INT
6272 neon_element_bits (enum machine_mode mode)
6273 {
6274   if (mode == DImode)
6275     return GET_MODE_BITSIZE (mode);
6276   else
6277     return GET_MODE_BITSIZE (GET_MODE_INNER (mode));
6278 }
6279
6280 \f
6281 /* Predicates for `match_operand' and `match_operator'.  */
6282
6283 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
6284 int
6285 cirrus_memory_offset (rtx op)
6286 {
6287   /* Reject eliminable registers.  */
6288   if (! (reload_in_progress || reload_completed)
6289       && (   reg_mentioned_p (frame_pointer_rtx, op)
6290           || reg_mentioned_p (arg_pointer_rtx, op)
6291           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6292           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6293           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6294           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6295     return 0;
6296
6297   if (GET_CODE (op) == MEM)
6298     {
6299       rtx ind;
6300
6301       ind = XEXP (op, 0);
6302
6303       /* Match: (mem (reg)).  */
6304       if (GET_CODE (ind) == REG)
6305         return 1;
6306
6307       /* Match:
6308          (mem (plus (reg)
6309                     (const))).  */
6310       if (GET_CODE (ind) == PLUS
6311           && GET_CODE (XEXP (ind, 0)) == REG
6312           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6313           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
6314         return 1;
6315     }
6316
6317   return 0;
6318 }
6319
6320 /* Return TRUE if OP is a valid coprocessor memory address pattern.
6321    WB is true if full writeback address modes are allowed and is false
6322    if limited writeback address modes (POST_INC and PRE_DEC) are
6323    allowed.  */
6324
6325 int
6326 arm_coproc_mem_operand (rtx op, bool wb)
6327 {
6328   rtx ind;
6329
6330   /* Reject eliminable registers.  */
6331   if (! (reload_in_progress || reload_completed)
6332       && (   reg_mentioned_p (frame_pointer_rtx, op)
6333           || reg_mentioned_p (arg_pointer_rtx, op)
6334           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6335           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6336           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6337           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6338     return FALSE;
6339
6340   /* Constants are converted into offsets from labels.  */
6341   if (GET_CODE (op) != MEM)
6342     return FALSE;
6343
6344   ind = XEXP (op, 0);
6345
6346   if (reload_completed
6347       && (GET_CODE (ind) == LABEL_REF
6348           || (GET_CODE (ind) == CONST
6349               && GET_CODE (XEXP (ind, 0)) == PLUS
6350               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6351               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6352     return TRUE;
6353
6354   /* Match: (mem (reg)).  */
6355   if (GET_CODE (ind) == REG)
6356     return arm_address_register_rtx_p (ind, 0);
6357
6358   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
6359      acceptable in any case (subject to verification by
6360      arm_address_register_rtx_p).  We need WB to be true to accept
6361      PRE_INC and POST_DEC.  */
6362   if (GET_CODE (ind) == POST_INC
6363       || GET_CODE (ind) == PRE_DEC
6364       || (wb
6365           && (GET_CODE (ind) == PRE_INC
6366               || GET_CODE (ind) == POST_DEC)))
6367     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6368
6369   if (wb
6370       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
6371       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6372       && GET_CODE (XEXP (ind, 1)) == PLUS
6373       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6374     ind = XEXP (ind, 1);
6375
6376   /* Match:
6377      (plus (reg)
6378            (const)).  */
6379   if (GET_CODE (ind) == PLUS
6380       && GET_CODE (XEXP (ind, 0)) == REG
6381       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6382       && GET_CODE (XEXP (ind, 1)) == CONST_INT
6383       && INTVAL (XEXP (ind, 1)) > -1024
6384       && INTVAL (XEXP (ind, 1)) <  1024
6385       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6386     return TRUE;
6387
6388   return FALSE;
6389 }
6390
6391 /* Return TRUE if OP is a memory operand which we can load or store a vector
6392    to/from. If CORE is true, we're moving from ARM registers not Neon
6393    registers.  */
6394 int
6395 neon_vector_mem_operand (rtx op, bool core)
6396 {
6397   rtx ind;
6398
6399   /* Reject eliminable registers.  */
6400   if (! (reload_in_progress || reload_completed)
6401       && (   reg_mentioned_p (frame_pointer_rtx, op)
6402           || reg_mentioned_p (arg_pointer_rtx, op)
6403           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6404           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6405           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6406           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6407     return FALSE;
6408
6409   /* Constants are converted into offsets from labels.  */
6410   if (GET_CODE (op) != MEM)
6411     return FALSE;
6412
6413   ind = XEXP (op, 0);
6414
6415   if (reload_completed
6416       && (GET_CODE (ind) == LABEL_REF
6417           || (GET_CODE (ind) == CONST
6418               && GET_CODE (XEXP (ind, 0)) == PLUS
6419               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6420               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6421     return TRUE;
6422
6423   /* Match: (mem (reg)).  */
6424   if (GET_CODE (ind) == REG)
6425     return arm_address_register_rtx_p (ind, 0);
6426
6427   /* Allow post-increment with Neon registers.  */
6428   if (!core && GET_CODE (ind) == POST_INC)
6429     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
6430
6431 #if 0
6432   /* FIXME: We can support this too if we use VLD1/VST1.  */
6433   if (!core
6434       && GET_CODE (ind) == POST_MODIFY
6435       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
6436       && GET_CODE (XEXP (ind, 1)) == PLUS
6437       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
6438     ind = XEXP (ind, 1);
6439 #endif
6440
6441   /* Match:
6442      (plus (reg)
6443           (const)).  */
6444   if (!core
6445       && GET_CODE (ind) == PLUS
6446       && GET_CODE (XEXP (ind, 0)) == REG
6447       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
6448       && GET_CODE (XEXP (ind, 1)) == CONST_INT
6449       && INTVAL (XEXP (ind, 1)) > -1024
6450       && INTVAL (XEXP (ind, 1)) < 1016
6451       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
6452     return TRUE;
6453
6454   return FALSE;
6455 }
6456
6457 /* Return TRUE if OP is a mem suitable for loading/storing a Neon struct
6458    type.  */
6459 int
6460 neon_struct_mem_operand (rtx op)
6461 {
6462   rtx ind;
6463
6464   /* Reject eliminable registers.  */
6465   if (! (reload_in_progress || reload_completed)
6466       && (   reg_mentioned_p (frame_pointer_rtx, op)
6467           || reg_mentioned_p (arg_pointer_rtx, op)
6468           || reg_mentioned_p (virtual_incoming_args_rtx, op)
6469           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
6470           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
6471           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
6472     return FALSE;
6473
6474   /* Constants are converted into offsets from labels.  */
6475   if (GET_CODE (op) != MEM)
6476     return FALSE;
6477
6478   ind = XEXP (op, 0);
6479
6480   if (reload_completed
6481       && (GET_CODE (ind) == LABEL_REF
6482           || (GET_CODE (ind) == CONST
6483               && GET_CODE (XEXP (ind, 0)) == PLUS
6484               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
6485               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
6486     return TRUE;
6487
6488   /* Match: (mem (reg)).  */
6489   if (GET_CODE (ind) == REG)
6490     return arm_address_register_rtx_p (ind, 0);
6491
6492   return FALSE;
6493 }
6494
6495 /* Return true if X is a register that will be eliminated later on.  */
6496 int
6497 arm_eliminable_register (rtx x)
6498 {
6499   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
6500                        || REGNO (x) == ARG_POINTER_REGNUM
6501                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
6502                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
6503 }
6504
6505 /* Return GENERAL_REGS if a scratch register required to reload x to/from
6506    coprocessor registers.  Otherwise return NO_REGS.  */
6507
6508 enum reg_class
6509 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
6510 {
6511   if (TARGET_NEON
6512       && (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
6513           || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6514       && neon_vector_mem_operand (x, FALSE))
6515      return NO_REGS;
6516
6517   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
6518     return NO_REGS;
6519
6520   return GENERAL_REGS;
6521 }
6522
6523 /* Values which must be returned in the most-significant end of the return
6524    register.  */
6525
6526 static bool
6527 arm_return_in_msb (tree valtype)
6528 {
6529   return (TARGET_AAPCS_BASED
6530           && BYTES_BIG_ENDIAN
6531           && (AGGREGATE_TYPE_P (valtype)
6532               || TREE_CODE (valtype) == COMPLEX_TYPE));
6533 }
6534
6535 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
6536    Use by the Cirrus Maverick code which has to workaround
6537    a hardware bug triggered by such instructions.  */
6538 static bool
6539 arm_memory_load_p (rtx insn)
6540 {
6541   rtx body, lhs, rhs;;
6542
6543   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
6544     return false;
6545
6546   body = PATTERN (insn);
6547
6548   if (GET_CODE (body) != SET)
6549     return false;
6550
6551   lhs = XEXP (body, 0);
6552   rhs = XEXP (body, 1);
6553
6554   lhs = REG_OR_SUBREG_RTX (lhs);
6555
6556   /* If the destination is not a general purpose
6557      register we do not have to worry.  */
6558   if (GET_CODE (lhs) != REG
6559       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
6560     return false;
6561
6562   /* As well as loads from memory we also have to react
6563      to loads of invalid constants which will be turned
6564      into loads from the minipool.  */
6565   return (GET_CODE (rhs) == MEM
6566           || GET_CODE (rhs) == SYMBOL_REF
6567           || note_invalid_constants (insn, -1, false));
6568 }
6569
6570 /* Return TRUE if INSN is a Cirrus instruction.  */
6571 static bool
6572 arm_cirrus_insn_p (rtx insn)
6573 {
6574   enum attr_cirrus attr;
6575
6576   /* get_attr cannot accept USE or CLOBBER.  */
6577   if (!insn
6578       || GET_CODE (insn) != INSN
6579       || GET_CODE (PATTERN (insn)) == USE
6580       || GET_CODE (PATTERN (insn)) == CLOBBER)
6581     return 0;
6582
6583   attr = get_attr_cirrus (insn);
6584
6585   return attr != CIRRUS_NOT;
6586 }
6587
6588 /* Cirrus reorg for invalid instruction combinations.  */
6589 static void
6590 cirrus_reorg (rtx first)
6591 {
6592   enum attr_cirrus attr;
6593   rtx body = PATTERN (first);
6594   rtx t;
6595   int nops;
6596
6597   /* Any branch must be followed by 2 non Cirrus instructions.  */
6598   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
6599     {
6600       nops = 0;
6601       t = next_nonnote_insn (first);
6602
6603       if (arm_cirrus_insn_p (t))
6604         ++ nops;
6605
6606       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
6607         ++ nops;
6608
6609       while (nops --)
6610         emit_insn_after (gen_nop (), first);
6611
6612       return;
6613     }
6614
6615   /* (float (blah)) is in parallel with a clobber.  */
6616   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
6617     body = XVECEXP (body, 0, 0);
6618
6619   if (GET_CODE (body) == SET)
6620     {
6621       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
6622
6623       /* cfldrd, cfldr64, cfstrd, cfstr64 must
6624          be followed by a non Cirrus insn.  */
6625       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
6626         {
6627           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
6628             emit_insn_after (gen_nop (), first);
6629
6630           return;
6631         }
6632       else if (arm_memory_load_p (first))
6633         {
6634           unsigned int arm_regno;
6635
6636           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
6637              ldr/cfmv64hr combination where the Rd field is the same
6638              in both instructions must be split with a non Cirrus
6639              insn.  Example:
6640
6641              ldr r0, blah
6642              nop
6643              cfmvsr mvf0, r0.  */
6644
6645           /* Get Arm register number for ldr insn.  */
6646           if (GET_CODE (lhs) == REG)
6647             arm_regno = REGNO (lhs);
6648           else
6649             {
6650               gcc_assert (GET_CODE (rhs) == REG);
6651               arm_regno = REGNO (rhs);
6652             }
6653
6654           /* Next insn.  */
6655           first = next_nonnote_insn (first);
6656
6657           if (! arm_cirrus_insn_p (first))
6658             return;
6659
6660           body = PATTERN (first);
6661
6662           /* (float (blah)) is in parallel with a clobber.  */
6663           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
6664             body = XVECEXP (body, 0, 0);
6665
6666           if (GET_CODE (body) == FLOAT)
6667             body = XEXP (body, 0);
6668
6669           if (get_attr_cirrus (first) == CIRRUS_MOVE
6670               && GET_CODE (XEXP (body, 1)) == REG
6671               && arm_regno == REGNO (XEXP (body, 1)))
6672             emit_insn_after (gen_nop (), first);
6673
6674           return;
6675         }
6676     }
6677
6678   /* get_attr cannot accept USE or CLOBBER.  */
6679   if (!first
6680       || GET_CODE (first) != INSN
6681       || GET_CODE (PATTERN (first)) == USE
6682       || GET_CODE (PATTERN (first)) == CLOBBER)
6683     return;
6684
6685   attr = get_attr_cirrus (first);
6686
6687   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
6688      must be followed by a non-coprocessor instruction.  */
6689   if (attr == CIRRUS_COMPARE)
6690     {
6691       nops = 0;
6692
6693       t = next_nonnote_insn (first);
6694
6695       if (arm_cirrus_insn_p (t))
6696         ++ nops;
6697
6698       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
6699         ++ nops;
6700
6701       while (nops --)
6702         emit_insn_after (gen_nop (), first);
6703
6704       return;
6705     }
6706 }
6707
6708 /* Return TRUE if X references a SYMBOL_REF.  */
6709 int
6710 symbol_mentioned_p (rtx x)
6711 {
6712   const char * fmt;
6713   int i;
6714
6715   if (GET_CODE (x) == SYMBOL_REF)
6716     return 1;
6717
6718   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
6719      are constant offsets, not symbols.  */
6720   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6721     return 0;
6722
6723   fmt = GET_RTX_FORMAT (GET_CODE (x));
6724
6725   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6726     {
6727       if (fmt[i] == 'E')
6728         {
6729           int j;
6730
6731           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6732             if (symbol_mentioned_p (XVECEXP (x, i, j)))
6733               return 1;
6734         }
6735       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
6736         return 1;
6737     }
6738
6739   return 0;
6740 }
6741
6742 /* Return TRUE if X references a LABEL_REF.  */
6743 int
6744 label_mentioned_p (rtx x)
6745 {
6746   const char * fmt;
6747   int i;
6748
6749   if (GET_CODE (x) == LABEL_REF)
6750     return 1;
6751
6752   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
6753      instruction, but they are constant offsets, not symbols.  */
6754   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6755     return 0;
6756
6757   fmt = GET_RTX_FORMAT (GET_CODE (x));
6758   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6759     {
6760       if (fmt[i] == 'E')
6761         {
6762           int j;
6763
6764           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6765             if (label_mentioned_p (XVECEXP (x, i, j)))
6766               return 1;
6767         }
6768       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
6769         return 1;
6770     }
6771
6772   return 0;
6773 }
6774
6775 int
6776 tls_mentioned_p (rtx x)
6777 {
6778   switch (GET_CODE (x))
6779     {
6780     case CONST:
6781       return tls_mentioned_p (XEXP (x, 0));
6782
6783     case UNSPEC:
6784       if (XINT (x, 1) == UNSPEC_TLS)
6785         return 1;
6786
6787     default:
6788       return 0;
6789     }
6790 }
6791
6792 /* Must not copy a SET whose source operand is PC-relative.  */
6793
6794 static bool
6795 arm_cannot_copy_insn_p (rtx insn)
6796 {
6797   rtx pat = PATTERN (insn);
6798
6799   if (GET_CODE (pat) == SET)
6800     {
6801       rtx rhs = SET_SRC (pat);
6802
6803       if (GET_CODE (rhs) == UNSPEC
6804           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
6805         return TRUE;
6806
6807       if (GET_CODE (rhs) == MEM
6808           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
6809           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
6810         return TRUE;
6811     }
6812
6813   return FALSE;
6814 }
6815
6816 enum rtx_code
6817 minmax_code (rtx x)
6818 {
6819   enum rtx_code code = GET_CODE (x);
6820
6821   switch (code)
6822     {
6823     case SMAX:
6824       return GE;
6825     case SMIN:
6826       return LE;
6827     case UMIN:
6828       return LEU;
6829     case UMAX:
6830       return GEU;
6831     default:
6832       gcc_unreachable ();
6833     }
6834 }
6835
6836 /* Return 1 if memory locations are adjacent.  */
6837 int
6838 adjacent_mem_locations (rtx a, rtx b)
6839 {
6840   /* We don't guarantee to preserve the order of these memory refs.  */
6841   if (volatile_refs_p (a) || volatile_refs_p (b))
6842     return 0;
6843
6844   if ((GET_CODE (XEXP (a, 0)) == REG
6845        || (GET_CODE (XEXP (a, 0)) == PLUS
6846            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
6847       && (GET_CODE (XEXP (b, 0)) == REG
6848           || (GET_CODE (XEXP (b, 0)) == PLUS
6849               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
6850     {
6851       HOST_WIDE_INT val0 = 0, val1 = 0;
6852       rtx reg0, reg1;
6853       int val_diff;
6854
6855       if (GET_CODE (XEXP (a, 0)) == PLUS)
6856         {
6857           reg0 = XEXP (XEXP (a, 0), 0);
6858           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
6859         }
6860       else
6861         reg0 = XEXP (a, 0);
6862
6863       if (GET_CODE (XEXP (b, 0)) == PLUS)
6864         {
6865           reg1 = XEXP (XEXP (b, 0), 0);
6866           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
6867         }
6868       else
6869         reg1 = XEXP (b, 0);
6870
6871       /* Don't accept any offset that will require multiple
6872          instructions to handle, since this would cause the
6873          arith_adjacentmem pattern to output an overlong sequence.  */
6874       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
6875         return 0;
6876
6877       /* Don't allow an eliminable register: register elimination can make
6878          the offset too large.  */
6879       if (arm_eliminable_register (reg0))
6880         return 0;
6881
6882       val_diff = val1 - val0;
6883
6884       if (arm_ld_sched)
6885         {
6886           /* If the target has load delay slots, then there's no benefit
6887              to using an ldm instruction unless the offset is zero and
6888              we are optimizing for size.  */
6889           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
6890                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
6891                   && (val_diff == 4 || val_diff == -4));
6892         }
6893
6894       return ((REGNO (reg0) == REGNO (reg1))
6895               && (val_diff == 4 || val_diff == -4));
6896     }
6897
6898   return 0;
6899 }
6900
6901 int
6902 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6903                         HOST_WIDE_INT *load_offset)
6904 {
6905   int unsorted_regs[4];
6906   HOST_WIDE_INT unsorted_offsets[4];
6907   int order[4];
6908   int base_reg = -1;
6909   int i;
6910
6911   /* Can only handle 2, 3, or 4 insns at present,
6912      though could be easily extended if required.  */
6913   gcc_assert (nops >= 2 && nops <= 4);
6914
6915   /* Loop over the operands and check that the memory references are
6916      suitable (i.e. immediate offsets from the same base register).  At
6917      the same time, extract the target register, and the memory
6918      offsets.  */
6919   for (i = 0; i < nops; i++)
6920     {
6921       rtx reg;
6922       rtx offset;
6923
6924       /* Convert a subreg of a mem into the mem itself.  */
6925       if (GET_CODE (operands[nops + i]) == SUBREG)
6926         operands[nops + i] = alter_subreg (operands + (nops + i));
6927
6928       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6929
6930       /* Don't reorder volatile memory references; it doesn't seem worth
6931          looking for the case where the order is ok anyway.  */
6932       if (MEM_VOLATILE_P (operands[nops + i]))
6933         return 0;
6934
6935       offset = const0_rtx;
6936
6937       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6938            || (GET_CODE (reg) == SUBREG
6939                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6940           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6941               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6942                    == REG)
6943                   || (GET_CODE (reg) == SUBREG
6944                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6945               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6946                   == CONST_INT)))
6947         {
6948           if (i == 0)
6949             {
6950               base_reg = REGNO (reg);
6951               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6952                                   ? REGNO (operands[i])
6953                                   : REGNO (SUBREG_REG (operands[i])));
6954               order[0] = 0;
6955             }
6956           else
6957             {
6958               if (base_reg != (int) REGNO (reg))
6959                 /* Not addressed from the same base register.  */
6960                 return 0;
6961
6962               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6963                                   ? REGNO (operands[i])
6964                                   : REGNO (SUBREG_REG (operands[i])));
6965               if (unsorted_regs[i] < unsorted_regs[order[0]])
6966                 order[0] = i;
6967             }
6968
6969           /* If it isn't an integer register, or if it overwrites the
6970              base register but isn't the last insn in the list, then
6971              we can't do this.  */
6972           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
6973               || (i != nops - 1 && unsorted_regs[i] == base_reg))
6974             return 0;
6975
6976           unsorted_offsets[i] = INTVAL (offset);
6977         }
6978       else
6979         /* Not a suitable memory address.  */
6980         return 0;
6981     }
6982
6983   /* All the useful information has now been extracted from the
6984      operands into unsorted_regs and unsorted_offsets; additionally,
6985      order[0] has been set to the lowest numbered register in the
6986      list.  Sort the registers into order, and check that the memory
6987      offsets are ascending and adjacent.  */
6988
6989   for (i = 1; i < nops; i++)
6990     {
6991       int j;
6992
6993       order[i] = order[i - 1];
6994       for (j = 0; j < nops; j++)
6995         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6996             && (order[i] == order[i - 1]
6997                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6998           order[i] = j;
6999
7000       /* Have we found a suitable register? if not, one must be used more
7001          than once.  */
7002       if (order[i] == order[i - 1])
7003         return 0;
7004
7005       /* Is the memory address adjacent and ascending? */
7006       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7007         return 0;
7008     }
7009
7010   if (base)
7011     {
7012       *base = base_reg;
7013
7014       for (i = 0; i < nops; i++)
7015         regs[i] = unsorted_regs[order[i]];
7016
7017       *load_offset = unsorted_offsets[order[0]];
7018     }
7019
7020   if (unsorted_offsets[order[0]] == 0)
7021     return 1; /* ldmia */
7022
7023   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
7024     return 2; /* ldmib */
7025
7026   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
7027     return 3; /* ldmda */
7028
7029   if (unsorted_offsets[order[nops - 1]] == -4)
7030     return 4; /* ldmdb */
7031
7032   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
7033      if the offset isn't small enough.  The reason 2 ldrs are faster
7034      is because these ARMs are able to do more than one cache access
7035      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
7036      whilst the ARM8 has a double bandwidth cache.  This means that
7037      these cores can do both an instruction fetch and a data fetch in
7038      a single cycle, so the trick of calculating the address into a
7039      scratch register (one of the result regs) and then doing a load
7040      multiple actually becomes slower (and no smaller in code size).
7041      That is the transformation
7042
7043         ldr     rd1, [rbase + offset]
7044         ldr     rd2, [rbase + offset + 4]
7045
7046      to
7047
7048         add     rd1, rbase, offset
7049         ldmia   rd1, {rd1, rd2}
7050
7051      produces worse code -- '3 cycles + any stalls on rd2' instead of
7052      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
7053      access per cycle, the first sequence could never complete in less
7054      than 6 cycles, whereas the ldm sequence would only take 5 and
7055      would make better use of sequential accesses if not hitting the
7056      cache.
7057
7058      We cheat here and test 'arm_ld_sched' which we currently know to
7059      only be true for the ARM8, ARM9 and StrongARM.  If this ever
7060      changes, then the test below needs to be reworked.  */
7061   if (nops == 2 && arm_ld_sched)
7062     return 0;
7063
7064   /* Can't do it without setting up the offset, only do this if it takes
7065      no more than one insn.  */
7066   return (const_ok_for_arm (unsorted_offsets[order[0]])
7067           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
7068 }
7069
7070 const char *
7071 emit_ldm_seq (rtx *operands, int nops)
7072 {
7073   int regs[4];
7074   int base_reg;
7075   HOST_WIDE_INT offset;
7076   char buf[100];
7077   int i;
7078
7079   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7080     {
7081     case 1:
7082       strcpy (buf, "ldm%(ia%)\t");
7083       break;
7084
7085     case 2:
7086       strcpy (buf, "ldm%(ib%)\t");
7087       break;
7088
7089     case 3:
7090       strcpy (buf, "ldm%(da%)\t");
7091       break;
7092
7093     case 4:
7094       strcpy (buf, "ldm%(db%)\t");
7095       break;
7096
7097     case 5:
7098       if (offset >= 0)
7099         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7100                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7101                  (long) offset);
7102       else
7103         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
7104                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
7105                  (long) -offset);
7106       output_asm_insn (buf, operands);
7107       base_reg = regs[0];
7108       strcpy (buf, "ldm%(ia%)\t");
7109       break;
7110
7111     default:
7112       gcc_unreachable ();
7113     }
7114
7115   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7116            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7117
7118   for (i = 1; i < nops; i++)
7119     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7120              reg_names[regs[i]]);
7121
7122   strcat (buf, "}\t%@ phole ldm");
7123
7124   output_asm_insn (buf, operands);
7125   return "";
7126 }
7127
7128 int
7129 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
7130                          HOST_WIDE_INT * load_offset)
7131 {
7132   int unsorted_regs[4];
7133   HOST_WIDE_INT unsorted_offsets[4];
7134   int order[4];
7135   int base_reg = -1;
7136   int i;
7137
7138   /* Can only handle 2, 3, or 4 insns at present, though could be easily
7139      extended if required.  */
7140   gcc_assert (nops >= 2 && nops <= 4);
7141
7142   /* Loop over the operands and check that the memory references are
7143      suitable (i.e. immediate offsets from the same base register).  At
7144      the same time, extract the target register, and the memory
7145      offsets.  */
7146   for (i = 0; i < nops; i++)
7147     {
7148       rtx reg;
7149       rtx offset;
7150
7151       /* Convert a subreg of a mem into the mem itself.  */
7152       if (GET_CODE (operands[nops + i]) == SUBREG)
7153         operands[nops + i] = alter_subreg (operands + (nops + i));
7154
7155       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
7156
7157       /* Don't reorder volatile memory references; it doesn't seem worth
7158          looking for the case where the order is ok anyway.  */
7159       if (MEM_VOLATILE_P (operands[nops + i]))
7160         return 0;
7161
7162       offset = const0_rtx;
7163
7164       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
7165            || (GET_CODE (reg) == SUBREG
7166                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7167           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
7168               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
7169                    == REG)
7170                   || (GET_CODE (reg) == SUBREG
7171                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
7172               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
7173                   == CONST_INT)))
7174         {
7175           if (i == 0)
7176             {
7177               base_reg = REGNO (reg);
7178               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
7179                                   ? REGNO (operands[i])
7180                                   : REGNO (SUBREG_REG (operands[i])));
7181               order[0] = 0;
7182             }
7183           else
7184             {
7185               if (base_reg != (int) REGNO (reg))
7186                 /* Not addressed from the same base register.  */
7187                 return 0;
7188
7189               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
7190                                   ? REGNO (operands[i])
7191                                   : REGNO (SUBREG_REG (operands[i])));
7192               if (unsorted_regs[i] < unsorted_regs[order[0]])
7193                 order[0] = i;
7194             }
7195
7196           /* If it isn't an integer register, then we can't do this.  */
7197           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
7198             return 0;
7199
7200           unsorted_offsets[i] = INTVAL (offset);
7201         }
7202       else
7203         /* Not a suitable memory address.  */
7204         return 0;
7205     }
7206
7207   /* All the useful information has now been extracted from the
7208      operands into unsorted_regs and unsorted_offsets; additionally,
7209      order[0] has been set to the lowest numbered register in the
7210      list.  Sort the registers into order, and check that the memory
7211      offsets are ascending and adjacent.  */
7212
7213   for (i = 1; i < nops; i++)
7214     {
7215       int j;
7216
7217       order[i] = order[i - 1];
7218       for (j = 0; j < nops; j++)
7219         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
7220             && (order[i] == order[i - 1]
7221                 || unsorted_regs[j] < unsorted_regs[order[i]]))
7222           order[i] = j;
7223
7224       /* Have we found a suitable register? if not, one must be used more
7225          than once.  */
7226       if (order[i] == order[i - 1])
7227         return 0;
7228
7229       /* Is the memory address adjacent and ascending? */
7230       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
7231         return 0;
7232     }
7233
7234   if (base)
7235     {
7236       *base = base_reg;
7237
7238       for (i = 0; i < nops; i++)
7239         regs[i] = unsorted_regs[order[i]];
7240
7241       *load_offset = unsorted_offsets[order[0]];
7242     }
7243
7244   if (unsorted_offsets[order[0]] == 0)
7245     return 1; /* stmia */
7246
7247   if (unsorted_offsets[order[0]] == 4)
7248     return 2; /* stmib */
7249
7250   if (unsorted_offsets[order[nops - 1]] == 0)
7251     return 3; /* stmda */
7252
7253   if (unsorted_offsets[order[nops - 1]] == -4)
7254     return 4; /* stmdb */
7255
7256   return 0;
7257 }
7258
7259 const char *
7260 emit_stm_seq (rtx *operands, int nops)
7261 {
7262   int regs[4];
7263   int base_reg;
7264   HOST_WIDE_INT offset;
7265   char buf[100];
7266   int i;
7267
7268   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
7269     {
7270     case 1:
7271       strcpy (buf, "stm%(ia%)\t");
7272       break;
7273
7274     case 2:
7275       strcpy (buf, "stm%(ib%)\t");
7276       break;
7277
7278     case 3:
7279       strcpy (buf, "stm%(da%)\t");
7280       break;
7281
7282     case 4:
7283       strcpy (buf, "stm%(db%)\t");
7284       break;
7285
7286     default:
7287       gcc_unreachable ();
7288     }
7289
7290   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
7291            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
7292
7293   for (i = 1; i < nops; i++)
7294     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
7295              reg_names[regs[i]]);
7296
7297   strcat (buf, "}\t%@ phole stm");
7298
7299   output_asm_insn (buf, operands);
7300   return "";
7301 }
7302 \f
7303 /* Routines for use in generating RTL.  */
7304
7305 rtx
7306 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
7307                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
7308 {
7309   HOST_WIDE_INT offset = *offsetp;
7310   int i = 0, j;
7311   rtx result;
7312   int sign = up ? 1 : -1;
7313   rtx mem, addr;
7314
7315   /* XScale has load-store double instructions, but they have stricter
7316      alignment requirements than load-store multiple, so we cannot
7317      use them.
7318
7319      For XScale ldm requires 2 + NREGS cycles to complete and blocks
7320      the pipeline until completion.
7321
7322         NREGS           CYCLES
7323           1               3
7324           2               4
7325           3               5
7326           4               6
7327
7328      An ldr instruction takes 1-3 cycles, but does not block the
7329      pipeline.
7330
7331         NREGS           CYCLES
7332           1              1-3
7333           2              2-6
7334           3              3-9
7335           4              4-12
7336
7337      Best case ldr will always win.  However, the more ldr instructions
7338      we issue, the less likely we are to be able to schedule them well.
7339      Using ldr instructions also increases code size.
7340
7341      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
7342      for counts of 3 or 4 regs.  */
7343   if (arm_tune_xscale && count <= 2 && ! optimize_size)
7344     {
7345       rtx seq;
7346
7347       start_sequence ();
7348
7349       for (i = 0; i < count; i++)
7350         {
7351           addr = plus_constant (from, i * 4 * sign);
7352           mem = adjust_automodify_address (basemem, SImode, addr, offset);
7353           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
7354           offset += 4 * sign;
7355         }
7356
7357       if (write_back)
7358         {
7359           emit_move_insn (from, plus_constant (from, count * 4 * sign));
7360           *offsetp = offset;
7361         }
7362
7363       seq = get_insns ();
7364       end_sequence ();
7365
7366       return seq;
7367     }
7368
7369   result = gen_rtx_PARALLEL (VOIDmode,
7370                              rtvec_alloc (count + (write_back ? 1 : 0)));
7371   if (write_back)
7372     {
7373       XVECEXP (result, 0, 0)
7374         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
7375       i = 1;
7376       count++;
7377     }
7378
7379   for (j = 0; i < count; i++, j++)
7380     {
7381       addr = plus_constant (from, j * 4 * sign);
7382       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
7383       XVECEXP (result, 0, i)
7384         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
7385       offset += 4 * sign;
7386     }
7387
7388   if (write_back)
7389     *offsetp = offset;
7390
7391   return result;
7392 }
7393
7394 rtx
7395 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
7396                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
7397 {
7398   HOST_WIDE_INT offset = *offsetp;
7399   int i = 0, j;
7400   rtx result;
7401   int sign = up ? 1 : -1;
7402   rtx mem, addr;
7403
7404   /* See arm_gen_load_multiple for discussion of
7405      the pros/cons of ldm/stm usage for XScale.  */
7406   if (arm_tune_xscale && count <= 2 && ! optimize_size)
7407     {
7408       rtx seq;
7409
7410       start_sequence ();
7411
7412       for (i = 0; i < count; i++)
7413         {
7414           addr = plus_constant (to, i * 4 * sign);
7415           mem = adjust_automodify_address (basemem, SImode, addr, offset);
7416           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
7417           offset += 4 * sign;
7418         }
7419
7420       if (write_back)
7421         {
7422           emit_move_insn (to, plus_constant (to, count * 4 * sign));
7423           *offsetp = offset;
7424         }
7425
7426       seq = get_insns ();
7427       end_sequence ();
7428
7429       return seq;
7430     }
7431
7432   result = gen_rtx_PARALLEL (VOIDmode,
7433                              rtvec_alloc (count + (write_back ? 1 : 0)));
7434   if (write_back)
7435     {
7436       XVECEXP (result, 0, 0)
7437         = gen_rtx_SET (VOIDmode, to,
7438                        plus_constant (to, count * 4 * sign));
7439       i = 1;
7440       count++;
7441     }
7442
7443   for (j = 0; i < count; i++, j++)
7444     {
7445       addr = plus_constant (to, j * 4 * sign);
7446       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
7447       XVECEXP (result, 0, i)
7448         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
7449       offset += 4 * sign;
7450     }
7451
7452   if (write_back)
7453     *offsetp = offset;
7454
7455   return result;
7456 }
7457
7458 int
7459 arm_gen_movmemqi (rtx *operands)
7460 {
7461   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
7462   HOST_WIDE_INT srcoffset, dstoffset;
7463   int i;
7464   rtx src, dst, srcbase, dstbase;
7465   rtx part_bytes_reg = NULL;
7466   rtx mem;
7467
7468   if (GET_CODE (operands[2]) != CONST_INT
7469       || GET_CODE (operands[3]) != CONST_INT
7470       || INTVAL (operands[2]) > 64
7471       || INTVAL (operands[3]) & 3)
7472     return 0;
7473
7474   dstbase = operands[0];
7475   srcbase = operands[1];
7476
7477   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
7478   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
7479
7480   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
7481   out_words_to_go = INTVAL (operands[2]) / 4;
7482   last_bytes = INTVAL (operands[2]) & 3;
7483   dstoffset = srcoffset = 0;
7484
7485   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
7486     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
7487
7488   for (i = 0; in_words_to_go >= 2; i+=4)
7489     {
7490       if (in_words_to_go > 4)
7491         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
7492                                           srcbase, &srcoffset));
7493       else
7494         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
7495                                           FALSE, srcbase, &srcoffset));
7496
7497       if (out_words_to_go)
7498         {
7499           if (out_words_to_go > 4)
7500             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
7501                                                dstbase, &dstoffset));
7502           else if (out_words_to_go != 1)
7503             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
7504                                                dst, TRUE,
7505                                                (last_bytes == 0
7506                                                 ? FALSE : TRUE),
7507                                                dstbase, &dstoffset));
7508           else
7509             {
7510               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
7511               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
7512               if (last_bytes != 0)
7513                 {
7514                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
7515                   dstoffset += 4;
7516                 }
7517             }
7518         }
7519
7520       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
7521       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
7522     }
7523
7524   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
7525   if (out_words_to_go)
7526     {
7527       rtx sreg;
7528
7529       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7530       sreg = copy_to_reg (mem);
7531
7532       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
7533       emit_move_insn (mem, sreg);
7534       in_words_to_go--;
7535
7536       gcc_assert (!in_words_to_go);     /* Sanity check */
7537     }
7538
7539   if (in_words_to_go)
7540     {
7541       gcc_assert (in_words_to_go > 0);
7542
7543       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7544       part_bytes_reg = copy_to_mode_reg (SImode, mem);
7545     }
7546
7547   gcc_assert (!last_bytes || part_bytes_reg);
7548
7549   if (BYTES_BIG_ENDIAN && last_bytes)
7550     {
7551       rtx tmp = gen_reg_rtx (SImode);
7552
7553       /* The bytes we want are in the top end of the word.  */
7554       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
7555                               GEN_INT (8 * (4 - last_bytes))));
7556       part_bytes_reg = tmp;
7557
7558       while (last_bytes)
7559         {
7560           mem = adjust_automodify_address (dstbase, QImode,
7561                                            plus_constant (dst, last_bytes - 1),
7562                                            dstoffset + last_bytes - 1);
7563           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7564
7565           if (--last_bytes)
7566             {
7567               tmp = gen_reg_rtx (SImode);
7568               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
7569               part_bytes_reg = tmp;
7570             }
7571         }
7572
7573     }
7574   else
7575     {
7576       if (last_bytes > 1)
7577         {
7578           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
7579           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
7580           last_bytes -= 2;
7581           if (last_bytes)
7582             {
7583               rtx tmp = gen_reg_rtx (SImode);
7584               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
7585               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
7586               part_bytes_reg = tmp;
7587               dstoffset += 2;
7588             }
7589         }
7590
7591       if (last_bytes)
7592         {
7593           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
7594           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7595         }
7596     }
7597
7598   return 1;
7599 }
7600
7601 /* Select a dominance comparison mode if possible for a test of the general
7602    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
7603    COND_OR == DOM_CC_X_AND_Y => (X && Y)
7604    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
7605    COND_OR == DOM_CC_X_OR_Y => (X || Y)
7606    In all cases OP will be either EQ or NE, but we don't need to know which
7607    here.  If we are unable to support a dominance comparison we return
7608    CC mode.  This will then fail to match for the RTL expressions that
7609    generate this call.  */
7610 enum machine_mode
7611 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
7612 {
7613   enum rtx_code cond1, cond2;
7614   int swapped = 0;
7615
7616   /* Currently we will probably get the wrong result if the individual
7617      comparisons are not simple.  This also ensures that it is safe to
7618      reverse a comparison if necessary.  */
7619   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
7620        != CCmode)
7621       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
7622           != CCmode))
7623     return CCmode;
7624
7625   /* The if_then_else variant of this tests the second condition if the
7626      first passes, but is true if the first fails.  Reverse the first
7627      condition to get a true "inclusive-or" expression.  */
7628   if (cond_or == DOM_CC_NX_OR_Y)
7629     cond1 = reverse_condition (cond1);
7630
7631   /* If the comparisons are not equal, and one doesn't dominate the other,
7632      then we can't do this.  */
7633   if (cond1 != cond2
7634       && !comparison_dominates_p (cond1, cond2)
7635       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
7636     return CCmode;
7637
7638   if (swapped)
7639     {
7640       enum rtx_code temp = cond1;
7641       cond1 = cond2;
7642       cond2 = temp;
7643     }
7644
7645   switch (cond1)
7646     {
7647     case EQ:
7648       if (cond_or == DOM_CC_X_AND_Y)
7649         return CC_DEQmode;
7650
7651       switch (cond2)
7652         {
7653         case EQ: return CC_DEQmode;
7654         case LE: return CC_DLEmode;
7655         case LEU: return CC_DLEUmode;
7656         case GE: return CC_DGEmode;
7657         case GEU: return CC_DGEUmode;
7658         default: gcc_unreachable ();
7659         }
7660
7661     case LT:
7662       if (cond_or == DOM_CC_X_AND_Y)
7663         return CC_DLTmode;
7664
7665       switch (cond2)
7666         {
7667         case  LT:
7668             return CC_DLTmode;
7669         case LE:
7670           return CC_DLEmode;
7671         case NE:
7672           return CC_DNEmode;
7673         default:
7674           gcc_unreachable ();
7675         }
7676
7677     case GT:
7678       if (cond_or == DOM_CC_X_AND_Y)
7679         return CC_DGTmode;
7680
7681       switch (cond2)
7682         {
7683         case GT:
7684           return CC_DGTmode;
7685         case GE:
7686           return CC_DGEmode;
7687         case NE:
7688           return CC_DNEmode;
7689         default:
7690           gcc_unreachable ();
7691         }
7692
7693     case LTU:
7694       if (cond_or == DOM_CC_X_AND_Y)
7695         return CC_DLTUmode;
7696
7697       switch (cond2)
7698         {
7699         case LTU:
7700           return CC_DLTUmode;
7701         case LEU:
7702           return CC_DLEUmode;
7703         case NE:
7704           return CC_DNEmode;
7705         default:
7706           gcc_unreachable ();
7707         }
7708
7709     case GTU:
7710       if (cond_or == DOM_CC_X_AND_Y)
7711         return CC_DGTUmode;
7712
7713       switch (cond2)
7714         {
7715         case GTU:
7716           return CC_DGTUmode;
7717         case GEU:
7718           return CC_DGEUmode;
7719         case NE:
7720           return CC_DNEmode;
7721         default:
7722           gcc_unreachable ();
7723         }
7724
7725     /* The remaining cases only occur when both comparisons are the
7726        same.  */
7727     case NE:
7728       gcc_assert (cond1 == cond2);
7729       return CC_DNEmode;
7730
7731     case LE:
7732       gcc_assert (cond1 == cond2);
7733       return CC_DLEmode;
7734
7735     case GE:
7736       gcc_assert (cond1 == cond2);
7737       return CC_DGEmode;
7738
7739     case LEU:
7740       gcc_assert (cond1 == cond2);
7741       return CC_DLEUmode;
7742
7743     case GEU:
7744       gcc_assert (cond1 == cond2);
7745       return CC_DGEUmode;
7746
7747     default:
7748       gcc_unreachable ();
7749     }
7750 }
7751
7752 enum machine_mode
7753 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
7754 {
7755   /* All floating point compares return CCFP if it is an equality
7756      comparison, and CCFPE otherwise.  */
7757   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
7758     {
7759       switch (op)
7760         {
7761         case EQ:
7762         case NE:
7763         case UNORDERED:
7764         case ORDERED:
7765         case UNLT:
7766         case UNLE:
7767         case UNGT:
7768         case UNGE:
7769         case UNEQ:
7770         case LTGT:
7771           return CCFPmode;
7772
7773         case LT:
7774         case LE:
7775         case GT:
7776         case GE:
7777           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
7778             return CCFPmode;
7779           return CCFPEmode;
7780
7781         default:
7782           gcc_unreachable ();
7783         }
7784     }
7785
7786   /* A compare with a shifted operand.  Because of canonicalization, the
7787      comparison will have to be swapped when we emit the assembler.  */
7788   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
7789       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7790           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
7791           || GET_CODE (x) == ROTATERT))
7792     return CC_SWPmode;
7793
7794   /* This operation is performed swapped, but since we only rely on the Z
7795      flag we don't need an additional mode.  */
7796   if (GET_MODE (y) == SImode && REG_P (y)
7797       && GET_CODE (x) == NEG
7798       && (op == EQ || op == NE))
7799     return CC_Zmode;
7800
7801   /* This is a special case that is used by combine to allow a
7802      comparison of a shifted byte load to be split into a zero-extend
7803      followed by a comparison of the shifted integer (only valid for
7804      equalities and unsigned inequalities).  */
7805   if (GET_MODE (x) == SImode
7806       && GET_CODE (x) == ASHIFT
7807       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
7808       && GET_CODE (XEXP (x, 0)) == SUBREG
7809       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
7810       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
7811       && (op == EQ || op == NE
7812           || op == GEU || op == GTU || op == LTU || op == LEU)
7813       && GET_CODE (y) == CONST_INT)
7814     return CC_Zmode;
7815
7816   /* A construct for a conditional compare, if the false arm contains
7817      0, then both conditions must be true, otherwise either condition
7818      must be true.  Not all conditions are possible, so CCmode is
7819      returned if it can't be done.  */
7820   if (GET_CODE (x) == IF_THEN_ELSE
7821       && (XEXP (x, 2) == const0_rtx
7822           || XEXP (x, 2) == const1_rtx)
7823       && COMPARISON_P (XEXP (x, 0))
7824       && COMPARISON_P (XEXP (x, 1)))
7825     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7826                                          INTVAL (XEXP (x, 2)));
7827
7828   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
7829   if (GET_CODE (x) == AND
7830       && COMPARISON_P (XEXP (x, 0))
7831       && COMPARISON_P (XEXP (x, 1)))
7832     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7833                                          DOM_CC_X_AND_Y);
7834
7835   if (GET_CODE (x) == IOR
7836       && COMPARISON_P (XEXP (x, 0))
7837       && COMPARISON_P (XEXP (x, 1)))
7838     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7839                                          DOM_CC_X_OR_Y);
7840
7841   /* An operation (on Thumb) where we want to test for a single bit.
7842      This is done by shifting that bit up into the top bit of a
7843      scratch register; we can then branch on the sign bit.  */
7844   if (TARGET_THUMB1
7845       && GET_MODE (x) == SImode
7846       && (op == EQ || op == NE)
7847       && GET_CODE (x) == ZERO_EXTRACT
7848       && XEXP (x, 1) == const1_rtx)
7849     return CC_Nmode;
7850
7851   /* An operation that sets the condition codes as a side-effect, the
7852      V flag is not set correctly, so we can only use comparisons where
7853      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
7854      instead.)  */
7855   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
7856   if (GET_MODE (x) == SImode
7857       && y == const0_rtx
7858       && (op == EQ || op == NE || op == LT || op == GE)
7859       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
7860           || GET_CODE (x) == AND || GET_CODE (x) == IOR
7861           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
7862           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
7863           || GET_CODE (x) == LSHIFTRT
7864           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7865           || GET_CODE (x) == ROTATERT
7866           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
7867     return CC_NOOVmode;
7868
7869   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
7870     return CC_Zmode;
7871
7872   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
7873       && GET_CODE (x) == PLUS
7874       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
7875     return CC_Cmode;
7876
7877   return CCmode;
7878 }
7879
7880 /* X and Y are two things to compare using CODE.  Emit the compare insn and
7881    return the rtx for register 0 in the proper mode.  FP means this is a
7882    floating point compare: I don't think that it is needed on the arm.  */
7883 rtx
7884 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
7885 {
7886   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
7887   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
7888
7889   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
7890
7891   return cc_reg;
7892 }
7893
7894 /* Generate a sequence of insns that will generate the correct return
7895    address mask depending on the physical architecture that the program
7896    is running on.  */
7897 rtx
7898 arm_gen_return_addr_mask (void)
7899 {
7900   rtx reg = gen_reg_rtx (Pmode);
7901
7902   emit_insn (gen_return_addr_mask (reg));
7903   return reg;
7904 }
7905
7906 void
7907 arm_reload_in_hi (rtx *operands)
7908 {
7909   rtx ref = operands[1];
7910   rtx base, scratch;
7911   HOST_WIDE_INT offset = 0;
7912
7913   if (GET_CODE (ref) == SUBREG)
7914     {
7915       offset = SUBREG_BYTE (ref);
7916       ref = SUBREG_REG (ref);
7917     }
7918
7919   if (GET_CODE (ref) == REG)
7920     {
7921       /* We have a pseudo which has been spilt onto the stack; there
7922          are two cases here: the first where there is a simple
7923          stack-slot replacement and a second where the stack-slot is
7924          out of range, or is used as a subreg.  */
7925       if (reg_equiv_mem[REGNO (ref)])
7926         {
7927           ref = reg_equiv_mem[REGNO (ref)];
7928           base = find_replacement (&XEXP (ref, 0));
7929         }
7930       else
7931         /* The slot is out of range, or was dressed up in a SUBREG.  */
7932         base = reg_equiv_address[REGNO (ref)];
7933     }
7934   else
7935     base = find_replacement (&XEXP (ref, 0));
7936
7937   /* Handle the case where the address is too complex to be offset by 1.  */
7938   if (GET_CODE (base) == MINUS
7939       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7940     {
7941       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7942
7943       emit_set_insn (base_plus, base);
7944       base = base_plus;
7945     }
7946   else if (GET_CODE (base) == PLUS)
7947     {
7948       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7949       HOST_WIDE_INT hi, lo;
7950
7951       offset += INTVAL (XEXP (base, 1));
7952       base = XEXP (base, 0);
7953
7954       /* Rework the address into a legal sequence of insns.  */
7955       /* Valid range for lo is -4095 -> 4095 */
7956       lo = (offset >= 0
7957             ? (offset & 0xfff)
7958             : -((-offset) & 0xfff));
7959
7960       /* Corner case, if lo is the max offset then we would be out of range
7961          once we have added the additional 1 below, so bump the msb into the
7962          pre-loading insn(s).  */
7963       if (lo == 4095)
7964         lo &= 0x7ff;
7965
7966       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7967              ^ (HOST_WIDE_INT) 0x80000000)
7968             - (HOST_WIDE_INT) 0x80000000);
7969
7970       gcc_assert (hi + lo == offset);
7971
7972       if (hi != 0)
7973         {
7974           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7975
7976           /* Get the base address; addsi3 knows how to handle constants
7977              that require more than one insn.  */
7978           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7979           base = base_plus;
7980           offset = lo;
7981         }
7982     }
7983
7984   /* Operands[2] may overlap operands[0] (though it won't overlap
7985      operands[1]), that's why we asked for a DImode reg -- so we can
7986      use the bit that does not overlap.  */
7987   if (REGNO (operands[2]) == REGNO (operands[0]))
7988     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7989   else
7990     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7991
7992   emit_insn (gen_zero_extendqisi2 (scratch,
7993                                    gen_rtx_MEM (QImode,
7994                                                 plus_constant (base,
7995                                                                offset))));
7996   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
7997                                    gen_rtx_MEM (QImode,
7998                                                 plus_constant (base,
7999                                                                offset + 1))));
8000   if (!BYTES_BIG_ENDIAN)
8001     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8002                    gen_rtx_IOR (SImode,
8003                                 gen_rtx_ASHIFT
8004                                 (SImode,
8005                                  gen_rtx_SUBREG (SImode, operands[0], 0),
8006                                  GEN_INT (8)),
8007                                 scratch));
8008   else
8009     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
8010                    gen_rtx_IOR (SImode,
8011                                 gen_rtx_ASHIFT (SImode, scratch,
8012                                                 GEN_INT (8)),
8013                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
8014 }
8015
8016 /* Handle storing a half-word to memory during reload by synthesizing as two
8017    byte stores.  Take care not to clobber the input values until after we
8018    have moved them somewhere safe.  This code assumes that if the DImode
8019    scratch in operands[2] overlaps either the input value or output address
8020    in some way, then that value must die in this insn (we absolutely need
8021    two scratch registers for some corner cases).  */
8022 void
8023 arm_reload_out_hi (rtx *operands)
8024 {
8025   rtx ref = operands[0];
8026   rtx outval = operands[1];
8027   rtx base, scratch;
8028   HOST_WIDE_INT offset = 0;
8029
8030   if (GET_CODE (ref) == SUBREG)
8031     {
8032       offset = SUBREG_BYTE (ref);
8033       ref = SUBREG_REG (ref);
8034     }
8035
8036   if (GET_CODE (ref) == REG)
8037     {
8038       /* We have a pseudo which has been spilt onto the stack; there
8039          are two cases here: the first where there is a simple
8040          stack-slot replacement and a second where the stack-slot is
8041          out of range, or is used as a subreg.  */
8042       if (reg_equiv_mem[REGNO (ref)])
8043         {
8044           ref = reg_equiv_mem[REGNO (ref)];
8045           base = find_replacement (&XEXP (ref, 0));
8046         }
8047       else
8048         /* The slot is out of range, or was dressed up in a SUBREG.  */
8049         base = reg_equiv_address[REGNO (ref)];
8050     }
8051   else
8052     base = find_replacement (&XEXP (ref, 0));
8053
8054   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
8055
8056   /* Handle the case where the address is too complex to be offset by 1.  */
8057   if (GET_CODE (base) == MINUS
8058       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
8059     {
8060       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8061
8062       /* Be careful not to destroy OUTVAL.  */
8063       if (reg_overlap_mentioned_p (base_plus, outval))
8064         {
8065           /* Updating base_plus might destroy outval, see if we can
8066              swap the scratch and base_plus.  */
8067           if (!reg_overlap_mentioned_p (scratch, outval))
8068             {
8069               rtx tmp = scratch;
8070               scratch = base_plus;
8071               base_plus = tmp;
8072             }
8073           else
8074             {
8075               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8076
8077               /* Be conservative and copy OUTVAL into the scratch now,
8078                  this should only be necessary if outval is a subreg
8079                  of something larger than a word.  */
8080               /* XXX Might this clobber base?  I can't see how it can,
8081                  since scratch is known to overlap with OUTVAL, and
8082                  must be wider than a word.  */
8083               emit_insn (gen_movhi (scratch_hi, outval));
8084               outval = scratch_hi;
8085             }
8086         }
8087
8088       emit_set_insn (base_plus, base);
8089       base = base_plus;
8090     }
8091   else if (GET_CODE (base) == PLUS)
8092     {
8093       /* The addend must be CONST_INT, or we would have dealt with it above.  */
8094       HOST_WIDE_INT hi, lo;
8095
8096       offset += INTVAL (XEXP (base, 1));
8097       base = XEXP (base, 0);
8098
8099       /* Rework the address into a legal sequence of insns.  */
8100       /* Valid range for lo is -4095 -> 4095 */
8101       lo = (offset >= 0
8102             ? (offset & 0xfff)
8103             : -((-offset) & 0xfff));
8104
8105       /* Corner case, if lo is the max offset then we would be out of range
8106          once we have added the additional 1 below, so bump the msb into the
8107          pre-loading insn(s).  */
8108       if (lo == 4095)
8109         lo &= 0x7ff;
8110
8111       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
8112              ^ (HOST_WIDE_INT) 0x80000000)
8113             - (HOST_WIDE_INT) 0x80000000);
8114
8115       gcc_assert (hi + lo == offset);
8116
8117       if (hi != 0)
8118         {
8119           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
8120
8121           /* Be careful not to destroy OUTVAL.  */
8122           if (reg_overlap_mentioned_p (base_plus, outval))
8123             {
8124               /* Updating base_plus might destroy outval, see if we
8125                  can swap the scratch and base_plus.  */
8126               if (!reg_overlap_mentioned_p (scratch, outval))
8127                 {
8128                   rtx tmp = scratch;
8129                   scratch = base_plus;
8130                   base_plus = tmp;
8131                 }
8132               else
8133                 {
8134                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
8135
8136                   /* Be conservative and copy outval into scratch now,
8137                      this should only be necessary if outval is a
8138                      subreg of something larger than a word.  */
8139                   /* XXX Might this clobber base?  I can't see how it
8140                      can, since scratch is known to overlap with
8141                      outval.  */
8142                   emit_insn (gen_movhi (scratch_hi, outval));
8143                   outval = scratch_hi;
8144                 }
8145             }
8146
8147           /* Get the base address; addsi3 knows how to handle constants
8148              that require more than one insn.  */
8149           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
8150           base = base_plus;
8151           offset = lo;
8152         }
8153     }
8154
8155   if (BYTES_BIG_ENDIAN)
8156     {
8157       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8158                                          plus_constant (base, offset + 1)),
8159                             gen_lowpart (QImode, outval)));
8160       emit_insn (gen_lshrsi3 (scratch,
8161                               gen_rtx_SUBREG (SImode, outval, 0),
8162                               GEN_INT (8)));
8163       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8164                             gen_lowpart (QImode, scratch)));
8165     }
8166   else
8167     {
8168       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
8169                             gen_lowpart (QImode, outval)));
8170       emit_insn (gen_lshrsi3 (scratch,
8171                               gen_rtx_SUBREG (SImode, outval, 0),
8172                               GEN_INT (8)));
8173       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
8174                                          plus_constant (base, offset + 1)),
8175                             gen_lowpart (QImode, scratch)));
8176     }
8177 }
8178
8179 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
8180    (padded to the size of a word) should be passed in a register.  */
8181
8182 static bool
8183 arm_must_pass_in_stack (enum machine_mode mode, tree type)
8184 {
8185   if (TARGET_AAPCS_BASED)
8186     return must_pass_in_stack_var_size (mode, type);
8187   else
8188     return must_pass_in_stack_var_size_or_pad (mode, type);
8189 }
8190
8191
8192 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
8193    Return true if an argument passed on the stack should be padded upwards,
8194    i.e. if the least-significant byte has useful data.
8195    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
8196    aggregate types are placed in the lowest memory address.  */
8197
8198 bool
8199 arm_pad_arg_upward (enum machine_mode mode, tree type)
8200 {
8201   if (!TARGET_AAPCS_BASED)
8202     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
8203
8204   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
8205     return false;
8206
8207   return true;
8208 }
8209
8210
8211 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
8212    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
8213    byte of the register has useful data, and return the opposite if the
8214    most significant byte does.
8215    For AAPCS, small aggregates and small complex types are always padded
8216    upwards.  */
8217
8218 bool
8219 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
8220                     tree type, int first ATTRIBUTE_UNUSED)
8221 {
8222   if (TARGET_AAPCS_BASED
8223       && BYTES_BIG_ENDIAN
8224       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
8225       && int_size_in_bytes (type) <= 4)
8226     return true;
8227
8228   /* Otherwise, use default padding.  */
8229   return !BYTES_BIG_ENDIAN;
8230 }
8231
8232 \f
8233 /* Print a symbolic form of X to the debug file, F.  */
8234 static void
8235 arm_print_value (FILE *f, rtx x)
8236 {
8237   switch (GET_CODE (x))
8238     {
8239     case CONST_INT:
8240       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
8241       return;
8242
8243     case CONST_DOUBLE:
8244       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
8245       return;
8246
8247     case CONST_VECTOR:
8248       {
8249         int i;
8250
8251         fprintf (f, "<");
8252         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
8253           {
8254             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
8255             if (i < (CONST_VECTOR_NUNITS (x) - 1))
8256               fputc (',', f);
8257           }
8258         fprintf (f, ">");
8259       }
8260       return;
8261
8262     case CONST_STRING:
8263       fprintf (f, "\"%s\"", XSTR (x, 0));
8264       return;
8265
8266     case SYMBOL_REF:
8267       fprintf (f, "`%s'", XSTR (x, 0));
8268       return;
8269
8270     case LABEL_REF:
8271       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
8272       return;
8273
8274     case CONST:
8275       arm_print_value (f, XEXP (x, 0));
8276       return;
8277
8278     case PLUS:
8279       arm_print_value (f, XEXP (x, 0));
8280       fprintf (f, "+");
8281       arm_print_value (f, XEXP (x, 1));
8282       return;
8283
8284     case PC:
8285       fprintf (f, "pc");
8286       return;
8287
8288     default:
8289       fprintf (f, "????");
8290       return;
8291     }
8292 }
8293 \f
8294 /* Routines for manipulation of the constant pool.  */
8295
8296 /* Arm instructions cannot load a large constant directly into a
8297    register; they have to come from a pc relative load.  The constant
8298    must therefore be placed in the addressable range of the pc
8299    relative load.  Depending on the precise pc relative load
8300    instruction the range is somewhere between 256 bytes and 4k.  This
8301    means that we often have to dump a constant inside a function, and
8302    generate code to branch around it.
8303
8304    It is important to minimize this, since the branches will slow
8305    things down and make the code larger.
8306
8307    Normally we can hide the table after an existing unconditional
8308    branch so that there is no interruption of the flow, but in the
8309    worst case the code looks like this:
8310
8311         ldr     rn, L1
8312         ...
8313         b       L2
8314         align
8315         L1:     .long value
8316         L2:
8317         ...
8318
8319         ldr     rn, L3
8320         ...
8321         b       L4
8322         align
8323         L3:     .long value
8324         L4:
8325         ...
8326
8327    We fix this by performing a scan after scheduling, which notices
8328    which instructions need to have their operands fetched from the
8329    constant table and builds the table.
8330
8331    The algorithm starts by building a table of all the constants that
8332    need fixing up and all the natural barriers in the function (places
8333    where a constant table can be dropped without breaking the flow).
8334    For each fixup we note how far the pc-relative replacement will be
8335    able to reach and the offset of the instruction into the function.
8336
8337    Having built the table we then group the fixes together to form
8338    tables that are as large as possible (subject to addressing
8339    constraints) and emit each table of constants after the last
8340    barrier that is within range of all the instructions in the group.
8341    If a group does not contain a barrier, then we forcibly create one
8342    by inserting a jump instruction into the flow.  Once the table has
8343    been inserted, the insns are then modified to reference the
8344    relevant entry in the pool.
8345
8346    Possible enhancements to the algorithm (not implemented) are:
8347
8348    1) For some processors and object formats, there may be benefit in
8349    aligning the pools to the start of cache lines; this alignment
8350    would need to be taken into account when calculating addressability
8351    of a pool.  */
8352
8353 /* These typedefs are located at the start of this file, so that
8354    they can be used in the prototypes there.  This comment is to
8355    remind readers of that fact so that the following structures
8356    can be understood more easily.
8357
8358      typedef struct minipool_node    Mnode;
8359      typedef struct minipool_fixup   Mfix;  */
8360
8361 struct minipool_node
8362 {
8363   /* Doubly linked chain of entries.  */
8364   Mnode * next;
8365   Mnode * prev;
8366   /* The maximum offset into the code that this entry can be placed.  While
8367      pushing fixes for forward references, all entries are sorted in order
8368      of increasing max_address.  */
8369   HOST_WIDE_INT max_address;
8370   /* Similarly for an entry inserted for a backwards ref.  */
8371   HOST_WIDE_INT min_address;
8372   /* The number of fixes referencing this entry.  This can become zero
8373      if we "unpush" an entry.  In this case we ignore the entry when we
8374      come to emit the code.  */
8375   int refcount;
8376   /* The offset from the start of the minipool.  */
8377   HOST_WIDE_INT offset;
8378   /* The value in table.  */
8379   rtx value;
8380   /* The mode of value.  */
8381   enum machine_mode mode;
8382   /* The size of the value.  With iWMMXt enabled
8383      sizes > 4 also imply an alignment of 8-bytes.  */
8384   int fix_size;
8385 };
8386
8387 struct minipool_fixup
8388 {
8389   Mfix *            next;
8390   rtx               insn;
8391   HOST_WIDE_INT     address;
8392   rtx *             loc;
8393   enum machine_mode mode;
8394   int               fix_size;
8395   rtx               value;
8396   Mnode *           minipool;
8397   HOST_WIDE_INT     forwards;
8398   HOST_WIDE_INT     backwards;
8399 };
8400
8401 /* Fixes less than a word need padding out to a word boundary.  */
8402 #define MINIPOOL_FIX_SIZE(mode) \
8403   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
8404
8405 static Mnode *  minipool_vector_head;
8406 static Mnode *  minipool_vector_tail;
8407 static rtx      minipool_vector_label;
8408 static int      minipool_pad;
8409
8410 /* The linked list of all minipool fixes required for this function.  */
8411 Mfix *          minipool_fix_head;
8412 Mfix *          minipool_fix_tail;
8413 /* The fix entry for the current minipool, once it has been placed.  */
8414 Mfix *          minipool_barrier;
8415
8416 /* Determines if INSN is the start of a jump table.  Returns the end
8417    of the TABLE or NULL_RTX.  */
8418 static rtx
8419 is_jump_table (rtx insn)
8420 {
8421   rtx table;
8422
8423   if (GET_CODE (insn) == JUMP_INSN
8424       && JUMP_LABEL (insn) != NULL
8425       && ((table = next_real_insn (JUMP_LABEL (insn)))
8426           == next_real_insn (insn))
8427       && table != NULL
8428       && GET_CODE (table) == JUMP_INSN
8429       && (GET_CODE (PATTERN (table)) == ADDR_VEC
8430           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
8431     return table;
8432
8433   return NULL_RTX;
8434 }
8435
8436 #ifndef JUMP_TABLES_IN_TEXT_SECTION
8437 #define JUMP_TABLES_IN_TEXT_SECTION 0
8438 #endif
8439
8440 static HOST_WIDE_INT
8441 get_jump_table_size (rtx insn)
8442 {
8443   /* ADDR_VECs only take room if read-only data does into the text
8444      section.  */
8445   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
8446     {
8447       rtx body = PATTERN (insn);
8448       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
8449       HOST_WIDE_INT size;
8450       HOST_WIDE_INT modesize;
8451
8452       modesize = GET_MODE_SIZE (GET_MODE (body));
8453       size = modesize * XVECLEN (body, elt);
8454       switch (modesize)
8455         {
8456         case 1:
8457           /* Round up size  of TBB table to a halfword boundary.  */
8458           size = (size + 1) & ~(HOST_WIDE_INT)1;
8459           break;
8460         case 2:
8461           /* No padding necessary for TBH.  */
8462           break;
8463         case 4:
8464           /* Add two bytes for alignment on Thumb.  */
8465           if (TARGET_THUMB)
8466             size += 2;
8467           break;
8468         default:
8469           gcc_unreachable ();
8470         }
8471       return size;
8472     }
8473
8474   return 0;
8475 }
8476
8477 /* Move a minipool fix MP from its current location to before MAX_MP.
8478    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
8479    constraints may need updating.  */
8480 static Mnode *
8481 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
8482                                HOST_WIDE_INT max_address)
8483 {
8484   /* The code below assumes these are different.  */
8485   gcc_assert (mp != max_mp);
8486
8487   if (max_mp == NULL)
8488     {
8489       if (max_address < mp->max_address)
8490         mp->max_address = max_address;
8491     }
8492   else
8493     {
8494       if (max_address > max_mp->max_address - mp->fix_size)
8495         mp->max_address = max_mp->max_address - mp->fix_size;
8496       else
8497         mp->max_address = max_address;
8498
8499       /* Unlink MP from its current position.  Since max_mp is non-null,
8500        mp->prev must be non-null.  */
8501       mp->prev->next = mp->next;
8502       if (mp->next != NULL)
8503         mp->next->prev = mp->prev;
8504       else
8505         minipool_vector_tail = mp->prev;
8506
8507       /* Re-insert it before MAX_MP.  */
8508       mp->next = max_mp;
8509       mp->prev = max_mp->prev;
8510       max_mp->prev = mp;
8511
8512       if (mp->prev != NULL)
8513         mp->prev->next = mp;
8514       else
8515         minipool_vector_head = mp;
8516     }
8517
8518   /* Save the new entry.  */
8519   max_mp = mp;
8520
8521   /* Scan over the preceding entries and adjust their addresses as
8522      required.  */
8523   while (mp->prev != NULL
8524          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8525     {
8526       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8527       mp = mp->prev;
8528     }
8529
8530   return max_mp;
8531 }
8532
8533 /* Add a constant to the minipool for a forward reference.  Returns the
8534    node added or NULL if the constant will not fit in this pool.  */
8535 static Mnode *
8536 add_minipool_forward_ref (Mfix *fix)
8537 {
8538   /* If set, max_mp is the first pool_entry that has a lower
8539      constraint than the one we are trying to add.  */
8540   Mnode *       max_mp = NULL;
8541   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
8542   Mnode *       mp;
8543
8544   /* If the minipool starts before the end of FIX->INSN then this FIX
8545      can not be placed into the current pool.  Furthermore, adding the
8546      new constant pool entry may cause the pool to start FIX_SIZE bytes
8547      earlier.  */
8548   if (minipool_vector_head &&
8549       (fix->address + get_attr_length (fix->insn)
8550        >= minipool_vector_head->max_address - fix->fix_size))
8551     return NULL;
8552
8553   /* Scan the pool to see if a constant with the same value has
8554      already been added.  While we are doing this, also note the
8555      location where we must insert the constant if it doesn't already
8556      exist.  */
8557   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8558     {
8559       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8560           && fix->mode == mp->mode
8561           && (GET_CODE (fix->value) != CODE_LABEL
8562               || (CODE_LABEL_NUMBER (fix->value)
8563                   == CODE_LABEL_NUMBER (mp->value)))
8564           && rtx_equal_p (fix->value, mp->value))
8565         {
8566           /* More than one fix references this entry.  */
8567           mp->refcount++;
8568           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
8569         }
8570
8571       /* Note the insertion point if necessary.  */
8572       if (max_mp == NULL
8573           && mp->max_address > max_address)
8574         max_mp = mp;
8575
8576       /* If we are inserting an 8-bytes aligned quantity and
8577          we have not already found an insertion point, then
8578          make sure that all such 8-byte aligned quantities are
8579          placed at the start of the pool.  */
8580       if (ARM_DOUBLEWORD_ALIGN
8581           && max_mp == NULL
8582           && fix->fix_size >= 8
8583           && mp->fix_size < 8)
8584         {
8585           max_mp = mp;
8586           max_address = mp->max_address;
8587         }
8588     }
8589
8590   /* The value is not currently in the minipool, so we need to create
8591      a new entry for it.  If MAX_MP is NULL, the entry will be put on
8592      the end of the list since the placement is less constrained than
8593      any existing entry.  Otherwise, we insert the new fix before
8594      MAX_MP and, if necessary, adjust the constraints on the other
8595      entries.  */
8596   mp = XNEW (Mnode);
8597   mp->fix_size = fix->fix_size;
8598   mp->mode = fix->mode;
8599   mp->value = fix->value;
8600   mp->refcount = 1;
8601   /* Not yet required for a backwards ref.  */
8602   mp->min_address = -65536;
8603
8604   if (max_mp == NULL)
8605     {
8606       mp->max_address = max_address;
8607       mp->next = NULL;
8608       mp->prev = minipool_vector_tail;
8609
8610       if (mp->prev == NULL)
8611         {
8612           minipool_vector_head = mp;
8613           minipool_vector_label = gen_label_rtx ();
8614         }
8615       else
8616         mp->prev->next = mp;
8617
8618       minipool_vector_tail = mp;
8619     }
8620   else
8621     {
8622       if (max_address > max_mp->max_address - mp->fix_size)
8623         mp->max_address = max_mp->max_address - mp->fix_size;
8624       else
8625         mp->max_address = max_address;
8626
8627       mp->next = max_mp;
8628       mp->prev = max_mp->prev;
8629       max_mp->prev = mp;
8630       if (mp->prev != NULL)
8631         mp->prev->next = mp;
8632       else
8633         minipool_vector_head = mp;
8634     }
8635
8636   /* Save the new entry.  */
8637   max_mp = mp;
8638
8639   /* Scan over the preceding entries and adjust their addresses as
8640      required.  */
8641   while (mp->prev != NULL
8642          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8643     {
8644       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8645       mp = mp->prev;
8646     }
8647
8648   return max_mp;
8649 }
8650
8651 static Mnode *
8652 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
8653                                 HOST_WIDE_INT  min_address)
8654 {
8655   HOST_WIDE_INT offset;
8656
8657   /* The code below assumes these are different.  */
8658   gcc_assert (mp != min_mp);
8659
8660   if (min_mp == NULL)
8661     {
8662       if (min_address > mp->min_address)
8663         mp->min_address = min_address;
8664     }
8665   else
8666     {
8667       /* We will adjust this below if it is too loose.  */
8668       mp->min_address = min_address;
8669
8670       /* Unlink MP from its current position.  Since min_mp is non-null,
8671          mp->next must be non-null.  */
8672       mp->next->prev = mp->prev;
8673       if (mp->prev != NULL)
8674         mp->prev->next = mp->next;
8675       else
8676         minipool_vector_head = mp->next;
8677
8678       /* Reinsert it after MIN_MP.  */
8679       mp->prev = min_mp;
8680       mp->next = min_mp->next;
8681       min_mp->next = mp;
8682       if (mp->next != NULL)
8683         mp->next->prev = mp;
8684       else
8685         minipool_vector_tail = mp;
8686     }
8687
8688   min_mp = mp;
8689
8690   offset = 0;
8691   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8692     {
8693       mp->offset = offset;
8694       if (mp->refcount > 0)
8695         offset += mp->fix_size;
8696
8697       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
8698         mp->next->min_address = mp->min_address + mp->fix_size;
8699     }
8700
8701   return min_mp;
8702 }
8703
8704 /* Add a constant to the minipool for a backward reference.  Returns the
8705    node added or NULL if the constant will not fit in this pool.
8706
8707    Note that the code for insertion for a backwards reference can be
8708    somewhat confusing because the calculated offsets for each fix do
8709    not take into account the size of the pool (which is still under
8710    construction.  */
8711 static Mnode *
8712 add_minipool_backward_ref (Mfix *fix)
8713 {
8714   /* If set, min_mp is the last pool_entry that has a lower constraint
8715      than the one we are trying to add.  */
8716   Mnode *min_mp = NULL;
8717   /* This can be negative, since it is only a constraint.  */
8718   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
8719   Mnode *mp;
8720
8721   /* If we can't reach the current pool from this insn, or if we can't
8722      insert this entry at the end of the pool without pushing other
8723      fixes out of range, then we don't try.  This ensures that we
8724      can't fail later on.  */
8725   if (min_address >= minipool_barrier->address
8726       || (minipool_vector_tail->min_address + fix->fix_size
8727           >= minipool_barrier->address))
8728     return NULL;
8729
8730   /* Scan the pool to see if a constant with the same value has
8731      already been added.  While we are doing this, also note the
8732      location where we must insert the constant if it doesn't already
8733      exist.  */
8734   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
8735     {
8736       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8737           && fix->mode == mp->mode
8738           && (GET_CODE (fix->value) != CODE_LABEL
8739               || (CODE_LABEL_NUMBER (fix->value)
8740                   == CODE_LABEL_NUMBER (mp->value)))
8741           && rtx_equal_p (fix->value, mp->value)
8742           /* Check that there is enough slack to move this entry to the
8743              end of the table (this is conservative).  */
8744           && (mp->max_address
8745               > (minipool_barrier->address
8746                  + minipool_vector_tail->offset
8747                  + minipool_vector_tail->fix_size)))
8748         {
8749           mp->refcount++;
8750           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
8751         }
8752
8753       if (min_mp != NULL)
8754         mp->min_address += fix->fix_size;
8755       else
8756         {
8757           /* Note the insertion point if necessary.  */
8758           if (mp->min_address < min_address)
8759             {
8760               /* For now, we do not allow the insertion of 8-byte alignment
8761                  requiring nodes anywhere but at the start of the pool.  */
8762               if (ARM_DOUBLEWORD_ALIGN
8763                   && fix->fix_size >= 8 && mp->fix_size < 8)
8764                 return NULL;
8765               else
8766                 min_mp = mp;
8767             }
8768           else if (mp->max_address
8769                    < minipool_barrier->address + mp->offset + fix->fix_size)
8770             {
8771               /* Inserting before this entry would push the fix beyond
8772                  its maximum address (which can happen if we have
8773                  re-located a forwards fix); force the new fix to come
8774                  after it.  */
8775               min_mp = mp;
8776               min_address = mp->min_address + fix->fix_size;
8777             }
8778           /* If we are inserting an 8-bytes aligned quantity and
8779              we have not already found an insertion point, then
8780              make sure that all such 8-byte aligned quantities are
8781              placed at the start of the pool.  */
8782           else if (ARM_DOUBLEWORD_ALIGN
8783                    && min_mp == NULL
8784                    && fix->fix_size >= 8
8785                    && mp->fix_size < 8)
8786             {
8787               min_mp = mp;
8788               min_address = mp->min_address + fix->fix_size;
8789             }
8790         }
8791     }
8792
8793   /* We need to create a new entry.  */
8794   mp = XNEW (Mnode);
8795   mp->fix_size = fix->fix_size;
8796   mp->mode = fix->mode;
8797   mp->value = fix->value;
8798   mp->refcount = 1;
8799   mp->max_address = minipool_barrier->address + 65536;
8800
8801   mp->min_address = min_address;
8802
8803   if (min_mp == NULL)
8804     {
8805       mp->prev = NULL;
8806       mp->next = minipool_vector_head;
8807
8808       if (mp->next == NULL)
8809         {
8810           minipool_vector_tail = mp;
8811           minipool_vector_label = gen_label_rtx ();
8812         }
8813       else
8814         mp->next->prev = mp;
8815
8816       minipool_vector_head = mp;
8817     }
8818   else
8819     {
8820       mp->next = min_mp->next;
8821       mp->prev = min_mp;
8822       min_mp->next = mp;
8823
8824       if (mp->next != NULL)
8825         mp->next->prev = mp;
8826       else
8827         minipool_vector_tail = mp;
8828     }
8829
8830   /* Save the new entry.  */
8831   min_mp = mp;
8832
8833   if (mp->prev)
8834     mp = mp->prev;
8835   else
8836     mp->offset = 0;
8837
8838   /* Scan over the following entries and adjust their offsets.  */
8839   while (mp->next != NULL)
8840     {
8841       if (mp->next->min_address < mp->min_address + mp->fix_size)
8842         mp->next->min_address = mp->min_address + mp->fix_size;
8843
8844       if (mp->refcount)
8845         mp->next->offset = mp->offset + mp->fix_size;
8846       else
8847         mp->next->offset = mp->offset;
8848
8849       mp = mp->next;
8850     }
8851
8852   return min_mp;
8853 }
8854
8855 static void
8856 assign_minipool_offsets (Mfix *barrier)
8857 {
8858   HOST_WIDE_INT offset = 0;
8859   Mnode *mp;
8860
8861   minipool_barrier = barrier;
8862
8863   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8864     {
8865       mp->offset = offset;
8866
8867       if (mp->refcount > 0)
8868         offset += mp->fix_size;
8869     }
8870 }
8871
8872 /* Output the literal table */
8873 static void
8874 dump_minipool (rtx scan)
8875 {
8876   Mnode * mp;
8877   Mnode * nmp;
8878   int align64 = 0;
8879
8880   if (ARM_DOUBLEWORD_ALIGN)
8881     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8882       if (mp->refcount > 0 && mp->fix_size >= 8)
8883         {
8884           align64 = 1;
8885           break;
8886         }
8887
8888   if (dump_file)
8889     fprintf (dump_file,
8890              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
8891              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
8892
8893   scan = emit_label_after (gen_label_rtx (), scan);
8894   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
8895   scan = emit_label_after (minipool_vector_label, scan);
8896
8897   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
8898     {
8899       if (mp->refcount > 0)
8900         {
8901           if (dump_file)
8902             {
8903               fprintf (dump_file,
8904                        ";;  Offset %u, min %ld, max %ld ",
8905                        (unsigned) mp->offset, (unsigned long) mp->min_address,
8906                        (unsigned long) mp->max_address);
8907               arm_print_value (dump_file, mp->value);
8908               fputc ('\n', dump_file);
8909             }
8910
8911           switch (mp->fix_size)
8912             {
8913 #ifdef HAVE_consttable_1
8914             case 1:
8915               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
8916               break;
8917
8918 #endif
8919 #ifdef HAVE_consttable_2
8920             case 2:
8921               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
8922               break;
8923
8924 #endif
8925 #ifdef HAVE_consttable_4
8926             case 4:
8927               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
8928               break;
8929
8930 #endif
8931 #ifdef HAVE_consttable_8
8932             case 8:
8933               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
8934               break;
8935
8936 #endif
8937 #ifdef HAVE_consttable_16
8938             case 16:
8939               scan = emit_insn_after (gen_consttable_16 (mp->value), scan);
8940               break;
8941
8942 #endif
8943             default:
8944               gcc_unreachable ();
8945             }
8946         }
8947
8948       nmp = mp->next;
8949       free (mp);
8950     }
8951
8952   minipool_vector_head = minipool_vector_tail = NULL;
8953   scan = emit_insn_after (gen_consttable_end (), scan);
8954   scan = emit_barrier_after (scan);
8955 }
8956
8957 /* Return the cost of forcibly inserting a barrier after INSN.  */
8958 static int
8959 arm_barrier_cost (rtx insn)
8960 {
8961   /* Basing the location of the pool on the loop depth is preferable,
8962      but at the moment, the basic block information seems to be
8963      corrupt by this stage of the compilation.  */
8964   int base_cost = 50;
8965   rtx next = next_nonnote_insn (insn);
8966
8967   if (next != NULL && GET_CODE (next) == CODE_LABEL)
8968     base_cost -= 20;
8969
8970   switch (GET_CODE (insn))
8971     {
8972     case CODE_LABEL:
8973       /* It will always be better to place the table before the label, rather
8974          than after it.  */
8975       return 50;
8976
8977     case INSN:
8978     case CALL_INSN:
8979       return base_cost;
8980
8981     case JUMP_INSN:
8982       return base_cost - 10;
8983
8984     default:
8985       return base_cost + 10;
8986     }
8987 }
8988
8989 /* Find the best place in the insn stream in the range
8990    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
8991    Create the barrier by inserting a jump and add a new fix entry for
8992    it.  */
8993 static Mfix *
8994 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
8995 {
8996   HOST_WIDE_INT count = 0;
8997   rtx barrier;
8998   rtx from = fix->insn;
8999   /* The instruction after which we will insert the jump.  */
9000   rtx selected = NULL;
9001   int selected_cost;
9002   /* The address at which the jump instruction will be placed.  */
9003   HOST_WIDE_INT selected_address;
9004   Mfix * new_fix;
9005   HOST_WIDE_INT max_count = max_address - fix->address;
9006   rtx label = gen_label_rtx ();
9007
9008   selected_cost = arm_barrier_cost (from);
9009   selected_address = fix->address;
9010
9011   while (from && count < max_count)
9012     {
9013       rtx tmp;
9014       int new_cost;
9015
9016       /* This code shouldn't have been called if there was a natural barrier
9017          within range.  */
9018       gcc_assert (GET_CODE (from) != BARRIER);
9019
9020       /* Count the length of this insn.  */
9021       count += get_attr_length (from);
9022
9023       /* If there is a jump table, add its length.  */
9024       tmp = is_jump_table (from);
9025       if (tmp != NULL)
9026         {
9027           count += get_jump_table_size (tmp);
9028
9029           /* Jump tables aren't in a basic block, so base the cost on
9030              the dispatch insn.  If we select this location, we will
9031              still put the pool after the table.  */
9032           new_cost = arm_barrier_cost (from);
9033
9034           if (count < max_count 
9035               && (!selected || new_cost <= selected_cost))
9036             {
9037               selected = tmp;
9038               selected_cost = new_cost;
9039               selected_address = fix->address + count;
9040             }
9041
9042           /* Continue after the dispatch table.  */
9043           from = NEXT_INSN (tmp);
9044           continue;
9045         }
9046
9047       new_cost = arm_barrier_cost (from);
9048
9049       if (count < max_count
9050           && (!selected || new_cost <= selected_cost))
9051         {
9052           selected = from;
9053           selected_cost = new_cost;
9054           selected_address = fix->address + count;
9055         }
9056
9057       from = NEXT_INSN (from);
9058     }
9059
9060   /* Make sure that we found a place to insert the jump.  */
9061   gcc_assert (selected);
9062
9063   /* Create a new JUMP_INSN that branches around a barrier.  */
9064   from = emit_jump_insn_after (gen_jump (label), selected);
9065   JUMP_LABEL (from) = label;
9066   barrier = emit_barrier_after (from);
9067   emit_label_after (label, barrier);
9068
9069   /* Create a minipool barrier entry for the new barrier.  */
9070   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
9071   new_fix->insn = barrier;
9072   new_fix->address = selected_address;
9073   new_fix->next = fix->next;
9074   fix->next = new_fix;
9075
9076   return new_fix;
9077 }
9078
9079 /* Record that there is a natural barrier in the insn stream at
9080    ADDRESS.  */
9081 static void
9082 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
9083 {
9084   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9085
9086   fix->insn = insn;
9087   fix->address = address;
9088
9089   fix->next = NULL;
9090   if (minipool_fix_head != NULL)
9091     minipool_fix_tail->next = fix;
9092   else
9093     minipool_fix_head = fix;
9094
9095   minipool_fix_tail = fix;
9096 }
9097
9098 /* Record INSN, which will need fixing up to load a value from the
9099    minipool.  ADDRESS is the offset of the insn since the start of the
9100    function; LOC is a pointer to the part of the insn which requires
9101    fixing; VALUE is the constant that must be loaded, which is of type
9102    MODE.  */
9103 static void
9104 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
9105                    enum machine_mode mode, rtx value)
9106 {
9107   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
9108
9109 #ifdef AOF_ASSEMBLER
9110   /* PIC symbol references need to be converted into offsets into the
9111      based area.  */
9112   /* XXX This shouldn't be done here.  */
9113   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
9114     value = aof_pic_entry (value);
9115 #endif /* AOF_ASSEMBLER */
9116
9117   fix->insn = insn;
9118   fix->address = address;
9119   fix->loc = loc;
9120   fix->mode = mode;
9121   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
9122   fix->value = value;
9123   fix->forwards = get_attr_pool_range (insn);
9124   fix->backwards = get_attr_neg_pool_range (insn);
9125   fix->minipool = NULL;
9126
9127   /* If an insn doesn't have a range defined for it, then it isn't
9128      expecting to be reworked by this code.  Better to stop now than
9129      to generate duff assembly code.  */
9130   gcc_assert (fix->forwards || fix->backwards);
9131
9132   /* If an entry requires 8-byte alignment then assume all constant pools
9133      require 4 bytes of padding.  Trying to do this later on a per-pool
9134      basis is awkward because existing pool entries have to be modified.  */
9135   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size >= 8)
9136     minipool_pad = 4;
9137
9138   if (dump_file)
9139     {
9140       fprintf (dump_file,
9141                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
9142                GET_MODE_NAME (mode),
9143                INSN_UID (insn), (unsigned long) address,
9144                -1 * (long)fix->backwards, (long)fix->forwards);
9145       arm_print_value (dump_file, fix->value);
9146       fprintf (dump_file, "\n");
9147     }
9148
9149   /* Add it to the chain of fixes.  */
9150   fix->next = NULL;
9151
9152   if (minipool_fix_head != NULL)
9153     minipool_fix_tail->next = fix;
9154   else
9155     minipool_fix_head = fix;
9156
9157   minipool_fix_tail = fix;
9158 }
9159
9160 /* Return the cost of synthesizing a 64-bit constant VAL inline.
9161    Returns the number of insns needed, or 99 if we don't know how to
9162    do it.  */
9163 int
9164 arm_const_double_inline_cost (rtx val)
9165 {
9166   rtx lowpart, highpart;
9167   enum machine_mode mode;
9168
9169   mode = GET_MODE (val);
9170
9171   if (mode == VOIDmode)
9172     mode = DImode;
9173
9174   gcc_assert (GET_MODE_SIZE (mode) == 8);
9175
9176   lowpart = gen_lowpart (SImode, val);
9177   highpart = gen_highpart_mode (SImode, mode, val);
9178
9179   gcc_assert (GET_CODE (lowpart) == CONST_INT);
9180   gcc_assert (GET_CODE (highpart) == CONST_INT);
9181
9182   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
9183                             NULL_RTX, NULL_RTX, 0, 0)
9184           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
9185                               NULL_RTX, NULL_RTX, 0, 0));
9186 }
9187
9188 /* Return true if it is worthwhile to split a 64-bit constant into two
9189    32-bit operations.  This is the case if optimizing for size, or
9190    if we have load delay slots, or if one 32-bit part can be done with
9191    a single data operation.  */
9192 bool
9193 arm_const_double_by_parts (rtx val)
9194 {
9195   enum machine_mode mode = GET_MODE (val);
9196   rtx part;
9197
9198   if (optimize_size || arm_ld_sched)
9199     return true;
9200
9201   if (mode == VOIDmode)
9202     mode = DImode;
9203
9204   part = gen_highpart_mode (SImode, mode, val);
9205
9206   gcc_assert (GET_CODE (part) == CONST_INT);
9207
9208   if (const_ok_for_arm (INTVAL (part))
9209       || const_ok_for_arm (~INTVAL (part)))
9210     return true;
9211
9212   part = gen_lowpart (SImode, val);
9213
9214   gcc_assert (GET_CODE (part) == CONST_INT);
9215
9216   if (const_ok_for_arm (INTVAL (part))
9217       || const_ok_for_arm (~INTVAL (part)))
9218     return true;
9219
9220   return false;
9221 }
9222
9223 /* Scan INSN and note any of its operands that need fixing.
9224    If DO_PUSHES is false we do not actually push any of the fixups
9225    needed.  The function returns TRUE if any fixups were needed/pushed.
9226    This is used by arm_memory_load_p() which needs to know about loads
9227    of constants that will be converted into minipool loads.  */
9228 static bool
9229 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
9230 {
9231   bool result = false;
9232   int opno;
9233
9234   extract_insn (insn);
9235
9236   if (!constrain_operands (1))
9237     fatal_insn_not_found (insn);
9238
9239   if (recog_data.n_alternatives == 0)
9240     return false;
9241
9242   /* Fill in recog_op_alt with information about the constraints of
9243      this insn.  */
9244   preprocess_constraints ();
9245
9246   for (opno = 0; opno < recog_data.n_operands; opno++)
9247     {
9248       /* Things we need to fix can only occur in inputs.  */
9249       if (recog_data.operand_type[opno] != OP_IN)
9250         continue;
9251
9252       /* If this alternative is a memory reference, then any mention
9253          of constants in this alternative is really to fool reload
9254          into allowing us to accept one there.  We need to fix them up
9255          now so that we output the right code.  */
9256       if (recog_op_alt[opno][which_alternative].memory_ok)
9257         {
9258           rtx op = recog_data.operand[opno];
9259
9260           if (CONSTANT_P (op))
9261             {
9262               if (do_pushes)
9263                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
9264                                    recog_data.operand_mode[opno], op);
9265               result = true;
9266             }
9267           else if (GET_CODE (op) == MEM
9268                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
9269                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
9270             {
9271               if (do_pushes)
9272                 {
9273                   rtx cop = avoid_constant_pool_reference (op);
9274
9275                   /* Casting the address of something to a mode narrower
9276                      than a word can cause avoid_constant_pool_reference()
9277                      to return the pool reference itself.  That's no good to
9278                      us here.  Lets just hope that we can use the
9279                      constant pool value directly.  */
9280                   if (op == cop)
9281                     cop = get_pool_constant (XEXP (op, 0));
9282
9283                   push_minipool_fix (insn, address,
9284                                      recog_data.operand_loc[opno],
9285                                      recog_data.operand_mode[opno], cop);
9286                 }
9287
9288               result = true;
9289             }
9290         }
9291     }
9292
9293   return result;
9294 }
9295
9296 /* Gcc puts the pool in the wrong place for ARM, since we can only
9297    load addresses a limited distance around the pc.  We do some
9298    special munging to move the constant pool values to the correct
9299    point in the code.  */
9300 static void
9301 arm_reorg (void)
9302 {
9303   rtx insn;
9304   HOST_WIDE_INT address = 0;
9305   Mfix * fix;
9306
9307   minipool_fix_head = minipool_fix_tail = NULL;
9308
9309   /* The first insn must always be a note, or the code below won't
9310      scan it properly.  */
9311   insn = get_insns ();
9312   gcc_assert (GET_CODE (insn) == NOTE);
9313   minipool_pad = 0;
9314
9315   /* Scan all the insns and record the operands that will need fixing.  */
9316   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
9317     {
9318       if (TARGET_CIRRUS_FIX_INVALID_INSNS
9319           && (arm_cirrus_insn_p (insn)
9320               || GET_CODE (insn) == JUMP_INSN
9321               || arm_memory_load_p (insn)))
9322         cirrus_reorg (insn);
9323
9324       if (GET_CODE (insn) == BARRIER)
9325         push_minipool_barrier (insn, address);
9326       else if (INSN_P (insn))
9327         {
9328           rtx table;
9329
9330           note_invalid_constants (insn, address, true);
9331           address += get_attr_length (insn);
9332
9333           /* If the insn is a vector jump, add the size of the table
9334              and skip the table.  */
9335           if ((table = is_jump_table (insn)) != NULL)
9336             {
9337               address += get_jump_table_size (table);
9338               insn = table;
9339             }
9340         }
9341     }
9342
9343   fix = minipool_fix_head;
9344
9345   /* Now scan the fixups and perform the required changes.  */
9346   while (fix)
9347     {
9348       Mfix * ftmp;
9349       Mfix * fdel;
9350       Mfix *  last_added_fix;
9351       Mfix * last_barrier = NULL;
9352       Mfix * this_fix;
9353
9354       /* Skip any further barriers before the next fix.  */
9355       while (fix && GET_CODE (fix->insn) == BARRIER)
9356         fix = fix->next;
9357
9358       /* No more fixes.  */
9359       if (fix == NULL)
9360         break;
9361
9362       last_added_fix = NULL;
9363
9364       for (ftmp = fix; ftmp; ftmp = ftmp->next)
9365         {
9366           if (GET_CODE (ftmp->insn) == BARRIER)
9367             {
9368               if (ftmp->address >= minipool_vector_head->max_address)
9369                 break;
9370
9371               last_barrier = ftmp;
9372             }
9373           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
9374             break;
9375
9376           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
9377         }
9378
9379       /* If we found a barrier, drop back to that; any fixes that we
9380          could have reached but come after the barrier will now go in
9381          the next mini-pool.  */
9382       if (last_barrier != NULL)
9383         {
9384           /* Reduce the refcount for those fixes that won't go into this
9385              pool after all.  */
9386           for (fdel = last_barrier->next;
9387                fdel && fdel != ftmp;
9388                fdel = fdel->next)
9389             {
9390               fdel->minipool->refcount--;
9391               fdel->minipool = NULL;
9392             }
9393
9394           ftmp = last_barrier;
9395         }
9396       else
9397         {
9398           /* ftmp is first fix that we can't fit into this pool and
9399              there no natural barriers that we could use.  Insert a
9400              new barrier in the code somewhere between the previous
9401              fix and this one, and arrange to jump around it.  */
9402           HOST_WIDE_INT max_address;
9403
9404           /* The last item on the list of fixes must be a barrier, so
9405              we can never run off the end of the list of fixes without
9406              last_barrier being set.  */
9407           gcc_assert (ftmp);
9408
9409           max_address = minipool_vector_head->max_address;
9410           /* Check that there isn't another fix that is in range that
9411              we couldn't fit into this pool because the pool was
9412              already too large: we need to put the pool before such an
9413              instruction.  The pool itself may come just after the
9414              fix because create_fix_barrier also allows space for a
9415              jump instruction.  */
9416           if (ftmp->address < max_address)
9417             max_address = ftmp->address + 1;
9418
9419           last_barrier = create_fix_barrier (last_added_fix, max_address);
9420         }
9421
9422       assign_minipool_offsets (last_barrier);
9423
9424       while (ftmp)
9425         {
9426           if (GET_CODE (ftmp->insn) != BARRIER
9427               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
9428                   == NULL))
9429             break;
9430
9431           ftmp = ftmp->next;
9432         }
9433
9434       /* Scan over the fixes we have identified for this pool, fixing them
9435          up and adding the constants to the pool itself.  */
9436       for (this_fix = fix; this_fix && ftmp != this_fix;
9437            this_fix = this_fix->next)
9438         if (GET_CODE (this_fix->insn) != BARRIER)
9439           {
9440             rtx addr
9441               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
9442                                                   minipool_vector_label),
9443                                this_fix->minipool->offset);
9444             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
9445           }
9446
9447       dump_minipool (last_barrier->insn);
9448       fix = ftmp;
9449     }
9450
9451   /* From now on we must synthesize any constants that we can't handle
9452      directly.  This can happen if the RTL gets split during final
9453      instruction generation.  */
9454   after_arm_reorg = 1;
9455
9456   /* Free the minipool memory.  */
9457   obstack_free (&minipool_obstack, minipool_startobj);
9458 }
9459 \f
9460 /* Routines to output assembly language.  */
9461
9462 /* If the rtx is the correct value then return the string of the number.
9463    In this way we can ensure that valid double constants are generated even
9464    when cross compiling.  */
9465 const char *
9466 fp_immediate_constant (rtx x)
9467 {
9468   REAL_VALUE_TYPE r;
9469   int i;
9470
9471   if (!fp_consts_inited)
9472     init_fp_table ();
9473
9474   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9475   for (i = 0; i < 8; i++)
9476     if (REAL_VALUES_EQUAL (r, values_fp[i]))
9477       return strings_fp[i];
9478
9479   gcc_unreachable ();
9480 }
9481
9482 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
9483 static const char *
9484 fp_const_from_val (REAL_VALUE_TYPE *r)
9485 {
9486   int i;
9487
9488   if (!fp_consts_inited)
9489     init_fp_table ();
9490
9491   for (i = 0; i < 8; i++)
9492     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
9493       return strings_fp[i];
9494
9495   gcc_unreachable ();
9496 }
9497
9498 /* Output the operands of a LDM/STM instruction to STREAM.
9499    MASK is the ARM register set mask of which only bits 0-15 are important.
9500    REG is the base register, either the frame pointer or the stack pointer,
9501    INSTR is the possibly suffixed load or store instruction.
9502    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
9503
9504 static void
9505 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
9506                  unsigned long mask, int rfe)
9507 {
9508   unsigned i;
9509   bool not_first = FALSE;
9510
9511   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
9512   fputc ('\t', stream);
9513   asm_fprintf (stream, instr, reg);
9514   fputc ('{', stream);
9515
9516   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9517     if (mask & (1 << i))
9518       {
9519         if (not_first)
9520           fprintf (stream, ", ");
9521
9522         asm_fprintf (stream, "%r", i);
9523         not_first = TRUE;
9524       }
9525
9526   if (rfe)
9527     fprintf (stream, "}^\n");
9528   else
9529     fprintf (stream, "}\n");
9530 }
9531
9532
9533 /* Output a FLDMD instruction to STREAM.
9534    BASE if the register containing the address.
9535    REG and COUNT specify the register range.
9536    Extra registers may be added to avoid hardware bugs.
9537
9538    We output FLDMD even for ARMv5 VFP implementations.  Although
9539    FLDMD is technically not supported until ARMv6, it is believed
9540    that all VFP implementations support its use in this context.  */
9541
9542 static void
9543 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
9544 {
9545   int i;
9546
9547   /* Workaround ARM10 VFPr1 bug.  */
9548   if (count == 2 && !arm_arch6)
9549     {
9550       if (reg == 15)
9551         reg--;
9552       count++;
9553     }
9554
9555   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
9556      load into multiple parts if we have to handle more than 16 registers.  */
9557   if (count > 16)
9558     {
9559       vfp_output_fldmd (stream, base, reg, 16);
9560       vfp_output_fldmd (stream, base, reg + 16, count - 16);
9561       return;
9562     }
9563
9564   fputc ('\t', stream);
9565   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9566
9567   for (i = reg; i < reg + count; i++)
9568     {
9569       if (i > reg)
9570         fputs (", ", stream);
9571       asm_fprintf (stream, "d%d", i);
9572     }
9573   fputs ("}\n", stream);
9574
9575 }
9576
9577
9578 /* Output the assembly for a store multiple.  */
9579
9580 const char *
9581 vfp_output_fstmd (rtx * operands)
9582 {
9583   char pattern[100];
9584   int p;
9585   int base;
9586   int i;
9587
9588   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
9589   p = strlen (pattern);
9590
9591   gcc_assert (GET_CODE (operands[1]) == REG);
9592
9593   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
9594   for (i = 1; i < XVECLEN (operands[2], 0); i++)
9595     {
9596       p += sprintf (&pattern[p], ", d%d", base + i);
9597     }
9598   strcpy (&pattern[p], "}");
9599
9600   output_asm_insn (pattern, operands);
9601   return "";
9602 }
9603
9604
9605 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
9606    number of bytes pushed.  */
9607
9608 static int
9609 vfp_emit_fstmd (int base_reg, int count)
9610 {
9611   rtx par;
9612   rtx dwarf;
9613   rtx tmp, reg;
9614   int i;
9615
9616   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
9617      register pairs are stored by a store multiple insn.  We avoid this
9618      by pushing an extra pair.  */
9619   if (count == 2 && !arm_arch6)
9620     {
9621       if (base_reg == LAST_VFP_REGNUM - 3)
9622         base_reg -= 2;
9623       count++;
9624     }
9625
9626   /* FSTMD may not store more than 16 doubleword registers at once.  Split
9627      larger stores into multiple parts (up to a maximum of two, in
9628      practice).  */
9629   if (count > 16)
9630     {
9631       int saved;
9632       /* NOTE: base_reg is an internal register number, so each D register
9633          counts as 2.  */
9634       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
9635       saved += vfp_emit_fstmd (base_reg, 16);
9636       return saved;
9637     }
9638
9639   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9640   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9641
9642   reg = gen_rtx_REG (DFmode, base_reg);
9643   base_reg += 2;
9644
9645   XVECEXP (par, 0, 0)
9646     = gen_rtx_SET (VOIDmode,
9647                    gen_frame_mem (BLKmode,
9648                                   gen_rtx_PRE_DEC (BLKmode,
9649                                                    stack_pointer_rtx)),
9650                    gen_rtx_UNSPEC (BLKmode,
9651                                    gen_rtvec (1, reg),
9652                                    UNSPEC_PUSH_MULT));
9653
9654   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9655                      plus_constant (stack_pointer_rtx, -(count * 8)));
9656   RTX_FRAME_RELATED_P (tmp) = 1;
9657   XVECEXP (dwarf, 0, 0) = tmp;
9658
9659   tmp = gen_rtx_SET (VOIDmode,
9660                      gen_frame_mem (DFmode, stack_pointer_rtx),
9661                      reg);
9662   RTX_FRAME_RELATED_P (tmp) = 1;
9663   XVECEXP (dwarf, 0, 1) = tmp;
9664
9665   for (i = 1; i < count; i++)
9666     {
9667       reg = gen_rtx_REG (DFmode, base_reg);
9668       base_reg += 2;
9669       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9670
9671       tmp = gen_rtx_SET (VOIDmode,
9672                          gen_frame_mem (DFmode,
9673                                         plus_constant (stack_pointer_rtx,
9674                                                        i * 8)),
9675                          reg);
9676       RTX_FRAME_RELATED_P (tmp) = 1;
9677       XVECEXP (dwarf, 0, i + 1) = tmp;
9678     }
9679
9680   par = emit_insn (par);
9681   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9682                                        REG_NOTES (par));
9683   RTX_FRAME_RELATED_P (par) = 1;
9684
9685   return count * 8;
9686 }
9687
9688 /* Emit a call instruction with pattern PAT.  ADDR is the address of
9689    the call target.  */
9690
9691 void
9692 arm_emit_call_insn (rtx pat, rtx addr)
9693 {
9694   rtx insn;
9695
9696   insn = emit_call_insn (pat);
9697
9698   /* The PIC register is live on entry to VxWorks PIC PLT entries.
9699      If the call might use such an entry, add a use of the PIC register
9700      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
9701   if (TARGET_VXWORKS_RTP
9702       && flag_pic
9703       && GET_CODE (addr) == SYMBOL_REF
9704       && (SYMBOL_REF_DECL (addr)
9705           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
9706           : !SYMBOL_REF_LOCAL_P (addr)))
9707     {
9708       require_pic_register ();
9709       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
9710     }
9711 }
9712
9713 /* Output a 'call' insn.  */
9714 const char *
9715 output_call (rtx *operands)
9716 {
9717   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
9718
9719   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
9720   if (REGNO (operands[0]) == LR_REGNUM)
9721     {
9722       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
9723       output_asm_insn ("mov%?\t%0, %|lr", operands);
9724     }
9725
9726   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9727
9728   if (TARGET_INTERWORK || arm_arch4t)
9729     output_asm_insn ("bx%?\t%0", operands);
9730   else
9731     output_asm_insn ("mov%?\t%|pc, %0", operands);
9732
9733   return "";
9734 }
9735
9736 /* Output a 'call' insn that is a reference in memory.  */
9737 const char *
9738 output_call_mem (rtx *operands)
9739 {
9740   if (TARGET_INTERWORK && !arm_arch5)
9741     {
9742       output_asm_insn ("ldr%?\t%|ip, %0", operands);
9743       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9744       output_asm_insn ("bx%?\t%|ip", operands);
9745     }
9746   else if (regno_use_in (LR_REGNUM, operands[0]))
9747     {
9748       /* LR is used in the memory address.  We load the address in the
9749          first instruction.  It's safe to use IP as the target of the
9750          load since the call will kill it anyway.  */
9751       output_asm_insn ("ldr%?\t%|ip, %0", operands);
9752       if (arm_arch5)
9753         output_asm_insn ("blx%?\t%|ip", operands);
9754       else
9755         {
9756           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9757           if (arm_arch4t)
9758             output_asm_insn ("bx%?\t%|ip", operands);
9759           else
9760             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
9761         }
9762     }
9763   else
9764     {
9765       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9766       output_asm_insn ("ldr%?\t%|pc, %0", operands);
9767     }
9768
9769   return "";
9770 }
9771
9772
9773 /* Output a move from arm registers to an fpa registers.
9774    OPERANDS[0] is an fpa register.
9775    OPERANDS[1] is the first registers of an arm register pair.  */
9776 const char *
9777 output_mov_long_double_fpa_from_arm (rtx *operands)
9778 {
9779   int arm_reg0 = REGNO (operands[1]);
9780   rtx ops[3];
9781
9782   gcc_assert (arm_reg0 != IP_REGNUM);
9783
9784   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9785   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9786   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9787
9788   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9789   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
9790
9791   return "";
9792 }
9793
9794 /* Output a move from an fpa register to arm registers.
9795    OPERANDS[0] is the first registers of an arm register pair.
9796    OPERANDS[1] is an fpa register.  */
9797 const char *
9798 output_mov_long_double_arm_from_fpa (rtx *operands)
9799 {
9800   int arm_reg0 = REGNO (operands[0]);
9801   rtx ops[3];
9802
9803   gcc_assert (arm_reg0 != IP_REGNUM);
9804
9805   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9806   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9807   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9808
9809   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
9810   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9811   return "";
9812 }
9813
9814 /* Output a move from arm registers to arm registers of a long double
9815    OPERANDS[0] is the destination.
9816    OPERANDS[1] is the source.  */
9817 const char *
9818 output_mov_long_double_arm_from_arm (rtx *operands)
9819 {
9820   /* We have to be careful here because the two might overlap.  */
9821   int dest_start = REGNO (operands[0]);
9822   int src_start = REGNO (operands[1]);
9823   rtx ops[2];
9824   int i;
9825
9826   if (dest_start < src_start)
9827     {
9828       for (i = 0; i < 3; i++)
9829         {
9830           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9831           ops[1] = gen_rtx_REG (SImode, src_start + i);
9832           output_asm_insn ("mov%?\t%0, %1", ops);
9833         }
9834     }
9835   else
9836     {
9837       for (i = 2; i >= 0; i--)
9838         {
9839           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9840           ops[1] = gen_rtx_REG (SImode, src_start + i);
9841           output_asm_insn ("mov%?\t%0, %1", ops);
9842         }
9843     }
9844
9845   return "";
9846 }
9847
9848
9849 /* Output a move from arm registers to an fpa registers.
9850    OPERANDS[0] is an fpa register.
9851    OPERANDS[1] is the first registers of an arm register pair.  */
9852 const char *
9853 output_mov_double_fpa_from_arm (rtx *operands)
9854 {
9855   int arm_reg0 = REGNO (operands[1]);
9856   rtx ops[2];
9857
9858   gcc_assert (arm_reg0 != IP_REGNUM);
9859
9860   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9861   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9862   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
9863   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
9864   return "";
9865 }
9866
9867 /* Output a move from an fpa register to arm registers.
9868    OPERANDS[0] is the first registers of an arm register pair.
9869    OPERANDS[1] is an fpa register.  */
9870 const char *
9871 output_mov_double_arm_from_fpa (rtx *operands)
9872 {
9873   int arm_reg0 = REGNO (operands[0]);
9874   rtx ops[2];
9875
9876   gcc_assert (arm_reg0 != IP_REGNUM);
9877
9878   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9879   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9880   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
9881   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
9882   return "";
9883 }
9884
9885 /* Output a move between double words.
9886    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
9887    or MEM<-REG and all MEMs must be offsettable addresses.  */
9888 const char *
9889 output_move_double (rtx *operands)
9890 {
9891   enum rtx_code code0 = GET_CODE (operands[0]);
9892   enum rtx_code code1 = GET_CODE (operands[1]);
9893   rtx otherops[3];
9894
9895   if (code0 == REG)
9896     {
9897       int reg0 = REGNO (operands[0]);
9898
9899       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9900
9901       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
9902
9903       switch (GET_CODE (XEXP (operands[1], 0)))
9904         {
9905         case REG:
9906           output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
9907           break;
9908
9909         case PRE_INC:
9910           gcc_assert (TARGET_LDRD);
9911           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
9912           break;
9913
9914         case PRE_DEC:
9915           if (TARGET_LDRD)
9916             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
9917           else
9918             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
9919           break;
9920
9921         case POST_INC:
9922           output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
9923           break;
9924
9925         case POST_DEC:
9926           gcc_assert (TARGET_LDRD);
9927           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
9928           break;
9929
9930         case PRE_MODIFY:
9931         case POST_MODIFY:
9932           otherops[0] = operands[0];
9933           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
9934           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
9935
9936           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
9937             {
9938               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9939                 {
9940                   /* Registers overlap so split out the increment.  */
9941                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
9942                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
9943                 }
9944               else
9945                 {
9946                   /* IWMMXT allows offsets larger than ldrd can handle,
9947                      fix these up with a pair of ldr.  */
9948                   if (GET_CODE (otherops[2]) == CONST_INT
9949                       && (INTVAL(otherops[2]) <= -256
9950                           || INTVAL(otherops[2]) >= 256))
9951                     {
9952                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
9953                       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9954                       output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9955                     }
9956                   else
9957                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
9958                 }
9959             }
9960           else
9961             {
9962               /* IWMMXT allows offsets larger than ldrd can handle,
9963                  fix these up with a pair of ldr.  */
9964               if (GET_CODE (otherops[2]) == CONST_INT
9965                   && (INTVAL(otherops[2]) <= -256
9966                       || INTVAL(otherops[2]) >= 256))
9967                 {
9968                   otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9969                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9970                   otherops[0] = operands[0];
9971                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
9972                 }
9973               else
9974                 /* We only allow constant increments, so this is safe.  */
9975                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
9976             }
9977           break;
9978
9979         case LABEL_REF:
9980         case CONST:
9981           output_asm_insn ("adr%?\t%0, %1", operands);
9982           output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
9983           break;
9984
9985           /* ??? This needs checking for thumb2.  */
9986         default:
9987           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
9988                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
9989             {
9990               otherops[0] = operands[0];
9991               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
9992               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
9993
9994               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
9995                 {
9996                   if (GET_CODE (otherops[2]) == CONST_INT)
9997                     {
9998                       switch ((int) INTVAL (otherops[2]))
9999                         {
10000                         case -8:
10001                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
10002                           return "";
10003                         case -4:
10004                           if (TARGET_THUMB2)
10005                             break;
10006                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
10007                           return "";
10008                         case 4:
10009                           if (TARGET_THUMB2)
10010                             break;
10011                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
10012                           return "";
10013                         }
10014                     }
10015                   if (TARGET_LDRD
10016                       && (GET_CODE (otherops[2]) == REG
10017                           || (GET_CODE (otherops[2]) == CONST_INT
10018                               && INTVAL (otherops[2]) > -256
10019                               && INTVAL (otherops[2]) < 256)))
10020                     {
10021                       if (reg_overlap_mentioned_p (otherops[0],
10022                                                    otherops[2]))
10023                         {
10024                           /* Swap base and index registers over to
10025                              avoid a conflict.  */
10026                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
10027                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
10028                         }
10029                       /* If both registers conflict, it will usually
10030                          have been fixed by a splitter.  */
10031                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
10032                         {
10033                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
10034                           output_asm_insn ("ldr%(d%)\t%0, [%1]",
10035                                            otherops);
10036                         }
10037                       else
10038                         output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
10039                       return "";
10040                     }
10041
10042                   if (GET_CODE (otherops[2]) == CONST_INT)
10043                     {
10044                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
10045                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
10046                       else
10047                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
10048                     }
10049                   else
10050                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
10051                 }
10052               else
10053                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
10054
10055               return "ldm%(ia%)\t%0, %M0";
10056             }
10057           else
10058             {
10059               otherops[1] = adjust_address (operands[1], SImode, 4);
10060               /* Take care of overlapping base/data reg.  */
10061               if (reg_mentioned_p (operands[0], operands[1]))
10062                 {
10063                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10064                   output_asm_insn ("ldr%?\t%0, %1", operands);
10065                 }
10066               else
10067                 {
10068                   output_asm_insn ("ldr%?\t%0, %1", operands);
10069                   output_asm_insn ("ldr%?\t%0, %1", otherops);
10070                 }
10071             }
10072         }
10073     }
10074   else
10075     {
10076       /* Constraints should ensure this.  */
10077       gcc_assert (code0 == MEM && code1 == REG);
10078       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
10079
10080       switch (GET_CODE (XEXP (operands[0], 0)))
10081         {
10082         case REG:
10083           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10084           break;
10085
10086         case PRE_INC:
10087           gcc_assert (TARGET_LDRD);
10088           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
10089           break;
10090
10091         case PRE_DEC:
10092           if (TARGET_LDRD)
10093             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
10094           else
10095             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
10096           break;
10097
10098         case POST_INC:
10099           output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
10100           break;
10101
10102         case POST_DEC:
10103           gcc_assert (TARGET_LDRD);
10104           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
10105           break;
10106
10107         case PRE_MODIFY:
10108         case POST_MODIFY:
10109           otherops[0] = operands[1];
10110           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
10111           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
10112
10113           /* IWMMXT allows offsets larger than ldrd can handle,
10114              fix these up with a pair of ldr.  */
10115           if (GET_CODE (otherops[2]) == CONST_INT
10116               && (INTVAL(otherops[2]) <= -256
10117                   || INTVAL(otherops[2]) >= 256))
10118             {
10119               rtx reg1;
10120               reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10121               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10122                 {
10123                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
10124                   otherops[0] = reg1;
10125                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10126                 }
10127               else
10128                 {
10129                   otherops[0] = reg1;
10130                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
10131                   otherops[0] = operands[1];
10132                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
10133                 }
10134             }
10135           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
10136             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
10137           else
10138             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
10139           break;
10140
10141         case PLUS:
10142           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
10143           if (GET_CODE (otherops[2]) == CONST_INT)
10144             {
10145               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
10146                 {
10147                 case -8:
10148                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
10149                   return "";
10150
10151                 case -4:
10152                   if (TARGET_THUMB2)
10153                     break;
10154                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
10155                   return "";
10156
10157                 case 4:
10158                   if (TARGET_THUMB2)
10159                     break;
10160                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
10161                   return "";
10162                 }
10163             }
10164           if (TARGET_LDRD
10165               && (GET_CODE (otherops[2]) == REG
10166                   || (GET_CODE (otherops[2]) == CONST_INT
10167                       && INTVAL (otherops[2]) > -256
10168                       && INTVAL (otherops[2]) < 256)))
10169             {
10170               otherops[0] = operands[1];
10171               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
10172               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
10173               return "";
10174             }
10175           /* Fall through */
10176
10177         default:
10178           otherops[0] = adjust_address (operands[0], SImode, 4);
10179           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
10180           output_asm_insn ("str%?\t%1, %0", operands);
10181           output_asm_insn ("str%?\t%1, %0", otherops);
10182         }
10183     }
10184
10185   return "";
10186 }
10187
10188 /* Output a move, load or store for quad-word vectors in ARM registers.  Only
10189    handles MEMs accepted by neon_vector_mem_operand with CORE=true.  */
10190
10191 const char *
10192 output_move_quad (rtx *operands)
10193 {
10194   if (REG_P (operands[0]))
10195     {
10196       /* Load, or reg->reg move.  */
10197
10198       if (MEM_P (operands[1]))
10199         {
10200           switch (GET_CODE (XEXP (operands[1], 0)))
10201             {
10202             case REG:
10203               output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
10204               break;
10205
10206             case LABEL_REF:
10207             case CONST:
10208               output_asm_insn ("adr%?\t%0, %1", operands);
10209               output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
10210               break;
10211
10212             default:
10213               gcc_unreachable ();
10214             }
10215         }
10216       else
10217         {
10218           rtx ops[2];
10219           int dest, src, i;
10220
10221           gcc_assert (REG_P (operands[1]));
10222
10223           dest = REGNO (operands[0]);
10224           src = REGNO (operands[1]);
10225
10226           /* This seems pretty dumb, but hopefully GCC won't try to do it
10227              very often.  */
10228           if (dest < src)
10229             for (i = 0; i < 4; i++)
10230               {
10231                 ops[0] = gen_rtx_REG (SImode, dest + i);
10232                 ops[1] = gen_rtx_REG (SImode, src + i);
10233                 output_asm_insn ("mov%?\t%0, %1", ops);
10234               }
10235           else
10236             for (i = 3; i >= 0; i--)
10237               {
10238                 ops[0] = gen_rtx_REG (SImode, dest + i);
10239                 ops[1] = gen_rtx_REG (SImode, src + i);
10240                 output_asm_insn ("mov%?\t%0, %1", ops);
10241               }
10242         }
10243     }
10244   else
10245     {
10246       gcc_assert (MEM_P (operands[0]));
10247       gcc_assert (REG_P (operands[1]));
10248       gcc_assert (!reg_overlap_mentioned_p (operands[1], operands[0]));
10249
10250       switch (GET_CODE (XEXP (operands[0], 0)))
10251         {
10252         case REG:
10253           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
10254           break;
10255
10256         default:
10257           gcc_unreachable ();
10258         }
10259     }
10260
10261   return "";
10262 }
10263
10264 /* Output a VFP load or store instruction.  */
10265
10266 const char *
10267 output_move_vfp (rtx *operands)
10268 {
10269   rtx reg, mem, addr, ops[2];
10270   int load = REG_P (operands[0]);
10271   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
10272   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
10273   const char *template;
10274   char buff[50];
10275   enum machine_mode mode;
10276
10277   reg = operands[!load];
10278   mem = operands[load];
10279
10280   mode = GET_MODE (reg);
10281
10282   gcc_assert (REG_P (reg));
10283   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
10284   gcc_assert (mode == SFmode
10285               || mode == DFmode
10286               || mode == SImode
10287               || mode == DImode
10288               || (TARGET_NEON && VALID_NEON_DREG_MODE (mode)));
10289   gcc_assert (MEM_P (mem));
10290
10291   addr = XEXP (mem, 0);
10292
10293   switch (GET_CODE (addr))
10294     {
10295     case PRE_DEC:
10296       template = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
10297       ops[0] = XEXP (addr, 0);
10298       ops[1] = reg;
10299       break;
10300
10301     case POST_INC:
10302       template = "f%smia%c%%?\t%%0!, {%%%s1}%s";
10303       ops[0] = XEXP (addr, 0);
10304       ops[1] = reg;
10305       break;
10306
10307     default:
10308       template = "f%s%c%%?\t%%%s0, %%1%s";
10309       ops[0] = reg;
10310       ops[1] = mem;
10311       break;
10312     }
10313
10314   sprintf (buff, template,
10315            load ? "ld" : "st",
10316            dp ? 'd' : 's',
10317            dp ? "P" : "",
10318            integer_p ? "\t%@ int" : "");
10319   output_asm_insn (buff, ops);
10320
10321   return "";
10322 }
10323
10324 /* Output a Neon quad-word load or store, or a load or store for
10325    larger structure modes. We could also support post-modify forms using
10326    VLD1/VST1 (for the vectorizer, and perhaps otherwise), but we don't do that
10327    yet.
10328    WARNING: The ordering of elements in memory is weird in big-endian mode,
10329    because we use VSTM instead of VST1, to make it easy to make vector stores
10330    via ARM registers write values in the same order as stores direct from Neon
10331    registers.  For example, the byte ordering of a quadword vector with 16-byte
10332    elements like this:
10333
10334      [e7:e6:e5:e4:e3:e2:e1:e0]  (highest-numbered element first)
10335
10336    will be (with lowest address first, h = most-significant byte,
10337    l = least-significant byte of element):
10338
10339      [e3h, e3l, e2h, e2l, e1h, e1l, e0h, e0l,
10340       e7h, e7l, e6h, e6l, e5h, e5l, e4h, e4l]
10341
10342    When necessary, quadword registers (dN, dN+1) are moved to ARM registers from
10343    rN in the order:
10344
10345      dN -> (rN+1, rN), dN+1 -> (rN+3, rN+2)
10346
10347    So that STM/LDM can be used on vectors in ARM registers, and the same memory
10348    layout will result as if VSTM/VLDM were used.  */
10349
10350 const char *
10351 output_move_neon (rtx *operands)
10352 {
10353   rtx reg, mem, addr, ops[2];
10354   int regno, load = REG_P (operands[0]);
10355   const char *template;
10356   char buff[50];
10357   enum machine_mode mode;
10358
10359   reg = operands[!load];
10360   mem = operands[load];
10361
10362   mode = GET_MODE (reg);
10363
10364   gcc_assert (REG_P (reg));
10365   regno = REGNO (reg);
10366   gcc_assert (VFP_REGNO_OK_FOR_DOUBLE (regno)
10367               || NEON_REGNO_OK_FOR_QUAD (regno));
10368   gcc_assert (VALID_NEON_DREG_MODE (mode)
10369               || VALID_NEON_QREG_MODE (mode)
10370               || VALID_NEON_STRUCT_MODE (mode));
10371   gcc_assert (MEM_P (mem));
10372
10373   addr = XEXP (mem, 0);
10374
10375   /* Strip off const from addresses like (const (plus (...))).  */
10376   if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS)
10377     addr = XEXP (addr, 0);
10378
10379   switch (GET_CODE (addr))
10380     {
10381     case POST_INC:
10382       template = "v%smia%%?\t%%0!, %%h1";
10383       ops[0] = XEXP (addr, 0);
10384       ops[1] = reg;
10385       break;
10386
10387     case POST_MODIFY:
10388       /* FIXME: Not currently enabled in neon_vector_mem_operand.  */
10389       gcc_unreachable ();
10390
10391     case LABEL_REF:
10392     case PLUS:
10393       {
10394         int nregs = HARD_REGNO_NREGS (REGNO (reg), mode) / 2;
10395         int i;
10396         int overlap = -1;
10397         for (i = 0; i < nregs; i++)
10398           {
10399             /* We're only using DImode here because it's a convenient size.  */
10400             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * i);
10401             ops[1] = adjust_address (mem, SImode, 8 * i);
10402             if (reg_overlap_mentioned_p (ops[0], mem))
10403               {
10404                 gcc_assert (overlap == -1);
10405                 overlap = i;
10406               }
10407             else
10408               {
10409                 sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10410                 output_asm_insn (buff, ops);
10411               }
10412           }
10413         if (overlap != -1)
10414           {
10415             ops[0] = gen_rtx_REG (DImode, REGNO (reg) + 2 * overlap);
10416             ops[1] = adjust_address (mem, SImode, 8 * overlap);
10417             sprintf (buff, "v%sr%%?\t%%P0, %%1", load ? "ld" : "st");
10418             output_asm_insn (buff, ops);
10419           }
10420
10421         return "";
10422       }
10423
10424     default:
10425       template = "v%smia%%?\t%%m0, %%h1";
10426       ops[0] = mem;
10427       ops[1] = reg;
10428     }
10429
10430   sprintf (buff, template, load ? "ld" : "st");
10431   output_asm_insn (buff, ops);
10432
10433   return "";
10434 }
10435
10436 /* Output an ADD r, s, #n where n may be too big for one instruction.
10437    If adding zero to one register, output nothing.  */
10438 const char *
10439 output_add_immediate (rtx *operands)
10440 {
10441   HOST_WIDE_INT n = INTVAL (operands[2]);
10442
10443   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
10444     {
10445       if (n < 0)
10446         output_multi_immediate (operands,
10447                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
10448                                 -n);
10449       else
10450         output_multi_immediate (operands,
10451                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
10452                                 n);
10453     }
10454
10455   return "";
10456 }
10457
10458 /* Output a multiple immediate operation.
10459    OPERANDS is the vector of operands referred to in the output patterns.
10460    INSTR1 is the output pattern to use for the first constant.
10461    INSTR2 is the output pattern to use for subsequent constants.
10462    IMMED_OP is the index of the constant slot in OPERANDS.
10463    N is the constant value.  */
10464 static const char *
10465 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
10466                         int immed_op, HOST_WIDE_INT n)
10467 {
10468 #if HOST_BITS_PER_WIDE_INT > 32
10469   n &= 0xffffffff;
10470 #endif
10471
10472   if (n == 0)
10473     {
10474       /* Quick and easy output.  */
10475       operands[immed_op] = const0_rtx;
10476       output_asm_insn (instr1, operands);
10477     }
10478   else
10479     {
10480       int i;
10481       const char * instr = instr1;
10482
10483       /* Note that n is never zero here (which would give no output).  */
10484       for (i = 0; i < 32; i += 2)
10485         {
10486           if (n & (3 << i))
10487             {
10488               operands[immed_op] = GEN_INT (n & (255 << i));
10489               output_asm_insn (instr, operands);
10490               instr = instr2;
10491               i += 6;
10492             }
10493         }
10494     }
10495
10496   return "";
10497 }
10498
10499 /* Return the name of a shifter operation.  */
10500 static const char *
10501 arm_shift_nmem(enum rtx_code code)
10502 {
10503   switch (code)
10504     {
10505     case ASHIFT:
10506       return ARM_LSL_NAME;
10507
10508     case ASHIFTRT:
10509       return "asr";
10510
10511     case LSHIFTRT:
10512       return "lsr";
10513
10514     case ROTATERT:
10515       return "ror";
10516
10517     default:
10518       abort();
10519     }
10520 }
10521
10522 /* Return the appropriate ARM instruction for the operation code.
10523    The returned result should not be overwritten.  OP is the rtx of the
10524    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
10525    was shifted.  */
10526 const char *
10527 arithmetic_instr (rtx op, int shift_first_arg)
10528 {
10529   switch (GET_CODE (op))
10530     {
10531     case PLUS:
10532       return "add";
10533
10534     case MINUS:
10535       return shift_first_arg ? "rsb" : "sub";
10536
10537     case IOR:
10538       return "orr";
10539
10540     case XOR:
10541       return "eor";
10542
10543     case AND:
10544       return "and";
10545
10546     case ASHIFT:
10547     case ASHIFTRT:
10548     case LSHIFTRT:
10549     case ROTATERT:
10550       return arm_shift_nmem(GET_CODE(op));
10551
10552     default:
10553       gcc_unreachable ();
10554     }
10555 }
10556
10557 /* Ensure valid constant shifts and return the appropriate shift mnemonic
10558    for the operation code.  The returned result should not be overwritten.
10559    OP is the rtx code of the shift.
10560    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
10561    shift.  */
10562 static const char *
10563 shift_op (rtx op, HOST_WIDE_INT *amountp)
10564 {
10565   const char * mnem;
10566   enum rtx_code code = GET_CODE (op);
10567
10568   switch (GET_CODE (XEXP (op, 1)))
10569     {
10570     case REG:
10571     case SUBREG:
10572       *amountp = -1;
10573       break;
10574
10575     case CONST_INT:
10576       *amountp = INTVAL (XEXP (op, 1));
10577       break;
10578
10579     default:
10580       gcc_unreachable ();
10581     }
10582
10583   switch (code)
10584     {
10585     case ROTATE:
10586       gcc_assert (*amountp != -1);
10587       *amountp = 32 - *amountp;
10588       code = ROTATERT;
10589
10590       /* Fall through.  */
10591
10592     case ASHIFT:
10593     case ASHIFTRT:
10594     case LSHIFTRT:
10595     case ROTATERT:
10596       mnem = arm_shift_nmem(code);
10597       break;
10598
10599     case MULT:
10600       /* We never have to worry about the amount being other than a
10601          power of 2, since this case can never be reloaded from a reg.  */
10602       gcc_assert (*amountp != -1);
10603       *amountp = int_log2 (*amountp);
10604       return ARM_LSL_NAME;
10605
10606     default:
10607       gcc_unreachable ();
10608     }
10609
10610   if (*amountp != -1)
10611     {
10612       /* This is not 100% correct, but follows from the desire to merge
10613          multiplication by a power of 2 with the recognizer for a
10614          shift.  >=32 is not a valid shift for "lsl", so we must try and
10615          output a shift that produces the correct arithmetical result.
10616          Using lsr #32 is identical except for the fact that the carry bit
10617          is not set correctly if we set the flags; but we never use the
10618          carry bit from such an operation, so we can ignore that.  */
10619       if (code == ROTATERT)
10620         /* Rotate is just modulo 32.  */
10621         *amountp &= 31;
10622       else if (*amountp != (*amountp & 31))
10623         {
10624           if (code == ASHIFT)
10625             mnem = "lsr";
10626           *amountp = 32;
10627         }
10628
10629       /* Shifts of 0 are no-ops.  */
10630       if (*amountp == 0)
10631         return NULL;
10632     }
10633
10634   return mnem;
10635 }
10636
10637 /* Obtain the shift from the POWER of two.  */
10638
10639 static HOST_WIDE_INT
10640 int_log2 (HOST_WIDE_INT power)
10641 {
10642   HOST_WIDE_INT shift = 0;
10643
10644   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
10645     {
10646       gcc_assert (shift <= 31);
10647       shift++;
10648     }
10649
10650   return shift;
10651 }
10652
10653 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
10654    because /bin/as is horribly restrictive.  The judgement about
10655    whether or not each character is 'printable' (and can be output as
10656    is) or not (and must be printed with an octal escape) must be made
10657    with reference to the *host* character set -- the situation is
10658    similar to that discussed in the comments above pp_c_char in
10659    c-pretty-print.c.  */
10660
10661 #define MAX_ASCII_LEN 51
10662
10663 void
10664 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
10665 {
10666   int i;
10667   int len_so_far = 0;
10668
10669   fputs ("\t.ascii\t\"", stream);
10670
10671   for (i = 0; i < len; i++)
10672     {
10673       int c = p[i];
10674
10675       if (len_so_far >= MAX_ASCII_LEN)
10676         {
10677           fputs ("\"\n\t.ascii\t\"", stream);
10678           len_so_far = 0;
10679         }
10680
10681       if (ISPRINT (c))
10682         {
10683           if (c == '\\' || c == '\"')
10684             {
10685               putc ('\\', stream);
10686               len_so_far++;
10687             }
10688           putc (c, stream);
10689           len_so_far++;
10690         }
10691       else
10692         {
10693           fprintf (stream, "\\%03o", c);
10694           len_so_far += 4;
10695         }
10696     }
10697
10698   fputs ("\"\n", stream);
10699 }
10700 \f
10701 /* Compute the register save mask for registers 0 through 12
10702    inclusive.  This code is used by arm_compute_save_reg_mask.  */
10703
10704 static unsigned long
10705 arm_compute_save_reg0_reg12_mask (void)
10706 {
10707   unsigned long func_type = arm_current_func_type ();
10708   unsigned long save_reg_mask = 0;
10709   unsigned int reg;
10710
10711   if (IS_INTERRUPT (func_type))
10712     {
10713       unsigned int max_reg;
10714       /* Interrupt functions must not corrupt any registers,
10715          even call clobbered ones.  If this is a leaf function
10716          we can just examine the registers used by the RTL, but
10717          otherwise we have to assume that whatever function is
10718          called might clobber anything, and so we have to save
10719          all the call-clobbered registers as well.  */
10720       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
10721         /* FIQ handlers have registers r8 - r12 banked, so
10722            we only need to check r0 - r7, Normal ISRs only
10723            bank r14 and r15, so we must check up to r12.
10724            r13 is the stack pointer which is always preserved,
10725            so we do not need to consider it here.  */
10726         max_reg = 7;
10727       else
10728         max_reg = 12;
10729
10730       for (reg = 0; reg <= max_reg; reg++)
10731         if (df_regs_ever_live_p (reg)
10732             || (! current_function_is_leaf && call_used_regs[reg]))
10733           save_reg_mask |= (1 << reg);
10734
10735       /* Also save the pic base register if necessary.  */
10736       if (flag_pic
10737           && !TARGET_SINGLE_PIC_BASE
10738           && arm_pic_register != INVALID_REGNUM
10739           && current_function_uses_pic_offset_table)
10740         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10741     }
10742   else
10743     {
10744       /* In arm mode we handle r11 (FP) as a special case.  */
10745       unsigned last_reg = TARGET_ARM ? 10 : 11;
10746       
10747       /* In the normal case we only need to save those registers
10748          which are call saved and which are used by this function.  */
10749       for (reg = 0; reg <= last_reg; reg++)
10750         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
10751           save_reg_mask |= (1 << reg);
10752
10753       /* Handle the frame pointer as a special case.  */
10754       if (! TARGET_APCS_FRAME
10755           && ! frame_pointer_needed
10756           && df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM)
10757           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
10758         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
10759       else if (! TARGET_APCS_FRAME
10760                && ! frame_pointer_needed
10761                && df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM)
10762                && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
10763         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
10764
10765       /* If we aren't loading the PIC register,
10766          don't stack it even though it may be live.  */
10767       if (flag_pic
10768           && !TARGET_SINGLE_PIC_BASE
10769           && arm_pic_register != INVALID_REGNUM
10770           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
10771               || current_function_uses_pic_offset_table))
10772         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10773
10774       /* The prologue will copy SP into R0, so save it.  */
10775       if (IS_STACKALIGN (func_type))
10776         save_reg_mask |= 1;
10777     }
10778
10779   /* Save registers so the exception handler can modify them.  */
10780   if (current_function_calls_eh_return)
10781     {
10782       unsigned int i;
10783
10784       for (i = 0; ; i++)
10785         {
10786           reg = EH_RETURN_DATA_REGNO (i);
10787           if (reg == INVALID_REGNUM)
10788             break;
10789           save_reg_mask |= 1 << reg;
10790         }
10791     }
10792
10793   return save_reg_mask;
10794 }
10795
10796
10797 /* Compute a bit mask of which registers need to be
10798    saved on the stack for the current function.  */
10799
10800 static unsigned long
10801 arm_compute_save_reg_mask (void)
10802 {
10803   unsigned int save_reg_mask = 0;
10804   unsigned long func_type = arm_current_func_type ();
10805   unsigned int reg;
10806
10807   if (IS_NAKED (func_type))
10808     /* This should never really happen.  */
10809     return 0;
10810
10811   /* If we are creating a stack frame, then we must save the frame pointer,
10812      IP (which will hold the old stack pointer), LR and the PC.  */
10813   if (frame_pointer_needed && TARGET_ARM)
10814     save_reg_mask |=
10815       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
10816       | (1 << IP_REGNUM)
10817       | (1 << LR_REGNUM)
10818       | (1 << PC_REGNUM);
10819
10820   /* Volatile functions do not return, so there
10821      is no need to save any other registers.  */
10822   if (IS_VOLATILE (func_type))
10823     return save_reg_mask;
10824
10825   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
10826
10827   /* Decide if we need to save the link register.
10828      Interrupt routines have their own banked link register,
10829      so they never need to save it.
10830      Otherwise if we do not use the link register we do not need to save
10831      it.  If we are pushing other registers onto the stack however, we
10832      can save an instruction in the epilogue by pushing the link register
10833      now and then popping it back into the PC.  This incurs extra memory
10834      accesses though, so we only do it when optimizing for size, and only
10835      if we know that we will not need a fancy return sequence.  */
10836   if (df_regs_ever_live_p (LR_REGNUM)
10837       || (save_reg_mask
10838           && optimize_size
10839           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10840           && !current_function_calls_eh_return))
10841     save_reg_mask |= 1 << LR_REGNUM;
10842
10843   if (cfun->machine->lr_save_eliminated)
10844     save_reg_mask &= ~ (1 << LR_REGNUM);
10845
10846   if (TARGET_REALLY_IWMMXT
10847       && ((bit_count (save_reg_mask)
10848            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
10849     {
10850       /* The total number of registers that are going to be pushed
10851          onto the stack is odd.  We need to ensure that the stack
10852          is 64-bit aligned before we start to save iWMMXt registers,
10853          and also before we start to create locals.  (A local variable
10854          might be a double or long long which we will load/store using
10855          an iWMMXt instruction).  Therefore we need to push another
10856          ARM register, so that the stack will be 64-bit aligned.  We
10857          try to avoid using the arg registers (r0 -r3) as they might be
10858          used to pass values in a tail call.  */
10859       for (reg = 4; reg <= 12; reg++)
10860         if ((save_reg_mask & (1 << reg)) == 0)
10861           break;
10862
10863       if (reg <= 12)
10864         save_reg_mask |= (1 << reg);
10865       else
10866         {
10867           cfun->machine->sibcall_blocked = 1;
10868           save_reg_mask |= (1 << 3);
10869         }
10870     }
10871
10872   /* We may need to push an additional register for use initializing the
10873      PIC base register.  */
10874   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
10875       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
10876     {
10877       reg = thumb_find_work_register (1 << 4);
10878       if (!call_used_regs[reg])
10879         save_reg_mask |= (1 << reg);
10880     }
10881
10882   return save_reg_mask;
10883 }
10884
10885
10886 /* Compute a bit mask of which registers need to be
10887    saved on the stack for the current function.  */
10888 static unsigned long
10889 thumb1_compute_save_reg_mask (void)
10890 {
10891   unsigned long mask;
10892   unsigned reg;
10893
10894   mask = 0;
10895   for (reg = 0; reg < 12; reg ++)
10896     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10897       mask |= 1 << reg;
10898
10899   if (flag_pic
10900       && !TARGET_SINGLE_PIC_BASE
10901       && arm_pic_register != INVALID_REGNUM
10902       && current_function_uses_pic_offset_table)
10903     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10904
10905   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
10906   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
10907     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
10908
10909   /* LR will also be pushed if any lo regs are pushed.  */
10910   if (mask & 0xff || thumb_force_lr_save ())
10911     mask |= (1 << LR_REGNUM);
10912
10913   /* Make sure we have a low work register if we need one.
10914      We will need one if we are going to push a high register,
10915      but we are not currently intending to push a low register.  */
10916   if ((mask & 0xff) == 0
10917       && ((mask & 0x0f00) || TARGET_BACKTRACE))
10918     {
10919       /* Use thumb_find_work_register to choose which register
10920          we will use.  If the register is live then we will
10921          have to push it.  Use LAST_LO_REGNUM as our fallback
10922          choice for the register to select.  */
10923       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
10924       /* Make sure the register returned by thumb_find_work_register is
10925          not part of the return value.  */
10926       if (reg * UNITS_PER_WORD <= arm_size_return_regs ())
10927         reg = LAST_LO_REGNUM;
10928
10929       if (! call_used_regs[reg])
10930         mask |= 1 << reg;
10931     }
10932
10933   return mask;
10934 }
10935
10936
10937 /* Return the number of bytes required to save VFP registers.  */
10938 static int
10939 arm_get_vfp_saved_size (void)
10940 {
10941   unsigned int regno;
10942   int count;
10943   int saved;
10944
10945   saved = 0;
10946   /* Space for saved VFP registers.  */
10947   if (TARGET_HARD_FLOAT && TARGET_VFP)
10948     {
10949       count = 0;
10950       for (regno = FIRST_VFP_REGNUM;
10951            regno < LAST_VFP_REGNUM;
10952            regno += 2)
10953         {
10954           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
10955               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
10956             {
10957               if (count > 0)
10958                 {
10959                   /* Workaround ARM10 VFPr1 bug.  */
10960                   if (count == 2 && !arm_arch6)
10961                     count++;
10962                   saved += count * 8;
10963                 }
10964               count = 0;
10965             }
10966           else
10967             count++;
10968         }
10969       if (count > 0)
10970         {
10971           if (count == 2 && !arm_arch6)
10972             count++;
10973           saved += count * 8;
10974         }
10975     }
10976   return saved;
10977 }
10978
10979
10980 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
10981    everything bar the final return instruction.  */
10982 const char *
10983 output_return_instruction (rtx operand, int really_return, int reverse)
10984 {
10985   char conditional[10];
10986   char instr[100];
10987   unsigned reg;
10988   unsigned long live_regs_mask;
10989   unsigned long func_type;
10990   arm_stack_offsets *offsets;
10991
10992   func_type = arm_current_func_type ();
10993
10994   if (IS_NAKED (func_type))
10995     return "";
10996
10997   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
10998     {
10999       /* If this function was declared non-returning, and we have
11000          found a tail call, then we have to trust that the called
11001          function won't return.  */
11002       if (really_return)
11003         {
11004           rtx ops[2];
11005
11006           /* Otherwise, trap an attempted return by aborting.  */
11007           ops[0] = operand;
11008           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
11009                                        : "abort");
11010           assemble_external_libcall (ops[1]);
11011           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
11012         }
11013
11014       return "";
11015     }
11016
11017   gcc_assert (!current_function_calls_alloca || really_return);
11018
11019   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
11020
11021   return_used_this_function = 1;
11022
11023   live_regs_mask = arm_compute_save_reg_mask ();
11024
11025   if (live_regs_mask)
11026     {
11027       const char * return_reg;
11028
11029       /* If we do not have any special requirements for function exit
11030          (e.g. interworking) then we can load the return address
11031          directly into the PC.  Otherwise we must load it into LR.  */
11032       if (really_return
11033           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
11034         return_reg = reg_names[PC_REGNUM];
11035       else
11036         return_reg = reg_names[LR_REGNUM];
11037
11038       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
11039         {
11040           /* There are three possible reasons for the IP register
11041              being saved.  1) a stack frame was created, in which case
11042              IP contains the old stack pointer, or 2) an ISR routine
11043              corrupted it, or 3) it was saved to align the stack on
11044              iWMMXt.  In case 1, restore IP into SP, otherwise just
11045              restore IP.  */
11046           if (frame_pointer_needed)
11047             {
11048               live_regs_mask &= ~ (1 << IP_REGNUM);
11049               live_regs_mask |=   (1 << SP_REGNUM);
11050             }
11051           else
11052             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
11053         }
11054
11055       /* On some ARM architectures it is faster to use LDR rather than
11056          LDM to load a single register.  On other architectures, the
11057          cost is the same.  In 26 bit mode, or for exception handlers,
11058          we have to use LDM to load the PC so that the CPSR is also
11059          restored.  */
11060       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11061         if (live_regs_mask == (1U << reg))
11062           break;
11063
11064       if (reg <= LAST_ARM_REGNUM
11065           && (reg != LR_REGNUM
11066               || ! really_return
11067               || ! IS_INTERRUPT (func_type)))
11068         {
11069           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
11070                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
11071         }
11072       else
11073         {
11074           char *p;
11075           int first = 1;
11076
11077           /* Generate the load multiple instruction to restore the
11078              registers.  Note we can get here, even if
11079              frame_pointer_needed is true, but only if sp already
11080              points to the base of the saved core registers.  */
11081           if (live_regs_mask & (1 << SP_REGNUM))
11082             {
11083               unsigned HOST_WIDE_INT stack_adjust;
11084
11085               offsets = arm_get_frame_offsets ();
11086               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
11087               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
11088
11089               if (stack_adjust && arm_arch5 && TARGET_ARM)
11090                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
11091               else
11092                 {
11093                   /* If we can't use ldmib (SA110 bug),
11094                      then try to pop r3 instead.  */
11095                   if (stack_adjust)
11096                     live_regs_mask |= 1 << 3;
11097                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
11098                 }
11099             }
11100           else
11101             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
11102
11103           p = instr + strlen (instr);
11104
11105           for (reg = 0; reg <= SP_REGNUM; reg++)
11106             if (live_regs_mask & (1 << reg))
11107               {
11108                 int l = strlen (reg_names[reg]);
11109
11110                 if (first)
11111                   first = 0;
11112                 else
11113                   {
11114                     memcpy (p, ", ", 2);
11115                     p += 2;
11116                   }
11117
11118                 memcpy (p, "%|", 2);
11119                 memcpy (p + 2, reg_names[reg], l);
11120                 p += l + 2;
11121               }
11122
11123           if (live_regs_mask & (1 << LR_REGNUM))
11124             {
11125               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
11126               /* If returning from an interrupt, restore the CPSR.  */
11127               if (IS_INTERRUPT (func_type))
11128                 strcat (p, "^");
11129             }
11130           else
11131             strcpy (p, "}");
11132         }
11133
11134       output_asm_insn (instr, & operand);
11135
11136       /* See if we need to generate an extra instruction to
11137          perform the actual function return.  */
11138       if (really_return
11139           && func_type != ARM_FT_INTERWORKED
11140           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
11141         {
11142           /* The return has already been handled
11143              by loading the LR into the PC.  */
11144           really_return = 0;
11145         }
11146     }
11147
11148   if (really_return)
11149     {
11150       switch ((int) ARM_FUNC_TYPE (func_type))
11151         {
11152         case ARM_FT_ISR:
11153         case ARM_FT_FIQ:
11154           /* ??? This is wrong for unified assembly syntax.  */
11155           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
11156           break;
11157
11158         case ARM_FT_INTERWORKED:
11159           sprintf (instr, "bx%s\t%%|lr", conditional);
11160           break;
11161
11162         case ARM_FT_EXCEPTION:
11163           /* ??? This is wrong for unified assembly syntax.  */
11164           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
11165           break;
11166
11167         default:
11168           /* Use bx if it's available.  */
11169           if (arm_arch5 || arm_arch4t)
11170             sprintf (instr, "bx%s\t%%|lr", conditional);
11171           else
11172             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
11173           break;
11174         }
11175
11176       output_asm_insn (instr, & operand);
11177     }
11178
11179   return "";
11180 }
11181
11182 /* Write the function name into the code section, directly preceding
11183    the function prologue.
11184
11185    Code will be output similar to this:
11186      t0
11187          .ascii "arm_poke_function_name", 0
11188          .align
11189      t1
11190          .word 0xff000000 + (t1 - t0)
11191      arm_poke_function_name
11192          mov     ip, sp
11193          stmfd   sp!, {fp, ip, lr, pc}
11194          sub     fp, ip, #4
11195
11196    When performing a stack backtrace, code can inspect the value
11197    of 'pc' stored at 'fp' + 0.  If the trace function then looks
11198    at location pc - 12 and the top 8 bits are set, then we know
11199    that there is a function name embedded immediately preceding this
11200    location and has length ((pc[-3]) & 0xff000000).
11201
11202    We assume that pc is declared as a pointer to an unsigned long.
11203
11204    It is of no benefit to output the function name if we are assembling
11205    a leaf function.  These function types will not contain a stack
11206    backtrace structure, therefore it is not possible to determine the
11207    function name.  */
11208 void
11209 arm_poke_function_name (FILE *stream, const char *name)
11210 {
11211   unsigned long alignlength;
11212   unsigned long length;
11213   rtx           x;
11214
11215   length      = strlen (name) + 1;
11216   alignlength = ROUND_UP_WORD (length);
11217
11218   ASM_OUTPUT_ASCII (stream, name, length);
11219   ASM_OUTPUT_ALIGN (stream, 2);
11220   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
11221   assemble_aligned_integer (UNITS_PER_WORD, x);
11222 }
11223
11224 /* Place some comments into the assembler stream
11225    describing the current function.  */
11226 static void
11227 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
11228 {
11229   unsigned long func_type;
11230
11231   if (TARGET_THUMB1)
11232     {
11233       thumb1_output_function_prologue (f, frame_size);
11234       return;
11235     }
11236
11237   /* Sanity check.  */
11238   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
11239
11240   func_type = arm_current_func_type ();
11241
11242   switch ((int) ARM_FUNC_TYPE (func_type))
11243     {
11244     default:
11245     case ARM_FT_NORMAL:
11246       break;
11247     case ARM_FT_INTERWORKED:
11248       asm_fprintf (f, "\t%@ Function supports interworking.\n");
11249       break;
11250     case ARM_FT_ISR:
11251       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
11252       break;
11253     case ARM_FT_FIQ:
11254       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
11255       break;
11256     case ARM_FT_EXCEPTION:
11257       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
11258       break;
11259     }
11260
11261   if (IS_NAKED (func_type))
11262     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
11263
11264   if (IS_VOLATILE (func_type))
11265     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
11266
11267   if (IS_NESTED (func_type))
11268     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
11269   if (IS_STACKALIGN (func_type))
11270     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
11271
11272   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
11273                current_function_args_size,
11274                current_function_pretend_args_size, frame_size);
11275
11276   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
11277                frame_pointer_needed,
11278                cfun->machine->uses_anonymous_args);
11279
11280   if (cfun->machine->lr_save_eliminated)
11281     asm_fprintf (f, "\t%@ link register save eliminated.\n");
11282
11283   if (current_function_calls_eh_return)
11284     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
11285
11286 #ifdef AOF_ASSEMBLER
11287   if (flag_pic)
11288     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
11289 #endif
11290
11291   return_used_this_function = 0;
11292 }
11293
11294 const char *
11295 arm_output_epilogue (rtx sibling)
11296 {
11297   int reg;
11298   unsigned long saved_regs_mask;
11299   unsigned long func_type;
11300   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
11301      frame that is $fp + 4 for a non-variadic function.  */
11302   int floats_offset = 0;
11303   rtx operands[3];
11304   FILE * f = asm_out_file;
11305   unsigned int lrm_count = 0;
11306   int really_return = (sibling == NULL);
11307   int start_reg;
11308   arm_stack_offsets *offsets;
11309
11310   /* If we have already generated the return instruction
11311      then it is futile to generate anything else.  */
11312   if (use_return_insn (FALSE, sibling) && return_used_this_function)
11313     return "";
11314
11315   func_type = arm_current_func_type ();
11316
11317   if (IS_NAKED (func_type))
11318     /* Naked functions don't have epilogues.  */
11319     return "";
11320
11321   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
11322     {
11323       rtx op;
11324
11325       /* A volatile function should never return.  Call abort.  */
11326       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
11327       assemble_external_libcall (op);
11328       output_asm_insn ("bl\t%a0", &op);
11329
11330       return "";
11331     }
11332
11333   /* If we are throwing an exception, then we really must be doing a
11334      return, so we can't tail-call.  */
11335   gcc_assert (!current_function_calls_eh_return || really_return);
11336
11337   offsets = arm_get_frame_offsets ();
11338   saved_regs_mask = arm_compute_save_reg_mask ();
11339
11340   if (TARGET_IWMMXT)
11341     lrm_count = bit_count (saved_regs_mask);
11342
11343   floats_offset = offsets->saved_args;
11344   /* Compute how far away the floats will be.  */
11345   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
11346     if (saved_regs_mask & (1 << reg))
11347       floats_offset += 4;
11348
11349   if (frame_pointer_needed && TARGET_ARM)
11350     {
11351       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
11352       int vfp_offset = offsets->frame;
11353
11354       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11355         {
11356           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11357             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11358               {
11359                 floats_offset += 12;
11360                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
11361                              reg, FP_REGNUM, floats_offset - vfp_offset);
11362               }
11363         }
11364       else
11365         {
11366           start_reg = LAST_FPA_REGNUM;
11367
11368           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11369             {
11370               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11371                 {
11372                   floats_offset += 12;
11373
11374                   /* We can't unstack more than four registers at once.  */
11375                   if (start_reg - reg == 3)
11376                     {
11377                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
11378                                    reg, FP_REGNUM, floats_offset - vfp_offset);
11379                       start_reg = reg - 1;
11380                     }
11381                 }
11382               else
11383                 {
11384                   if (reg != start_reg)
11385                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11386                                  reg + 1, start_reg - reg,
11387                                  FP_REGNUM, floats_offset - vfp_offset);
11388                   start_reg = reg - 1;
11389                 }
11390             }
11391
11392           /* Just in case the last register checked also needs unstacking.  */
11393           if (reg != start_reg)
11394             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
11395                          reg + 1, start_reg - reg,
11396                          FP_REGNUM, floats_offset - vfp_offset);
11397         }
11398
11399       if (TARGET_HARD_FLOAT && TARGET_VFP)
11400         {
11401           int saved_size;
11402
11403           /* The fldmd insns do not have base+offset addressing
11404              modes, so we use IP to hold the address.  */
11405           saved_size = arm_get_vfp_saved_size ();
11406
11407           if (saved_size > 0)
11408             {
11409               floats_offset += saved_size;
11410               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
11411                            FP_REGNUM, floats_offset - vfp_offset);
11412             }
11413           start_reg = FIRST_VFP_REGNUM;
11414           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11415             {
11416               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11417                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
11418                 {
11419                   if (start_reg != reg)
11420                     vfp_output_fldmd (f, IP_REGNUM,
11421                                       (start_reg - FIRST_VFP_REGNUM) / 2,
11422                                       (reg - start_reg) / 2);
11423                   start_reg = reg + 2;
11424                 }
11425             }
11426           if (start_reg != reg)
11427             vfp_output_fldmd (f, IP_REGNUM,
11428                               (start_reg - FIRST_VFP_REGNUM) / 2,
11429                               (reg - start_reg) / 2);
11430         }
11431
11432       if (TARGET_IWMMXT)
11433         {
11434           /* The frame pointer is guaranteed to be non-double-word aligned.
11435              This is because it is set to (old_stack_pointer - 4) and the
11436              old_stack_pointer was double word aligned.  Thus the offset to
11437              the iWMMXt registers to be loaded must also be non-double-word
11438              sized, so that the resultant address *is* double-word aligned.
11439              We can ignore floats_offset since that was already included in
11440              the live_regs_mask.  */
11441           lrm_count += (lrm_count % 2 ? 2 : 1);
11442
11443           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
11444             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11445               {
11446                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
11447                              reg, FP_REGNUM, lrm_count * 4);
11448                 lrm_count += 2;
11449               }
11450         }
11451
11452       /* saved_regs_mask should contain the IP, which at the time of stack
11453          frame generation actually contains the old stack pointer.  So a
11454          quick way to unwind the stack is just pop the IP register directly
11455          into the stack pointer.  */
11456       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
11457       saved_regs_mask &= ~ (1 << IP_REGNUM);
11458       saved_regs_mask |=   (1 << SP_REGNUM);
11459
11460       /* There are two registers left in saved_regs_mask - LR and PC.  We
11461          only need to restore the LR register (the return address), but to
11462          save time we can load it directly into the PC, unless we need a
11463          special function exit sequence, or we are not really returning.  */
11464       if (really_return
11465           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
11466           && !current_function_calls_eh_return)
11467         /* Delete the LR from the register mask, so that the LR on
11468            the stack is loaded into the PC in the register mask.  */
11469         saved_regs_mask &= ~ (1 << LR_REGNUM);
11470       else
11471         saved_regs_mask &= ~ (1 << PC_REGNUM);
11472
11473       /* We must use SP as the base register, because SP is one of the
11474          registers being restored.  If an interrupt or page fault
11475          happens in the ldm instruction, the SP might or might not
11476          have been restored.  That would be bad, as then SP will no
11477          longer indicate the safe area of stack, and we can get stack
11478          corruption.  Using SP as the base register means that it will
11479          be reset correctly to the original value, should an interrupt
11480          occur.  If the stack pointer already points at the right
11481          place, then omit the subtraction.  */
11482       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
11483           || current_function_calls_alloca)
11484         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
11485                      4 * bit_count (saved_regs_mask));
11486       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
11487
11488       if (IS_INTERRUPT (func_type))
11489         /* Interrupt handlers will have pushed the
11490            IP onto the stack, so restore it now.  */
11491         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
11492     }
11493   else
11494     {
11495       HOST_WIDE_INT amount;
11496       int rfe;
11497       /* Restore stack pointer if necessary.  */
11498       if (frame_pointer_needed)
11499         {
11500           /* For Thumb-2 restore sp from the frame pointer.
11501              Operand restrictions mean we have to increment FP, then copy
11502              to SP.  */
11503           amount = offsets->locals_base - offsets->saved_regs;
11504           operands[0] = hard_frame_pointer_rtx;
11505         }
11506       else
11507         {
11508           operands[0] = stack_pointer_rtx;
11509           amount = offsets->outgoing_args - offsets->saved_regs;
11510         }
11511
11512       if (amount)
11513         {
11514           operands[1] = operands[0];
11515           operands[2] = GEN_INT (amount);
11516           output_add_immediate (operands);
11517         }
11518       if (frame_pointer_needed)
11519         asm_fprintf (f, "\tmov\t%r, %r\n",
11520                      SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
11521
11522       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11523         {
11524           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
11525             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11526               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
11527                            reg, SP_REGNUM);
11528         }
11529       else
11530         {
11531           start_reg = FIRST_FPA_REGNUM;
11532
11533           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
11534             {
11535               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11536                 {
11537                   if (reg - start_reg == 3)
11538                     {
11539                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
11540                                    start_reg, SP_REGNUM);
11541                       start_reg = reg + 1;
11542                     }
11543                 }
11544               else
11545                 {
11546                   if (reg != start_reg)
11547                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
11548                                  start_reg, reg - start_reg,
11549                                  SP_REGNUM);
11550
11551                   start_reg = reg + 1;
11552                 }
11553             }
11554
11555           /* Just in case the last register checked also needs unstacking.  */
11556           if (reg != start_reg)
11557             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
11558                          start_reg, reg - start_reg, SP_REGNUM);
11559         }
11560
11561       if (TARGET_HARD_FLOAT && TARGET_VFP)
11562         {
11563           start_reg = FIRST_VFP_REGNUM;
11564           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11565             {
11566               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11567                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
11568                 {
11569                   if (start_reg != reg)
11570                     vfp_output_fldmd (f, SP_REGNUM,
11571                                       (start_reg - FIRST_VFP_REGNUM) / 2,
11572                                       (reg - start_reg) / 2);
11573                   start_reg = reg + 2;
11574                 }
11575             }
11576           if (start_reg != reg)
11577             vfp_output_fldmd (f, SP_REGNUM,
11578                               (start_reg - FIRST_VFP_REGNUM) / 2,
11579                               (reg - start_reg) / 2);
11580         }
11581       if (TARGET_IWMMXT)
11582         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
11583           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11584             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
11585
11586       /* If we can, restore the LR into the PC.  */
11587       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
11588           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
11589           && !IS_STACKALIGN (func_type)
11590           && really_return
11591           && current_function_pretend_args_size == 0
11592           && saved_regs_mask & (1 << LR_REGNUM)
11593           && !current_function_calls_eh_return)
11594         {
11595           saved_regs_mask &= ~ (1 << LR_REGNUM);
11596           saved_regs_mask |=   (1 << PC_REGNUM);
11597           rfe = IS_INTERRUPT (func_type);
11598         }
11599       else
11600         rfe = 0;
11601
11602       /* Load the registers off the stack.  If we only have one register
11603          to load use the LDR instruction - it is faster.  For Thumb-2
11604          always use pop and the assembler will pick the best instruction.*/
11605       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
11606           && !IS_INTERRUPT(func_type))
11607         {
11608           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
11609         }
11610       else if (saved_regs_mask)
11611         {
11612           if (saved_regs_mask & (1 << SP_REGNUM))
11613             /* Note - write back to the stack register is not enabled
11614                (i.e. "ldmfd sp!...").  We know that the stack pointer is
11615                in the list of registers and if we add writeback the
11616                instruction becomes UNPREDICTABLE.  */
11617             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
11618                              rfe);
11619           else if (TARGET_ARM)
11620             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
11621                              rfe);
11622           else
11623             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
11624         }
11625
11626       if (current_function_pretend_args_size)
11627         {
11628           /* Unwind the pre-pushed regs.  */
11629           operands[0] = operands[1] = stack_pointer_rtx;
11630           operands[2] = GEN_INT (current_function_pretend_args_size);
11631           output_add_immediate (operands);
11632         }
11633     }
11634
11635   /* We may have already restored PC directly from the stack.  */
11636   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
11637     return "";
11638
11639   /* Stack adjustment for exception handler.  */
11640   if (current_function_calls_eh_return)
11641     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
11642                  ARM_EH_STACKADJ_REGNUM);
11643
11644   /* Generate the return instruction.  */
11645   switch ((int) ARM_FUNC_TYPE (func_type))
11646     {
11647     case ARM_FT_ISR:
11648     case ARM_FT_FIQ:
11649       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
11650       break;
11651
11652     case ARM_FT_EXCEPTION:
11653       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
11654       break;
11655
11656     case ARM_FT_INTERWORKED:
11657       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
11658       break;
11659
11660     default:
11661       if (IS_STACKALIGN (func_type))
11662         {
11663           /* See comment in arm_expand_prologue.  */
11664           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
11665         }
11666       if (arm_arch5 || arm_arch4t)
11667         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
11668       else
11669         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
11670       break;
11671     }
11672
11673   return "";
11674 }
11675
11676 static void
11677 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
11678                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
11679 {
11680   arm_stack_offsets *offsets;
11681
11682   if (TARGET_THUMB1)
11683     {
11684       int regno;
11685
11686       /* Emit any call-via-reg trampolines that are needed for v4t support
11687          of call_reg and call_value_reg type insns.  */
11688       for (regno = 0; regno < LR_REGNUM; regno++)
11689         {
11690           rtx label = cfun->machine->call_via[regno];
11691
11692           if (label != NULL)
11693             {
11694               switch_to_section (function_section (current_function_decl));
11695               targetm.asm_out.internal_label (asm_out_file, "L",
11696                                               CODE_LABEL_NUMBER (label));
11697               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
11698             }
11699         }
11700
11701       /* ??? Probably not safe to set this here, since it assumes that a
11702          function will be emitted as assembly immediately after we generate
11703          RTL for it.  This does not happen for inline functions.  */
11704       return_used_this_function = 0;
11705     }
11706   else /* TARGET_32BIT */
11707     {
11708       /* We need to take into account any stack-frame rounding.  */
11709       offsets = arm_get_frame_offsets ();
11710
11711       gcc_assert (!use_return_insn (FALSE, NULL)
11712                   || !return_used_this_function
11713                   || offsets->saved_regs == offsets->outgoing_args
11714                   || frame_pointer_needed);
11715
11716       /* Reset the ARM-specific per-function variables.  */
11717       after_arm_reorg = 0;
11718     }
11719 }
11720
11721 /* Generate and emit an insn that we will recognize as a push_multi.
11722    Unfortunately, since this insn does not reflect very well the actual
11723    semantics of the operation, we need to annotate the insn for the benefit
11724    of DWARF2 frame unwind information.  */
11725 static rtx
11726 emit_multi_reg_push (unsigned long mask)
11727 {
11728   int num_regs = 0;
11729   int num_dwarf_regs;
11730   int i, j;
11731   rtx par;
11732   rtx dwarf;
11733   int dwarf_par_index;
11734   rtx tmp, reg;
11735
11736   for (i = 0; i <= LAST_ARM_REGNUM; i++)
11737     if (mask & (1 << i))
11738       num_regs++;
11739
11740   gcc_assert (num_regs && num_regs <= 16);
11741
11742   /* We don't record the PC in the dwarf frame information.  */
11743   num_dwarf_regs = num_regs;
11744   if (mask & (1 << PC_REGNUM))
11745     num_dwarf_regs--;
11746
11747   /* For the body of the insn we are going to generate an UNSPEC in
11748      parallel with several USEs.  This allows the insn to be recognized
11749      by the push_multi pattern in the arm.md file.  The insn looks
11750      something like this:
11751
11752        (parallel [
11753            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
11754                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
11755            (use (reg:SI 11 fp))
11756            (use (reg:SI 12 ip))
11757            (use (reg:SI 14 lr))
11758            (use (reg:SI 15 pc))
11759         ])
11760
11761      For the frame note however, we try to be more explicit and actually
11762      show each register being stored into the stack frame, plus a (single)
11763      decrement of the stack pointer.  We do it this way in order to be
11764      friendly to the stack unwinding code, which only wants to see a single
11765      stack decrement per instruction.  The RTL we generate for the note looks
11766      something like this:
11767
11768       (sequence [
11769            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
11770            (set (mem:SI (reg:SI sp)) (reg:SI r4))
11771            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
11772            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
11773            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
11774         ])
11775
11776       This sequence is used both by the code to support stack unwinding for
11777       exceptions handlers and the code to generate dwarf2 frame debugging.  */
11778
11779   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
11780   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
11781   dwarf_par_index = 1;
11782
11783   for (i = 0; i <= LAST_ARM_REGNUM; i++)
11784     {
11785       if (mask & (1 << i))
11786         {
11787           reg = gen_rtx_REG (SImode, i);
11788
11789           XVECEXP (par, 0, 0)
11790             = gen_rtx_SET (VOIDmode,
11791                            gen_frame_mem (BLKmode,
11792                                           gen_rtx_PRE_DEC (BLKmode,
11793                                                            stack_pointer_rtx)),
11794                            gen_rtx_UNSPEC (BLKmode,
11795                                            gen_rtvec (1, reg),
11796                                            UNSPEC_PUSH_MULT));
11797
11798           if (i != PC_REGNUM)
11799             {
11800               tmp = gen_rtx_SET (VOIDmode,
11801                                  gen_frame_mem (SImode, stack_pointer_rtx),
11802                                  reg);
11803               RTX_FRAME_RELATED_P (tmp) = 1;
11804               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
11805               dwarf_par_index++;
11806             }
11807
11808           break;
11809         }
11810     }
11811
11812   for (j = 1, i++; j < num_regs; i++)
11813     {
11814       if (mask & (1 << i))
11815         {
11816           reg = gen_rtx_REG (SImode, i);
11817
11818           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
11819
11820           if (i != PC_REGNUM)
11821             {
11822               tmp
11823                 = gen_rtx_SET (VOIDmode,
11824                                gen_frame_mem (SImode,
11825                                               plus_constant (stack_pointer_rtx,
11826                                                              4 * j)),
11827                                reg);
11828               RTX_FRAME_RELATED_P (tmp) = 1;
11829               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
11830             }
11831
11832           j++;
11833         }
11834     }
11835
11836   par = emit_insn (par);
11837
11838   tmp = gen_rtx_SET (VOIDmode,
11839                      stack_pointer_rtx,
11840                      plus_constant (stack_pointer_rtx, -4 * num_regs));
11841   RTX_FRAME_RELATED_P (tmp) = 1;
11842   XVECEXP (dwarf, 0, 0) = tmp;
11843
11844   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11845                                        REG_NOTES (par));
11846   return par;
11847 }
11848
11849 /* Calculate the size of the return value that is passed in registers.  */
11850 static int
11851 arm_size_return_regs (void)
11852 {
11853   enum machine_mode mode;
11854
11855   if (current_function_return_rtx != 0)
11856     mode = GET_MODE (current_function_return_rtx);
11857   else
11858     mode = DECL_MODE (DECL_RESULT (current_function_decl));
11859
11860   return GET_MODE_SIZE (mode);
11861 }
11862
11863 static rtx
11864 emit_sfm (int base_reg, int count)
11865 {
11866   rtx par;
11867   rtx dwarf;
11868   rtx tmp, reg;
11869   int i;
11870
11871   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
11872   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
11873
11874   reg = gen_rtx_REG (XFmode, base_reg++);
11875
11876   XVECEXP (par, 0, 0)
11877     = gen_rtx_SET (VOIDmode,
11878                    gen_frame_mem (BLKmode,
11879                                   gen_rtx_PRE_DEC (BLKmode,
11880                                                    stack_pointer_rtx)),
11881                    gen_rtx_UNSPEC (BLKmode,
11882                                    gen_rtvec (1, reg),
11883                                    UNSPEC_PUSH_MULT));
11884   tmp = gen_rtx_SET (VOIDmode,
11885                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
11886   RTX_FRAME_RELATED_P (tmp) = 1;
11887   XVECEXP (dwarf, 0, 1) = tmp;
11888
11889   for (i = 1; i < count; i++)
11890     {
11891       reg = gen_rtx_REG (XFmode, base_reg++);
11892       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
11893
11894       tmp = gen_rtx_SET (VOIDmode,
11895                          gen_frame_mem (XFmode,
11896                                         plus_constant (stack_pointer_rtx,
11897                                                        i * 12)),
11898                          reg);
11899       RTX_FRAME_RELATED_P (tmp) = 1;
11900       XVECEXP (dwarf, 0, i + 1) = tmp;
11901     }
11902
11903   tmp = gen_rtx_SET (VOIDmode,
11904                      stack_pointer_rtx,
11905                      plus_constant (stack_pointer_rtx, -12 * count));
11906
11907   RTX_FRAME_RELATED_P (tmp) = 1;
11908   XVECEXP (dwarf, 0, 0) = tmp;
11909
11910   par = emit_insn (par);
11911   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11912                                        REG_NOTES (par));
11913   return par;
11914 }
11915
11916
11917 /* Return true if the current function needs to save/restore LR.  */
11918
11919 static bool
11920 thumb_force_lr_save (void)
11921 {
11922   return !cfun->machine->lr_save_eliminated
11923          && (!leaf_function_p ()
11924              || thumb_far_jump_used_p ()
11925              || df_regs_ever_live_p (LR_REGNUM));
11926 }
11927
11928
11929 /* Compute the distance from register FROM to register TO.
11930    These can be the arg pointer (26), the soft frame pointer (25),
11931    the stack pointer (13) or the hard frame pointer (11).
11932    In thumb mode r7 is used as the soft frame pointer, if needed.
11933    Typical stack layout looks like this:
11934
11935        old stack pointer -> |    |
11936                              ----
11937                             |    | \
11938                             |    |   saved arguments for
11939                             |    |   vararg functions
11940                             |    | /
11941                               --
11942    hard FP & arg pointer -> |    | \
11943                             |    |   stack
11944                             |    |   frame
11945                             |    | /
11946                               --
11947                             |    | \
11948                             |    |   call saved
11949                             |    |   registers
11950       soft frame pointer -> |    | /
11951                               --
11952                             |    | \
11953                             |    |   local
11954                             |    |   variables
11955      locals base pointer -> |    | /
11956                               --
11957                             |    | \
11958                             |    |   outgoing
11959                             |    |   arguments
11960    current stack pointer -> |    | /
11961                               --
11962
11963   For a given function some or all of these stack components
11964   may not be needed, giving rise to the possibility of
11965   eliminating some of the registers.
11966
11967   The values returned by this function must reflect the behavior
11968   of arm_expand_prologue() and arm_compute_save_reg_mask().
11969
11970   The sign of the number returned reflects the direction of stack
11971   growth, so the values are positive for all eliminations except
11972   from the soft frame pointer to the hard frame pointer.
11973
11974   SFP may point just inside the local variables block to ensure correct
11975   alignment.  */
11976
11977
11978 /* Calculate stack offsets.  These are used to calculate register elimination
11979    offsets and in prologue/epilogue code.  */
11980
11981 static arm_stack_offsets *
11982 arm_get_frame_offsets (void)
11983 {
11984   struct arm_stack_offsets *offsets;
11985   unsigned long func_type;
11986   int leaf;
11987   int saved;
11988   HOST_WIDE_INT frame_size;
11989
11990   offsets = &cfun->machine->stack_offsets;
11991
11992   /* We need to know if we are a leaf function.  Unfortunately, it
11993      is possible to be called after start_sequence has been called,
11994      which causes get_insns to return the insns for the sequence,
11995      not the function, which will cause leaf_function_p to return
11996      the incorrect result.
11997
11998      to know about leaf functions once reload has completed, and the
11999      frame size cannot be changed after that time, so we can safely
12000      use the cached value.  */
12001
12002   if (reload_completed)
12003     return offsets;
12004
12005   /* Initially this is the size of the local variables.  It will translated
12006      into an offset once we have determined the size of preceding data.  */
12007   frame_size = ROUND_UP_WORD (get_frame_size ());
12008
12009   leaf = leaf_function_p ();
12010
12011   /* Space for variadic functions.  */
12012   offsets->saved_args = current_function_pretend_args_size;
12013
12014   /* In Thumb mode this is incorrect, but never used.  */
12015   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
12016
12017   if (TARGET_32BIT)
12018     {
12019       unsigned int regno;
12020
12021       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
12022
12023       /* We know that SP will be doubleword aligned on entry, and we must
12024          preserve that condition at any subroutine call.  We also require the
12025          soft frame pointer to be doubleword aligned.  */
12026
12027       if (TARGET_REALLY_IWMMXT)
12028         {
12029           /* Check for the call-saved iWMMXt registers.  */
12030           for (regno = FIRST_IWMMXT_REGNUM;
12031                regno <= LAST_IWMMXT_REGNUM;
12032                regno++)
12033             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12034               saved += 8;
12035         }
12036
12037       func_type = arm_current_func_type ();
12038       if (! IS_VOLATILE (func_type))
12039         {
12040           /* Space for saved FPA registers.  */
12041           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
12042             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
12043             saved += 12;
12044
12045           /* Space for saved VFP registers.  */
12046           if (TARGET_HARD_FLOAT && TARGET_VFP)
12047             saved += arm_get_vfp_saved_size ();
12048         }
12049     }
12050   else /* TARGET_THUMB1 */
12051     {
12052       saved = bit_count (thumb1_compute_save_reg_mask ()) * 4;
12053       if (TARGET_BACKTRACE)
12054         saved += 16;
12055     }
12056
12057   /* Saved registers include the stack frame.  */
12058   offsets->saved_regs = offsets->saved_args + saved;
12059   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
12060   /* A leaf function does not need any stack alignment if it has nothing
12061      on the stack.  */
12062   if (leaf && frame_size == 0)
12063     {
12064       offsets->outgoing_args = offsets->soft_frame;
12065       offsets->locals_base = offsets->soft_frame;
12066       return offsets;
12067     }
12068
12069   /* Ensure SFP has the correct alignment.  */
12070   if (ARM_DOUBLEWORD_ALIGN
12071       && (offsets->soft_frame & 7))
12072     offsets->soft_frame += 4;
12073
12074   offsets->locals_base = offsets->soft_frame + frame_size;
12075   offsets->outgoing_args = (offsets->locals_base
12076                             + current_function_outgoing_args_size);
12077
12078   if (ARM_DOUBLEWORD_ALIGN)
12079     {
12080       /* Ensure SP remains doubleword aligned.  */
12081       if (offsets->outgoing_args & 7)
12082         offsets->outgoing_args += 4;
12083       gcc_assert (!(offsets->outgoing_args & 7));
12084     }
12085
12086   return offsets;
12087 }
12088
12089
12090 /* Calculate the relative offsets for the different stack pointers.  Positive
12091    offsets are in the direction of stack growth.  */
12092
12093 HOST_WIDE_INT
12094 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
12095 {
12096   arm_stack_offsets *offsets;
12097
12098   offsets = arm_get_frame_offsets ();
12099
12100   /* OK, now we have enough information to compute the distances.
12101      There must be an entry in these switch tables for each pair
12102      of registers in ELIMINABLE_REGS, even if some of the entries
12103      seem to be redundant or useless.  */
12104   switch (from)
12105     {
12106     case ARG_POINTER_REGNUM:
12107       switch (to)
12108         {
12109         case THUMB_HARD_FRAME_POINTER_REGNUM:
12110           return 0;
12111
12112         case FRAME_POINTER_REGNUM:
12113           /* This is the reverse of the soft frame pointer
12114              to hard frame pointer elimination below.  */
12115           return offsets->soft_frame - offsets->saved_args;
12116
12117         case ARM_HARD_FRAME_POINTER_REGNUM:
12118           /* If there is no stack frame then the hard
12119              frame pointer and the arg pointer coincide.  */
12120           if (offsets->frame == offsets->saved_regs)
12121             return 0;
12122           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
12123           return (frame_pointer_needed
12124                   && cfun->static_chain_decl != NULL
12125                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
12126
12127         case STACK_POINTER_REGNUM:
12128           /* If nothing has been pushed on the stack at all
12129              then this will return -4.  This *is* correct!  */
12130           return offsets->outgoing_args - (offsets->saved_args + 4);
12131
12132         default:
12133           gcc_unreachable ();
12134         }
12135       gcc_unreachable ();
12136
12137     case FRAME_POINTER_REGNUM:
12138       switch (to)
12139         {
12140         case THUMB_HARD_FRAME_POINTER_REGNUM:
12141           return 0;
12142
12143         case ARM_HARD_FRAME_POINTER_REGNUM:
12144           /* The hard frame pointer points to the top entry in the
12145              stack frame.  The soft frame pointer to the bottom entry
12146              in the stack frame.  If there is no stack frame at all,
12147              then they are identical.  */
12148
12149           return offsets->frame - offsets->soft_frame;
12150
12151         case STACK_POINTER_REGNUM:
12152           return offsets->outgoing_args - offsets->soft_frame;
12153
12154         default:
12155           gcc_unreachable ();
12156         }
12157       gcc_unreachable ();
12158
12159     default:
12160       /* You cannot eliminate from the stack pointer.
12161          In theory you could eliminate from the hard frame
12162          pointer to the stack pointer, but this will never
12163          happen, since if a stack frame is not needed the
12164          hard frame pointer will never be used.  */
12165       gcc_unreachable ();
12166     }
12167 }
12168
12169
12170 /* Emit RTL to save coprocessor registers on function entry.  Returns the
12171    number of bytes pushed.  */
12172
12173 static int
12174 arm_save_coproc_regs(void)
12175 {
12176   int saved_size = 0;
12177   unsigned reg;
12178   unsigned start_reg;
12179   rtx insn;
12180
12181   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
12182     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
12183       {
12184         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
12185         insn = gen_rtx_MEM (V2SImode, insn);
12186         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
12187         RTX_FRAME_RELATED_P (insn) = 1;
12188         saved_size += 8;
12189       }
12190
12191   /* Save any floating point call-saved registers used by this
12192      function.  */
12193   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
12194     {
12195       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12196         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12197           {
12198             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
12199             insn = gen_rtx_MEM (XFmode, insn);
12200             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
12201             RTX_FRAME_RELATED_P (insn) = 1;
12202             saved_size += 12;
12203           }
12204     }
12205   else
12206     {
12207       start_reg = LAST_FPA_REGNUM;
12208
12209       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
12210         {
12211           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
12212             {
12213               if (start_reg - reg == 3)
12214                 {
12215                   insn = emit_sfm (reg, 4);
12216                   RTX_FRAME_RELATED_P (insn) = 1;
12217                   saved_size += 48;
12218                   start_reg = reg - 1;
12219                 }
12220             }
12221           else
12222             {
12223               if (start_reg != reg)
12224                 {
12225                   insn = emit_sfm (reg + 1, start_reg - reg);
12226                   RTX_FRAME_RELATED_P (insn) = 1;
12227                   saved_size += (start_reg - reg) * 12;
12228                 }
12229               start_reg = reg - 1;
12230             }
12231         }
12232
12233       if (start_reg != reg)
12234         {
12235           insn = emit_sfm (reg + 1, start_reg - reg);
12236           saved_size += (start_reg - reg) * 12;
12237           RTX_FRAME_RELATED_P (insn) = 1;
12238         }
12239     }
12240   if (TARGET_HARD_FLOAT && TARGET_VFP)
12241     {
12242       start_reg = FIRST_VFP_REGNUM;
12243
12244       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
12245         {
12246           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
12247               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
12248             {
12249               if (start_reg != reg)
12250                 saved_size += vfp_emit_fstmd (start_reg,
12251                                               (reg - start_reg) / 2);
12252               start_reg = reg + 2;
12253             }
12254         }
12255       if (start_reg != reg)
12256         saved_size += vfp_emit_fstmd (start_reg,
12257                                       (reg - start_reg) / 2);
12258     }
12259   return saved_size;
12260 }
12261
12262
12263 /* Set the Thumb frame pointer from the stack pointer.  */
12264
12265 static void
12266 thumb_set_frame_pointer (arm_stack_offsets *offsets)
12267 {
12268   HOST_WIDE_INT amount;
12269   rtx insn, dwarf;
12270
12271   amount = offsets->outgoing_args - offsets->locals_base;
12272   if (amount < 1024)
12273     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12274                                   stack_pointer_rtx, GEN_INT (amount)));
12275   else
12276     {
12277       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
12278       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
12279                                     hard_frame_pointer_rtx,
12280                                     stack_pointer_rtx));
12281       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
12282                            plus_constant (stack_pointer_rtx, amount));
12283       RTX_FRAME_RELATED_P (dwarf) = 1;
12284       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
12285                                             REG_NOTES (insn));
12286     }
12287
12288   RTX_FRAME_RELATED_P (insn) = 1;
12289 }
12290
12291 /* Generate the prologue instructions for entry into an ARM or Thumb-2
12292    function.  */
12293 void
12294 arm_expand_prologue (void)
12295 {
12296   rtx amount;
12297   rtx insn;
12298   rtx ip_rtx;
12299   unsigned long live_regs_mask;
12300   unsigned long func_type;
12301   int fp_offset = 0;
12302   int saved_pretend_args = 0;
12303   int saved_regs = 0;
12304   unsigned HOST_WIDE_INT args_to_push;
12305   arm_stack_offsets *offsets;
12306
12307   func_type = arm_current_func_type ();
12308
12309   /* Naked functions don't have prologues.  */
12310   if (IS_NAKED (func_type))
12311     return;
12312
12313   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
12314   args_to_push = current_function_pretend_args_size;
12315
12316   /* Compute which register we will have to save onto the stack.  */
12317   live_regs_mask = arm_compute_save_reg_mask ();
12318
12319   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
12320
12321   if (IS_STACKALIGN (func_type))
12322     {
12323       rtx dwarf;
12324       rtx r0;
12325       rtx r1;
12326       /* Handle a word-aligned stack pointer.  We generate the following:
12327
12328           mov r0, sp
12329           bic r1, r0, #7
12330           mov sp, r1
12331           <save and restore r0 in normal prologue/epilogue>
12332           mov sp, r0
12333           bx lr
12334
12335          The unwinder doesn't need to know about the stack realignment.
12336          Just tell it we saved SP in r0.  */
12337       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
12338
12339       r0 = gen_rtx_REG (SImode, 0);
12340       r1 = gen_rtx_REG (SImode, 1);
12341       dwarf = gen_rtx_UNSPEC (SImode, NULL_RTVEC, UNSPEC_STACK_ALIGN);
12342       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
12343       insn = gen_movsi (r0, stack_pointer_rtx);
12344       RTX_FRAME_RELATED_P (insn) = 1;
12345       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12346                                             dwarf, REG_NOTES (insn));
12347       emit_insn (insn);
12348       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
12349       emit_insn (gen_movsi (stack_pointer_rtx, r1));
12350     }
12351
12352   if (frame_pointer_needed && TARGET_ARM)
12353     {
12354       if (IS_INTERRUPT (func_type))
12355         {
12356           /* Interrupt functions must not corrupt any registers.
12357              Creating a frame pointer however, corrupts the IP
12358              register, so we must push it first.  */
12359           insn = emit_multi_reg_push (1 << IP_REGNUM);
12360
12361           /* Do not set RTX_FRAME_RELATED_P on this insn.
12362              The dwarf stack unwinding code only wants to see one
12363              stack decrement per function, and this is not it.  If
12364              this instruction is labeled as being part of the frame
12365              creation sequence then dwarf2out_frame_debug_expr will
12366              die when it encounters the assignment of IP to FP
12367              later on, since the use of SP here establishes SP as
12368              the CFA register and not IP.
12369
12370              Anyway this instruction is not really part of the stack
12371              frame creation although it is part of the prologue.  */
12372         }
12373       else if (IS_NESTED (func_type))
12374         {
12375           /* The Static chain register is the same as the IP register
12376              used as a scratch register during stack frame creation.
12377              To get around this need to find somewhere to store IP
12378              whilst the frame is being created.  We try the following
12379              places in order:
12380
12381                1. The last argument register.
12382                2. A slot on the stack above the frame.  (This only
12383                   works if the function is not a varargs function).
12384                3. Register r3, after pushing the argument registers
12385                   onto the stack.
12386
12387              Note - we only need to tell the dwarf2 backend about the SP
12388              adjustment in the second variant; the static chain register
12389              doesn't need to be unwound, as it doesn't contain a value
12390              inherited from the caller.  */
12391
12392           if (df_regs_ever_live_p (3) == false)
12393             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
12394           else if (args_to_push == 0)
12395             {
12396               rtx dwarf;
12397
12398               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
12399               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
12400               fp_offset = 4;
12401
12402               /* Just tell the dwarf backend that we adjusted SP.  */
12403               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
12404                                    plus_constant (stack_pointer_rtx,
12405                                                   -fp_offset));
12406               RTX_FRAME_RELATED_P (insn) = 1;
12407               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
12408                                                     dwarf, REG_NOTES (insn));
12409             }
12410           else
12411             {
12412               /* Store the args on the stack.  */
12413               if (cfun->machine->uses_anonymous_args)
12414                 insn = emit_multi_reg_push
12415                   ((0xf0 >> (args_to_push / 4)) & 0xf);
12416               else
12417                 insn = emit_insn
12418                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12419                                GEN_INT (- args_to_push)));
12420
12421               RTX_FRAME_RELATED_P (insn) = 1;
12422
12423               saved_pretend_args = 1;
12424               fp_offset = args_to_push;
12425               args_to_push = 0;
12426
12427               /* Now reuse r3 to preserve IP.  */
12428               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
12429             }
12430         }
12431
12432       insn = emit_set_insn (ip_rtx,
12433                             plus_constant (stack_pointer_rtx, fp_offset));
12434       RTX_FRAME_RELATED_P (insn) = 1;
12435     }
12436
12437   if (args_to_push)
12438     {
12439       /* Push the argument registers, or reserve space for them.  */
12440       if (cfun->machine->uses_anonymous_args)
12441         insn = emit_multi_reg_push
12442           ((0xf0 >> (args_to_push / 4)) & 0xf);
12443       else
12444         insn = emit_insn
12445           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12446                        GEN_INT (- args_to_push)));
12447       RTX_FRAME_RELATED_P (insn) = 1;
12448     }
12449
12450   /* If this is an interrupt service routine, and the link register
12451      is going to be pushed, and we are not creating a stack frame,
12452      (which would involve an extra push of IP and a pop in the epilogue)
12453      subtracting four from LR now will mean that the function return
12454      can be done with a single instruction.  */
12455   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
12456       && (live_regs_mask & (1 << LR_REGNUM)) != 0
12457       && ! frame_pointer_needed
12458       && TARGET_ARM)
12459     {
12460       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
12461       
12462       emit_set_insn (lr, plus_constant (lr, -4));
12463     }
12464
12465   if (live_regs_mask)
12466     {
12467       insn = emit_multi_reg_push (live_regs_mask);
12468       saved_regs += bit_count (live_regs_mask) * 4;
12469       RTX_FRAME_RELATED_P (insn) = 1;
12470     }
12471
12472   if (! IS_VOLATILE (func_type))
12473     saved_regs += arm_save_coproc_regs ();
12474
12475   if (frame_pointer_needed && TARGET_ARM)
12476     {
12477       /* Create the new frame pointer.  */
12478         {
12479           insn = GEN_INT (-(4 + args_to_push + fp_offset));
12480           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
12481           RTX_FRAME_RELATED_P (insn) = 1;
12482
12483           if (IS_NESTED (func_type))
12484             {
12485               /* Recover the static chain register.  */
12486               if (!df_regs_ever_live_p (3)
12487                   || saved_pretend_args)
12488                 insn = gen_rtx_REG (SImode, 3);
12489               else /* if (current_function_pretend_args_size == 0) */
12490                 {
12491                   insn = plus_constant (hard_frame_pointer_rtx, 4);
12492                   insn = gen_frame_mem (SImode, insn);
12493                 }
12494               emit_set_insn (ip_rtx, insn);
12495               /* Add a USE to stop propagate_one_insn() from barfing.  */
12496               emit_insn (gen_prologue_use (ip_rtx));
12497             }
12498         }
12499     }
12500
12501   offsets = arm_get_frame_offsets ();
12502   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
12503     {
12504       /* This add can produce multiple insns for a large constant, so we
12505          need to get tricky.  */
12506       rtx last = get_last_insn ();
12507
12508       amount = GEN_INT (offsets->saved_args + saved_regs
12509                         - offsets->outgoing_args);
12510
12511       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
12512                                     amount));
12513       do
12514         {
12515           last = last ? NEXT_INSN (last) : get_insns ();
12516           RTX_FRAME_RELATED_P (last) = 1;
12517         }
12518       while (last != insn);
12519
12520       /* If the frame pointer is needed, emit a special barrier that
12521          will prevent the scheduler from moving stores to the frame
12522          before the stack adjustment.  */
12523       if (frame_pointer_needed)
12524         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
12525                                          hard_frame_pointer_rtx));
12526     }
12527
12528
12529   if (frame_pointer_needed && TARGET_THUMB2)
12530     thumb_set_frame_pointer (offsets);
12531
12532   if (flag_pic && arm_pic_register != INVALID_REGNUM)
12533     {
12534       unsigned long mask;
12535
12536       mask = live_regs_mask;
12537       mask &= THUMB2_WORK_REGS;
12538       if (!IS_NESTED (func_type))
12539         mask |= (1 << IP_REGNUM);
12540       arm_load_pic_register (mask);
12541     }
12542
12543   /* If we are profiling, make sure no instructions are scheduled before
12544      the call to mcount.  Similarly if the user has requested no
12545      scheduling in the prolog.  Similarly if we want non-call exceptions
12546      using the EABI unwinder, to prevent faulting instructions from being
12547      swapped with a stack adjustment.  */
12548   if (current_function_profile || !TARGET_SCHED_PROLOG
12549       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
12550     emit_insn (gen_blockage ());
12551
12552   /* If the link register is being kept alive, with the return address in it,
12553      then make sure that it does not get reused by the ce2 pass.  */
12554   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
12555     cfun->machine->lr_save_eliminated = 1;
12556 }
12557 \f
12558 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
12559 static void
12560 arm_print_condition (FILE *stream)
12561 {
12562   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
12563     {
12564       /* Branch conversion is not implemented for Thumb-2.  */
12565       if (TARGET_THUMB)
12566         {
12567           output_operand_lossage ("predicated Thumb instruction");
12568           return;
12569         }
12570       if (current_insn_predicate != NULL)
12571         {
12572           output_operand_lossage
12573             ("predicated instruction in conditional sequence");
12574           return;
12575         }
12576
12577       fputs (arm_condition_codes[arm_current_cc], stream);
12578     }
12579   else if (current_insn_predicate)
12580     {
12581       enum arm_cond_code code;
12582
12583       if (TARGET_THUMB1)
12584         {
12585           output_operand_lossage ("predicated Thumb instruction");
12586           return;
12587         }
12588
12589       code = get_arm_condition_code (current_insn_predicate);
12590       fputs (arm_condition_codes[code], stream);
12591     }
12592 }
12593
12594
12595 /* If CODE is 'd', then the X is a condition operand and the instruction
12596    should only be executed if the condition is true.
12597    if CODE is 'D', then the X is a condition operand and the instruction
12598    should only be executed if the condition is false: however, if the mode
12599    of the comparison is CCFPEmode, then always execute the instruction -- we
12600    do this because in these circumstances !GE does not necessarily imply LT;
12601    in these cases the instruction pattern will take care to make sure that
12602    an instruction containing %d will follow, thereby undoing the effects of
12603    doing this instruction unconditionally.
12604    If CODE is 'N' then X is a floating point operand that must be negated
12605    before output.
12606    If CODE is 'B' then output a bitwise inverted value of X (a const int).
12607    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
12608 void
12609 arm_print_operand (FILE *stream, rtx x, int code)
12610 {
12611   switch (code)
12612     {
12613     case '@':
12614       fputs (ASM_COMMENT_START, stream);
12615       return;
12616
12617     case '_':
12618       fputs (user_label_prefix, stream);
12619       return;
12620
12621     case '|':
12622       fputs (REGISTER_PREFIX, stream);
12623       return;
12624
12625     case '?':
12626       arm_print_condition (stream);
12627       return;
12628
12629     case '(':
12630       /* Nothing in unified syntax, otherwise the current condition code.  */
12631       if (!TARGET_UNIFIED_ASM)
12632         arm_print_condition (stream);
12633       break;
12634
12635     case ')':
12636       /* The current condition code in unified syntax, otherwise nothing.  */
12637       if (TARGET_UNIFIED_ASM)
12638         arm_print_condition (stream);
12639       break;
12640   
12641     case '.':
12642       /* The current condition code for a condition code setting instruction.
12643          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
12644       if (TARGET_UNIFIED_ASM)
12645         {
12646           fputc('s', stream);
12647           arm_print_condition (stream);
12648         }
12649       else
12650         {
12651           arm_print_condition (stream);
12652           fputc('s', stream);
12653         }
12654       return;
12655
12656     case '!':
12657       /* If the instruction is conditionally executed then print
12658          the current condition code, otherwise print 's'.  */
12659       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
12660       if (current_insn_predicate)
12661         arm_print_condition (stream);
12662       else
12663         fputc('s', stream);
12664       break;
12665
12666     /* %# is a "break" sequence. It doesn't output anything, but is used to
12667        seperate e.g. operand numbers from following text, if that text consists
12668        of further digits which we don't want to be part of the operand
12669        number.  */
12670     case '#':
12671       return;
12672
12673     case 'N':
12674       {
12675         REAL_VALUE_TYPE r;
12676         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12677         r = REAL_VALUE_NEGATE (r);
12678         fprintf (stream, "%s", fp_const_from_val (&r));
12679       }
12680       return;
12681
12682     /* An integer without a preceding # sign.  */
12683     case 'c':
12684       gcc_assert (GET_CODE (x) == CONST_INT);
12685       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12686       return;
12687
12688     case 'B':
12689       if (GET_CODE (x) == CONST_INT)
12690         {
12691           HOST_WIDE_INT val;
12692           val = ARM_SIGN_EXTEND (~INTVAL (x));
12693           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
12694         }
12695       else
12696         {
12697           putc ('~', stream);
12698           output_addr_const (stream, x);
12699         }
12700       return;
12701
12702     case 'L':
12703       /* The low 16 bits of an immediate constant.  */
12704       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
12705       return;
12706
12707     case 'i':
12708       fprintf (stream, "%s", arithmetic_instr (x, 1));
12709       return;
12710
12711     /* Truncate Cirrus shift counts.  */
12712     case 's':
12713       if (GET_CODE (x) == CONST_INT)
12714         {
12715           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
12716           return;
12717         }
12718       arm_print_operand (stream, x, 0);
12719       return;
12720
12721     case 'I':
12722       fprintf (stream, "%s", arithmetic_instr (x, 0));
12723       return;
12724
12725     case 'S':
12726       {
12727         HOST_WIDE_INT val;
12728         const char *shift;
12729
12730         if (!shift_operator (x, SImode))
12731           {
12732             output_operand_lossage ("invalid shift operand");
12733             break;
12734           }
12735
12736         shift = shift_op (x, &val);
12737
12738         if (shift)
12739           {
12740             fprintf (stream, ", %s ", shift);
12741             if (val == -1)
12742               arm_print_operand (stream, XEXP (x, 1), 0);
12743             else
12744               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
12745           }
12746       }
12747       return;
12748
12749       /* An explanation of the 'Q', 'R' and 'H' register operands:
12750
12751          In a pair of registers containing a DI or DF value the 'Q'
12752          operand returns the register number of the register containing
12753          the least significant part of the value.  The 'R' operand returns
12754          the register number of the register containing the most
12755          significant part of the value.
12756
12757          The 'H' operand returns the higher of the two register numbers.
12758          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
12759          same as the 'Q' operand, since the most significant part of the
12760          value is held in the lower number register.  The reverse is true
12761          on systems where WORDS_BIG_ENDIAN is false.
12762
12763          The purpose of these operands is to distinguish between cases
12764          where the endian-ness of the values is important (for example
12765          when they are added together), and cases where the endian-ness
12766          is irrelevant, but the order of register operations is important.
12767          For example when loading a value from memory into a register
12768          pair, the endian-ness does not matter.  Provided that the value
12769          from the lower memory address is put into the lower numbered
12770          register, and the value from the higher address is put into the
12771          higher numbered register, the load will work regardless of whether
12772          the value being loaded is big-wordian or little-wordian.  The
12773          order of the two register loads can matter however, if the address
12774          of the memory location is actually held in one of the registers
12775          being overwritten by the load.  */
12776     case 'Q':
12777       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12778         {
12779           output_operand_lossage ("invalid operand for code '%c'", code);
12780           return;
12781         }
12782
12783       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
12784       return;
12785
12786     case 'R':
12787       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12788         {
12789           output_operand_lossage ("invalid operand for code '%c'", code);
12790           return;
12791         }
12792
12793       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
12794       return;
12795
12796     case 'H':
12797       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12798         {
12799           output_operand_lossage ("invalid operand for code '%c'", code);
12800           return;
12801         }
12802
12803       asm_fprintf (stream, "%r", REGNO (x) + 1);
12804       return;
12805
12806     case 'J':
12807       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12808         {
12809           output_operand_lossage ("invalid operand for code '%c'", code);
12810           return;
12811         }
12812
12813       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 3 : 2));
12814       return;
12815
12816     case 'K':
12817       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12818         {
12819           output_operand_lossage ("invalid operand for code '%c'", code);
12820           return;
12821         }
12822
12823       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 2 : 3));
12824       return;
12825
12826     case 'm':
12827       asm_fprintf (stream, "%r",
12828                    GET_CODE (XEXP (x, 0)) == REG
12829                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
12830       return;
12831
12832     case 'M':
12833       asm_fprintf (stream, "{%r-%r}",
12834                    REGNO (x),
12835                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
12836       return;
12837
12838     /* Like 'M', but writing doubleword vector registers, for use by Neon
12839        insns.  */
12840     case 'h':
12841       {
12842         int regno = (REGNO (x) - FIRST_VFP_REGNUM) / 2;
12843         int numregs = ARM_NUM_REGS (GET_MODE (x)) / 2;
12844         if (numregs == 1)
12845           asm_fprintf (stream, "{d%d}", regno);
12846         else
12847           asm_fprintf (stream, "{d%d-d%d}", regno, regno + numregs - 1);
12848       }
12849       return;
12850
12851     case 'd':
12852       /* CONST_TRUE_RTX means always -- that's the default.  */
12853       if (x == const_true_rtx)
12854         return;
12855
12856       if (!COMPARISON_P (x))
12857         {
12858           output_operand_lossage ("invalid operand for code '%c'", code);
12859           return;
12860         }
12861
12862       fputs (arm_condition_codes[get_arm_condition_code (x)],
12863              stream);
12864       return;
12865
12866     case 'D':
12867       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
12868          want to do that.  */
12869       if (x == const_true_rtx)
12870         {
12871           output_operand_lossage ("instruction never executed");
12872           return;
12873         }
12874       if (!COMPARISON_P (x))
12875         {
12876           output_operand_lossage ("invalid operand for code '%c'", code);
12877           return;
12878         }
12879
12880       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
12881                                  (get_arm_condition_code (x))],
12882              stream);
12883       return;
12884
12885     /* Cirrus registers can be accessed in a variety of ways:
12886          single floating point (f)
12887          double floating point (d)
12888          32bit integer         (fx)
12889          64bit integer         (dx).  */
12890     case 'W':                   /* Cirrus register in F mode.  */
12891     case 'X':                   /* Cirrus register in D mode.  */
12892     case 'Y':                   /* Cirrus register in FX mode.  */
12893     case 'Z':                   /* Cirrus register in DX mode.  */
12894       gcc_assert (GET_CODE (x) == REG
12895                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
12896
12897       fprintf (stream, "mv%s%s",
12898                code == 'W' ? "f"
12899                : code == 'X' ? "d"
12900                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
12901
12902       return;
12903
12904     /* Print cirrus register in the mode specified by the register's mode.  */
12905     case 'V':
12906       {
12907         int mode = GET_MODE (x);
12908
12909         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
12910           {
12911             output_operand_lossage ("invalid operand for code '%c'", code);
12912             return;
12913           }
12914
12915         fprintf (stream, "mv%s%s",
12916                  mode == DFmode ? "d"
12917                  : mode == SImode ? "fx"
12918                  : mode == DImode ? "dx"
12919                  : "f", reg_names[REGNO (x)] + 2);
12920
12921         return;
12922       }
12923
12924     case 'U':
12925       if (GET_CODE (x) != REG
12926           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
12927           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
12928         /* Bad value for wCG register number.  */
12929         {
12930           output_operand_lossage ("invalid operand for code '%c'", code);
12931           return;
12932         }
12933
12934       else
12935         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
12936       return;
12937
12938       /* Print an iWMMXt control register name.  */
12939     case 'w':
12940       if (GET_CODE (x) != CONST_INT
12941           || INTVAL (x) < 0
12942           || INTVAL (x) >= 16)
12943         /* Bad value for wC register number.  */
12944         {
12945           output_operand_lossage ("invalid operand for code '%c'", code);
12946           return;
12947         }
12948
12949       else
12950         {
12951           static const char * wc_reg_names [16] =
12952             {
12953               "wCID",  "wCon",  "wCSSF", "wCASF",
12954               "wC4",   "wC5",   "wC6",   "wC7",
12955               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
12956               "wC12",  "wC13",  "wC14",  "wC15"
12957             };
12958
12959           fprintf (stream, wc_reg_names [INTVAL (x)]);
12960         }
12961       return;
12962
12963     /* Print a VFP/Neon double precision or quad precision register name.  */
12964     case 'P':
12965     case 'q':
12966       {
12967         int mode = GET_MODE (x);
12968         int is_quad = (code == 'q');
12969         int regno;
12970
12971         if (GET_MODE_SIZE (mode) != (is_quad ? 16 : 8))
12972           {
12973             output_operand_lossage ("invalid operand for code '%c'", code);
12974             return;
12975           }
12976
12977         if (GET_CODE (x) != REG
12978             || !IS_VFP_REGNUM (REGNO (x)))
12979           {
12980             output_operand_lossage ("invalid operand for code '%c'", code);
12981             return;
12982           }
12983
12984         regno = REGNO (x);
12985         if ((is_quad && !NEON_REGNO_OK_FOR_QUAD (regno))
12986             || (!is_quad && !VFP_REGNO_OK_FOR_DOUBLE (regno)))
12987           {
12988             output_operand_lossage ("invalid operand for code '%c'", code);
12989             return;
12990           }
12991
12992         fprintf (stream, "%c%d", is_quad ? 'q' : 'd',
12993           (regno - FIRST_VFP_REGNUM) >> (is_quad ? 2 : 1));
12994       }
12995       return;
12996
12997     /* These two codes print the low/high doubleword register of a Neon quad
12998        register, respectively.  For pair-structure types, can also print
12999        low/high quadword registers.  */
13000     case 'e':
13001     case 'f':
13002       {
13003         int mode = GET_MODE (x);
13004         int regno;
13005
13006         if ((GET_MODE_SIZE (mode) != 16
13007              && GET_MODE_SIZE (mode) != 32) || GET_CODE (x) != REG)
13008           {
13009             output_operand_lossage ("invalid operand for code '%c'", code);
13010             return;
13011           }
13012
13013         regno = REGNO (x);
13014         if (!NEON_REGNO_OK_FOR_QUAD (regno))
13015           {
13016             output_operand_lossage ("invalid operand for code '%c'", code);
13017             return;
13018           }
13019
13020         if (GET_MODE_SIZE (mode) == 16)
13021           fprintf (stream, "d%d", ((regno - FIRST_VFP_REGNUM) >> 1)
13022                                   + (code == 'f' ? 1 : 0));
13023         else
13024           fprintf (stream, "q%d", ((regno - FIRST_VFP_REGNUM) >> 2)
13025                                   + (code == 'f' ? 1 : 0));
13026       }
13027       return;
13028
13029     /* Print a VFPv3 floating-point constant, represented as an integer
13030        index.  */
13031     case 'G':
13032       {
13033         int index = vfp3_const_double_index (x);
13034         gcc_assert (index != -1);
13035         fprintf (stream, "%d", index);
13036       }
13037       return;
13038
13039     /* Print bits representing opcode features for Neon.
13040
13041        Bit 0 is 1 for signed, 0 for unsigned.  Floats count as signed
13042        and polynomials as unsigned.
13043
13044        Bit 1 is 1 for floats and polynomials, 0 for ordinary integers.
13045
13046        Bit 2 is 1 for rounding functions, 0 otherwise.  */
13047
13048     /* Identify the type as 's', 'u', 'p' or 'f'.  */
13049     case 'T':
13050       {
13051         HOST_WIDE_INT bits = INTVAL (x);
13052         fputc ("uspf"[bits & 3], stream);
13053       }
13054       return;
13055
13056     /* Likewise, but signed and unsigned integers are both 'i'.  */
13057     case 'F':
13058       {
13059         HOST_WIDE_INT bits = INTVAL (x);
13060         fputc ("iipf"[bits & 3], stream);
13061       }
13062       return;
13063
13064     /* As for 'T', but emit 'u' instead of 'p'.  */
13065     case 't':
13066       {
13067         HOST_WIDE_INT bits = INTVAL (x);
13068         fputc ("usuf"[bits & 3], stream);
13069       }
13070       return;
13071
13072     /* Bit 2: rounding (vs none).  */
13073     case 'O':
13074       {
13075         HOST_WIDE_INT bits = INTVAL (x);
13076         fputs ((bits & 4) != 0 ? "r" : "", stream);
13077       }
13078       return;
13079
13080     default:
13081       if (x == 0)
13082         {
13083           output_operand_lossage ("missing operand");
13084           return;
13085         }
13086
13087       switch (GET_CODE (x))
13088         {
13089         case REG:
13090           asm_fprintf (stream, "%r", REGNO (x));
13091           break;
13092
13093         case MEM:
13094           output_memory_reference_mode = GET_MODE (x);
13095           output_address (XEXP (x, 0));
13096           break;
13097
13098         case CONST_DOUBLE:
13099           if (TARGET_NEON)
13100             {
13101               char fpstr[20];
13102               real_to_decimal (fpstr, CONST_DOUBLE_REAL_VALUE (x),
13103                                sizeof (fpstr), 0, 1);
13104               fprintf (stream, "#%s", fpstr);
13105             }
13106           else
13107             fprintf (stream, "#%s", fp_immediate_constant (x));
13108           break;
13109
13110         default:
13111           gcc_assert (GET_CODE (x) != NEG);
13112           fputc ('#', stream);
13113           output_addr_const (stream, x);
13114           break;
13115         }
13116     }
13117 }
13118 \f
13119 #ifndef AOF_ASSEMBLER
13120 /* Target hook for assembling integer objects.  The ARM version needs to
13121    handle word-sized values specially.  */
13122 static bool
13123 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
13124 {
13125   enum machine_mode mode;
13126
13127   if (size == UNITS_PER_WORD && aligned_p)
13128     {
13129       fputs ("\t.word\t", asm_out_file);
13130       output_addr_const (asm_out_file, x);
13131
13132       /* Mark symbols as position independent.  We only do this in the
13133          .text segment, not in the .data segment.  */
13134       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
13135           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
13136         {
13137           /* See legitimize_pic_address for an explanation of the
13138              TARGET_VXWORKS_RTP check.  */
13139           if (TARGET_VXWORKS_RTP
13140               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
13141             fputs ("(GOT)", asm_out_file);
13142           else
13143             fputs ("(GOTOFF)", asm_out_file);
13144         }
13145       fputc ('\n', asm_out_file);
13146       return true;
13147     }
13148
13149   mode = GET_MODE (x);
13150
13151   if (arm_vector_mode_supported_p (mode))
13152     {
13153       int i, units;
13154       unsigned int invmask = 0, parts_per_word;
13155
13156       gcc_assert (GET_CODE (x) == CONST_VECTOR);
13157
13158       units = CONST_VECTOR_NUNITS (x);
13159       size = GET_MODE_SIZE (GET_MODE_INNER (mode));
13160
13161       /* For big-endian Neon vectors, we must permute the vector to the form
13162          which, when loaded by a VLDR or VLDM instruction, will give a vector
13163          with the elements in the right order.  */
13164       if (TARGET_NEON && WORDS_BIG_ENDIAN)
13165         {
13166           parts_per_word = UNITS_PER_WORD / size;
13167           /* FIXME: This might be wrong for 64-bit vector elements, but we don't
13168              support those anywhere yet.  */
13169           invmask = (parts_per_word == 0) ? 0 : (1 << (parts_per_word - 1)) - 1;
13170         }
13171
13172       if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
13173         for (i = 0; i < units; i++)
13174           {
13175             rtx elt = CONST_VECTOR_ELT (x, i ^ invmask);
13176             assemble_integer
13177               (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
13178           }
13179       else
13180         for (i = 0; i < units; i++)
13181           {
13182             rtx elt = CONST_VECTOR_ELT (x, i);
13183             REAL_VALUE_TYPE rval;
13184
13185             REAL_VALUE_FROM_CONST_DOUBLE (rval, elt);
13186
13187             assemble_real
13188               (rval, GET_MODE_INNER (mode),
13189               i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT);
13190           }
13191
13192       return true;
13193     }
13194
13195   return default_assemble_integer (x, size, aligned_p);
13196 }
13197
13198 static void
13199 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
13200 {
13201   section *s;
13202
13203   if (!TARGET_AAPCS_BASED)
13204     {
13205       (is_ctor ? 
13206        default_named_section_asm_out_constructor 
13207        : default_named_section_asm_out_destructor) (symbol, priority);
13208       return;
13209     }
13210
13211   /* Put these in the .init_array section, using a special relocation.  */
13212   if (priority != DEFAULT_INIT_PRIORITY)
13213     {
13214       char buf[18];
13215       sprintf (buf, "%s.%.5u", 
13216                is_ctor ? ".init_array" : ".fini_array",
13217                priority);
13218       s = get_section (buf, SECTION_WRITE, NULL_TREE);
13219     }
13220   else if (is_ctor)
13221     s = ctors_section;
13222   else
13223     s = dtors_section;
13224
13225   switch_to_section (s);
13226   assemble_align (POINTER_SIZE);
13227   fputs ("\t.word\t", asm_out_file);
13228   output_addr_const (asm_out_file, symbol);
13229   fputs ("(target1)\n", asm_out_file);
13230 }
13231
13232 /* Add a function to the list of static constructors.  */
13233
13234 static void
13235 arm_elf_asm_constructor (rtx symbol, int priority)
13236 {
13237   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
13238 }
13239
13240 /* Add a function to the list of static destructors.  */
13241
13242 static void
13243 arm_elf_asm_destructor (rtx symbol, int priority)
13244 {
13245   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
13246 }
13247 #endif
13248 \f
13249 /* A finite state machine takes care of noticing whether or not instructions
13250    can be conditionally executed, and thus decrease execution time and code
13251    size by deleting branch instructions.  The fsm is controlled by
13252    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
13253
13254 /* The state of the fsm controlling condition codes are:
13255    0: normal, do nothing special
13256    1: make ASM_OUTPUT_OPCODE not output this instruction
13257    2: make ASM_OUTPUT_OPCODE not output this instruction
13258    3: make instructions conditional
13259    4: make instructions conditional
13260
13261    State transitions (state->state by whom under condition):
13262    0 -> 1 final_prescan_insn if the `target' is a label
13263    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
13264    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
13265    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
13266    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
13267           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
13268    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
13269           (the target insn is arm_target_insn).
13270
13271    If the jump clobbers the conditions then we use states 2 and 4.
13272
13273    A similar thing can be done with conditional return insns.
13274
13275    XXX In case the `target' is an unconditional branch, this conditionalising
13276    of the instructions always reduces code size, but not always execution
13277    time.  But then, I want to reduce the code size to somewhere near what
13278    /bin/cc produces.  */
13279
13280 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
13281    instructions.  When a COND_EXEC instruction is seen the subsequent
13282    instructions are scanned so that multiple conditional instructions can be
13283    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
13284    specify the length and true/false mask for the IT block.  These will be
13285    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
13286
13287 /* Returns the index of the ARM condition code string in
13288    `arm_condition_codes'.  COMPARISON should be an rtx like
13289    `(eq (...) (...))'.  */
13290 static enum arm_cond_code
13291 get_arm_condition_code (rtx comparison)
13292 {
13293   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
13294   int code;
13295   enum rtx_code comp_code = GET_CODE (comparison);
13296
13297   if (GET_MODE_CLASS (mode) != MODE_CC)
13298     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
13299                            XEXP (comparison, 1));
13300
13301   switch (mode)
13302     {
13303     case CC_DNEmode: code = ARM_NE; goto dominance;
13304     case CC_DEQmode: code = ARM_EQ; goto dominance;
13305     case CC_DGEmode: code = ARM_GE; goto dominance;
13306     case CC_DGTmode: code = ARM_GT; goto dominance;
13307     case CC_DLEmode: code = ARM_LE; goto dominance;
13308     case CC_DLTmode: code = ARM_LT; goto dominance;
13309     case CC_DGEUmode: code = ARM_CS; goto dominance;
13310     case CC_DGTUmode: code = ARM_HI; goto dominance;
13311     case CC_DLEUmode: code = ARM_LS; goto dominance;
13312     case CC_DLTUmode: code = ARM_CC;
13313
13314     dominance:
13315       gcc_assert (comp_code == EQ || comp_code == NE);
13316
13317       if (comp_code == EQ)
13318         return ARM_INVERSE_CONDITION_CODE (code);
13319       return code;
13320
13321     case CC_NOOVmode:
13322       switch (comp_code)
13323         {
13324         case NE: return ARM_NE;
13325         case EQ: return ARM_EQ;
13326         case GE: return ARM_PL;
13327         case LT: return ARM_MI;
13328         default: gcc_unreachable ();
13329         }
13330
13331     case CC_Zmode:
13332       switch (comp_code)
13333         {
13334         case NE: return ARM_NE;
13335         case EQ: return ARM_EQ;
13336         default: gcc_unreachable ();
13337         }
13338
13339     case CC_Nmode:
13340       switch (comp_code)
13341         {
13342         case NE: return ARM_MI;
13343         case EQ: return ARM_PL;
13344         default: gcc_unreachable ();
13345         }
13346
13347     case CCFPEmode:
13348     case CCFPmode:
13349       /* These encodings assume that AC=1 in the FPA system control
13350          byte.  This allows us to handle all cases except UNEQ and
13351          LTGT.  */
13352       switch (comp_code)
13353         {
13354         case GE: return ARM_GE;
13355         case GT: return ARM_GT;
13356         case LE: return ARM_LS;
13357         case LT: return ARM_MI;
13358         case NE: return ARM_NE;
13359         case EQ: return ARM_EQ;
13360         case ORDERED: return ARM_VC;
13361         case UNORDERED: return ARM_VS;
13362         case UNLT: return ARM_LT;
13363         case UNLE: return ARM_LE;
13364         case UNGT: return ARM_HI;
13365         case UNGE: return ARM_PL;
13366           /* UNEQ and LTGT do not have a representation.  */
13367         case UNEQ: /* Fall through.  */
13368         case LTGT: /* Fall through.  */
13369         default: gcc_unreachable ();
13370         }
13371
13372     case CC_SWPmode:
13373       switch (comp_code)
13374         {
13375         case NE: return ARM_NE;
13376         case EQ: return ARM_EQ;
13377         case GE: return ARM_LE;
13378         case GT: return ARM_LT;
13379         case LE: return ARM_GE;
13380         case LT: return ARM_GT;
13381         case GEU: return ARM_LS;
13382         case GTU: return ARM_CC;
13383         case LEU: return ARM_CS;
13384         case LTU: return ARM_HI;
13385         default: gcc_unreachable ();
13386         }
13387
13388     case CC_Cmode:
13389       switch (comp_code)
13390       {
13391       case LTU: return ARM_CS;
13392       case GEU: return ARM_CC;
13393       default: gcc_unreachable ();
13394       }
13395
13396     case CCmode:
13397       switch (comp_code)
13398         {
13399         case NE: return ARM_NE;
13400         case EQ: return ARM_EQ;
13401         case GE: return ARM_GE;
13402         case GT: return ARM_GT;
13403         case LE: return ARM_LE;
13404         case LT: return ARM_LT;
13405         case GEU: return ARM_CS;
13406         case GTU: return ARM_HI;
13407         case LEU: return ARM_LS;
13408         case LTU: return ARM_CC;
13409         default: gcc_unreachable ();
13410         }
13411
13412     default: gcc_unreachable ();
13413     }
13414 }
13415
13416 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
13417    instructions.  */
13418 void
13419 thumb2_final_prescan_insn (rtx insn)
13420 {
13421   rtx first_insn = insn;
13422   rtx body = PATTERN (insn);
13423   rtx predicate;
13424   enum arm_cond_code code;
13425   int n;
13426   int mask;
13427
13428   /* Remove the previous insn from the count of insns to be output.  */
13429   if (arm_condexec_count)
13430       arm_condexec_count--;
13431
13432   /* Nothing to do if we are already inside a conditional block.  */
13433   if (arm_condexec_count)
13434     return;
13435
13436   if (GET_CODE (body) != COND_EXEC)
13437     return;
13438
13439   /* Conditional jumps are implemented directly.  */
13440   if (GET_CODE (insn) == JUMP_INSN)
13441     return;
13442
13443   predicate = COND_EXEC_TEST (body);
13444   arm_current_cc = get_arm_condition_code (predicate);
13445
13446   n = get_attr_ce_count (insn);
13447   arm_condexec_count = 1;
13448   arm_condexec_mask = (1 << n) - 1;
13449   arm_condexec_masklen = n;
13450   /* See if subsequent instructions can be combined into the same block.  */
13451   for (;;)
13452     {
13453       insn = next_nonnote_insn (insn);
13454
13455       /* Jumping into the middle of an IT block is illegal, so a label or
13456          barrier terminates the block.  */
13457       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
13458         break;
13459
13460       body = PATTERN (insn);
13461       /* USE and CLOBBER aren't really insns, so just skip them.  */
13462       if (GET_CODE (body) == USE
13463           || GET_CODE (body) == CLOBBER)
13464         continue;
13465
13466       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
13467       if (GET_CODE (body) != COND_EXEC)
13468         break;
13469       /* Allow up to 4 conditionally executed instructions in a block.  */
13470       n = get_attr_ce_count (insn);
13471       if (arm_condexec_masklen + n > 4)
13472         break;
13473
13474       predicate = COND_EXEC_TEST (body);
13475       code = get_arm_condition_code (predicate);
13476       mask = (1 << n) - 1;
13477       if (arm_current_cc == code)
13478         arm_condexec_mask |= (mask << arm_condexec_masklen);
13479       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
13480         break;
13481
13482       arm_condexec_count++;
13483       arm_condexec_masklen += n;
13484
13485       /* A jump must be the last instruction in a conditional block.  */
13486       if (GET_CODE(insn) == JUMP_INSN)
13487         break;
13488     }
13489   /* Restore recog_data (getting the attributes of other insns can
13490      destroy this array, but final.c assumes that it remains intact
13491      across this call).  */
13492   extract_constrain_insn_cached (first_insn);
13493 }
13494
13495 void
13496 arm_final_prescan_insn (rtx insn)
13497 {
13498   /* BODY will hold the body of INSN.  */
13499   rtx body = PATTERN (insn);
13500
13501   /* This will be 1 if trying to repeat the trick, and things need to be
13502      reversed if it appears to fail.  */
13503   int reverse = 0;
13504
13505   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
13506      taken are clobbered, even if the rtl suggests otherwise.  It also
13507      means that we have to grub around within the jump expression to find
13508      out what the conditions are when the jump isn't taken.  */
13509   int jump_clobbers = 0;
13510
13511   /* If we start with a return insn, we only succeed if we find another one.  */
13512   int seeking_return = 0;
13513
13514   /* START_INSN will hold the insn from where we start looking.  This is the
13515      first insn after the following code_label if REVERSE is true.  */
13516   rtx start_insn = insn;
13517
13518   /* If in state 4, check if the target branch is reached, in order to
13519      change back to state 0.  */
13520   if (arm_ccfsm_state == 4)
13521     {
13522       if (insn == arm_target_insn)
13523         {
13524           arm_target_insn = NULL;
13525           arm_ccfsm_state = 0;
13526         }
13527       return;
13528     }
13529
13530   /* If in state 3, it is possible to repeat the trick, if this insn is an
13531      unconditional branch to a label, and immediately following this branch
13532      is the previous target label which is only used once, and the label this
13533      branch jumps to is not too far off.  */
13534   if (arm_ccfsm_state == 3)
13535     {
13536       if (simplejump_p (insn))
13537         {
13538           start_insn = next_nonnote_insn (start_insn);
13539           if (GET_CODE (start_insn) == BARRIER)
13540             {
13541               /* XXX Isn't this always a barrier?  */
13542               start_insn = next_nonnote_insn (start_insn);
13543             }
13544           if (GET_CODE (start_insn) == CODE_LABEL
13545               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
13546               && LABEL_NUSES (start_insn) == 1)
13547             reverse = TRUE;
13548           else
13549             return;
13550         }
13551       else if (GET_CODE (body) == RETURN)
13552         {
13553           start_insn = next_nonnote_insn (start_insn);
13554           if (GET_CODE (start_insn) == BARRIER)
13555             start_insn = next_nonnote_insn (start_insn);
13556           if (GET_CODE (start_insn) == CODE_LABEL
13557               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
13558               && LABEL_NUSES (start_insn) == 1)
13559             {
13560               reverse = TRUE;
13561               seeking_return = 1;
13562             }
13563           else
13564             return;
13565         }
13566       else
13567         return;
13568     }
13569
13570   gcc_assert (!arm_ccfsm_state || reverse);
13571   if (GET_CODE (insn) != JUMP_INSN)
13572     return;
13573
13574   /* This jump might be paralleled with a clobber of the condition codes
13575      the jump should always come first */
13576   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
13577     body = XVECEXP (body, 0, 0);
13578
13579   if (reverse
13580       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
13581           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
13582     {
13583       int insns_skipped;
13584       int fail = FALSE, succeed = FALSE;
13585       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
13586       int then_not_else = TRUE;
13587       rtx this_insn = start_insn, label = 0;
13588
13589       /* If the jump cannot be done with one instruction, we cannot
13590          conditionally execute the instruction in the inverse case.  */
13591       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
13592         {
13593           jump_clobbers = 1;
13594           return;
13595         }
13596
13597       /* Register the insn jumped to.  */
13598       if (reverse)
13599         {
13600           if (!seeking_return)
13601             label = XEXP (SET_SRC (body), 0);
13602         }
13603       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
13604         label = XEXP (XEXP (SET_SRC (body), 1), 0);
13605       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
13606         {
13607           label = XEXP (XEXP (SET_SRC (body), 2), 0);
13608           then_not_else = FALSE;
13609         }
13610       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
13611         seeking_return = 1;
13612       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
13613         {
13614           seeking_return = 1;
13615           then_not_else = FALSE;
13616         }
13617       else
13618         gcc_unreachable ();
13619
13620       /* See how many insns this branch skips, and what kind of insns.  If all
13621          insns are okay, and the label or unconditional branch to the same
13622          label is not too far away, succeed.  */
13623       for (insns_skipped = 0;
13624            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
13625         {
13626           rtx scanbody;
13627
13628           this_insn = next_nonnote_insn (this_insn);
13629           if (!this_insn)
13630             break;
13631
13632           switch (GET_CODE (this_insn))
13633             {
13634             case CODE_LABEL:
13635               /* Succeed if it is the target label, otherwise fail since
13636                  control falls in from somewhere else.  */
13637               if (this_insn == label)
13638                 {
13639                   if (jump_clobbers)
13640                     {
13641                       arm_ccfsm_state = 2;
13642                       this_insn = next_nonnote_insn (this_insn);
13643                     }
13644                   else
13645                     arm_ccfsm_state = 1;
13646                   succeed = TRUE;
13647                 }
13648               else
13649                 fail = TRUE;
13650               break;
13651
13652             case BARRIER:
13653               /* Succeed if the following insn is the target label.
13654                  Otherwise fail.
13655                  If return insns are used then the last insn in a function
13656                  will be a barrier.  */
13657               this_insn = next_nonnote_insn (this_insn);
13658               if (this_insn && this_insn == label)
13659                 {
13660                   if (jump_clobbers)
13661                     {
13662                       arm_ccfsm_state = 2;
13663                       this_insn = next_nonnote_insn (this_insn);
13664                     }
13665                   else
13666                     arm_ccfsm_state = 1;
13667                   succeed = TRUE;
13668                 }
13669               else
13670                 fail = TRUE;
13671               break;
13672
13673             case CALL_INSN:
13674               /* The AAPCS says that conditional calls should not be
13675                  used since they make interworking inefficient (the
13676                  linker can't transform BL<cond> into BLX).  That's
13677                  only a problem if the machine has BLX.  */
13678               if (arm_arch5)
13679                 {
13680                   fail = TRUE;
13681                   break;
13682                 }
13683
13684               /* Succeed if the following insn is the target label, or
13685                  if the following two insns are a barrier and the
13686                  target label.  */
13687               this_insn = next_nonnote_insn (this_insn);
13688               if (this_insn && GET_CODE (this_insn) == BARRIER)
13689                 this_insn = next_nonnote_insn (this_insn);
13690
13691               if (this_insn && this_insn == label
13692                   && insns_skipped < max_insns_skipped)
13693                 {
13694                   if (jump_clobbers)
13695                     {
13696                       arm_ccfsm_state = 2;
13697                       this_insn = next_nonnote_insn (this_insn);
13698                     }
13699                   else
13700                     arm_ccfsm_state = 1;
13701                   succeed = TRUE;
13702                 }
13703               else
13704                 fail = TRUE;
13705               break;
13706
13707             case JUMP_INSN:
13708               /* If this is an unconditional branch to the same label, succeed.
13709                  If it is to another label, do nothing.  If it is conditional,
13710                  fail.  */
13711               /* XXX Probably, the tests for SET and the PC are
13712                  unnecessary.  */
13713
13714               scanbody = PATTERN (this_insn);
13715               if (GET_CODE (scanbody) == SET
13716                   && GET_CODE (SET_DEST (scanbody)) == PC)
13717                 {
13718                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
13719                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
13720                     {
13721                       arm_ccfsm_state = 2;
13722                       succeed = TRUE;
13723                     }
13724                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
13725                     fail = TRUE;
13726                 }
13727               /* Fail if a conditional return is undesirable (e.g. on a
13728                  StrongARM), but still allow this if optimizing for size.  */
13729               else if (GET_CODE (scanbody) == RETURN
13730                        && !use_return_insn (TRUE, NULL)
13731                        && !optimize_size)
13732                 fail = TRUE;
13733               else if (GET_CODE (scanbody) == RETURN
13734                        && seeking_return)
13735                 {
13736                   arm_ccfsm_state = 2;
13737                   succeed = TRUE;
13738                 }
13739               else if (GET_CODE (scanbody) == PARALLEL)
13740                 {
13741                   switch (get_attr_conds (this_insn))
13742                     {
13743                     case CONDS_NOCOND:
13744                       break;
13745                     default:
13746                       fail = TRUE;
13747                       break;
13748                     }
13749                 }
13750               else
13751                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
13752
13753               break;
13754
13755             case INSN:
13756               /* Instructions using or affecting the condition codes make it
13757                  fail.  */
13758               scanbody = PATTERN (this_insn);
13759               if (!(GET_CODE (scanbody) == SET
13760                     || GET_CODE (scanbody) == PARALLEL)
13761                   || get_attr_conds (this_insn) != CONDS_NOCOND)
13762                 fail = TRUE;
13763
13764               /* A conditional cirrus instruction must be followed by
13765                  a non Cirrus instruction.  However, since we
13766                  conditionalize instructions in this function and by
13767                  the time we get here we can't add instructions
13768                  (nops), because shorten_branches() has already been
13769                  called, we will disable conditionalizing Cirrus
13770                  instructions to be safe.  */
13771               if (GET_CODE (scanbody) != USE
13772                   && GET_CODE (scanbody) != CLOBBER
13773                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
13774                 fail = TRUE;
13775               break;
13776
13777             default:
13778               break;
13779             }
13780         }
13781       if (succeed)
13782         {
13783           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
13784             arm_target_label = CODE_LABEL_NUMBER (label);
13785           else
13786             {
13787               gcc_assert (seeking_return || arm_ccfsm_state == 2);
13788
13789               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
13790                 {
13791                   this_insn = next_nonnote_insn (this_insn);
13792                   gcc_assert (!this_insn
13793                               || (GET_CODE (this_insn) != BARRIER
13794                                   && GET_CODE (this_insn) != CODE_LABEL));
13795                 }
13796               if (!this_insn)
13797                 {
13798                   /* Oh, dear! we ran off the end.. give up.  */
13799                   extract_constrain_insn_cached (insn);
13800                   arm_ccfsm_state = 0;
13801                   arm_target_insn = NULL;
13802                   return;
13803                 }
13804               arm_target_insn = this_insn;
13805             }
13806           if (jump_clobbers)
13807             {
13808               gcc_assert (!reverse);
13809               arm_current_cc =
13810                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
13811                                                             0), 0), 1));
13812               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
13813                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
13814               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
13815                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
13816             }
13817           else
13818             {
13819               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
13820                  what it was.  */
13821               if (!reverse)
13822                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
13823                                                                0));
13824             }
13825
13826           if (reverse || then_not_else)
13827             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
13828         }
13829
13830       /* Restore recog_data (getting the attributes of other insns can
13831          destroy this array, but final.c assumes that it remains intact
13832          across this call.  */
13833       extract_constrain_insn_cached (insn);
13834     }
13835 }
13836
13837 /* Output IT instructions.  */
13838 void
13839 thumb2_asm_output_opcode (FILE * stream)
13840 {
13841   char buff[5];
13842   int n;
13843
13844   if (arm_condexec_mask)
13845     {
13846       for (n = 0; n < arm_condexec_masklen; n++)
13847         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
13848       buff[n] = 0;
13849       asm_fprintf(stream, "i%s\t%s\n\t", buff,
13850                   arm_condition_codes[arm_current_cc]);
13851       arm_condexec_mask = 0;
13852     }
13853 }
13854
13855 /* Returns true if REGNO is a valid register
13856    for holding a quantity of type MODE.  */
13857 int
13858 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
13859 {
13860   if (GET_MODE_CLASS (mode) == MODE_CC)
13861     return (regno == CC_REGNUM
13862             || (TARGET_HARD_FLOAT && TARGET_VFP
13863                 && regno == VFPCC_REGNUM));
13864
13865   if (TARGET_THUMB1)
13866     /* For the Thumb we only allow values bigger than SImode in
13867        registers 0 - 6, so that there is always a second low
13868        register available to hold the upper part of the value.
13869        We probably we ought to ensure that the register is the
13870        start of an even numbered register pair.  */
13871     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
13872
13873   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
13874       && IS_CIRRUS_REGNUM (regno))
13875     /* We have outlawed SI values in Cirrus registers because they
13876        reside in the lower 32 bits, but SF values reside in the
13877        upper 32 bits.  This causes gcc all sorts of grief.  We can't
13878        even split the registers into pairs because Cirrus SI values
13879        get sign extended to 64bits-- aldyh.  */
13880     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
13881
13882   if (TARGET_HARD_FLOAT && TARGET_VFP
13883       && IS_VFP_REGNUM (regno))
13884     {
13885       if (mode == SFmode || mode == SImode)
13886         return VFP_REGNO_OK_FOR_SINGLE (regno);
13887
13888       if (mode == DFmode)
13889         return VFP_REGNO_OK_FOR_DOUBLE (regno);
13890
13891       if (TARGET_NEON)
13892         return (VALID_NEON_DREG_MODE (mode) && VFP_REGNO_OK_FOR_DOUBLE (regno))
13893                || (VALID_NEON_QREG_MODE (mode)
13894                    && NEON_REGNO_OK_FOR_QUAD (regno))
13895                || (mode == TImode && NEON_REGNO_OK_FOR_NREGS (regno, 2))
13896                || (mode == EImode && NEON_REGNO_OK_FOR_NREGS (regno, 3))
13897                || (mode == OImode && NEON_REGNO_OK_FOR_NREGS (regno, 4))
13898                || (mode == CImode && NEON_REGNO_OK_FOR_NREGS (regno, 6))
13899                || (mode == XImode && NEON_REGNO_OK_FOR_NREGS (regno, 8));
13900
13901       return FALSE;
13902     }
13903
13904   if (TARGET_REALLY_IWMMXT)
13905     {
13906       if (IS_IWMMXT_GR_REGNUM (regno))
13907         return mode == SImode;
13908
13909       if (IS_IWMMXT_REGNUM (regno))
13910         return VALID_IWMMXT_REG_MODE (mode);
13911     }
13912   
13913   /* We allow any value to be stored in the general registers.
13914      Restrict doubleword quantities to even register pairs so that we can
13915      use ldrd.  Do not allow Neon structure opaque modes in general registers;
13916      they would use too many.  */
13917   if (regno <= LAST_ARM_REGNUM)
13918     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0)
13919       && !VALID_NEON_STRUCT_MODE (mode);
13920
13921   if (regno == FRAME_POINTER_REGNUM
13922       || regno == ARG_POINTER_REGNUM)
13923     /* We only allow integers in the fake hard registers.  */
13924     return GET_MODE_CLASS (mode) == MODE_INT;
13925
13926   /* The only registers left are the FPA registers
13927      which we only allow to hold FP values.  */
13928   return (TARGET_HARD_FLOAT && TARGET_FPA
13929           && GET_MODE_CLASS (mode) == MODE_FLOAT
13930           && regno >= FIRST_FPA_REGNUM
13931           && regno <= LAST_FPA_REGNUM);
13932 }
13933
13934 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
13935    not used in arm mode.  */
13936 int
13937 arm_regno_class (int regno)
13938 {
13939   if (TARGET_THUMB1)
13940     {
13941       if (regno == STACK_POINTER_REGNUM)
13942         return STACK_REG;
13943       if (regno == CC_REGNUM)
13944         return CC_REG;
13945       if (regno < 8)
13946         return LO_REGS;
13947       return HI_REGS;
13948     }
13949
13950   if (TARGET_THUMB2 && regno < 8)
13951     return LO_REGS;
13952
13953   if (   regno <= LAST_ARM_REGNUM
13954       || regno == FRAME_POINTER_REGNUM
13955       || regno == ARG_POINTER_REGNUM)
13956     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
13957
13958   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
13959     return TARGET_THUMB2 ? CC_REG : NO_REGS;
13960
13961   if (IS_CIRRUS_REGNUM (regno))
13962     return CIRRUS_REGS;
13963
13964   if (IS_VFP_REGNUM (regno))
13965     {
13966       if (regno <= D7_VFP_REGNUM)
13967         return VFP_D0_D7_REGS;
13968       else if (regno <= LAST_LO_VFP_REGNUM)
13969         return VFP_LO_REGS;
13970       else
13971         return VFP_HI_REGS;
13972     }
13973
13974   if (IS_IWMMXT_REGNUM (regno))
13975     return IWMMXT_REGS;
13976
13977   if (IS_IWMMXT_GR_REGNUM (regno))
13978     return IWMMXT_GR_REGS;
13979
13980   return FPA_REGS;
13981 }
13982
13983 /* Handle a special case when computing the offset
13984    of an argument from the frame pointer.  */
13985 int
13986 arm_debugger_arg_offset (int value, rtx addr)
13987 {
13988   rtx insn;
13989
13990   /* We are only interested if dbxout_parms() failed to compute the offset.  */
13991   if (value != 0)
13992     return 0;
13993
13994   /* We can only cope with the case where the address is held in a register.  */
13995   if (GET_CODE (addr) != REG)
13996     return 0;
13997
13998   /* If we are using the frame pointer to point at the argument, then
13999      an offset of 0 is correct.  */
14000   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
14001     return 0;
14002
14003   /* If we are using the stack pointer to point at the
14004      argument, then an offset of 0 is correct.  */
14005   /* ??? Check this is consistent with thumb2 frame layout.  */
14006   if ((TARGET_THUMB || !frame_pointer_needed)
14007       && REGNO (addr) == SP_REGNUM)
14008     return 0;
14009
14010   /* Oh dear.  The argument is pointed to by a register rather
14011      than being held in a register, or being stored at a known
14012      offset from the frame pointer.  Since GDB only understands
14013      those two kinds of argument we must translate the address
14014      held in the register into an offset from the frame pointer.
14015      We do this by searching through the insns for the function
14016      looking to see where this register gets its value.  If the
14017      register is initialized from the frame pointer plus an offset
14018      then we are in luck and we can continue, otherwise we give up.
14019
14020      This code is exercised by producing debugging information
14021      for a function with arguments like this:
14022
14023            double func (double a, double b, int c, double d) {return d;}
14024
14025      Without this code the stab for parameter 'd' will be set to
14026      an offset of 0 from the frame pointer, rather than 8.  */
14027
14028   /* The if() statement says:
14029
14030      If the insn is a normal instruction
14031      and if the insn is setting the value in a register
14032      and if the register being set is the register holding the address of the argument
14033      and if the address is computing by an addition
14034      that involves adding to a register
14035      which is the frame pointer
14036      a constant integer
14037
14038      then...  */
14039
14040   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14041     {
14042       if (   GET_CODE (insn) == INSN
14043           && GET_CODE (PATTERN (insn)) == SET
14044           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
14045           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
14046           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
14047           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
14048           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
14049              )
14050         {
14051           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
14052
14053           break;
14054         }
14055     }
14056
14057   if (value == 0)
14058     {
14059       debug_rtx (addr);
14060       warning (0, "unable to compute real location of stacked parameter");
14061       value = 8; /* XXX magic hack */
14062     }
14063
14064   return value;
14065 }
14066 \f
14067 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
14068   do                                                                    \
14069     {                                                                   \
14070       if ((MASK) & insn_flags)                                          \
14071         add_builtin_function ((NAME), (TYPE), (CODE),                   \
14072                              BUILT_IN_MD, NULL, NULL_TREE);             \
14073     }                                                                   \
14074   while (0)
14075
14076 struct builtin_description
14077 {
14078   const unsigned int       mask;
14079   const enum insn_code     icode;
14080   const char * const       name;
14081   const enum arm_builtins  code;
14082   const enum rtx_code      comparison;
14083   const unsigned int       flag;
14084 };
14085
14086 static const struct builtin_description bdesc_2arg[] =
14087 {
14088 #define IWMMXT_BUILTIN(code, string, builtin) \
14089   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
14090     ARM_BUILTIN_##builtin, 0, 0 },
14091
14092   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
14093   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
14094   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
14095   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
14096   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
14097   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
14098   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
14099   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
14100   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
14101   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
14102   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
14103   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
14104   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
14105   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
14106   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
14107   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
14108   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
14109   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
14110   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
14111   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
14112   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
14113   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
14114   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
14115   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
14116   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
14117   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
14118   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
14119   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
14120   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
14121   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
14122   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
14123   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
14124   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
14125   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
14126   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
14127   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
14128   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
14129   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
14130   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
14131   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
14132   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
14133   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
14134   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
14135   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
14136   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
14137   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
14138   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
14139   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
14140   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
14141   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
14142   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
14143   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
14144   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
14145   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
14146   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
14147   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
14148   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
14149   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
14150
14151 #define IWMMXT_BUILTIN2(code, builtin) \
14152   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
14153
14154   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
14155   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
14156   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
14157   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
14158   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
14159   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
14160   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
14161   IWMMXT_BUILTIN2 (ashlv4hi3_iwmmxt, WSLLHI)
14162   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
14163   IWMMXT_BUILTIN2 (ashlv2si3_iwmmxt, WSLLWI)
14164   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
14165   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
14166   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
14167   IWMMXT_BUILTIN2 (lshrv4hi3_iwmmxt, WSRLHI)
14168   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
14169   IWMMXT_BUILTIN2 (lshrv2si3_iwmmxt, WSRLWI)
14170   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
14171   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
14172   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
14173   IWMMXT_BUILTIN2 (ashrv4hi3_iwmmxt, WSRAHI)
14174   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
14175   IWMMXT_BUILTIN2 (ashrv2si3_iwmmxt, WSRAWI)
14176   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
14177   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
14178   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
14179   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
14180   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
14181   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
14182   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
14183   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
14184   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
14185   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
14186 };
14187
14188 static const struct builtin_description bdesc_1arg[] =
14189 {
14190   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
14191   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
14192   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
14193   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
14194   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
14195   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
14196   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
14197   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
14198   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
14199   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
14200   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
14201   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
14202   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
14203   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
14204   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
14205   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
14206   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
14207   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
14208 };
14209
14210 /* Set up all the iWMMXt builtins.  This is
14211    not called if TARGET_IWMMXT is zero.  */
14212
14213 static void
14214 arm_init_iwmmxt_builtins (void)
14215 {
14216   const struct builtin_description * d;
14217   size_t i;
14218   tree endlink = void_list_node;
14219
14220   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14221   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14222   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14223
14224   tree int_ftype_int
14225     = build_function_type (integer_type_node,
14226                            tree_cons (NULL_TREE, integer_type_node, endlink));
14227   tree v8qi_ftype_v8qi_v8qi_int
14228     = build_function_type (V8QI_type_node,
14229                            tree_cons (NULL_TREE, V8QI_type_node,
14230                                       tree_cons (NULL_TREE, V8QI_type_node,
14231                                                  tree_cons (NULL_TREE,
14232                                                             integer_type_node,
14233                                                             endlink))));
14234   tree v4hi_ftype_v4hi_int
14235     = build_function_type (V4HI_type_node,
14236                            tree_cons (NULL_TREE, V4HI_type_node,
14237                                       tree_cons (NULL_TREE, integer_type_node,
14238                                                  endlink)));
14239   tree v2si_ftype_v2si_int
14240     = build_function_type (V2SI_type_node,
14241                            tree_cons (NULL_TREE, V2SI_type_node,
14242                                       tree_cons (NULL_TREE, integer_type_node,
14243                                                  endlink)));
14244   tree v2si_ftype_di_di
14245     = build_function_type (V2SI_type_node,
14246                            tree_cons (NULL_TREE, long_long_integer_type_node,
14247                                       tree_cons (NULL_TREE, long_long_integer_type_node,
14248                                                  endlink)));
14249   tree di_ftype_di_int
14250     = build_function_type (long_long_integer_type_node,
14251                            tree_cons (NULL_TREE, long_long_integer_type_node,
14252                                       tree_cons (NULL_TREE, integer_type_node,
14253                                                  endlink)));
14254   tree di_ftype_di_int_int
14255     = build_function_type (long_long_integer_type_node,
14256                            tree_cons (NULL_TREE, long_long_integer_type_node,
14257                                       tree_cons (NULL_TREE, integer_type_node,
14258                                                  tree_cons (NULL_TREE,
14259                                                             integer_type_node,
14260                                                             endlink))));
14261   tree int_ftype_v8qi
14262     = build_function_type (integer_type_node,
14263                            tree_cons (NULL_TREE, V8QI_type_node,
14264                                       endlink));
14265   tree int_ftype_v4hi
14266     = build_function_type (integer_type_node,
14267                            tree_cons (NULL_TREE, V4HI_type_node,
14268                                       endlink));
14269   tree int_ftype_v2si
14270     = build_function_type (integer_type_node,
14271                            tree_cons (NULL_TREE, V2SI_type_node,
14272                                       endlink));
14273   tree int_ftype_v8qi_int
14274     = build_function_type (integer_type_node,
14275                            tree_cons (NULL_TREE, V8QI_type_node,
14276                                       tree_cons (NULL_TREE, integer_type_node,
14277                                                  endlink)));
14278   tree int_ftype_v4hi_int
14279     = build_function_type (integer_type_node,
14280                            tree_cons (NULL_TREE, V4HI_type_node,
14281                                       tree_cons (NULL_TREE, integer_type_node,
14282                                                  endlink)));
14283   tree int_ftype_v2si_int
14284     = build_function_type (integer_type_node,
14285                            tree_cons (NULL_TREE, V2SI_type_node,
14286                                       tree_cons (NULL_TREE, integer_type_node,
14287                                                  endlink)));
14288   tree v8qi_ftype_v8qi_int_int
14289     = build_function_type (V8QI_type_node,
14290                            tree_cons (NULL_TREE, V8QI_type_node,
14291                                       tree_cons (NULL_TREE, integer_type_node,
14292                                                  tree_cons (NULL_TREE,
14293                                                             integer_type_node,
14294                                                             endlink))));
14295   tree v4hi_ftype_v4hi_int_int
14296     = build_function_type (V4HI_type_node,
14297                            tree_cons (NULL_TREE, V4HI_type_node,
14298                                       tree_cons (NULL_TREE, integer_type_node,
14299                                                  tree_cons (NULL_TREE,
14300                                                             integer_type_node,
14301                                                             endlink))));
14302   tree v2si_ftype_v2si_int_int
14303     = build_function_type (V2SI_type_node,
14304                            tree_cons (NULL_TREE, V2SI_type_node,
14305                                       tree_cons (NULL_TREE, integer_type_node,
14306                                                  tree_cons (NULL_TREE,
14307                                                             integer_type_node,
14308                                                             endlink))));
14309   /* Miscellaneous.  */
14310   tree v8qi_ftype_v4hi_v4hi
14311     = build_function_type (V8QI_type_node,
14312                            tree_cons (NULL_TREE, V4HI_type_node,
14313                                       tree_cons (NULL_TREE, V4HI_type_node,
14314                                                  endlink)));
14315   tree v4hi_ftype_v2si_v2si
14316     = build_function_type (V4HI_type_node,
14317                            tree_cons (NULL_TREE, V2SI_type_node,
14318                                       tree_cons (NULL_TREE, V2SI_type_node,
14319                                                  endlink)));
14320   tree v2si_ftype_v4hi_v4hi
14321     = build_function_type (V2SI_type_node,
14322                            tree_cons (NULL_TREE, V4HI_type_node,
14323                                       tree_cons (NULL_TREE, V4HI_type_node,
14324                                                  endlink)));
14325   tree v2si_ftype_v8qi_v8qi
14326     = build_function_type (V2SI_type_node,
14327                            tree_cons (NULL_TREE, V8QI_type_node,
14328                                       tree_cons (NULL_TREE, V8QI_type_node,
14329                                                  endlink)));
14330   tree v4hi_ftype_v4hi_di
14331     = build_function_type (V4HI_type_node,
14332                            tree_cons (NULL_TREE, V4HI_type_node,
14333                                       tree_cons (NULL_TREE,
14334                                                  long_long_integer_type_node,
14335                                                  endlink)));
14336   tree v2si_ftype_v2si_di
14337     = build_function_type (V2SI_type_node,
14338                            tree_cons (NULL_TREE, V2SI_type_node,
14339                                       tree_cons (NULL_TREE,
14340                                                  long_long_integer_type_node,
14341                                                  endlink)));
14342   tree void_ftype_int_int
14343     = build_function_type (void_type_node,
14344                            tree_cons (NULL_TREE, integer_type_node,
14345                                       tree_cons (NULL_TREE, integer_type_node,
14346                                                  endlink)));
14347   tree di_ftype_void
14348     = build_function_type (long_long_unsigned_type_node, endlink);
14349   tree di_ftype_v8qi
14350     = build_function_type (long_long_integer_type_node,
14351                            tree_cons (NULL_TREE, V8QI_type_node,
14352                                       endlink));
14353   tree di_ftype_v4hi
14354     = build_function_type (long_long_integer_type_node,
14355                            tree_cons (NULL_TREE, V4HI_type_node,
14356                                       endlink));
14357   tree di_ftype_v2si
14358     = build_function_type (long_long_integer_type_node,
14359                            tree_cons (NULL_TREE, V2SI_type_node,
14360                                       endlink));
14361   tree v2si_ftype_v4hi
14362     = build_function_type (V2SI_type_node,
14363                            tree_cons (NULL_TREE, V4HI_type_node,
14364                                       endlink));
14365   tree v4hi_ftype_v8qi
14366     = build_function_type (V4HI_type_node,
14367                            tree_cons (NULL_TREE, V8QI_type_node,
14368                                       endlink));
14369
14370   tree di_ftype_di_v4hi_v4hi
14371     = build_function_type (long_long_unsigned_type_node,
14372                            tree_cons (NULL_TREE,
14373                                       long_long_unsigned_type_node,
14374                                       tree_cons (NULL_TREE, V4HI_type_node,
14375                                                  tree_cons (NULL_TREE,
14376                                                             V4HI_type_node,
14377                                                             endlink))));
14378
14379   tree di_ftype_v4hi_v4hi
14380     = build_function_type (long_long_unsigned_type_node,
14381                            tree_cons (NULL_TREE, V4HI_type_node,
14382                                       tree_cons (NULL_TREE, V4HI_type_node,
14383                                                  endlink)));
14384
14385   /* Normal vector binops.  */
14386   tree v8qi_ftype_v8qi_v8qi
14387     = build_function_type (V8QI_type_node,
14388                            tree_cons (NULL_TREE, V8QI_type_node,
14389                                       tree_cons (NULL_TREE, V8QI_type_node,
14390                                                  endlink)));
14391   tree v4hi_ftype_v4hi_v4hi
14392     = build_function_type (V4HI_type_node,
14393                            tree_cons (NULL_TREE, V4HI_type_node,
14394                                       tree_cons (NULL_TREE, V4HI_type_node,
14395                                                  endlink)));
14396   tree v2si_ftype_v2si_v2si
14397     = build_function_type (V2SI_type_node,
14398                            tree_cons (NULL_TREE, V2SI_type_node,
14399                                       tree_cons (NULL_TREE, V2SI_type_node,
14400                                                  endlink)));
14401   tree di_ftype_di_di
14402     = build_function_type (long_long_unsigned_type_node,
14403                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
14404                                       tree_cons (NULL_TREE,
14405                                                  long_long_unsigned_type_node,
14406                                                  endlink)));
14407
14408   /* Add all builtins that are more or less simple operations on two
14409      operands.  */
14410   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
14411     {
14412       /* Use one of the operands; the target can have a different mode for
14413          mask-generating compares.  */
14414       enum machine_mode mode;
14415       tree type;
14416
14417       if (d->name == 0)
14418         continue;
14419
14420       mode = insn_data[d->icode].operand[1].mode;
14421
14422       switch (mode)
14423         {
14424         case V8QImode:
14425           type = v8qi_ftype_v8qi_v8qi;
14426           break;
14427         case V4HImode:
14428           type = v4hi_ftype_v4hi_v4hi;
14429           break;
14430         case V2SImode:
14431           type = v2si_ftype_v2si_v2si;
14432           break;
14433         case DImode:
14434           type = di_ftype_di_di;
14435           break;
14436
14437         default:
14438           gcc_unreachable ();
14439         }
14440
14441       def_mbuiltin (d->mask, d->name, type, d->code);
14442     }
14443
14444   /* Add the remaining MMX insns with somewhat more complicated types.  */
14445   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
14446   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
14447   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
14448
14449   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
14450   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
14451   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
14452   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
14453   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
14454   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
14455
14456   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
14457   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
14458   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
14459   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
14460   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
14461   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
14462
14463   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
14464   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
14465   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
14466   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
14467   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
14468   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
14469
14470   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
14471   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
14472   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
14473   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
14474   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
14475   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
14476
14477   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
14478
14479   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
14480   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
14481   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
14482   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
14483
14484   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
14485   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
14486   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
14487   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
14488   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
14489   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
14490   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
14491   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
14492   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
14493
14494   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
14495   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
14496   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
14497
14498   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
14499   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
14500   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
14501
14502   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
14503   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
14504   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
14505   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
14506   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
14507   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
14508
14509   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
14510   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
14511   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
14512   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
14513   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
14514   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
14515   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
14516   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
14517   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
14518   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
14519   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
14520   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
14521
14522   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
14523   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
14524   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
14525   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
14526
14527   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
14528   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
14529   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
14530   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
14531   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
14532   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
14533   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
14534 }
14535
14536 static void
14537 arm_init_tls_builtins (void)
14538 {
14539   tree ftype;
14540   tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
14541   tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
14542
14543   ftype = build_function_type (ptr_type_node, void_list_node);
14544   add_builtin_function ("__builtin_thread_pointer", ftype,
14545                         ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
14546                         NULL, const_nothrow);
14547 }
14548
14549 typedef enum {
14550   T_V8QI  = 0x0001,
14551   T_V4HI  = 0x0002,
14552   T_V2SI  = 0x0004,
14553   T_V2SF  = 0x0008,
14554   T_DI    = 0x0010,
14555   T_V16QI = 0x0020,
14556   T_V8HI  = 0x0040,
14557   T_V4SI  = 0x0080,
14558   T_V4SF  = 0x0100,
14559   T_V2DI  = 0x0200,
14560   T_TI    = 0x0400,
14561   T_EI    = 0x0800,
14562   T_OI    = 0x1000
14563 } neon_builtin_type_bits;
14564
14565 #define v8qi_UP  T_V8QI
14566 #define v4hi_UP  T_V4HI
14567 #define v2si_UP  T_V2SI
14568 #define v2sf_UP  T_V2SF
14569 #define di_UP    T_DI
14570 #define v16qi_UP T_V16QI
14571 #define v8hi_UP  T_V8HI
14572 #define v4si_UP  T_V4SI
14573 #define v4sf_UP  T_V4SF
14574 #define v2di_UP  T_V2DI
14575 #define ti_UP    T_TI
14576 #define ei_UP    T_EI
14577 #define oi_UP    T_OI
14578
14579 #define UP(X) X##_UP
14580
14581 #define T_MAX 13
14582
14583 typedef enum {
14584   NEON_BINOP,
14585   NEON_TERNOP,
14586   NEON_UNOP,
14587   NEON_GETLANE,
14588   NEON_SETLANE,
14589   NEON_CREATE,
14590   NEON_DUP,
14591   NEON_DUPLANE,
14592   NEON_COMBINE,
14593   NEON_SPLIT,
14594   NEON_LANEMUL,
14595   NEON_LANEMULL,
14596   NEON_LANEMULH,
14597   NEON_LANEMAC,
14598   NEON_SCALARMUL,
14599   NEON_SCALARMULL,
14600   NEON_SCALARMULH,
14601   NEON_SCALARMAC,
14602   NEON_CONVERT,
14603   NEON_FIXCONV,
14604   NEON_SELECT,
14605   NEON_RESULTPAIR,
14606   NEON_REINTERP,
14607   NEON_VTBL,
14608   NEON_VTBX,
14609   NEON_LOAD1,
14610   NEON_LOAD1LANE,
14611   NEON_STORE1,
14612   NEON_STORE1LANE,
14613   NEON_LOADSTRUCT,
14614   NEON_LOADSTRUCTLANE,
14615   NEON_STORESTRUCT,
14616   NEON_STORESTRUCTLANE,
14617   NEON_LOGICBINOP,
14618   NEON_SHIFTINSERT,
14619   NEON_SHIFTIMM,
14620   NEON_SHIFTACC
14621 } neon_itype;
14622
14623 typedef struct {
14624   const char *name;
14625   const neon_itype itype;
14626   const neon_builtin_type_bits bits;
14627   const enum insn_code codes[T_MAX];
14628   const unsigned int num_vars;
14629   unsigned int base_fcode;
14630 } neon_builtin_datum;
14631
14632 #define CF(N,X) CODE_FOR_neon_##N##X
14633
14634 #define VAR1(T, N, A) \
14635   #N, NEON_##T, UP (A), { CF (N, A) }, 1, 0
14636 #define VAR2(T, N, A, B) \
14637   #N, NEON_##T, UP (A) | UP (B), { CF (N, A), CF (N, B) }, 2, 0
14638 #define VAR3(T, N, A, B, C) \
14639   #N, NEON_##T, UP (A) | UP (B) | UP (C), \
14640   { CF (N, A), CF (N, B), CF (N, C) }, 3, 0
14641 #define VAR4(T, N, A, B, C, D) \
14642   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D), \
14643   { CF (N, A), CF (N, B), CF (N, C), CF (N, D) }, 4, 0
14644 #define VAR5(T, N, A, B, C, D, E) \
14645   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E), \
14646   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E) }, 5, 0
14647 #define VAR6(T, N, A, B, C, D, E, F) \
14648   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F), \
14649   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F) }, 6, 0
14650 #define VAR7(T, N, A, B, C, D, E, F, G) \
14651   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G), \
14652   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14653     CF (N, G) }, 7, 0
14654 #define VAR8(T, N, A, B, C, D, E, F, G, H) \
14655   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14656                 | UP (H), \
14657   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14658     CF (N, G), CF (N, H) }, 8, 0
14659 #define VAR9(T, N, A, B, C, D, E, F, G, H, I) \
14660   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14661                 | UP (H) | UP (I), \
14662   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14663     CF (N, G), CF (N, H), CF (N, I) }, 9, 0
14664 #define VAR10(T, N, A, B, C, D, E, F, G, H, I, J) \
14665   #N, NEON_##T, UP (A) | UP (B) | UP (C) | UP (D) | UP (E) | UP (F) | UP (G) \
14666                 | UP (H) | UP (I) | UP (J), \
14667   { CF (N, A), CF (N, B), CF (N, C), CF (N, D), CF (N, E), CF (N, F), \
14668     CF (N, G), CF (N, H), CF (N, I), CF (N, J) }, 10, 0
14669
14670 /* The mode entries in the following table correspond to the "key" type of the
14671    instruction variant, i.e. equivalent to that which would be specified after
14672    the assembler mnemonic, which usually refers to the last vector operand.
14673    (Signed/unsigned/polynomial types are not differentiated between though, and
14674    are all mapped onto the same mode for a given element size.) The modes
14675    listed per instruction should be the same as those defined for that
14676    instruction's pattern in neon.md.
14677    WARNING: Variants should be listed in the same increasing order as
14678    neon_builtin_type_bits.  */
14679
14680 static neon_builtin_datum neon_builtin_data[] =
14681 {
14682   { VAR10 (BINOP, vadd,
14683            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14684   { VAR3 (BINOP, vaddl, v8qi, v4hi, v2si) },
14685   { VAR3 (BINOP, vaddw, v8qi, v4hi, v2si) },
14686   { VAR6 (BINOP, vhadd, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14687   { VAR8 (BINOP, vqadd, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14688   { VAR3 (BINOP, vaddhn, v8hi, v4si, v2di) },
14689   { VAR8 (BINOP, vmul, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14690   { VAR8 (TERNOP, vmla, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14691   { VAR3 (TERNOP, vmlal, v8qi, v4hi, v2si) },
14692   { VAR8 (TERNOP, vmls, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14693   { VAR3 (TERNOP, vmlsl, v8qi, v4hi, v2si) },
14694   { VAR4 (BINOP, vqdmulh, v4hi, v2si, v8hi, v4si) },
14695   { VAR2 (TERNOP, vqdmlal, v4hi, v2si) },
14696   { VAR2 (TERNOP, vqdmlsl, v4hi, v2si) },
14697   { VAR3 (BINOP, vmull, v8qi, v4hi, v2si) },
14698   { VAR2 (SCALARMULL, vmull_n, v4hi, v2si) },
14699   { VAR2 (LANEMULL, vmull_lane, v4hi, v2si) },
14700   { VAR2 (SCALARMULL, vqdmull_n, v4hi, v2si) },
14701   { VAR2 (LANEMULL, vqdmull_lane, v4hi, v2si) },
14702   { VAR4 (SCALARMULH, vqdmulh_n, v4hi, v2si, v8hi, v4si) },
14703   { VAR4 (LANEMULH, vqdmulh_lane, v4hi, v2si, v8hi, v4si) },
14704   { VAR2 (BINOP, vqdmull, v4hi, v2si) },
14705   { VAR8 (BINOP, vshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14706   { VAR8 (BINOP, vqshl, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14707   { VAR8 (SHIFTIMM, vshr_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14708   { VAR3 (SHIFTIMM, vshrn_n, v8hi, v4si, v2di) },
14709   { VAR3 (SHIFTIMM, vqshrn_n, v8hi, v4si, v2di) },
14710   { VAR3 (SHIFTIMM, vqshrun_n, v8hi, v4si, v2di) },
14711   { VAR8 (SHIFTIMM, vshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14712   { VAR8 (SHIFTIMM, vqshl_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14713   { VAR8 (SHIFTIMM, vqshlu_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14714   { VAR3 (SHIFTIMM, vshll_n, v8qi, v4hi, v2si) },
14715   { VAR8 (SHIFTACC, vsra_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14716   { VAR10 (BINOP, vsub,
14717            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14718   { VAR3 (BINOP, vsubl, v8qi, v4hi, v2si) },
14719   { VAR3 (BINOP, vsubw, v8qi, v4hi, v2si) },
14720   { VAR8 (BINOP, vqsub, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14721   { VAR6 (BINOP, vhsub, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14722   { VAR3 (BINOP, vsubhn, v8hi, v4si, v2di) },
14723   { VAR8 (BINOP, vceq, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14724   { VAR8 (BINOP, vcge, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14725   { VAR8 (BINOP, vcgt, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14726   { VAR2 (BINOP, vcage, v2sf, v4sf) },
14727   { VAR2 (BINOP, vcagt, v2sf, v4sf) },
14728   { VAR6 (BINOP, vtst, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14729   { VAR8 (BINOP, vabd, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14730   { VAR3 (BINOP, vabdl, v8qi, v4hi, v2si) },
14731   { VAR6 (TERNOP, vaba, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14732   { VAR3 (TERNOP, vabal, v8qi, v4hi, v2si) },
14733   { VAR8 (BINOP, vmax, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14734   { VAR8 (BINOP, vmin, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14735   { VAR4 (BINOP, vpadd, v8qi, v4hi, v2si, v2sf) },
14736   { VAR6 (UNOP, vpaddl, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14737   { VAR6 (BINOP, vpadal, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14738   { VAR4 (BINOP, vpmax, v8qi, v4hi, v2si, v2sf) },
14739   { VAR4 (BINOP, vpmin, v8qi, v4hi, v2si, v2sf) },
14740   { VAR2 (BINOP, vrecps, v2sf, v4sf) },
14741   { VAR2 (BINOP, vrsqrts, v2sf, v4sf) },
14742   { VAR8 (SHIFTINSERT, vsri_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14743   { VAR8 (SHIFTINSERT, vsli_n, v8qi, v4hi, v2si, di, v16qi, v8hi, v4si, v2di) },
14744   { VAR8 (UNOP, vabs, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14745   { VAR6 (UNOP, vqabs, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14746   { VAR8 (UNOP, vneg, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14747   { VAR6 (UNOP, vqneg, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14748   { VAR6 (UNOP, vcls, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14749   { VAR6 (UNOP, vclz, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14750   { VAR2 (UNOP, vcnt, v8qi, v16qi) },
14751   { VAR4 (UNOP, vrecpe, v2si, v2sf, v4si, v4sf) },
14752   { VAR4 (UNOP, vrsqrte, v2si, v2sf, v4si, v4sf) },
14753   { VAR6 (UNOP, vmvn, v8qi, v4hi, v2si, v16qi, v8hi, v4si) },
14754   /* FIXME: vget_lane supports more variants than this!  */
14755   { VAR10 (GETLANE, vget_lane,
14756            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14757   { VAR10 (SETLANE, vset_lane,
14758            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14759   { VAR5 (CREATE, vcreate, v8qi, v4hi, v2si, v2sf, di) },
14760   { VAR10 (DUP, vdup_n,
14761            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14762   { VAR10 (DUPLANE, vdup_lane,
14763            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14764   { VAR5 (COMBINE, vcombine, v8qi, v4hi, v2si, v2sf, di) },
14765   { VAR5 (SPLIT, vget_high, v16qi, v8hi, v4si, v4sf, v2di) },
14766   { VAR5 (SPLIT, vget_low, v16qi, v8hi, v4si, v4sf, v2di) },
14767   { VAR3 (UNOP, vmovn, v8hi, v4si, v2di) },
14768   { VAR3 (UNOP, vqmovn, v8hi, v4si, v2di) },
14769   { VAR3 (UNOP, vqmovun, v8hi, v4si, v2di) },
14770   { VAR3 (UNOP, vmovl, v8qi, v4hi, v2si) },
14771   { VAR6 (LANEMUL, vmul_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14772   { VAR6 (LANEMAC, vmla_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14773   { VAR2 (LANEMAC, vmlal_lane, v4hi, v2si) },
14774   { VAR2 (LANEMAC, vqdmlal_lane, v4hi, v2si) },
14775   { VAR6 (LANEMAC, vmls_lane, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14776   { VAR2 (LANEMAC, vmlsl_lane, v4hi, v2si) },
14777   { VAR2 (LANEMAC, vqdmlsl_lane, v4hi, v2si) },
14778   { VAR6 (SCALARMUL, vmul_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14779   { VAR6 (SCALARMAC, vmla_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14780   { VAR2 (SCALARMAC, vmlal_n, v4hi, v2si) },
14781   { VAR2 (SCALARMAC, vqdmlal_n, v4hi, v2si) },
14782   { VAR6 (SCALARMAC, vmls_n, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14783   { VAR2 (SCALARMAC, vmlsl_n, v4hi, v2si) },
14784   { VAR2 (SCALARMAC, vqdmlsl_n, v4hi, v2si) },
14785   { VAR10 (BINOP, vext,
14786            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14787   { VAR8 (UNOP, vrev64, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14788   { VAR4 (UNOP, vrev32, v8qi, v4hi, v16qi, v8hi) },
14789   { VAR2 (UNOP, vrev16, v8qi, v16qi) },
14790   { VAR4 (CONVERT, vcvt, v2si, v2sf, v4si, v4sf) },
14791   { VAR4 (FIXCONV, vcvt_n, v2si, v2sf, v4si, v4sf) },
14792   { VAR10 (SELECT, vbsl,
14793            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14794   { VAR1 (VTBL, vtbl1, v8qi) },
14795   { VAR1 (VTBL, vtbl2, v8qi) },
14796   { VAR1 (VTBL, vtbl3, v8qi) },
14797   { VAR1 (VTBL, vtbl4, v8qi) },
14798   { VAR1 (VTBX, vtbx1, v8qi) },
14799   { VAR1 (VTBX, vtbx2, v8qi) },
14800   { VAR1 (VTBX, vtbx3, v8qi) },
14801   { VAR1 (VTBX, vtbx4, v8qi) },
14802   { VAR8 (RESULTPAIR, vtrn, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14803   { VAR8 (RESULTPAIR, vzip, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14804   { VAR8 (RESULTPAIR, vuzp, v8qi, v4hi, v2si, v2sf, v16qi, v8hi, v4si, v4sf) },
14805   { VAR5 (REINTERP, vreinterpretv8qi, v8qi, v4hi, v2si, v2sf, di) },
14806   { VAR5 (REINTERP, vreinterpretv4hi, v8qi, v4hi, v2si, v2sf, di) },
14807   { VAR5 (REINTERP, vreinterpretv2si, v8qi, v4hi, v2si, v2sf, di) },
14808   { VAR5 (REINTERP, vreinterpretv2sf, v8qi, v4hi, v2si, v2sf, di) },
14809   { VAR5 (REINTERP, vreinterpretdi, v8qi, v4hi, v2si, v2sf, di) },
14810   { VAR5 (REINTERP, vreinterpretv16qi, v16qi, v8hi, v4si, v4sf, v2di) },
14811   { VAR5 (REINTERP, vreinterpretv8hi, v16qi, v8hi, v4si, v4sf, v2di) },
14812   { VAR5 (REINTERP, vreinterpretv4si, v16qi, v8hi, v4si, v4sf, v2di) },
14813   { VAR5 (REINTERP, vreinterpretv4sf, v16qi, v8hi, v4si, v4sf, v2di) },
14814   { VAR5 (REINTERP, vreinterpretv2di, v16qi, v8hi, v4si, v4sf, v2di) },
14815   { VAR10 (LOAD1, vld1,
14816            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14817   { VAR10 (LOAD1LANE, vld1_lane,
14818            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14819   { VAR10 (LOAD1, vld1_dup,
14820            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14821   { VAR10 (STORE1, vst1,
14822            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14823   { VAR10 (STORE1LANE, vst1_lane,
14824            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14825   { VAR9 (LOADSTRUCT,
14826           vld2, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
14827   { VAR7 (LOADSTRUCTLANE, vld2_lane,
14828           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14829   { VAR5 (LOADSTRUCT, vld2_dup, v8qi, v4hi, v2si, v2sf, di) },
14830   { VAR9 (STORESTRUCT, vst2,
14831           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
14832   { VAR7 (STORESTRUCTLANE, vst2_lane,
14833           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14834   { VAR9 (LOADSTRUCT,
14835           vld3, v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
14836   { VAR7 (LOADSTRUCTLANE, vld3_lane,
14837           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14838   { VAR5 (LOADSTRUCT, vld3_dup, v8qi, v4hi, v2si, v2sf, di) },
14839   { VAR9 (STORESTRUCT, vst3,
14840           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
14841   { VAR7 (STORESTRUCTLANE, vst3_lane,
14842           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14843   { VAR9 (LOADSTRUCT, vld4,
14844           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
14845   { VAR7 (LOADSTRUCTLANE, vld4_lane,
14846           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14847   { VAR5 (LOADSTRUCT, vld4_dup, v8qi, v4hi, v2si, v2sf, di) },
14848   { VAR9 (STORESTRUCT, vst4,
14849           v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf) },
14850   { VAR7 (STORESTRUCTLANE, vst4_lane,
14851           v8qi, v4hi, v2si, v2sf, v8hi, v4si, v4sf) },
14852   { VAR10 (LOGICBINOP, vand,
14853            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14854   { VAR10 (LOGICBINOP, vorr,
14855            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14856   { VAR10 (BINOP, veor,
14857            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14858   { VAR10 (LOGICBINOP, vbic,
14859            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) },
14860   { VAR10 (LOGICBINOP, vorn,
14861            v8qi, v4hi, v2si, v2sf, di, v16qi, v8hi, v4si, v4sf, v2di) }
14862 };
14863
14864 #undef CF
14865 #undef VAR1
14866 #undef VAR2
14867 #undef VAR3
14868 #undef VAR4
14869 #undef VAR5
14870 #undef VAR6
14871 #undef VAR7
14872 #undef VAR8
14873 #undef VAR9
14874 #undef VAR10
14875
14876 static void
14877 arm_init_neon_builtins (void)
14878 {
14879   unsigned int i, fcode = ARM_BUILTIN_NEON_BASE;
14880
14881   /* Create distinguished type nodes for NEON vector element types,
14882      and pointers to values of such types, so we can detect them later.  */
14883   tree neon_intQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
14884   tree neon_intHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
14885   tree neon_polyQI_type_node = make_signed_type (GET_MODE_PRECISION (QImode));
14886   tree neon_polyHI_type_node = make_signed_type (GET_MODE_PRECISION (HImode));
14887   tree neon_intSI_type_node = make_signed_type (GET_MODE_PRECISION (SImode));
14888   tree neon_intDI_type_node = make_signed_type (GET_MODE_PRECISION (DImode));
14889   tree neon_float_type_node = make_node (REAL_TYPE);
14890   TYPE_PRECISION (neon_float_type_node) = FLOAT_TYPE_SIZE;
14891   layout_type (neon_float_type_node);
14892
14893   /* Define typedefs which exactly correspond to the modes we are basing vector
14894      types on.  If you change these names you'll need to change
14895      the table used by arm_mangle_type too.  */
14896   (*lang_hooks.types.register_builtin_type) (neon_intQI_type_node,
14897                                              "__builtin_neon_qi");
14898   (*lang_hooks.types.register_builtin_type) (neon_intHI_type_node,
14899                                              "__builtin_neon_hi");
14900   (*lang_hooks.types.register_builtin_type) (neon_intSI_type_node,
14901                                              "__builtin_neon_si");
14902   (*lang_hooks.types.register_builtin_type) (neon_float_type_node,
14903                                              "__builtin_neon_sf");
14904   (*lang_hooks.types.register_builtin_type) (neon_intDI_type_node,
14905                                              "__builtin_neon_di");
14906
14907   (*lang_hooks.types.register_builtin_type) (neon_polyQI_type_node,
14908                                              "__builtin_neon_poly8");
14909   (*lang_hooks.types.register_builtin_type) (neon_polyHI_type_node,
14910                                              "__builtin_neon_poly16");
14911
14912   tree intQI_pointer_node = build_pointer_type (neon_intQI_type_node);
14913   tree intHI_pointer_node = build_pointer_type (neon_intHI_type_node);
14914   tree intSI_pointer_node = build_pointer_type (neon_intSI_type_node);
14915   tree intDI_pointer_node = build_pointer_type (neon_intDI_type_node);
14916   tree float_pointer_node = build_pointer_type (neon_float_type_node);
14917
14918   /* Next create constant-qualified versions of the above types.  */
14919   tree const_intQI_node = build_qualified_type (neon_intQI_type_node,
14920                                                 TYPE_QUAL_CONST);
14921   tree const_intHI_node = build_qualified_type (neon_intHI_type_node,
14922                                                 TYPE_QUAL_CONST);
14923   tree const_intSI_node = build_qualified_type (neon_intSI_type_node,
14924                                                 TYPE_QUAL_CONST);
14925   tree const_intDI_node = build_qualified_type (neon_intDI_type_node,
14926                                                 TYPE_QUAL_CONST);
14927   tree const_float_node = build_qualified_type (neon_float_type_node,
14928                                                 TYPE_QUAL_CONST);
14929
14930   tree const_intQI_pointer_node = build_pointer_type (const_intQI_node);
14931   tree const_intHI_pointer_node = build_pointer_type (const_intHI_node);
14932   tree const_intSI_pointer_node = build_pointer_type (const_intSI_node);
14933   tree const_intDI_pointer_node = build_pointer_type (const_intDI_node);
14934   tree const_float_pointer_node = build_pointer_type (const_float_node);
14935
14936   /* Now create vector types based on our NEON element types.  */
14937   /* 64-bit vectors.  */
14938   tree V8QI_type_node =
14939     build_vector_type_for_mode (neon_intQI_type_node, V8QImode);
14940   tree V4HI_type_node =
14941     build_vector_type_for_mode (neon_intHI_type_node, V4HImode);
14942   tree V2SI_type_node =
14943     build_vector_type_for_mode (neon_intSI_type_node, V2SImode);
14944   tree V2SF_type_node =
14945     build_vector_type_for_mode (neon_float_type_node, V2SFmode);
14946   /* 128-bit vectors.  */
14947   tree V16QI_type_node =
14948     build_vector_type_for_mode (neon_intQI_type_node, V16QImode);
14949   tree V8HI_type_node =
14950     build_vector_type_for_mode (neon_intHI_type_node, V8HImode);
14951   tree V4SI_type_node =
14952     build_vector_type_for_mode (neon_intSI_type_node, V4SImode);
14953   tree V4SF_type_node =
14954     build_vector_type_for_mode (neon_float_type_node, V4SFmode);
14955   tree V2DI_type_node =
14956     build_vector_type_for_mode (neon_intDI_type_node, V2DImode);
14957
14958   /* Unsigned integer types for various mode sizes.  */
14959   tree intUQI_type_node = make_unsigned_type (GET_MODE_PRECISION (QImode));
14960   tree intUHI_type_node = make_unsigned_type (GET_MODE_PRECISION (HImode));
14961   tree intUSI_type_node = make_unsigned_type (GET_MODE_PRECISION (SImode));
14962   tree intUDI_type_node = make_unsigned_type (GET_MODE_PRECISION (DImode));
14963
14964   (*lang_hooks.types.register_builtin_type) (intUQI_type_node,
14965                                              "__builtin_neon_uqi");
14966   (*lang_hooks.types.register_builtin_type) (intUHI_type_node,
14967                                              "__builtin_neon_uhi");
14968   (*lang_hooks.types.register_builtin_type) (intUSI_type_node,
14969                                              "__builtin_neon_usi");
14970   (*lang_hooks.types.register_builtin_type) (intUDI_type_node,
14971                                              "__builtin_neon_udi");
14972
14973   /* Opaque integer types for structures of vectors.  */
14974   tree intEI_type_node = make_signed_type (GET_MODE_PRECISION (EImode));
14975   tree intOI_type_node = make_signed_type (GET_MODE_PRECISION (OImode));
14976   tree intCI_type_node = make_signed_type (GET_MODE_PRECISION (CImode));
14977   tree intXI_type_node = make_signed_type (GET_MODE_PRECISION (XImode));
14978
14979   (*lang_hooks.types.register_builtin_type) (intTI_type_node,
14980                                              "__builtin_neon_ti");
14981   (*lang_hooks.types.register_builtin_type) (intEI_type_node,
14982                                              "__builtin_neon_ei");
14983   (*lang_hooks.types.register_builtin_type) (intOI_type_node,
14984                                              "__builtin_neon_oi");
14985   (*lang_hooks.types.register_builtin_type) (intCI_type_node,
14986                                              "__builtin_neon_ci");
14987   (*lang_hooks.types.register_builtin_type) (intXI_type_node,
14988                                              "__builtin_neon_xi");
14989
14990   /* Pointers to vector types.  */
14991   tree V8QI_pointer_node = build_pointer_type (V8QI_type_node);
14992   tree V4HI_pointer_node = build_pointer_type (V4HI_type_node);
14993   tree V2SI_pointer_node = build_pointer_type (V2SI_type_node);
14994   tree V2SF_pointer_node = build_pointer_type (V2SF_type_node);
14995   tree V16QI_pointer_node = build_pointer_type (V16QI_type_node);
14996   tree V8HI_pointer_node = build_pointer_type (V8HI_type_node);
14997   tree V4SI_pointer_node = build_pointer_type (V4SI_type_node);
14998   tree V4SF_pointer_node = build_pointer_type (V4SF_type_node);
14999   tree V2DI_pointer_node = build_pointer_type (V2DI_type_node);
15000
15001   /* Operations which return results as pairs.  */
15002   tree void_ftype_pv8qi_v8qi_v8qi =
15003     build_function_type_list (void_type_node, V8QI_pointer_node, V8QI_type_node,
15004                               V8QI_type_node, NULL);
15005   tree void_ftype_pv4hi_v4hi_v4hi =
15006     build_function_type_list (void_type_node, V4HI_pointer_node, V4HI_type_node,
15007                               V4HI_type_node, NULL);
15008   tree void_ftype_pv2si_v2si_v2si =
15009     build_function_type_list (void_type_node, V2SI_pointer_node, V2SI_type_node,
15010                               V2SI_type_node, NULL);
15011   tree void_ftype_pv2sf_v2sf_v2sf =
15012     build_function_type_list (void_type_node, V2SF_pointer_node, V2SF_type_node,
15013                               V2SF_type_node, NULL);
15014   tree void_ftype_pdi_di_di =
15015     build_function_type_list (void_type_node, intDI_pointer_node,
15016                               neon_intDI_type_node, neon_intDI_type_node, NULL);
15017   tree void_ftype_pv16qi_v16qi_v16qi =
15018     build_function_type_list (void_type_node, V16QI_pointer_node,
15019                               V16QI_type_node, V16QI_type_node, NULL);
15020   tree void_ftype_pv8hi_v8hi_v8hi =
15021     build_function_type_list (void_type_node, V8HI_pointer_node, V8HI_type_node,
15022                               V8HI_type_node, NULL);
15023   tree void_ftype_pv4si_v4si_v4si =
15024     build_function_type_list (void_type_node, V4SI_pointer_node, V4SI_type_node,
15025                               V4SI_type_node, NULL);
15026   tree void_ftype_pv4sf_v4sf_v4sf =
15027     build_function_type_list (void_type_node, V4SF_pointer_node, V4SF_type_node,
15028                               V4SF_type_node, NULL);
15029   tree void_ftype_pv2di_v2di_v2di =
15030     build_function_type_list (void_type_node, V2DI_pointer_node, V2DI_type_node,
15031                               V2DI_type_node, NULL);
15032
15033   tree reinterp_ftype_dreg[5][5];
15034   tree reinterp_ftype_qreg[5][5];
15035   tree dreg_types[5], qreg_types[5];
15036
15037   dreg_types[0] = V8QI_type_node;
15038   dreg_types[1] = V4HI_type_node;
15039   dreg_types[2] = V2SI_type_node;
15040   dreg_types[3] = V2SF_type_node;
15041   dreg_types[4] = neon_intDI_type_node;
15042
15043   qreg_types[0] = V16QI_type_node;
15044   qreg_types[1] = V8HI_type_node;
15045   qreg_types[2] = V4SI_type_node;
15046   qreg_types[3] = V4SF_type_node;
15047   qreg_types[4] = V2DI_type_node;
15048
15049   for (i = 0; i < 5; i++)
15050     {
15051       int j;
15052       for (j = 0; j < 5; j++)
15053         {
15054           reinterp_ftype_dreg[i][j]
15055             = build_function_type_list (dreg_types[i], dreg_types[j], NULL);
15056           reinterp_ftype_qreg[i][j]
15057             = build_function_type_list (qreg_types[i], qreg_types[j], NULL);
15058         }
15059     }
15060
15061   for (i = 0; i < ARRAY_SIZE (neon_builtin_data); i++)
15062     {
15063       neon_builtin_datum *d = &neon_builtin_data[i];
15064       unsigned int j, codeidx = 0;
15065
15066       d->base_fcode = fcode;
15067
15068       for (j = 0; j < T_MAX; j++)
15069         {
15070           const char* const modenames[] = {
15071             "v8qi", "v4hi", "v2si", "v2sf", "di",
15072             "v16qi", "v8hi", "v4si", "v4sf", "v2di"
15073           };
15074           char namebuf[60];
15075           tree ftype = NULL;
15076           enum insn_code icode;
15077           int is_load = 0, is_store = 0;
15078
15079           if ((d->bits & (1 << j)) == 0)
15080             continue;
15081
15082           icode = d->codes[codeidx++];
15083
15084           switch (d->itype)
15085             {
15086             case NEON_LOAD1:
15087             case NEON_LOAD1LANE:
15088             case NEON_LOADSTRUCT:
15089             case NEON_LOADSTRUCTLANE:
15090               is_load = 1;
15091               /* Fall through.  */
15092             case NEON_STORE1:
15093             case NEON_STORE1LANE:
15094             case NEON_STORESTRUCT:
15095             case NEON_STORESTRUCTLANE:
15096               if (!is_load)
15097                 is_store = 1;
15098               /* Fall through.  */
15099             case NEON_UNOP:
15100             case NEON_BINOP:
15101             case NEON_LOGICBINOP:
15102             case NEON_SHIFTINSERT:
15103             case NEON_TERNOP:
15104             case NEON_GETLANE:
15105             case NEON_SETLANE:
15106             case NEON_CREATE:
15107             case NEON_DUP:
15108             case NEON_DUPLANE:
15109             case NEON_SHIFTIMM:
15110             case NEON_SHIFTACC:
15111             case NEON_COMBINE:
15112             case NEON_SPLIT:
15113             case NEON_CONVERT:
15114             case NEON_FIXCONV:
15115             case NEON_LANEMUL:
15116             case NEON_LANEMULL:
15117             case NEON_LANEMULH:
15118             case NEON_LANEMAC:
15119             case NEON_SCALARMUL:
15120             case NEON_SCALARMULL:
15121             case NEON_SCALARMULH:
15122             case NEON_SCALARMAC:
15123             case NEON_SELECT:
15124             case NEON_VTBL:
15125             case NEON_VTBX:
15126               {
15127                 int k;
15128                 tree return_type = void_type_node, args = void_list_node;
15129
15130                 /* Build a function type directly from the insn_data for this
15131                    builtin.  The build_function_type() function takes care of
15132                    removing duplicates for us.  */
15133                 for (k = insn_data[icode].n_operands - 1; k >= 0; k--)
15134                   {
15135                     tree eltype;
15136
15137                     if (is_load && k == 1)
15138                       {
15139                         /* Neon load patterns always have the memory operand
15140                            (a SImode pointer) in the operand 1 position.  We
15141                            want a const pointer to the element type in that
15142                            position.  */
15143                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
15144
15145                         switch (1 << j)
15146                           {
15147                           case T_V8QI:
15148                           case T_V16QI:
15149                             eltype = const_intQI_pointer_node;
15150                             break;
15151
15152                           case T_V4HI:
15153                           case T_V8HI:
15154                             eltype = const_intHI_pointer_node;
15155                             break;
15156
15157                           case T_V2SI:
15158                           case T_V4SI:
15159                             eltype = const_intSI_pointer_node;
15160                             break;
15161
15162                           case T_V2SF:
15163                           case T_V4SF:
15164                             eltype = const_float_pointer_node;
15165                             break;
15166
15167                           case T_DI:
15168                           case T_V2DI:
15169                             eltype = const_intDI_pointer_node;
15170                             break;
15171
15172                           default: gcc_unreachable ();
15173                           }
15174                       }
15175                     else if (is_store && k == 0)
15176                       {
15177                         /* Similarly, Neon store patterns use operand 0 as
15178                            the memory location to store to (a SImode pointer).
15179                            Use a pointer to the element type of the store in
15180                            that position.  */
15181                         gcc_assert (insn_data[icode].operand[k].mode == SImode);
15182
15183                         switch (1 << j)
15184                           {
15185                           case T_V8QI:
15186                           case T_V16QI:
15187                             eltype = intQI_pointer_node;
15188                             break;
15189
15190                           case T_V4HI:
15191                           case T_V8HI:
15192                             eltype = intHI_pointer_node;
15193                             break;
15194
15195                           case T_V2SI:
15196                           case T_V4SI:
15197                             eltype = intSI_pointer_node;
15198                             break;
15199
15200                           case T_V2SF:
15201                           case T_V4SF:
15202                             eltype = float_pointer_node;
15203                             break;
15204
15205                           case T_DI:
15206                           case T_V2DI:
15207                             eltype = intDI_pointer_node;
15208                             break;
15209
15210                           default: gcc_unreachable ();
15211                           }
15212                       }
15213                     else
15214                       {
15215                         switch (insn_data[icode].operand[k].mode)
15216                           {
15217                           case VOIDmode: eltype = void_type_node; break;
15218                           /* Scalars.  */
15219                           case QImode: eltype = neon_intQI_type_node; break;
15220                           case HImode: eltype = neon_intHI_type_node; break;
15221                           case SImode: eltype = neon_intSI_type_node; break;
15222                           case SFmode: eltype = neon_float_type_node; break;
15223                           case DImode: eltype = neon_intDI_type_node; break;
15224                           case TImode: eltype = intTI_type_node; break;
15225                           case EImode: eltype = intEI_type_node; break;
15226                           case OImode: eltype = intOI_type_node; break;
15227                           case CImode: eltype = intCI_type_node; break;
15228                           case XImode: eltype = intXI_type_node; break;
15229                           /* 64-bit vectors.  */
15230                           case V8QImode: eltype = V8QI_type_node; break;
15231                           case V4HImode: eltype = V4HI_type_node; break;
15232                           case V2SImode: eltype = V2SI_type_node; break;
15233                           case V2SFmode: eltype = V2SF_type_node; break;
15234                           /* 128-bit vectors.  */
15235                           case V16QImode: eltype = V16QI_type_node; break;
15236                           case V8HImode: eltype = V8HI_type_node; break;
15237                           case V4SImode: eltype = V4SI_type_node; break;
15238                           case V4SFmode: eltype = V4SF_type_node; break;
15239                           case V2DImode: eltype = V2DI_type_node; break;
15240                           default: gcc_unreachable ();
15241                           }
15242                       }
15243
15244                     if (k == 0 && !is_store)
15245                       return_type = eltype;
15246                     else
15247                       args = tree_cons (NULL_TREE, eltype, args);
15248                   }
15249
15250                 ftype = build_function_type (return_type, args);
15251               }
15252               break;
15253
15254             case NEON_RESULTPAIR:
15255               {
15256                 switch (insn_data[icode].operand[1].mode)
15257                   {
15258                   case V8QImode: ftype = void_ftype_pv8qi_v8qi_v8qi; break;
15259                   case V4HImode: ftype = void_ftype_pv4hi_v4hi_v4hi; break;
15260                   case V2SImode: ftype = void_ftype_pv2si_v2si_v2si; break;
15261                   case V2SFmode: ftype = void_ftype_pv2sf_v2sf_v2sf; break;
15262                   case DImode: ftype = void_ftype_pdi_di_di; break;
15263                   case V16QImode: ftype = void_ftype_pv16qi_v16qi_v16qi; break;
15264                   case V8HImode: ftype = void_ftype_pv8hi_v8hi_v8hi; break;
15265                   case V4SImode: ftype = void_ftype_pv4si_v4si_v4si; break;
15266                   case V4SFmode: ftype = void_ftype_pv4sf_v4sf_v4sf; break;
15267                   case V2DImode: ftype = void_ftype_pv2di_v2di_v2di; break;
15268                   default: gcc_unreachable ();
15269                   }
15270               }
15271               break;
15272
15273             case NEON_REINTERP:
15274               {
15275                 /* We iterate over 5 doubleword types, then 5 quadword
15276                    types.  */
15277                 int rhs = j % 5;
15278                 switch (insn_data[icode].operand[0].mode)
15279                   {
15280                   case V8QImode: ftype = reinterp_ftype_dreg[0][rhs]; break;
15281                   case V4HImode: ftype = reinterp_ftype_dreg[1][rhs]; break;
15282                   case V2SImode: ftype = reinterp_ftype_dreg[2][rhs]; break;
15283                   case V2SFmode: ftype = reinterp_ftype_dreg[3][rhs]; break;
15284                   case DImode: ftype = reinterp_ftype_dreg[4][rhs]; break;
15285                   case V16QImode: ftype = reinterp_ftype_qreg[0][rhs]; break;
15286                   case V8HImode: ftype = reinterp_ftype_qreg[1][rhs]; break;
15287                   case V4SImode: ftype = reinterp_ftype_qreg[2][rhs]; break;
15288                   case V4SFmode: ftype = reinterp_ftype_qreg[3][rhs]; break;
15289                   case V2DImode: ftype = reinterp_ftype_qreg[4][rhs]; break;
15290                   default: gcc_unreachable ();
15291                   }
15292               }
15293               break;
15294
15295             default:
15296               gcc_unreachable ();
15297             }
15298
15299           gcc_assert (ftype != NULL);
15300
15301           sprintf (namebuf, "__builtin_neon_%s%s", d->name, modenames[j]);
15302
15303           add_builtin_function (namebuf, ftype, fcode++, BUILT_IN_MD, NULL,
15304                                 NULL_TREE);
15305         }
15306     }
15307 }
15308
15309 static void
15310 arm_init_builtins (void)
15311 {
15312   arm_init_tls_builtins ();
15313
15314   if (TARGET_REALLY_IWMMXT)
15315     arm_init_iwmmxt_builtins ();
15316
15317   if (TARGET_NEON)
15318     arm_init_neon_builtins ();
15319 }
15320
15321 /* Errors in the source file can cause expand_expr to return const0_rtx
15322    where we expect a vector.  To avoid crashing, use one of the vector
15323    clear instructions.  */
15324
15325 static rtx
15326 safe_vector_operand (rtx x, enum machine_mode mode)
15327 {
15328   if (x != const0_rtx)
15329     return x;
15330   x = gen_reg_rtx (mode);
15331
15332   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
15333                                : gen_rtx_SUBREG (DImode, x, 0)));
15334   return x;
15335 }
15336
15337 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
15338
15339 static rtx
15340 arm_expand_binop_builtin (enum insn_code icode,
15341                           tree exp, rtx target)
15342 {
15343   rtx pat;
15344   tree arg0 = CALL_EXPR_ARG (exp, 0);
15345   tree arg1 = CALL_EXPR_ARG (exp, 1);
15346   rtx op0 = expand_normal (arg0);
15347   rtx op1 = expand_normal (arg1);
15348   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15349   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15350   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
15351
15352   if (VECTOR_MODE_P (mode0))
15353     op0 = safe_vector_operand (op0, mode0);
15354   if (VECTOR_MODE_P (mode1))
15355     op1 = safe_vector_operand (op1, mode1);
15356
15357   if (! target
15358       || GET_MODE (target) != tmode
15359       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15360     target = gen_reg_rtx (tmode);
15361
15362   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
15363
15364   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15365     op0 = copy_to_mode_reg (mode0, op0);
15366   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15367     op1 = copy_to_mode_reg (mode1, op1);
15368
15369   pat = GEN_FCN (icode) (target, op0, op1);
15370   if (! pat)
15371     return 0;
15372   emit_insn (pat);
15373   return target;
15374 }
15375
15376 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
15377
15378 static rtx
15379 arm_expand_unop_builtin (enum insn_code icode,
15380                          tree exp, rtx target, int do_load)
15381 {
15382   rtx pat;
15383   tree arg0 = CALL_EXPR_ARG (exp, 0);
15384   rtx op0 = expand_normal (arg0);
15385   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15386   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15387
15388   if (! target
15389       || GET_MODE (target) != tmode
15390       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15391     target = gen_reg_rtx (tmode);
15392   if (do_load)
15393     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15394   else
15395     {
15396       if (VECTOR_MODE_P (mode0))
15397         op0 = safe_vector_operand (op0, mode0);
15398
15399       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15400         op0 = copy_to_mode_reg (mode0, op0);
15401     }
15402
15403   pat = GEN_FCN (icode) (target, op0);
15404   if (! pat)
15405     return 0;
15406   emit_insn (pat);
15407   return target;
15408 }
15409
15410 static int
15411 neon_builtin_compare (const void *a, const void *b)
15412 {
15413   const neon_builtin_datum *key = a;
15414   const neon_builtin_datum *memb = b;
15415   unsigned int soughtcode = key->base_fcode;
15416
15417   if (soughtcode >= memb->base_fcode
15418       && soughtcode < memb->base_fcode + memb->num_vars)
15419     return 0;
15420   else if (soughtcode < memb->base_fcode)
15421     return -1;
15422   else
15423     return 1;
15424 }
15425
15426 static enum insn_code
15427 locate_neon_builtin_icode (int fcode, neon_itype *itype)
15428 {
15429   neon_builtin_datum key, *found;
15430   int idx;
15431
15432   key.base_fcode = fcode;
15433   found = bsearch (&key, &neon_builtin_data[0], ARRAY_SIZE (neon_builtin_data),
15434                    sizeof (neon_builtin_data[0]), neon_builtin_compare);
15435   gcc_assert (found);
15436   idx = fcode - (int) found->base_fcode;
15437   gcc_assert (idx >= 0 && idx < T_MAX && idx < (int)found->num_vars);
15438
15439   if (itype)
15440     *itype = found->itype;
15441
15442   return found->codes[idx];
15443 }
15444
15445 typedef enum {
15446   NEON_ARG_COPY_TO_REG,
15447   NEON_ARG_CONSTANT,
15448   NEON_ARG_STOP
15449 } builtin_arg;
15450
15451 #define NEON_MAX_BUILTIN_ARGS 5
15452
15453 /* Expand a Neon builtin.  */
15454 static rtx
15455 arm_expand_neon_args (rtx target, int icode, int have_retval,
15456                       tree exp, ...)
15457 {
15458   va_list ap;
15459   rtx pat;
15460   tree arg[NEON_MAX_BUILTIN_ARGS];
15461   rtx op[NEON_MAX_BUILTIN_ARGS];
15462   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15463   enum machine_mode mode[NEON_MAX_BUILTIN_ARGS];
15464   int argc = 0;
15465
15466   if (have_retval
15467       && (!target
15468           || GET_MODE (target) != tmode
15469           || !(*insn_data[icode].operand[0].predicate) (target, tmode)))
15470     target = gen_reg_rtx (tmode);
15471
15472   va_start (ap, exp);
15473
15474   for (;;)
15475     {
15476       builtin_arg thisarg = va_arg (ap, int);
15477
15478       if (thisarg == NEON_ARG_STOP)
15479         break;
15480       else
15481         {
15482           arg[argc] = CALL_EXPR_ARG (exp, argc);
15483           op[argc] = expand_normal (arg[argc]);
15484           mode[argc] = insn_data[icode].operand[argc + have_retval].mode;
15485
15486           switch (thisarg)
15487             {
15488             case NEON_ARG_COPY_TO_REG:
15489               /*gcc_assert (GET_MODE (op[argc]) == mode[argc]);*/
15490               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
15491                      (op[argc], mode[argc]))
15492                 op[argc] = copy_to_mode_reg (mode[argc], op[argc]);
15493               break;
15494
15495             case NEON_ARG_CONSTANT:
15496               /* FIXME: This error message is somewhat unhelpful.  */
15497               if (!(*insn_data[icode].operand[argc + have_retval].predicate)
15498                     (op[argc], mode[argc]))
15499                 error ("argument must be a constant");
15500               break;
15501
15502             case NEON_ARG_STOP:
15503               gcc_unreachable ();
15504             }
15505
15506           argc++;
15507         }
15508     }
15509
15510   va_end (ap);
15511
15512   if (have_retval)
15513     switch (argc)
15514       {
15515       case 1:
15516         pat = GEN_FCN (icode) (target, op[0]);
15517         break;
15518
15519       case 2:
15520         pat = GEN_FCN (icode) (target, op[0], op[1]);
15521         break;
15522
15523       case 3:
15524         pat = GEN_FCN (icode) (target, op[0], op[1], op[2]);
15525         break;
15526
15527       case 4:
15528         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3]);
15529         break;
15530
15531       case 5:
15532         pat = GEN_FCN (icode) (target, op[0], op[1], op[2], op[3], op[4]);
15533         break;
15534
15535       default:
15536         gcc_unreachable ();
15537       }
15538   else
15539     switch (argc)
15540       {
15541       case 1:
15542         pat = GEN_FCN (icode) (op[0]);
15543         break;
15544
15545       case 2:
15546         pat = GEN_FCN (icode) (op[0], op[1]);
15547         break;
15548
15549       case 3:
15550         pat = GEN_FCN (icode) (op[0], op[1], op[2]);
15551         break;
15552
15553       case 4:
15554         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3]);
15555         break;
15556
15557       case 5:
15558         pat = GEN_FCN (icode) (op[0], op[1], op[2], op[3], op[4]);
15559         break;
15560
15561       default:
15562         gcc_unreachable ();
15563       }
15564
15565   if (!pat)
15566     return 0;
15567
15568   emit_insn (pat);
15569
15570   return target;
15571 }
15572
15573 /* Expand a Neon builtin. These are "special" because they don't have symbolic
15574    constants defined per-instruction or per instruction-variant. Instead, the
15575    required info is looked up in the table neon_builtin_data.  */
15576 static rtx
15577 arm_expand_neon_builtin (int fcode, tree exp, rtx target)
15578 {
15579   neon_itype itype;
15580   enum insn_code icode = locate_neon_builtin_icode (fcode, &itype);
15581
15582   switch (itype)
15583     {
15584     case NEON_UNOP:
15585     case NEON_CONVERT:
15586     case NEON_DUPLANE:
15587       return arm_expand_neon_args (target, icode, 1, exp,
15588         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_STOP);
15589
15590     case NEON_BINOP:
15591     case NEON_SETLANE:
15592     case NEON_SCALARMUL:
15593     case NEON_SCALARMULL:
15594     case NEON_SCALARMULH:
15595     case NEON_SHIFTINSERT:
15596     case NEON_LOGICBINOP:
15597       return arm_expand_neon_args (target, icode, 1, exp,
15598         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15599         NEON_ARG_STOP);
15600
15601     case NEON_TERNOP:
15602       return arm_expand_neon_args (target, icode, 1, exp,
15603         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15604         NEON_ARG_CONSTANT, NEON_ARG_STOP);
15605
15606     case NEON_GETLANE:
15607     case NEON_FIXCONV:
15608     case NEON_SHIFTIMM:
15609       return arm_expand_neon_args (target, icode, 1, exp,
15610         NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT, NEON_ARG_CONSTANT,
15611         NEON_ARG_STOP);
15612
15613     case NEON_CREATE:
15614       return arm_expand_neon_args (target, icode, 1, exp,
15615         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15616
15617     case NEON_DUP:
15618     case NEON_SPLIT:
15619     case NEON_REINTERP:
15620       return arm_expand_neon_args (target, icode, 1, exp,
15621         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15622
15623     case NEON_COMBINE:
15624     case NEON_VTBL:
15625       return arm_expand_neon_args (target, icode, 1, exp,
15626         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15627
15628     case NEON_RESULTPAIR:
15629       return arm_expand_neon_args (target, icode, 0, exp,
15630         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15631         NEON_ARG_STOP);
15632
15633     case NEON_LANEMUL:
15634     case NEON_LANEMULL:
15635     case NEON_LANEMULH:
15636       return arm_expand_neon_args (target, icode, 1, exp,
15637         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15638         NEON_ARG_CONSTANT, NEON_ARG_STOP);
15639
15640     case NEON_LANEMAC:
15641       return arm_expand_neon_args (target, icode, 1, exp,
15642         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15643         NEON_ARG_CONSTANT, NEON_ARG_CONSTANT, NEON_ARG_STOP);
15644
15645     case NEON_SHIFTACC:
15646       return arm_expand_neon_args (target, icode, 1, exp,
15647         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15648         NEON_ARG_CONSTANT, NEON_ARG_STOP);
15649
15650     case NEON_SCALARMAC:
15651       return arm_expand_neon_args (target, icode, 1, exp,
15652         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15653         NEON_ARG_CONSTANT, NEON_ARG_STOP);
15654
15655     case NEON_SELECT:
15656     case NEON_VTBX:
15657       return arm_expand_neon_args (target, icode, 1, exp,
15658         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG,
15659         NEON_ARG_STOP);
15660
15661     case NEON_LOAD1:
15662     case NEON_LOADSTRUCT:
15663       return arm_expand_neon_args (target, icode, 1, exp,
15664         NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15665
15666     case NEON_LOAD1LANE:
15667     case NEON_LOADSTRUCTLANE:
15668       return arm_expand_neon_args (target, icode, 1, exp,
15669         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15670         NEON_ARG_STOP);
15671
15672     case NEON_STORE1:
15673     case NEON_STORESTRUCT:
15674       return arm_expand_neon_args (target, icode, 0, exp,
15675         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_STOP);
15676
15677     case NEON_STORE1LANE:
15678     case NEON_STORESTRUCTLANE:
15679       return arm_expand_neon_args (target, icode, 0, exp,
15680         NEON_ARG_COPY_TO_REG, NEON_ARG_COPY_TO_REG, NEON_ARG_CONSTANT,
15681         NEON_ARG_STOP);
15682     }
15683
15684   gcc_unreachable ();
15685 }
15686
15687 /* Emit code to reinterpret one Neon type as another, without altering bits.  */
15688 void
15689 neon_reinterpret (rtx dest, rtx src)
15690 {
15691   emit_move_insn (dest, gen_lowpart (GET_MODE (dest), src));
15692 }
15693
15694 /* Emit code to place a Neon pair result in memory locations (with equal
15695    registers).  */
15696 void
15697 neon_emit_pair_result_insn (enum machine_mode mode,
15698                             rtx (*intfn) (rtx, rtx, rtx, rtx), rtx destaddr,
15699                             rtx op1, rtx op2)
15700 {
15701   rtx mem = gen_rtx_MEM (mode, destaddr);
15702   rtx tmp1 = gen_reg_rtx (mode);
15703   rtx tmp2 = gen_reg_rtx (mode);
15704
15705   emit_insn (intfn (tmp1, op1, tmp2, op2));
15706
15707   emit_move_insn (mem, tmp1);
15708   mem = adjust_address (mem, mode, GET_MODE_SIZE (mode));
15709   emit_move_insn (mem, tmp2);
15710 }
15711
15712 /* Set up operands for a register copy from src to dest, taking care not to
15713    clobber registers in the process.
15714    FIXME: This has rather high polynomial complexity (O(n^3)?) but shouldn't
15715    be called with a large N, so that should be OK.  */
15716
15717 void
15718 neon_disambiguate_copy (rtx *operands, rtx *dest, rtx *src, unsigned int count)
15719 {
15720   unsigned int copied = 0, opctr = 0;
15721   unsigned int done = (1 << count) - 1;
15722   unsigned int i, j;
15723
15724   while (copied != done)
15725     {
15726       for (i = 0; i < count; i++)
15727         {
15728           int good = 1;
15729
15730           for (j = 0; good && j < count; j++)
15731             if (i != j && (copied & (1 << j)) == 0
15732                 && reg_overlap_mentioned_p (src[j], dest[i]))
15733               good = 0;
15734
15735           if (good)
15736             {
15737               operands[opctr++] = dest[i];
15738               operands[opctr++] = src[i];
15739               copied |= 1 << i;
15740             }
15741         }
15742     }
15743
15744   gcc_assert (opctr == count * 2);
15745 }
15746
15747 /* Expand an expression EXP that calls a built-in function,
15748    with result going to TARGET if that's convenient
15749    (and in mode MODE if that's convenient).
15750    SUBTARGET may be used as the target for computing one of EXP's operands.
15751    IGNORE is nonzero if the value is to be ignored.  */
15752
15753 static rtx
15754 arm_expand_builtin (tree exp,
15755                     rtx target,
15756                     rtx subtarget ATTRIBUTE_UNUSED,
15757                     enum machine_mode mode ATTRIBUTE_UNUSED,
15758                     int ignore ATTRIBUTE_UNUSED)
15759 {
15760   const struct builtin_description * d;
15761   enum insn_code    icode;
15762   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
15763   tree              arg0;
15764   tree              arg1;
15765   tree              arg2;
15766   rtx               op0;
15767   rtx               op1;
15768   rtx               op2;
15769   rtx               pat;
15770   int               fcode = DECL_FUNCTION_CODE (fndecl);
15771   size_t            i;
15772   enum machine_mode tmode;
15773   enum machine_mode mode0;
15774   enum machine_mode mode1;
15775   enum machine_mode mode2;
15776
15777   if (fcode >= ARM_BUILTIN_NEON_BASE)
15778     return arm_expand_neon_builtin (fcode, exp, target);
15779
15780   switch (fcode)
15781     {
15782     case ARM_BUILTIN_TEXTRMSB:
15783     case ARM_BUILTIN_TEXTRMUB:
15784     case ARM_BUILTIN_TEXTRMSH:
15785     case ARM_BUILTIN_TEXTRMUH:
15786     case ARM_BUILTIN_TEXTRMSW:
15787     case ARM_BUILTIN_TEXTRMUW:
15788       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
15789                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
15790                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
15791                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
15792                : CODE_FOR_iwmmxt_textrmw);
15793
15794       arg0 = CALL_EXPR_ARG (exp, 0);
15795       arg1 = CALL_EXPR_ARG (exp, 1);
15796       op0 = expand_normal (arg0);
15797       op1 = expand_normal (arg1);
15798       tmode = insn_data[icode].operand[0].mode;
15799       mode0 = insn_data[icode].operand[1].mode;
15800       mode1 = insn_data[icode].operand[2].mode;
15801
15802       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15803         op0 = copy_to_mode_reg (mode0, op0);
15804       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15805         {
15806           /* @@@ better error message */
15807           error ("selector must be an immediate");
15808           return gen_reg_rtx (tmode);
15809         }
15810       if (target == 0
15811           || GET_MODE (target) != tmode
15812           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15813         target = gen_reg_rtx (tmode);
15814       pat = GEN_FCN (icode) (target, op0, op1);
15815       if (! pat)
15816         return 0;
15817       emit_insn (pat);
15818       return target;
15819
15820     case ARM_BUILTIN_TINSRB:
15821     case ARM_BUILTIN_TINSRH:
15822     case ARM_BUILTIN_TINSRW:
15823       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
15824                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
15825                : CODE_FOR_iwmmxt_tinsrw);
15826       arg0 = CALL_EXPR_ARG (exp, 0);
15827       arg1 = CALL_EXPR_ARG (exp, 1);
15828       arg2 = CALL_EXPR_ARG (exp, 2);
15829       op0 = expand_normal (arg0);
15830       op1 = expand_normal (arg1);
15831       op2 = expand_normal (arg2);
15832       tmode = insn_data[icode].operand[0].mode;
15833       mode0 = insn_data[icode].operand[1].mode;
15834       mode1 = insn_data[icode].operand[2].mode;
15835       mode2 = insn_data[icode].operand[3].mode;
15836
15837       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15838         op0 = copy_to_mode_reg (mode0, op0);
15839       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15840         op1 = copy_to_mode_reg (mode1, op1);
15841       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15842         {
15843           /* @@@ better error message */
15844           error ("selector must be an immediate");
15845           return const0_rtx;
15846         }
15847       if (target == 0
15848           || GET_MODE (target) != tmode
15849           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15850         target = gen_reg_rtx (tmode);
15851       pat = GEN_FCN (icode) (target, op0, op1, op2);
15852       if (! pat)
15853         return 0;
15854       emit_insn (pat);
15855       return target;
15856
15857     case ARM_BUILTIN_SETWCX:
15858       arg0 = CALL_EXPR_ARG (exp, 0);
15859       arg1 = CALL_EXPR_ARG (exp, 1);
15860       op0 = force_reg (SImode, expand_normal (arg0));
15861       op1 = expand_normal (arg1);
15862       emit_insn (gen_iwmmxt_tmcr (op1, op0));
15863       return 0;
15864
15865     case ARM_BUILTIN_GETWCX:
15866       arg0 = CALL_EXPR_ARG (exp, 0);
15867       op0 = expand_normal (arg0);
15868       target = gen_reg_rtx (SImode);
15869       emit_insn (gen_iwmmxt_tmrc (target, op0));
15870       return target;
15871
15872     case ARM_BUILTIN_WSHUFH:
15873       icode = CODE_FOR_iwmmxt_wshufh;
15874       arg0 = CALL_EXPR_ARG (exp, 0);
15875       arg1 = CALL_EXPR_ARG (exp, 1);
15876       op0 = expand_normal (arg0);
15877       op1 = expand_normal (arg1);
15878       tmode = insn_data[icode].operand[0].mode;
15879       mode1 = insn_data[icode].operand[1].mode;
15880       mode2 = insn_data[icode].operand[2].mode;
15881
15882       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
15883         op0 = copy_to_mode_reg (mode1, op0);
15884       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
15885         {
15886           /* @@@ better error message */
15887           error ("mask must be an immediate");
15888           return const0_rtx;
15889         }
15890       if (target == 0
15891           || GET_MODE (target) != tmode
15892           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15893         target = gen_reg_rtx (tmode);
15894       pat = GEN_FCN (icode) (target, op0, op1);
15895       if (! pat)
15896         return 0;
15897       emit_insn (pat);
15898       return target;
15899
15900     case ARM_BUILTIN_WSADB:
15901       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
15902     case ARM_BUILTIN_WSADH:
15903       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
15904     case ARM_BUILTIN_WSADBZ:
15905       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
15906     case ARM_BUILTIN_WSADHZ:
15907       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
15908
15909       /* Several three-argument builtins.  */
15910     case ARM_BUILTIN_WMACS:
15911     case ARM_BUILTIN_WMACU:
15912     case ARM_BUILTIN_WALIGN:
15913     case ARM_BUILTIN_TMIA:
15914     case ARM_BUILTIN_TMIAPH:
15915     case ARM_BUILTIN_TMIATT:
15916     case ARM_BUILTIN_TMIATB:
15917     case ARM_BUILTIN_TMIABT:
15918     case ARM_BUILTIN_TMIABB:
15919       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
15920                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
15921                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
15922                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
15923                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
15924                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
15925                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
15926                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
15927                : CODE_FOR_iwmmxt_walign);
15928       arg0 = CALL_EXPR_ARG (exp, 0);
15929       arg1 = CALL_EXPR_ARG (exp, 1);
15930       arg2 = CALL_EXPR_ARG (exp, 2);
15931       op0 = expand_normal (arg0);
15932       op1 = expand_normal (arg1);
15933       op2 = expand_normal (arg2);
15934       tmode = insn_data[icode].operand[0].mode;
15935       mode0 = insn_data[icode].operand[1].mode;
15936       mode1 = insn_data[icode].operand[2].mode;
15937       mode2 = insn_data[icode].operand[3].mode;
15938
15939       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15940         op0 = copy_to_mode_reg (mode0, op0);
15941       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
15942         op1 = copy_to_mode_reg (mode1, op1);
15943       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
15944         op2 = copy_to_mode_reg (mode2, op2);
15945       if (target == 0
15946           || GET_MODE (target) != tmode
15947           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15948         target = gen_reg_rtx (tmode);
15949       pat = GEN_FCN (icode) (target, op0, op1, op2);
15950       if (! pat)
15951         return 0;
15952       emit_insn (pat);
15953       return target;
15954
15955     case ARM_BUILTIN_WZERO:
15956       target = gen_reg_rtx (DImode);
15957       emit_insn (gen_iwmmxt_clrdi (target));
15958       return target;
15959
15960     case ARM_BUILTIN_THREAD_POINTER:
15961       return arm_load_tp (target);
15962
15963     default:
15964       break;
15965     }
15966
15967   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15968     if (d->code == (const enum arm_builtins) fcode)
15969       return arm_expand_binop_builtin (d->icode, exp, target);
15970
15971   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15972     if (d->code == (const enum arm_builtins) fcode)
15973       return arm_expand_unop_builtin (d->icode, exp, target, 0);
15974
15975   /* @@@ Should really do something sensible here.  */
15976   return NULL_RTX;
15977 }
15978 \f
15979 /* Return the number (counting from 0) of
15980    the least significant set bit in MASK.  */
15981
15982 inline static int
15983 number_of_first_bit_set (unsigned mask)
15984 {
15985   int bit;
15986
15987   for (bit = 0;
15988        (mask & (1 << bit)) == 0;
15989        ++bit)
15990     continue;
15991
15992   return bit;
15993 }
15994
15995 /* Emit code to push or pop registers to or from the stack.  F is the
15996    assembly file.  MASK is the registers to push or pop.  PUSH is
15997    nonzero if we should push, and zero if we should pop.  For debugging
15998    output, if pushing, adjust CFA_OFFSET by the amount of space added
15999    to the stack.  REAL_REGS should have the same number of bits set as
16000    MASK, and will be used instead (in the same order) to describe which
16001    registers were saved - this is used to mark the save slots when we
16002    push high registers after moving them to low registers.  */
16003 static void
16004 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
16005                unsigned long real_regs)
16006 {
16007   int regno;
16008   int lo_mask = mask & 0xFF;
16009   int pushed_words = 0;
16010
16011   gcc_assert (mask);
16012
16013   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
16014     {
16015       /* Special case.  Do not generate a POP PC statement here, do it in
16016          thumb_exit() */
16017       thumb_exit (f, -1);
16018       return;
16019     }
16020
16021   if (ARM_EABI_UNWIND_TABLES && push)
16022     {
16023       fprintf (f, "\t.save\t{");
16024       for (regno = 0; regno < 15; regno++)
16025         {
16026           if (real_regs & (1 << regno))
16027             {
16028               if (real_regs & ((1 << regno) -1))
16029                 fprintf (f, ", ");
16030               asm_fprintf (f, "%r", regno);
16031             }
16032         }
16033       fprintf (f, "}\n");
16034     }
16035
16036   fprintf (f, "\t%s\t{", push ? "push" : "pop");
16037
16038   /* Look at the low registers first.  */
16039   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
16040     {
16041       if (lo_mask & 1)
16042         {
16043           asm_fprintf (f, "%r", regno);
16044
16045           if ((lo_mask & ~1) != 0)
16046             fprintf (f, ", ");
16047
16048           pushed_words++;
16049         }
16050     }
16051
16052   if (push && (mask & (1 << LR_REGNUM)))
16053     {
16054       /* Catch pushing the LR.  */
16055       if (mask & 0xFF)
16056         fprintf (f, ", ");
16057
16058       asm_fprintf (f, "%r", LR_REGNUM);
16059
16060       pushed_words++;
16061     }
16062   else if (!push && (mask & (1 << PC_REGNUM)))
16063     {
16064       /* Catch popping the PC.  */
16065       if (TARGET_INTERWORK || TARGET_BACKTRACE
16066           || current_function_calls_eh_return)
16067         {
16068           /* The PC is never poped directly, instead
16069              it is popped into r3 and then BX is used.  */
16070           fprintf (f, "}\n");
16071
16072           thumb_exit (f, -1);
16073
16074           return;
16075         }
16076       else
16077         {
16078           if (mask & 0xFF)
16079             fprintf (f, ", ");
16080
16081           asm_fprintf (f, "%r", PC_REGNUM);
16082         }
16083     }
16084
16085   fprintf (f, "}\n");
16086
16087   if (push && pushed_words && dwarf2out_do_frame ())
16088     {
16089       char *l = dwarf2out_cfi_label ();
16090       int pushed_mask = real_regs;
16091
16092       *cfa_offset += pushed_words * 4;
16093       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
16094
16095       pushed_words = 0;
16096       pushed_mask = real_regs;
16097       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
16098         {
16099           if (pushed_mask & 1)
16100             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
16101         }
16102     }
16103 }
16104
16105 /* Generate code to return from a thumb function.
16106    If 'reg_containing_return_addr' is -1, then the return address is
16107    actually on the stack, at the stack pointer.  */
16108 static void
16109 thumb_exit (FILE *f, int reg_containing_return_addr)
16110 {
16111   unsigned regs_available_for_popping;
16112   unsigned regs_to_pop;
16113   int pops_needed;
16114   unsigned available;
16115   unsigned required;
16116   int mode;
16117   int size;
16118   int restore_a4 = FALSE;
16119
16120   /* Compute the registers we need to pop.  */
16121   regs_to_pop = 0;
16122   pops_needed = 0;
16123
16124   if (reg_containing_return_addr == -1)
16125     {
16126       regs_to_pop |= 1 << LR_REGNUM;
16127       ++pops_needed;
16128     }
16129
16130   if (TARGET_BACKTRACE)
16131     {
16132       /* Restore the (ARM) frame pointer and stack pointer.  */
16133       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
16134       pops_needed += 2;
16135     }
16136
16137   /* If there is nothing to pop then just emit the BX instruction and
16138      return.  */
16139   if (pops_needed == 0)
16140     {
16141       if (current_function_calls_eh_return)
16142         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
16143
16144       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16145       return;
16146     }
16147   /* Otherwise if we are not supporting interworking and we have not created
16148      a backtrace structure and the function was not entered in ARM mode then
16149      just pop the return address straight into the PC.  */
16150   else if (!TARGET_INTERWORK
16151            && !TARGET_BACKTRACE
16152            && !is_called_in_ARM_mode (current_function_decl)
16153            && !current_function_calls_eh_return)
16154     {
16155       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
16156       return;
16157     }
16158
16159   /* Find out how many of the (return) argument registers we can corrupt.  */
16160   regs_available_for_popping = 0;
16161
16162   /* If returning via __builtin_eh_return, the bottom three registers
16163      all contain information needed for the return.  */
16164   if (current_function_calls_eh_return)
16165     size = 12;
16166   else
16167     {
16168       /* If we can deduce the registers used from the function's
16169          return value.  This is more reliable that examining
16170          df_regs_ever_live_p () because that will be set if the register is
16171          ever used in the function, not just if the register is used
16172          to hold a return value.  */
16173
16174       if (current_function_return_rtx != 0)
16175         mode = GET_MODE (current_function_return_rtx);
16176       else
16177         mode = DECL_MODE (DECL_RESULT (current_function_decl));
16178
16179       size = GET_MODE_SIZE (mode);
16180
16181       if (size == 0)
16182         {
16183           /* In a void function we can use any argument register.
16184              In a function that returns a structure on the stack
16185              we can use the second and third argument registers.  */
16186           if (mode == VOIDmode)
16187             regs_available_for_popping =
16188               (1 << ARG_REGISTER (1))
16189               | (1 << ARG_REGISTER (2))
16190               | (1 << ARG_REGISTER (3));
16191           else
16192             regs_available_for_popping =
16193               (1 << ARG_REGISTER (2))
16194               | (1 << ARG_REGISTER (3));
16195         }
16196       else if (size <= 4)
16197         regs_available_for_popping =
16198           (1 << ARG_REGISTER (2))
16199           | (1 << ARG_REGISTER (3));
16200       else if (size <= 8)
16201         regs_available_for_popping =
16202           (1 << ARG_REGISTER (3));
16203     }
16204
16205   /* Match registers to be popped with registers into which we pop them.  */
16206   for (available = regs_available_for_popping,
16207        required  = regs_to_pop;
16208        required != 0 && available != 0;
16209        available &= ~(available & - available),
16210        required  &= ~(required  & - required))
16211     -- pops_needed;
16212
16213   /* If we have any popping registers left over, remove them.  */
16214   if (available > 0)
16215     regs_available_for_popping &= ~available;
16216
16217   /* Otherwise if we need another popping register we can use
16218      the fourth argument register.  */
16219   else if (pops_needed)
16220     {
16221       /* If we have not found any free argument registers and
16222          reg a4 contains the return address, we must move it.  */
16223       if (regs_available_for_popping == 0
16224           && reg_containing_return_addr == LAST_ARG_REGNUM)
16225         {
16226           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16227           reg_containing_return_addr = LR_REGNUM;
16228         }
16229       else if (size > 12)
16230         {
16231           /* Register a4 is being used to hold part of the return value,
16232              but we have dire need of a free, low register.  */
16233           restore_a4 = TRUE;
16234
16235           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
16236         }
16237
16238       if (reg_containing_return_addr != LAST_ARG_REGNUM)
16239         {
16240           /* The fourth argument register is available.  */
16241           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
16242
16243           --pops_needed;
16244         }
16245     }
16246
16247   /* Pop as many registers as we can.  */
16248   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16249                  regs_available_for_popping);
16250
16251   /* Process the registers we popped.  */
16252   if (reg_containing_return_addr == -1)
16253     {
16254       /* The return address was popped into the lowest numbered register.  */
16255       regs_to_pop &= ~(1 << LR_REGNUM);
16256
16257       reg_containing_return_addr =
16258         number_of_first_bit_set (regs_available_for_popping);
16259
16260       /* Remove this register for the mask of available registers, so that
16261          the return address will not be corrupted by further pops.  */
16262       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
16263     }
16264
16265   /* If we popped other registers then handle them here.  */
16266   if (regs_available_for_popping)
16267     {
16268       int frame_pointer;
16269
16270       /* Work out which register currently contains the frame pointer.  */
16271       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
16272
16273       /* Move it into the correct place.  */
16274       asm_fprintf (f, "\tmov\t%r, %r\n",
16275                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
16276
16277       /* (Temporarily) remove it from the mask of popped registers.  */
16278       regs_available_for_popping &= ~(1 << frame_pointer);
16279       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
16280
16281       if (regs_available_for_popping)
16282         {
16283           int stack_pointer;
16284
16285           /* We popped the stack pointer as well,
16286              find the register that contains it.  */
16287           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
16288
16289           /* Move it into the stack register.  */
16290           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
16291
16292           /* At this point we have popped all necessary registers, so
16293              do not worry about restoring regs_available_for_popping
16294              to its correct value:
16295
16296              assert (pops_needed == 0)
16297              assert (regs_available_for_popping == (1 << frame_pointer))
16298              assert (regs_to_pop == (1 << STACK_POINTER))  */
16299         }
16300       else
16301         {
16302           /* Since we have just move the popped value into the frame
16303              pointer, the popping register is available for reuse, and
16304              we know that we still have the stack pointer left to pop.  */
16305           regs_available_for_popping |= (1 << frame_pointer);
16306         }
16307     }
16308
16309   /* If we still have registers left on the stack, but we no longer have
16310      any registers into which we can pop them, then we must move the return
16311      address into the link register and make available the register that
16312      contained it.  */
16313   if (regs_available_for_popping == 0 && pops_needed > 0)
16314     {
16315       regs_available_for_popping |= 1 << reg_containing_return_addr;
16316
16317       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
16318                    reg_containing_return_addr);
16319
16320       reg_containing_return_addr = LR_REGNUM;
16321     }
16322
16323   /* If we have registers left on the stack then pop some more.
16324      We know that at most we will want to pop FP and SP.  */
16325   if (pops_needed > 0)
16326     {
16327       int  popped_into;
16328       int  move_to;
16329
16330       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16331                      regs_available_for_popping);
16332
16333       /* We have popped either FP or SP.
16334          Move whichever one it is into the correct register.  */
16335       popped_into = number_of_first_bit_set (regs_available_for_popping);
16336       move_to     = number_of_first_bit_set (regs_to_pop);
16337
16338       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
16339
16340       regs_to_pop &= ~(1 << move_to);
16341
16342       --pops_needed;
16343     }
16344
16345   /* If we still have not popped everything then we must have only
16346      had one register available to us and we are now popping the SP.  */
16347   if (pops_needed > 0)
16348     {
16349       int  popped_into;
16350
16351       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
16352                      regs_available_for_popping);
16353
16354       popped_into = number_of_first_bit_set (regs_available_for_popping);
16355
16356       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
16357       /*
16358         assert (regs_to_pop == (1 << STACK_POINTER))
16359         assert (pops_needed == 1)
16360       */
16361     }
16362
16363   /* If necessary restore the a4 register.  */
16364   if (restore_a4)
16365     {
16366       if (reg_containing_return_addr != LR_REGNUM)
16367         {
16368           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
16369           reg_containing_return_addr = LR_REGNUM;
16370         }
16371
16372       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
16373     }
16374
16375   if (current_function_calls_eh_return)
16376     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
16377
16378   /* Return to caller.  */
16379   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
16380 }
16381
16382 \f
16383 void
16384 thumb1_final_prescan_insn (rtx insn)
16385 {
16386   if (flag_print_asm_name)
16387     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
16388                  INSN_ADDRESSES (INSN_UID (insn)));
16389 }
16390
16391 int
16392 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
16393 {
16394   unsigned HOST_WIDE_INT mask = 0xff;
16395   int i;
16396
16397   if (val == 0) /* XXX */
16398     return 0;
16399
16400   for (i = 0; i < 25; i++)
16401     if ((val & (mask << i)) == val)
16402       return 1;
16403
16404   return 0;
16405 }
16406
16407 /* Returns nonzero if the current function contains,
16408    or might contain a far jump.  */
16409 static int
16410 thumb_far_jump_used_p (void)
16411 {
16412   rtx insn;
16413
16414   /* This test is only important for leaf functions.  */
16415   /* assert (!leaf_function_p ()); */
16416
16417   /* If we have already decided that far jumps may be used,
16418      do not bother checking again, and always return true even if
16419      it turns out that they are not being used.  Once we have made
16420      the decision that far jumps are present (and that hence the link
16421      register will be pushed onto the stack) we cannot go back on it.  */
16422   if (cfun->machine->far_jump_used)
16423     return 1;
16424
16425   /* If this function is not being called from the prologue/epilogue
16426      generation code then it must be being called from the
16427      INITIAL_ELIMINATION_OFFSET macro.  */
16428   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
16429     {
16430       /* In this case we know that we are being asked about the elimination
16431          of the arg pointer register.  If that register is not being used,
16432          then there are no arguments on the stack, and we do not have to
16433          worry that a far jump might force the prologue to push the link
16434          register, changing the stack offsets.  In this case we can just
16435          return false, since the presence of far jumps in the function will
16436          not affect stack offsets.
16437
16438          If the arg pointer is live (or if it was live, but has now been
16439          eliminated and so set to dead) then we do have to test to see if
16440          the function might contain a far jump.  This test can lead to some
16441          false negatives, since before reload is completed, then length of
16442          branch instructions is not known, so gcc defaults to returning their
16443          longest length, which in turn sets the far jump attribute to true.
16444
16445          A false negative will not result in bad code being generated, but it
16446          will result in a needless push and pop of the link register.  We
16447          hope that this does not occur too often.
16448
16449          If we need doubleword stack alignment this could affect the other
16450          elimination offsets so we can't risk getting it wrong.  */
16451       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
16452         cfun->machine->arg_pointer_live = 1;
16453       else if (!cfun->machine->arg_pointer_live)
16454         return 0;
16455     }
16456
16457   /* Check to see if the function contains a branch
16458      insn with the far jump attribute set.  */
16459   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16460     {
16461       if (GET_CODE (insn) == JUMP_INSN
16462           /* Ignore tablejump patterns.  */
16463           && GET_CODE (PATTERN (insn)) != ADDR_VEC
16464           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
16465           && get_attr_far_jump (insn) == FAR_JUMP_YES
16466           )
16467         {
16468           /* Record the fact that we have decided that
16469              the function does use far jumps.  */
16470           cfun->machine->far_jump_used = 1;
16471           return 1;
16472         }
16473     }
16474
16475   return 0;
16476 }
16477
16478 /* Return nonzero if FUNC must be entered in ARM mode.  */
16479 int
16480 is_called_in_ARM_mode (tree func)
16481 {
16482   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
16483
16484   /* Ignore the problem about functions whose address is taken.  */
16485   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
16486     return TRUE;
16487
16488 #ifdef ARM_PE
16489   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
16490 #else
16491   return FALSE;
16492 #endif
16493 }
16494
16495 /* The bits which aren't usefully expanded as rtl.  */
16496 const char *
16497 thumb_unexpanded_epilogue (void)
16498 {
16499   int regno;
16500   unsigned long live_regs_mask = 0;
16501   int high_regs_pushed = 0;
16502   int had_to_push_lr;
16503   int size;
16504
16505   if (return_used_this_function)
16506     return "";
16507
16508   if (IS_NAKED (arm_current_func_type ()))
16509     return "";
16510
16511   live_regs_mask = thumb1_compute_save_reg_mask ();
16512   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
16513
16514   /* If we can deduce the registers used from the function's return value.
16515      This is more reliable that examining df_regs_ever_live_p () because that
16516      will be set if the register is ever used in the function, not just if
16517      the register is used to hold a return value.  */
16518   size = arm_size_return_regs ();
16519
16520   /* The prolog may have pushed some high registers to use as
16521      work registers.  e.g. the testsuite file:
16522      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
16523      compiles to produce:
16524         push    {r4, r5, r6, r7, lr}
16525         mov     r7, r9
16526         mov     r6, r8
16527         push    {r6, r7}
16528      as part of the prolog.  We have to undo that pushing here.  */
16529
16530   if (high_regs_pushed)
16531     {
16532       unsigned long mask = live_regs_mask & 0xff;
16533       int next_hi_reg;
16534
16535       /* The available low registers depend on the size of the value we are
16536          returning.  */
16537       if (size <= 12)
16538         mask |=  1 << 3;
16539       if (size <= 8)
16540         mask |= 1 << 2;
16541
16542       if (mask == 0)
16543         /* Oh dear!  We have no low registers into which we can pop
16544            high registers!  */
16545         internal_error
16546           ("no low registers available for popping high registers");
16547
16548       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
16549         if (live_regs_mask & (1 << next_hi_reg))
16550           break;
16551
16552       while (high_regs_pushed)
16553         {
16554           /* Find lo register(s) into which the high register(s) can
16555              be popped.  */
16556           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
16557             {
16558               if (mask & (1 << regno))
16559                 high_regs_pushed--;
16560               if (high_regs_pushed == 0)
16561                 break;
16562             }
16563
16564           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
16565
16566           /* Pop the values into the low register(s).  */
16567           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
16568
16569           /* Move the value(s) into the high registers.  */
16570           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
16571             {
16572               if (mask & (1 << regno))
16573                 {
16574                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
16575                                regno);
16576
16577                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
16578                     if (live_regs_mask & (1 << next_hi_reg))
16579                       break;
16580                 }
16581             }
16582         }
16583       live_regs_mask &= ~0x0f00;
16584     }
16585
16586   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
16587   live_regs_mask &= 0xff;
16588
16589   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
16590     {
16591       /* Pop the return address into the PC.  */
16592       if (had_to_push_lr)
16593         live_regs_mask |= 1 << PC_REGNUM;
16594
16595       /* Either no argument registers were pushed or a backtrace
16596          structure was created which includes an adjusted stack
16597          pointer, so just pop everything.  */
16598       if (live_regs_mask)
16599         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
16600                        live_regs_mask);
16601
16602       /* We have either just popped the return address into the
16603          PC or it is was kept in LR for the entire function.  */
16604       if (!had_to_push_lr)
16605         thumb_exit (asm_out_file, LR_REGNUM);
16606     }
16607   else
16608     {
16609       /* Pop everything but the return address.  */
16610       if (live_regs_mask)
16611         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
16612                        live_regs_mask);
16613
16614       if (had_to_push_lr)
16615         {
16616           if (size > 12)
16617             {
16618               /* We have no free low regs, so save one.  */
16619               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
16620                            LAST_ARG_REGNUM);
16621             }
16622
16623           /* Get the return address into a temporary register.  */
16624           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
16625                          1 << LAST_ARG_REGNUM);
16626
16627           if (size > 12)
16628             {
16629               /* Move the return address to lr.  */
16630               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
16631                            LAST_ARG_REGNUM);
16632               /* Restore the low register.  */
16633               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
16634                            IP_REGNUM);
16635               regno = LR_REGNUM;
16636             }
16637           else
16638             regno = LAST_ARG_REGNUM;
16639         }
16640       else
16641         regno = LR_REGNUM;
16642
16643       /* Remove the argument registers that were pushed onto the stack.  */
16644       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
16645                    SP_REGNUM, SP_REGNUM,
16646                    current_function_pretend_args_size);
16647
16648       thumb_exit (asm_out_file, regno);
16649     }
16650
16651   return "";
16652 }
16653
16654 /* Functions to save and restore machine-specific function data.  */
16655 static struct machine_function *
16656 arm_init_machine_status (void)
16657 {
16658   struct machine_function *machine;
16659   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
16660
16661 #if ARM_FT_UNKNOWN != 0
16662   machine->func_type = ARM_FT_UNKNOWN;
16663 #endif
16664   return machine;
16665 }
16666
16667 /* Return an RTX indicating where the return address to the
16668    calling function can be found.  */
16669 rtx
16670 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
16671 {
16672   if (count != 0)
16673     return NULL_RTX;
16674
16675   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
16676 }
16677
16678 /* Do anything needed before RTL is emitted for each function.  */
16679 void
16680 arm_init_expanders (void)
16681 {
16682   /* Arrange to initialize and mark the machine per-function status.  */
16683   init_machine_status = arm_init_machine_status;
16684
16685   /* This is to stop the combine pass optimizing away the alignment
16686      adjustment of va_arg.  */
16687   /* ??? It is claimed that this should not be necessary.  */
16688   if (cfun)
16689     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
16690 }
16691
16692
16693 /* Like arm_compute_initial_elimination offset.  Simpler because there
16694    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
16695    to point at the base of the local variables after static stack
16696    space for a function has been allocated.  */
16697
16698 HOST_WIDE_INT
16699 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
16700 {
16701   arm_stack_offsets *offsets;
16702
16703   offsets = arm_get_frame_offsets ();
16704
16705   switch (from)
16706     {
16707     case ARG_POINTER_REGNUM:
16708       switch (to)
16709         {
16710         case STACK_POINTER_REGNUM:
16711           return offsets->outgoing_args - offsets->saved_args;
16712
16713         case FRAME_POINTER_REGNUM:
16714           return offsets->soft_frame - offsets->saved_args;
16715
16716         case ARM_HARD_FRAME_POINTER_REGNUM:
16717           return offsets->saved_regs - offsets->saved_args;
16718
16719         case THUMB_HARD_FRAME_POINTER_REGNUM:
16720           return offsets->locals_base - offsets->saved_args;
16721
16722         default:
16723           gcc_unreachable ();
16724         }
16725       break;
16726
16727     case FRAME_POINTER_REGNUM:
16728       switch (to)
16729         {
16730         case STACK_POINTER_REGNUM:
16731           return offsets->outgoing_args - offsets->soft_frame;
16732
16733         case ARM_HARD_FRAME_POINTER_REGNUM:
16734           return offsets->saved_regs - offsets->soft_frame;
16735
16736         case THUMB_HARD_FRAME_POINTER_REGNUM:
16737           return offsets->locals_base - offsets->soft_frame;
16738
16739         default:
16740           gcc_unreachable ();
16741         }
16742       break;
16743
16744     default:
16745       gcc_unreachable ();
16746     }
16747 }
16748
16749 /* Generate the rest of a function's prologue.  */
16750 void
16751 thumb1_expand_prologue (void)
16752 {
16753   rtx insn, dwarf;
16754
16755   HOST_WIDE_INT amount;
16756   arm_stack_offsets *offsets;
16757   unsigned long func_type;
16758   int regno;
16759   unsigned long live_regs_mask;
16760
16761   func_type = arm_current_func_type ();
16762
16763   /* Naked functions don't have prologues.  */
16764   if (IS_NAKED (func_type))
16765     return;
16766
16767   if (IS_INTERRUPT (func_type))
16768     {
16769       error ("interrupt Service Routines cannot be coded in Thumb mode");
16770       return;
16771     }
16772
16773   live_regs_mask = thumb1_compute_save_reg_mask ();
16774   /* Load the pic register before setting the frame pointer,
16775      so we can use r7 as a temporary work register.  */
16776   if (flag_pic && arm_pic_register != INVALID_REGNUM)
16777     arm_load_pic_register (live_regs_mask);
16778
16779   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
16780     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
16781                     stack_pointer_rtx);
16782
16783   offsets = arm_get_frame_offsets ();
16784   amount = offsets->outgoing_args - offsets->saved_regs;
16785   if (amount)
16786     {
16787       if (amount < 512)
16788         {
16789           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
16790                                         GEN_INT (- amount)));
16791           RTX_FRAME_RELATED_P (insn) = 1;
16792         }
16793       else
16794         {
16795           rtx reg;
16796
16797           /* The stack decrement is too big for an immediate value in a single
16798              insn.  In theory we could issue multiple subtracts, but after
16799              three of them it becomes more space efficient to place the full
16800              value in the constant pool and load into a register.  (Also the
16801              ARM debugger really likes to see only one stack decrement per
16802              function).  So instead we look for a scratch register into which
16803              we can load the decrement, and then we subtract this from the
16804              stack pointer.  Unfortunately on the thumb the only available
16805              scratch registers are the argument registers, and we cannot use
16806              these as they may hold arguments to the function.  Instead we
16807              attempt to locate a call preserved register which is used by this
16808              function.  If we can find one, then we know that it will have
16809              been pushed at the start of the prologue and so we can corrupt
16810              it now.  */
16811           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
16812             if (live_regs_mask & (1 << regno)
16813                 && !(frame_pointer_needed
16814                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
16815               break;
16816
16817           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
16818             {
16819               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
16820
16821               /* Choose an arbitrary, non-argument low register.  */
16822               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
16823
16824               /* Save it by copying it into a high, scratch register.  */
16825               emit_insn (gen_movsi (spare, reg));
16826               /* Add a USE to stop propagate_one_insn() from barfing.  */
16827               emit_insn (gen_prologue_use (spare));
16828
16829               /* Decrement the stack.  */
16830               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
16831               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
16832                                             stack_pointer_rtx, reg));
16833               RTX_FRAME_RELATED_P (insn) = 1;
16834               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
16835                                    plus_constant (stack_pointer_rtx,
16836                                                   -amount));
16837               RTX_FRAME_RELATED_P (dwarf) = 1;
16838               REG_NOTES (insn)
16839                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
16840                                      REG_NOTES (insn));
16841
16842               /* Restore the low register's original value.  */
16843               emit_insn (gen_movsi (reg, spare));
16844
16845               /* Emit a USE of the restored scratch register, so that flow
16846                  analysis will not consider the restore redundant.  The
16847                  register won't be used again in this function and isn't
16848                  restored by the epilogue.  */
16849               emit_insn (gen_prologue_use (reg));
16850             }
16851           else
16852             {
16853               reg = gen_rtx_REG (SImode, regno);
16854
16855               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
16856
16857               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
16858                                             stack_pointer_rtx, reg));
16859               RTX_FRAME_RELATED_P (insn) = 1;
16860               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
16861                                    plus_constant (stack_pointer_rtx,
16862                                                   -amount));
16863               RTX_FRAME_RELATED_P (dwarf) = 1;
16864               REG_NOTES (insn)
16865                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
16866                                      REG_NOTES (insn));
16867             }
16868         }
16869     }
16870
16871   if (frame_pointer_needed)
16872     thumb_set_frame_pointer (offsets);
16873
16874   /* If we are profiling, make sure no instructions are scheduled before
16875      the call to mcount.  Similarly if the user has requested no
16876      scheduling in the prolog.  Similarly if we want non-call exceptions
16877      using the EABI unwinder, to prevent faulting instructions from being
16878      swapped with a stack adjustment.  */
16879   if (current_function_profile || !TARGET_SCHED_PROLOG
16880       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
16881     emit_insn (gen_blockage ());
16882
16883   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
16884   if (live_regs_mask & 0xff)
16885     cfun->machine->lr_save_eliminated = 0;
16886 }
16887
16888
16889 void
16890 thumb1_expand_epilogue (void)
16891 {
16892   HOST_WIDE_INT amount;
16893   arm_stack_offsets *offsets;
16894   int regno;
16895
16896   /* Naked functions don't have prologues.  */
16897   if (IS_NAKED (arm_current_func_type ()))
16898     return;
16899
16900   offsets = arm_get_frame_offsets ();
16901   amount = offsets->outgoing_args - offsets->saved_regs;
16902
16903   if (frame_pointer_needed)
16904     {
16905       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
16906       amount = offsets->locals_base - offsets->saved_regs;
16907     }
16908
16909   gcc_assert (amount >= 0);
16910   if (amount)
16911     {
16912       if (amount < 512)
16913         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
16914                                GEN_INT (amount)));
16915       else
16916         {
16917           /* r3 is always free in the epilogue.  */
16918           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
16919
16920           emit_insn (gen_movsi (reg, GEN_INT (amount)));
16921           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
16922         }
16923     }
16924
16925   /* Emit a USE (stack_pointer_rtx), so that
16926      the stack adjustment will not be deleted.  */
16927   emit_insn (gen_prologue_use (stack_pointer_rtx));
16928
16929   if (current_function_profile || !TARGET_SCHED_PROLOG)
16930     emit_insn (gen_blockage ());
16931
16932   /* Emit a clobber for each insn that will be restored in the epilogue,
16933      so that flow2 will get register lifetimes correct.  */
16934   for (regno = 0; regno < 13; regno++)
16935     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
16936       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
16937
16938   if (! df_regs_ever_live_p (LR_REGNUM))
16939     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
16940 }
16941
16942 static void
16943 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
16944 {
16945   unsigned long live_regs_mask = 0;
16946   unsigned long l_mask;
16947   unsigned high_regs_pushed = 0;
16948   int cfa_offset = 0;
16949   int regno;
16950
16951   if (IS_NAKED (arm_current_func_type ()))
16952     return;
16953
16954   if (is_called_in_ARM_mode (current_function_decl))
16955     {
16956       const char * name;
16957
16958       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
16959       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
16960                   == SYMBOL_REF);
16961       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
16962
16963       /* Generate code sequence to switch us into Thumb mode.  */
16964       /* The .code 32 directive has already been emitted by
16965          ASM_DECLARE_FUNCTION_NAME.  */
16966       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
16967       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
16968
16969       /* Generate a label, so that the debugger will notice the
16970          change in instruction sets.  This label is also used by
16971          the assembler to bypass the ARM code when this function
16972          is called from a Thumb encoded function elsewhere in the
16973          same file.  Hence the definition of STUB_NAME here must
16974          agree with the definition in gas/config/tc-arm.c.  */
16975
16976 #define STUB_NAME ".real_start_of"
16977
16978       fprintf (f, "\t.code\t16\n");
16979 #ifdef ARM_PE
16980       if (arm_dllexport_name_p (name))
16981         name = arm_strip_name_encoding (name);
16982 #endif
16983       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
16984       fprintf (f, "\t.thumb_func\n");
16985       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
16986     }
16987
16988   if (current_function_pretend_args_size)
16989     {
16990       /* Output unwind directive for the stack adjustment.  */
16991       if (ARM_EABI_UNWIND_TABLES)
16992         fprintf (f, "\t.pad #%d\n",
16993                  current_function_pretend_args_size);
16994
16995       if (cfun->machine->uses_anonymous_args)
16996         {
16997           int num_pushes;
16998
16999           fprintf (f, "\tpush\t{");
17000
17001           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
17002
17003           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
17004                regno <= LAST_ARG_REGNUM;
17005                regno++)
17006             asm_fprintf (f, "%r%s", regno,
17007                          regno == LAST_ARG_REGNUM ? "" : ", ");
17008
17009           fprintf (f, "}\n");
17010         }
17011       else
17012         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
17013                      SP_REGNUM, SP_REGNUM,
17014                      current_function_pretend_args_size);
17015
17016       /* We don't need to record the stores for unwinding (would it
17017          help the debugger any if we did?), but record the change in
17018          the stack pointer.  */
17019       if (dwarf2out_do_frame ())
17020         {
17021           char *l = dwarf2out_cfi_label ();
17022
17023           cfa_offset = cfa_offset + current_function_pretend_args_size;
17024           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17025         }
17026     }
17027
17028   /* Get the registers we are going to push.  */
17029   live_regs_mask = thumb1_compute_save_reg_mask ();
17030   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
17031   l_mask = live_regs_mask & 0x40ff;
17032   /* Then count how many other high registers will need to be pushed.  */
17033   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
17034
17035   if (TARGET_BACKTRACE)
17036     {
17037       unsigned offset;
17038       unsigned work_register;
17039
17040       /* We have been asked to create a stack backtrace structure.
17041          The code looks like this:
17042
17043          0   .align 2
17044          0   func:
17045          0     sub   SP, #16         Reserve space for 4 registers.
17046          2     push  {R7}            Push low registers.
17047          4     add   R7, SP, #20     Get the stack pointer before the push.
17048          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
17049          8     mov   R7, PC          Get hold of the start of this code plus 12.
17050         10     str   R7, [SP, #16]   Store it.
17051         12     mov   R7, FP          Get hold of the current frame pointer.
17052         14     str   R7, [SP, #4]    Store it.
17053         16     mov   R7, LR          Get hold of the current return address.
17054         18     str   R7, [SP, #12]   Store it.
17055         20     add   R7, SP, #16     Point at the start of the backtrace structure.
17056         22     mov   FP, R7          Put this value into the frame pointer.  */
17057
17058       work_register = thumb_find_work_register (live_regs_mask);
17059
17060       if (ARM_EABI_UNWIND_TABLES)
17061         asm_fprintf (f, "\t.pad #16\n");
17062
17063       asm_fprintf
17064         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
17065          SP_REGNUM, SP_REGNUM);
17066
17067       if (dwarf2out_do_frame ())
17068         {
17069           char *l = dwarf2out_cfi_label ();
17070
17071           cfa_offset = cfa_offset + 16;
17072           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
17073         }
17074
17075       if (l_mask)
17076         {
17077           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
17078           offset = bit_count (l_mask) * UNITS_PER_WORD;
17079         }
17080       else
17081         offset = 0;
17082
17083       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17084                    offset + 16 + current_function_pretend_args_size);
17085
17086       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17087                    offset + 4);
17088
17089       /* Make sure that the instruction fetching the PC is in the right place
17090          to calculate "start of backtrace creation code + 12".  */
17091       if (l_mask)
17092         {
17093           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17094           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17095                        offset + 12);
17096           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17097                        ARM_HARD_FRAME_POINTER_REGNUM);
17098           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17099                        offset);
17100         }
17101       else
17102         {
17103           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
17104                        ARM_HARD_FRAME_POINTER_REGNUM);
17105           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17106                        offset);
17107           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
17108           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17109                        offset + 12);
17110         }
17111
17112       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
17113       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
17114                    offset + 8);
17115       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
17116                    offset + 12);
17117       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
17118                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
17119     }
17120   /* Optimization:  If we are not pushing any low registers but we are going
17121      to push some high registers then delay our first push.  This will just
17122      be a push of LR and we can combine it with the push of the first high
17123      register.  */
17124   else if ((l_mask & 0xff) != 0
17125            || (high_regs_pushed == 0 && l_mask))
17126     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
17127
17128   if (high_regs_pushed)
17129     {
17130       unsigned pushable_regs;
17131       unsigned next_hi_reg;
17132
17133       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
17134         if (live_regs_mask & (1 << next_hi_reg))
17135           break;
17136
17137       pushable_regs = l_mask & 0xff;
17138
17139       if (pushable_regs == 0)
17140         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
17141
17142       while (high_regs_pushed > 0)
17143         {
17144           unsigned long real_regs_mask = 0;
17145
17146           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
17147             {
17148               if (pushable_regs & (1 << regno))
17149                 {
17150                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
17151
17152                   high_regs_pushed --;
17153                   real_regs_mask |= (1 << next_hi_reg);
17154
17155                   if (high_regs_pushed)
17156                     {
17157                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
17158                            next_hi_reg --)
17159                         if (live_regs_mask & (1 << next_hi_reg))
17160                           break;
17161                     }
17162                   else
17163                     {
17164                       pushable_regs &= ~((1 << regno) - 1);
17165                       break;
17166                     }
17167                 }
17168             }
17169
17170           /* If we had to find a work register and we have not yet
17171              saved the LR then add it to the list of regs to push.  */
17172           if (l_mask == (1 << LR_REGNUM))
17173             {
17174               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
17175                              1, &cfa_offset,
17176                              real_regs_mask | (1 << LR_REGNUM));
17177               l_mask = 0;
17178             }
17179           else
17180             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
17181         }
17182     }
17183 }
17184
17185 /* Handle the case of a double word load into a low register from
17186    a computed memory address.  The computed address may involve a
17187    register which is overwritten by the load.  */
17188 const char *
17189 thumb_load_double_from_address (rtx *operands)
17190 {
17191   rtx addr;
17192   rtx base;
17193   rtx offset;
17194   rtx arg1;
17195   rtx arg2;
17196
17197   gcc_assert (GET_CODE (operands[0]) == REG);
17198   gcc_assert (GET_CODE (operands[1]) == MEM);
17199
17200   /* Get the memory address.  */
17201   addr = XEXP (operands[1], 0);
17202
17203   /* Work out how the memory address is computed.  */
17204   switch (GET_CODE (addr))
17205     {
17206     case REG:
17207       operands[2] = adjust_address (operands[1], SImode, 4);
17208
17209       if (REGNO (operands[0]) == REGNO (addr))
17210         {
17211           output_asm_insn ("ldr\t%H0, %2", operands);
17212           output_asm_insn ("ldr\t%0, %1", operands);
17213         }
17214       else
17215         {
17216           output_asm_insn ("ldr\t%0, %1", operands);
17217           output_asm_insn ("ldr\t%H0, %2", operands);
17218         }
17219       break;
17220
17221     case CONST:
17222       /* Compute <address> + 4 for the high order load.  */
17223       operands[2] = adjust_address (operands[1], SImode, 4);
17224
17225       output_asm_insn ("ldr\t%0, %1", operands);
17226       output_asm_insn ("ldr\t%H0, %2", operands);
17227       break;
17228
17229     case PLUS:
17230       arg1   = XEXP (addr, 0);
17231       arg2   = XEXP (addr, 1);
17232
17233       if (CONSTANT_P (arg1))
17234         base = arg2, offset = arg1;
17235       else
17236         base = arg1, offset = arg2;
17237
17238       gcc_assert (GET_CODE (base) == REG);
17239
17240       /* Catch the case of <address> = <reg> + <reg> */
17241       if (GET_CODE (offset) == REG)
17242         {
17243           int reg_offset = REGNO (offset);
17244           int reg_base   = REGNO (base);
17245           int reg_dest   = REGNO (operands[0]);
17246
17247           /* Add the base and offset registers together into the
17248              higher destination register.  */
17249           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
17250                        reg_dest + 1, reg_base, reg_offset);
17251
17252           /* Load the lower destination register from the address in
17253              the higher destination register.  */
17254           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
17255                        reg_dest, reg_dest + 1);
17256
17257           /* Load the higher destination register from its own address
17258              plus 4.  */
17259           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
17260                        reg_dest + 1, reg_dest + 1);
17261         }
17262       else
17263         {
17264           /* Compute <address> + 4 for the high order load.  */
17265           operands[2] = adjust_address (operands[1], SImode, 4);
17266
17267           /* If the computed address is held in the low order register
17268              then load the high order register first, otherwise always
17269              load the low order register first.  */
17270           if (REGNO (operands[0]) == REGNO (base))
17271             {
17272               output_asm_insn ("ldr\t%H0, %2", operands);
17273               output_asm_insn ("ldr\t%0, %1", operands);
17274             }
17275           else
17276             {
17277               output_asm_insn ("ldr\t%0, %1", operands);
17278               output_asm_insn ("ldr\t%H0, %2", operands);
17279             }
17280         }
17281       break;
17282
17283     case LABEL_REF:
17284       /* With no registers to worry about we can just load the value
17285          directly.  */
17286       operands[2] = adjust_address (operands[1], SImode, 4);
17287
17288       output_asm_insn ("ldr\t%H0, %2", operands);
17289       output_asm_insn ("ldr\t%0, %1", operands);
17290       break;
17291
17292     default:
17293       gcc_unreachable ();
17294     }
17295
17296   return "";
17297 }
17298
17299 const char *
17300 thumb_output_move_mem_multiple (int n, rtx *operands)
17301 {
17302   rtx tmp;
17303
17304   switch (n)
17305     {
17306     case 2:
17307       if (REGNO (operands[4]) > REGNO (operands[5]))
17308         {
17309           tmp = operands[4];
17310           operands[4] = operands[5];
17311           operands[5] = tmp;
17312         }
17313       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
17314       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
17315       break;
17316
17317     case 3:
17318       if (REGNO (operands[4]) > REGNO (operands[5]))
17319         {
17320           tmp = operands[4];
17321           operands[4] = operands[5];
17322           operands[5] = tmp;
17323         }
17324       if (REGNO (operands[5]) > REGNO (operands[6]))
17325         {
17326           tmp = operands[5];
17327           operands[5] = operands[6];
17328           operands[6] = tmp;
17329         }
17330       if (REGNO (operands[4]) > REGNO (operands[5]))
17331         {
17332           tmp = operands[4];
17333           operands[4] = operands[5];
17334           operands[5] = tmp;
17335         }
17336
17337       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
17338       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
17339       break;
17340
17341     default:
17342       gcc_unreachable ();
17343     }
17344
17345   return "";
17346 }
17347
17348 /* Output a call-via instruction for thumb state.  */
17349 const char *
17350 thumb_call_via_reg (rtx reg)
17351 {
17352   int regno = REGNO (reg);
17353   rtx *labelp;
17354
17355   gcc_assert (regno < LR_REGNUM);
17356
17357   /* If we are in the normal text section we can use a single instance
17358      per compilation unit.  If we are doing function sections, then we need
17359      an entry per section, since we can't rely on reachability.  */
17360   if (in_section == text_section)
17361     {
17362       thumb_call_reg_needed = 1;
17363
17364       if (thumb_call_via_label[regno] == NULL)
17365         thumb_call_via_label[regno] = gen_label_rtx ();
17366       labelp = thumb_call_via_label + regno;
17367     }
17368   else
17369     {
17370       if (cfun->machine->call_via[regno] == NULL)
17371         cfun->machine->call_via[regno] = gen_label_rtx ();
17372       labelp = cfun->machine->call_via + regno;
17373     }
17374
17375   output_asm_insn ("bl\t%a0", labelp);
17376   return "";
17377 }
17378
17379 /* Routines for generating rtl.  */
17380 void
17381 thumb_expand_movmemqi (rtx *operands)
17382 {
17383   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
17384   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
17385   HOST_WIDE_INT len = INTVAL (operands[2]);
17386   HOST_WIDE_INT offset = 0;
17387
17388   while (len >= 12)
17389     {
17390       emit_insn (gen_movmem12b (out, in, out, in));
17391       len -= 12;
17392     }
17393
17394   if (len >= 8)
17395     {
17396       emit_insn (gen_movmem8b (out, in, out, in));
17397       len -= 8;
17398     }
17399
17400   if (len >= 4)
17401     {
17402       rtx reg = gen_reg_rtx (SImode);
17403       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
17404       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
17405       len -= 4;
17406       offset += 4;
17407     }
17408
17409   if (len >= 2)
17410     {
17411       rtx reg = gen_reg_rtx (HImode);
17412       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
17413                                               plus_constant (in, offset))));
17414       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
17415                             reg));
17416       len -= 2;
17417       offset += 2;
17418     }
17419
17420   if (len)
17421     {
17422       rtx reg = gen_reg_rtx (QImode);
17423       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
17424                                               plus_constant (in, offset))));
17425       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
17426                             reg));
17427     }
17428 }
17429
17430 void
17431 thumb_reload_out_hi (rtx *operands)
17432 {
17433   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
17434 }
17435
17436 /* Handle reading a half-word from memory during reload.  */
17437 void
17438 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
17439 {
17440   gcc_unreachable ();
17441 }
17442
17443 /* Return the length of a function name prefix
17444     that starts with the character 'c'.  */
17445 static int
17446 arm_get_strip_length (int c)
17447 {
17448   switch (c)
17449     {
17450     ARM_NAME_ENCODING_LENGTHS
17451       default: return 0;
17452     }
17453 }
17454
17455 /* Return a pointer to a function's name with any
17456    and all prefix encodings stripped from it.  */
17457 const char *
17458 arm_strip_name_encoding (const char *name)
17459 {
17460   int skip;
17461
17462   while ((skip = arm_get_strip_length (* name)))
17463     name += skip;
17464
17465   return name;
17466 }
17467
17468 /* If there is a '*' anywhere in the name's prefix, then
17469    emit the stripped name verbatim, otherwise prepend an
17470    underscore if leading underscores are being used.  */
17471 void
17472 arm_asm_output_labelref (FILE *stream, const char *name)
17473 {
17474   int skip;
17475   int verbatim = 0;
17476
17477   while ((skip = arm_get_strip_length (* name)))
17478     {
17479       verbatim |= (*name == '*');
17480       name += skip;
17481     }
17482
17483   if (verbatim)
17484     fputs (name, stream);
17485   else
17486     asm_fprintf (stream, "%U%s", name);
17487 }
17488
17489 static void
17490 arm_file_start (void)
17491 {
17492   int val;
17493
17494   if (TARGET_UNIFIED_ASM)
17495     asm_fprintf (asm_out_file, "\t.syntax unified\n");
17496
17497   if (TARGET_BPABI)
17498     {
17499       const char *fpu_name;
17500       if (arm_select[0].string)
17501         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
17502       else if (arm_select[1].string)
17503         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
17504       else
17505         asm_fprintf (asm_out_file, "\t.cpu %s\n",
17506                      all_cores[arm_default_cpu].name);
17507
17508       if (TARGET_SOFT_FLOAT)
17509         {
17510           if (TARGET_VFP)
17511             fpu_name = "softvfp";
17512           else
17513             fpu_name = "softfpa";
17514         }
17515       else
17516         {
17517           int set_float_abi_attributes = 0;
17518           switch (arm_fpu_arch)
17519             {
17520             case FPUTYPE_FPA:
17521               fpu_name = "fpa";
17522               break;
17523             case FPUTYPE_FPA_EMU2:
17524               fpu_name = "fpe2";
17525               break;
17526             case FPUTYPE_FPA_EMU3:
17527               fpu_name = "fpe3";
17528               break;
17529             case FPUTYPE_MAVERICK:
17530               fpu_name = "maverick";
17531               break;
17532             case FPUTYPE_VFP:
17533               fpu_name = "vfp";
17534               set_float_abi_attributes = 1;
17535               break;
17536             case FPUTYPE_VFP3:
17537               fpu_name = "vfp3";
17538               set_float_abi_attributes = 1;
17539               break;
17540             case FPUTYPE_NEON:
17541               fpu_name = "neon";
17542               set_float_abi_attributes = 1;
17543               break;
17544             default:
17545               abort();
17546             }
17547           if (set_float_abi_attributes)
17548             {
17549               if (TARGET_HARD_FLOAT)
17550                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
17551               if (TARGET_HARD_FLOAT_ABI)
17552                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
17553             }
17554         }
17555       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
17556
17557       /* Some of these attributes only apply when the corresponding features
17558          are used.  However we don't have any easy way of figuring this out.
17559          Conservatively record the setting that would have been used.  */
17560
17561       /* Tag_ABI_PCS_wchar_t.  */
17562       asm_fprintf (asm_out_file, "\t.eabi_attribute 18, %d\n",
17563                    (int)WCHAR_TYPE_SIZE / BITS_PER_UNIT);
17564
17565       /* Tag_ABI_FP_rounding.  */
17566       if (flag_rounding_math)
17567         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
17568       if (!flag_unsafe_math_optimizations)
17569         {
17570           /* Tag_ABI_FP_denomal.  */
17571           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
17572           /* Tag_ABI_FP_exceptions.  */
17573           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
17574         }
17575       /* Tag_ABI_FP_user_exceptions.  */
17576       if (flag_signaling_nans)
17577         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
17578       /* Tag_ABI_FP_number_model.  */
17579       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
17580                    flag_finite_math_only ? 1 : 3);
17581
17582       /* Tag_ABI_align8_needed.  */
17583       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
17584       /* Tag_ABI_align8_preserved.  */
17585       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
17586       /* Tag_ABI_enum_size.  */
17587       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
17588                    flag_short_enums ? 1 : 2);
17589
17590       /* Tag_ABI_optimization_goals.  */
17591       if (optimize_size)
17592         val = 4;
17593       else if (optimize >= 2)
17594         val = 2;
17595       else if (optimize)
17596         val = 1;
17597       else
17598         val = 6;
17599       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
17600     }
17601   default_file_start();
17602 }
17603
17604 static void
17605 arm_file_end (void)
17606 {
17607   int regno;
17608
17609   if (NEED_INDICATE_EXEC_STACK)
17610     /* Add .note.GNU-stack.  */
17611     file_end_indicate_exec_stack ();
17612
17613   if (! thumb_call_reg_needed)
17614     return;
17615
17616   switch_to_section (text_section);
17617   asm_fprintf (asm_out_file, "\t.code 16\n");
17618   ASM_OUTPUT_ALIGN (asm_out_file, 1);
17619
17620   for (regno = 0; regno < LR_REGNUM; regno++)
17621     {
17622       rtx label = thumb_call_via_label[regno];
17623
17624       if (label != 0)
17625         {
17626           targetm.asm_out.internal_label (asm_out_file, "L",
17627                                           CODE_LABEL_NUMBER (label));
17628           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
17629         }
17630     }
17631 }
17632
17633 rtx aof_pic_label;
17634
17635 #ifdef AOF_ASSEMBLER
17636 /* Special functions only needed when producing AOF syntax assembler.  */
17637
17638 struct pic_chain
17639 {
17640   struct pic_chain * next;
17641   const char * symname;
17642 };
17643
17644 static struct pic_chain * aof_pic_chain = NULL;
17645
17646 rtx
17647 aof_pic_entry (rtx x)
17648 {
17649   struct pic_chain ** chainp;
17650   int offset;
17651
17652   if (aof_pic_label == NULL_RTX)
17653     {
17654       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
17655     }
17656
17657   for (offset = 0, chainp = &aof_pic_chain; *chainp;
17658        offset += 4, chainp = &(*chainp)->next)
17659     if ((*chainp)->symname == XSTR (x, 0))
17660       return plus_constant (aof_pic_label, offset);
17661
17662   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
17663   (*chainp)->next = NULL;
17664   (*chainp)->symname = XSTR (x, 0);
17665   return plus_constant (aof_pic_label, offset);
17666 }
17667
17668 void
17669 aof_dump_pic_table (FILE *f)
17670 {
17671   struct pic_chain * chain;
17672
17673   if (aof_pic_chain == NULL)
17674     return;
17675
17676   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
17677                PIC_OFFSET_TABLE_REGNUM,
17678                PIC_OFFSET_TABLE_REGNUM);
17679   fputs ("|x$adcons|\n", f);
17680
17681   for (chain = aof_pic_chain; chain; chain = chain->next)
17682     {
17683       fputs ("\tDCD\t", f);
17684       assemble_name (f, chain->symname);
17685       fputs ("\n", f);
17686     }
17687 }
17688
17689 int arm_text_section_count = 1;
17690
17691 /* A get_unnamed_section callback for switching to the text section.  */
17692
17693 static void
17694 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
17695 {
17696   fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
17697            arm_text_section_count++);
17698   if (flag_pic)
17699     fprintf (asm_out_file, ", PIC, REENTRANT");
17700   fprintf (asm_out_file, "\n");
17701 }
17702
17703 static int arm_data_section_count = 1;
17704
17705 /* A get_unnamed_section callback for switching to the data section.  */
17706
17707 static void
17708 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
17709 {
17710   fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
17711            arm_data_section_count++);
17712 }
17713
17714 /* Implement TARGET_ASM_INIT_SECTIONS.
17715
17716    AOF Assembler syntax is a nightmare when it comes to areas, since once
17717    we change from one area to another, we can't go back again.  Instead,
17718    we must create a new area with the same attributes and add the new output
17719    to that.  Unfortunately, there is nothing we can do here to guarantee that
17720    two areas with the same attributes will be linked adjacently in the
17721    resulting executable, so we have to be careful not to do pc-relative
17722    addressing across such boundaries.  */
17723
17724 static void
17725 aof_asm_init_sections (void)
17726 {
17727   text_section = get_unnamed_section (SECTION_CODE,
17728                                       aof_output_text_section_asm_op, NULL);
17729   data_section = get_unnamed_section (SECTION_WRITE,
17730                                       aof_output_data_section_asm_op, NULL);
17731   readonly_data_section = text_section;
17732 }
17733
17734 void
17735 zero_init_section (void)
17736 {
17737   static int zero_init_count = 1;
17738
17739   fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
17740   in_section = NULL;
17741 }
17742
17743 /* The AOF assembler is religiously strict about declarations of
17744    imported and exported symbols, so that it is impossible to declare
17745    a function as imported near the beginning of the file, and then to
17746    export it later on.  It is, however, possible to delay the decision
17747    until all the functions in the file have been compiled.  To get
17748    around this, we maintain a list of the imports and exports, and
17749    delete from it any that are subsequently defined.  At the end of
17750    compilation we spit the remainder of the list out before the END
17751    directive.  */
17752
17753 struct import
17754 {
17755   struct import * next;
17756   const char * name;
17757 };
17758
17759 static struct import * imports_list = NULL;
17760
17761 void
17762 aof_add_import (const char *name)
17763 {
17764   struct import * new;
17765
17766   for (new = imports_list; new; new = new->next)
17767     if (new->name == name)
17768       return;
17769
17770   new = (struct import *) xmalloc (sizeof (struct import));
17771   new->next = imports_list;
17772   imports_list = new;
17773   new->name = name;
17774 }
17775
17776 void
17777 aof_delete_import (const char *name)
17778 {
17779   struct import ** old;
17780
17781   for (old = &imports_list; *old; old = & (*old)->next)
17782     {
17783       if ((*old)->name == name)
17784         {
17785           *old = (*old)->next;
17786           return;
17787         }
17788     }
17789 }
17790
17791 int arm_main_function = 0;
17792
17793 static void
17794 aof_dump_imports (FILE *f)
17795 {
17796   /* The AOF assembler needs this to cause the startup code to be extracted
17797      from the library.  Brining in __main causes the whole thing to work
17798      automagically.  */
17799   if (arm_main_function)
17800     {
17801       switch_to_section (text_section);
17802       fputs ("\tIMPORT __main\n", f);
17803       fputs ("\tDCD __main\n", f);
17804     }
17805
17806   /* Now dump the remaining imports.  */
17807   while (imports_list)
17808     {
17809       fprintf (f, "\tIMPORT\t");
17810       assemble_name (f, imports_list->name);
17811       fputc ('\n', f);
17812       imports_list = imports_list->next;
17813     }
17814 }
17815
17816 static void
17817 aof_globalize_label (FILE *stream, const char *name)
17818 {
17819   default_globalize_label (stream, name);
17820   if (! strcmp (name, "main"))
17821     arm_main_function = 1;
17822 }
17823
17824 static void
17825 aof_file_start (void)
17826 {
17827   fputs ("__r0\tRN\t0\n", asm_out_file);
17828   fputs ("__a1\tRN\t0\n", asm_out_file);
17829   fputs ("__a2\tRN\t1\n", asm_out_file);
17830   fputs ("__a3\tRN\t2\n", asm_out_file);
17831   fputs ("__a4\tRN\t3\n", asm_out_file);
17832   fputs ("__v1\tRN\t4\n", asm_out_file);
17833   fputs ("__v2\tRN\t5\n", asm_out_file);
17834   fputs ("__v3\tRN\t6\n", asm_out_file);
17835   fputs ("__v4\tRN\t7\n", asm_out_file);
17836   fputs ("__v5\tRN\t8\n", asm_out_file);
17837   fputs ("__v6\tRN\t9\n", asm_out_file);
17838   fputs ("__sl\tRN\t10\n", asm_out_file);
17839   fputs ("__fp\tRN\t11\n", asm_out_file);
17840   fputs ("__ip\tRN\t12\n", asm_out_file);
17841   fputs ("__sp\tRN\t13\n", asm_out_file);
17842   fputs ("__lr\tRN\t14\n", asm_out_file);
17843   fputs ("__pc\tRN\t15\n", asm_out_file);
17844   fputs ("__f0\tFN\t0\n", asm_out_file);
17845   fputs ("__f1\tFN\t1\n", asm_out_file);
17846   fputs ("__f2\tFN\t2\n", asm_out_file);
17847   fputs ("__f3\tFN\t3\n", asm_out_file);
17848   fputs ("__f4\tFN\t4\n", asm_out_file);
17849   fputs ("__f5\tFN\t5\n", asm_out_file);
17850   fputs ("__f6\tFN\t6\n", asm_out_file);
17851   fputs ("__f7\tFN\t7\n", asm_out_file);
17852   switch_to_section (text_section);
17853 }
17854
17855 static void
17856 aof_file_end (void)
17857 {
17858   if (flag_pic)
17859     aof_dump_pic_table (asm_out_file);
17860   arm_file_end ();
17861   aof_dump_imports (asm_out_file);
17862   fputs ("\tEND\n", asm_out_file);
17863 }
17864 #endif /* AOF_ASSEMBLER */
17865
17866 #ifndef ARM_PE
17867 /* Symbols in the text segment can be accessed without indirecting via the
17868    constant pool; it may take an extra binary operation, but this is still
17869    faster than indirecting via memory.  Don't do this when not optimizing,
17870    since we won't be calculating al of the offsets necessary to do this
17871    simplification.  */
17872
17873 static void
17874 arm_encode_section_info (tree decl, rtx rtl, int first)
17875 {
17876   /* This doesn't work with AOF syntax, since the string table may be in
17877      a different AREA.  */
17878 #ifndef AOF_ASSEMBLER
17879   if (optimize > 0 && TREE_CONSTANT (decl))
17880     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
17881 #endif
17882
17883   default_encode_section_info (decl, rtl, first);
17884 }
17885 #endif /* !ARM_PE */
17886
17887 static void
17888 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
17889 {
17890   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
17891       && !strcmp (prefix, "L"))
17892     {
17893       arm_ccfsm_state = 0;
17894       arm_target_insn = NULL;
17895     }
17896   default_internal_label (stream, prefix, labelno);
17897 }
17898
17899 /* Output code to add DELTA to the first argument, and then jump
17900    to FUNCTION.  Used for C++ multiple inheritance.  */
17901 static void
17902 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
17903                      HOST_WIDE_INT delta,
17904                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
17905                      tree function)
17906 {
17907   static int thunk_label = 0;
17908   char label[256];
17909   char labelpc[256];
17910   int mi_delta = delta;
17911   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
17912   int shift = 0;
17913   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
17914                     ? 1 : 0);
17915   if (mi_delta < 0)
17916     mi_delta = - mi_delta;
17917   /* When generating 16-bit thumb code, thunks are entered in arm mode.  */
17918   if (TARGET_THUMB1)
17919     {
17920       int labelno = thunk_label++;
17921       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
17922       fputs ("\tldr\tr12, ", file);
17923       assemble_name (file, label);
17924       fputc ('\n', file);
17925       if (flag_pic)
17926         {
17927           /* If we are generating PIC, the ldr instruction below loads
17928              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
17929              the address of the add + 8, so we have:
17930
17931              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
17932                  = target + 1.
17933
17934              Note that we have "+ 1" because some versions of GNU ld
17935              don't set the low bit of the result for R_ARM_REL32
17936              relocations against thumb function symbols.  */
17937           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
17938           assemble_name (file, labelpc);
17939           fputs (":\n", file);
17940           fputs ("\tadd\tr12, pc, r12\n", file);
17941         }
17942     }
17943   /* TODO: Use movw/movt for large constants when available.  */
17944   while (mi_delta != 0)
17945     {
17946       if ((mi_delta & (3 << shift)) == 0)
17947         shift += 2;
17948       else
17949         {
17950           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
17951                        mi_op, this_regno, this_regno,
17952                        mi_delta & (0xff << shift));
17953           mi_delta &= ~(0xff << shift);
17954           shift += 8;
17955         }
17956     }
17957   if (TARGET_THUMB1)
17958     {
17959       fprintf (file, "\tbx\tr12\n");
17960       ASM_OUTPUT_ALIGN (file, 2);
17961       assemble_name (file, label);
17962       fputs (":\n", file);
17963       if (flag_pic)
17964         {
17965           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
17966           rtx tem = XEXP (DECL_RTL (function), 0);
17967           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
17968           tem = gen_rtx_MINUS (GET_MODE (tem),
17969                                tem,
17970                                gen_rtx_SYMBOL_REF (Pmode,
17971                                                    ggc_strdup (labelpc)));
17972           assemble_integer (tem, 4, BITS_PER_WORD, 1);
17973         }
17974       else
17975         /* Output ".word .LTHUNKn".  */
17976         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
17977     }
17978   else
17979     {
17980       fputs ("\tb\t", file);
17981       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
17982       if (NEED_PLT_RELOC)
17983         fputs ("(PLT)", file);
17984       fputc ('\n', file);
17985     }
17986 }
17987
17988 int
17989 arm_emit_vector_const (FILE *file, rtx x)
17990 {
17991   int i;
17992   const char * pattern;
17993
17994   gcc_assert (GET_CODE (x) == CONST_VECTOR);
17995
17996   switch (GET_MODE (x))
17997     {
17998     case V2SImode: pattern = "%08x"; break;
17999     case V4HImode: pattern = "%04x"; break;
18000     case V8QImode: pattern = "%02x"; break;
18001     default:       gcc_unreachable ();
18002     }
18003
18004   fprintf (file, "0x");
18005   for (i = CONST_VECTOR_NUNITS (x); i--;)
18006     {
18007       rtx element;
18008
18009       element = CONST_VECTOR_ELT (x, i);
18010       fprintf (file, pattern, INTVAL (element));
18011     }
18012
18013   return 1;
18014 }
18015
18016 const char *
18017 arm_output_load_gr (rtx *operands)
18018 {
18019   rtx reg;
18020   rtx offset;
18021   rtx wcgr;
18022   rtx sum;
18023
18024   if (GET_CODE (operands [1]) != MEM
18025       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
18026       || GET_CODE (reg = XEXP (sum, 0)) != REG
18027       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
18028       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
18029     return "wldrw%?\t%0, %1";
18030
18031   /* Fix up an out-of-range load of a GR register.  */
18032   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
18033   wcgr = operands[0];
18034   operands[0] = reg;
18035   output_asm_insn ("ldr%?\t%0, %1", operands);
18036
18037   operands[0] = wcgr;
18038   operands[1] = reg;
18039   output_asm_insn ("tmcr%?\t%0, %1", operands);
18040   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
18041
18042   return "";
18043 }
18044
18045 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
18046
18047    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
18048    named arg and all anonymous args onto the stack.
18049    XXX I know the prologue shouldn't be pushing registers, but it is faster
18050    that way.  */
18051
18052 static void
18053 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
18054                             enum machine_mode mode ATTRIBUTE_UNUSED,
18055                             tree type ATTRIBUTE_UNUSED,
18056                             int *pretend_size,
18057                             int second_time ATTRIBUTE_UNUSED)
18058 {
18059   cfun->machine->uses_anonymous_args = 1;
18060   if (cum->nregs < NUM_ARG_REGS)
18061     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
18062 }
18063
18064 /* Return nonzero if the CONSUMER instruction (a store) does not need
18065    PRODUCER's value to calculate the address.  */
18066
18067 int
18068 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
18069 {
18070   rtx value = PATTERN (producer);
18071   rtx addr = PATTERN (consumer);
18072
18073   if (GET_CODE (value) == COND_EXEC)
18074     value = COND_EXEC_CODE (value);
18075   if (GET_CODE (value) == PARALLEL)
18076     value = XVECEXP (value, 0, 0);
18077   value = XEXP (value, 0);
18078   if (GET_CODE (addr) == COND_EXEC)
18079     addr = COND_EXEC_CODE (addr);
18080   if (GET_CODE (addr) == PARALLEL)
18081     addr = XVECEXP (addr, 0, 0);
18082   addr = XEXP (addr, 0);
18083
18084   return !reg_overlap_mentioned_p (value, addr);
18085 }
18086
18087 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
18088    have an early register shift value or amount dependency on the
18089    result of PRODUCER.  */
18090
18091 int
18092 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
18093 {
18094   rtx value = PATTERN (producer);
18095   rtx op = PATTERN (consumer);
18096   rtx early_op;
18097
18098   if (GET_CODE (value) == COND_EXEC)
18099     value = COND_EXEC_CODE (value);
18100   if (GET_CODE (value) == PARALLEL)
18101     value = XVECEXP (value, 0, 0);
18102   value = XEXP (value, 0);
18103   if (GET_CODE (op) == COND_EXEC)
18104     op = COND_EXEC_CODE (op);
18105   if (GET_CODE (op) == PARALLEL)
18106     op = XVECEXP (op, 0, 0);
18107   op = XEXP (op, 1);
18108
18109   early_op = XEXP (op, 0);
18110   /* This is either an actual independent shift, or a shift applied to
18111      the first operand of another operation.  We want the whole shift
18112      operation.  */
18113   if (GET_CODE (early_op) == REG)
18114     early_op = op;
18115
18116   return !reg_overlap_mentioned_p (value, early_op);
18117 }
18118
18119 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
18120    have an early register shift value dependency on the result of
18121    PRODUCER.  */
18122
18123 int
18124 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
18125 {
18126   rtx value = PATTERN (producer);
18127   rtx op = PATTERN (consumer);
18128   rtx early_op;
18129
18130   if (GET_CODE (value) == COND_EXEC)
18131     value = COND_EXEC_CODE (value);
18132   if (GET_CODE (value) == PARALLEL)
18133     value = XVECEXP (value, 0, 0);
18134   value = XEXP (value, 0);
18135   if (GET_CODE (op) == COND_EXEC)
18136     op = COND_EXEC_CODE (op);
18137   if (GET_CODE (op) == PARALLEL)
18138     op = XVECEXP (op, 0, 0);
18139   op = XEXP (op, 1);
18140
18141   early_op = XEXP (op, 0);
18142
18143   /* This is either an actual independent shift, or a shift applied to
18144      the first operand of another operation.  We want the value being
18145      shifted, in either case.  */
18146   if (GET_CODE (early_op) != REG)
18147     early_op = XEXP (early_op, 0);
18148
18149   return !reg_overlap_mentioned_p (value, early_op);
18150 }
18151
18152 /* Return nonzero if the CONSUMER (a mul or mac op) does not
18153    have an early register mult dependency on the result of
18154    PRODUCER.  */
18155
18156 int
18157 arm_no_early_mul_dep (rtx producer, rtx consumer)
18158 {
18159   rtx value = PATTERN (producer);
18160   rtx op = PATTERN (consumer);
18161
18162   if (GET_CODE (value) == COND_EXEC)
18163     value = COND_EXEC_CODE (value);
18164   if (GET_CODE (value) == PARALLEL)
18165     value = XVECEXP (value, 0, 0);
18166   value = XEXP (value, 0);
18167   if (GET_CODE (op) == COND_EXEC)
18168     op = COND_EXEC_CODE (op);
18169   if (GET_CODE (op) == PARALLEL)
18170     op = XVECEXP (op, 0, 0);
18171   op = XEXP (op, 1);
18172
18173   return (GET_CODE (op) == PLUS
18174           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
18175 }
18176
18177 /* We can't rely on the caller doing the proper promotion when
18178    using APCS or ATPCS.  */
18179
18180 static bool
18181 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
18182 {
18183     return !TARGET_AAPCS_BASED;
18184 }
18185
18186
18187 /* AAPCS based ABIs use short enums by default.  */
18188
18189 static bool
18190 arm_default_short_enums (void)
18191 {
18192   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
18193 }
18194
18195
18196 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
18197
18198 static bool
18199 arm_align_anon_bitfield (void)
18200 {
18201   return TARGET_AAPCS_BASED;
18202 }
18203
18204
18205 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
18206
18207 static tree
18208 arm_cxx_guard_type (void)
18209 {
18210   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
18211 }
18212
18213 /* Return non-zero if the consumer (a multiply-accumulate instruction)
18214    has an accumulator dependency on the result of the producer (a
18215    multiplication instruction) and no other dependency on that result.  */
18216 int
18217 arm_mac_accumulator_is_mul_result (rtx producer, rtx consumer)
18218 {
18219   rtx mul = PATTERN (producer);
18220   rtx mac = PATTERN (consumer);
18221   rtx mul_result;
18222   rtx mac_op0, mac_op1, mac_acc;
18223
18224   if (GET_CODE (mul) == COND_EXEC)
18225     mul = COND_EXEC_CODE (mul);
18226   if (GET_CODE (mac) == COND_EXEC)
18227     mac = COND_EXEC_CODE (mac);
18228
18229   /* Check that mul is of the form (set (...) (mult ...))
18230      and mla is of the form (set (...) (plus (mult ...) (...))).  */
18231   if ((GET_CODE (mul) != SET || GET_CODE (XEXP (mul, 1)) != MULT)
18232       || (GET_CODE (mac) != SET || GET_CODE (XEXP (mac, 1)) != PLUS
18233           || GET_CODE (XEXP (XEXP (mac, 1), 0)) != MULT))
18234     return 0;
18235
18236   mul_result = XEXP (mul, 0);
18237   mac_op0 = XEXP (XEXP (XEXP (mac, 1), 0), 0);
18238   mac_op1 = XEXP (XEXP (XEXP (mac, 1), 0), 1);
18239   mac_acc = XEXP (XEXP (mac, 1), 1);
18240
18241   return (reg_overlap_mentioned_p (mul_result, mac_acc)
18242           && !reg_overlap_mentioned_p (mul_result, mac_op0)
18243           && !reg_overlap_mentioned_p (mul_result, mac_op1));
18244 }
18245
18246
18247 /* The EABI says test the least significant bit of a guard variable.  */
18248
18249 static bool
18250 arm_cxx_guard_mask_bit (void)
18251 {
18252   return TARGET_AAPCS_BASED;
18253 }
18254
18255
18256 /* The EABI specifies that all array cookies are 8 bytes long.  */
18257
18258 static tree
18259 arm_get_cookie_size (tree type)
18260 {
18261   tree size;
18262
18263   if (!TARGET_AAPCS_BASED)
18264     return default_cxx_get_cookie_size (type);
18265
18266   size = build_int_cst (sizetype, 8);
18267   return size;
18268 }
18269
18270
18271 /* The EABI says that array cookies should also contain the element size.  */
18272
18273 static bool
18274 arm_cookie_has_size (void)
18275 {
18276   return TARGET_AAPCS_BASED;
18277 }
18278
18279
18280 /* The EABI says constructors and destructors should return a pointer to
18281    the object constructed/destroyed.  */
18282
18283 static bool
18284 arm_cxx_cdtor_returns_this (void)
18285 {
18286   return TARGET_AAPCS_BASED;
18287 }
18288
18289 /* The EABI says that an inline function may never be the key
18290    method.  */
18291
18292 static bool
18293 arm_cxx_key_method_may_be_inline (void)
18294 {
18295   return !TARGET_AAPCS_BASED;
18296 }
18297
18298 static void
18299 arm_cxx_determine_class_data_visibility (tree decl)
18300 {
18301   if (!TARGET_AAPCS_BASED)
18302     return;
18303
18304   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
18305      is exported.  However, on systems without dynamic vague linkage,
18306      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
18307   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
18308     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
18309   else
18310     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
18311   DECL_VISIBILITY_SPECIFIED (decl) = 1;
18312 }
18313
18314 static bool
18315 arm_cxx_class_data_always_comdat (void)
18316 {
18317   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
18318      vague linkage if the class has no key function.  */
18319   return !TARGET_AAPCS_BASED;
18320 }
18321
18322
18323 /* The EABI says __aeabi_atexit should be used to register static
18324    destructors.  */
18325
18326 static bool
18327 arm_cxx_use_aeabi_atexit (void)
18328 {
18329   return TARGET_AAPCS_BASED;
18330 }
18331
18332
18333 void
18334 arm_set_return_address (rtx source, rtx scratch)
18335 {
18336   arm_stack_offsets *offsets;
18337   HOST_WIDE_INT delta;
18338   rtx addr;
18339   unsigned long saved_regs;
18340
18341   saved_regs = arm_compute_save_reg_mask ();
18342
18343   if ((saved_regs & (1 << LR_REGNUM)) == 0)
18344     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18345   else
18346     {
18347       if (frame_pointer_needed)
18348         addr = plus_constant(hard_frame_pointer_rtx, -4);
18349       else
18350         {
18351           /* LR will be the first saved register.  */
18352           offsets = arm_get_frame_offsets ();
18353           delta = offsets->outgoing_args - (offsets->frame + 4);
18354
18355
18356           if (delta >= 4096)
18357             {
18358               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
18359                                      GEN_INT (delta & ~4095)));
18360               addr = scratch;
18361               delta &= 4095;
18362             }
18363           else
18364             addr = stack_pointer_rtx;
18365
18366           addr = plus_constant (addr, delta);
18367         }
18368       emit_move_insn (gen_frame_mem (Pmode, addr), source);
18369     }
18370 }
18371
18372
18373 void
18374 thumb_set_return_address (rtx source, rtx scratch)
18375 {
18376   arm_stack_offsets *offsets;
18377   HOST_WIDE_INT delta;
18378   HOST_WIDE_INT limit;
18379   int reg;
18380   rtx addr;
18381   unsigned long mask;
18382
18383   emit_insn (gen_rtx_USE (VOIDmode, source));
18384
18385   mask = thumb1_compute_save_reg_mask ();
18386   if (mask & (1 << LR_REGNUM))
18387     {
18388       offsets = arm_get_frame_offsets ();
18389
18390       limit = 1024;
18391       /* Find the saved regs.  */
18392       if (frame_pointer_needed)
18393         {
18394           delta = offsets->soft_frame - offsets->saved_args;
18395           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
18396           if (TARGET_THUMB1)
18397             limit = 128;
18398         }
18399       else
18400         {
18401           delta = offsets->outgoing_args - offsets->saved_args;
18402           reg = SP_REGNUM;
18403         }
18404       /* Allow for the stack frame.  */
18405       if (TARGET_THUMB1 && TARGET_BACKTRACE)
18406         delta -= 16;
18407       /* The link register is always the first saved register.  */
18408       delta -= 4;
18409
18410       /* Construct the address.  */
18411       addr = gen_rtx_REG (SImode, reg);
18412       if (delta > limit)
18413         {
18414           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
18415           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
18416           addr = scratch;
18417         }
18418       else
18419         addr = plus_constant (addr, delta);
18420
18421       emit_move_insn (gen_frame_mem (Pmode, addr), source);
18422     }
18423   else
18424     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
18425 }
18426
18427 /* Implements target hook vector_mode_supported_p.  */
18428 bool
18429 arm_vector_mode_supported_p (enum machine_mode mode)
18430 {
18431   /* Neon also supports V2SImode, etc. listed in the clause below.  */
18432   if (TARGET_NEON && (mode == V2SFmode || mode == V4SImode || mode == V8HImode
18433       || mode == V16QImode || mode == V4SFmode || mode == V2DImode))
18434     return true;
18435
18436   if ((mode == V2SImode)
18437       || (mode == V4HImode)
18438       || (mode == V8QImode))
18439     return true;
18440
18441   return false;
18442 }
18443
18444 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
18445    ARM insns and therefore guarantee that the shift count is modulo 256.
18446    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
18447    guarantee no particular behavior for out-of-range counts.  */
18448
18449 static unsigned HOST_WIDE_INT
18450 arm_shift_truncation_mask (enum machine_mode mode)
18451 {
18452   return mode == SImode ? 255 : 0;
18453 }
18454
18455
18456 /* Map internal gcc register numbers to DWARF2 register numbers.  */
18457
18458 unsigned int
18459 arm_dbx_register_number (unsigned int regno)
18460 {
18461   if (regno < 16)
18462     return regno;
18463
18464   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
18465      compatibility.  The EABI defines them as registers 96-103.  */
18466   if (IS_FPA_REGNUM (regno))
18467     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
18468
18469   /* FIXME: VFPv3 register numbering.  */
18470   if (IS_VFP_REGNUM (regno))
18471     return 64 + regno - FIRST_VFP_REGNUM;
18472
18473   if (IS_IWMMXT_GR_REGNUM (regno))
18474     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
18475
18476   if (IS_IWMMXT_REGNUM (regno))
18477     return 112 + regno - FIRST_IWMMXT_REGNUM;
18478
18479   gcc_unreachable ();
18480 }
18481
18482
18483 #ifdef TARGET_UNWIND_INFO
18484 /* Emit unwind directives for a store-multiple instruction or stack pointer
18485    push during alignment.
18486    These should only ever be generated by the function prologue code, so
18487    expect them to have a particular form.  */
18488
18489 static void
18490 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
18491 {
18492   int i;
18493   HOST_WIDE_INT offset;
18494   HOST_WIDE_INT nregs;
18495   int reg_size;
18496   unsigned reg;
18497   unsigned lastreg;
18498   rtx e;
18499
18500   e = XVECEXP (p, 0, 0);
18501   if (GET_CODE (e) != SET)
18502     abort ();
18503
18504   /* First insn will adjust the stack pointer.  */
18505   if (GET_CODE (e) != SET
18506       || GET_CODE (XEXP (e, 0)) != REG
18507       || REGNO (XEXP (e, 0)) != SP_REGNUM
18508       || GET_CODE (XEXP (e, 1)) != PLUS)
18509     abort ();
18510
18511   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
18512   nregs = XVECLEN (p, 0) - 1;
18513
18514   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
18515   if (reg < 16)
18516     {
18517       /* The function prologue may also push pc, but not annotate it as it is
18518          never restored.  We turn this into a stack pointer adjustment.  */
18519       if (nregs * 4 == offset - 4)
18520         {
18521           fprintf (asm_out_file, "\t.pad #4\n");
18522           offset -= 4;
18523         }
18524       reg_size = 4;
18525       fprintf (asm_out_file, "\t.save {");
18526     }
18527   else if (IS_VFP_REGNUM (reg))
18528     {
18529       reg_size = 8;
18530       fprintf (asm_out_file, "\t.vsave {");
18531     }
18532   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
18533     {
18534       /* FPA registers are done differently.  */
18535       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
18536       return;
18537     }
18538   else
18539     /* Unknown register type.  */
18540     abort ();
18541
18542   /* If the stack increment doesn't match the size of the saved registers,
18543      something has gone horribly wrong.  */
18544   if (offset != nregs * reg_size)
18545     abort ();
18546
18547   offset = 0;
18548   lastreg = 0;
18549   /* The remaining insns will describe the stores.  */
18550   for (i = 1; i <= nregs; i++)
18551     {
18552       /* Expect (set (mem <addr>) (reg)).
18553          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
18554       e = XVECEXP (p, 0, i);
18555       if (GET_CODE (e) != SET
18556           || GET_CODE (XEXP (e, 0)) != MEM
18557           || GET_CODE (XEXP (e, 1)) != REG)
18558         abort ();
18559
18560       reg = REGNO (XEXP (e, 1));
18561       if (reg < lastreg)
18562         abort ();
18563
18564       if (i != 1)
18565         fprintf (asm_out_file, ", ");
18566       /* We can't use %r for vfp because we need to use the
18567          double precision register names.  */
18568       if (IS_VFP_REGNUM (reg))
18569         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
18570       else
18571         asm_fprintf (asm_out_file, "%r", reg);
18572
18573 #ifdef ENABLE_CHECKING
18574       /* Check that the addresses are consecutive.  */
18575       e = XEXP (XEXP (e, 0), 0);
18576       if (GET_CODE (e) == PLUS)
18577         {
18578           offset += reg_size;
18579           if (GET_CODE (XEXP (e, 0)) != REG
18580               || REGNO (XEXP (e, 0)) != SP_REGNUM
18581               || GET_CODE (XEXP (e, 1)) != CONST_INT
18582               || offset != INTVAL (XEXP (e, 1)))
18583             abort ();
18584         }
18585       else if (i != 1
18586                || GET_CODE (e) != REG
18587                || REGNO (e) != SP_REGNUM)
18588         abort ();
18589 #endif
18590     }
18591   fprintf (asm_out_file, "}\n");
18592 }
18593
18594 /*  Emit unwind directives for a SET.  */
18595
18596 static void
18597 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
18598 {
18599   rtx e0;
18600   rtx e1;
18601   unsigned reg;
18602
18603   e0 = XEXP (p, 0);
18604   e1 = XEXP (p, 1);
18605   switch (GET_CODE (e0))
18606     {
18607     case MEM:
18608       /* Pushing a single register.  */
18609       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
18610           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
18611           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
18612         abort ();
18613
18614       asm_fprintf (asm_out_file, "\t.save ");
18615       if (IS_VFP_REGNUM (REGNO (e1)))
18616         asm_fprintf(asm_out_file, "{d%d}\n",
18617                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
18618       else
18619         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
18620       break;
18621
18622     case REG:
18623       if (REGNO (e0) == SP_REGNUM)
18624         {
18625           /* A stack increment.  */
18626           if (GET_CODE (e1) != PLUS
18627               || GET_CODE (XEXP (e1, 0)) != REG
18628               || REGNO (XEXP (e1, 0)) != SP_REGNUM
18629               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
18630             abort ();
18631
18632           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
18633                        -INTVAL (XEXP (e1, 1)));
18634         }
18635       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
18636         {
18637           HOST_WIDE_INT offset;
18638
18639           if (GET_CODE (e1) == PLUS)
18640             {
18641               if (GET_CODE (XEXP (e1, 0)) != REG
18642                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
18643                 abort ();
18644               reg = REGNO (XEXP (e1, 0));
18645               offset = INTVAL (XEXP (e1, 1));
18646               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
18647                            HARD_FRAME_POINTER_REGNUM, reg,
18648                            INTVAL (XEXP (e1, 1)));
18649             }
18650           else if (GET_CODE (e1) == REG)
18651             {
18652               reg = REGNO (e1);
18653               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
18654                            HARD_FRAME_POINTER_REGNUM, reg);
18655             }
18656           else
18657             abort ();
18658         }
18659       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
18660         {
18661           /* Move from sp to reg.  */
18662           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
18663         }
18664      else if (GET_CODE (e1) == PLUS
18665               && GET_CODE (XEXP (e1, 0)) == REG
18666               && REGNO (XEXP (e1, 0)) == SP_REGNUM
18667               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
18668         {
18669           /* Set reg to offset from sp.  */
18670           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
18671                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
18672         }
18673       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
18674         {
18675           /* Stack pointer save before alignment.  */
18676           reg = REGNO (e0);
18677           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
18678                        reg + 0x90, reg);
18679         }
18680       else
18681         abort ();
18682       break;
18683
18684     default:
18685       abort ();
18686     }
18687 }
18688
18689
18690 /* Emit unwind directives for the given insn.  */
18691
18692 static void
18693 arm_unwind_emit (FILE * asm_out_file, rtx insn)
18694 {
18695   rtx pat;
18696
18697   if (!ARM_EABI_UNWIND_TABLES)
18698     return;
18699
18700   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
18701     return;
18702
18703   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
18704   if (pat)
18705     pat = XEXP (pat, 0);
18706   else
18707     pat = PATTERN (insn);
18708
18709   switch (GET_CODE (pat))
18710     {
18711     case SET:
18712       arm_unwind_emit_set (asm_out_file, pat);
18713       break;
18714
18715     case SEQUENCE:
18716       /* Store multiple.  */
18717       arm_unwind_emit_sequence (asm_out_file, pat);
18718       break;
18719
18720     default:
18721       abort();
18722     }
18723 }
18724
18725
18726 /* Output a reference from a function exception table to the type_info
18727    object X.  The EABI specifies that the symbol should be relocated by
18728    an R_ARM_TARGET2 relocation.  */
18729
18730 static bool
18731 arm_output_ttype (rtx x)
18732 {
18733   fputs ("\t.word\t", asm_out_file);
18734   output_addr_const (asm_out_file, x);
18735   /* Use special relocations for symbol references.  */
18736   if (GET_CODE (x) != CONST_INT)
18737     fputs ("(TARGET2)", asm_out_file);
18738   fputc ('\n', asm_out_file);
18739
18740   return TRUE;
18741 }
18742 #endif /* TARGET_UNWIND_INFO */
18743
18744
18745 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
18746    stack alignment.  */
18747
18748 static void
18749 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
18750 {
18751   rtx unspec = SET_SRC (pattern);
18752   gcc_assert (GET_CODE (unspec) == UNSPEC);
18753
18754   switch (index)
18755     {
18756     case UNSPEC_STACK_ALIGN:
18757       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
18758          put anything on the stack, so hopefully it won't matter.
18759          CFA = SP will be correct after alignment.  */
18760       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
18761                               SET_DEST (pattern));
18762       break;
18763     default:
18764       gcc_unreachable ();
18765     }
18766 }
18767
18768
18769 /* Output unwind directives for the start/end of a function.  */
18770
18771 void
18772 arm_output_fn_unwind (FILE * f, bool prologue)
18773 {
18774   if (!ARM_EABI_UNWIND_TABLES)
18775     return;
18776
18777   if (prologue)
18778     fputs ("\t.fnstart\n", f);
18779   else
18780     fputs ("\t.fnend\n", f);
18781 }
18782
18783 static bool
18784 arm_emit_tls_decoration (FILE *fp, rtx x)
18785 {
18786   enum tls_reloc reloc;
18787   rtx val;
18788
18789   val = XVECEXP (x, 0, 0);
18790   reloc = INTVAL (XVECEXP (x, 0, 1));
18791
18792   output_addr_const (fp, val);
18793
18794   switch (reloc)
18795     {
18796     case TLS_GD32:
18797       fputs ("(tlsgd)", fp);
18798       break;
18799     case TLS_LDM32:
18800       fputs ("(tlsldm)", fp);
18801       break;
18802     case TLS_LDO32:
18803       fputs ("(tlsldo)", fp);
18804       break;
18805     case TLS_IE32:
18806       fputs ("(gottpoff)", fp);
18807       break;
18808     case TLS_LE32:
18809       fputs ("(tpoff)", fp);
18810       break;
18811     default:
18812       gcc_unreachable ();
18813     }
18814
18815   switch (reloc)
18816     {
18817     case TLS_GD32:
18818     case TLS_LDM32:
18819     case TLS_IE32:
18820       fputs (" + (. - ", fp);
18821       output_addr_const (fp, XVECEXP (x, 0, 2));
18822       fputs (" - ", fp);
18823       output_addr_const (fp, XVECEXP (x, 0, 3));
18824       fputc (')', fp);
18825       break;
18826     default:
18827       break;
18828     }
18829
18830   return TRUE;
18831 }
18832
18833 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
18834
18835 static void
18836 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
18837 {
18838   gcc_assert (size == 4);
18839   fputs ("\t.word\t", file);
18840   output_addr_const (file, x);
18841   fputs ("(tlsldo)", file);
18842 }
18843
18844 bool
18845 arm_output_addr_const_extra (FILE *fp, rtx x)
18846 {
18847   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
18848     return arm_emit_tls_decoration (fp, x);
18849   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
18850     {
18851       char label[256];
18852       int labelno = INTVAL (XVECEXP (x, 0, 0));
18853
18854       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
18855       assemble_name_raw (fp, label);
18856
18857       return TRUE;
18858     }
18859   else if (GET_CODE (x) == CONST_VECTOR)
18860     return arm_emit_vector_const (fp, x);
18861
18862   return FALSE;
18863 }
18864
18865 /* Output assembly for a shift instruction.
18866    SET_FLAGS determines how the instruction modifies the condition codes.
18867    0 - Do not set condition codes.
18868    1 - Set condition codes.
18869    2 - Use smallest instruction.  */
18870 const char *
18871 arm_output_shift(rtx * operands, int set_flags)
18872 {
18873   char pattern[100];
18874   static const char flag_chars[3] = {'?', '.', '!'};
18875   const char *shift;
18876   HOST_WIDE_INT val;
18877   char c;
18878   
18879   c = flag_chars[set_flags];
18880   if (TARGET_UNIFIED_ASM)
18881     {
18882       shift = shift_op(operands[3], &val);
18883       if (shift)
18884         {
18885           if (val != -1)
18886             operands[2] = GEN_INT(val);
18887           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
18888         }
18889       else
18890         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
18891     }
18892   else
18893     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
18894   output_asm_insn (pattern, operands);
18895   return "";
18896 }
18897
18898 /* Output a Thumb-2 casesi instruction.  */
18899 const char *
18900 thumb2_output_casesi (rtx *operands)
18901 {
18902   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
18903
18904   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
18905
18906   output_asm_insn ("cmp\t%0, %1", operands);
18907   output_asm_insn ("bhi\t%l3", operands);
18908   switch (GET_MODE(diff_vec))
18909     {
18910     case QImode:
18911       return "tbb\t[%|pc, %0]";
18912     case HImode:
18913       return "tbh\t[%|pc, %0, lsl #1]";
18914     case SImode:
18915       if (flag_pic)
18916         {
18917           output_asm_insn ("adr\t%4, %l2", operands);
18918           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
18919           output_asm_insn ("add\t%4, %4, %5", operands);
18920           return "bx\t%4";
18921         }
18922       else
18923         {
18924           output_asm_insn ("adr\t%4, %l2", operands);
18925           return "ldr\t%|pc, [%4, %0, lsl #2]";
18926         }
18927     default:
18928       gcc_unreachable ();
18929     }
18930 }
18931
18932 /* A table and a function to perform ARM-specific name mangling for
18933    NEON vector types in order to conform to the AAPCS (see "Procedure
18934    Call Standard for the ARM Architecture", Appendix A).  To qualify
18935    for emission with the mangled names defined in that document, a
18936    vector type must not only be of the correct mode but also be
18937    composed of NEON vector element types (e.g. __builtin_neon_qi).  */
18938 typedef struct
18939 {
18940   enum machine_mode mode;
18941   const char *element_type_name;
18942   const char *aapcs_name;
18943 } arm_mangle_map_entry;
18944
18945 static arm_mangle_map_entry arm_mangle_map[] = {
18946   /* 64-bit containerized types.  */
18947   { V8QImode,  "__builtin_neon_qi",     "15__simd64_int8_t" },
18948   { V8QImode,  "__builtin_neon_uqi",    "16__simd64_uint8_t" },
18949   { V4HImode,  "__builtin_neon_hi",     "16__simd64_int16_t" },
18950   { V4HImode,  "__builtin_neon_uhi",    "17__simd64_uint16_t" },
18951   { V2SImode,  "__builtin_neon_si",     "16__simd64_int32_t" },
18952   { V2SImode,  "__builtin_neon_usi",    "17__simd64_uint32_t" },
18953   { V2SFmode,  "__builtin_neon_sf",     "18__simd64_float32_t" },
18954   { V8QImode,  "__builtin_neon_poly8",  "16__simd64_poly8_t" },
18955   { V4HImode,  "__builtin_neon_poly16", "17__simd64_poly16_t" },
18956   /* 128-bit containerized types.  */
18957   { V16QImode, "__builtin_neon_qi",     "16__simd128_int8_t" },
18958   { V16QImode, "__builtin_neon_uqi",    "17__simd128_uint8_t" },
18959   { V8HImode,  "__builtin_neon_hi",     "17__simd128_int16_t" },
18960   { V8HImode,  "__builtin_neon_uhi",    "18__simd128_uint16_t" },
18961   { V4SImode,  "__builtin_neon_si",     "17__simd128_int32_t" },
18962   { V4SImode,  "__builtin_neon_usi",    "18__simd128_uint32_t" },
18963   { V4SFmode,  "__builtin_neon_sf",     "19__simd128_float32_t" },
18964   { V16QImode, "__builtin_neon_poly8",  "17__simd128_poly8_t" },
18965   { V8HImode,  "__builtin_neon_poly16", "18__simd128_poly16_t" },
18966   { VOIDmode, NULL, NULL }
18967 };
18968
18969 const char *
18970 arm_mangle_type (tree type)
18971 {
18972   arm_mangle_map_entry *pos = arm_mangle_map;
18973
18974   if (TREE_CODE (type) != VECTOR_TYPE)
18975     return NULL;
18976
18977   /* Check the mode of the vector type, and the name of the vector
18978      element type, against the table.  */
18979   while (pos->mode != VOIDmode)
18980     {
18981       tree elt_type = TREE_TYPE (type);
18982
18983       if (pos->mode == TYPE_MODE (type)
18984           && TREE_CODE (TYPE_NAME (elt_type)) == TYPE_DECL
18985           && !strcmp (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (elt_type))),
18986                       pos->element_type_name))
18987         return pos->aapcs_name;
18988
18989       pos++;
18990     }
18991
18992   /* Use the default mangling for unrecognized (possibly user-defined)
18993      vector types.  */
18994   return NULL;
18995 }
18996
18997 #include "gt-arm.h"