OSDN Git Service

c1f37bffa6c46c73cf754050af8dca828a2506c8
[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_tls_referenced_p
384
385 #ifdef HAVE_AS_TLS
386 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
387 #define TARGET_ASM_OUTPUT_DWARF_DTPREL arm_output_dwarf_dtprel
388 #endif
389
390 struct gcc_target targetm = TARGET_INITIALIZER;
391 \f
392 /* Obstack for minipool constant handling.  */
393 static struct obstack minipool_obstack;
394 static char *         minipool_startobj;
395
396 /* The maximum number of insns skipped which
397    will be conditionalised if possible.  */
398 static int max_insns_skipped = 5;
399
400 extern FILE * asm_out_file;
401
402 /* True if we are currently building a constant table.  */
403 int making_const_table;
404
405 /* Define the information needed to generate branch insns.  This is
406    stored from the compare operation.  */
407 rtx arm_compare_op0, arm_compare_op1;
408
409 /* The processor for which instructions should be scheduled.  */
410 enum processor_type arm_tune = arm_none;
411
412 /* The default processor used if not overridden by commandline.  */
413 static enum processor_type arm_default_cpu = arm_none;
414
415 /* Which floating point model to use.  */
416 enum arm_fp_model arm_fp_model;
417
418 /* Which floating point hardware is available.  */
419 enum fputype arm_fpu_arch;
420
421 /* Which floating point hardware to schedule for.  */
422 enum fputype arm_fpu_tune;
423
424 /* Whether to use floating point hardware.  */
425 enum float_abi_type arm_float_abi;
426
427 /* Which ABI to use.  */
428 enum arm_abi_type arm_abi;
429
430 /* Which thread pointer model to use.  */
431 enum arm_tp_type target_thread_pointer = TP_AUTO;
432
433 /* Used to parse -mstructure_size_boundary command line option.  */
434 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
435
436 /* Used for Thumb call_via trampolines.  */
437 rtx thumb_call_via_label[14];
438 static int thumb_call_reg_needed;
439
440 /* Bit values used to identify processor capabilities.  */
441 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
442 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
443 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
444 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
445 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
446 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
447 #define FL_THUMB      (1 << 6)        /* Thumb aware */
448 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
449 #define FL_STRONG     (1 << 8)        /* StrongARM */
450 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
451 #define FL_XSCALE     (1 << 10)       /* XScale */
452 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
453 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
454                                          media instructions.  */
455 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
456 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
457                                          Note: ARM6 & 7 derivatives only.  */
458 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
459 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
460 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
461                                          profile.  */
462 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
463 #define FL_VFPV3      (1 << 19)       /* Vector Floating Point V3.  */
464
465 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
466
467 #define FL_FOR_ARCH2    FL_NOTM
468 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
469 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
470 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
471 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
472 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
473 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
474 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
475 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
476 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
477 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
478 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
479 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
480 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
481 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
482 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
483 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
484 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
485 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
486 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
487
488 /* The bits in this mask specify which
489    instructions we are allowed to generate.  */
490 static unsigned long insn_flags = 0;
491
492 /* The bits in this mask specify which instruction scheduling options should
493    be used.  */
494 static unsigned long tune_flags = 0;
495
496 /* The following are used in the arm.md file as equivalents to bits
497    in the above two flag variables.  */
498
499 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
500 int arm_arch3m = 0;
501
502 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
503 int arm_arch4 = 0;
504
505 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
506 int arm_arch4t = 0;
507
508 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
509 int arm_arch5 = 0;
510
511 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
512 int arm_arch5e = 0;
513
514 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
515 int arm_arch6 = 0;
516
517 /* Nonzero if this chip supports the ARM 6K extensions.  */
518 int arm_arch6k = 0;
519
520 /* Nonzero if instructions not present in the 'M' profile can be used.  */
521 int arm_arch_notm = 0;
522
523 /* Nonzero if this chip can benefit from load scheduling.  */
524 int arm_ld_sched = 0;
525
526 /* Nonzero if this chip is a StrongARM.  */
527 int arm_tune_strongarm = 0;
528
529 /* Nonzero if this chip is a Cirrus variant.  */
530 int arm_arch_cirrus = 0;
531
532 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
533 int arm_arch_iwmmxt = 0;
534
535 /* Nonzero if this chip is an XScale.  */
536 int arm_arch_xscale = 0;
537
538 /* Nonzero if tuning for XScale  */
539 int arm_tune_xscale = 0;
540
541 /* Nonzero if we want to tune for stores that access the write-buffer.
542    This typically means an ARM6 or ARM7 with MMU or MPU.  */
543 int arm_tune_wbuf = 0;
544
545 /* Nonzero if generating Thumb instructions.  */
546 int thumb_code = 0;
547
548 /* Nonzero if we should define __THUMB_INTERWORK__ in the
549    preprocessor.
550    XXX This is a bit of a hack, it's intended to help work around
551    problems in GLD which doesn't understand that armv5t code is
552    interworking clean.  */
553 int arm_cpp_interwork = 0;
554
555 /* Nonzero if chip supports Thumb 2.  */
556 int arm_arch_thumb2;
557
558 /* Nonzero if chip supports integer division instruction.  */
559 int arm_arch_hwdiv;
560
561 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
562    must report the mode of the memory reference from PRINT_OPERAND to
563    PRINT_OPERAND_ADDRESS.  */
564 enum machine_mode output_memory_reference_mode;
565
566 /* The register number to be used for the PIC offset register.  */
567 unsigned arm_pic_register = INVALID_REGNUM;
568
569 /* Set to 1 when a return insn is output, this means that the epilogue
570    is not needed.  */
571 int return_used_this_function;
572
573 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
574    the next function.  */
575 static int after_arm_reorg = 0;
576
577 /* The maximum number of insns to be used when loading a constant.  */
578 static int arm_constant_limit = 3;
579
580 /* For an explanation of these variables, see final_prescan_insn below.  */
581 int arm_ccfsm_state;
582 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
583 enum arm_cond_code arm_current_cc;
584 rtx arm_target_insn;
585 int arm_target_label;
586 /* The number of conditionally executed insns, including the current insn.  */
587 int arm_condexec_count = 0;
588 /* A bitmask specifying the patterns for the IT block.
589    Zero means do not output an IT block before this insn. */
590 int arm_condexec_mask = 0;
591 /* The number of bits used in arm_condexec_mask.  */
592 int arm_condexec_masklen = 0;
593
594 /* The condition codes of the ARM, and the inverse function.  */
595 static const char * const arm_condition_codes[] =
596 {
597   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
598   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
599 };
600
601 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
602 #define streq(string1, string2) (strcmp (string1, string2) == 0)
603
604 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
605                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
606                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
607 \f
608 /* Initialization code.  */
609
610 struct processors
611 {
612   const char *const name;
613   enum processor_type core;
614   const char *arch;
615   const unsigned long flags;
616   bool (* rtx_costs) (rtx, int, int, int *);
617 };
618
619 /* Not all of these give usefully different compilation alternatives,
620    but there is no simple way of generalizing them.  */
621 static const struct processors all_cores[] =
622 {
623   /* ARM Cores */
624 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
625   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
626 #include "arm-cores.def"
627 #undef ARM_CORE
628   {NULL, arm_none, NULL, 0, NULL}
629 };
630
631 static const struct processors all_architectures[] =
632 {
633   /* ARM Architectures */
634   /* We don't specify rtx_costs here as it will be figured out
635      from the core.  */
636
637   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
638   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
639   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
640   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
641   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
642   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
643      implementations that support it, so we will leave it out for now.  */
644   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
645   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
646   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
647   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
648   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
649   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
650   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
651   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
652   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
653   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
654   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
655   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
656   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
657   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
658   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
659   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
660   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
661   {NULL, arm_none, NULL, 0 , NULL}
662 };
663
664 struct arm_cpu_select
665 {
666   const char *              string;
667   const char *              name;
668   const struct processors * processors;
669 };
670
671 /* This is a magic structure.  The 'string' field is magically filled in
672    with a pointer to the value specified by the user on the command line
673    assuming that the user has specified such a value.  */
674
675 static struct arm_cpu_select arm_select[] =
676 {
677   /* string       name            processors  */
678   { NULL,       "-mcpu=",       all_cores  },
679   { NULL,       "-march=",      all_architectures },
680   { NULL,       "-mtune=",      all_cores }
681 };
682
683 /* Defines representing the indexes into the above table.  */
684 #define ARM_OPT_SET_CPU 0
685 #define ARM_OPT_SET_ARCH 1
686 #define ARM_OPT_SET_TUNE 2
687
688 /* The name of the preprocessor macro to define for this architecture.  */
689
690 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
691
692 struct fpu_desc
693 {
694   const char * name;
695   enum fputype fpu;
696 };
697
698
699 /* Available values for -mfpu=.  */
700
701 static const struct fpu_desc all_fpus[] =
702 {
703   {"fpa",       FPUTYPE_FPA},
704   {"fpe2",      FPUTYPE_FPA_EMU2},
705   {"fpe3",      FPUTYPE_FPA_EMU2},
706   {"maverick",  FPUTYPE_MAVERICK},
707   {"vfp",       FPUTYPE_VFP},
708   {"vfp3",      FPUTYPE_VFP3},
709 };
710
711
712 /* Floating point models used by the different hardware.
713    See fputype in arm.h.  */
714
715 static const enum fputype fp_model_for_fpu[] =
716 {
717   /* No FP hardware.  */
718   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
719   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
720   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
721   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
722   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
723   ARM_FP_MODEL_VFP,             /* FPUTYPE_VFP  */
724   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP3  */
725 };
726
727
728 struct float_abi
729 {
730   const char * name;
731   enum float_abi_type abi_type;
732 };
733
734
735 /* Available values for -mfloat-abi=.  */
736
737 static const struct float_abi all_float_abis[] =
738 {
739   {"soft",      ARM_FLOAT_ABI_SOFT},
740   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
741   {"hard",      ARM_FLOAT_ABI_HARD}
742 };
743
744
745 struct abi_name
746 {
747   const char *name;
748   enum arm_abi_type abi_type;
749 };
750
751
752 /* Available values for -mabi=.  */
753
754 static const struct abi_name arm_all_abis[] =
755 {
756   {"apcs-gnu",    ARM_ABI_APCS},
757   {"atpcs",   ARM_ABI_ATPCS},
758   {"aapcs",   ARM_ABI_AAPCS},
759   {"iwmmxt",  ARM_ABI_IWMMXT},
760   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
761 };
762
763 /* Supported TLS relocations.  */
764
765 enum tls_reloc {
766   TLS_GD32,
767   TLS_LDM32,
768   TLS_LDO32,
769   TLS_IE32,
770   TLS_LE32
771 };
772
773 /* Emit an insn that's a simple single-set.  Both the operands must be known
774    to be valid.  */
775 inline static rtx
776 emit_set_insn (rtx x, rtx y)
777 {
778   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
779 }
780
781 /* Return the number of bits set in VALUE.  */
782 static unsigned
783 bit_count (unsigned long value)
784 {
785   unsigned long count = 0;
786
787   while (value)
788     {
789       count++;
790       value &= value - 1;  /* Clear the least-significant set bit.  */
791     }
792
793   return count;
794 }
795
796 /* Set up library functions unique to ARM.  */
797
798 static void
799 arm_init_libfuncs (void)
800 {
801   /* There are no special library functions unless we are using the
802      ARM BPABI.  */
803   if (!TARGET_BPABI)
804     return;
805
806   /* The functions below are described in Section 4 of the "Run-Time
807      ABI for the ARM architecture", Version 1.0.  */
808
809   /* Double-precision floating-point arithmetic.  Table 2.  */
810   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
811   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
812   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
813   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
814   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
815
816   /* Double-precision comparisons.  Table 3.  */
817   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
818   set_optab_libfunc (ne_optab, DFmode, NULL);
819   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
820   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
821   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
822   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
823   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
824
825   /* Single-precision floating-point arithmetic.  Table 4.  */
826   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
827   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
828   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
829   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
830   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
831
832   /* Single-precision comparisons.  Table 5.  */
833   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
834   set_optab_libfunc (ne_optab, SFmode, NULL);
835   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
836   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
837   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
838   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
839   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
840
841   /* Floating-point to integer conversions.  Table 6.  */
842   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
843   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
844   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
845   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
846   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
847   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
848   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
849   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
850
851   /* Conversions between floating types.  Table 7.  */
852   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
853   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
854
855   /* Integer to floating-point conversions.  Table 8.  */
856   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
857   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
858   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
859   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
860   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
861   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
862   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
863   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
864
865   /* Long long.  Table 9.  */
866   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
867   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
868   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
869   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
870   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
871   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
872   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
873   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
874
875   /* Integer (32/32->32) division.  \S 4.3.1.  */
876   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
877   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
878
879   /* The divmod functions are designed so that they can be used for
880      plain division, even though they return both the quotient and the
881      remainder.  The quotient is returned in the usual location (i.e.,
882      r0 for SImode, {r0, r1} for DImode), just as would be expected
883      for an ordinary division routine.  Because the AAPCS calling
884      conventions specify that all of { r0, r1, r2, r3 } are
885      callee-saved registers, there is no need to tell the compiler
886      explicitly that those registers are clobbered by these
887      routines.  */
888   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
889   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
890
891   /* For SImode division the ABI provides div-without-mod routines,
892      which are faster.  */
893   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
894   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
895
896   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
897      divmod libcalls instead.  */
898   set_optab_libfunc (smod_optab, DImode, NULL);
899   set_optab_libfunc (umod_optab, DImode, NULL);
900   set_optab_libfunc (smod_optab, SImode, NULL);
901   set_optab_libfunc (umod_optab, SImode, NULL);
902 }
903
904 /* Implement TARGET_HANDLE_OPTION.  */
905
906 static bool
907 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
908 {
909   switch (code)
910     {
911     case OPT_march_:
912       arm_select[1].string = arg;
913       return true;
914
915     case OPT_mcpu_:
916       arm_select[0].string = arg;
917       return true;
918
919     case OPT_mhard_float:
920       target_float_abi_name = "hard";
921       return true;
922
923     case OPT_msoft_float:
924       target_float_abi_name = "soft";
925       return true;
926
927     case OPT_mtune_:
928       arm_select[2].string = arg;
929       return true;
930
931     default:
932       return true;
933     }
934 }
935
936 static void
937 arm_target_help (void)
938 {
939   int i;
940   static int columns = 0;
941   int remaining;
942
943   /* If we have not done so already, obtain the desired maximum width of
944      the output.  Note - this is a duplication of the code at the start of
945      gcc/opts.c:print_specific_help() - the two copies should probably be
946      replaced by a single function.  */
947   if (columns == 0)
948     {
949       const char *p;
950
951       GET_ENVIRONMENT (p, "COLUMNS");
952       if (p != NULL)
953         {
954           int value = atoi (p);
955
956           if (value > 0)
957             columns = value;
958         }
959
960       if (columns == 0)
961         /* Use a reasonable default.  */
962         columns = 80;
963     }
964
965   printf ("  Known ARM CPUs (for use with the -mcpu= and -mtune= options):\n");
966
967   /* The - 2 is because we know that the last entry in the array is NULL.  */
968   i = ARRAY_SIZE (all_cores) - 2;
969   gcc_assert (i > 0);
970   printf ("    %s", all_cores[i].name);
971   remaining = columns - (strlen (all_cores[i].name) + 4);
972   gcc_assert (remaining >= 0);
973
974   while (i--)
975     {
976       int len = strlen (all_cores[i].name);
977
978       if (remaining > len + 2)
979         {
980           printf (", %s", all_cores[i].name);
981           remaining -= len + 2;
982         }
983       else
984         {
985           if (remaining > 0)
986             printf (",");
987           printf ("\n    %s", all_cores[i].name);
988           remaining = columns - (len + 4);
989         }
990     }
991
992   printf ("\n\n  Known ARM architectures (for use with the -march= option):\n");
993
994   i = ARRAY_SIZE (all_architectures) - 2;
995   gcc_assert (i > 0);
996   
997   printf ("    %s", all_architectures[i].name);
998   remaining = columns - (strlen (all_architectures[i].name) + 4);
999   gcc_assert (remaining >= 0);
1000
1001   while (i--)
1002     {
1003       int len = strlen (all_architectures[i].name);
1004
1005       if (remaining > len + 2)
1006         {
1007           printf (", %s", all_architectures[i].name);
1008           remaining -= len + 2;
1009         }
1010       else
1011         {
1012           if (remaining > 0)
1013             printf (",");
1014           printf ("\n    %s", all_architectures[i].name);
1015           remaining = columns - (len + 4);
1016         }
1017     }
1018   printf ("\n");
1019
1020 }
1021
1022 /* Fix up any incompatible options that the user has specified.
1023    This has now turned into a maze.  */
1024 void
1025 arm_override_options (void)
1026 {
1027   unsigned i;
1028   enum processor_type target_arch_cpu = arm_none;
1029
1030   /* Set up the flags based on the cpu/architecture selected by the user.  */
1031   for (i = ARRAY_SIZE (arm_select); i--;)
1032     {
1033       struct arm_cpu_select * ptr = arm_select + i;
1034
1035       if (ptr->string != NULL && ptr->string[0] != '\0')
1036         {
1037           const struct processors * sel;
1038
1039           for (sel = ptr->processors; sel->name != NULL; sel++)
1040             if (streq (ptr->string, sel->name))
1041               {
1042                 /* Set the architecture define.  */
1043                 if (i != ARM_OPT_SET_TUNE)
1044                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1045
1046                 /* Determine the processor core for which we should
1047                    tune code-generation.  */
1048                 if (/* -mcpu= is a sensible default.  */
1049                     i == ARM_OPT_SET_CPU
1050                     /* -mtune= overrides -mcpu= and -march=.  */
1051                     || i == ARM_OPT_SET_TUNE)
1052                   arm_tune = (enum processor_type) (sel - ptr->processors);
1053
1054                 /* Remember the CPU associated with this architecture.
1055                    If no other option is used to set the CPU type,
1056                    we'll use this to guess the most suitable tuning
1057                    options.  */
1058                 if (i == ARM_OPT_SET_ARCH)
1059                   target_arch_cpu = sel->core;
1060
1061                 if (i != ARM_OPT_SET_TUNE)
1062                   {
1063                     /* If we have been given an architecture and a processor
1064                        make sure that they are compatible.  We only generate
1065                        a warning though, and we prefer the CPU over the
1066                        architecture.  */
1067                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
1068                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
1069                                ptr->string);
1070
1071                     insn_flags = sel->flags;
1072                   }
1073
1074                 break;
1075               }
1076
1077           if (sel->name == NULL)
1078             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
1079         }
1080     }
1081
1082   /* Guess the tuning options from the architecture if necessary.  */
1083   if (arm_tune == arm_none)
1084     arm_tune = target_arch_cpu;
1085
1086   /* If the user did not specify a processor, choose one for them.  */
1087   if (insn_flags == 0)
1088     {
1089       const struct processors * sel;
1090       unsigned int        sought;
1091       enum processor_type cpu;
1092
1093       cpu = TARGET_CPU_DEFAULT;
1094       if (cpu == arm_none)
1095         {
1096 #ifdef SUBTARGET_CPU_DEFAULT
1097           /* Use the subtarget default CPU if none was specified by
1098              configure.  */
1099           cpu = SUBTARGET_CPU_DEFAULT;
1100 #endif
1101           /* Default to ARM6.  */
1102           if (cpu == arm_none)
1103             cpu = arm6;
1104         }
1105       sel = &all_cores[cpu];
1106
1107       insn_flags = sel->flags;
1108
1109       /* Now check to see if the user has specified some command line
1110          switch that require certain abilities from the cpu.  */
1111       sought = 0;
1112
1113       if (TARGET_INTERWORK || TARGET_THUMB)
1114         {
1115           sought |= (FL_THUMB | FL_MODE32);
1116
1117           /* There are no ARM processors that support both APCS-26 and
1118              interworking.  Therefore we force FL_MODE26 to be removed
1119              from insn_flags here (if it was set), so that the search
1120              below will always be able to find a compatible processor.  */
1121           insn_flags &= ~FL_MODE26;
1122         }
1123
1124       if (sought != 0 && ((sought & insn_flags) != sought))
1125         {
1126           /* Try to locate a CPU type that supports all of the abilities
1127              of the default CPU, plus the extra abilities requested by
1128              the user.  */
1129           for (sel = all_cores; sel->name != NULL; sel++)
1130             if ((sel->flags & sought) == (sought | insn_flags))
1131               break;
1132
1133           if (sel->name == NULL)
1134             {
1135               unsigned current_bit_count = 0;
1136               const struct processors * best_fit = NULL;
1137
1138               /* Ideally we would like to issue an error message here
1139                  saying that it was not possible to find a CPU compatible
1140                  with the default CPU, but which also supports the command
1141                  line options specified by the programmer, and so they
1142                  ought to use the -mcpu=<name> command line option to
1143                  override the default CPU type.
1144
1145                  If we cannot find a cpu that has both the
1146                  characteristics of the default cpu and the given
1147                  command line options we scan the array again looking
1148                  for a best match.  */
1149               for (sel = all_cores; sel->name != NULL; sel++)
1150                 if ((sel->flags & sought) == sought)
1151                   {
1152                     unsigned count;
1153
1154                     count = bit_count (sel->flags & insn_flags);
1155
1156                     if (count >= current_bit_count)
1157                       {
1158                         best_fit = sel;
1159                         current_bit_count = count;
1160                       }
1161                   }
1162
1163               gcc_assert (best_fit);
1164               sel = best_fit;
1165             }
1166
1167           insn_flags = sel->flags;
1168         }
1169       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1170       arm_default_cpu = (enum processor_type) (sel - all_cores);
1171       if (arm_tune == arm_none)
1172         arm_tune = arm_default_cpu;
1173     }
1174
1175   /* The processor for which we should tune should now have been
1176      chosen.  */
1177   gcc_assert (arm_tune != arm_none);
1178
1179   tune_flags = all_cores[(int)arm_tune].flags;
1180   if (optimize_size)
1181     targetm.rtx_costs = arm_size_rtx_costs;
1182   else
1183     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1184
1185   /* Make sure that the processor choice does not conflict with any of the
1186      other command line choices.  */
1187   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1188     error ("target CPU does not support ARM mode");
1189
1190   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1191     {
1192       warning (0, "target CPU does not support interworking" );
1193       target_flags &= ~MASK_INTERWORK;
1194     }
1195
1196   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1197     {
1198       warning (0, "target CPU does not support THUMB instructions");
1199       target_flags &= ~MASK_THUMB;
1200     }
1201
1202   if (TARGET_APCS_FRAME && TARGET_THUMB)
1203     {
1204       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1205       target_flags &= ~MASK_APCS_FRAME;
1206     }
1207
1208   /* Callee super interworking implies thumb interworking.  Adding
1209      this to the flags here simplifies the logic elsewhere.  */
1210   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1211       target_flags |= MASK_INTERWORK;
1212
1213   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1214      from here where no function is being compiled currently.  */
1215   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1216     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1217
1218   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1219     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1220
1221   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1222     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1223
1224   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1225     {
1226       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1227       target_flags |= MASK_APCS_FRAME;
1228     }
1229
1230   if (TARGET_POKE_FUNCTION_NAME)
1231     target_flags |= MASK_APCS_FRAME;
1232
1233   if (TARGET_APCS_REENT && flag_pic)
1234     error ("-fpic and -mapcs-reent are incompatible");
1235
1236   if (TARGET_APCS_REENT)
1237     warning (0, "APCS reentrant code not supported.  Ignored");
1238
1239   /* If this target is normally configured to use APCS frames, warn if they
1240      are turned off and debugging is turned on.  */
1241   if (TARGET_ARM
1242       && write_symbols != NO_DEBUG
1243       && !TARGET_APCS_FRAME
1244       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1245     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1246
1247   if (TARGET_APCS_FLOAT)
1248     warning (0, "passing floating point arguments in fp regs not yet supported");
1249
1250   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1251   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1252   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1253   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1254   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1255   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1256   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1257   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1258   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1259   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1260   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1261   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1262
1263   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1264   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1265   thumb_code = (TARGET_ARM == 0);
1266   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1267   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1268   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1269   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1270
1271   /* V5 code we generate is completely interworking capable, so we turn off
1272      TARGET_INTERWORK here to avoid many tests later on.  */
1273
1274   /* XXX However, we must pass the right pre-processor defines to CPP
1275      or GLD can get confused.  This is a hack.  */
1276   if (TARGET_INTERWORK)
1277     arm_cpp_interwork = 1;
1278
1279   if (arm_arch5)
1280     target_flags &= ~MASK_INTERWORK;
1281
1282   if (target_abi_name)
1283     {
1284       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1285         {
1286           if (streq (arm_all_abis[i].name, target_abi_name))
1287             {
1288               arm_abi = arm_all_abis[i].abi_type;
1289               break;
1290             }
1291         }
1292       if (i == ARRAY_SIZE (arm_all_abis))
1293         error ("invalid ABI option: -mabi=%s", target_abi_name);
1294     }
1295   else
1296     arm_abi = ARM_DEFAULT_ABI;
1297
1298   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1299     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1300
1301   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1302     error ("iwmmxt abi requires an iwmmxt capable cpu");
1303
1304   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1305   if (target_fpu_name == NULL && target_fpe_name != NULL)
1306     {
1307       if (streq (target_fpe_name, "2"))
1308         target_fpu_name = "fpe2";
1309       else if (streq (target_fpe_name, "3"))
1310         target_fpu_name = "fpe3";
1311       else
1312         error ("invalid floating point emulation option: -mfpe=%s",
1313                target_fpe_name);
1314     }
1315   if (target_fpu_name != NULL)
1316     {
1317       /* The user specified a FPU.  */
1318       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1319         {
1320           if (streq (all_fpus[i].name, target_fpu_name))
1321             {
1322               arm_fpu_arch = all_fpus[i].fpu;
1323               arm_fpu_tune = arm_fpu_arch;
1324               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1325               break;
1326             }
1327         }
1328       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1329         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1330     }
1331   else
1332     {
1333 #ifdef FPUTYPE_DEFAULT
1334       /* Use the default if it is specified for this platform.  */
1335       arm_fpu_arch = FPUTYPE_DEFAULT;
1336       arm_fpu_tune = FPUTYPE_DEFAULT;
1337 #else
1338       /* Pick one based on CPU type.  */
1339       /* ??? Some targets assume FPA is the default.
1340       if ((insn_flags & FL_VFP) != 0)
1341         arm_fpu_arch = FPUTYPE_VFP;
1342       else
1343       */
1344       if (arm_arch_cirrus)
1345         arm_fpu_arch = FPUTYPE_MAVERICK;
1346       else
1347         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1348 #endif
1349       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1350         arm_fpu_tune = FPUTYPE_FPA;
1351       else
1352         arm_fpu_tune = arm_fpu_arch;
1353       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1354       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1355     }
1356
1357   if (target_float_abi_name != NULL)
1358     {
1359       /* The user specified a FP ABI.  */
1360       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1361         {
1362           if (streq (all_float_abis[i].name, target_float_abi_name))
1363             {
1364               arm_float_abi = all_float_abis[i].abi_type;
1365               break;
1366             }
1367         }
1368       if (i == ARRAY_SIZE (all_float_abis))
1369         error ("invalid floating point abi: -mfloat-abi=%s",
1370                target_float_abi_name);
1371     }
1372   else
1373     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1374
1375   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1376     sorry ("-mfloat-abi=hard and VFP");
1377
1378   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1379      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1380      will ever exist.  GCC makes no attempt to support this combination.  */
1381   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1382     sorry ("iWMMXt and hardware floating point");
1383
1384   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1385   if (TARGET_THUMB2 && TARGET_IWMMXT)
1386     sorry ("Thumb-2 iWMMXt");
1387
1388   /* If soft-float is specified then don't use FPU.  */
1389   if (TARGET_SOFT_FLOAT)
1390     arm_fpu_arch = FPUTYPE_NONE;
1391
1392   /* For arm2/3 there is no need to do any scheduling if there is only
1393      a floating point emulator, or we are doing software floating-point.  */
1394   if ((TARGET_SOFT_FLOAT
1395        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1396        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1397       && (tune_flags & FL_MODE32) == 0)
1398     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1399
1400   if (target_thread_switch)
1401     {
1402       if (strcmp (target_thread_switch, "soft") == 0)
1403         target_thread_pointer = TP_SOFT;
1404       else if (strcmp (target_thread_switch, "auto") == 0)
1405         target_thread_pointer = TP_AUTO;
1406       else if (strcmp (target_thread_switch, "cp15") == 0)
1407         target_thread_pointer = TP_CP15;
1408       else
1409         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1410     }
1411
1412   /* Use the cp15 method if it is available.  */
1413   if (target_thread_pointer == TP_AUTO)
1414     {
1415       if (arm_arch6k && !TARGET_THUMB)
1416         target_thread_pointer = TP_CP15;
1417       else
1418         target_thread_pointer = TP_SOFT;
1419     }
1420
1421   if (TARGET_HARD_TP && TARGET_THUMB1)
1422     error ("can not use -mtp=cp15 with 16-bit Thumb");
1423
1424   /* Override the default structure alignment for AAPCS ABI.  */
1425   if (TARGET_AAPCS_BASED)
1426     arm_structure_size_boundary = 8;
1427
1428   if (structure_size_string != NULL)
1429     {
1430       int size = strtol (structure_size_string, NULL, 0);
1431
1432       if (size == 8 || size == 32
1433           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1434         arm_structure_size_boundary = size;
1435       else
1436         warning (0, "structure size boundary can only be set to %s",
1437                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1438     }
1439
1440   if (!TARGET_ARM && TARGET_VXWORKS_RTP && flag_pic)
1441     {
1442       error ("RTP PIC is incompatible with Thumb");
1443       flag_pic = 0;
1444     }
1445
1446   /* If stack checking is disabled, we can use r10 as the PIC register,
1447      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1448   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1449     {
1450       if (TARGET_VXWORKS_RTP)
1451         warning (0, "RTP PIC is incompatible with -msingle-pic-base");
1452       arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1453     }
1454
1455   if (flag_pic && TARGET_VXWORKS_RTP)
1456     arm_pic_register = 9;
1457
1458   if (arm_pic_register_string != NULL)
1459     {
1460       int pic_register = decode_reg_name (arm_pic_register_string);
1461
1462       if (!flag_pic)
1463         warning (0, "-mpic-register= is useless without -fpic");
1464
1465       /* Prevent the user from choosing an obviously stupid PIC register.  */
1466       else if (pic_register < 0 || call_used_regs[pic_register]
1467                || pic_register == HARD_FRAME_POINTER_REGNUM
1468                || pic_register == STACK_POINTER_REGNUM
1469                || pic_register >= PC_REGNUM
1470                || (TARGET_VXWORKS_RTP
1471                    && (unsigned int) pic_register != arm_pic_register))
1472         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1473       else
1474         arm_pic_register = pic_register;
1475     }
1476
1477   /* ??? We might want scheduling for thumb2.  */
1478   if (TARGET_THUMB && flag_schedule_insns)
1479     {
1480       /* Don't warn since it's on by default in -O2.  */
1481       flag_schedule_insns = 0;
1482     }
1483
1484   if (optimize_size)
1485     {
1486       arm_constant_limit = 1;
1487
1488       /* If optimizing for size, bump the number of instructions that we
1489          are prepared to conditionally execute (even on a StrongARM).  */
1490       max_insns_skipped = 6;
1491     }
1492   else
1493     {
1494       /* For processors with load scheduling, it never costs more than
1495          2 cycles to load a constant, and the load scheduler may well
1496          reduce that to 1.  */
1497       if (arm_ld_sched)
1498         arm_constant_limit = 1;
1499
1500       /* On XScale the longer latency of a load makes it more difficult
1501          to achieve a good schedule, so it's faster to synthesize
1502          constants that can be done in two insns.  */
1503       if (arm_tune_xscale)
1504         arm_constant_limit = 2;
1505
1506       /* StrongARM has early execution of branches, so a sequence
1507          that is worth skipping is shorter.  */
1508       if (arm_tune_strongarm)
1509         max_insns_skipped = 3;
1510     }
1511
1512   /* Register global variables with the garbage collector.  */
1513   arm_add_gc_roots ();
1514 }
1515
1516 static void
1517 arm_add_gc_roots (void)
1518 {
1519   gcc_obstack_init(&minipool_obstack);
1520   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1521 }
1522 \f
1523 /* A table of known ARM exception types.
1524    For use with the interrupt function attribute.  */
1525
1526 typedef struct
1527 {
1528   const char *const arg;
1529   const unsigned long return_value;
1530 }
1531 isr_attribute_arg;
1532
1533 static const isr_attribute_arg isr_attribute_args [] =
1534 {
1535   { "IRQ",   ARM_FT_ISR },
1536   { "irq",   ARM_FT_ISR },
1537   { "FIQ",   ARM_FT_FIQ },
1538   { "fiq",   ARM_FT_FIQ },
1539   { "ABORT", ARM_FT_ISR },
1540   { "abort", ARM_FT_ISR },
1541   { "ABORT", ARM_FT_ISR },
1542   { "abort", ARM_FT_ISR },
1543   { "UNDEF", ARM_FT_EXCEPTION },
1544   { "undef", ARM_FT_EXCEPTION },
1545   { "SWI",   ARM_FT_EXCEPTION },
1546   { "swi",   ARM_FT_EXCEPTION },
1547   { NULL,    ARM_FT_NORMAL }
1548 };
1549
1550 /* Returns the (interrupt) function type of the current
1551    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1552
1553 static unsigned long
1554 arm_isr_value (tree argument)
1555 {
1556   const isr_attribute_arg * ptr;
1557   const char *              arg;
1558
1559   if (!arm_arch_notm)
1560     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1561
1562   /* No argument - default to IRQ.  */
1563   if (argument == NULL_TREE)
1564     return ARM_FT_ISR;
1565
1566   /* Get the value of the argument.  */
1567   if (TREE_VALUE (argument) == NULL_TREE
1568       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1569     return ARM_FT_UNKNOWN;
1570
1571   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1572
1573   /* Check it against the list of known arguments.  */
1574   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1575     if (streq (arg, ptr->arg))
1576       return ptr->return_value;
1577
1578   /* An unrecognized interrupt type.  */
1579   return ARM_FT_UNKNOWN;
1580 }
1581
1582 /* Computes the type of the current function.  */
1583
1584 static unsigned long
1585 arm_compute_func_type (void)
1586 {
1587   unsigned long type = ARM_FT_UNKNOWN;
1588   tree a;
1589   tree attr;
1590
1591   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1592
1593   /* Decide if the current function is volatile.  Such functions
1594      never return, and many memory cycles can be saved by not storing
1595      register values that will never be needed again.  This optimization
1596      was added to speed up context switching in a kernel application.  */
1597   if (optimize > 0
1598       && (TREE_NOTHROW (current_function_decl)
1599           || !(flag_unwind_tables
1600                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1601       && TREE_THIS_VOLATILE (current_function_decl))
1602     type |= ARM_FT_VOLATILE;
1603
1604   if (cfun->static_chain_decl != NULL)
1605     type |= ARM_FT_NESTED;
1606
1607   attr = DECL_ATTRIBUTES (current_function_decl);
1608
1609   a = lookup_attribute ("naked", attr);
1610   if (a != NULL_TREE)
1611     type |= ARM_FT_NAKED;
1612
1613   a = lookup_attribute ("isr", attr);
1614   if (a == NULL_TREE)
1615     a = lookup_attribute ("interrupt", attr);
1616
1617   if (a == NULL_TREE)
1618     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1619   else
1620     type |= arm_isr_value (TREE_VALUE (a));
1621
1622   return type;
1623 }
1624
1625 /* Returns the type of the current function.  */
1626
1627 unsigned long
1628 arm_current_func_type (void)
1629 {
1630   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1631     cfun->machine->func_type = arm_compute_func_type ();
1632
1633   return cfun->machine->func_type;
1634 }
1635 \f
1636 /* Return 1 if it is possible to return using a single instruction.
1637    If SIBLING is non-null, this is a test for a return before a sibling
1638    call.  SIBLING is the call insn, so we can examine its register usage.  */
1639
1640 int
1641 use_return_insn (int iscond, rtx sibling)
1642 {
1643   int regno;
1644   unsigned int func_type;
1645   unsigned long saved_int_regs;
1646   unsigned HOST_WIDE_INT stack_adjust;
1647   arm_stack_offsets *offsets;
1648
1649   /* Never use a return instruction before reload has run.  */
1650   if (!reload_completed)
1651     return 0;
1652
1653   func_type = arm_current_func_type ();
1654
1655   /* Naked, volatile and stack alignment functions need special
1656      consideration.  */
1657   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1658     return 0;
1659
1660   /* So do interrupt functions that use the frame pointer and Thumb
1661      interrupt functions.  */
1662   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1663     return 0;
1664
1665   offsets = arm_get_frame_offsets ();
1666   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1667
1668   /* As do variadic functions.  */
1669   if (current_function_pretend_args_size
1670       || cfun->machine->uses_anonymous_args
1671       /* Or if the function calls __builtin_eh_return () */
1672       || current_function_calls_eh_return
1673       /* Or if the function calls alloca */
1674       || current_function_calls_alloca
1675       /* Or if there is a stack adjustment.  However, if the stack pointer
1676          is saved on the stack, we can use a pre-incrementing stack load.  */
1677       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1678     return 0;
1679
1680   saved_int_regs = arm_compute_save_reg_mask ();
1681
1682   /* Unfortunately, the insn
1683
1684        ldmib sp, {..., sp, ...}
1685
1686      triggers a bug on most SA-110 based devices, such that the stack
1687      pointer won't be correctly restored if the instruction takes a
1688      page fault.  We work around this problem by popping r3 along with
1689      the other registers, since that is never slower than executing
1690      another instruction.
1691
1692      We test for !arm_arch5 here, because code for any architecture
1693      less than this could potentially be run on one of the buggy
1694      chips.  */
1695   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1696     {
1697       /* Validate that r3 is a call-clobbered register (always true in
1698          the default abi) ...  */
1699       if (!call_used_regs[3])
1700         return 0;
1701
1702       /* ... that it isn't being used for a return value ... */
1703       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1704         return 0;
1705
1706       /* ... or for a tail-call argument ...  */
1707       if (sibling)
1708         {
1709           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1710
1711           if (find_regno_fusage (sibling, USE, 3))
1712             return 0;
1713         }
1714
1715       /* ... and that there are no call-saved registers in r0-r2
1716          (always true in the default ABI).  */
1717       if (saved_int_regs & 0x7)
1718         return 0;
1719     }
1720
1721   /* Can't be done if interworking with Thumb, and any registers have been
1722      stacked.  */
1723   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1724     return 0;
1725
1726   /* On StrongARM, conditional returns are expensive if they aren't
1727      taken and multiple registers have been stacked.  */
1728   if (iscond && arm_tune_strongarm)
1729     {
1730       /* Conditional return when just the LR is stored is a simple
1731          conditional-load instruction, that's not expensive.  */
1732       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1733         return 0;
1734
1735       if (flag_pic 
1736           && arm_pic_register != INVALID_REGNUM
1737           && df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM))
1738         return 0;
1739     }
1740
1741   /* If there are saved registers but the LR isn't saved, then we need
1742      two instructions for the return.  */
1743   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1744     return 0;
1745
1746   /* Can't be done if any of the FPA regs are pushed,
1747      since this also requires an insn.  */
1748   if (TARGET_HARD_FLOAT && TARGET_FPA)
1749     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1750       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
1751         return 0;
1752
1753   /* Likewise VFP regs.  */
1754   if (TARGET_HARD_FLOAT && TARGET_VFP)
1755     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1756       if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
1757         return 0;
1758
1759   if (TARGET_REALLY_IWMMXT)
1760     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1761       if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
1762         return 0;
1763
1764   return 1;
1765 }
1766
1767 /* Return TRUE if int I is a valid immediate ARM constant.  */
1768
1769 int
1770 const_ok_for_arm (HOST_WIDE_INT i)
1771 {
1772   int lowbit;
1773
1774   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1775      be all zero, or all one.  */
1776   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1777       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1778           != ((~(unsigned HOST_WIDE_INT) 0)
1779               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1780     return FALSE;
1781
1782   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1783
1784   /* Fast return for 0 and small values.  We must do this for zero, since
1785      the code below can't handle that one case.  */
1786   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1787     return TRUE;
1788
1789   /* Get the number of trailing zeros.  */
1790   lowbit = ffs((int) i) - 1;
1791   
1792   /* Only even shifts are allowed in ARM mode so round down to the
1793      nearest even number.  */
1794   if (TARGET_ARM)
1795     lowbit &= ~1;
1796
1797   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1798     return TRUE;
1799
1800   if (TARGET_ARM)
1801     {
1802       /* Allow rotated constants in ARM mode.  */
1803       if (lowbit <= 4
1804            && ((i & ~0xc000003f) == 0
1805                || (i & ~0xf000000f) == 0
1806                || (i & ~0xfc000003) == 0))
1807         return TRUE;
1808     }
1809   else
1810     {
1811       HOST_WIDE_INT v;
1812
1813       /* Allow repeated pattern.  */
1814       v = i & 0xff;
1815       v |= v << 16;
1816       if (i == v || i == (v | (v << 8)))
1817         return TRUE;
1818     }
1819
1820   return FALSE;
1821 }
1822
1823 /* Return true if I is a valid constant for the operation CODE.  */
1824 static int
1825 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1826 {
1827   if (const_ok_for_arm (i))
1828     return 1;
1829
1830   switch (code)
1831     {
1832     case PLUS:
1833       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1834
1835     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1836     case XOR:
1837     case IOR:
1838       return 0;
1839
1840     case AND:
1841       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1842
1843     default:
1844       gcc_unreachable ();
1845     }
1846 }
1847
1848 /* Emit a sequence of insns to handle a large constant.
1849    CODE is the code of the operation required, it can be any of SET, PLUS,
1850    IOR, AND, XOR, MINUS;
1851    MODE is the mode in which the operation is being performed;
1852    VAL is the integer to operate on;
1853    SOURCE is the other operand (a register, or a null-pointer for SET);
1854    SUBTARGETS means it is safe to create scratch registers if that will
1855    either produce a simpler sequence, or we will want to cse the values.
1856    Return value is the number of insns emitted.  */
1857
1858 /* ??? Tweak this for thumb2.  */
1859 int
1860 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1861                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1862 {
1863   rtx cond;
1864
1865   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1866     cond = COND_EXEC_TEST (PATTERN (insn));
1867   else
1868     cond = NULL_RTX;
1869
1870   if (subtargets || code == SET
1871       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1872           && REGNO (target) != REGNO (source)))
1873     {
1874       /* After arm_reorg has been called, we can't fix up expensive
1875          constants by pushing them into memory so we must synthesize
1876          them in-line, regardless of the cost.  This is only likely to
1877          be more costly on chips that have load delay slots and we are
1878          compiling without running the scheduler (so no splitting
1879          occurred before the final instruction emission).
1880
1881          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1882       */
1883       if (!after_arm_reorg
1884           && !cond
1885           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1886                                 1, 0)
1887               > arm_constant_limit + (code != SET)))
1888         {
1889           if (code == SET)
1890             {
1891               /* Currently SET is the only monadic value for CODE, all
1892                  the rest are diadic.  */
1893               emit_set_insn (target, GEN_INT (val));
1894               return 1;
1895             }
1896           else
1897             {
1898               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1899
1900               emit_set_insn (temp, GEN_INT (val));
1901               /* For MINUS, the value is subtracted from, since we never
1902                  have subtraction of a constant.  */
1903               if (code == MINUS)
1904                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1905               else
1906                 emit_set_insn (target,
1907                                gen_rtx_fmt_ee (code, mode, source, temp));
1908               return 2;
1909             }
1910         }
1911     }
1912
1913   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1914                            1);
1915 }
1916
1917 /* Return the number of ARM instructions required to synthesize the given
1918    constant.  */
1919 static int
1920 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1921 {
1922   HOST_WIDE_INT temp1;
1923   int num_insns = 0;
1924   do
1925     {
1926       int end;
1927
1928       if (i <= 0)
1929         i += 32;
1930       if (remainder & (3 << (i - 2)))
1931         {
1932           end = i - 8;
1933           if (end < 0)
1934             end += 32;
1935           temp1 = remainder & ((0x0ff << end)
1936                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1937           remainder &= ~temp1;
1938           num_insns++;
1939           i -= 6;
1940         }
1941       i -= 2;
1942     } while (remainder);
1943   return num_insns;
1944 }
1945
1946 /* Emit an instruction with the indicated PATTERN.  If COND is
1947    non-NULL, conditionalize the execution of the instruction on COND
1948    being true.  */
1949
1950 static void
1951 emit_constant_insn (rtx cond, rtx pattern)
1952 {
1953   if (cond)
1954     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1955   emit_insn (pattern);
1956 }
1957
1958 /* As above, but extra parameter GENERATE which, if clear, suppresses
1959    RTL generation.  */
1960 /* ??? This needs more work for thumb2.  */
1961
1962 static int
1963 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1964                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1965                   int generate)
1966 {
1967   int can_invert = 0;
1968   int can_negate = 0;
1969   int can_negate_initial = 0;
1970   int can_shift = 0;
1971   int i;
1972   int num_bits_set = 0;
1973   int set_sign_bit_copies = 0;
1974   int clear_sign_bit_copies = 0;
1975   int clear_zero_bit_copies = 0;
1976   int set_zero_bit_copies = 0;
1977   int insns = 0;
1978   unsigned HOST_WIDE_INT temp1, temp2;
1979   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1980
1981   /* Find out which operations are safe for a given CODE.  Also do a quick
1982      check for degenerate cases; these can occur when DImode operations
1983      are split.  */
1984   switch (code)
1985     {
1986     case SET:
1987       can_invert = 1;
1988       can_shift = 1;
1989       can_negate = 1;
1990       break;
1991
1992     case PLUS:
1993       can_negate = 1;
1994       can_negate_initial = 1;
1995       break;
1996
1997     case IOR:
1998       if (remainder == 0xffffffff)
1999         {
2000           if (generate)
2001             emit_constant_insn (cond,
2002                                 gen_rtx_SET (VOIDmode, target,
2003                                              GEN_INT (ARM_SIGN_EXTEND (val))));
2004           return 1;
2005         }
2006       if (remainder == 0)
2007         {
2008           if (reload_completed && rtx_equal_p (target, source))
2009             return 0;
2010           if (generate)
2011             emit_constant_insn (cond,
2012                                 gen_rtx_SET (VOIDmode, target, source));
2013           return 1;
2014         }
2015       break;
2016
2017     case AND:
2018       if (remainder == 0)
2019         {
2020           if (generate)
2021             emit_constant_insn (cond,
2022                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
2023           return 1;
2024         }
2025       if (remainder == 0xffffffff)
2026         {
2027           if (reload_completed && rtx_equal_p (target, source))
2028             return 0;
2029           if (generate)
2030             emit_constant_insn (cond,
2031                                 gen_rtx_SET (VOIDmode, target, source));
2032           return 1;
2033         }
2034       can_invert = 1;
2035       break;
2036
2037     case XOR:
2038       if (remainder == 0)
2039         {
2040           if (reload_completed && rtx_equal_p (target, source))
2041             return 0;
2042           if (generate)
2043             emit_constant_insn (cond,
2044                                 gen_rtx_SET (VOIDmode, target, source));
2045           return 1;
2046         }
2047
2048       /* We don't know how to handle other cases yet.  */
2049       gcc_assert (remainder == 0xffffffff);
2050
2051       if (generate)
2052         emit_constant_insn (cond,
2053                             gen_rtx_SET (VOIDmode, target,
2054                                          gen_rtx_NOT (mode, source)));
2055       return 1;
2056
2057     case MINUS:
2058       /* We treat MINUS as (val - source), since (source - val) is always
2059          passed as (source + (-val)).  */
2060       if (remainder == 0)
2061         {
2062           if (generate)
2063             emit_constant_insn (cond,
2064                                 gen_rtx_SET (VOIDmode, target,
2065                                              gen_rtx_NEG (mode, source)));
2066           return 1;
2067         }
2068       if (const_ok_for_arm (val))
2069         {
2070           if (generate)
2071             emit_constant_insn (cond,
2072                                 gen_rtx_SET (VOIDmode, target,
2073                                              gen_rtx_MINUS (mode, GEN_INT (val),
2074                                                             source)));
2075           return 1;
2076         }
2077       can_negate = 1;
2078
2079       break;
2080
2081     default:
2082       gcc_unreachable ();
2083     }
2084
2085   /* If we can do it in one insn get out quickly.  */
2086   if (const_ok_for_arm (val)
2087       || (can_negate_initial && const_ok_for_arm (-val))
2088       || (can_invert && const_ok_for_arm (~val)))
2089     {
2090       if (generate)
2091         emit_constant_insn (cond,
2092                             gen_rtx_SET (VOIDmode, target,
2093                                          (source
2094                                           ? gen_rtx_fmt_ee (code, mode, source,
2095                                                             GEN_INT (val))
2096                                           : GEN_INT (val))));
2097       return 1;
2098     }
2099
2100   /* Calculate a few attributes that may be useful for specific
2101      optimizations.  */
2102   for (i = 31; i >= 0; i--)
2103     {
2104       if ((remainder & (1 << i)) == 0)
2105         clear_sign_bit_copies++;
2106       else
2107         break;
2108     }
2109
2110   for (i = 31; i >= 0; i--)
2111     {
2112       if ((remainder & (1 << i)) != 0)
2113         set_sign_bit_copies++;
2114       else
2115         break;
2116     }
2117
2118   for (i = 0; i <= 31; i++)
2119     {
2120       if ((remainder & (1 << i)) == 0)
2121         clear_zero_bit_copies++;
2122       else
2123         break;
2124     }
2125
2126   for (i = 0; i <= 31; i++)
2127     {
2128       if ((remainder & (1 << i)) != 0)
2129         set_zero_bit_copies++;
2130       else
2131         break;
2132     }
2133
2134   switch (code)
2135     {
2136     case SET:
2137       /* See if we can use movw.  */
2138       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2139         {
2140           if (generate)
2141             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2142                                                    GEN_INT (val)));
2143           return 1;
2144         }
2145
2146       /* See if we can do this by sign_extending a constant that is known
2147          to be negative.  This is a good, way of doing it, since the shift
2148          may well merge into a subsequent insn.  */
2149       if (set_sign_bit_copies > 1)
2150         {
2151           if (const_ok_for_arm
2152               (temp1 = ARM_SIGN_EXTEND (remainder
2153                                         << (set_sign_bit_copies - 1))))
2154             {
2155               if (generate)
2156                 {
2157                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2158                   emit_constant_insn (cond,
2159                                       gen_rtx_SET (VOIDmode, new_src,
2160                                                    GEN_INT (temp1)));
2161                   emit_constant_insn (cond,
2162                                       gen_ashrsi3 (target, new_src,
2163                                                    GEN_INT (set_sign_bit_copies - 1)));
2164                 }
2165               return 2;
2166             }
2167           /* For an inverted constant, we will need to set the low bits,
2168              these will be shifted out of harm's way.  */
2169           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2170           if (const_ok_for_arm (~temp1))
2171             {
2172               if (generate)
2173                 {
2174                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2175                   emit_constant_insn (cond,
2176                                       gen_rtx_SET (VOIDmode, new_src,
2177                                                    GEN_INT (temp1)));
2178                   emit_constant_insn (cond,
2179                                       gen_ashrsi3 (target, new_src,
2180                                                    GEN_INT (set_sign_bit_copies - 1)));
2181                 }
2182               return 2;
2183             }
2184         }
2185
2186       /* See if we can calculate the value as the difference between two
2187          valid immediates.  */
2188       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2189         {
2190           int topshift = clear_sign_bit_copies & ~1;
2191
2192           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2193                                    & (0xff000000 >> topshift));
2194
2195           /* If temp1 is zero, then that means the 9 most significant
2196              bits of remainder were 1 and we've caused it to overflow.
2197              When topshift is 0 we don't need to do anything since we
2198              can borrow from 'bit 32'.  */
2199           if (temp1 == 0 && topshift != 0)
2200             temp1 = 0x80000000 >> (topshift - 1);
2201
2202           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2203
2204           if (const_ok_for_arm (temp2))
2205             {
2206               if (generate)
2207                 {
2208                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2209                   emit_constant_insn (cond,
2210                                       gen_rtx_SET (VOIDmode, new_src,
2211                                                    GEN_INT (temp1)));
2212                   emit_constant_insn (cond,
2213                                       gen_addsi3 (target, new_src,
2214                                                   GEN_INT (-temp2)));
2215                 }
2216
2217               return 2;
2218             }
2219         }
2220
2221       /* See if we can generate this by setting the bottom (or the top)
2222          16 bits, and then shifting these into the other half of the
2223          word.  We only look for the simplest cases, to do more would cost
2224          too much.  Be careful, however, not to generate this when the
2225          alternative would take fewer insns.  */
2226       if (val & 0xffff0000)
2227         {
2228           temp1 = remainder & 0xffff0000;
2229           temp2 = remainder & 0x0000ffff;
2230
2231           /* Overlaps outside this range are best done using other methods.  */
2232           for (i = 9; i < 24; i++)
2233             {
2234               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2235                   && !const_ok_for_arm (temp2))
2236                 {
2237                   rtx new_src = (subtargets
2238                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2239                                  : target);
2240                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2241                                             source, subtargets, generate);
2242                   source = new_src;
2243                   if (generate)
2244                     emit_constant_insn
2245                       (cond,
2246                        gen_rtx_SET
2247                        (VOIDmode, target,
2248                         gen_rtx_IOR (mode,
2249                                      gen_rtx_ASHIFT (mode, source,
2250                                                      GEN_INT (i)),
2251                                      source)));
2252                   return insns + 1;
2253                 }
2254             }
2255
2256           /* Don't duplicate cases already considered.  */
2257           for (i = 17; i < 24; i++)
2258             {
2259               if (((temp1 | (temp1 >> i)) == remainder)
2260                   && !const_ok_for_arm (temp1))
2261                 {
2262                   rtx new_src = (subtargets
2263                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2264                                  : target);
2265                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2266                                             source, subtargets, generate);
2267                   source = new_src;
2268                   if (generate)
2269                     emit_constant_insn
2270                       (cond,
2271                        gen_rtx_SET (VOIDmode, target,
2272                                     gen_rtx_IOR
2273                                     (mode,
2274                                      gen_rtx_LSHIFTRT (mode, source,
2275                                                        GEN_INT (i)),
2276                                      source)));
2277                   return insns + 1;
2278                 }
2279             }
2280         }
2281       break;
2282
2283     case IOR:
2284     case XOR:
2285       /* If we have IOR or XOR, and the constant can be loaded in a
2286          single instruction, and we can find a temporary to put it in,
2287          then this can be done in two instructions instead of 3-4.  */
2288       if (subtargets
2289           /* TARGET can't be NULL if SUBTARGETS is 0 */
2290           || (reload_completed && !reg_mentioned_p (target, source)))
2291         {
2292           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2293             {
2294               if (generate)
2295                 {
2296                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2297
2298                   emit_constant_insn (cond,
2299                                       gen_rtx_SET (VOIDmode, sub,
2300                                                    GEN_INT (val)));
2301                   emit_constant_insn (cond,
2302                                       gen_rtx_SET (VOIDmode, target,
2303                                                    gen_rtx_fmt_ee (code, mode,
2304                                                                    source, sub)));
2305                 }
2306               return 2;
2307             }
2308         }
2309
2310       if (code == XOR)
2311         break;
2312
2313       if (set_sign_bit_copies > 8
2314           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2315         {
2316           if (generate)
2317             {
2318               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2319               rtx shift = GEN_INT (set_sign_bit_copies);
2320
2321               emit_constant_insn
2322                 (cond,
2323                  gen_rtx_SET (VOIDmode, sub,
2324                               gen_rtx_NOT (mode,
2325                                            gen_rtx_ASHIFT (mode,
2326                                                            source,
2327                                                            shift))));
2328               emit_constant_insn
2329                 (cond,
2330                  gen_rtx_SET (VOIDmode, target,
2331                               gen_rtx_NOT (mode,
2332                                            gen_rtx_LSHIFTRT (mode, sub,
2333                                                              shift))));
2334             }
2335           return 2;
2336         }
2337
2338       if (set_zero_bit_copies > 8
2339           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2340         {
2341           if (generate)
2342             {
2343               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2344               rtx shift = GEN_INT (set_zero_bit_copies);
2345
2346               emit_constant_insn
2347                 (cond,
2348                  gen_rtx_SET (VOIDmode, sub,
2349                               gen_rtx_NOT (mode,
2350                                            gen_rtx_LSHIFTRT (mode,
2351                                                              source,
2352                                                              shift))));
2353               emit_constant_insn
2354                 (cond,
2355                  gen_rtx_SET (VOIDmode, target,
2356                               gen_rtx_NOT (mode,
2357                                            gen_rtx_ASHIFT (mode, sub,
2358                                                            shift))));
2359             }
2360           return 2;
2361         }
2362
2363       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2364         {
2365           if (generate)
2366             {
2367               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2368               emit_constant_insn (cond,
2369                                   gen_rtx_SET (VOIDmode, sub,
2370                                                gen_rtx_NOT (mode, source)));
2371               source = sub;
2372               if (subtargets)
2373                 sub = gen_reg_rtx (mode);
2374               emit_constant_insn (cond,
2375                                   gen_rtx_SET (VOIDmode, sub,
2376                                                gen_rtx_AND (mode, source,
2377                                                             GEN_INT (temp1))));
2378               emit_constant_insn (cond,
2379                                   gen_rtx_SET (VOIDmode, target,
2380                                                gen_rtx_NOT (mode, sub)));
2381             }
2382           return 3;
2383         }
2384       break;
2385
2386     case AND:
2387       /* See if two shifts will do 2 or more insn's worth of work.  */
2388       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2389         {
2390           HOST_WIDE_INT shift_mask = ((0xffffffff
2391                                        << (32 - clear_sign_bit_copies))
2392                                       & 0xffffffff);
2393
2394           if ((remainder | shift_mask) != 0xffffffff)
2395             {
2396               if (generate)
2397                 {
2398                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2399                   insns = arm_gen_constant (AND, mode, cond,
2400                                             remainder | shift_mask,
2401                                             new_src, source, subtargets, 1);
2402                   source = new_src;
2403                 }
2404               else
2405                 {
2406                   rtx targ = subtargets ? NULL_RTX : target;
2407                   insns = arm_gen_constant (AND, mode, cond,
2408                                             remainder | shift_mask,
2409                                             targ, source, subtargets, 0);
2410                 }
2411             }
2412
2413           if (generate)
2414             {
2415               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2416               rtx shift = GEN_INT (clear_sign_bit_copies);
2417
2418               emit_insn (gen_ashlsi3 (new_src, source, shift));
2419               emit_insn (gen_lshrsi3 (target, new_src, shift));
2420             }
2421
2422           return insns + 2;
2423         }
2424
2425       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2426         {
2427           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2428
2429           if ((remainder | shift_mask) != 0xffffffff)
2430             {
2431               if (generate)
2432                 {
2433                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2434
2435                   insns = arm_gen_constant (AND, mode, cond,
2436                                             remainder | shift_mask,
2437                                             new_src, source, subtargets, 1);
2438                   source = new_src;
2439                 }
2440               else
2441                 {
2442                   rtx targ = subtargets ? NULL_RTX : target;
2443
2444                   insns = arm_gen_constant (AND, mode, cond,
2445                                             remainder | shift_mask,
2446                                             targ, source, subtargets, 0);
2447                 }
2448             }
2449
2450           if (generate)
2451             {
2452               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2453               rtx shift = GEN_INT (clear_zero_bit_copies);
2454
2455               emit_insn (gen_lshrsi3 (new_src, source, shift));
2456               emit_insn (gen_ashlsi3 (target, new_src, shift));
2457             }
2458
2459           return insns + 2;
2460         }
2461
2462       break;
2463
2464     default:
2465       break;
2466     }
2467
2468   for (i = 0; i < 32; i++)
2469     if (remainder & (1 << i))
2470       num_bits_set++;
2471
2472   if (code == AND || (can_invert && num_bits_set > 16))
2473     remainder = (~remainder) & 0xffffffff;
2474   else if (code == PLUS && num_bits_set > 16)
2475     remainder = (-remainder) & 0xffffffff;
2476   else
2477     {
2478       can_invert = 0;
2479       can_negate = 0;
2480     }
2481
2482   /* Now try and find a way of doing the job in either two or three
2483      instructions.
2484      We start by looking for the largest block of zeros that are aligned on
2485      a 2-bit boundary, we then fill up the temps, wrapping around to the
2486      top of the word when we drop off the bottom.
2487      In the worst case this code should produce no more than four insns.
2488      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2489      best place to start.  */
2490
2491   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2492      the same.  */
2493   {
2494     int best_start = 0;
2495     if (!TARGET_THUMB2)
2496       {
2497         int best_consecutive_zeros = 0;
2498
2499         for (i = 0; i < 32; i += 2)
2500           {
2501             int consecutive_zeros = 0;
2502
2503             if (!(remainder & (3 << i)))
2504               {
2505                 while ((i < 32) && !(remainder & (3 << i)))
2506                   {
2507                     consecutive_zeros += 2;
2508                     i += 2;
2509                   }
2510                 if (consecutive_zeros > best_consecutive_zeros)
2511                   {
2512                     best_consecutive_zeros = consecutive_zeros;
2513                     best_start = i - consecutive_zeros;
2514                   }
2515                 i -= 2;
2516               }
2517           }
2518
2519         /* So long as it won't require any more insns to do so, it's
2520            desirable to emit a small constant (in bits 0...9) in the last
2521            insn.  This way there is more chance that it can be combined with
2522            a later addressing insn to form a pre-indexed load or store
2523            operation.  Consider:
2524
2525                    *((volatile int *)0xe0000100) = 1;
2526                    *((volatile int *)0xe0000110) = 2;
2527
2528            We want this to wind up as:
2529
2530                     mov rA, #0xe0000000
2531                     mov rB, #1
2532                     str rB, [rA, #0x100]
2533                     mov rB, #2
2534                     str rB, [rA, #0x110]
2535
2536            rather than having to synthesize both large constants from scratch.
2537
2538            Therefore, we calculate how many insns would be required to emit
2539            the constant starting from `best_start', and also starting from
2540            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2541            yield a shorter sequence, we may as well use zero.  */
2542         if (best_start != 0
2543             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2544             && (count_insns_for_constant (remainder, 0) <=
2545                 count_insns_for_constant (remainder, best_start)))
2546           best_start = 0;
2547       }
2548
2549     /* Now start emitting the insns.  */
2550     i = best_start;
2551     do
2552       {
2553         int end;
2554
2555         if (i <= 0)
2556           i += 32;
2557         if (remainder & (3 << (i - 2)))
2558           {
2559             end = i - 8;
2560             if (end < 0)
2561               end += 32;
2562             temp1 = remainder & ((0x0ff << end)
2563                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2564             remainder &= ~temp1;
2565
2566             if (generate)
2567               {
2568                 rtx new_src, temp1_rtx;
2569
2570                 if (code == SET || code == MINUS)
2571                   {
2572                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2573                     if (can_invert && code != MINUS)
2574                       temp1 = ~temp1;
2575                   }
2576                 else
2577                   {
2578                     if (remainder && subtargets)
2579                       new_src = gen_reg_rtx (mode);
2580                     else
2581                       new_src = target;
2582                     if (can_invert)
2583                       temp1 = ~temp1;
2584                     else if (can_negate)
2585                       temp1 = -temp1;
2586                   }
2587
2588                 temp1 = trunc_int_for_mode (temp1, mode);
2589                 temp1_rtx = GEN_INT (temp1);
2590
2591                 if (code == SET)
2592                   ;
2593                 else if (code == MINUS)
2594                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2595                 else
2596                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2597
2598                 emit_constant_insn (cond,
2599                                     gen_rtx_SET (VOIDmode, new_src,
2600                                                  temp1_rtx));
2601                 source = new_src;
2602               }
2603
2604             if (code == SET)
2605               {
2606                 can_invert = 0;
2607                 code = PLUS;
2608               }
2609             else if (code == MINUS)
2610               code = PLUS;
2611
2612             insns++;
2613             if (TARGET_ARM)
2614               i -= 6;
2615             else
2616               i -= 7;
2617           }
2618         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2619            shifts.  */
2620         if (TARGET_ARM)
2621           i -= 2;
2622         else
2623           i--;
2624       }
2625     while (remainder);
2626   }
2627
2628   return insns;
2629 }
2630
2631 /* Canonicalize a comparison so that we are more likely to recognize it.
2632    This can be done for a few constant compares, where we can make the
2633    immediate value easier to load.  */
2634
2635 enum rtx_code
2636 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2637                              rtx * op1)
2638 {
2639   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2640   unsigned HOST_WIDE_INT maxval;
2641   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2642
2643   switch (code)
2644     {
2645     case EQ:
2646     case NE:
2647       return code;
2648
2649     case GT:
2650     case LE:
2651       if (i != maxval
2652           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2653         {
2654           *op1 = GEN_INT (i + 1);
2655           return code == GT ? GE : LT;
2656         }
2657       break;
2658
2659     case GE:
2660     case LT:
2661       if (i != ~maxval
2662           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2663         {
2664           *op1 = GEN_INT (i - 1);
2665           return code == GE ? GT : LE;
2666         }
2667       break;
2668
2669     case GTU:
2670     case LEU:
2671       if (i != ~((unsigned HOST_WIDE_INT) 0)
2672           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2673         {
2674           *op1 = GEN_INT (i + 1);
2675           return code == GTU ? GEU : LTU;
2676         }
2677       break;
2678
2679     case GEU:
2680     case LTU:
2681       if (i != 0
2682           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2683         {
2684           *op1 = GEN_INT (i - 1);
2685           return code == GEU ? GTU : LEU;
2686         }
2687       break;
2688
2689     default:
2690       gcc_unreachable ();
2691     }
2692
2693   return code;
2694 }
2695
2696
2697 /* Define how to find the value returned by a function.  */
2698
2699 rtx
2700 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2701 {
2702   enum machine_mode mode;
2703   int unsignedp ATTRIBUTE_UNUSED;
2704   rtx r ATTRIBUTE_UNUSED;
2705
2706   mode = TYPE_MODE (type);
2707   /* Promote integer types.  */
2708   if (INTEGRAL_TYPE_P (type))
2709     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2710
2711   /* Promotes small structs returned in a register to full-word size
2712      for big-endian AAPCS.  */
2713   if (arm_return_in_msb (type))
2714     {
2715       HOST_WIDE_INT size = int_size_in_bytes (type);
2716       if (size % UNITS_PER_WORD != 0)
2717         {
2718           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2719           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2720         }
2721     }
2722
2723   return LIBCALL_VALUE(mode);
2724 }
2725
2726 /* Determine the amount of memory needed to store the possible return
2727    registers of an untyped call.  */
2728 int
2729 arm_apply_result_size (void)
2730 {
2731   int size = 16;
2732
2733   if (TARGET_ARM)
2734     {
2735       if (TARGET_HARD_FLOAT_ABI)
2736         {
2737           if (TARGET_FPA)
2738             size += 12;
2739           if (TARGET_MAVERICK)
2740             size += 8;
2741         }
2742       if (TARGET_IWMMXT_ABI)
2743         size += 8;
2744     }
2745
2746   return size;
2747 }
2748
2749 /* Decide whether a type should be returned in memory (true)
2750    or in a register (false).  This is called by the macro
2751    RETURN_IN_MEMORY.  */
2752 int
2753 arm_return_in_memory (tree type)
2754 {
2755   HOST_WIDE_INT size;
2756
2757   if (!AGGREGATE_TYPE_P (type) &&
2758       (TREE_CODE (type) != VECTOR_TYPE) &&
2759       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2760     /* All simple types are returned in registers.
2761        For AAPCS, complex types are treated the same as aggregates.  */
2762     return 0;
2763
2764   size = int_size_in_bytes (type);
2765
2766   if (arm_abi != ARM_ABI_APCS)
2767     {
2768       /* ATPCS and later return aggregate types in memory only if they are
2769          larger than a word (or are variable size).  */
2770       return (size < 0 || size > UNITS_PER_WORD);
2771     }
2772
2773   /* To maximize backwards compatibility with previous versions of gcc,
2774      return vectors up to 4 words in registers.  */
2775   if (TREE_CODE (type) == VECTOR_TYPE)
2776     return (size < 0 || size > (4 * UNITS_PER_WORD));
2777
2778   /* For the arm-wince targets we choose to be compatible with Microsoft's
2779      ARM and Thumb compilers, which always return aggregates in memory.  */
2780 #ifndef ARM_WINCE
2781   /* All structures/unions bigger than one word are returned in memory.
2782      Also catch the case where int_size_in_bytes returns -1.  In this case
2783      the aggregate is either huge or of variable size, and in either case
2784      we will want to return it via memory and not in a register.  */
2785   if (size < 0 || size > UNITS_PER_WORD)
2786     return 1;
2787
2788   if (TREE_CODE (type) == RECORD_TYPE)
2789     {
2790       tree field;
2791
2792       /* For a struct the APCS says that we only return in a register
2793          if the type is 'integer like' and every addressable element
2794          has an offset of zero.  For practical purposes this means
2795          that the structure can have at most one non bit-field element
2796          and that this element must be the first one in the structure.  */
2797
2798       /* Find the first field, ignoring non FIELD_DECL things which will
2799          have been created by C++.  */
2800       for (field = TYPE_FIELDS (type);
2801            field && TREE_CODE (field) != FIELD_DECL;
2802            field = TREE_CHAIN (field))
2803         continue;
2804
2805       if (field == NULL)
2806         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2807
2808       /* Check that the first field is valid for returning in a register.  */
2809
2810       /* ... Floats are not allowed */
2811       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2812         return 1;
2813
2814       /* ... Aggregates that are not themselves valid for returning in
2815          a register are not allowed.  */
2816       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2817         return 1;
2818
2819       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2820          since they are not addressable.  */
2821       for (field = TREE_CHAIN (field);
2822            field;
2823            field = TREE_CHAIN (field))
2824         {
2825           if (TREE_CODE (field) != FIELD_DECL)
2826             continue;
2827
2828           if (!DECL_BIT_FIELD_TYPE (field))
2829             return 1;
2830         }
2831
2832       return 0;
2833     }
2834
2835   if (TREE_CODE (type) == UNION_TYPE)
2836     {
2837       tree field;
2838
2839       /* Unions can be returned in registers if every element is
2840          integral, or can be returned in an integer register.  */
2841       for (field = TYPE_FIELDS (type);
2842            field;
2843            field = TREE_CHAIN (field))
2844         {
2845           if (TREE_CODE (field) != FIELD_DECL)
2846             continue;
2847
2848           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2849             return 1;
2850
2851           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2852             return 1;
2853         }
2854
2855       return 0;
2856     }
2857 #endif /* not ARM_WINCE */
2858
2859   /* Return all other types in memory.  */
2860   return 1;
2861 }
2862
2863 /* Indicate whether or not words of a double are in big-endian order.  */
2864
2865 int
2866 arm_float_words_big_endian (void)
2867 {
2868   if (TARGET_MAVERICK)
2869     return 0;
2870
2871   /* For FPA, float words are always big-endian.  For VFP, floats words
2872      follow the memory system mode.  */
2873
2874   if (TARGET_FPA)
2875     {
2876       return 1;
2877     }
2878
2879   if (TARGET_VFP)
2880     return (TARGET_BIG_END ? 1 : 0);
2881
2882   return 1;
2883 }
2884
2885 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2886    for a call to a function whose data type is FNTYPE.
2887    For a library call, FNTYPE is NULL.  */
2888 void
2889 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2890                           rtx libname  ATTRIBUTE_UNUSED,
2891                           tree fndecl ATTRIBUTE_UNUSED)
2892 {
2893   /* On the ARM, the offset starts at 0.  */
2894   pcum->nregs = 0;
2895   pcum->iwmmxt_nregs = 0;
2896   pcum->can_split = true;
2897
2898   /* Varargs vectors are treated the same as long long.
2899      named_count avoids having to change the way arm handles 'named' */
2900   pcum->named_count = 0;
2901   pcum->nargs = 0;
2902
2903   if (TARGET_REALLY_IWMMXT && fntype)
2904     {
2905       tree fn_arg;
2906
2907       for (fn_arg = TYPE_ARG_TYPES (fntype);
2908            fn_arg;
2909            fn_arg = TREE_CHAIN (fn_arg))
2910         pcum->named_count += 1;
2911
2912       if (! pcum->named_count)
2913         pcum->named_count = INT_MAX;
2914     }
2915 }
2916
2917
2918 /* Return true if mode/type need doubleword alignment.  */
2919 bool
2920 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2921 {
2922   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2923           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2924 }
2925
2926
2927 /* Determine where to put an argument to a function.
2928    Value is zero to push the argument on the stack,
2929    or a hard register in which to store the argument.
2930
2931    MODE is the argument's machine mode.
2932    TYPE is the data type of the argument (as a tree).
2933     This is null for libcalls where that information may
2934     not be available.
2935    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2936     the preceding args and about the function being called.
2937    NAMED is nonzero if this argument is a named parameter
2938     (otherwise it is an extra parameter matching an ellipsis).  */
2939
2940 rtx
2941 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2942                   tree type, int named)
2943 {
2944   int nregs;
2945
2946   /* Varargs vectors are treated the same as long long.
2947      named_count avoids having to change the way arm handles 'named' */
2948   if (TARGET_IWMMXT_ABI
2949       && arm_vector_mode_supported_p (mode)
2950       && pcum->named_count > pcum->nargs + 1)
2951     {
2952       if (pcum->iwmmxt_nregs <= 9)
2953         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2954       else
2955         {
2956           pcum->can_split = false;
2957           return NULL_RTX;
2958         }
2959     }
2960
2961   /* Put doubleword aligned quantities in even register pairs.  */
2962   if (pcum->nregs & 1
2963       && ARM_DOUBLEWORD_ALIGN
2964       && arm_needs_doubleword_align (mode, type))
2965     pcum->nregs++;
2966
2967   if (mode == VOIDmode)
2968     /* Pick an arbitrary value for operand 2 of the call insn.  */
2969     return const0_rtx;
2970
2971   /* Only allow splitting an arg between regs and memory if all preceding
2972      args were allocated to regs.  For args passed by reference we only count
2973      the reference pointer.  */
2974   if (pcum->can_split)
2975     nregs = 1;
2976   else
2977     nregs = ARM_NUM_REGS2 (mode, type);
2978
2979   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2980     return NULL_RTX;
2981
2982   return gen_rtx_REG (mode, pcum->nregs);
2983 }
2984
2985 static int
2986 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2987                        tree type, bool named ATTRIBUTE_UNUSED)
2988 {
2989   int nregs = pcum->nregs;
2990
2991   if (arm_vector_mode_supported_p (mode))
2992     return 0;
2993
2994   if (NUM_ARG_REGS > nregs
2995       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2996       && pcum->can_split)
2997     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2998
2999   return 0;
3000 }
3001
3002 /* Variable sized types are passed by reference.  This is a GCC
3003    extension to the ARM ABI.  */
3004
3005 static bool
3006 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3007                        enum machine_mode mode ATTRIBUTE_UNUSED,
3008                        tree type, bool named ATTRIBUTE_UNUSED)
3009 {
3010   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
3011 }
3012 \f
3013 /* Encode the current state of the #pragma [no_]long_calls.  */
3014 typedef enum
3015 {
3016   OFF,          /* No #pragma [no_]long_calls is in effect.  */
3017   LONG,         /* #pragma long_calls is in effect.  */
3018   SHORT         /* #pragma no_long_calls is in effect.  */
3019 } arm_pragma_enum;
3020
3021 static arm_pragma_enum arm_pragma_long_calls = OFF;
3022
3023 void
3024 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3025 {
3026   arm_pragma_long_calls = LONG;
3027 }
3028
3029 void
3030 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3031 {
3032   arm_pragma_long_calls = SHORT;
3033 }
3034
3035 void
3036 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
3037 {
3038   arm_pragma_long_calls = OFF;
3039 }
3040 \f
3041 /* Table of machine attributes.  */
3042 const struct attribute_spec arm_attribute_table[] =
3043 {
3044   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
3045   /* Function calls made to this symbol must be done indirectly, because
3046      it may lie outside of the 26 bit addressing range of a normal function
3047      call.  */
3048   { "long_call",    0, 0, false, true,  true,  NULL },
3049   /* Whereas these functions are always known to reside within the 26 bit
3050      addressing range.  */
3051   { "short_call",   0, 0, false, true,  true,  NULL },
3052   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
3053   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
3054   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
3055   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
3056 #ifdef ARM_PE
3057   /* ARM/PE has three new attributes:
3058      interfacearm - ?
3059      dllexport - for exporting a function/variable that will live in a dll
3060      dllimport - for importing a function/variable from a dll
3061
3062      Microsoft allows multiple declspecs in one __declspec, separating
3063      them with spaces.  We do NOT support this.  Instead, use __declspec
3064      multiple times.
3065   */
3066   { "dllimport",    0, 0, true,  false, false, NULL },
3067   { "dllexport",    0, 0, true,  false, false, NULL },
3068   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
3069 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
3070   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
3071   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
3072   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
3073 #endif
3074   { NULL,           0, 0, false, false, false, NULL }
3075 };
3076
3077 /* Handle an attribute requiring a FUNCTION_DECL;
3078    arguments as in struct attribute_spec.handler.  */
3079 static tree
3080 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
3081                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
3082 {
3083   if (TREE_CODE (*node) != FUNCTION_DECL)
3084     {
3085       warning (OPT_Wattributes, "%qs attribute only applies to functions",
3086                IDENTIFIER_POINTER (name));
3087       *no_add_attrs = true;
3088     }
3089
3090   return NULL_TREE;
3091 }
3092
3093 /* Handle an "interrupt" or "isr" attribute;
3094    arguments as in struct attribute_spec.handler.  */
3095 static tree
3096 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
3097                           bool *no_add_attrs)
3098 {
3099   if (DECL_P (*node))
3100     {
3101       if (TREE_CODE (*node) != FUNCTION_DECL)
3102         {
3103           warning (OPT_Wattributes, "%qs attribute only applies to functions",
3104                    IDENTIFIER_POINTER (name));
3105           *no_add_attrs = true;
3106         }
3107       /* FIXME: the argument if any is checked for type attributes;
3108          should it be checked for decl ones?  */
3109     }
3110   else
3111     {
3112       if (TREE_CODE (*node) == FUNCTION_TYPE
3113           || TREE_CODE (*node) == METHOD_TYPE)
3114         {
3115           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3116             {
3117               warning (OPT_Wattributes, "%qs attribute ignored",
3118                        IDENTIFIER_POINTER (name));
3119               *no_add_attrs = true;
3120             }
3121         }
3122       else if (TREE_CODE (*node) == POINTER_TYPE
3123                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3124                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3125                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3126         {
3127           *node = build_variant_type_copy (*node);
3128           TREE_TYPE (*node) = build_type_attribute_variant
3129             (TREE_TYPE (*node),
3130              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3131           *no_add_attrs = true;
3132         }
3133       else
3134         {
3135           /* Possibly pass this attribute on from the type to a decl.  */
3136           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3137                        | (int) ATTR_FLAG_FUNCTION_NEXT
3138                        | (int) ATTR_FLAG_ARRAY_NEXT))
3139             {
3140               *no_add_attrs = true;
3141               return tree_cons (name, args, NULL_TREE);
3142             }
3143           else
3144             {
3145               warning (OPT_Wattributes, "%qs attribute ignored",
3146                        IDENTIFIER_POINTER (name));
3147             }
3148         }
3149     }
3150
3151   return NULL_TREE;
3152 }
3153
3154 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3155 /* Handle the "notshared" attribute.  This attribute is another way of
3156    requesting hidden visibility.  ARM's compiler supports
3157    "__declspec(notshared)"; we support the same thing via an
3158    attribute.  */
3159
3160 static tree
3161 arm_handle_notshared_attribute (tree *node,
3162                                 tree name ATTRIBUTE_UNUSED,
3163                                 tree args ATTRIBUTE_UNUSED,
3164                                 int flags ATTRIBUTE_UNUSED,
3165                                 bool *no_add_attrs)
3166 {
3167   tree decl = TYPE_NAME (*node);
3168
3169   if (decl)
3170     {
3171       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3172       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3173       *no_add_attrs = false;
3174     }
3175   return NULL_TREE;
3176 }
3177 #endif
3178
3179 /* Return 0 if the attributes for two types are incompatible, 1 if they
3180    are compatible, and 2 if they are nearly compatible (which causes a
3181    warning to be generated).  */
3182 static int
3183 arm_comp_type_attributes (tree type1, tree type2)
3184 {
3185   int l1, l2, s1, s2;
3186
3187   /* Check for mismatch of non-default calling convention.  */
3188   if (TREE_CODE (type1) != FUNCTION_TYPE)
3189     return 1;
3190
3191   /* Check for mismatched call attributes.  */
3192   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3193   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3194   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3195   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3196
3197   /* Only bother to check if an attribute is defined.  */
3198   if (l1 | l2 | s1 | s2)
3199     {
3200       /* If one type has an attribute, the other must have the same attribute.  */
3201       if ((l1 != l2) || (s1 != s2))
3202         return 0;
3203
3204       /* Disallow mixed attributes.  */
3205       if ((l1 & s2) || (l2 & s1))
3206         return 0;
3207     }
3208
3209   /* Check for mismatched ISR attribute.  */
3210   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3211   if (! l1)
3212     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3213   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3214   if (! l2)
3215     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3216   if (l1 != l2)
3217     return 0;
3218
3219   return 1;
3220 }
3221
3222 /*  Assigns default attributes to newly defined type.  This is used to
3223     set short_call/long_call attributes for function types of
3224     functions defined inside corresponding #pragma scopes.  */
3225 static void
3226 arm_set_default_type_attributes (tree type)
3227 {
3228   /* Add __attribute__ ((long_call)) to all functions, when
3229      inside #pragma long_calls or __attribute__ ((short_call)),
3230      when inside #pragma no_long_calls.  */
3231   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3232     {
3233       tree type_attr_list, attr_name;
3234       type_attr_list = TYPE_ATTRIBUTES (type);
3235
3236       if (arm_pragma_long_calls == LONG)
3237         attr_name = get_identifier ("long_call");
3238       else if (arm_pragma_long_calls == SHORT)
3239         attr_name = get_identifier ("short_call");
3240       else
3241         return;
3242
3243       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3244       TYPE_ATTRIBUTES (type) = type_attr_list;
3245     }
3246 }
3247 \f
3248 /* Return true if DECL is known to be linked into section SECTION.  */
3249
3250 static bool
3251 arm_function_in_section_p (tree decl, section *section)
3252 {
3253   /* We can only be certain about functions defined in the same
3254      compilation unit.  */
3255   if (!TREE_STATIC (decl))
3256     return false;
3257
3258   /* Make sure that SYMBOL always binds to the definition in this
3259      compilation unit.  */
3260   if (!targetm.binds_local_p (decl))
3261     return false;
3262
3263   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3264   if (!DECL_SECTION_NAME (decl))
3265     {
3266       /* Only cater for unit-at-a-time mode, where we know that the user
3267          cannot later specify a section for DECL.  */
3268       if (!flag_unit_at_a_time)
3269         return false;
3270
3271       /* Make sure that we will not create a unique section for DECL.  */
3272       if (flag_function_sections || DECL_ONE_ONLY (decl))
3273         return false;
3274     }
3275
3276   return function_section (decl) == section;
3277 }
3278
3279 /* Return nonzero if a 32-bit "long_call" should be generated for
3280    a call from the current function to DECL.  We generate a long_call
3281    if the function:
3282
3283         a.  has an __attribute__((long call))
3284      or b.  is within the scope of a #pragma long_calls
3285      or c.  the -mlong-calls command line switch has been specified
3286
3287    However we do not generate a long call if the function:
3288
3289         d.  has an __attribute__ ((short_call))
3290      or e.  is inside the scope of a #pragma no_long_calls
3291      or f.  is defined in the same section as the current function.  */
3292
3293 bool
3294 arm_is_long_call_p (tree decl)
3295 {
3296   tree attrs;
3297
3298   if (!decl)
3299     return TARGET_LONG_CALLS;
3300
3301   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3302   if (lookup_attribute ("short_call", attrs))
3303     return false;
3304
3305   /* For "f", be conservative, and only cater for cases in which the
3306      whole of the current function is placed in the same section.  */
3307   if (!flag_reorder_blocks_and_partition
3308       && arm_function_in_section_p (decl, current_function_section ()))
3309     return false;
3310
3311   if (lookup_attribute ("long_call", attrs))
3312     return true;
3313
3314   return TARGET_LONG_CALLS;
3315 }
3316
3317 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3318 static bool
3319 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3320 {
3321   unsigned long func_type;
3322
3323   if (cfun->machine->sibcall_blocked)
3324     return false;
3325
3326   /* Never tailcall something for which we have no decl, or if we
3327      are in Thumb mode.  */
3328   if (decl == NULL || TARGET_THUMB)
3329     return false;
3330
3331   /* The PIC register is live on entry to VxWorks PLT entries, so we
3332      must make the call before restoring the PIC register.  */
3333   if (TARGET_VXWORKS_RTP && flag_pic && !targetm.binds_local_p (decl))
3334     return false;
3335
3336   /* Cannot tail-call to long calls, since these are out of range of
3337      a branch instruction.  */
3338   if (arm_is_long_call_p (decl))
3339     return false;
3340
3341   /* If we are interworking and the function is not declared static
3342      then we can't tail-call it unless we know that it exists in this
3343      compilation unit (since it might be a Thumb routine).  */
3344   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3345     return false;
3346
3347   func_type = arm_current_func_type ();
3348   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3349   if (IS_INTERRUPT (func_type))
3350     return false;
3351
3352   /* Never tailcall if function may be called with a misaligned SP.  */
3353   if (IS_STACKALIGN (func_type))
3354     return false;
3355
3356   /* Everything else is ok.  */
3357   return true;
3358 }
3359
3360 \f
3361 /* Addressing mode support functions.  */
3362
3363 /* Return nonzero if X is a legitimate immediate operand when compiling
3364    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3365 int
3366 legitimate_pic_operand_p (rtx x)
3367 {
3368   if (GET_CODE (x) == SYMBOL_REF
3369       || (GET_CODE (x) == CONST
3370           && GET_CODE (XEXP (x, 0)) == PLUS
3371           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3372     return 0;
3373
3374   return 1;
3375 }
3376
3377 /* Record that the current function needs a PIC register.  Initialize
3378    cfun->machine->pic_reg if we have not already done so.  */
3379
3380 static void
3381 require_pic_register (void)
3382 {
3383   /* A lot of the logic here is made obscure by the fact that this
3384      routine gets called as part of the rtx cost estimation process.
3385      We don't want those calls to affect any assumptions about the real
3386      function; and further, we can't call entry_of_function() until we
3387      start the real expansion process.  */
3388   if (!current_function_uses_pic_offset_table)
3389     {
3390       gcc_assert (!no_new_pseudos);
3391       if (arm_pic_register != INVALID_REGNUM)
3392         {
3393           cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3394
3395           /* Play games to avoid marking the function as needing pic
3396              if we are being called as part of the cost-estimation
3397              process.  */
3398           if (current_ir_type () != IR_GIMPLE)
3399             current_function_uses_pic_offset_table = 1;
3400         }
3401       else
3402         {
3403           rtx seq;
3404
3405           cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3406
3407           /* Play games to avoid marking the function as needing pic
3408              if we are being called as part of the cost-estimation
3409              process.  */
3410           if (current_ir_type () != IR_GIMPLE)
3411             {
3412               current_function_uses_pic_offset_table = 1;
3413               start_sequence ();
3414
3415               arm_load_pic_register (0UL);
3416
3417               seq = get_insns ();
3418               end_sequence ();
3419               emit_insn_after (seq, entry_of_function ());
3420             }
3421         }
3422     }
3423 }
3424
3425 rtx
3426 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3427 {
3428   if (GET_CODE (orig) == SYMBOL_REF
3429       || GET_CODE (orig) == LABEL_REF)
3430     {
3431 #ifndef AOF_ASSEMBLER
3432       rtx pic_ref, address;
3433 #endif
3434       rtx insn;
3435       int subregs = 0;
3436
3437       /* If this function doesn't have a pic register, create one now.  */
3438       require_pic_register ();
3439
3440       if (reg == 0)
3441         {
3442           gcc_assert (!no_new_pseudos);
3443           reg = gen_reg_rtx (Pmode);
3444
3445           subregs = 1;
3446         }
3447
3448 #ifdef AOF_ASSEMBLER
3449       /* The AOF assembler can generate relocations for these directly, and
3450          understands that the PIC register has to be added into the offset.  */
3451       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3452 #else
3453       if (subregs)
3454         address = gen_reg_rtx (Pmode);
3455       else
3456         address = reg;
3457
3458       if (TARGET_ARM)
3459         emit_insn (gen_pic_load_addr_arm (address, orig));
3460       else if (TARGET_THUMB2)
3461         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3462       else /* TARGET_THUMB1 */
3463         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3464
3465       /* VxWorks does not impose a fixed gap between segments; the run-time
3466          gap can be different from the object-file gap.  We therefore can't
3467          use GOTOFF unless we are absolutely sure that the symbol is in the
3468          same segment as the GOT.  Unfortunately, the flexibility of linker
3469          scripts means that we can't be sure of that in general, so assume
3470          that GOTOFF is never valid on VxWorks.  */
3471       if ((GET_CODE (orig) == LABEL_REF
3472            || (GET_CODE (orig) == SYMBOL_REF &&
3473                SYMBOL_REF_LOCAL_P (orig)))
3474           && NEED_GOT_RELOC
3475           && !TARGET_VXWORKS_RTP)
3476         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3477       else
3478         {
3479           pic_ref = gen_const_mem (Pmode,
3480                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3481                                                  address));
3482         }
3483
3484       insn = emit_move_insn (reg, pic_ref);
3485 #endif
3486       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3487          by loop.  */
3488       set_unique_reg_note (insn, REG_EQUAL, orig);
3489
3490       return reg;
3491     }
3492   else if (GET_CODE (orig) == CONST)
3493     {
3494       rtx base, offset;
3495
3496       if (GET_CODE (XEXP (orig, 0)) == PLUS
3497           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3498         return orig;
3499
3500       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3501           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3502         return orig;
3503
3504       if (reg == 0)
3505         {
3506           gcc_assert (!no_new_pseudos);
3507           reg = gen_reg_rtx (Pmode);
3508         }
3509
3510       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3511
3512       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3513       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3514                                        base == reg ? 0 : reg);
3515
3516       if (GET_CODE (offset) == CONST_INT)
3517         {
3518           /* The base register doesn't really matter, we only want to
3519              test the index for the appropriate mode.  */
3520           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3521             {
3522               gcc_assert (!no_new_pseudos);
3523               offset = force_reg (Pmode, offset);
3524             }
3525
3526           if (GET_CODE (offset) == CONST_INT)
3527             return plus_constant (base, INTVAL (offset));
3528         }
3529
3530       if (GET_MODE_SIZE (mode) > 4
3531           && (GET_MODE_CLASS (mode) == MODE_INT
3532               || TARGET_SOFT_FLOAT))
3533         {
3534           emit_insn (gen_addsi3 (reg, base, offset));
3535           return reg;
3536         }
3537
3538       return gen_rtx_PLUS (Pmode, base, offset);
3539     }
3540
3541   return orig;
3542 }
3543
3544
3545 /* Find a spare register to use during the prolog of a function.  */
3546
3547 static int
3548 thumb_find_work_register (unsigned long pushed_regs_mask)
3549 {
3550   int reg;
3551
3552   /* Check the argument registers first as these are call-used.  The
3553      register allocation order means that sometimes r3 might be used
3554      but earlier argument registers might not, so check them all.  */
3555   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3556     if (!df_regs_ever_live_p (reg))
3557       return reg;
3558
3559   /* Before going on to check the call-saved registers we can try a couple
3560      more ways of deducing that r3 is available.  The first is when we are
3561      pushing anonymous arguments onto the stack and we have less than 4
3562      registers worth of fixed arguments(*).  In this case r3 will be part of
3563      the variable argument list and so we can be sure that it will be
3564      pushed right at the start of the function.  Hence it will be available
3565      for the rest of the prologue.
3566      (*): ie current_function_pretend_args_size is greater than 0.  */
3567   if (cfun->machine->uses_anonymous_args
3568       && current_function_pretend_args_size > 0)
3569     return LAST_ARG_REGNUM;
3570
3571   /* The other case is when we have fixed arguments but less than 4 registers
3572      worth.  In this case r3 might be used in the body of the function, but
3573      it is not being used to convey an argument into the function.  In theory
3574      we could just check current_function_args_size to see how many bytes are
3575      being passed in argument registers, but it seems that it is unreliable.
3576      Sometimes it will have the value 0 when in fact arguments are being
3577      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3578      check the args_info.nregs field as well.  The problem with this field is
3579      that it makes no allowances for arguments that are passed to the
3580      function but which are not used.  Hence we could miss an opportunity
3581      when a function has an unused argument in r3.  But it is better to be
3582      safe than to be sorry.  */
3583   if (! cfun->machine->uses_anonymous_args
3584       && current_function_args_size >= 0
3585       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3586       && cfun->args_info.nregs < 4)
3587     return LAST_ARG_REGNUM;
3588
3589   /* Otherwise look for a call-saved register that is going to be pushed.  */
3590   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3591     if (pushed_regs_mask & (1 << reg))
3592       return reg;
3593
3594   if (TARGET_THUMB2)
3595     {
3596       /* Thumb-2 can use high regs.  */
3597       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3598         if (pushed_regs_mask & (1 << reg))
3599           return reg;
3600     }
3601   /* Something went wrong - thumb_compute_save_reg_mask()
3602      should have arranged for a suitable register to be pushed.  */
3603   gcc_unreachable ();
3604 }
3605
3606 static GTY(()) int pic_labelno;
3607
3608 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3609    low register.  */
3610
3611 void
3612 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3613 {
3614 #ifndef AOF_ASSEMBLER
3615   rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx, pic_reg;
3616   rtx global_offset_table;
3617
3618   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3619     return;
3620
3621   gcc_assert (flag_pic);
3622
3623   pic_reg = cfun->machine->pic_reg;
3624   if (TARGET_VXWORKS_RTP)
3625     {
3626       pic_rtx = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE);
3627       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3628       emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3629
3630       emit_insn (gen_rtx_SET (Pmode, pic_reg, gen_rtx_MEM (Pmode, pic_reg)));
3631
3632       pic_tmp = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
3633       emit_insn (gen_pic_offset_arm (pic_reg, pic_reg, pic_tmp));
3634     }
3635   else
3636     {
3637       /* We use an UNSPEC rather than a LABEL_REF because this label
3638          never appears in the code stream.  */
3639
3640       labelno = GEN_INT (pic_labelno++);
3641       l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3642       l1 = gen_rtx_CONST (VOIDmode, l1);
3643
3644       global_offset_table
3645         = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3646       /* On the ARM the PC register contains 'dot + 8' at the time of the
3647          addition, on the Thumb it is 'dot + 4'.  */
3648       pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3649       if (GOT_PCREL)
3650         {
3651           pic_tmp2 = gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx);
3652           pic_tmp2 = gen_rtx_CONST (VOIDmode, pic_tmp2);
3653         }
3654       else
3655         pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3656
3657       pic_rtx = gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp);
3658       pic_rtx = gen_rtx_CONST (Pmode, pic_rtx);
3659
3660       if (TARGET_ARM)
3661         {
3662           emit_insn (gen_pic_load_addr_arm (pic_reg, pic_rtx));
3663           emit_insn (gen_pic_add_dot_plus_eight (pic_reg, pic_reg, labelno));
3664         }
3665       else if (TARGET_THUMB2)
3666         {
3667           /* Thumb-2 only allows very limited access to the PC.  Calculate the
3668              address in a temporary register.  */
3669           if (arm_pic_register != INVALID_REGNUM)
3670             {
3671               pic_tmp = gen_rtx_REG (SImode,
3672                                      thumb_find_work_register (saved_regs));
3673             }
3674           else
3675             {
3676               gcc_assert (!no_new_pseudos);
3677               pic_tmp = gen_reg_rtx (Pmode);
3678             }
3679
3680           emit_insn (gen_pic_load_addr_thumb2 (pic_reg, pic_rtx));
3681           emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3682           emit_insn (gen_addsi3 (pic_reg, pic_reg, pic_tmp));
3683         }
3684       else /* TARGET_THUMB1 */
3685         {
3686           if (arm_pic_register != INVALID_REGNUM
3687               && REGNO (pic_reg) > LAST_LO_REGNUM)
3688             {
3689               /* We will have pushed the pic register, so we should always be
3690                  able to find a work register.  */
3691               pic_tmp = gen_rtx_REG (SImode,
3692                                      thumb_find_work_register (saved_regs));
3693               emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3694               emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3695             }
3696           else
3697             emit_insn (gen_pic_load_addr_thumb1 (pic_reg, pic_rtx));
3698           emit_insn (gen_pic_add_dot_plus_four (pic_reg, pic_reg, labelno));
3699         }
3700     }
3701
3702   /* Need to emit this whether or not we obey regdecls,
3703      since setjmp/longjmp can cause life info to screw up.  */
3704   emit_insn (gen_rtx_USE (VOIDmode, pic_reg));
3705 #endif /* AOF_ASSEMBLER */
3706 }
3707
3708
3709 /* Return nonzero if X is valid as an ARM state addressing register.  */
3710 static int
3711 arm_address_register_rtx_p (rtx x, int strict_p)
3712 {
3713   int regno;
3714
3715   if (GET_CODE (x) != REG)
3716     return 0;
3717
3718   regno = REGNO (x);
3719
3720   if (strict_p)
3721     return ARM_REGNO_OK_FOR_BASE_P (regno);
3722
3723   return (regno <= LAST_ARM_REGNUM
3724           || regno >= FIRST_PSEUDO_REGISTER
3725           || regno == FRAME_POINTER_REGNUM
3726           || regno == ARG_POINTER_REGNUM);
3727 }
3728
3729 /* Return TRUE if this rtx is the difference of a symbol and a label,
3730    and will reduce to a PC-relative relocation in the object file.
3731    Expressions like this can be left alone when generating PIC, rather
3732    than forced through the GOT.  */
3733 static int
3734 pcrel_constant_p (rtx x)
3735 {
3736   if (GET_CODE (x) == MINUS)
3737     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3738
3739   return FALSE;
3740 }
3741
3742 /* Return nonzero if X is a valid ARM state address operand.  */
3743 int
3744 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3745                           int strict_p)
3746 {
3747   bool use_ldrd;
3748   enum rtx_code code = GET_CODE (x);
3749
3750   if (arm_address_register_rtx_p (x, strict_p))
3751     return 1;
3752
3753   use_ldrd = (TARGET_LDRD
3754               && (mode == DImode
3755                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3756
3757   if (code == POST_INC || code == PRE_DEC
3758       || ((code == PRE_INC || code == POST_DEC)
3759           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3760     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3761
3762   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3763            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3764            && GET_CODE (XEXP (x, 1)) == PLUS
3765            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3766     {
3767       rtx addend = XEXP (XEXP (x, 1), 1);
3768
3769       /* Don't allow ldrd post increment by register because it's hard
3770          to fixup invalid register choices.  */
3771       if (use_ldrd
3772           && GET_CODE (x) == POST_MODIFY
3773           && GET_CODE (addend) == REG)
3774         return 0;
3775
3776       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3777               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3778     }
3779
3780   /* After reload constants split into minipools will have addresses
3781      from a LABEL_REF.  */
3782   else if (reload_completed
3783            && (code == LABEL_REF
3784                || (code == CONST
3785                    && GET_CODE (XEXP (x, 0)) == PLUS
3786                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3787                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3788     return 1;
3789
3790   else if (mode == TImode)
3791     return 0;
3792
3793   else if (code == PLUS)
3794     {
3795       rtx xop0 = XEXP (x, 0);
3796       rtx xop1 = XEXP (x, 1);
3797
3798       return ((arm_address_register_rtx_p (xop0, strict_p)
3799                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3800               || (arm_address_register_rtx_p (xop1, strict_p)
3801                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3802     }
3803
3804 #if 0
3805   /* Reload currently can't handle MINUS, so disable this for now */
3806   else if (GET_CODE (x) == MINUS)
3807     {
3808       rtx xop0 = XEXP (x, 0);
3809       rtx xop1 = XEXP (x, 1);
3810
3811       return (arm_address_register_rtx_p (xop0, strict_p)
3812               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3813     }
3814 #endif
3815
3816   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3817            && code == SYMBOL_REF
3818            && CONSTANT_POOL_ADDRESS_P (x)
3819            && ! (flag_pic
3820                  && symbol_mentioned_p (get_pool_constant (x))
3821                  && ! pcrel_constant_p (get_pool_constant (x))))
3822     return 1;
3823
3824   return 0;
3825 }
3826
3827 /* Return nonzero if X is a valid Thumb-2 address operand.  */
3828 int
3829 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3830 {
3831   bool use_ldrd;
3832   enum rtx_code code = GET_CODE (x);
3833   
3834   if (arm_address_register_rtx_p (x, strict_p))
3835     return 1;
3836
3837   use_ldrd = (TARGET_LDRD
3838               && (mode == DImode
3839                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3840
3841   if (code == POST_INC || code == PRE_DEC
3842       || ((code == PRE_INC || code == POST_DEC)
3843           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3844     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3845
3846   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3847            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3848            && GET_CODE (XEXP (x, 1)) == PLUS
3849            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3850     {
3851       /* Thumb-2 only has autoincrement by constant.  */
3852       rtx addend = XEXP (XEXP (x, 1), 1);
3853       HOST_WIDE_INT offset;
3854
3855       if (GET_CODE (addend) != CONST_INT)
3856         return 0;
3857
3858       offset = INTVAL(addend);
3859       if (GET_MODE_SIZE (mode) <= 4)
3860         return (offset > -256 && offset < 256);
3861       
3862       return (use_ldrd && offset > -1024 && offset < 1024
3863               && (offset & 3) == 0);
3864     }
3865
3866   /* After reload constants split into minipools will have addresses
3867      from a LABEL_REF.  */
3868   else if (reload_completed
3869            && (code == LABEL_REF
3870                || (code == CONST
3871                    && GET_CODE (XEXP (x, 0)) == PLUS
3872                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3873                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3874     return 1;
3875
3876   else if (mode == TImode)
3877     return 0;
3878
3879   else if (code == PLUS)
3880     {
3881       rtx xop0 = XEXP (x, 0);
3882       rtx xop1 = XEXP (x, 1);
3883
3884       return ((arm_address_register_rtx_p (xop0, strict_p)
3885                && thumb2_legitimate_index_p (mode, xop1, strict_p))
3886               || (arm_address_register_rtx_p (xop1, strict_p)
3887                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3888     }
3889
3890   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3891            && code == SYMBOL_REF
3892            && CONSTANT_POOL_ADDRESS_P (x)
3893            && ! (flag_pic
3894                  && symbol_mentioned_p (get_pool_constant (x))
3895                  && ! pcrel_constant_p (get_pool_constant (x))))
3896     return 1;
3897
3898   return 0;
3899 }
3900
3901 /* Return nonzero if INDEX is valid for an address index operand in
3902    ARM state.  */
3903 static int
3904 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3905                         int strict_p)
3906 {
3907   HOST_WIDE_INT range;
3908   enum rtx_code code = GET_CODE (index);
3909
3910   /* Standard coprocessor addressing modes.  */
3911   if (TARGET_HARD_FLOAT
3912       && (TARGET_FPA || TARGET_MAVERICK)
3913       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3914           || (TARGET_MAVERICK && mode == DImode)))
3915     return (code == CONST_INT && INTVAL (index) < 1024
3916             && INTVAL (index) > -1024
3917             && (INTVAL (index) & 3) == 0);
3918
3919   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3920     return (code == CONST_INT
3921             && INTVAL (index) < 1024
3922             && INTVAL (index) > -1024
3923             && (INTVAL (index) & 3) == 0);
3924
3925   if (arm_address_register_rtx_p (index, strict_p)
3926       && (GET_MODE_SIZE (mode) <= 4))
3927     return 1;
3928
3929   if (mode == DImode || mode == DFmode)
3930     {
3931       if (code == CONST_INT)
3932         {
3933           HOST_WIDE_INT val = INTVAL (index);
3934
3935           if (TARGET_LDRD)
3936             return val > -256 && val < 256;
3937           else
3938             return val > -4096 && val < 4092;
3939         }
3940
3941       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3942     }
3943
3944   if (GET_MODE_SIZE (mode) <= 4
3945       && ! (arm_arch4
3946             && (mode == HImode
3947                 || (mode == QImode && outer == SIGN_EXTEND))))
3948     {
3949       if (code == MULT)
3950         {
3951           rtx xiop0 = XEXP (index, 0);
3952           rtx xiop1 = XEXP (index, 1);
3953
3954           return ((arm_address_register_rtx_p (xiop0, strict_p)
3955                    && power_of_two_operand (xiop1, SImode))
3956                   || (arm_address_register_rtx_p (xiop1, strict_p)
3957                       && power_of_two_operand (xiop0, SImode)));
3958         }
3959       else if (code == LSHIFTRT || code == ASHIFTRT
3960                || code == ASHIFT || code == ROTATERT)
3961         {
3962           rtx op = XEXP (index, 1);
3963
3964           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3965                   && GET_CODE (op) == CONST_INT
3966                   && INTVAL (op) > 0
3967                   && INTVAL (op) <= 31);
3968         }
3969     }
3970
3971   /* For ARM v4 we may be doing a sign-extend operation during the
3972      load.  */
3973   if (arm_arch4)
3974     {
3975       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3976         range = 256;
3977       else
3978         range = 4096;
3979     }
3980   else
3981     range = (mode == HImode) ? 4095 : 4096;
3982
3983   return (code == CONST_INT
3984           && INTVAL (index) < range
3985           && INTVAL (index) > -range);
3986 }
3987
3988 /* Return true if OP is a valid index scaling factor for Thumb-2 address
3989    index operand.  i.e. 1, 2, 4 or 8.  */
3990 static bool
3991 thumb2_index_mul_operand (rtx op)
3992 {
3993   HOST_WIDE_INT val;
3994   
3995   if (GET_CODE(op) != CONST_INT)
3996     return false;
3997
3998   val = INTVAL(op);
3999   return (val == 1 || val == 2 || val == 4 || val == 8);
4000 }
4001   
4002 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
4003 static int
4004 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
4005 {
4006   enum rtx_code code = GET_CODE (index);
4007
4008   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
4009   /* Standard coprocessor addressing modes.  */
4010   if (TARGET_HARD_FLOAT
4011       && (TARGET_FPA || TARGET_MAVERICK)
4012       && (GET_MODE_CLASS (mode) == MODE_FLOAT
4013           || (TARGET_MAVERICK && mode == DImode)))
4014     return (code == CONST_INT && INTVAL (index) < 1024
4015             && INTVAL (index) > -1024
4016             && (INTVAL (index) & 3) == 0);
4017
4018   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
4019     {
4020       /* For DImode assume values will usually live in core regs
4021          and only allow LDRD addressing modes.  */
4022       if (!TARGET_LDRD || mode != DImode)
4023         return (code == CONST_INT
4024                 && INTVAL (index) < 1024
4025                 && INTVAL (index) > -1024
4026                 && (INTVAL (index) & 3) == 0);
4027     }
4028
4029   if (arm_address_register_rtx_p (index, strict_p)
4030       && (GET_MODE_SIZE (mode) <= 4))
4031     return 1;
4032
4033   if (mode == DImode || mode == DFmode)
4034     {
4035       HOST_WIDE_INT val = INTVAL (index);
4036       /* ??? Can we assume ldrd for thumb2?  */
4037       /* Thumb-2 ldrd only has reg+const addressing modes.  */
4038       if (code != CONST_INT)
4039         return 0;
4040
4041       /* ldrd supports offsets of +-1020.
4042          However the ldr fallback does not.  */
4043       return val > -256 && val < 256 && (val & 3) == 0;
4044     }
4045
4046   if (code == MULT)
4047     {
4048       rtx xiop0 = XEXP (index, 0);
4049       rtx xiop1 = XEXP (index, 1);
4050
4051       return ((arm_address_register_rtx_p (xiop0, strict_p)
4052                && thumb2_index_mul_operand (xiop1))
4053               || (arm_address_register_rtx_p (xiop1, strict_p)
4054                   && thumb2_index_mul_operand (xiop0)));
4055     }
4056   else if (code == ASHIFT)
4057     {
4058       rtx op = XEXP (index, 1);
4059
4060       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
4061               && GET_CODE (op) == CONST_INT
4062               && INTVAL (op) > 0
4063               && INTVAL (op) <= 3);
4064     }
4065
4066   return (code == CONST_INT
4067           && INTVAL (index) < 4096
4068           && INTVAL (index) > -256);
4069 }
4070
4071 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
4072 static int
4073 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
4074 {
4075   int regno;
4076
4077   if (GET_CODE (x) != REG)
4078     return 0;
4079
4080   regno = REGNO (x);
4081
4082   if (strict_p)
4083     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
4084
4085   return (regno <= LAST_LO_REGNUM
4086           || regno > LAST_VIRTUAL_REGISTER
4087           || regno == FRAME_POINTER_REGNUM
4088           || (GET_MODE_SIZE (mode) >= 4
4089               && (regno == STACK_POINTER_REGNUM
4090                   || regno >= FIRST_PSEUDO_REGISTER
4091                   || x == hard_frame_pointer_rtx
4092                   || x == arg_pointer_rtx)));
4093 }
4094
4095 /* Return nonzero if x is a legitimate index register.  This is the case
4096    for any base register that can access a QImode object.  */
4097 inline static int
4098 thumb1_index_register_rtx_p (rtx x, int strict_p)
4099 {
4100   return thumb1_base_register_rtx_p (x, QImode, strict_p);
4101 }
4102
4103 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
4104
4105    The AP may be eliminated to either the SP or the FP, so we use the
4106    least common denominator, e.g. SImode, and offsets from 0 to 64.
4107
4108    ??? Verify whether the above is the right approach.
4109
4110    ??? Also, the FP may be eliminated to the SP, so perhaps that
4111    needs special handling also.
4112
4113    ??? Look at how the mips16 port solves this problem.  It probably uses
4114    better ways to solve some of these problems.
4115
4116    Although it is not incorrect, we don't accept QImode and HImode
4117    addresses based on the frame pointer or arg pointer until the
4118    reload pass starts.  This is so that eliminating such addresses
4119    into stack based ones won't produce impossible code.  */
4120 int
4121 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
4122 {
4123   /* ??? Not clear if this is right.  Experiment.  */
4124   if (GET_MODE_SIZE (mode) < 4
4125       && !(reload_in_progress || reload_completed)
4126       && (reg_mentioned_p (frame_pointer_rtx, x)
4127           || reg_mentioned_p (arg_pointer_rtx, x)
4128           || reg_mentioned_p (virtual_incoming_args_rtx, x)
4129           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
4130           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
4131           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
4132     return 0;
4133
4134   /* Accept any base register.  SP only in SImode or larger.  */
4135   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
4136     return 1;
4137
4138   /* This is PC relative data before arm_reorg runs.  */
4139   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
4140            && GET_CODE (x) == SYMBOL_REF
4141            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
4142     return 1;
4143
4144   /* This is PC relative data after arm_reorg runs.  */
4145   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
4146            && (GET_CODE (x) == LABEL_REF
4147                || (GET_CODE (x) == CONST
4148                    && GET_CODE (XEXP (x, 0)) == PLUS
4149                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4150                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4151     return 1;
4152
4153   /* Post-inc indexing only supported for SImode and larger.  */
4154   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4155            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4156     return 1;
4157
4158   else if (GET_CODE (x) == PLUS)
4159     {
4160       /* REG+REG address can be any two index registers.  */
4161       /* We disallow FRAME+REG addressing since we know that FRAME
4162          will be replaced with STACK, and SP relative addressing only
4163          permits SP+OFFSET.  */
4164       if (GET_MODE_SIZE (mode) <= 4
4165           && XEXP (x, 0) != frame_pointer_rtx
4166           && XEXP (x, 1) != frame_pointer_rtx
4167           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4168           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4169         return 1;
4170
4171       /* REG+const has 5-7 bit offset for non-SP registers.  */
4172       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4173                 || XEXP (x, 0) == arg_pointer_rtx)
4174                && GET_CODE (XEXP (x, 1)) == CONST_INT
4175                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4176         return 1;
4177
4178       /* REG+const has 10-bit offset for SP, but only SImode and
4179          larger is supported.  */
4180       /* ??? Should probably check for DI/DFmode overflow here
4181          just like GO_IF_LEGITIMATE_OFFSET does.  */
4182       else if (GET_CODE (XEXP (x, 0)) == REG
4183                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4184                && GET_MODE_SIZE (mode) >= 4
4185                && GET_CODE (XEXP (x, 1)) == CONST_INT
4186                && INTVAL (XEXP (x, 1)) >= 0
4187                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4188                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4189         return 1;
4190
4191       else if (GET_CODE (XEXP (x, 0)) == REG
4192                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4193                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4194                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4195                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4196                && GET_MODE_SIZE (mode) >= 4
4197                && GET_CODE (XEXP (x, 1)) == CONST_INT
4198                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4199         return 1;
4200     }
4201
4202   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4203            && GET_MODE_SIZE (mode) == 4
4204            && GET_CODE (x) == SYMBOL_REF
4205            && CONSTANT_POOL_ADDRESS_P (x)
4206            && ! (flag_pic
4207                  && symbol_mentioned_p (get_pool_constant (x))
4208                  && ! pcrel_constant_p (get_pool_constant (x))))
4209     return 1;
4210
4211   return 0;
4212 }
4213
4214 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4215    instruction of mode MODE.  */
4216 int
4217 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4218 {
4219   switch (GET_MODE_SIZE (mode))
4220     {
4221     case 1:
4222       return val >= 0 && val < 32;
4223
4224     case 2:
4225       return val >= 0 && val < 64 && (val & 1) == 0;
4226
4227     default:
4228       return (val >= 0
4229               && (val + GET_MODE_SIZE (mode)) <= 128
4230               && (val & 3) == 0);
4231     }
4232 }
4233
4234 /* Build the SYMBOL_REF for __tls_get_addr.  */
4235
4236 static GTY(()) rtx tls_get_addr_libfunc;
4237
4238 static rtx
4239 get_tls_get_addr (void)
4240 {
4241   if (!tls_get_addr_libfunc)
4242     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4243   return tls_get_addr_libfunc;
4244 }
4245
4246 static rtx
4247 arm_load_tp (rtx target)
4248 {
4249   if (!target)
4250     target = gen_reg_rtx (SImode);
4251
4252   if (TARGET_HARD_TP)
4253     {
4254       /* Can return in any reg.  */
4255       emit_insn (gen_load_tp_hard (target));
4256     }
4257   else
4258     {
4259       /* Always returned in r0.  Immediately copy the result into a pseudo,
4260          otherwise other uses of r0 (e.g. setting up function arguments) may
4261          clobber the value.  */
4262
4263       rtx tmp;
4264
4265       emit_insn (gen_load_tp_soft ());
4266
4267       tmp = gen_rtx_REG (SImode, 0);
4268       emit_move_insn (target, tmp);
4269     }
4270   return target;
4271 }
4272
4273 static rtx
4274 load_tls_operand (rtx x, rtx reg)
4275 {
4276   rtx tmp;
4277
4278   if (reg == NULL_RTX)
4279     reg = gen_reg_rtx (SImode);
4280
4281   tmp = gen_rtx_CONST (SImode, x);
4282
4283   emit_move_insn (reg, tmp);
4284
4285   return reg;
4286 }
4287
4288 static rtx
4289 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4290 {
4291   rtx insns, label, labelno, sum;
4292
4293   start_sequence ();
4294
4295   labelno = GEN_INT (pic_labelno++);
4296   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4297   label = gen_rtx_CONST (VOIDmode, label);
4298
4299   sum = gen_rtx_UNSPEC (Pmode,
4300                         gen_rtvec (4, x, GEN_INT (reloc), label,
4301                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4302                         UNSPEC_TLS);
4303   reg = load_tls_operand (sum, reg);
4304
4305   if (TARGET_ARM)
4306     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4307   else if (TARGET_THUMB2)
4308     {
4309       rtx tmp;
4310       /* Thumb-2 only allows very limited access to the PC.  Calculate
4311          the address in a temporary register.  */
4312       tmp = gen_reg_rtx (SImode);
4313       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4314       emit_insn (gen_addsi3(reg, reg, tmp));
4315     }
4316   else /* TARGET_THUMB1 */
4317     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4318
4319   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4320                                      Pmode, 1, reg, Pmode);
4321
4322   insns = get_insns ();
4323   end_sequence ();
4324
4325   return insns;
4326 }
4327
4328 rtx
4329 legitimize_tls_address (rtx x, rtx reg)
4330 {
4331   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4332   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4333
4334   switch (model)
4335     {
4336     case TLS_MODEL_GLOBAL_DYNAMIC:
4337       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4338       dest = gen_reg_rtx (Pmode);
4339       emit_libcall_block (insns, dest, ret, x);
4340       return dest;
4341
4342     case TLS_MODEL_LOCAL_DYNAMIC:
4343       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4344
4345       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4346          share the LDM result with other LD model accesses.  */
4347       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4348                             UNSPEC_TLS);
4349       dest = gen_reg_rtx (Pmode);
4350       emit_libcall_block (insns, dest, ret, eqv);
4351
4352       /* Load the addend.  */
4353       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4354                                UNSPEC_TLS);
4355       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4356       return gen_rtx_PLUS (Pmode, dest, addend);
4357
4358     case TLS_MODEL_INITIAL_EXEC:
4359       labelno = GEN_INT (pic_labelno++);
4360       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4361       label = gen_rtx_CONST (VOIDmode, label);
4362       sum = gen_rtx_UNSPEC (Pmode,
4363                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4364                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4365                             UNSPEC_TLS);
4366       reg = load_tls_operand (sum, reg);
4367
4368       if (TARGET_ARM)
4369         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4370       else if (TARGET_THUMB2)
4371         {
4372           rtx tmp;
4373           /* Thumb-2 only allows very limited access to the PC.  Calculate
4374              the address in a temporary register.  */
4375           tmp = gen_reg_rtx (SImode);
4376           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4377           emit_insn (gen_addsi3(reg, reg, tmp));
4378           emit_move_insn (reg, gen_const_mem (SImode, reg));
4379         }
4380       else
4381         {
4382           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4383           emit_move_insn (reg, gen_const_mem (SImode, reg));
4384         }
4385
4386       tp = arm_load_tp (NULL_RTX);
4387
4388       return gen_rtx_PLUS (Pmode, tp, reg);
4389
4390     case TLS_MODEL_LOCAL_EXEC:
4391       tp = arm_load_tp (NULL_RTX);
4392
4393       reg = gen_rtx_UNSPEC (Pmode,
4394                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4395                             UNSPEC_TLS);
4396       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4397
4398       return gen_rtx_PLUS (Pmode, tp, reg);
4399
4400     default:
4401       abort ();
4402     }
4403 }
4404
4405 /* Try machine-dependent ways of modifying an illegitimate address
4406    to be legitimate.  If we find one, return the new, valid address.  */
4407 rtx
4408 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4409 {
4410   if (arm_tls_symbol_p (x))
4411     return legitimize_tls_address (x, NULL_RTX);
4412
4413   if (GET_CODE (x) == PLUS)
4414     {
4415       rtx xop0 = XEXP (x, 0);
4416       rtx xop1 = XEXP (x, 1);
4417
4418       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4419         xop0 = force_reg (SImode, xop0);
4420
4421       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4422         xop1 = force_reg (SImode, xop1);
4423
4424       if (ARM_BASE_REGISTER_RTX_P (xop0)
4425           && GET_CODE (xop1) == CONST_INT)
4426         {
4427           HOST_WIDE_INT n, low_n;
4428           rtx base_reg, val;
4429           n = INTVAL (xop1);
4430
4431           /* VFP addressing modes actually allow greater offsets, but for
4432              now we just stick with the lowest common denominator.  */
4433           if (mode == DImode
4434               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4435             {
4436               low_n = n & 0x0f;
4437               n &= ~0x0f;
4438               if (low_n > 4)
4439                 {
4440                   n += 16;
4441                   low_n -= 16;
4442                 }
4443             }
4444           else
4445             {
4446               low_n = ((mode) == TImode ? 0
4447                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4448               n -= low_n;
4449             }
4450
4451           base_reg = gen_reg_rtx (SImode);
4452           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4453           emit_move_insn (base_reg, val);
4454           x = plus_constant (base_reg, low_n);
4455         }
4456       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4457         x = gen_rtx_PLUS (SImode, xop0, xop1);
4458     }
4459
4460   /* XXX We don't allow MINUS any more -- see comment in
4461      arm_legitimate_address_p ().  */
4462   else if (GET_CODE (x) == MINUS)
4463     {
4464       rtx xop0 = XEXP (x, 0);
4465       rtx xop1 = XEXP (x, 1);
4466
4467       if (CONSTANT_P (xop0))
4468         xop0 = force_reg (SImode, xop0);
4469
4470       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4471         xop1 = force_reg (SImode, xop1);
4472
4473       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4474         x = gen_rtx_MINUS (SImode, xop0, xop1);
4475     }
4476
4477   /* Make sure to take full advantage of the pre-indexed addressing mode
4478      with absolute addresses which often allows for the base register to
4479      be factorized for multiple adjacent memory references, and it might
4480      even allows for the mini pool to be avoided entirely. */
4481   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4482     {
4483       unsigned int bits;
4484       HOST_WIDE_INT mask, base, index;
4485       rtx base_reg;
4486
4487       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4488          use a 8-bit index. So let's use a 12-bit index for SImode only and
4489          hope that arm_gen_constant will enable ldrb to use more bits. */
4490       bits = (mode == SImode) ? 12 : 8;
4491       mask = (1 << bits) - 1;
4492       base = INTVAL (x) & ~mask;
4493       index = INTVAL (x) & mask;
4494       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4495         {
4496           /* It'll most probably be more efficient to generate the base
4497              with more bits set and use a negative index instead. */
4498           base |= mask;
4499           index -= mask;
4500         }
4501       base_reg = force_reg (SImode, GEN_INT (base));
4502       x = plus_constant (base_reg, index);
4503     }
4504
4505   if (flag_pic)
4506     {
4507       /* We need to find and carefully transform any SYMBOL and LABEL
4508          references; so go back to the original address expression.  */
4509       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4510
4511       if (new_x != orig_x)
4512         x = new_x;
4513     }
4514
4515   return x;
4516 }
4517
4518
4519 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4520    to be legitimate.  If we find one, return the new, valid address.  */
4521 rtx
4522 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4523 {
4524   if (arm_tls_symbol_p (x))
4525     return legitimize_tls_address (x, NULL_RTX);
4526
4527   if (GET_CODE (x) == PLUS
4528       && GET_CODE (XEXP (x, 1)) == CONST_INT
4529       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4530           || INTVAL (XEXP (x, 1)) < 0))
4531     {
4532       rtx xop0 = XEXP (x, 0);
4533       rtx xop1 = XEXP (x, 1);
4534       HOST_WIDE_INT offset = INTVAL (xop1);
4535
4536       /* Try and fold the offset into a biasing of the base register and
4537          then offsetting that.  Don't do this when optimizing for space
4538          since it can cause too many CSEs.  */
4539       if (optimize_size && offset >= 0
4540           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4541         {
4542           HOST_WIDE_INT delta;
4543
4544           if (offset >= 256)
4545             delta = offset - (256 - GET_MODE_SIZE (mode));
4546           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4547             delta = 31 * GET_MODE_SIZE (mode);
4548           else
4549             delta = offset & (~31 * GET_MODE_SIZE (mode));
4550
4551           xop0 = force_operand (plus_constant (xop0, offset - delta),
4552                                 NULL_RTX);
4553           x = plus_constant (xop0, delta);
4554         }
4555       else if (offset < 0 && offset > -256)
4556         /* Small negative offsets are best done with a subtract before the
4557            dereference, forcing these into a register normally takes two
4558            instructions.  */
4559         x = force_operand (x, NULL_RTX);
4560       else
4561         {
4562           /* For the remaining cases, force the constant into a register.  */
4563           xop1 = force_reg (SImode, xop1);
4564           x = gen_rtx_PLUS (SImode, xop0, xop1);
4565         }
4566     }
4567   else if (GET_CODE (x) == PLUS
4568            && s_register_operand (XEXP (x, 1), SImode)
4569            && !s_register_operand (XEXP (x, 0), SImode))
4570     {
4571       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4572
4573       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4574     }
4575
4576   if (flag_pic)
4577     {
4578       /* We need to find and carefully transform any SYMBOL and LABEL
4579          references; so go back to the original address expression.  */
4580       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4581
4582       if (new_x != orig_x)
4583         x = new_x;
4584     }
4585
4586   return x;
4587 }
4588
4589 rtx
4590 thumb_legitimize_reload_address (rtx *x_p,
4591                                  enum machine_mode mode,
4592                                  int opnum, int type,
4593                                  int ind_levels ATTRIBUTE_UNUSED)
4594 {
4595   rtx x = *x_p;
4596
4597   if (GET_CODE (x) == PLUS
4598       && GET_MODE_SIZE (mode) < 4
4599       && REG_P (XEXP (x, 0))
4600       && XEXP (x, 0) == stack_pointer_rtx
4601       && GET_CODE (XEXP (x, 1)) == CONST_INT
4602       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4603     {
4604       rtx orig_x = x;
4605
4606       x = copy_rtx (x);
4607       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4608                    Pmode, VOIDmode, 0, 0, opnum, type);
4609       return x;
4610     }
4611
4612   /* If both registers are hi-regs, then it's better to reload the
4613      entire expression rather than each register individually.  That
4614      only requires one reload register rather than two.  */
4615   if (GET_CODE (x) == PLUS
4616       && REG_P (XEXP (x, 0))
4617       && REG_P (XEXP (x, 1))
4618       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4619       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4620     {
4621       rtx orig_x = x;
4622
4623       x = copy_rtx (x);
4624       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4625                    Pmode, VOIDmode, 0, 0, opnum, type);
4626       return x;
4627     }
4628
4629   return NULL;
4630 }
4631
4632 /* Test for various thread-local symbols.  */
4633
4634 /* Return TRUE if X is a thread-local symbol.  */
4635
4636 static bool
4637 arm_tls_symbol_p (rtx x)
4638 {
4639   if (! TARGET_HAVE_TLS)
4640     return false;
4641
4642   if (GET_CODE (x) != SYMBOL_REF)
4643     return false;
4644
4645   return SYMBOL_REF_TLS_MODEL (x) != 0;
4646 }
4647
4648 /* Helper for arm_tls_referenced_p.  */
4649
4650 static int
4651 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4652 {
4653   if (GET_CODE (*x) == SYMBOL_REF)
4654     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4655
4656   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4657      TLS offsets, not real symbol references.  */
4658   if (GET_CODE (*x) == UNSPEC
4659       && XINT (*x, 1) == UNSPEC_TLS)
4660     return -1;
4661
4662   return 0;
4663 }
4664
4665 /* Return TRUE if X contains any TLS symbol references.  */
4666
4667 bool
4668 arm_tls_referenced_p (rtx x)
4669 {
4670   if (! TARGET_HAVE_TLS)
4671     return false;
4672
4673   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4674 }
4675 \f
4676 #define REG_OR_SUBREG_REG(X)                                            \
4677   (GET_CODE (X) == REG                                                  \
4678    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4679
4680 #define REG_OR_SUBREG_RTX(X)                    \
4681    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4682
4683 #ifndef COSTS_N_INSNS
4684 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4685 #endif
4686 static inline int
4687 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4688 {
4689   enum machine_mode mode = GET_MODE (x);
4690
4691   switch (code)
4692     {
4693     case ASHIFT:
4694     case ASHIFTRT:
4695     case LSHIFTRT:
4696     case ROTATERT:
4697     case PLUS:
4698     case MINUS:
4699     case COMPARE:
4700     case NEG:
4701     case NOT:
4702       return COSTS_N_INSNS (1);
4703
4704     case MULT:
4705       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4706         {
4707           int cycles = 0;
4708           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4709
4710           while (i)
4711             {
4712               i >>= 2;
4713               cycles++;
4714             }
4715           return COSTS_N_INSNS (2) + cycles;
4716         }
4717       return COSTS_N_INSNS (1) + 16;
4718
4719     case SET:
4720       return (COSTS_N_INSNS (1)
4721               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4722                      + GET_CODE (SET_DEST (x)) == MEM));
4723
4724     case CONST_INT:
4725       if (outer == SET)
4726         {
4727           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4728             return 0;
4729           if (thumb_shiftable_const (INTVAL (x)))
4730             return COSTS_N_INSNS (2);
4731           return COSTS_N_INSNS (3);
4732         }
4733       else if ((outer == PLUS || outer == COMPARE)
4734                && INTVAL (x) < 256 && INTVAL (x) > -256)
4735         return 0;
4736       else if (outer == AND
4737                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4738         return COSTS_N_INSNS (1);
4739       else if (outer == ASHIFT || outer == ASHIFTRT
4740                || outer == LSHIFTRT)
4741         return 0;
4742       return COSTS_N_INSNS (2);
4743
4744     case CONST:
4745     case CONST_DOUBLE:
4746     case LABEL_REF:
4747     case SYMBOL_REF:
4748       return COSTS_N_INSNS (3);
4749
4750     case UDIV:
4751     case UMOD:
4752     case DIV:
4753     case MOD:
4754       return 100;
4755
4756     case TRUNCATE:
4757       return 99;
4758
4759     case AND:
4760     case XOR:
4761     case IOR:
4762       /* XXX guess.  */
4763       return 8;
4764
4765     case MEM:
4766       /* XXX another guess.  */
4767       /* Memory costs quite a lot for the first word, but subsequent words
4768          load at the equivalent of a single insn each.  */
4769       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4770               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4771                  ? 4 : 0));
4772
4773     case IF_THEN_ELSE:
4774       /* XXX a guess.  */
4775       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4776         return 14;
4777       return 2;
4778
4779     case ZERO_EXTEND:
4780       /* XXX still guessing.  */
4781       switch (GET_MODE (XEXP (x, 0)))
4782         {
4783         case QImode:
4784           return (1 + (mode == DImode ? 4 : 0)
4785                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4786
4787         case HImode:
4788           return (4 + (mode == DImode ? 4 : 0)
4789                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4790
4791         case SImode:
4792           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4793
4794         default:
4795           return 99;
4796         }
4797
4798     default:
4799       return 99;
4800     }
4801 }
4802
4803
4804 /* Worker routine for arm_rtx_costs.  */
4805 /* ??? This needs updating for thumb2.  */
4806 static inline int
4807 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4808 {
4809   enum machine_mode mode = GET_MODE (x);
4810   enum rtx_code subcode;
4811   int extra_cost;
4812
4813   switch (code)
4814     {
4815     case MEM:
4816       /* Memory costs quite a lot for the first word, but subsequent words
4817          load at the equivalent of a single insn each.  */
4818       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4819               + (GET_CODE (x) == SYMBOL_REF
4820                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4821
4822     case DIV:
4823     case MOD:
4824     case UDIV:
4825     case UMOD:
4826       return optimize_size ? COSTS_N_INSNS (2) : 100;
4827
4828     case ROTATE:
4829       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4830         return 4;
4831       /* Fall through */
4832     case ROTATERT:
4833       if (mode != SImode)
4834         return 8;
4835       /* Fall through */
4836     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4837       if (mode == DImode)
4838         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4839                 + ((GET_CODE (XEXP (x, 0)) == REG
4840                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4841                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4842                    ? 0 : 8));
4843       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4844                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4845                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4846                    ? 0 : 4)
4847               + ((GET_CODE (XEXP (x, 1)) == REG
4848                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4849                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4850                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4851                  ? 0 : 4));
4852
4853     case MINUS:
4854       if (GET_CODE (XEXP (x, 1)) == MULT && mode == SImode && arm_arch_thumb2)
4855         {
4856           extra_cost = rtx_cost (XEXP (x, 1), code);
4857           if (!REG_OR_SUBREG_REG (XEXP (x, 0)))
4858             extra_cost += 4 * ARM_NUM_REGS (mode);
4859           return extra_cost;
4860         }
4861
4862       if (mode == DImode)
4863         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4864                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4865                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4866                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4867                    ? 0 : 8));
4868
4869       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4870         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4871                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4872                           && arm_const_double_rtx (XEXP (x, 1))))
4873                      ? 0 : 8)
4874                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4875                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4876                         && arm_const_double_rtx (XEXP (x, 0))))
4877                    ? 0 : 8));
4878
4879       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4880             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4881             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4882           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4883                || subcode == ASHIFTRT || subcode == LSHIFTRT
4884                || subcode == ROTATE || subcode == ROTATERT
4885                || (subcode == MULT
4886                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4887                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4888                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4889               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4890               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4891                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4892               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4893         return 1;
4894       /* Fall through */
4895
4896     case PLUS:
4897       if (GET_CODE (XEXP (x, 0)) == MULT)
4898         {
4899           extra_cost = rtx_cost (XEXP (x, 0), code);
4900           if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4901             extra_cost += 4 * ARM_NUM_REGS (mode);
4902           return extra_cost;
4903         }
4904
4905       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4906         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4907                 + ((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
4912       /* Fall through */
4913     case AND: case XOR: case IOR:
4914       extra_cost = 0;
4915
4916       /* Normally the frame registers will be spilt into reg+const during
4917          reload, so it is a bad idea to combine them with other instructions,
4918          since then they might not be moved outside of loops.  As a compromise
4919          we allow integration with ops that have a constant as their second
4920          operand.  */
4921       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4922            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4923            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4924           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4925               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4926         extra_cost = 4;
4927
4928       if (mode == DImode)
4929         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4930                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4931                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4932                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4933                    ? 0 : 8));
4934
4935       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4936         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4937                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4938                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4939                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4940                    ? 0 : 4));
4941
4942       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4943         return (1 + extra_cost
4944                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4945                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4946                      || subcode == ROTATE || subcode == ROTATERT
4947                      || (subcode == MULT
4948                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4949                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4950                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4951                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4952                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4953                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4954                    ? 0 : 4));
4955
4956       return 8;
4957
4958     case MULT:
4959       /* This should have been handled by the CPU specific routines.  */
4960       gcc_unreachable ();
4961
4962     case TRUNCATE:
4963       if (arm_arch3m && mode == SImode
4964           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4965           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4966           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4967               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4968           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4969               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4970         return 8;
4971       return 99;
4972
4973     case NEG:
4974       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4975         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4976       /* Fall through */
4977     case NOT:
4978       if (mode == DImode)
4979         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4980
4981       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4982
4983     case IF_THEN_ELSE:
4984       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4985         return 14;
4986       return 2;
4987
4988     case COMPARE:
4989       return 1;
4990
4991     case ABS:
4992       return 4 + (mode == DImode ? 4 : 0);
4993
4994     case SIGN_EXTEND:
4995       /* ??? value extensions are cheaper on armv6. */
4996       if (GET_MODE (XEXP (x, 0)) == QImode)
4997         return (4 + (mode == DImode ? 4 : 0)
4998                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4999       /* Fall through */
5000     case ZERO_EXTEND:
5001       switch (GET_MODE (XEXP (x, 0)))
5002         {
5003         case QImode:
5004           return (1 + (mode == DImode ? 4 : 0)
5005                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5006
5007         case HImode:
5008           return (4 + (mode == DImode ? 4 : 0)
5009                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5010
5011         case SImode:
5012           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
5013
5014         case V8QImode:
5015         case V4HImode:
5016         case V2SImode:
5017         case V4QImode:
5018         case V2HImode:
5019             return 1;
5020
5021         default:
5022           gcc_unreachable ();
5023         }
5024       gcc_unreachable ();
5025
5026     case CONST_INT:
5027       if (const_ok_for_arm (INTVAL (x)))
5028         return outer == SET ? 2 : -1;
5029       else if (outer == AND
5030                && const_ok_for_arm (~INTVAL (x)))
5031         return -1;
5032       else if ((outer == COMPARE
5033                 || outer == PLUS || outer == MINUS)
5034                && const_ok_for_arm (-INTVAL (x)))
5035         return -1;
5036       else
5037         return 5;
5038
5039     case CONST:
5040     case LABEL_REF:
5041     case SYMBOL_REF:
5042       return 6;
5043
5044     case CONST_DOUBLE:
5045       if (arm_const_double_rtx (x) || vfp3_const_double_rtx (x))
5046         return outer == SET ? 2 : -1;
5047       else if ((outer == COMPARE || outer == PLUS)
5048                && neg_const_double_rtx_ok_for_fpa (x))
5049         return -1;
5050       return 7;
5051
5052     default:
5053       return 99;
5054     }
5055 }
5056
5057 /* RTX costs when optimizing for size.  */
5058 static bool
5059 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
5060 {
5061   enum machine_mode mode = GET_MODE (x);
5062
5063   if (TARGET_THUMB)
5064     {
5065       /* XXX TBD.  For now, use the standard costs.  */
5066       *total = thumb1_rtx_costs (x, code, outer_code);
5067       return true;
5068     }
5069
5070   switch (code)
5071     {
5072     case MEM:
5073       /* A memory access costs 1 insn if the mode is small, or the address is
5074          a single register, otherwise it costs one insn per word.  */
5075       if (REG_P (XEXP (x, 0)))
5076         *total = COSTS_N_INSNS (1);
5077       else
5078         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5079       return true;
5080
5081     case DIV:
5082     case MOD:
5083     case UDIV:
5084     case UMOD:
5085       /* Needs a libcall, so it costs about this.  */
5086       *total = COSTS_N_INSNS (2);
5087       return false;
5088
5089     case ROTATE:
5090       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
5091         {
5092           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
5093           return true;
5094         }
5095       /* Fall through */
5096     case ROTATERT:
5097     case ASHIFT:
5098     case LSHIFTRT:
5099     case ASHIFTRT:
5100       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
5101         {
5102           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
5103           return true;
5104         }
5105       else if (mode == SImode)
5106         {
5107           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
5108           /* Slightly disparage register shifts, but not by much.  */
5109           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5110             *total += 1 + rtx_cost (XEXP (x, 1), code);
5111           return true;
5112         }
5113
5114       /* Needs a libcall.  */
5115       *total = COSTS_N_INSNS (2);
5116       return false;
5117
5118     case MINUS:
5119       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5120         {
5121           *total = COSTS_N_INSNS (1);
5122           return false;
5123         }
5124
5125       if (mode == SImode)
5126         {
5127           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
5128           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
5129
5130           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
5131               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
5132               || subcode1 == ROTATE || subcode1 == ROTATERT
5133               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
5134               || subcode1 == ASHIFTRT)
5135             {
5136               /* It's just the cost of the two operands.  */
5137               *total = 0;
5138               return false;
5139             }
5140
5141           *total = COSTS_N_INSNS (1);
5142           return false;
5143         }
5144
5145       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5146       return false;
5147
5148     case PLUS:
5149       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5150         {
5151           *total = COSTS_N_INSNS (1);
5152           return false;
5153         }
5154
5155       /* Fall through */
5156     case AND: case XOR: case IOR:
5157       if (mode == SImode)
5158         {
5159           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5160
5161           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5162               || subcode == LSHIFTRT || subcode == ASHIFTRT
5163               || (code == AND && subcode == NOT))
5164             {
5165               /* It's just the cost of the two operands.  */
5166               *total = 0;
5167               return false;
5168             }
5169         }
5170
5171       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5172       return false;
5173
5174     case MULT:
5175       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5176       return false;
5177
5178     case NEG:
5179       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5180         *total = COSTS_N_INSNS (1);
5181       /* Fall through */
5182     case NOT:
5183       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5184
5185       return false;
5186
5187     case IF_THEN_ELSE:
5188       *total = 0;
5189       return false;
5190
5191     case COMPARE:
5192       if (cc_register (XEXP (x, 0), VOIDmode))
5193         * total = 0;
5194       else
5195         *total = COSTS_N_INSNS (1);
5196       return false;
5197
5198     case ABS:
5199       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5200         *total = COSTS_N_INSNS (1);
5201       else
5202         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5203       return false;
5204
5205     case SIGN_EXTEND:
5206       *total = 0;
5207       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5208         {
5209           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5210             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5211         }
5212       if (mode == DImode)
5213         *total += COSTS_N_INSNS (1);
5214       return false;
5215
5216     case ZERO_EXTEND:
5217       *total = 0;
5218       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5219         {
5220           switch (GET_MODE (XEXP (x, 0)))
5221             {
5222             case QImode:
5223               *total += COSTS_N_INSNS (1);
5224               break;
5225
5226             case HImode:
5227               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5228
5229             case SImode:
5230               break;
5231
5232             default:
5233               *total += COSTS_N_INSNS (2);
5234             }
5235         }
5236
5237       if (mode == DImode)
5238         *total += COSTS_N_INSNS (1);
5239
5240       return false;
5241
5242     case CONST_INT:
5243       if (const_ok_for_arm (INTVAL (x)))
5244         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5245       else if (const_ok_for_arm (~INTVAL (x)))
5246         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5247       else if (const_ok_for_arm (-INTVAL (x)))
5248         {
5249           if (outer_code == COMPARE || outer_code == PLUS
5250               || outer_code == MINUS)
5251             *total = 0;
5252           else
5253             *total = COSTS_N_INSNS (1);
5254         }
5255       else
5256         *total = COSTS_N_INSNS (2);
5257       return true;
5258
5259     case CONST:
5260     case LABEL_REF:
5261     case SYMBOL_REF:
5262       *total = COSTS_N_INSNS (2);
5263       return true;
5264
5265     case CONST_DOUBLE:
5266       *total = COSTS_N_INSNS (4);
5267       return true;
5268
5269     default:
5270       if (mode != VOIDmode)
5271         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5272       else
5273         *total = COSTS_N_INSNS (4); /* How knows?  */
5274       return false;
5275     }
5276 }
5277
5278 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
5279    supported on any "slowmul" cores, so it can be ignored.  */
5280
5281 static bool
5282 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5283 {
5284   enum machine_mode mode = GET_MODE (x);
5285
5286   if (TARGET_THUMB)
5287     {
5288       *total = thumb1_rtx_costs (x, code, outer_code);
5289       return true;
5290     }
5291
5292   switch (code)
5293     {
5294     case MULT:
5295       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5296           || mode == DImode)
5297         {
5298           *total = 30;
5299           return true;
5300         }
5301
5302       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5303         {
5304           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5305                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5306           int cost, const_ok = const_ok_for_arm (i);
5307           int j, booth_unit_size;
5308
5309           /* Tune as appropriate.  */
5310           cost = const_ok ? 4 : 8;
5311           booth_unit_size = 2;
5312           for (j = 0; i && j < 32; j += booth_unit_size)
5313             {
5314               i >>= booth_unit_size;
5315               cost += 2;
5316             }
5317
5318           *total = cost;
5319           return true;
5320         }
5321
5322       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5323                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5324       return true;
5325
5326     default:
5327       *total = arm_rtx_costs_1 (x, code, outer_code);
5328       return true;
5329     }
5330 }
5331
5332
5333 /* RTX cost for cores with a fast multiply unit (M variants).  */
5334
5335 static bool
5336 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5337 {
5338   enum machine_mode mode = GET_MODE (x);
5339
5340   if (TARGET_THUMB1)
5341     {
5342       *total = thumb1_rtx_costs (x, code, outer_code);
5343       return true;
5344     }
5345
5346   /* ??? should thumb2 use different costs?  */
5347   switch (code)
5348     {
5349     case MULT:
5350       /* There is no point basing this on the tuning, since it is always the
5351          fast variant if it exists at all.  */
5352       if (mode == DImode
5353           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5354           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5355               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5356         {
5357           *total = 8;
5358           return true;
5359         }
5360
5361
5362       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5363           || mode == DImode)
5364         {
5365           *total = 30;
5366           return true;
5367         }
5368
5369       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5370         {
5371           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5372                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5373           int cost, const_ok = const_ok_for_arm (i);
5374           int j, booth_unit_size;
5375
5376           /* Tune as appropriate.  */
5377           cost = const_ok ? 4 : 8;
5378           booth_unit_size = 8;
5379           for (j = 0; i && j < 32; j += booth_unit_size)
5380             {
5381               i >>= booth_unit_size;
5382               cost += 2;
5383             }
5384
5385           *total = cost;
5386           return true;
5387         }
5388
5389       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5390                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5391       return true;
5392
5393     default:
5394       *total = arm_rtx_costs_1 (x, code, outer_code);
5395       return true;
5396     }
5397 }
5398
5399
5400 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
5401    so it can be ignored.  */
5402
5403 static bool
5404 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
5405 {
5406   enum machine_mode mode = GET_MODE (x);
5407
5408   if (TARGET_THUMB)
5409     {
5410       *total = thumb1_rtx_costs (x, code, outer_code);
5411       return true;
5412     }
5413
5414   switch (code)
5415     {
5416     case MULT:
5417       /* There is no point basing this on the tuning, since it is always the
5418          fast variant if it exists at all.  */
5419       if (mode == DImode
5420           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5421           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5422               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5423         {
5424           *total = 8;
5425           return true;
5426         }
5427
5428
5429       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5430           || mode == DImode)
5431         {
5432           *total = 30;
5433           return true;
5434         }
5435
5436       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5437         {
5438           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5439                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5440           int cost, const_ok = const_ok_for_arm (i);
5441           unsigned HOST_WIDE_INT masked_const;
5442
5443           /* The cost will be related to two insns.
5444              First a load of the constant (MOV or LDR), then a multiply.  */
5445           cost = 2;
5446           if (! const_ok)
5447             cost += 1;      /* LDR is probably more expensive because
5448                                of longer result latency.  */
5449           masked_const = i & 0xffff8000;
5450           if (masked_const != 0 && masked_const != 0xffff8000)
5451             {
5452               masked_const = i & 0xf8000000;
5453               if (masked_const == 0 || masked_const == 0xf8000000)
5454                 cost += 1;
5455               else
5456                 cost += 2;
5457             }
5458           *total = cost;
5459           return true;
5460         }
5461
5462       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5463                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5464       return true;
5465
5466     case COMPARE:
5467       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5468          will stall until the multiplication is complete.  */
5469       if (GET_CODE (XEXP (x, 0)) == MULT)
5470         *total = 4 + rtx_cost (XEXP (x, 0), code);
5471       else
5472         *total = arm_rtx_costs_1 (x, code, outer_code);
5473       return true;
5474
5475     default:
5476       *total = arm_rtx_costs_1 (x, code, outer_code);
5477       return true;
5478     }
5479 }
5480
5481
5482 /* RTX costs for 9e (and later) cores.  */
5483
5484 static bool
5485 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5486 {
5487   enum machine_mode mode = GET_MODE (x);
5488   int nonreg_cost;
5489   int cost;
5490
5491   if (TARGET_THUMB1)
5492     {
5493       switch (code)
5494         {
5495         case MULT:
5496           *total = COSTS_N_INSNS (3);
5497           return true;
5498
5499         default:
5500           *total = thumb1_rtx_costs (x, code, outer_code);
5501           return true;
5502         }
5503     }
5504
5505   switch (code)
5506     {
5507     case MULT:
5508       /* There is no point basing this on the tuning, since it is always the
5509          fast variant if it exists at all.  */
5510       if (mode == DImode
5511           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5512           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5513               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5514         {
5515           *total = 3;
5516           return true;
5517         }
5518
5519
5520       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5521         {
5522           *total = 30;
5523           return true;
5524         }
5525       if (mode == DImode)
5526         {
5527           cost = 7;
5528           nonreg_cost = 8;
5529         }
5530       else
5531         {
5532           cost = 2;
5533           nonreg_cost = 4;
5534         }
5535
5536
5537       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5538                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5539       return true;
5540
5541     default:
5542       *total = arm_rtx_costs_1 (x, code, outer_code);
5543       return true;
5544     }
5545 }
5546 /* All address computations that can be done are free, but rtx cost returns
5547    the same for practically all of them.  So we weight the different types
5548    of address here in the order (most pref first):
5549    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5550 static inline int
5551 arm_arm_address_cost (rtx x)
5552 {
5553   enum rtx_code c  = GET_CODE (x);
5554
5555   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5556     return 0;
5557   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5558     return 10;
5559
5560   if (c == PLUS || c == MINUS)
5561     {
5562       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5563         return 2;
5564
5565       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5566         return 3;
5567
5568       return 4;
5569     }
5570
5571   return 6;
5572 }
5573
5574 static inline int
5575 arm_thumb_address_cost (rtx x)
5576 {
5577   enum rtx_code c  = GET_CODE (x);
5578
5579   if (c == REG)
5580     return 1;
5581   if (c == PLUS
5582       && GET_CODE (XEXP (x, 0)) == REG
5583       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5584     return 1;
5585
5586   return 2;
5587 }
5588
5589 static int
5590 arm_address_cost (rtx x)
5591 {
5592   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5593 }
5594
5595 static int
5596 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5597 {
5598   rtx i_pat, d_pat;
5599
5600   /* Some true dependencies can have a higher cost depending
5601      on precisely how certain input operands are used.  */
5602   if (arm_tune_xscale
5603       && REG_NOTE_KIND (link) == 0
5604       && recog_memoized (insn) >= 0
5605       && recog_memoized (dep) >= 0)
5606     {
5607       int shift_opnum = get_attr_shift (insn);
5608       enum attr_type attr_type = get_attr_type (dep);
5609
5610       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5611          operand for INSN.  If we have a shifted input operand and the
5612          instruction we depend on is another ALU instruction, then we may
5613          have to account for an additional stall.  */
5614       if (shift_opnum != 0
5615           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5616         {
5617           rtx shifted_operand;
5618           int opno;
5619
5620           /* Get the shifted operand.  */
5621           extract_insn (insn);
5622           shifted_operand = recog_data.operand[shift_opnum];
5623
5624           /* Iterate over all the operands in DEP.  If we write an operand
5625              that overlaps with SHIFTED_OPERAND, then we have increase the
5626              cost of this dependency.  */
5627           extract_insn (dep);
5628           preprocess_constraints ();
5629           for (opno = 0; opno < recog_data.n_operands; opno++)
5630             {
5631               /* We can ignore strict inputs.  */
5632               if (recog_data.operand_type[opno] == OP_IN)
5633                 continue;
5634
5635               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5636                                            shifted_operand))
5637                 return 2;
5638             }
5639         }
5640     }
5641
5642   /* XXX This is not strictly true for the FPA.  */
5643   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5644       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5645     return 0;
5646
5647   /* Call insns don't incur a stall, even if they follow a load.  */
5648   if (REG_NOTE_KIND (link) == 0
5649       && GET_CODE (insn) == CALL_INSN)
5650     return 1;
5651
5652   if ((i_pat = single_set (insn)) != NULL
5653       && GET_CODE (SET_SRC (i_pat)) == MEM
5654       && (d_pat = single_set (dep)) != NULL
5655       && GET_CODE (SET_DEST (d_pat)) == MEM)
5656     {
5657       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5658       /* This is a load after a store, there is no conflict if the load reads
5659          from a cached area.  Assume that loads from the stack, and from the
5660          constant pool are cached, and that others will miss.  This is a
5661          hack.  */
5662
5663       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5664           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5665           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5666           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5667         return 1;
5668     }
5669
5670   return cost;
5671 }
5672
5673 static int fp_consts_inited = 0;
5674
5675 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5676 static const char * const strings_fp[8] =
5677 {
5678   "0",   "1",   "2",   "3",
5679   "4",   "5",   "0.5", "10"
5680 };
5681
5682 static REAL_VALUE_TYPE values_fp[8];
5683
5684 static void
5685 init_fp_table (void)
5686 {
5687   int i;
5688   REAL_VALUE_TYPE r;
5689
5690   if (TARGET_VFP)
5691     fp_consts_inited = 1;
5692   else
5693     fp_consts_inited = 8;
5694
5695   for (i = 0; i < fp_consts_inited; i++)
5696     {
5697       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5698       values_fp[i] = r;
5699     }
5700 }
5701
5702 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5703 int
5704 arm_const_double_rtx (rtx x)
5705 {
5706   REAL_VALUE_TYPE r;
5707   int i;
5708
5709   if (!fp_consts_inited)
5710     init_fp_table ();
5711
5712   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5713   if (REAL_VALUE_MINUS_ZERO (r))
5714     return 0;
5715
5716   for (i = 0; i < fp_consts_inited; i++)
5717     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5718       return 1;
5719
5720   return 0;
5721 }
5722
5723 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5724 int
5725 neg_const_double_rtx_ok_for_fpa (rtx x)
5726 {
5727   REAL_VALUE_TYPE r;
5728   int i;
5729
5730   if (!fp_consts_inited)
5731     init_fp_table ();
5732
5733   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5734   r = REAL_VALUE_NEGATE (r);
5735   if (REAL_VALUE_MINUS_ZERO (r))
5736     return 0;
5737
5738   for (i = 0; i < 8; i++)
5739     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5740       return 1;
5741
5742   return 0;
5743 }
5744
5745
5746 /* VFPv3 has a fairly wide range of representable immediates, formed from
5747    "quarter-precision" floating-point values. These can be evaluated using this
5748    formula (with ^ for exponentiation):
5749
5750      -1^s * n * 2^-r
5751
5752    Where 's' is a sign bit (0/1), 'n' and 'r' are integers such that
5753    16 <= n <= 31 and 0 <= r <= 7.
5754
5755    These values are mapped onto an 8-bit integer ABCDEFGH s.t.
5756
5757      - A (most-significant) is the sign bit.
5758      - BCD are the exponent (encoded as r XOR 3).
5759      - EFGH are the mantissa (encoded as n - 16).
5760 */
5761
5762 /* Return an integer index for a VFPv3 immediate operand X suitable for the
5763    fconst[sd] instruction, or -1 if X isn't suitable.  */
5764 static int
5765 vfp3_const_double_index (rtx x)
5766 {
5767   REAL_VALUE_TYPE r, m;
5768   int sign, exponent;
5769   unsigned HOST_WIDE_INT mantissa, mant_hi;
5770   unsigned HOST_WIDE_INT mask;
5771   HOST_WIDE_INT m1, m2;
5772   int point_pos = 2 * HOST_BITS_PER_WIDE_INT - 1;
5773
5774   if (!TARGET_VFP3 || GET_CODE (x) != CONST_DOUBLE)
5775     return -1;
5776
5777   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5778
5779   /* We can't represent these things, so detect them first.  */
5780   if (REAL_VALUE_ISINF (r) || REAL_VALUE_ISNAN (r) || REAL_VALUE_MINUS_ZERO (r))
5781     return -1;
5782
5783   /* Extract sign, exponent and mantissa.  */
5784   sign = REAL_VALUE_NEGATIVE (r) ? 1 : 0;
5785   r = REAL_VALUE_ABS (r);
5786   exponent = REAL_EXP (&r);
5787   /* For the mantissa, we expand into two HOST_WIDE_INTS, apart from the
5788      highest (sign) bit, with a fixed binary point at bit point_pos.
5789      WARNING: If there's ever a VFP version which uses more than 2 * H_W_I - 1
5790      bits for the mantissa, this may fail (low bits would be lost).  */
5791   real_ldexp (&m, &r, point_pos - exponent);
5792   REAL_VALUE_TO_INT (&m1, &m2, m);
5793   mantissa = m1;
5794   mant_hi = m2;
5795
5796   /* If there are bits set in the low part of the mantissa, we can't
5797      represent this value.  */
5798   if (mantissa != 0)
5799     return -1;
5800
5801   /* Now make it so that mantissa contains the most-significant bits, and move
5802      the point_pos to indicate that the least-significant bits have been
5803      discarded.  */
5804   point_pos -= HOST_BITS_PER_WIDE_INT;
5805   mantissa = mant_hi;
5806
5807   /* We can permit four significant bits of mantissa only, plus a high bit
5808      which is always 1.  */
5809   mask = ((unsigned HOST_WIDE_INT)1 << (point_pos - 5)) - 1;
5810   if ((mantissa & mask) != 0)
5811     return -1;
5812
5813   /* Now we know the mantissa is in range, chop off the unneeded bits.  */
5814   mantissa >>= point_pos - 5;
5815
5816   /* The mantissa may be zero. Disallow that case. (It's possible to load the
5817      floating-point immediate zero with Neon using an integer-zero load, but
5818      that case is handled elsewhere.)  */
5819   if (mantissa == 0)
5820     return -1;
5821
5822   gcc_assert (mantissa >= 16 && mantissa <= 31);
5823
5824   /* The value of 5 here would be 4 if GCC used IEEE754-like encoding (where
5825      normalized significands are in the range [1, 2). (Our mantissa is shifted
5826      left 4 places at this point relative to normalized IEEE754 values).  GCC
5827      internally uses [0.5, 1) (see real.c), so the exponent returned from
5828      REAL_EXP must be altered.  */
5829   exponent = 5 - exponent;
5830
5831   if (exponent < 0 || exponent > 7)
5832     return -1;
5833
5834   /* Sign, mantissa and exponent are now in the correct form to plug into the
5835      formulae described in the comment above.  */
5836   return (sign << 7) | ((exponent ^ 3) << 4) | (mantissa - 16);
5837 }
5838
5839 /* Return TRUE if rtx X is a valid immediate VFPv3 constant.  */
5840 int
5841 vfp3_const_double_rtx (rtx x)
5842 {
5843   if (!TARGET_VFP3)
5844     return 0;
5845
5846   return vfp3_const_double_index (x) != -1;
5847 }
5848
5849 \f
5850 /* Predicates for `match_operand' and `match_operator'.  */
5851
5852 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
5853 int
5854 cirrus_memory_offset (rtx op)
5855 {
5856   /* Reject eliminable registers.  */
5857   if (! (reload_in_progress || reload_completed)
5858       && (   reg_mentioned_p (frame_pointer_rtx, op)
5859           || reg_mentioned_p (arg_pointer_rtx, op)
5860           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5861           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5862           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5863           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5864     return 0;
5865
5866   if (GET_CODE (op) == MEM)
5867     {
5868       rtx ind;
5869
5870       ind = XEXP (op, 0);
5871
5872       /* Match: (mem (reg)).  */
5873       if (GET_CODE (ind) == REG)
5874         return 1;
5875
5876       /* Match:
5877          (mem (plus (reg)
5878                     (const))).  */
5879       if (GET_CODE (ind) == PLUS
5880           && GET_CODE (XEXP (ind, 0)) == REG
5881           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5882           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5883         return 1;
5884     }
5885
5886   return 0;
5887 }
5888
5889 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5890    WB is true if full writeback address modes are allowed and is false
5891    if limited writeback address modes (POST_INC and PRE_DEC) are
5892    allowed.  */
5893
5894 int
5895 arm_coproc_mem_operand (rtx op, bool wb)
5896 {
5897   rtx ind;
5898
5899   /* Reject eliminable registers.  */
5900   if (! (reload_in_progress || reload_completed)
5901       && (   reg_mentioned_p (frame_pointer_rtx, op)
5902           || reg_mentioned_p (arg_pointer_rtx, op)
5903           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5904           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5905           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5906           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5907     return FALSE;
5908
5909   /* Constants are converted into offsets from labels.  */
5910   if (GET_CODE (op) != MEM)
5911     return FALSE;
5912
5913   ind = XEXP (op, 0);
5914
5915   if (reload_completed
5916       && (GET_CODE (ind) == LABEL_REF
5917           || (GET_CODE (ind) == CONST
5918               && GET_CODE (XEXP (ind, 0)) == PLUS
5919               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5920               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5921     return TRUE;
5922
5923   /* Match: (mem (reg)).  */
5924   if (GET_CODE (ind) == REG)
5925     return arm_address_register_rtx_p (ind, 0);
5926
5927   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
5928      acceptable in any case (subject to verification by
5929      arm_address_register_rtx_p).  We need WB to be true to accept
5930      PRE_INC and POST_DEC.  */
5931   if (GET_CODE (ind) == POST_INC
5932       || GET_CODE (ind) == PRE_DEC
5933       || (wb
5934           && (GET_CODE (ind) == PRE_INC
5935               || GET_CODE (ind) == POST_DEC)))
5936     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5937
5938   if (wb
5939       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5940       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5941       && GET_CODE (XEXP (ind, 1)) == PLUS
5942       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5943     ind = XEXP (ind, 1);
5944
5945   /* Match:
5946      (plus (reg)
5947            (const)).  */
5948   if (GET_CODE (ind) == PLUS
5949       && GET_CODE (XEXP (ind, 0)) == REG
5950       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5951       && GET_CODE (XEXP (ind, 1)) == CONST_INT
5952       && INTVAL (XEXP (ind, 1)) > -1024
5953       && INTVAL (XEXP (ind, 1)) <  1024
5954       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5955     return TRUE;
5956
5957   return FALSE;
5958 }
5959
5960 /* Return true if X is a register that will be eliminated later on.  */
5961 int
5962 arm_eliminable_register (rtx x)
5963 {
5964   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5965                        || REGNO (x) == ARG_POINTER_REGNUM
5966                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5967                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5968 }
5969
5970 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5971    coprocessor registers.  Otherwise return NO_REGS.  */
5972
5973 enum reg_class
5974 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
5975 {
5976   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
5977     return NO_REGS;
5978
5979   return GENERAL_REGS;
5980 }
5981
5982 /* Values which must be returned in the most-significant end of the return
5983    register.  */
5984
5985 static bool
5986 arm_return_in_msb (tree valtype)
5987 {
5988   return (TARGET_AAPCS_BASED
5989           && BYTES_BIG_ENDIAN
5990           && (AGGREGATE_TYPE_P (valtype)
5991               || TREE_CODE (valtype) == COMPLEX_TYPE));
5992 }
5993
5994 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5995    Use by the Cirrus Maverick code which has to workaround
5996    a hardware bug triggered by such instructions.  */
5997 static bool
5998 arm_memory_load_p (rtx insn)
5999 {
6000   rtx body, lhs, rhs;;
6001
6002   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
6003     return false;
6004
6005   body = PATTERN (insn);
6006
6007   if (GET_CODE (body) != SET)
6008     return false;
6009
6010   lhs = XEXP (body, 0);
6011   rhs = XEXP (body, 1);
6012
6013   lhs = REG_OR_SUBREG_RTX (lhs);
6014
6015   /* If the destination is not a general purpose
6016      register we do not have to worry.  */
6017   if (GET_CODE (lhs) != REG
6018       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
6019     return false;
6020
6021   /* As well as loads from memory we also have to react
6022      to loads of invalid constants which will be turned
6023      into loads from the minipool.  */
6024   return (GET_CODE (rhs) == MEM
6025           || GET_CODE (rhs) == SYMBOL_REF
6026           || note_invalid_constants (insn, -1, false));
6027 }
6028
6029 /* Return TRUE if INSN is a Cirrus instruction.  */
6030 static bool
6031 arm_cirrus_insn_p (rtx insn)
6032 {
6033   enum attr_cirrus attr;
6034
6035   /* get_attr cannot accept USE or CLOBBER.  */
6036   if (!insn
6037       || GET_CODE (insn) != INSN
6038       || GET_CODE (PATTERN (insn)) == USE
6039       || GET_CODE (PATTERN (insn)) == CLOBBER)
6040     return 0;
6041
6042   attr = get_attr_cirrus (insn);
6043
6044   return attr != CIRRUS_NOT;
6045 }
6046
6047 /* Cirrus reorg for invalid instruction combinations.  */
6048 static void
6049 cirrus_reorg (rtx first)
6050 {
6051   enum attr_cirrus attr;
6052   rtx body = PATTERN (first);
6053   rtx t;
6054   int nops;
6055
6056   /* Any branch must be followed by 2 non Cirrus instructions.  */
6057   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
6058     {
6059       nops = 0;
6060       t = next_nonnote_insn (first);
6061
6062       if (arm_cirrus_insn_p (t))
6063         ++ nops;
6064
6065       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
6066         ++ nops;
6067
6068       while (nops --)
6069         emit_insn_after (gen_nop (), first);
6070
6071       return;
6072     }
6073
6074   /* (float (blah)) is in parallel with a clobber.  */
6075   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
6076     body = XVECEXP (body, 0, 0);
6077
6078   if (GET_CODE (body) == SET)
6079     {
6080       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
6081
6082       /* cfldrd, cfldr64, cfstrd, cfstr64 must
6083          be followed by a non Cirrus insn.  */
6084       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
6085         {
6086           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
6087             emit_insn_after (gen_nop (), first);
6088
6089           return;
6090         }
6091       else if (arm_memory_load_p (first))
6092         {
6093           unsigned int arm_regno;
6094
6095           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
6096              ldr/cfmv64hr combination where the Rd field is the same
6097              in both instructions must be split with a non Cirrus
6098              insn.  Example:
6099
6100              ldr r0, blah
6101              nop
6102              cfmvsr mvf0, r0.  */
6103
6104           /* Get Arm register number for ldr insn.  */
6105           if (GET_CODE (lhs) == REG)
6106             arm_regno = REGNO (lhs);
6107           else
6108             {
6109               gcc_assert (GET_CODE (rhs) == REG);
6110               arm_regno = REGNO (rhs);
6111             }
6112
6113           /* Next insn.  */
6114           first = next_nonnote_insn (first);
6115
6116           if (! arm_cirrus_insn_p (first))
6117             return;
6118
6119           body = PATTERN (first);
6120
6121           /* (float (blah)) is in parallel with a clobber.  */
6122           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
6123             body = XVECEXP (body, 0, 0);
6124
6125           if (GET_CODE (body) == FLOAT)
6126             body = XEXP (body, 0);
6127
6128           if (get_attr_cirrus (first) == CIRRUS_MOVE
6129               && GET_CODE (XEXP (body, 1)) == REG
6130               && arm_regno == REGNO (XEXP (body, 1)))
6131             emit_insn_after (gen_nop (), first);
6132
6133           return;
6134         }
6135     }
6136
6137   /* get_attr cannot accept USE or CLOBBER.  */
6138   if (!first
6139       || GET_CODE (first) != INSN
6140       || GET_CODE (PATTERN (first)) == USE
6141       || GET_CODE (PATTERN (first)) == CLOBBER)
6142     return;
6143
6144   attr = get_attr_cirrus (first);
6145
6146   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
6147      must be followed by a non-coprocessor instruction.  */
6148   if (attr == CIRRUS_COMPARE)
6149     {
6150       nops = 0;
6151
6152       t = next_nonnote_insn (first);
6153
6154       if (arm_cirrus_insn_p (t))
6155         ++ nops;
6156
6157       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
6158         ++ nops;
6159
6160       while (nops --)
6161         emit_insn_after (gen_nop (), first);
6162
6163       return;
6164     }
6165 }
6166
6167 /* Return TRUE if X references a SYMBOL_REF.  */
6168 int
6169 symbol_mentioned_p (rtx x)
6170 {
6171   const char * fmt;
6172   int i;
6173
6174   if (GET_CODE (x) == SYMBOL_REF)
6175     return 1;
6176
6177   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
6178      are constant offsets, not symbols.  */
6179   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6180     return 0;
6181
6182   fmt = GET_RTX_FORMAT (GET_CODE (x));
6183
6184   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6185     {
6186       if (fmt[i] == 'E')
6187         {
6188           int j;
6189
6190           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6191             if (symbol_mentioned_p (XVECEXP (x, i, j)))
6192               return 1;
6193         }
6194       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
6195         return 1;
6196     }
6197
6198   return 0;
6199 }
6200
6201 /* Return TRUE if X references a LABEL_REF.  */
6202 int
6203 label_mentioned_p (rtx x)
6204 {
6205   const char * fmt;
6206   int i;
6207
6208   if (GET_CODE (x) == LABEL_REF)
6209     return 1;
6210
6211   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
6212      instruction, but they are constant offsets, not symbols.  */
6213   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
6214     return 0;
6215
6216   fmt = GET_RTX_FORMAT (GET_CODE (x));
6217   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6218     {
6219       if (fmt[i] == 'E')
6220         {
6221           int j;
6222
6223           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6224             if (label_mentioned_p (XVECEXP (x, i, j)))
6225               return 1;
6226         }
6227       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
6228         return 1;
6229     }
6230
6231   return 0;
6232 }
6233
6234 int
6235 tls_mentioned_p (rtx x)
6236 {
6237   switch (GET_CODE (x))
6238     {
6239     case CONST:
6240       return tls_mentioned_p (XEXP (x, 0));
6241
6242     case UNSPEC:
6243       if (XINT (x, 1) == UNSPEC_TLS)
6244         return 1;
6245
6246     default:
6247       return 0;
6248     }
6249 }
6250
6251 /* Must not copy a SET whose source operand is PC-relative.  */
6252
6253 static bool
6254 arm_cannot_copy_insn_p (rtx insn)
6255 {
6256   rtx pat = PATTERN (insn);
6257
6258   if (GET_CODE (pat) == SET)
6259     {
6260       rtx rhs = SET_SRC (pat);
6261
6262       if (GET_CODE (rhs) == UNSPEC
6263           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
6264         return TRUE;
6265
6266       if (GET_CODE (rhs) == MEM
6267           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
6268           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
6269         return TRUE;
6270     }
6271
6272   return FALSE;
6273 }
6274
6275 enum rtx_code
6276 minmax_code (rtx x)
6277 {
6278   enum rtx_code code = GET_CODE (x);
6279
6280   switch (code)
6281     {
6282     case SMAX:
6283       return GE;
6284     case SMIN:
6285       return LE;
6286     case UMIN:
6287       return LEU;
6288     case UMAX:
6289       return GEU;
6290     default:
6291       gcc_unreachable ();
6292     }
6293 }
6294
6295 /* Return 1 if memory locations are adjacent.  */
6296 int
6297 adjacent_mem_locations (rtx a, rtx b)
6298 {
6299   /* We don't guarantee to preserve the order of these memory refs.  */
6300   if (volatile_refs_p (a) || volatile_refs_p (b))
6301     return 0;
6302
6303   if ((GET_CODE (XEXP (a, 0)) == REG
6304        || (GET_CODE (XEXP (a, 0)) == PLUS
6305            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
6306       && (GET_CODE (XEXP (b, 0)) == REG
6307           || (GET_CODE (XEXP (b, 0)) == PLUS
6308               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
6309     {
6310       HOST_WIDE_INT val0 = 0, val1 = 0;
6311       rtx reg0, reg1;
6312       int val_diff;
6313
6314       if (GET_CODE (XEXP (a, 0)) == PLUS)
6315         {
6316           reg0 = XEXP (XEXP (a, 0), 0);
6317           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
6318         }
6319       else
6320         reg0 = XEXP (a, 0);
6321
6322       if (GET_CODE (XEXP (b, 0)) == PLUS)
6323         {
6324           reg1 = XEXP (XEXP (b, 0), 0);
6325           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
6326         }
6327       else
6328         reg1 = XEXP (b, 0);
6329
6330       /* Don't accept any offset that will require multiple
6331          instructions to handle, since this would cause the
6332          arith_adjacentmem pattern to output an overlong sequence.  */
6333       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
6334         return 0;
6335
6336       /* Don't allow an eliminable register: register elimination can make
6337          the offset too large.  */
6338       if (arm_eliminable_register (reg0))
6339         return 0;
6340
6341       val_diff = val1 - val0;
6342
6343       if (arm_ld_sched)
6344         {
6345           /* If the target has load delay slots, then there's no benefit
6346              to using an ldm instruction unless the offset is zero and
6347              we are optimizing for size.  */
6348           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
6349                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
6350                   && (val_diff == 4 || val_diff == -4));
6351         }
6352
6353       return ((REGNO (reg0) == REGNO (reg1))
6354               && (val_diff == 4 || val_diff == -4));
6355     }
6356
6357   return 0;
6358 }
6359
6360 int
6361 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6362                         HOST_WIDE_INT *load_offset)
6363 {
6364   int unsorted_regs[4];
6365   HOST_WIDE_INT unsorted_offsets[4];
6366   int order[4];
6367   int base_reg = -1;
6368   int i;
6369
6370   /* Can only handle 2, 3, or 4 insns at present,
6371      though could be easily extended if required.  */
6372   gcc_assert (nops >= 2 && nops <= 4);
6373
6374   /* Loop over the operands and check that the memory references are
6375      suitable (i.e. immediate offsets from the same base register).  At
6376      the same time, extract the target register, and the memory
6377      offsets.  */
6378   for (i = 0; i < nops; i++)
6379     {
6380       rtx reg;
6381       rtx offset;
6382
6383       /* Convert a subreg of a mem into the mem itself.  */
6384       if (GET_CODE (operands[nops + i]) == SUBREG)
6385         operands[nops + i] = alter_subreg (operands + (nops + i));
6386
6387       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6388
6389       /* Don't reorder volatile memory references; it doesn't seem worth
6390          looking for the case where the order is ok anyway.  */
6391       if (MEM_VOLATILE_P (operands[nops + i]))
6392         return 0;
6393
6394       offset = const0_rtx;
6395
6396       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6397            || (GET_CODE (reg) == SUBREG
6398                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6399           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6400               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6401                    == REG)
6402                   || (GET_CODE (reg) == SUBREG
6403                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6404               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6405                   == CONST_INT)))
6406         {
6407           if (i == 0)
6408             {
6409               base_reg = REGNO (reg);
6410               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6411                                   ? REGNO (operands[i])
6412                                   : REGNO (SUBREG_REG (operands[i])));
6413               order[0] = 0;
6414             }
6415           else
6416             {
6417               if (base_reg != (int) REGNO (reg))
6418                 /* Not addressed from the same base register.  */
6419                 return 0;
6420
6421               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6422                                   ? REGNO (operands[i])
6423                                   : REGNO (SUBREG_REG (operands[i])));
6424               if (unsorted_regs[i] < unsorted_regs[order[0]])
6425                 order[0] = i;
6426             }
6427
6428           /* If it isn't an integer register, or if it overwrites the
6429              base register but isn't the last insn in the list, then
6430              we can't do this.  */
6431           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
6432               || (i != nops - 1 && unsorted_regs[i] == base_reg))
6433             return 0;
6434
6435           unsorted_offsets[i] = INTVAL (offset);
6436         }
6437       else
6438         /* Not a suitable memory address.  */
6439         return 0;
6440     }
6441
6442   /* All the useful information has now been extracted from the
6443      operands into unsorted_regs and unsorted_offsets; additionally,
6444      order[0] has been set to the lowest numbered register in the
6445      list.  Sort the registers into order, and check that the memory
6446      offsets are ascending and adjacent.  */
6447
6448   for (i = 1; i < nops; i++)
6449     {
6450       int j;
6451
6452       order[i] = order[i - 1];
6453       for (j = 0; j < nops; j++)
6454         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6455             && (order[i] == order[i - 1]
6456                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6457           order[i] = j;
6458
6459       /* Have we found a suitable register? if not, one must be used more
6460          than once.  */
6461       if (order[i] == order[i - 1])
6462         return 0;
6463
6464       /* Is the memory address adjacent and ascending? */
6465       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6466         return 0;
6467     }
6468
6469   if (base)
6470     {
6471       *base = base_reg;
6472
6473       for (i = 0; i < nops; i++)
6474         regs[i] = unsorted_regs[order[i]];
6475
6476       *load_offset = unsorted_offsets[order[0]];
6477     }
6478
6479   if (unsorted_offsets[order[0]] == 0)
6480     return 1; /* ldmia */
6481
6482   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
6483     return 2; /* ldmib */
6484
6485   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
6486     return 3; /* ldmda */
6487
6488   if (unsorted_offsets[order[nops - 1]] == -4)
6489     return 4; /* ldmdb */
6490
6491   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
6492      if the offset isn't small enough.  The reason 2 ldrs are faster
6493      is because these ARMs are able to do more than one cache access
6494      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
6495      whilst the ARM8 has a double bandwidth cache.  This means that
6496      these cores can do both an instruction fetch and a data fetch in
6497      a single cycle, so the trick of calculating the address into a
6498      scratch register (one of the result regs) and then doing a load
6499      multiple actually becomes slower (and no smaller in code size).
6500      That is the transformation
6501
6502         ldr     rd1, [rbase + offset]
6503         ldr     rd2, [rbase + offset + 4]
6504
6505      to
6506
6507         add     rd1, rbase, offset
6508         ldmia   rd1, {rd1, rd2}
6509
6510      produces worse code -- '3 cycles + any stalls on rd2' instead of
6511      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
6512      access per cycle, the first sequence could never complete in less
6513      than 6 cycles, whereas the ldm sequence would only take 5 and
6514      would make better use of sequential accesses if not hitting the
6515      cache.
6516
6517      We cheat here and test 'arm_ld_sched' which we currently know to
6518      only be true for the ARM8, ARM9 and StrongARM.  If this ever
6519      changes, then the test below needs to be reworked.  */
6520   if (nops == 2 && arm_ld_sched)
6521     return 0;
6522
6523   /* Can't do it without setting up the offset, only do this if it takes
6524      no more than one insn.  */
6525   return (const_ok_for_arm (unsorted_offsets[order[0]])
6526           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
6527 }
6528
6529 const char *
6530 emit_ldm_seq (rtx *operands, int nops)
6531 {
6532   int regs[4];
6533   int base_reg;
6534   HOST_WIDE_INT offset;
6535   char buf[100];
6536   int i;
6537
6538   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6539     {
6540     case 1:
6541       strcpy (buf, "ldm%(ia%)\t");
6542       break;
6543
6544     case 2:
6545       strcpy (buf, "ldm%(ib%)\t");
6546       break;
6547
6548     case 3:
6549       strcpy (buf, "ldm%(da%)\t");
6550       break;
6551
6552     case 4:
6553       strcpy (buf, "ldm%(db%)\t");
6554       break;
6555
6556     case 5:
6557       if (offset >= 0)
6558         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6559                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6560                  (long) offset);
6561       else
6562         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6563                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6564                  (long) -offset);
6565       output_asm_insn (buf, operands);
6566       base_reg = regs[0];
6567       strcpy (buf, "ldm%(ia%)\t");
6568       break;
6569
6570     default:
6571       gcc_unreachable ();
6572     }
6573
6574   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6575            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6576
6577   for (i = 1; i < nops; i++)
6578     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6579              reg_names[regs[i]]);
6580
6581   strcat (buf, "}\t%@ phole ldm");
6582
6583   output_asm_insn (buf, operands);
6584   return "";
6585 }
6586
6587 int
6588 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6589                          HOST_WIDE_INT * load_offset)
6590 {
6591   int unsorted_regs[4];
6592   HOST_WIDE_INT unsorted_offsets[4];
6593   int order[4];
6594   int base_reg = -1;
6595   int i;
6596
6597   /* Can only handle 2, 3, or 4 insns at present, though could be easily
6598      extended if required.  */
6599   gcc_assert (nops >= 2 && nops <= 4);
6600
6601   /* Loop over the operands and check that the memory references are
6602      suitable (i.e. immediate offsets from the same base register).  At
6603      the same time, extract the target register, and the memory
6604      offsets.  */
6605   for (i = 0; i < nops; i++)
6606     {
6607       rtx reg;
6608       rtx offset;
6609
6610       /* Convert a subreg of a mem into the mem itself.  */
6611       if (GET_CODE (operands[nops + i]) == SUBREG)
6612         operands[nops + i] = alter_subreg (operands + (nops + i));
6613
6614       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6615
6616       /* Don't reorder volatile memory references; it doesn't seem worth
6617          looking for the case where the order is ok anyway.  */
6618       if (MEM_VOLATILE_P (operands[nops + i]))
6619         return 0;
6620
6621       offset = const0_rtx;
6622
6623       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6624            || (GET_CODE (reg) == SUBREG
6625                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6626           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6627               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6628                    == REG)
6629                   || (GET_CODE (reg) == SUBREG
6630                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6631               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6632                   == CONST_INT)))
6633         {
6634           if (i == 0)
6635             {
6636               base_reg = REGNO (reg);
6637               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6638                                   ? REGNO (operands[i])
6639                                   : REGNO (SUBREG_REG (operands[i])));
6640               order[0] = 0;
6641             }
6642           else
6643             {
6644               if (base_reg != (int) REGNO (reg))
6645                 /* Not addressed from the same base register.  */
6646                 return 0;
6647
6648               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6649                                   ? REGNO (operands[i])
6650                                   : REGNO (SUBREG_REG (operands[i])));
6651               if (unsorted_regs[i] < unsorted_regs[order[0]])
6652                 order[0] = i;
6653             }
6654
6655           /* If it isn't an integer register, then we can't do this.  */
6656           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6657             return 0;
6658
6659           unsorted_offsets[i] = INTVAL (offset);
6660         }
6661       else
6662         /* Not a suitable memory address.  */
6663         return 0;
6664     }
6665
6666   /* All the useful information has now been extracted from the
6667      operands into unsorted_regs and unsorted_offsets; additionally,
6668      order[0] has been set to the lowest numbered register in the
6669      list.  Sort the registers into order, and check that the memory
6670      offsets are ascending and adjacent.  */
6671
6672   for (i = 1; i < nops; i++)
6673     {
6674       int j;
6675
6676       order[i] = order[i - 1];
6677       for (j = 0; j < nops; j++)
6678         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6679             && (order[i] == order[i - 1]
6680                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6681           order[i] = j;
6682
6683       /* Have we found a suitable register? if not, one must be used more
6684          than once.  */
6685       if (order[i] == order[i - 1])
6686         return 0;
6687
6688       /* Is the memory address adjacent and ascending? */
6689       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6690         return 0;
6691     }
6692
6693   if (base)
6694     {
6695       *base = base_reg;
6696
6697       for (i = 0; i < nops; i++)
6698         regs[i] = unsorted_regs[order[i]];
6699
6700       *load_offset = unsorted_offsets[order[0]];
6701     }
6702
6703   if (unsorted_offsets[order[0]] == 0)
6704     return 1; /* stmia */
6705
6706   if (unsorted_offsets[order[0]] == 4)
6707     return 2; /* stmib */
6708
6709   if (unsorted_offsets[order[nops - 1]] == 0)
6710     return 3; /* stmda */
6711
6712   if (unsorted_offsets[order[nops - 1]] == -4)
6713     return 4; /* stmdb */
6714
6715   return 0;
6716 }
6717
6718 const char *
6719 emit_stm_seq (rtx *operands, int nops)
6720 {
6721   int regs[4];
6722   int base_reg;
6723   HOST_WIDE_INT offset;
6724   char buf[100];
6725   int i;
6726
6727   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6728     {
6729     case 1:
6730       strcpy (buf, "stm%(ia%)\t");
6731       break;
6732
6733     case 2:
6734       strcpy (buf, "stm%(ib%)\t");
6735       break;
6736
6737     case 3:
6738       strcpy (buf, "stm%(da%)\t");
6739       break;
6740
6741     case 4:
6742       strcpy (buf, "stm%(db%)\t");
6743       break;
6744
6745     default:
6746       gcc_unreachable ();
6747     }
6748
6749   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6750            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6751
6752   for (i = 1; i < nops; i++)
6753     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6754              reg_names[regs[i]]);
6755
6756   strcat (buf, "}\t%@ phole stm");
6757
6758   output_asm_insn (buf, operands);
6759   return "";
6760 }
6761 \f
6762 /* Routines for use in generating RTL.  */
6763
6764 rtx
6765 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6766                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6767 {
6768   HOST_WIDE_INT offset = *offsetp;
6769   int i = 0, j;
6770   rtx result;
6771   int sign = up ? 1 : -1;
6772   rtx mem, addr;
6773
6774   /* XScale has load-store double instructions, but they have stricter
6775      alignment requirements than load-store multiple, so we cannot
6776      use them.
6777
6778      For XScale ldm requires 2 + NREGS cycles to complete and blocks
6779      the pipeline until completion.
6780
6781         NREGS           CYCLES
6782           1               3
6783           2               4
6784           3               5
6785           4               6
6786
6787      An ldr instruction takes 1-3 cycles, but does not block the
6788      pipeline.
6789
6790         NREGS           CYCLES
6791           1              1-3
6792           2              2-6
6793           3              3-9
6794           4              4-12
6795
6796      Best case ldr will always win.  However, the more ldr instructions
6797      we issue, the less likely we are to be able to schedule them well.
6798      Using ldr instructions also increases code size.
6799
6800      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6801      for counts of 3 or 4 regs.  */
6802   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6803     {
6804       rtx seq;
6805
6806       start_sequence ();
6807
6808       for (i = 0; i < count; i++)
6809         {
6810           addr = plus_constant (from, i * 4 * sign);
6811           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6812           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6813           offset += 4 * sign;
6814         }
6815
6816       if (write_back)
6817         {
6818           emit_move_insn (from, plus_constant (from, count * 4 * sign));
6819           *offsetp = offset;
6820         }
6821
6822       seq = get_insns ();
6823       end_sequence ();
6824
6825       return seq;
6826     }
6827
6828   result = gen_rtx_PARALLEL (VOIDmode,
6829                              rtvec_alloc (count + (write_back ? 1 : 0)));
6830   if (write_back)
6831     {
6832       XVECEXP (result, 0, 0)
6833         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6834       i = 1;
6835       count++;
6836     }
6837
6838   for (j = 0; i < count; i++, j++)
6839     {
6840       addr = plus_constant (from, j * 4 * sign);
6841       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6842       XVECEXP (result, 0, i)
6843         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6844       offset += 4 * sign;
6845     }
6846
6847   if (write_back)
6848     *offsetp = offset;
6849
6850   return result;
6851 }
6852
6853 rtx
6854 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6855                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6856 {
6857   HOST_WIDE_INT offset = *offsetp;
6858   int i = 0, j;
6859   rtx result;
6860   int sign = up ? 1 : -1;
6861   rtx mem, addr;
6862
6863   /* See arm_gen_load_multiple for discussion of
6864      the pros/cons of ldm/stm usage for XScale.  */
6865   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6866     {
6867       rtx seq;
6868
6869       start_sequence ();
6870
6871       for (i = 0; i < count; i++)
6872         {
6873           addr = plus_constant (to, i * 4 * sign);
6874           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6875           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6876           offset += 4 * sign;
6877         }
6878
6879       if (write_back)
6880         {
6881           emit_move_insn (to, plus_constant (to, count * 4 * sign));
6882           *offsetp = offset;
6883         }
6884
6885       seq = get_insns ();
6886       end_sequence ();
6887
6888       return seq;
6889     }
6890
6891   result = gen_rtx_PARALLEL (VOIDmode,
6892                              rtvec_alloc (count + (write_back ? 1 : 0)));
6893   if (write_back)
6894     {
6895       XVECEXP (result, 0, 0)
6896         = gen_rtx_SET (VOIDmode, to,
6897                        plus_constant (to, count * 4 * sign));
6898       i = 1;
6899       count++;
6900     }
6901
6902   for (j = 0; i < count; i++, j++)
6903     {
6904       addr = plus_constant (to, j * 4 * sign);
6905       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6906       XVECEXP (result, 0, i)
6907         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6908       offset += 4 * sign;
6909     }
6910
6911   if (write_back)
6912     *offsetp = offset;
6913
6914   return result;
6915 }
6916
6917 int
6918 arm_gen_movmemqi (rtx *operands)
6919 {
6920   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6921   HOST_WIDE_INT srcoffset, dstoffset;
6922   int i;
6923   rtx src, dst, srcbase, dstbase;
6924   rtx part_bytes_reg = NULL;
6925   rtx mem;
6926
6927   if (GET_CODE (operands[2]) != CONST_INT
6928       || GET_CODE (operands[3]) != CONST_INT
6929       || INTVAL (operands[2]) > 64
6930       || INTVAL (operands[3]) & 3)
6931     return 0;
6932
6933   dstbase = operands[0];
6934   srcbase = operands[1];
6935
6936   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6937   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6938
6939   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6940   out_words_to_go = INTVAL (operands[2]) / 4;
6941   last_bytes = INTVAL (operands[2]) & 3;
6942   dstoffset = srcoffset = 0;
6943
6944   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6945     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6946
6947   for (i = 0; in_words_to_go >= 2; i+=4)
6948     {
6949       if (in_words_to_go > 4)
6950         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6951                                           srcbase, &srcoffset));
6952       else
6953         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6954                                           FALSE, srcbase, &srcoffset));
6955
6956       if (out_words_to_go)
6957         {
6958           if (out_words_to_go > 4)
6959             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6960                                                dstbase, &dstoffset));
6961           else if (out_words_to_go != 1)
6962             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6963                                                dst, TRUE,
6964                                                (last_bytes == 0
6965                                                 ? FALSE : TRUE),
6966                                                dstbase, &dstoffset));
6967           else
6968             {
6969               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6970               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6971               if (last_bytes != 0)
6972                 {
6973                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6974                   dstoffset += 4;
6975                 }
6976             }
6977         }
6978
6979       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6980       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6981     }
6982
6983   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6984   if (out_words_to_go)
6985     {
6986       rtx sreg;
6987
6988       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6989       sreg = copy_to_reg (mem);
6990
6991       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6992       emit_move_insn (mem, sreg);
6993       in_words_to_go--;
6994
6995       gcc_assert (!in_words_to_go);     /* Sanity check */
6996     }
6997
6998   if (in_words_to_go)
6999     {
7000       gcc_assert (in_words_to_go > 0);
7001
7002       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
7003       part_bytes_reg = copy_to_mode_reg (SImode, mem);
7004     }
7005
7006   gcc_assert (!last_bytes || part_bytes_reg);
7007
7008   if (BYTES_BIG_ENDIAN && last_bytes)
7009     {
7010       rtx tmp = gen_reg_rtx (SImode);
7011
7012       /* The bytes we want are in the top end of the word.  */
7013       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
7014                               GEN_INT (8 * (4 - last_bytes))));
7015       part_bytes_reg = tmp;
7016
7017       while (last_bytes)
7018         {
7019           mem = adjust_automodify_address (dstbase, QImode,
7020                                            plus_constant (dst, last_bytes - 1),
7021                                            dstoffset + last_bytes - 1);
7022           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7023
7024           if (--last_bytes)
7025             {
7026               tmp = gen_reg_rtx (SImode);
7027               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
7028               part_bytes_reg = tmp;
7029             }
7030         }
7031
7032     }
7033   else
7034     {
7035       if (last_bytes > 1)
7036         {
7037           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
7038           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
7039           last_bytes -= 2;
7040           if (last_bytes)
7041             {
7042               rtx tmp = gen_reg_rtx (SImode);
7043               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
7044               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
7045               part_bytes_reg = tmp;
7046               dstoffset += 2;
7047             }
7048         }
7049
7050       if (last_bytes)
7051         {
7052           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
7053           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
7054         }
7055     }
7056
7057   return 1;
7058 }
7059
7060 /* Select a dominance comparison mode if possible for a test of the general
7061    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
7062    COND_OR == DOM_CC_X_AND_Y => (X && Y)
7063    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
7064    COND_OR == DOM_CC_X_OR_Y => (X || Y)
7065    In all cases OP will be either EQ or NE, but we don't need to know which
7066    here.  If we are unable to support a dominance comparison we return
7067    CC mode.  This will then fail to match for the RTL expressions that
7068    generate this call.  */
7069 enum machine_mode
7070 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
7071 {
7072   enum rtx_code cond1, cond2;
7073   int swapped = 0;
7074
7075   /* Currently we will probably get the wrong result if the individual
7076      comparisons are not simple.  This also ensures that it is safe to
7077      reverse a comparison if necessary.  */
7078   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
7079        != CCmode)
7080       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
7081           != CCmode))
7082     return CCmode;
7083
7084   /* The if_then_else variant of this tests the second condition if the
7085      first passes, but is true if the first fails.  Reverse the first
7086      condition to get a true "inclusive-or" expression.  */
7087   if (cond_or == DOM_CC_NX_OR_Y)
7088     cond1 = reverse_condition (cond1);
7089
7090   /* If the comparisons are not equal, and one doesn't dominate the other,
7091      then we can't do this.  */
7092   if (cond1 != cond2
7093       && !comparison_dominates_p (cond1, cond2)
7094       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
7095     return CCmode;
7096
7097   if (swapped)
7098     {
7099       enum rtx_code temp = cond1;
7100       cond1 = cond2;
7101       cond2 = temp;
7102     }
7103
7104   switch (cond1)
7105     {
7106     case EQ:
7107       if (cond_or == DOM_CC_X_AND_Y)
7108         return CC_DEQmode;
7109
7110       switch (cond2)
7111         {
7112         case EQ: return CC_DEQmode;
7113         case LE: return CC_DLEmode;
7114         case LEU: return CC_DLEUmode;
7115         case GE: return CC_DGEmode;
7116         case GEU: return CC_DGEUmode;
7117         default: gcc_unreachable ();
7118         }
7119
7120     case LT:
7121       if (cond_or == DOM_CC_X_AND_Y)
7122         return CC_DLTmode;
7123
7124       switch (cond2)
7125         {
7126         case  LT:
7127             return CC_DLTmode;
7128         case LE:
7129           return CC_DLEmode;
7130         case NE:
7131           return CC_DNEmode;
7132         default:
7133           gcc_unreachable ();
7134         }
7135
7136     case GT:
7137       if (cond_or == DOM_CC_X_AND_Y)
7138         return CC_DGTmode;
7139
7140       switch (cond2)
7141         {
7142         case GT:
7143           return CC_DGTmode;
7144         case GE:
7145           return CC_DGEmode;
7146         case NE:
7147           return CC_DNEmode;
7148         default:
7149           gcc_unreachable ();
7150         }
7151
7152     case LTU:
7153       if (cond_or == DOM_CC_X_AND_Y)
7154         return CC_DLTUmode;
7155
7156       switch (cond2)
7157         {
7158         case LTU:
7159           return CC_DLTUmode;
7160         case LEU:
7161           return CC_DLEUmode;
7162         case NE:
7163           return CC_DNEmode;
7164         default:
7165           gcc_unreachable ();
7166         }
7167
7168     case GTU:
7169       if (cond_or == DOM_CC_X_AND_Y)
7170         return CC_DGTUmode;
7171
7172       switch (cond2)
7173         {
7174         case GTU:
7175           return CC_DGTUmode;
7176         case GEU:
7177           return CC_DGEUmode;
7178         case NE:
7179           return CC_DNEmode;
7180         default:
7181           gcc_unreachable ();
7182         }
7183
7184     /* The remaining cases only occur when both comparisons are the
7185        same.  */
7186     case NE:
7187       gcc_assert (cond1 == cond2);
7188       return CC_DNEmode;
7189
7190     case LE:
7191       gcc_assert (cond1 == cond2);
7192       return CC_DLEmode;
7193
7194     case GE:
7195       gcc_assert (cond1 == cond2);
7196       return CC_DGEmode;
7197
7198     case LEU:
7199       gcc_assert (cond1 == cond2);
7200       return CC_DLEUmode;
7201
7202     case GEU:
7203       gcc_assert (cond1 == cond2);
7204       return CC_DGEUmode;
7205
7206     default:
7207       gcc_unreachable ();
7208     }
7209 }
7210
7211 enum machine_mode
7212 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
7213 {
7214   /* All floating point compares return CCFP if it is an equality
7215      comparison, and CCFPE otherwise.  */
7216   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
7217     {
7218       switch (op)
7219         {
7220         case EQ:
7221         case NE:
7222         case UNORDERED:
7223         case ORDERED:
7224         case UNLT:
7225         case UNLE:
7226         case UNGT:
7227         case UNGE:
7228         case UNEQ:
7229         case LTGT:
7230           return CCFPmode;
7231
7232         case LT:
7233         case LE:
7234         case GT:
7235         case GE:
7236           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
7237             return CCFPmode;
7238           return CCFPEmode;
7239
7240         default:
7241           gcc_unreachable ();
7242         }
7243     }
7244
7245   /* A compare with a shifted operand.  Because of canonicalization, the
7246      comparison will have to be swapped when we emit the assembler.  */
7247   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
7248       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7249           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
7250           || GET_CODE (x) == ROTATERT))
7251     return CC_SWPmode;
7252
7253   /* This operation is performed swapped, but since we only rely on the Z
7254      flag we don't need an additional mode.  */
7255   if (GET_MODE (y) == SImode && REG_P (y)
7256       && GET_CODE (x) == NEG
7257       && (op == EQ || op == NE))
7258     return CC_Zmode;
7259
7260   /* This is a special case that is used by combine to allow a
7261      comparison of a shifted byte load to be split into a zero-extend
7262      followed by a comparison of the shifted integer (only valid for
7263      equalities and unsigned inequalities).  */
7264   if (GET_MODE (x) == SImode
7265       && GET_CODE (x) == ASHIFT
7266       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
7267       && GET_CODE (XEXP (x, 0)) == SUBREG
7268       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
7269       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
7270       && (op == EQ || op == NE
7271           || op == GEU || op == GTU || op == LTU || op == LEU)
7272       && GET_CODE (y) == CONST_INT)
7273     return CC_Zmode;
7274
7275   /* A construct for a conditional compare, if the false arm contains
7276      0, then both conditions must be true, otherwise either condition
7277      must be true.  Not all conditions are possible, so CCmode is
7278      returned if it can't be done.  */
7279   if (GET_CODE (x) == IF_THEN_ELSE
7280       && (XEXP (x, 2) == const0_rtx
7281           || XEXP (x, 2) == const1_rtx)
7282       && COMPARISON_P (XEXP (x, 0))
7283       && COMPARISON_P (XEXP (x, 1)))
7284     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7285                                          INTVAL (XEXP (x, 2)));
7286
7287   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
7288   if (GET_CODE (x) == AND
7289       && COMPARISON_P (XEXP (x, 0))
7290       && COMPARISON_P (XEXP (x, 1)))
7291     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7292                                          DOM_CC_X_AND_Y);
7293
7294   if (GET_CODE (x) == IOR
7295       && COMPARISON_P (XEXP (x, 0))
7296       && COMPARISON_P (XEXP (x, 1)))
7297     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7298                                          DOM_CC_X_OR_Y);
7299
7300   /* An operation (on Thumb) where we want to test for a single bit.
7301      This is done by shifting that bit up into the top bit of a
7302      scratch register; we can then branch on the sign bit.  */
7303   if (TARGET_THUMB1
7304       && GET_MODE (x) == SImode
7305       && (op == EQ || op == NE)
7306       && GET_CODE (x) == ZERO_EXTRACT
7307       && XEXP (x, 1) == const1_rtx)
7308     return CC_Nmode;
7309
7310   /* An operation that sets the condition codes as a side-effect, the
7311      V flag is not set correctly, so we can only use comparisons where
7312      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
7313      instead.)  */
7314   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
7315   if (GET_MODE (x) == SImode
7316       && y == const0_rtx
7317       && (op == EQ || op == NE || op == LT || op == GE)
7318       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
7319           || GET_CODE (x) == AND || GET_CODE (x) == IOR
7320           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
7321           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
7322           || GET_CODE (x) == LSHIFTRT
7323           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7324           || GET_CODE (x) == ROTATERT
7325           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
7326     return CC_NOOVmode;
7327
7328   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
7329     return CC_Zmode;
7330
7331   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
7332       && GET_CODE (x) == PLUS
7333       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
7334     return CC_Cmode;
7335
7336   return CCmode;
7337 }
7338
7339 /* X and Y are two things to compare using CODE.  Emit the compare insn and
7340    return the rtx for register 0 in the proper mode.  FP means this is a
7341    floating point compare: I don't think that it is needed on the arm.  */
7342 rtx
7343 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
7344 {
7345   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
7346   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
7347
7348   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
7349
7350   return cc_reg;
7351 }
7352
7353 /* Generate a sequence of insns that will generate the correct return
7354    address mask depending on the physical architecture that the program
7355    is running on.  */
7356 rtx
7357 arm_gen_return_addr_mask (void)
7358 {
7359   rtx reg = gen_reg_rtx (Pmode);
7360
7361   emit_insn (gen_return_addr_mask (reg));
7362   return reg;
7363 }
7364
7365 void
7366 arm_reload_in_hi (rtx *operands)
7367 {
7368   rtx ref = operands[1];
7369   rtx base, scratch;
7370   HOST_WIDE_INT offset = 0;
7371
7372   if (GET_CODE (ref) == SUBREG)
7373     {
7374       offset = SUBREG_BYTE (ref);
7375       ref = SUBREG_REG (ref);
7376     }
7377
7378   if (GET_CODE (ref) == REG)
7379     {
7380       /* We have a pseudo which has been spilt onto the stack; there
7381          are two cases here: the first where there is a simple
7382          stack-slot replacement and a second where the stack-slot is
7383          out of range, or is used as a subreg.  */
7384       if (reg_equiv_mem[REGNO (ref)])
7385         {
7386           ref = reg_equiv_mem[REGNO (ref)];
7387           base = find_replacement (&XEXP (ref, 0));
7388         }
7389       else
7390         /* The slot is out of range, or was dressed up in a SUBREG.  */
7391         base = reg_equiv_address[REGNO (ref)];
7392     }
7393   else
7394     base = find_replacement (&XEXP (ref, 0));
7395
7396   /* Handle the case where the address is too complex to be offset by 1.  */
7397   if (GET_CODE (base) == MINUS
7398       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7399     {
7400       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7401
7402       emit_set_insn (base_plus, base);
7403       base = base_plus;
7404     }
7405   else if (GET_CODE (base) == PLUS)
7406     {
7407       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7408       HOST_WIDE_INT hi, lo;
7409
7410       offset += INTVAL (XEXP (base, 1));
7411       base = XEXP (base, 0);
7412
7413       /* Rework the address into a legal sequence of insns.  */
7414       /* Valid range for lo is -4095 -> 4095 */
7415       lo = (offset >= 0
7416             ? (offset & 0xfff)
7417             : -((-offset) & 0xfff));
7418
7419       /* Corner case, if lo is the max offset then we would be out of range
7420          once we have added the additional 1 below, so bump the msb into the
7421          pre-loading insn(s).  */
7422       if (lo == 4095)
7423         lo &= 0x7ff;
7424
7425       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7426              ^ (HOST_WIDE_INT) 0x80000000)
7427             - (HOST_WIDE_INT) 0x80000000);
7428
7429       gcc_assert (hi + lo == offset);
7430
7431       if (hi != 0)
7432         {
7433           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7434
7435           /* Get the base address; addsi3 knows how to handle constants
7436              that require more than one insn.  */
7437           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7438           base = base_plus;
7439           offset = lo;
7440         }
7441     }
7442
7443   /* Operands[2] may overlap operands[0] (though it won't overlap
7444      operands[1]), that's why we asked for a DImode reg -- so we can
7445      use the bit that does not overlap.  */
7446   if (REGNO (operands[2]) == REGNO (operands[0]))
7447     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7448   else
7449     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7450
7451   emit_insn (gen_zero_extendqisi2 (scratch,
7452                                    gen_rtx_MEM (QImode,
7453                                                 plus_constant (base,
7454                                                                offset))));
7455   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
7456                                    gen_rtx_MEM (QImode,
7457                                                 plus_constant (base,
7458                                                                offset + 1))));
7459   if (!BYTES_BIG_ENDIAN)
7460     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
7461                    gen_rtx_IOR (SImode,
7462                                 gen_rtx_ASHIFT
7463                                 (SImode,
7464                                  gen_rtx_SUBREG (SImode, operands[0], 0),
7465                                  GEN_INT (8)),
7466                                 scratch));
7467   else
7468     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
7469                    gen_rtx_IOR (SImode,
7470                                 gen_rtx_ASHIFT (SImode, scratch,
7471                                                 GEN_INT (8)),
7472                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
7473 }
7474
7475 /* Handle storing a half-word to memory during reload by synthesizing as two
7476    byte stores.  Take care not to clobber the input values until after we
7477    have moved them somewhere safe.  This code assumes that if the DImode
7478    scratch in operands[2] overlaps either the input value or output address
7479    in some way, then that value must die in this insn (we absolutely need
7480    two scratch registers for some corner cases).  */
7481 void
7482 arm_reload_out_hi (rtx *operands)
7483 {
7484   rtx ref = operands[0];
7485   rtx outval = operands[1];
7486   rtx base, scratch;
7487   HOST_WIDE_INT offset = 0;
7488
7489   if (GET_CODE (ref) == SUBREG)
7490     {
7491       offset = SUBREG_BYTE (ref);
7492       ref = SUBREG_REG (ref);
7493     }
7494
7495   if (GET_CODE (ref) == REG)
7496     {
7497       /* We have a pseudo which has been spilt onto the stack; there
7498          are two cases here: the first where there is a simple
7499          stack-slot replacement and a second where the stack-slot is
7500          out of range, or is used as a subreg.  */
7501       if (reg_equiv_mem[REGNO (ref)])
7502         {
7503           ref = reg_equiv_mem[REGNO (ref)];
7504           base = find_replacement (&XEXP (ref, 0));
7505         }
7506       else
7507         /* The slot is out of range, or was dressed up in a SUBREG.  */
7508         base = reg_equiv_address[REGNO (ref)];
7509     }
7510   else
7511     base = find_replacement (&XEXP (ref, 0));
7512
7513   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7514
7515   /* Handle the case where the address is too complex to be offset by 1.  */
7516   if (GET_CODE (base) == MINUS
7517       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7518     {
7519       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7520
7521       /* Be careful not to destroy OUTVAL.  */
7522       if (reg_overlap_mentioned_p (base_plus, outval))
7523         {
7524           /* Updating base_plus might destroy outval, see if we can
7525              swap the scratch and base_plus.  */
7526           if (!reg_overlap_mentioned_p (scratch, outval))
7527             {
7528               rtx tmp = scratch;
7529               scratch = base_plus;
7530               base_plus = tmp;
7531             }
7532           else
7533             {
7534               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7535
7536               /* Be conservative and copy OUTVAL into the scratch now,
7537                  this should only be necessary if outval is a subreg
7538                  of something larger than a word.  */
7539               /* XXX Might this clobber base?  I can't see how it can,
7540                  since scratch is known to overlap with OUTVAL, and
7541                  must be wider than a word.  */
7542               emit_insn (gen_movhi (scratch_hi, outval));
7543               outval = scratch_hi;
7544             }
7545         }
7546
7547       emit_set_insn (base_plus, base);
7548       base = base_plus;
7549     }
7550   else if (GET_CODE (base) == PLUS)
7551     {
7552       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7553       HOST_WIDE_INT hi, lo;
7554
7555       offset += INTVAL (XEXP (base, 1));
7556       base = XEXP (base, 0);
7557
7558       /* Rework the address into a legal sequence of insns.  */
7559       /* Valid range for lo is -4095 -> 4095 */
7560       lo = (offset >= 0
7561             ? (offset & 0xfff)
7562             : -((-offset) & 0xfff));
7563
7564       /* Corner case, if lo is the max offset then we would be out of range
7565          once we have added the additional 1 below, so bump the msb into the
7566          pre-loading insn(s).  */
7567       if (lo == 4095)
7568         lo &= 0x7ff;
7569
7570       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7571              ^ (HOST_WIDE_INT) 0x80000000)
7572             - (HOST_WIDE_INT) 0x80000000);
7573
7574       gcc_assert (hi + lo == offset);
7575
7576       if (hi != 0)
7577         {
7578           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7579
7580           /* Be careful not to destroy OUTVAL.  */
7581           if (reg_overlap_mentioned_p (base_plus, outval))
7582             {
7583               /* Updating base_plus might destroy outval, see if we
7584                  can swap the scratch and base_plus.  */
7585               if (!reg_overlap_mentioned_p (scratch, outval))
7586                 {
7587                   rtx tmp = scratch;
7588                   scratch = base_plus;
7589                   base_plus = tmp;
7590                 }
7591               else
7592                 {
7593                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7594
7595                   /* Be conservative and copy outval into scratch now,
7596                      this should only be necessary if outval is a
7597                      subreg of something larger than a word.  */
7598                   /* XXX Might this clobber base?  I can't see how it
7599                      can, since scratch is known to overlap with
7600                      outval.  */
7601                   emit_insn (gen_movhi (scratch_hi, outval));
7602                   outval = scratch_hi;
7603                 }
7604             }
7605
7606           /* Get the base address; addsi3 knows how to handle constants
7607              that require more than one insn.  */
7608           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7609           base = base_plus;
7610           offset = lo;
7611         }
7612     }
7613
7614   if (BYTES_BIG_ENDIAN)
7615     {
7616       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7617                                          plus_constant (base, offset + 1)),
7618                             gen_lowpart (QImode, outval)));
7619       emit_insn (gen_lshrsi3 (scratch,
7620                               gen_rtx_SUBREG (SImode, outval, 0),
7621                               GEN_INT (8)));
7622       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7623                             gen_lowpart (QImode, scratch)));
7624     }
7625   else
7626     {
7627       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7628                             gen_lowpart (QImode, outval)));
7629       emit_insn (gen_lshrsi3 (scratch,
7630                               gen_rtx_SUBREG (SImode, outval, 0),
7631                               GEN_INT (8)));
7632       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7633                                          plus_constant (base, offset + 1)),
7634                             gen_lowpart (QImode, scratch)));
7635     }
7636 }
7637
7638 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7639    (padded to the size of a word) should be passed in a register.  */
7640
7641 static bool
7642 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7643 {
7644   if (TARGET_AAPCS_BASED)
7645     return must_pass_in_stack_var_size (mode, type);
7646   else
7647     return must_pass_in_stack_var_size_or_pad (mode, type);
7648 }
7649
7650
7651 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7652    Return true if an argument passed on the stack should be padded upwards,
7653    i.e. if the least-significant byte has useful data.
7654    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
7655    aggregate types are placed in the lowest memory address.  */
7656
7657 bool
7658 arm_pad_arg_upward (enum machine_mode mode, tree type)
7659 {
7660   if (!TARGET_AAPCS_BASED)
7661     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7662
7663   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7664     return false;
7665
7666   return true;
7667 }
7668
7669
7670 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7671    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7672    byte of the register has useful data, and return the opposite if the
7673    most significant byte does.
7674    For AAPCS, small aggregates and small complex types are always padded
7675    upwards.  */
7676
7677 bool
7678 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7679                     tree type, int first ATTRIBUTE_UNUSED)
7680 {
7681   if (TARGET_AAPCS_BASED
7682       && BYTES_BIG_ENDIAN
7683       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7684       && int_size_in_bytes (type) <= 4)
7685     return true;
7686
7687   /* Otherwise, use default padding.  */
7688   return !BYTES_BIG_ENDIAN;
7689 }
7690
7691 \f
7692 /* Print a symbolic form of X to the debug file, F.  */
7693 static void
7694 arm_print_value (FILE *f, rtx x)
7695 {
7696   switch (GET_CODE (x))
7697     {
7698     case CONST_INT:
7699       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7700       return;
7701
7702     case CONST_DOUBLE:
7703       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7704       return;
7705
7706     case CONST_VECTOR:
7707       {
7708         int i;
7709
7710         fprintf (f, "<");
7711         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7712           {
7713             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7714             if (i < (CONST_VECTOR_NUNITS (x) - 1))
7715               fputc (',', f);
7716           }
7717         fprintf (f, ">");
7718       }
7719       return;
7720
7721     case CONST_STRING:
7722       fprintf (f, "\"%s\"", XSTR (x, 0));
7723       return;
7724
7725     case SYMBOL_REF:
7726       fprintf (f, "`%s'", XSTR (x, 0));
7727       return;
7728
7729     case LABEL_REF:
7730       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7731       return;
7732
7733     case CONST:
7734       arm_print_value (f, XEXP (x, 0));
7735       return;
7736
7737     case PLUS:
7738       arm_print_value (f, XEXP (x, 0));
7739       fprintf (f, "+");
7740       arm_print_value (f, XEXP (x, 1));
7741       return;
7742
7743     case PC:
7744       fprintf (f, "pc");
7745       return;
7746
7747     default:
7748       fprintf (f, "????");
7749       return;
7750     }
7751 }
7752 \f
7753 /* Routines for manipulation of the constant pool.  */
7754
7755 /* Arm instructions cannot load a large constant directly into a
7756    register; they have to come from a pc relative load.  The constant
7757    must therefore be placed in the addressable range of the pc
7758    relative load.  Depending on the precise pc relative load
7759    instruction the range is somewhere between 256 bytes and 4k.  This
7760    means that we often have to dump a constant inside a function, and
7761    generate code to branch around it.
7762
7763    It is important to minimize this, since the branches will slow
7764    things down and make the code larger.
7765
7766    Normally we can hide the table after an existing unconditional
7767    branch so that there is no interruption of the flow, but in the
7768    worst case the code looks like this:
7769
7770         ldr     rn, L1
7771         ...
7772         b       L2
7773         align
7774         L1:     .long value
7775         L2:
7776         ...
7777
7778         ldr     rn, L3
7779         ...
7780         b       L4
7781         align
7782         L3:     .long value
7783         L4:
7784         ...
7785
7786    We fix this by performing a scan after scheduling, which notices
7787    which instructions need to have their operands fetched from the
7788    constant table and builds the table.
7789
7790    The algorithm starts by building a table of all the constants that
7791    need fixing up and all the natural barriers in the function (places
7792    where a constant table can be dropped without breaking the flow).
7793    For each fixup we note how far the pc-relative replacement will be
7794    able to reach and the offset of the instruction into the function.
7795
7796    Having built the table we then group the fixes together to form
7797    tables that are as large as possible (subject to addressing
7798    constraints) and emit each table of constants after the last
7799    barrier that is within range of all the instructions in the group.
7800    If a group does not contain a barrier, then we forcibly create one
7801    by inserting a jump instruction into the flow.  Once the table has
7802    been inserted, the insns are then modified to reference the
7803    relevant entry in the pool.
7804
7805    Possible enhancements to the algorithm (not implemented) are:
7806
7807    1) For some processors and object formats, there may be benefit in
7808    aligning the pools to the start of cache lines; this alignment
7809    would need to be taken into account when calculating addressability
7810    of a pool.  */
7811
7812 /* These typedefs are located at the start of this file, so that
7813    they can be used in the prototypes there.  This comment is to
7814    remind readers of that fact so that the following structures
7815    can be understood more easily.
7816
7817      typedef struct minipool_node    Mnode;
7818      typedef struct minipool_fixup   Mfix;  */
7819
7820 struct minipool_node
7821 {
7822   /* Doubly linked chain of entries.  */
7823   Mnode * next;
7824   Mnode * prev;
7825   /* The maximum offset into the code that this entry can be placed.  While
7826      pushing fixes for forward references, all entries are sorted in order
7827      of increasing max_address.  */
7828   HOST_WIDE_INT max_address;
7829   /* Similarly for an entry inserted for a backwards ref.  */
7830   HOST_WIDE_INT min_address;
7831   /* The number of fixes referencing this entry.  This can become zero
7832      if we "unpush" an entry.  In this case we ignore the entry when we
7833      come to emit the code.  */
7834   int refcount;
7835   /* The offset from the start of the minipool.  */
7836   HOST_WIDE_INT offset;
7837   /* The value in table.  */
7838   rtx value;
7839   /* The mode of value.  */
7840   enum machine_mode mode;
7841   /* The size of the value.  With iWMMXt enabled
7842      sizes > 4 also imply an alignment of 8-bytes.  */
7843   int fix_size;
7844 };
7845
7846 struct minipool_fixup
7847 {
7848   Mfix *            next;
7849   rtx               insn;
7850   HOST_WIDE_INT     address;
7851   rtx *             loc;
7852   enum machine_mode mode;
7853   int               fix_size;
7854   rtx               value;
7855   Mnode *           minipool;
7856   HOST_WIDE_INT     forwards;
7857   HOST_WIDE_INT     backwards;
7858 };
7859
7860 /* Fixes less than a word need padding out to a word boundary.  */
7861 #define MINIPOOL_FIX_SIZE(mode) \
7862   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7863
7864 static Mnode *  minipool_vector_head;
7865 static Mnode *  minipool_vector_tail;
7866 static rtx      minipool_vector_label;
7867 static int      minipool_pad;
7868
7869 /* The linked list of all minipool fixes required for this function.  */
7870 Mfix *          minipool_fix_head;
7871 Mfix *          minipool_fix_tail;
7872 /* The fix entry for the current minipool, once it has been placed.  */
7873 Mfix *          minipool_barrier;
7874
7875 /* Determines if INSN is the start of a jump table.  Returns the end
7876    of the TABLE or NULL_RTX.  */
7877 static rtx
7878 is_jump_table (rtx insn)
7879 {
7880   rtx table;
7881
7882   if (GET_CODE (insn) == JUMP_INSN
7883       && JUMP_LABEL (insn) != NULL
7884       && ((table = next_real_insn (JUMP_LABEL (insn)))
7885           == next_real_insn (insn))
7886       && table != NULL
7887       && GET_CODE (table) == JUMP_INSN
7888       && (GET_CODE (PATTERN (table)) == ADDR_VEC
7889           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7890     return table;
7891
7892   return NULL_RTX;
7893 }
7894
7895 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7896 #define JUMP_TABLES_IN_TEXT_SECTION 0
7897 #endif
7898
7899 static HOST_WIDE_INT
7900 get_jump_table_size (rtx insn)
7901 {
7902   /* ADDR_VECs only take room if read-only data does into the text
7903      section.  */
7904   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7905     {
7906       rtx body = PATTERN (insn);
7907       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7908       HOST_WIDE_INT size;
7909       HOST_WIDE_INT modesize;
7910
7911       modesize = GET_MODE_SIZE (GET_MODE (body));
7912       size = modesize * XVECLEN (body, elt);
7913       switch (modesize)
7914         {
7915         case 1:
7916           /* Round up size  of TBB table to a halfword boundary.  */
7917           size = (size + 1) & ~(HOST_WIDE_INT)1;
7918           break;
7919         case 2:
7920           /* No padding necessary for TBH.  */
7921           break;
7922         case 4:
7923           /* Add two bytes for alignment on Thumb.  */
7924           if (TARGET_THUMB)
7925             size += 2;
7926           break;
7927         default:
7928           gcc_unreachable ();
7929         }
7930       return size;
7931     }
7932
7933   return 0;
7934 }
7935
7936 /* Move a minipool fix MP from its current location to before MAX_MP.
7937    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7938    constraints may need updating.  */
7939 static Mnode *
7940 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7941                                HOST_WIDE_INT max_address)
7942 {
7943   /* The code below assumes these are different.  */
7944   gcc_assert (mp != max_mp);
7945
7946   if (max_mp == NULL)
7947     {
7948       if (max_address < mp->max_address)
7949         mp->max_address = max_address;
7950     }
7951   else
7952     {
7953       if (max_address > max_mp->max_address - mp->fix_size)
7954         mp->max_address = max_mp->max_address - mp->fix_size;
7955       else
7956         mp->max_address = max_address;
7957
7958       /* Unlink MP from its current position.  Since max_mp is non-null,
7959        mp->prev must be non-null.  */
7960       mp->prev->next = mp->next;
7961       if (mp->next != NULL)
7962         mp->next->prev = mp->prev;
7963       else
7964         minipool_vector_tail = mp->prev;
7965
7966       /* Re-insert it before MAX_MP.  */
7967       mp->next = max_mp;
7968       mp->prev = max_mp->prev;
7969       max_mp->prev = mp;
7970
7971       if (mp->prev != NULL)
7972         mp->prev->next = mp;
7973       else
7974         minipool_vector_head = mp;
7975     }
7976
7977   /* Save the new entry.  */
7978   max_mp = mp;
7979
7980   /* Scan over the preceding entries and adjust their addresses as
7981      required.  */
7982   while (mp->prev != NULL
7983          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7984     {
7985       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7986       mp = mp->prev;
7987     }
7988
7989   return max_mp;
7990 }
7991
7992 /* Add a constant to the minipool for a forward reference.  Returns the
7993    node added or NULL if the constant will not fit in this pool.  */
7994 static Mnode *
7995 add_minipool_forward_ref (Mfix *fix)
7996 {
7997   /* If set, max_mp is the first pool_entry that has a lower
7998      constraint than the one we are trying to add.  */
7999   Mnode *       max_mp = NULL;
8000   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
8001   Mnode *       mp;
8002
8003   /* If the minipool starts before the end of FIX->INSN then this FIX
8004      can not be placed into the current pool.  Furthermore, adding the
8005      new constant pool entry may cause the pool to start FIX_SIZE bytes
8006      earlier.  */
8007   if (minipool_vector_head &&
8008       (fix->address + get_attr_length (fix->insn)
8009        >= minipool_vector_head->max_address - fix->fix_size))
8010     return NULL;
8011
8012   /* Scan the pool to see if a constant with the same value has
8013      already been added.  While we are doing this, also note the
8014      location where we must insert the constant if it doesn't already
8015      exist.  */
8016   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8017     {
8018       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8019           && fix->mode == mp->mode
8020           && (GET_CODE (fix->value) != CODE_LABEL
8021               || (CODE_LABEL_NUMBER (fix->value)
8022                   == CODE_LABEL_NUMBER (mp->value)))
8023           && rtx_equal_p (fix->value, mp->value))
8024         {
8025           /* More than one fix references this entry.  */
8026           mp->refcount++;
8027           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
8028         }
8029
8030       /* Note the insertion point if necessary.  */
8031       if (max_mp == NULL
8032           && mp->max_address > max_address)
8033         max_mp = mp;
8034
8035       /* If we are inserting an 8-bytes aligned quantity and
8036          we have not already found an insertion point, then
8037          make sure that all such 8-byte aligned quantities are
8038          placed at the start of the pool.  */
8039       if (ARM_DOUBLEWORD_ALIGN
8040           && max_mp == NULL
8041           && fix->fix_size == 8
8042           && mp->fix_size != 8)
8043         {
8044           max_mp = mp;
8045           max_address = mp->max_address;
8046         }
8047     }
8048
8049   /* The value is not currently in the minipool, so we need to create
8050      a new entry for it.  If MAX_MP is NULL, the entry will be put on
8051      the end of the list since the placement is less constrained than
8052      any existing entry.  Otherwise, we insert the new fix before
8053      MAX_MP and, if necessary, adjust the constraints on the other
8054      entries.  */
8055   mp = XNEW (Mnode);
8056   mp->fix_size = fix->fix_size;
8057   mp->mode = fix->mode;
8058   mp->value = fix->value;
8059   mp->refcount = 1;
8060   /* Not yet required for a backwards ref.  */
8061   mp->min_address = -65536;
8062
8063   if (max_mp == NULL)
8064     {
8065       mp->max_address = max_address;
8066       mp->next = NULL;
8067       mp->prev = minipool_vector_tail;
8068
8069       if (mp->prev == NULL)
8070         {
8071           minipool_vector_head = mp;
8072           minipool_vector_label = gen_label_rtx ();
8073         }
8074       else
8075         mp->prev->next = mp;
8076
8077       minipool_vector_tail = mp;
8078     }
8079   else
8080     {
8081       if (max_address > max_mp->max_address - mp->fix_size)
8082         mp->max_address = max_mp->max_address - mp->fix_size;
8083       else
8084         mp->max_address = max_address;
8085
8086       mp->next = max_mp;
8087       mp->prev = max_mp->prev;
8088       max_mp->prev = mp;
8089       if (mp->prev != NULL)
8090         mp->prev->next = mp;
8091       else
8092         minipool_vector_head = mp;
8093     }
8094
8095   /* Save the new entry.  */
8096   max_mp = mp;
8097
8098   /* Scan over the preceding entries and adjust their addresses as
8099      required.  */
8100   while (mp->prev != NULL
8101          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
8102     {
8103       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
8104       mp = mp->prev;
8105     }
8106
8107   return max_mp;
8108 }
8109
8110 static Mnode *
8111 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
8112                                 HOST_WIDE_INT  min_address)
8113 {
8114   HOST_WIDE_INT offset;
8115
8116   /* The code below assumes these are different.  */
8117   gcc_assert (mp != min_mp);
8118
8119   if (min_mp == NULL)
8120     {
8121       if (min_address > mp->min_address)
8122         mp->min_address = min_address;
8123     }
8124   else
8125     {
8126       /* We will adjust this below if it is too loose.  */
8127       mp->min_address = min_address;
8128
8129       /* Unlink MP from its current position.  Since min_mp is non-null,
8130          mp->next must be non-null.  */
8131       mp->next->prev = mp->prev;
8132       if (mp->prev != NULL)
8133         mp->prev->next = mp->next;
8134       else
8135         minipool_vector_head = mp->next;
8136
8137       /* Reinsert it after MIN_MP.  */
8138       mp->prev = min_mp;
8139       mp->next = min_mp->next;
8140       min_mp->next = mp;
8141       if (mp->next != NULL)
8142         mp->next->prev = mp;
8143       else
8144         minipool_vector_tail = mp;
8145     }
8146
8147   min_mp = mp;
8148
8149   offset = 0;
8150   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8151     {
8152       mp->offset = offset;
8153       if (mp->refcount > 0)
8154         offset += mp->fix_size;
8155
8156       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
8157         mp->next->min_address = mp->min_address + mp->fix_size;
8158     }
8159
8160   return min_mp;
8161 }
8162
8163 /* Add a constant to the minipool for a backward reference.  Returns the
8164    node added or NULL if the constant will not fit in this pool.
8165
8166    Note that the code for insertion for a backwards reference can be
8167    somewhat confusing because the calculated offsets for each fix do
8168    not take into account the size of the pool (which is still under
8169    construction.  */
8170 static Mnode *
8171 add_minipool_backward_ref (Mfix *fix)
8172 {
8173   /* If set, min_mp is the last pool_entry that has a lower constraint
8174      than the one we are trying to add.  */
8175   Mnode *min_mp = NULL;
8176   /* This can be negative, since it is only a constraint.  */
8177   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
8178   Mnode *mp;
8179
8180   /* If we can't reach the current pool from this insn, or if we can't
8181      insert this entry at the end of the pool without pushing other
8182      fixes out of range, then we don't try.  This ensures that we
8183      can't fail later on.  */
8184   if (min_address >= minipool_barrier->address
8185       || (minipool_vector_tail->min_address + fix->fix_size
8186           >= minipool_barrier->address))
8187     return NULL;
8188
8189   /* Scan the pool to see if a constant with the same value has
8190      already been added.  While we are doing this, also note the
8191      location where we must insert the constant if it doesn't already
8192      exist.  */
8193   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
8194     {
8195       if (GET_CODE (fix->value) == GET_CODE (mp->value)
8196           && fix->mode == mp->mode
8197           && (GET_CODE (fix->value) != CODE_LABEL
8198               || (CODE_LABEL_NUMBER (fix->value)
8199                   == CODE_LABEL_NUMBER (mp->value)))
8200           && rtx_equal_p (fix->value, mp->value)
8201           /* Check that there is enough slack to move this entry to the
8202              end of the table (this is conservative).  */
8203           && (mp->max_address
8204               > (minipool_barrier->address
8205                  + minipool_vector_tail->offset
8206                  + minipool_vector_tail->fix_size)))
8207         {
8208           mp->refcount++;
8209           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
8210         }
8211
8212       if (min_mp != NULL)
8213         mp->min_address += fix->fix_size;
8214       else
8215         {
8216           /* Note the insertion point if necessary.  */
8217           if (mp->min_address < min_address)
8218             {
8219               /* For now, we do not allow the insertion of 8-byte alignment
8220                  requiring nodes anywhere but at the start of the pool.  */
8221               if (ARM_DOUBLEWORD_ALIGN
8222                   && fix->fix_size == 8 && mp->fix_size != 8)
8223                 return NULL;
8224               else
8225                 min_mp = mp;
8226             }
8227           else if (mp->max_address
8228                    < minipool_barrier->address + mp->offset + fix->fix_size)
8229             {
8230               /* Inserting before this entry would push the fix beyond
8231                  its maximum address (which can happen if we have
8232                  re-located a forwards fix); force the new fix to come
8233                  after it.  */
8234               min_mp = mp;
8235               min_address = mp->min_address + fix->fix_size;
8236             }
8237           /* If we are inserting an 8-bytes aligned quantity and
8238              we have not already found an insertion point, then
8239              make sure that all such 8-byte aligned quantities are
8240              placed at the start of the pool.  */
8241           else if (ARM_DOUBLEWORD_ALIGN
8242                    && min_mp == NULL
8243                    && fix->fix_size == 8
8244                    && mp->fix_size < 8)
8245             {
8246               min_mp = mp;
8247               min_address = mp->min_address + fix->fix_size;
8248             }
8249         }
8250     }
8251
8252   /* We need to create a new entry.  */
8253   mp = XNEW (Mnode);
8254   mp->fix_size = fix->fix_size;
8255   mp->mode = fix->mode;
8256   mp->value = fix->value;
8257   mp->refcount = 1;
8258   mp->max_address = minipool_barrier->address + 65536;
8259
8260   mp->min_address = min_address;
8261
8262   if (min_mp == NULL)
8263     {
8264       mp->prev = NULL;
8265       mp->next = minipool_vector_head;
8266
8267       if (mp->next == NULL)
8268         {
8269           minipool_vector_tail = mp;
8270           minipool_vector_label = gen_label_rtx ();
8271         }
8272       else
8273         mp->next->prev = mp;
8274
8275       minipool_vector_head = mp;
8276     }
8277   else
8278     {
8279       mp->next = min_mp->next;
8280       mp->prev = min_mp;
8281       min_mp->next = mp;
8282
8283       if (mp->next != NULL)
8284         mp->next->prev = mp;
8285       else
8286         minipool_vector_tail = mp;
8287     }
8288
8289   /* Save the new entry.  */
8290   min_mp = mp;
8291
8292   if (mp->prev)
8293     mp = mp->prev;
8294   else
8295     mp->offset = 0;
8296
8297   /* Scan over the following entries and adjust their offsets.  */
8298   while (mp->next != NULL)
8299     {
8300       if (mp->next->min_address < mp->min_address + mp->fix_size)
8301         mp->next->min_address = mp->min_address + mp->fix_size;
8302
8303       if (mp->refcount)
8304         mp->next->offset = mp->offset + mp->fix_size;
8305       else
8306         mp->next->offset = mp->offset;
8307
8308       mp = mp->next;
8309     }
8310
8311   return min_mp;
8312 }
8313
8314 static void
8315 assign_minipool_offsets (Mfix *barrier)
8316 {
8317   HOST_WIDE_INT offset = 0;
8318   Mnode *mp;
8319
8320   minipool_barrier = barrier;
8321
8322   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8323     {
8324       mp->offset = offset;
8325
8326       if (mp->refcount > 0)
8327         offset += mp->fix_size;
8328     }
8329 }
8330
8331 /* Output the literal table */
8332 static void
8333 dump_minipool (rtx scan)
8334 {
8335   Mnode * mp;
8336   Mnode * nmp;
8337   int align64 = 0;
8338
8339   if (ARM_DOUBLEWORD_ALIGN)
8340     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8341       if (mp->refcount > 0 && mp->fix_size == 8)
8342         {
8343           align64 = 1;
8344           break;
8345         }
8346
8347   if (dump_file)
8348     fprintf (dump_file,
8349              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
8350              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
8351
8352   scan = emit_label_after (gen_label_rtx (), scan);
8353   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
8354   scan = emit_label_after (minipool_vector_label, scan);
8355
8356   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
8357     {
8358       if (mp->refcount > 0)
8359         {
8360           if (dump_file)
8361             {
8362               fprintf (dump_file,
8363                        ";;  Offset %u, min %ld, max %ld ",
8364                        (unsigned) mp->offset, (unsigned long) mp->min_address,
8365                        (unsigned long) mp->max_address);
8366               arm_print_value (dump_file, mp->value);
8367               fputc ('\n', dump_file);
8368             }
8369
8370           switch (mp->fix_size)
8371             {
8372 #ifdef HAVE_consttable_1
8373             case 1:
8374               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
8375               break;
8376
8377 #endif
8378 #ifdef HAVE_consttable_2
8379             case 2:
8380               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
8381               break;
8382
8383 #endif
8384 #ifdef HAVE_consttable_4
8385             case 4:
8386               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
8387               break;
8388
8389 #endif
8390 #ifdef HAVE_consttable_8
8391             case 8:
8392               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
8393               break;
8394
8395 #endif
8396             default:
8397               gcc_unreachable ();
8398             }
8399         }
8400
8401       nmp = mp->next;
8402       free (mp);
8403     }
8404
8405   minipool_vector_head = minipool_vector_tail = NULL;
8406   scan = emit_insn_after (gen_consttable_end (), scan);
8407   scan = emit_barrier_after (scan);
8408 }
8409
8410 /* Return the cost of forcibly inserting a barrier after INSN.  */
8411 static int
8412 arm_barrier_cost (rtx insn)
8413 {
8414   /* Basing the location of the pool on the loop depth is preferable,
8415      but at the moment, the basic block information seems to be
8416      corrupt by this stage of the compilation.  */
8417   int base_cost = 50;
8418   rtx next = next_nonnote_insn (insn);
8419
8420   if (next != NULL && GET_CODE (next) == CODE_LABEL)
8421     base_cost -= 20;
8422
8423   switch (GET_CODE (insn))
8424     {
8425     case CODE_LABEL:
8426       /* It will always be better to place the table before the label, rather
8427          than after it.  */
8428       return 50;
8429
8430     case INSN:
8431     case CALL_INSN:
8432       return base_cost;
8433
8434     case JUMP_INSN:
8435       return base_cost - 10;
8436
8437     default:
8438       return base_cost + 10;
8439     }
8440 }
8441
8442 /* Find the best place in the insn stream in the range
8443    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
8444    Create the barrier by inserting a jump and add a new fix entry for
8445    it.  */
8446 static Mfix *
8447 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
8448 {
8449   HOST_WIDE_INT count = 0;
8450   rtx barrier;
8451   rtx from = fix->insn;
8452   /* The instruction after which we will insert the jump.  */
8453   rtx selected = NULL;
8454   int selected_cost;
8455   /* The address at which the jump instruction will be placed.  */
8456   HOST_WIDE_INT selected_address;
8457   Mfix * new_fix;
8458   HOST_WIDE_INT max_count = max_address - fix->address;
8459   rtx label = gen_label_rtx ();
8460
8461   selected_cost = arm_barrier_cost (from);
8462   selected_address = fix->address;
8463
8464   while (from && count < max_count)
8465     {
8466       rtx tmp;
8467       int new_cost;
8468
8469       /* This code shouldn't have been called if there was a natural barrier
8470          within range.  */
8471       gcc_assert (GET_CODE (from) != BARRIER);
8472
8473       /* Count the length of this insn.  */
8474       count += get_attr_length (from);
8475
8476       /* If there is a jump table, add its length.  */
8477       tmp = is_jump_table (from);
8478       if (tmp != NULL)
8479         {
8480           count += get_jump_table_size (tmp);
8481
8482           /* Jump tables aren't in a basic block, so base the cost on
8483              the dispatch insn.  If we select this location, we will
8484              still put the pool after the table.  */
8485           new_cost = arm_barrier_cost (from);
8486
8487           if (count < max_count 
8488               && (!selected || new_cost <= selected_cost))
8489             {
8490               selected = tmp;
8491               selected_cost = new_cost;
8492               selected_address = fix->address + count;
8493             }
8494
8495           /* Continue after the dispatch table.  */
8496           from = NEXT_INSN (tmp);
8497           continue;
8498         }
8499
8500       new_cost = arm_barrier_cost (from);
8501
8502       if (count < max_count
8503           && (!selected || new_cost <= selected_cost))
8504         {
8505           selected = from;
8506           selected_cost = new_cost;
8507           selected_address = fix->address + count;
8508         }
8509
8510       from = NEXT_INSN (from);
8511     }
8512
8513   /* Make sure that we found a place to insert the jump.  */
8514   gcc_assert (selected);
8515
8516   /* Create a new JUMP_INSN that branches around a barrier.  */
8517   from = emit_jump_insn_after (gen_jump (label), selected);
8518   JUMP_LABEL (from) = label;
8519   barrier = emit_barrier_after (from);
8520   emit_label_after (label, barrier);
8521
8522   /* Create a minipool barrier entry for the new barrier.  */
8523   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
8524   new_fix->insn = barrier;
8525   new_fix->address = selected_address;
8526   new_fix->next = fix->next;
8527   fix->next = new_fix;
8528
8529   return new_fix;
8530 }
8531
8532 /* Record that there is a natural barrier in the insn stream at
8533    ADDRESS.  */
8534 static void
8535 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
8536 {
8537   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8538
8539   fix->insn = insn;
8540   fix->address = address;
8541
8542   fix->next = NULL;
8543   if (minipool_fix_head != NULL)
8544     minipool_fix_tail->next = fix;
8545   else
8546     minipool_fix_head = fix;
8547
8548   minipool_fix_tail = fix;
8549 }
8550
8551 /* Record INSN, which will need fixing up to load a value from the
8552    minipool.  ADDRESS is the offset of the insn since the start of the
8553    function; LOC is a pointer to the part of the insn which requires
8554    fixing; VALUE is the constant that must be loaded, which is of type
8555    MODE.  */
8556 static void
8557 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
8558                    enum machine_mode mode, rtx value)
8559 {
8560   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8561
8562 #ifdef AOF_ASSEMBLER
8563   /* PIC symbol references need to be converted into offsets into the
8564      based area.  */
8565   /* XXX This shouldn't be done here.  */
8566   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
8567     value = aof_pic_entry (value);
8568 #endif /* AOF_ASSEMBLER */
8569
8570   fix->insn = insn;
8571   fix->address = address;
8572   fix->loc = loc;
8573   fix->mode = mode;
8574   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
8575   fix->value = value;
8576   fix->forwards = get_attr_pool_range (insn);
8577   fix->backwards = get_attr_neg_pool_range (insn);
8578   fix->minipool = NULL;
8579
8580   /* If an insn doesn't have a range defined for it, then it isn't
8581      expecting to be reworked by this code.  Better to stop now than
8582      to generate duff assembly code.  */
8583   gcc_assert (fix->forwards || fix->backwards);
8584
8585   /* If an entry requires 8-byte alignment then assume all constant pools
8586      require 4 bytes of padding.  Trying to do this later on a per-pool
8587      basis is awkward because existing pool entries have to be modified.  */
8588   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8589     minipool_pad = 4;
8590
8591   if (dump_file)
8592     {
8593       fprintf (dump_file,
8594                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8595                GET_MODE_NAME (mode),
8596                INSN_UID (insn), (unsigned long) address,
8597                -1 * (long)fix->backwards, (long)fix->forwards);
8598       arm_print_value (dump_file, fix->value);
8599       fprintf (dump_file, "\n");
8600     }
8601
8602   /* Add it to the chain of fixes.  */
8603   fix->next = NULL;
8604
8605   if (minipool_fix_head != NULL)
8606     minipool_fix_tail->next = fix;
8607   else
8608     minipool_fix_head = fix;
8609
8610   minipool_fix_tail = fix;
8611 }
8612
8613 /* Return the cost of synthesizing a 64-bit constant VAL inline.
8614    Returns the number of insns needed, or 99 if we don't know how to
8615    do it.  */
8616 int
8617 arm_const_double_inline_cost (rtx val)
8618 {
8619   rtx lowpart, highpart;
8620   enum machine_mode mode;
8621
8622   mode = GET_MODE (val);
8623
8624   if (mode == VOIDmode)
8625     mode = DImode;
8626
8627   gcc_assert (GET_MODE_SIZE (mode) == 8);
8628
8629   lowpart = gen_lowpart (SImode, val);
8630   highpart = gen_highpart_mode (SImode, mode, val);
8631
8632   gcc_assert (GET_CODE (lowpart) == CONST_INT);
8633   gcc_assert (GET_CODE (highpart) == CONST_INT);
8634
8635   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8636                             NULL_RTX, NULL_RTX, 0, 0)
8637           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8638                               NULL_RTX, NULL_RTX, 0, 0));
8639 }
8640
8641 /* Return true if it is worthwhile to split a 64-bit constant into two
8642    32-bit operations.  This is the case if optimizing for size, or
8643    if we have load delay slots, or if one 32-bit part can be done with
8644    a single data operation.  */
8645 bool
8646 arm_const_double_by_parts (rtx val)
8647 {
8648   enum machine_mode mode = GET_MODE (val);
8649   rtx part;
8650
8651   if (optimize_size || arm_ld_sched)
8652     return true;
8653
8654   if (mode == VOIDmode)
8655     mode = DImode;
8656
8657   part = gen_highpart_mode (SImode, mode, val);
8658
8659   gcc_assert (GET_CODE (part) == CONST_INT);
8660
8661   if (const_ok_for_arm (INTVAL (part))
8662       || const_ok_for_arm (~INTVAL (part)))
8663     return true;
8664
8665   part = gen_lowpart (SImode, val);
8666
8667   gcc_assert (GET_CODE (part) == CONST_INT);
8668
8669   if (const_ok_for_arm (INTVAL (part))
8670       || const_ok_for_arm (~INTVAL (part)))
8671     return true;
8672
8673   return false;
8674 }
8675
8676 /* Scan INSN and note any of its operands that need fixing.
8677    If DO_PUSHES is false we do not actually push any of the fixups
8678    needed.  The function returns TRUE if any fixups were needed/pushed.
8679    This is used by arm_memory_load_p() which needs to know about loads
8680    of constants that will be converted into minipool loads.  */
8681 static bool
8682 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8683 {
8684   bool result = false;
8685   int opno;
8686
8687   extract_insn (insn);
8688
8689   if (!constrain_operands (1))
8690     fatal_insn_not_found (insn);
8691
8692   if (recog_data.n_alternatives == 0)
8693     return false;
8694
8695   /* Fill in recog_op_alt with information about the constraints of
8696      this insn.  */
8697   preprocess_constraints ();
8698
8699   for (opno = 0; opno < recog_data.n_operands; opno++)
8700     {
8701       /* Things we need to fix can only occur in inputs.  */
8702       if (recog_data.operand_type[opno] != OP_IN)
8703         continue;
8704
8705       /* If this alternative is a memory reference, then any mention
8706          of constants in this alternative is really to fool reload
8707          into allowing us to accept one there.  We need to fix them up
8708          now so that we output the right code.  */
8709       if (recog_op_alt[opno][which_alternative].memory_ok)
8710         {
8711           rtx op = recog_data.operand[opno];
8712
8713           if (CONSTANT_P (op))
8714             {
8715               if (do_pushes)
8716                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8717                                    recog_data.operand_mode[opno], op);
8718               result = true;
8719             }
8720           else if (GET_CODE (op) == MEM
8721                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8722                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8723             {
8724               if (do_pushes)
8725                 {
8726                   rtx cop = avoid_constant_pool_reference (op);
8727
8728                   /* Casting the address of something to a mode narrower
8729                      than a word can cause avoid_constant_pool_reference()
8730                      to return the pool reference itself.  That's no good to
8731                      us here.  Lets just hope that we can use the
8732                      constant pool value directly.  */
8733                   if (op == cop)
8734                     cop = get_pool_constant (XEXP (op, 0));
8735
8736                   push_minipool_fix (insn, address,
8737                                      recog_data.operand_loc[opno],
8738                                      recog_data.operand_mode[opno], cop);
8739                 }
8740
8741               result = true;
8742             }
8743         }
8744     }
8745
8746   return result;
8747 }
8748
8749 /* Gcc puts the pool in the wrong place for ARM, since we can only
8750    load addresses a limited distance around the pc.  We do some
8751    special munging to move the constant pool values to the correct
8752    point in the code.  */
8753 static void
8754 arm_reorg (void)
8755 {
8756   rtx insn;
8757   HOST_WIDE_INT address = 0;
8758   Mfix * fix;
8759
8760   minipool_fix_head = minipool_fix_tail = NULL;
8761
8762   /* The first insn must always be a note, or the code below won't
8763      scan it properly.  */
8764   insn = get_insns ();
8765   gcc_assert (GET_CODE (insn) == NOTE);
8766   minipool_pad = 0;
8767
8768   /* Scan all the insns and record the operands that will need fixing.  */
8769   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8770     {
8771       if (TARGET_CIRRUS_FIX_INVALID_INSNS
8772           && (arm_cirrus_insn_p (insn)
8773               || GET_CODE (insn) == JUMP_INSN
8774               || arm_memory_load_p (insn)))
8775         cirrus_reorg (insn);
8776
8777       if (GET_CODE (insn) == BARRIER)
8778         push_minipool_barrier (insn, address);
8779       else if (INSN_P (insn))
8780         {
8781           rtx table;
8782
8783           note_invalid_constants (insn, address, true);
8784           address += get_attr_length (insn);
8785
8786           /* If the insn is a vector jump, add the size of the table
8787              and skip the table.  */
8788           if ((table = is_jump_table (insn)) != NULL)
8789             {
8790               address += get_jump_table_size (table);
8791               insn = table;
8792             }
8793         }
8794     }
8795
8796   fix = minipool_fix_head;
8797
8798   /* Now scan the fixups and perform the required changes.  */
8799   while (fix)
8800     {
8801       Mfix * ftmp;
8802       Mfix * fdel;
8803       Mfix *  last_added_fix;
8804       Mfix * last_barrier = NULL;
8805       Mfix * this_fix;
8806
8807       /* Skip any further barriers before the next fix.  */
8808       while (fix && GET_CODE (fix->insn) == BARRIER)
8809         fix = fix->next;
8810
8811       /* No more fixes.  */
8812       if (fix == NULL)
8813         break;
8814
8815       last_added_fix = NULL;
8816
8817       for (ftmp = fix; ftmp; ftmp = ftmp->next)
8818         {
8819           if (GET_CODE (ftmp->insn) == BARRIER)
8820             {
8821               if (ftmp->address >= minipool_vector_head->max_address)
8822                 break;
8823
8824               last_barrier = ftmp;
8825             }
8826           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8827             break;
8828
8829           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
8830         }
8831
8832       /* If we found a barrier, drop back to that; any fixes that we
8833          could have reached but come after the barrier will now go in
8834          the next mini-pool.  */
8835       if (last_barrier != NULL)
8836         {
8837           /* Reduce the refcount for those fixes that won't go into this
8838              pool after all.  */
8839           for (fdel = last_barrier->next;
8840                fdel && fdel != ftmp;
8841                fdel = fdel->next)
8842             {
8843               fdel->minipool->refcount--;
8844               fdel->minipool = NULL;
8845             }
8846
8847           ftmp = last_barrier;
8848         }
8849       else
8850         {
8851           /* ftmp is first fix that we can't fit into this pool and
8852              there no natural barriers that we could use.  Insert a
8853              new barrier in the code somewhere between the previous
8854              fix and this one, and arrange to jump around it.  */
8855           HOST_WIDE_INT max_address;
8856
8857           /* The last item on the list of fixes must be a barrier, so
8858              we can never run off the end of the list of fixes without
8859              last_barrier being set.  */
8860           gcc_assert (ftmp);
8861
8862           max_address = minipool_vector_head->max_address;
8863           /* Check that there isn't another fix that is in range that
8864              we couldn't fit into this pool because the pool was
8865              already too large: we need to put the pool before such an
8866              instruction.  The pool itself may come just after the
8867              fix because create_fix_barrier also allows space for a
8868              jump instruction.  */
8869           if (ftmp->address < max_address)
8870             max_address = ftmp->address + 1;
8871
8872           last_barrier = create_fix_barrier (last_added_fix, max_address);
8873         }
8874
8875       assign_minipool_offsets (last_barrier);
8876
8877       while (ftmp)
8878         {
8879           if (GET_CODE (ftmp->insn) != BARRIER
8880               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8881                   == NULL))
8882             break;
8883
8884           ftmp = ftmp->next;
8885         }
8886
8887       /* Scan over the fixes we have identified for this pool, fixing them
8888          up and adding the constants to the pool itself.  */
8889       for (this_fix = fix; this_fix && ftmp != this_fix;
8890            this_fix = this_fix->next)
8891         if (GET_CODE (this_fix->insn) != BARRIER)
8892           {
8893             rtx addr
8894               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8895                                                   minipool_vector_label),
8896                                this_fix->minipool->offset);
8897             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8898           }
8899
8900       dump_minipool (last_barrier->insn);
8901       fix = ftmp;
8902     }
8903
8904   /* From now on we must synthesize any constants that we can't handle
8905      directly.  This can happen if the RTL gets split during final
8906      instruction generation.  */
8907   after_arm_reorg = 1;
8908
8909   /* Free the minipool memory.  */
8910   obstack_free (&minipool_obstack, minipool_startobj);
8911 }
8912 \f
8913 /* Routines to output assembly language.  */
8914
8915 /* If the rtx is the correct value then return the string of the number.
8916    In this way we can ensure that valid double constants are generated even
8917    when cross compiling.  */
8918 const char *
8919 fp_immediate_constant (rtx x)
8920 {
8921   REAL_VALUE_TYPE r;
8922   int i;
8923
8924   if (!fp_consts_inited)
8925     init_fp_table ();
8926
8927   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8928   for (i = 0; i < 8; i++)
8929     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8930       return strings_fp[i];
8931
8932   gcc_unreachable ();
8933 }
8934
8935 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
8936 static const char *
8937 fp_const_from_val (REAL_VALUE_TYPE *r)
8938 {
8939   int i;
8940
8941   if (!fp_consts_inited)
8942     init_fp_table ();
8943
8944   for (i = 0; i < 8; i++)
8945     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8946       return strings_fp[i];
8947
8948   gcc_unreachable ();
8949 }
8950
8951 /* Output the operands of a LDM/STM instruction to STREAM.
8952    MASK is the ARM register set mask of which only bits 0-15 are important.
8953    REG is the base register, either the frame pointer or the stack pointer,
8954    INSTR is the possibly suffixed load or store instruction.
8955    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
8956
8957 static void
8958 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8959                  unsigned long mask, int rfe)
8960 {
8961   unsigned i;
8962   bool not_first = FALSE;
8963
8964   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
8965   fputc ('\t', stream);
8966   asm_fprintf (stream, instr, reg);
8967   fputc ('{', stream);
8968
8969   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8970     if (mask & (1 << i))
8971       {
8972         if (not_first)
8973           fprintf (stream, ", ");
8974
8975         asm_fprintf (stream, "%r", i);
8976         not_first = TRUE;
8977       }
8978
8979   if (rfe)
8980     fprintf (stream, "}^\n");
8981   else
8982     fprintf (stream, "}\n");
8983 }
8984
8985
8986 /* Output a FLDMD instruction to STREAM.
8987    BASE if the register containing the address.
8988    REG and COUNT specify the register range.
8989    Extra registers may be added to avoid hardware bugs.
8990
8991    We output FLDMD even for ARMv5 VFP implementations.  Although
8992    FLDMD is technically not supported until ARMv6, it is believed
8993    that all VFP implementations support its use in this context.  */
8994
8995 static void
8996 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
8997 {
8998   int i;
8999
9000   /* Workaround ARM10 VFPr1 bug.  */
9001   if (count == 2 && !arm_arch6)
9002     {
9003       if (reg == 15)
9004         reg--;
9005       count++;
9006     }
9007
9008   /* FLDMD may not load more than 16 doubleword registers at a time. Split the
9009      load into multiple parts if we have to handle more than 16 registers.  */
9010   if (count > 16)
9011     {
9012       vfp_output_fldmd (stream, base, reg, 16);
9013       vfp_output_fldmd (stream, base, reg + 16, count - 16);
9014       return;
9015     }
9016
9017   fputc ('\t', stream);
9018   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
9019
9020   for (i = reg; i < reg + count; i++)
9021     {
9022       if (i > reg)
9023         fputs (", ", stream);
9024       asm_fprintf (stream, "d%d", i);
9025     }
9026   fputs ("}\n", stream);
9027
9028 }
9029
9030
9031 /* Output the assembly for a store multiple.  */
9032
9033 const char *
9034 vfp_output_fstmd (rtx * operands)
9035 {
9036   char pattern[100];
9037   int p;
9038   int base;
9039   int i;
9040
9041   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
9042   p = strlen (pattern);
9043
9044   gcc_assert (GET_CODE (operands[1]) == REG);
9045
9046   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
9047   for (i = 1; i < XVECLEN (operands[2], 0); i++)
9048     {
9049       p += sprintf (&pattern[p], ", d%d", base + i);
9050     }
9051   strcpy (&pattern[p], "}");
9052
9053   output_asm_insn (pattern, operands);
9054   return "";
9055 }
9056
9057
9058 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
9059    number of bytes pushed.  */
9060
9061 static int
9062 vfp_emit_fstmd (int base_reg, int count)
9063 {
9064   rtx par;
9065   rtx dwarf;
9066   rtx tmp, reg;
9067   int i;
9068
9069   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
9070      register pairs are stored by a store multiple insn.  We avoid this
9071      by pushing an extra pair.  */
9072   if (count == 2 && !arm_arch6)
9073     {
9074       if (base_reg == LAST_VFP_REGNUM - 3)
9075         base_reg -= 2;
9076       count++;
9077     }
9078
9079   /* FSTMD may not store more than 16 doubleword registers at once.  Split
9080      larger stores into multiple parts (up to a maximum of two, in
9081      practice).  */
9082   if (count > 16)
9083     {
9084       int saved;
9085       /* NOTE: base_reg is an internal register number, so each D register
9086          counts as 2.  */
9087       saved = vfp_emit_fstmd (base_reg + 32, count - 16);
9088       saved += vfp_emit_fstmd (base_reg, 16);
9089       return saved;
9090     }
9091
9092   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9093   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9094
9095   reg = gen_rtx_REG (DFmode, base_reg);
9096   base_reg += 2;
9097
9098   XVECEXP (par, 0, 0)
9099     = gen_rtx_SET (VOIDmode,
9100                    gen_frame_mem (BLKmode,
9101                                   gen_rtx_PRE_DEC (BLKmode,
9102                                                    stack_pointer_rtx)),
9103                    gen_rtx_UNSPEC (BLKmode,
9104                                    gen_rtvec (1, reg),
9105                                    UNSPEC_PUSH_MULT));
9106
9107   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9108                      plus_constant (stack_pointer_rtx, -(count * 8)));
9109   RTX_FRAME_RELATED_P (tmp) = 1;
9110   XVECEXP (dwarf, 0, 0) = tmp;
9111
9112   tmp = gen_rtx_SET (VOIDmode,
9113                      gen_frame_mem (DFmode, stack_pointer_rtx),
9114                      reg);
9115   RTX_FRAME_RELATED_P (tmp) = 1;
9116   XVECEXP (dwarf, 0, 1) = tmp;
9117
9118   for (i = 1; i < count; i++)
9119     {
9120       reg = gen_rtx_REG (DFmode, base_reg);
9121       base_reg += 2;
9122       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9123
9124       tmp = gen_rtx_SET (VOIDmode,
9125                          gen_frame_mem (DFmode,
9126                                         plus_constant (stack_pointer_rtx,
9127                                                        i * 8)),
9128                          reg);
9129       RTX_FRAME_RELATED_P (tmp) = 1;
9130       XVECEXP (dwarf, 0, i + 1) = tmp;
9131     }
9132
9133   par = emit_insn (par);
9134   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9135                                        REG_NOTES (par));
9136   RTX_FRAME_RELATED_P (par) = 1;
9137
9138   return count * 8;
9139 }
9140
9141 /* Emit a call instruction with pattern PAT.  ADDR is the address of
9142    the call target.  */
9143
9144 void
9145 arm_emit_call_insn (rtx pat, rtx addr)
9146 {
9147   rtx insn;
9148
9149   insn = emit_call_insn (pat);
9150
9151   /* The PIC register is live on entry to VxWorks PIC PLT entries.
9152      If the call might use such an entry, add a use of the PIC register
9153      to the instruction's CALL_INSN_FUNCTION_USAGE.  */
9154   if (TARGET_VXWORKS_RTP
9155       && flag_pic
9156       && GET_CODE (addr) == SYMBOL_REF
9157       && (SYMBOL_REF_DECL (addr)
9158           ? !targetm.binds_local_p (SYMBOL_REF_DECL (addr))
9159           : !SYMBOL_REF_LOCAL_P (addr)))
9160     {
9161       require_pic_register ();
9162       use_reg (&CALL_INSN_FUNCTION_USAGE (insn), cfun->machine->pic_reg);
9163     }
9164 }
9165
9166 /* Output a 'call' insn.  */
9167 const char *
9168 output_call (rtx *operands)
9169 {
9170   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
9171
9172   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
9173   if (REGNO (operands[0]) == LR_REGNUM)
9174     {
9175       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
9176       output_asm_insn ("mov%?\t%0, %|lr", operands);
9177     }
9178
9179   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9180
9181   if (TARGET_INTERWORK || arm_arch4t)
9182     output_asm_insn ("bx%?\t%0", operands);
9183   else
9184     output_asm_insn ("mov%?\t%|pc, %0", operands);
9185
9186   return "";
9187 }
9188
9189 /* Output a 'call' insn that is a reference in memory.  */
9190 const char *
9191 output_call_mem (rtx *operands)
9192 {
9193   if (TARGET_INTERWORK && !arm_arch5)
9194     {
9195       output_asm_insn ("ldr%?\t%|ip, %0", operands);
9196       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9197       output_asm_insn ("bx%?\t%|ip", operands);
9198     }
9199   else if (regno_use_in (LR_REGNUM, operands[0]))
9200     {
9201       /* LR is used in the memory address.  We load the address in the
9202          first instruction.  It's safe to use IP as the target of the
9203          load since the call will kill it anyway.  */
9204       output_asm_insn ("ldr%?\t%|ip, %0", operands);
9205       if (arm_arch5)
9206         output_asm_insn ("blx%?\t%|ip", operands);
9207       else
9208         {
9209           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9210           if (arm_arch4t)
9211             output_asm_insn ("bx%?\t%|ip", operands);
9212           else
9213             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
9214         }
9215     }
9216   else
9217     {
9218       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
9219       output_asm_insn ("ldr%?\t%|pc, %0", operands);
9220     }
9221
9222   return "";
9223 }
9224
9225
9226 /* Output a move from arm registers to an fpa registers.
9227    OPERANDS[0] is an fpa register.
9228    OPERANDS[1] is the first registers of an arm register pair.  */
9229 const char *
9230 output_mov_long_double_fpa_from_arm (rtx *operands)
9231 {
9232   int arm_reg0 = REGNO (operands[1]);
9233   rtx ops[3];
9234
9235   gcc_assert (arm_reg0 != IP_REGNUM);
9236
9237   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9238   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9239   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9240
9241   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9242   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
9243
9244   return "";
9245 }
9246
9247 /* Output a move from an fpa register to arm registers.
9248    OPERANDS[0] is the first registers of an arm register pair.
9249    OPERANDS[1] is an fpa register.  */
9250 const char *
9251 output_mov_long_double_arm_from_fpa (rtx *operands)
9252 {
9253   int arm_reg0 = REGNO (operands[0]);
9254   rtx ops[3];
9255
9256   gcc_assert (arm_reg0 != IP_REGNUM);
9257
9258   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9259   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9260   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
9261
9262   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
9263   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
9264   return "";
9265 }
9266
9267 /* Output a move from arm registers to arm registers of a long double
9268    OPERANDS[0] is the destination.
9269    OPERANDS[1] is the source.  */
9270 const char *
9271 output_mov_long_double_arm_from_arm (rtx *operands)
9272 {
9273   /* We have to be careful here because the two might overlap.  */
9274   int dest_start = REGNO (operands[0]);
9275   int src_start = REGNO (operands[1]);
9276   rtx ops[2];
9277   int i;
9278
9279   if (dest_start < src_start)
9280     {
9281       for (i = 0; i < 3; i++)
9282         {
9283           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9284           ops[1] = gen_rtx_REG (SImode, src_start + i);
9285           output_asm_insn ("mov%?\t%0, %1", ops);
9286         }
9287     }
9288   else
9289     {
9290       for (i = 2; i >= 0; i--)
9291         {
9292           ops[0] = gen_rtx_REG (SImode, dest_start + i);
9293           ops[1] = gen_rtx_REG (SImode, src_start + i);
9294           output_asm_insn ("mov%?\t%0, %1", ops);
9295         }
9296     }
9297
9298   return "";
9299 }
9300
9301
9302 /* Output a move from arm registers to an fpa registers.
9303    OPERANDS[0] is an fpa register.
9304    OPERANDS[1] is the first registers of an arm register pair.  */
9305 const char *
9306 output_mov_double_fpa_from_arm (rtx *operands)
9307 {
9308   int arm_reg0 = REGNO (operands[1]);
9309   rtx ops[2];
9310
9311   gcc_assert (arm_reg0 != IP_REGNUM);
9312
9313   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9314   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9315   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
9316   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
9317   return "";
9318 }
9319
9320 /* Output a move from an fpa register to arm registers.
9321    OPERANDS[0] is the first registers of an arm register pair.
9322    OPERANDS[1] is an fpa register.  */
9323 const char *
9324 output_mov_double_arm_from_fpa (rtx *operands)
9325 {
9326   int arm_reg0 = REGNO (operands[0]);
9327   rtx ops[2];
9328
9329   gcc_assert (arm_reg0 != IP_REGNUM);
9330
9331   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9332   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9333   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
9334   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
9335   return "";
9336 }
9337
9338 /* Output a move between double words.
9339    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
9340    or MEM<-REG and all MEMs must be offsettable addresses.  */
9341 const char *
9342 output_move_double (rtx *operands)
9343 {
9344   enum rtx_code code0 = GET_CODE (operands[0]);
9345   enum rtx_code code1 = GET_CODE (operands[1]);
9346   rtx otherops[3];
9347
9348   if (code0 == REG)
9349     {
9350       int reg0 = REGNO (operands[0]);
9351
9352       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9353
9354       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
9355
9356       switch (GET_CODE (XEXP (operands[1], 0)))
9357         {
9358         case REG:
9359           output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
9360           break;
9361
9362         case PRE_INC:
9363           gcc_assert (TARGET_LDRD);
9364           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
9365           break;
9366
9367         case PRE_DEC:
9368           if (TARGET_LDRD)
9369             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
9370           else
9371             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
9372           break;
9373
9374         case POST_INC:
9375           output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
9376           break;
9377
9378         case POST_DEC:
9379           gcc_assert (TARGET_LDRD);
9380           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
9381           break;
9382
9383         case PRE_MODIFY:
9384         case POST_MODIFY:
9385           otherops[0] = operands[0];
9386           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
9387           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
9388
9389           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
9390             {
9391               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9392                 {
9393                   /* Registers overlap so split out the increment.  */
9394                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
9395                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
9396                 }
9397               else
9398                 {
9399                   /* IWMMXT allows offsets larger than ldrd can handle,
9400                      fix these up with a pair of ldr.  */
9401                   if (GET_CODE (otherops[2]) == CONST_INT
9402                       && (INTVAL(otherops[2]) <= -256
9403                           || INTVAL(otherops[2]) >= 256))
9404                     {
9405                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
9406                       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9407                       output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9408                     }
9409                   else
9410                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
9411                 }
9412             }
9413           else
9414             {
9415               /* IWMMXT allows offsets larger than ldrd can handle,
9416                  fix these up with a pair of ldr.  */
9417               if (GET_CODE (otherops[2]) == CONST_INT
9418                   && (INTVAL(otherops[2]) <= -256
9419                       || INTVAL(otherops[2]) >= 256))
9420                 {
9421                   otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9422                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9423                   otherops[0] = operands[0];
9424                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
9425                 }
9426               else
9427                 /* We only allow constant increments, so this is safe.  */
9428                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
9429             }
9430           break;
9431
9432         case LABEL_REF:
9433         case CONST:
9434           output_asm_insn ("adr%?\t%0, %1", operands);
9435           output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
9436           break;
9437
9438           /* ??? This needs checking for thumb2.  */
9439         default:
9440           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
9441                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
9442             {
9443               otherops[0] = operands[0];
9444               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
9445               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
9446
9447               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
9448                 {
9449                   if (GET_CODE (otherops[2]) == CONST_INT)
9450                     {
9451                       switch ((int) INTVAL (otherops[2]))
9452                         {
9453                         case -8:
9454                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
9455                           return "";
9456                         case -4:
9457                           if (TARGET_THUMB2)
9458                             break;
9459                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
9460                           return "";
9461                         case 4:
9462                           if (TARGET_THUMB2)
9463                             break;
9464                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
9465                           return "";
9466                         }
9467                     }
9468                   if (TARGET_LDRD
9469                       && (GET_CODE (otherops[2]) == REG
9470                           || (GET_CODE (otherops[2]) == CONST_INT
9471                               && INTVAL (otherops[2]) > -256
9472                               && INTVAL (otherops[2]) < 256)))
9473                     {
9474                       if (reg_overlap_mentioned_p (otherops[0],
9475                                                    otherops[2]))
9476                         {
9477                           /* Swap base and index registers over to
9478                              avoid a conflict.  */
9479                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
9480                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
9481                         }
9482                       /* If both registers conflict, it will usually
9483                          have been fixed by a splitter.  */
9484                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9485                         {
9486                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
9487                           output_asm_insn ("ldr%(d%)\t%0, [%1]",
9488                                            otherops);
9489                         }
9490                       else
9491                         output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
9492                       return "";
9493                     }
9494
9495                   if (GET_CODE (otherops[2]) == CONST_INT)
9496                     {
9497                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
9498                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
9499                       else
9500                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
9501                     }
9502                   else
9503                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
9504                 }
9505               else
9506                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
9507
9508               return "ldm%(ia%)\t%0, %M0";
9509             }
9510           else
9511             {
9512               otherops[1] = adjust_address (operands[1], SImode, 4);
9513               /* Take care of overlapping base/data reg.  */
9514               if (reg_mentioned_p (operands[0], operands[1]))
9515                 {
9516                   output_asm_insn ("ldr%?\t%0, %1", otherops);
9517                   output_asm_insn ("ldr%?\t%0, %1", operands);
9518                 }
9519               else
9520                 {
9521                   output_asm_insn ("ldr%?\t%0, %1", operands);
9522                   output_asm_insn ("ldr%?\t%0, %1", otherops);
9523                 }
9524             }
9525         }
9526     }
9527   else
9528     {
9529       /* Constraints should ensure this.  */
9530       gcc_assert (code0 == MEM && code1 == REG);
9531       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
9532
9533       switch (GET_CODE (XEXP (operands[0], 0)))
9534         {
9535         case REG:
9536           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
9537           break;
9538
9539         case PRE_INC:
9540           gcc_assert (TARGET_LDRD);
9541           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
9542           break;
9543
9544         case PRE_DEC:
9545           if (TARGET_LDRD)
9546             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
9547           else
9548             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
9549           break;
9550
9551         case POST_INC:
9552           output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
9553           break;
9554
9555         case POST_DEC:
9556           gcc_assert (TARGET_LDRD);
9557           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
9558           break;
9559
9560         case PRE_MODIFY:
9561         case POST_MODIFY:
9562           otherops[0] = operands[1];
9563           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
9564           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
9565
9566           /* IWMMXT allows offsets larger than ldrd can handle,
9567              fix these up with a pair of ldr.  */
9568           if (GET_CODE (otherops[2]) == CONST_INT
9569               && (INTVAL(otherops[2]) <= -256
9570                   || INTVAL(otherops[2]) >= 256))
9571             {
9572               rtx reg1;
9573               reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9574               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
9575                 {
9576                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
9577                   otherops[0] = reg1;
9578                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9579                 }
9580               else
9581                 {
9582                   otherops[0] = reg1;
9583                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9584                   otherops[0] = operands[1];
9585                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
9586                 }
9587             }
9588           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
9589             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
9590           else
9591             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
9592           break;
9593
9594         case PLUS:
9595           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
9596           if (GET_CODE (otherops[2]) == CONST_INT)
9597             {
9598               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
9599                 {
9600                 case -8:
9601                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
9602                   return "";
9603
9604                 case -4:
9605                   if (TARGET_THUMB2)
9606                     break;
9607                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
9608                   return "";
9609
9610                 case 4:
9611                   if (TARGET_THUMB2)
9612                     break;
9613                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
9614                   return "";
9615                 }
9616             }
9617           if (TARGET_LDRD
9618               && (GET_CODE (otherops[2]) == REG
9619                   || (GET_CODE (otherops[2]) == CONST_INT
9620                       && INTVAL (otherops[2]) > -256
9621                       && INTVAL (otherops[2]) < 256)))
9622             {
9623               otherops[0] = operands[1];
9624               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
9625               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
9626               return "";
9627             }
9628           /* Fall through */
9629
9630         default:
9631           otherops[0] = adjust_address (operands[0], SImode, 4);
9632           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9633           output_asm_insn ("str%?\t%1, %0", operands);
9634           output_asm_insn ("str%?\t%1, %0", otherops);
9635         }
9636     }
9637
9638   return "";
9639 }
9640
9641 /* Output a VFP load or store instruction.  */
9642
9643 const char *
9644 output_move_vfp (rtx *operands)
9645 {
9646   rtx reg, mem, addr, ops[2];
9647   int load = REG_P (operands[0]);
9648   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
9649   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
9650   const char *template;
9651   char buff[50];
9652
9653   reg = operands[!load];
9654   mem = operands[load];
9655
9656   gcc_assert (REG_P (reg));
9657   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
9658   gcc_assert (GET_MODE (reg) == SFmode
9659               || GET_MODE (reg) == DFmode
9660               || GET_MODE (reg) == SImode
9661               || GET_MODE (reg) == DImode);
9662   gcc_assert (MEM_P (mem));
9663
9664   addr = XEXP (mem, 0);
9665
9666   switch (GET_CODE (addr))
9667     {
9668     case PRE_DEC:
9669       template = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
9670       ops[0] = XEXP (addr, 0);
9671       ops[1] = reg;
9672       break;
9673
9674     case POST_INC:
9675       template = "f%smia%c%%?\t%%0!, {%%%s1}%s";
9676       ops[0] = XEXP (addr, 0);
9677       ops[1] = reg;
9678       break;
9679
9680     default:
9681       template = "f%s%c%%?\t%%%s0, %%1%s";
9682       ops[0] = reg;
9683       ops[1] = mem;
9684       break;
9685     }
9686
9687   sprintf (buff, template,
9688            load ? "ld" : "st",
9689            dp ? 'd' : 's',
9690            dp ? "P" : "",
9691            integer_p ? "\t%@ int" : "");
9692   output_asm_insn (buff, ops);
9693
9694   return "";
9695 }
9696
9697 /* Output an ADD r, s, #n where n may be too big for one instruction.
9698    If adding zero to one register, output nothing.  */
9699 const char *
9700 output_add_immediate (rtx *operands)
9701 {
9702   HOST_WIDE_INT n = INTVAL (operands[2]);
9703
9704   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
9705     {
9706       if (n < 0)
9707         output_multi_immediate (operands,
9708                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
9709                                 -n);
9710       else
9711         output_multi_immediate (operands,
9712                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
9713                                 n);
9714     }
9715
9716   return "";
9717 }
9718
9719 /* Output a multiple immediate operation.
9720    OPERANDS is the vector of operands referred to in the output patterns.
9721    INSTR1 is the output pattern to use for the first constant.
9722    INSTR2 is the output pattern to use for subsequent constants.
9723    IMMED_OP is the index of the constant slot in OPERANDS.
9724    N is the constant value.  */
9725 static const char *
9726 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
9727                         int immed_op, HOST_WIDE_INT n)
9728 {
9729 #if HOST_BITS_PER_WIDE_INT > 32
9730   n &= 0xffffffff;
9731 #endif
9732
9733   if (n == 0)
9734     {
9735       /* Quick and easy output.  */
9736       operands[immed_op] = const0_rtx;
9737       output_asm_insn (instr1, operands);
9738     }
9739   else
9740     {
9741       int i;
9742       const char * instr = instr1;
9743
9744       /* Note that n is never zero here (which would give no output).  */
9745       for (i = 0; i < 32; i += 2)
9746         {
9747           if (n & (3 << i))
9748             {
9749               operands[immed_op] = GEN_INT (n & (255 << i));
9750               output_asm_insn (instr, operands);
9751               instr = instr2;
9752               i += 6;
9753             }
9754         }
9755     }
9756
9757   return "";
9758 }
9759
9760 /* Return the name of a shifter operation.  */
9761 static const char *
9762 arm_shift_nmem(enum rtx_code code)
9763 {
9764   switch (code)
9765     {
9766     case ASHIFT:
9767       return ARM_LSL_NAME;
9768
9769     case ASHIFTRT:
9770       return "asr";
9771
9772     case LSHIFTRT:
9773       return "lsr";
9774
9775     case ROTATERT:
9776       return "ror";
9777
9778     default:
9779       abort();
9780     }
9781 }
9782
9783 /* Return the appropriate ARM instruction for the operation code.
9784    The returned result should not be overwritten.  OP is the rtx of the
9785    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9786    was shifted.  */
9787 const char *
9788 arithmetic_instr (rtx op, int shift_first_arg)
9789 {
9790   switch (GET_CODE (op))
9791     {
9792     case PLUS:
9793       return "add";
9794
9795     case MINUS:
9796       return shift_first_arg ? "rsb" : "sub";
9797
9798     case IOR:
9799       return "orr";
9800
9801     case XOR:
9802       return "eor";
9803
9804     case AND:
9805       return "and";
9806
9807     case ASHIFT:
9808     case ASHIFTRT:
9809     case LSHIFTRT:
9810     case ROTATERT:
9811       return arm_shift_nmem(GET_CODE(op));
9812
9813     default:
9814       gcc_unreachable ();
9815     }
9816 }
9817
9818 /* Ensure valid constant shifts and return the appropriate shift mnemonic
9819    for the operation code.  The returned result should not be overwritten.
9820    OP is the rtx code of the shift.
9821    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9822    shift.  */
9823 static const char *
9824 shift_op (rtx op, HOST_WIDE_INT *amountp)
9825 {
9826   const char * mnem;
9827   enum rtx_code code = GET_CODE (op);
9828
9829   switch (GET_CODE (XEXP (op, 1)))
9830     {
9831     case REG:
9832     case SUBREG:
9833       *amountp = -1;
9834       break;
9835
9836     case CONST_INT:
9837       *amountp = INTVAL (XEXP (op, 1));
9838       break;
9839
9840     default:
9841       gcc_unreachable ();
9842     }
9843
9844   switch (code)
9845     {
9846     case ROTATE:
9847       gcc_assert (*amountp != -1);
9848       *amountp = 32 - *amountp;
9849       code = ROTATERT;
9850
9851       /* Fall through.  */
9852
9853     case ASHIFT:
9854     case ASHIFTRT:
9855     case LSHIFTRT:
9856     case ROTATERT:
9857       mnem = arm_shift_nmem(code);
9858       break;
9859
9860     case MULT:
9861       /* We never have to worry about the amount being other than a
9862          power of 2, since this case can never be reloaded from a reg.  */
9863       gcc_assert (*amountp != -1);
9864       *amountp = int_log2 (*amountp);
9865       return ARM_LSL_NAME;
9866
9867     default:
9868       gcc_unreachable ();
9869     }
9870
9871   if (*amountp != -1)
9872     {
9873       /* This is not 100% correct, but follows from the desire to merge
9874          multiplication by a power of 2 with the recognizer for a
9875          shift.  >=32 is not a valid shift for "lsl", so we must try and
9876          output a shift that produces the correct arithmetical result.
9877          Using lsr #32 is identical except for the fact that the carry bit
9878          is not set correctly if we set the flags; but we never use the
9879          carry bit from such an operation, so we can ignore that.  */
9880       if (code == ROTATERT)
9881         /* Rotate is just modulo 32.  */
9882         *amountp &= 31;
9883       else if (*amountp != (*amountp & 31))
9884         {
9885           if (code == ASHIFT)
9886             mnem = "lsr";
9887           *amountp = 32;
9888         }
9889
9890       /* Shifts of 0 are no-ops.  */
9891       if (*amountp == 0)
9892         return NULL;
9893     }
9894
9895   return mnem;
9896 }
9897
9898 /* Obtain the shift from the POWER of two.  */
9899
9900 static HOST_WIDE_INT
9901 int_log2 (HOST_WIDE_INT power)
9902 {
9903   HOST_WIDE_INT shift = 0;
9904
9905   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9906     {
9907       gcc_assert (shift <= 31);
9908       shift++;
9909     }
9910
9911   return shift;
9912 }
9913
9914 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
9915    because /bin/as is horribly restrictive.  The judgement about
9916    whether or not each character is 'printable' (and can be output as
9917    is) or not (and must be printed with an octal escape) must be made
9918    with reference to the *host* character set -- the situation is
9919    similar to that discussed in the comments above pp_c_char in
9920    c-pretty-print.c.  */
9921
9922 #define MAX_ASCII_LEN 51
9923
9924 void
9925 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9926 {
9927   int i;
9928   int len_so_far = 0;
9929
9930   fputs ("\t.ascii\t\"", stream);
9931
9932   for (i = 0; i < len; i++)
9933     {
9934       int c = p[i];
9935
9936       if (len_so_far >= MAX_ASCII_LEN)
9937         {
9938           fputs ("\"\n\t.ascii\t\"", stream);
9939           len_so_far = 0;
9940         }
9941
9942       if (ISPRINT (c))
9943         {
9944           if (c == '\\' || c == '\"')
9945             {
9946               putc ('\\', stream);
9947               len_so_far++;
9948             }
9949           putc (c, stream);
9950           len_so_far++;
9951         }
9952       else
9953         {
9954           fprintf (stream, "\\%03o", c);
9955           len_so_far += 4;
9956         }
9957     }
9958
9959   fputs ("\"\n", stream);
9960 }
9961 \f
9962 /* Compute the register save mask for registers 0 through 12
9963    inclusive.  This code is used by arm_compute_save_reg_mask.  */
9964
9965 static unsigned long
9966 arm_compute_save_reg0_reg12_mask (void)
9967 {
9968   unsigned long func_type = arm_current_func_type ();
9969   unsigned long save_reg_mask = 0;
9970   unsigned int reg;
9971
9972   if (IS_INTERRUPT (func_type))
9973     {
9974       unsigned int max_reg;
9975       /* Interrupt functions must not corrupt any registers,
9976          even call clobbered ones.  If this is a leaf function
9977          we can just examine the registers used by the RTL, but
9978          otherwise we have to assume that whatever function is
9979          called might clobber anything, and so we have to save
9980          all the call-clobbered registers as well.  */
9981       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9982         /* FIQ handlers have registers r8 - r12 banked, so
9983            we only need to check r0 - r7, Normal ISRs only
9984            bank r14 and r15, so we must check up to r12.
9985            r13 is the stack pointer which is always preserved,
9986            so we do not need to consider it here.  */
9987         max_reg = 7;
9988       else
9989         max_reg = 12;
9990
9991       for (reg = 0; reg <= max_reg; reg++)
9992         if (df_regs_ever_live_p (reg)
9993             || (! current_function_is_leaf && call_used_regs[reg]))
9994           save_reg_mask |= (1 << reg);
9995
9996       /* Also save the pic base register if necessary.  */
9997       if (flag_pic
9998           && !TARGET_SINGLE_PIC_BASE
9999           && arm_pic_register != INVALID_REGNUM
10000           && current_function_uses_pic_offset_table)
10001         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10002     }
10003   else
10004     {
10005       /* In arm mode we handle r11 (FP) as a special case.  */
10006       unsigned last_reg = TARGET_ARM ? 10 : 11;
10007       
10008       /* In the normal case we only need to save those registers
10009          which are call saved and which are used by this function.  */
10010       for (reg = 0; reg <= last_reg; reg++)
10011         if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
10012           save_reg_mask |= (1 << reg);
10013
10014       /* Handle the frame pointer as a special case.  */
10015       if (! TARGET_APCS_FRAME
10016           && ! frame_pointer_needed
10017           && df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM)
10018           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
10019         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
10020       else if (! TARGET_APCS_FRAME
10021                && ! frame_pointer_needed
10022                && df_regs_ever_live_p (HARD_FRAME_POINTER_REGNUM)
10023                && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
10024         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
10025
10026       /* If we aren't loading the PIC register,
10027          don't stack it even though it may be live.  */
10028       if (flag_pic
10029           && !TARGET_SINGLE_PIC_BASE
10030           && arm_pic_register != INVALID_REGNUM
10031           && (df_regs_ever_live_p (PIC_OFFSET_TABLE_REGNUM)
10032               || current_function_uses_pic_offset_table))
10033         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10034
10035       /* The prologue will copy SP into R0, so save it.  */
10036       if (IS_STACKALIGN (func_type))
10037         save_reg_mask |= 1;
10038     }
10039
10040   /* Save registers so the exception handler can modify them.  */
10041   if (current_function_calls_eh_return)
10042     {
10043       unsigned int i;
10044
10045       for (i = 0; ; i++)
10046         {
10047           reg = EH_RETURN_DATA_REGNO (i);
10048           if (reg == INVALID_REGNUM)
10049             break;
10050           save_reg_mask |= 1 << reg;
10051         }
10052     }
10053
10054   return save_reg_mask;
10055 }
10056
10057
10058 /* Compute a bit mask of which registers need to be
10059    saved on the stack for the current function.  */
10060
10061 static unsigned long
10062 arm_compute_save_reg_mask (void)
10063 {
10064   unsigned int save_reg_mask = 0;
10065   unsigned long func_type = arm_current_func_type ();
10066   unsigned int reg;
10067
10068   if (IS_NAKED (func_type))
10069     /* This should never really happen.  */
10070     return 0;
10071
10072   /* If we are creating a stack frame, then we must save the frame pointer,
10073      IP (which will hold the old stack pointer), LR and the PC.  */
10074   if (frame_pointer_needed && TARGET_ARM)
10075     save_reg_mask |=
10076       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
10077       | (1 << IP_REGNUM)
10078       | (1 << LR_REGNUM)
10079       | (1 << PC_REGNUM);
10080
10081   /* Volatile functions do not return, so there
10082      is no need to save any other registers.  */
10083   if (IS_VOLATILE (func_type))
10084     return save_reg_mask;
10085
10086   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
10087
10088   /* Decide if we need to save the link register.
10089      Interrupt routines have their own banked link register,
10090      so they never need to save it.
10091      Otherwise if we do not use the link register we do not need to save
10092      it.  If we are pushing other registers onto the stack however, we
10093      can save an instruction in the epilogue by pushing the link register
10094      now and then popping it back into the PC.  This incurs extra memory
10095      accesses though, so we only do it when optimizing for size, and only
10096      if we know that we will not need a fancy return sequence.  */
10097   if (df_regs_ever_live_p (LR_REGNUM)
10098       || (save_reg_mask
10099           && optimize_size
10100           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10101           && !current_function_calls_eh_return))
10102     save_reg_mask |= 1 << LR_REGNUM;
10103
10104   if (cfun->machine->lr_save_eliminated)
10105     save_reg_mask &= ~ (1 << LR_REGNUM);
10106
10107   if (TARGET_REALLY_IWMMXT
10108       && ((bit_count (save_reg_mask)
10109            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
10110     {
10111       /* The total number of registers that are going to be pushed
10112          onto the stack is odd.  We need to ensure that the stack
10113          is 64-bit aligned before we start to save iWMMXt registers,
10114          and also before we start to create locals.  (A local variable
10115          might be a double or long long which we will load/store using
10116          an iWMMXt instruction).  Therefore we need to push another
10117          ARM register, so that the stack will be 64-bit aligned.  We
10118          try to avoid using the arg registers (r0 -r3) as they might be
10119          used to pass values in a tail call.  */
10120       for (reg = 4; reg <= 12; reg++)
10121         if ((save_reg_mask & (1 << reg)) == 0)
10122           break;
10123
10124       if (reg <= 12)
10125         save_reg_mask |= (1 << reg);
10126       else
10127         {
10128           cfun->machine->sibcall_blocked = 1;
10129           save_reg_mask |= (1 << 3);
10130         }
10131     }
10132
10133   /* We may need to push an additional register for use initializing the
10134      PIC base register.  */
10135   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
10136       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
10137     {
10138       reg = thumb_find_work_register (1 << 4);
10139       if (!call_used_regs[reg])
10140         save_reg_mask |= (1 << reg);
10141     }
10142
10143   return save_reg_mask;
10144 }
10145
10146
10147 /* Compute a bit mask of which registers need to be
10148    saved on the stack for the current function.  */
10149 static unsigned long
10150 thumb1_compute_save_reg_mask (void)
10151 {
10152   unsigned long mask;
10153   unsigned reg;
10154
10155   mask = 0;
10156   for (reg = 0; reg < 12; reg ++)
10157     if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10158       mask |= 1 << reg;
10159
10160   if (flag_pic
10161       && !TARGET_SINGLE_PIC_BASE
10162       && arm_pic_register != INVALID_REGNUM
10163       && current_function_uses_pic_offset_table)
10164     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
10165
10166   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
10167   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
10168     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
10169
10170   /* LR will also be pushed if any lo regs are pushed.  */
10171   if (mask & 0xff || thumb_force_lr_save ())
10172     mask |= (1 << LR_REGNUM);
10173
10174   /* Make sure we have a low work register if we need one.
10175      We will need one if we are going to push a high register,
10176      but we are not currently intending to push a low register.  */
10177   if ((mask & 0xff) == 0
10178       && ((mask & 0x0f00) || TARGET_BACKTRACE))
10179     {
10180       /* Use thumb_find_work_register to choose which register
10181          we will use.  If the register is live then we will
10182          have to push it.  Use LAST_LO_REGNUM as our fallback
10183          choice for the register to select.  */
10184       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
10185
10186       if (! call_used_regs[reg])
10187         mask |= 1 << reg;
10188     }
10189
10190   return mask;
10191 }
10192
10193
10194 /* Return the number of bytes required to save VFP registers.  */
10195 static int
10196 arm_get_vfp_saved_size (void)
10197 {
10198   unsigned int regno;
10199   int count;
10200   int saved;
10201
10202   saved = 0;
10203   /* Space for saved VFP registers.  */
10204   if (TARGET_HARD_FLOAT && TARGET_VFP)
10205     {
10206       count = 0;
10207       for (regno = FIRST_VFP_REGNUM;
10208            regno < LAST_VFP_REGNUM;
10209            regno += 2)
10210         {
10211           if ((!df_regs_ever_live_p (regno) || call_used_regs[regno])
10212               && (!df_regs_ever_live_p (regno + 1) || call_used_regs[regno + 1]))
10213             {
10214               if (count > 0)
10215                 {
10216                   /* Workaround ARM10 VFPr1 bug.  */
10217                   if (count == 2 && !arm_arch6)
10218                     count++;
10219                   saved += count * 8;
10220                 }
10221               count = 0;
10222             }
10223           else
10224             count++;
10225         }
10226       if (count > 0)
10227         {
10228           if (count == 2 && !arm_arch6)
10229             count++;
10230           saved += count * 8;
10231         }
10232     }
10233   return saved;
10234 }
10235
10236
10237 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
10238    everything bar the final return instruction.  */
10239 const char *
10240 output_return_instruction (rtx operand, int really_return, int reverse)
10241 {
10242   char conditional[10];
10243   char instr[100];
10244   unsigned reg;
10245   unsigned long live_regs_mask;
10246   unsigned long func_type;
10247   arm_stack_offsets *offsets;
10248
10249   func_type = arm_current_func_type ();
10250
10251   if (IS_NAKED (func_type))
10252     return "";
10253
10254   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
10255     {
10256       /* If this function was declared non-returning, and we have
10257          found a tail call, then we have to trust that the called
10258          function won't return.  */
10259       if (really_return)
10260         {
10261           rtx ops[2];
10262
10263           /* Otherwise, trap an attempted return by aborting.  */
10264           ops[0] = operand;
10265           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
10266                                        : "abort");
10267           assemble_external_libcall (ops[1]);
10268           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
10269         }
10270
10271       return "";
10272     }
10273
10274   gcc_assert (!current_function_calls_alloca || really_return);
10275
10276   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
10277
10278   return_used_this_function = 1;
10279
10280   live_regs_mask = arm_compute_save_reg_mask ();
10281
10282   if (live_regs_mask)
10283     {
10284       const char * return_reg;
10285
10286       /* If we do not have any special requirements for function exit
10287          (e.g. interworking) then we can load the return address
10288          directly into the PC.  Otherwise we must load it into LR.  */
10289       if (really_return
10290           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
10291         return_reg = reg_names[PC_REGNUM];
10292       else
10293         return_reg = reg_names[LR_REGNUM];
10294
10295       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
10296         {
10297           /* There are three possible reasons for the IP register
10298              being saved.  1) a stack frame was created, in which case
10299              IP contains the old stack pointer, or 2) an ISR routine
10300              corrupted it, or 3) it was saved to align the stack on
10301              iWMMXt.  In case 1, restore IP into SP, otherwise just
10302              restore IP.  */
10303           if (frame_pointer_needed)
10304             {
10305               live_regs_mask &= ~ (1 << IP_REGNUM);
10306               live_regs_mask |=   (1 << SP_REGNUM);
10307             }
10308           else
10309             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
10310         }
10311
10312       /* On some ARM architectures it is faster to use LDR rather than
10313          LDM to load a single register.  On other architectures, the
10314          cost is the same.  In 26 bit mode, or for exception handlers,
10315          we have to use LDM to load the PC so that the CPSR is also
10316          restored.  */
10317       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
10318         if (live_regs_mask == (1U << reg))
10319           break;
10320
10321       if (reg <= LAST_ARM_REGNUM
10322           && (reg != LR_REGNUM
10323               || ! really_return
10324               || ! IS_INTERRUPT (func_type)))
10325         {
10326           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
10327                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
10328         }
10329       else
10330         {
10331           char *p;
10332           int first = 1;
10333
10334           /* Generate the load multiple instruction to restore the
10335              registers.  Note we can get here, even if
10336              frame_pointer_needed is true, but only if sp already
10337              points to the base of the saved core registers.  */
10338           if (live_regs_mask & (1 << SP_REGNUM))
10339             {
10340               unsigned HOST_WIDE_INT stack_adjust;
10341
10342               offsets = arm_get_frame_offsets ();
10343               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
10344               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
10345
10346               if (stack_adjust && arm_arch5 && TARGET_ARM)
10347                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
10348               else
10349                 {
10350                   /* If we can't use ldmib (SA110 bug),
10351                      then try to pop r3 instead.  */
10352                   if (stack_adjust)
10353                     live_regs_mask |= 1 << 3;
10354                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
10355                 }
10356             }
10357           else
10358             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
10359
10360           p = instr + strlen (instr);
10361
10362           for (reg = 0; reg <= SP_REGNUM; reg++)
10363             if (live_regs_mask & (1 << reg))
10364               {
10365                 int l = strlen (reg_names[reg]);
10366
10367                 if (first)
10368                   first = 0;
10369                 else
10370                   {
10371                     memcpy (p, ", ", 2);
10372                     p += 2;
10373                   }
10374
10375                 memcpy (p, "%|", 2);
10376                 memcpy (p + 2, reg_names[reg], l);
10377                 p += l + 2;
10378               }
10379
10380           if (live_regs_mask & (1 << LR_REGNUM))
10381             {
10382               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
10383               /* If returning from an interrupt, restore the CPSR.  */
10384               if (IS_INTERRUPT (func_type))
10385                 strcat (p, "^");
10386             }
10387           else
10388             strcpy (p, "}");
10389         }
10390
10391       output_asm_insn (instr, & operand);
10392
10393       /* See if we need to generate an extra instruction to
10394          perform the actual function return.  */
10395       if (really_return
10396           && func_type != ARM_FT_INTERWORKED
10397           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
10398         {
10399           /* The return has already been handled
10400              by loading the LR into the PC.  */
10401           really_return = 0;
10402         }
10403     }
10404
10405   if (really_return)
10406     {
10407       switch ((int) ARM_FUNC_TYPE (func_type))
10408         {
10409         case ARM_FT_ISR:
10410         case ARM_FT_FIQ:
10411           /* ??? This is wrong for unified assembly syntax.  */
10412           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
10413           break;
10414
10415         case ARM_FT_INTERWORKED:
10416           sprintf (instr, "bx%s\t%%|lr", conditional);
10417           break;
10418
10419         case ARM_FT_EXCEPTION:
10420           /* ??? This is wrong for unified assembly syntax.  */
10421           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
10422           break;
10423
10424         default:
10425           /* Use bx if it's available.  */
10426           if (arm_arch5 || arm_arch4t)
10427             sprintf (instr, "bx%s\t%%|lr", conditional);
10428           else
10429             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
10430           break;
10431         }
10432
10433       output_asm_insn (instr, & operand);
10434     }
10435
10436   return "";
10437 }
10438
10439 /* Write the function name into the code section, directly preceding
10440    the function prologue.
10441
10442    Code will be output similar to this:
10443      t0
10444          .ascii "arm_poke_function_name", 0
10445          .align
10446      t1
10447          .word 0xff000000 + (t1 - t0)
10448      arm_poke_function_name
10449          mov     ip, sp
10450          stmfd   sp!, {fp, ip, lr, pc}
10451          sub     fp, ip, #4
10452
10453    When performing a stack backtrace, code can inspect the value
10454    of 'pc' stored at 'fp' + 0.  If the trace function then looks
10455    at location pc - 12 and the top 8 bits are set, then we know
10456    that there is a function name embedded immediately preceding this
10457    location and has length ((pc[-3]) & 0xff000000).
10458
10459    We assume that pc is declared as a pointer to an unsigned long.
10460
10461    It is of no benefit to output the function name if we are assembling
10462    a leaf function.  These function types will not contain a stack
10463    backtrace structure, therefore it is not possible to determine the
10464    function name.  */
10465 void
10466 arm_poke_function_name (FILE *stream, const char *name)
10467 {
10468   unsigned long alignlength;
10469   unsigned long length;
10470   rtx           x;
10471
10472   length      = strlen (name) + 1;
10473   alignlength = ROUND_UP_WORD (length);
10474
10475   ASM_OUTPUT_ASCII (stream, name, length);
10476   ASM_OUTPUT_ALIGN (stream, 2);
10477   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
10478   assemble_aligned_integer (UNITS_PER_WORD, x);
10479 }
10480
10481 /* Place some comments into the assembler stream
10482    describing the current function.  */
10483 static void
10484 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
10485 {
10486   unsigned long func_type;
10487
10488   if (TARGET_THUMB1)
10489     {
10490       thumb1_output_function_prologue (f, frame_size);
10491       return;
10492     }
10493
10494   /* Sanity check.  */
10495   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
10496
10497   func_type = arm_current_func_type ();
10498
10499   switch ((int) ARM_FUNC_TYPE (func_type))
10500     {
10501     default:
10502     case ARM_FT_NORMAL:
10503       break;
10504     case ARM_FT_INTERWORKED:
10505       asm_fprintf (f, "\t%@ Function supports interworking.\n");
10506       break;
10507     case ARM_FT_ISR:
10508       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
10509       break;
10510     case ARM_FT_FIQ:
10511       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
10512       break;
10513     case ARM_FT_EXCEPTION:
10514       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
10515       break;
10516     }
10517
10518   if (IS_NAKED (func_type))
10519     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
10520
10521   if (IS_VOLATILE (func_type))
10522     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
10523
10524   if (IS_NESTED (func_type))
10525     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
10526   if (IS_STACKALIGN (func_type))
10527     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
10528
10529   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
10530                current_function_args_size,
10531                current_function_pretend_args_size, frame_size);
10532
10533   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
10534                frame_pointer_needed,
10535                cfun->machine->uses_anonymous_args);
10536
10537   if (cfun->machine->lr_save_eliminated)
10538     asm_fprintf (f, "\t%@ link register save eliminated.\n");
10539
10540   if (current_function_calls_eh_return)
10541     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
10542
10543 #ifdef AOF_ASSEMBLER
10544   if (flag_pic)
10545     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
10546 #endif
10547
10548   return_used_this_function = 0;
10549 }
10550
10551 const char *
10552 arm_output_epilogue (rtx sibling)
10553 {
10554   int reg;
10555   unsigned long saved_regs_mask;
10556   unsigned long func_type;
10557   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
10558      frame that is $fp + 4 for a non-variadic function.  */
10559   int floats_offset = 0;
10560   rtx operands[3];
10561   FILE * f = asm_out_file;
10562   unsigned int lrm_count = 0;
10563   int really_return = (sibling == NULL);
10564   int start_reg;
10565   arm_stack_offsets *offsets;
10566
10567   /* If we have already generated the return instruction
10568      then it is futile to generate anything else.  */
10569   if (use_return_insn (FALSE, sibling) && return_used_this_function)
10570     return "";
10571
10572   func_type = arm_current_func_type ();
10573
10574   if (IS_NAKED (func_type))
10575     /* Naked functions don't have epilogues.  */
10576     return "";
10577
10578   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
10579     {
10580       rtx op;
10581
10582       /* A volatile function should never return.  Call abort.  */
10583       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
10584       assemble_external_libcall (op);
10585       output_asm_insn ("bl\t%a0", &op);
10586
10587       return "";
10588     }
10589
10590   /* If we are throwing an exception, then we really must be doing a
10591      return, so we can't tail-call.  */
10592   gcc_assert (!current_function_calls_eh_return || really_return);
10593
10594   offsets = arm_get_frame_offsets ();
10595   saved_regs_mask = arm_compute_save_reg_mask ();
10596
10597   if (TARGET_IWMMXT)
10598     lrm_count = bit_count (saved_regs_mask);
10599
10600   floats_offset = offsets->saved_args;
10601   /* Compute how far away the floats will be.  */
10602   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
10603     if (saved_regs_mask & (1 << reg))
10604       floats_offset += 4;
10605
10606   if (frame_pointer_needed && TARGET_ARM)
10607     {
10608       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
10609       int vfp_offset = offsets->frame;
10610
10611       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10612         {
10613           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10614             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10615               {
10616                 floats_offset += 12;
10617                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
10618                              reg, FP_REGNUM, floats_offset - vfp_offset);
10619               }
10620         }
10621       else
10622         {
10623           start_reg = LAST_FPA_REGNUM;
10624
10625           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10626             {
10627               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10628                 {
10629                   floats_offset += 12;
10630
10631                   /* We can't unstack more than four registers at once.  */
10632                   if (start_reg - reg == 3)
10633                     {
10634                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
10635                                    reg, FP_REGNUM, floats_offset - vfp_offset);
10636                       start_reg = reg - 1;
10637                     }
10638                 }
10639               else
10640                 {
10641                   if (reg != start_reg)
10642                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
10643                                  reg + 1, start_reg - reg,
10644                                  FP_REGNUM, floats_offset - vfp_offset);
10645                   start_reg = reg - 1;
10646                 }
10647             }
10648
10649           /* Just in case the last register checked also needs unstacking.  */
10650           if (reg != start_reg)
10651             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
10652                          reg + 1, start_reg - reg,
10653                          FP_REGNUM, floats_offset - vfp_offset);
10654         }
10655
10656       if (TARGET_HARD_FLOAT && TARGET_VFP)
10657         {
10658           int saved_size;
10659
10660           /* The fldmd insns do not have base+offset addressing
10661              modes, so we use IP to hold the address.  */
10662           saved_size = arm_get_vfp_saved_size ();
10663
10664           if (saved_size > 0)
10665             {
10666               floats_offset += saved_size;
10667               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
10668                            FP_REGNUM, floats_offset - vfp_offset);
10669             }
10670           start_reg = FIRST_VFP_REGNUM;
10671           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10672             {
10673               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
10674                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
10675                 {
10676                   if (start_reg != reg)
10677                     vfp_output_fldmd (f, IP_REGNUM,
10678                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10679                                       (reg - start_reg) / 2);
10680                   start_reg = reg + 2;
10681                 }
10682             }
10683           if (start_reg != reg)
10684             vfp_output_fldmd (f, IP_REGNUM,
10685                               (start_reg - FIRST_VFP_REGNUM) / 2,
10686                               (reg - start_reg) / 2);
10687         }
10688
10689       if (TARGET_IWMMXT)
10690         {
10691           /* The frame pointer is guaranteed to be non-double-word aligned.
10692              This is because it is set to (old_stack_pointer - 4) and the
10693              old_stack_pointer was double word aligned.  Thus the offset to
10694              the iWMMXt registers to be loaded must also be non-double-word
10695              sized, so that the resultant address *is* double-word aligned.
10696              We can ignore floats_offset since that was already included in
10697              the live_regs_mask.  */
10698           lrm_count += (lrm_count % 2 ? 2 : 1);
10699
10700           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10701             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10702               {
10703                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
10704                              reg, FP_REGNUM, lrm_count * 4);
10705                 lrm_count += 2;
10706               }
10707         }
10708
10709       /* saved_regs_mask should contain the IP, which at the time of stack
10710          frame generation actually contains the old stack pointer.  So a
10711          quick way to unwind the stack is just pop the IP register directly
10712          into the stack pointer.  */
10713       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
10714       saved_regs_mask &= ~ (1 << IP_REGNUM);
10715       saved_regs_mask |=   (1 << SP_REGNUM);
10716
10717       /* There are two registers left in saved_regs_mask - LR and PC.  We
10718          only need to restore the LR register (the return address), but to
10719          save time we can load it directly into the PC, unless we need a
10720          special function exit sequence, or we are not really returning.  */
10721       if (really_return
10722           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10723           && !current_function_calls_eh_return)
10724         /* Delete the LR from the register mask, so that the LR on
10725            the stack is loaded into the PC in the register mask.  */
10726         saved_regs_mask &= ~ (1 << LR_REGNUM);
10727       else
10728         saved_regs_mask &= ~ (1 << PC_REGNUM);
10729
10730       /* We must use SP as the base register, because SP is one of the
10731          registers being restored.  If an interrupt or page fault
10732          happens in the ldm instruction, the SP might or might not
10733          have been restored.  That would be bad, as then SP will no
10734          longer indicate the safe area of stack, and we can get stack
10735          corruption.  Using SP as the base register means that it will
10736          be reset correctly to the original value, should an interrupt
10737          occur.  If the stack pointer already points at the right
10738          place, then omit the subtraction.  */
10739       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
10740           || current_function_calls_alloca)
10741         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
10742                      4 * bit_count (saved_regs_mask));
10743       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
10744
10745       if (IS_INTERRUPT (func_type))
10746         /* Interrupt handlers will have pushed the
10747            IP onto the stack, so restore it now.  */
10748         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
10749     }
10750   else
10751     {
10752       HOST_WIDE_INT amount;
10753       int rfe;
10754       /* Restore stack pointer if necessary.  */
10755       if (frame_pointer_needed)
10756         {
10757           /* For Thumb-2 restore sp from the frame pointer.
10758              Operand restrictions mean we have to increment FP, then copy
10759              to SP.  */
10760           amount = offsets->locals_base - offsets->saved_regs;
10761           operands[0] = hard_frame_pointer_rtx;
10762         }
10763       else
10764         {
10765           operands[0] = stack_pointer_rtx;
10766           amount = offsets->outgoing_args - offsets->saved_regs;
10767         }
10768
10769       if (amount)
10770         {
10771           operands[1] = operands[0];
10772           operands[2] = GEN_INT (amount);
10773           output_add_immediate (operands);
10774         }
10775       if (frame_pointer_needed)
10776         asm_fprintf (f, "\tmov\t%r, %r\n",
10777                      SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
10778
10779       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10780         {
10781           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10782             if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10783               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
10784                            reg, SP_REGNUM);
10785         }
10786       else
10787         {
10788           start_reg = FIRST_FPA_REGNUM;
10789
10790           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10791             {
10792               if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10793                 {
10794                   if (reg - start_reg == 3)
10795                     {
10796                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
10797                                    start_reg, SP_REGNUM);
10798                       start_reg = reg + 1;
10799                     }
10800                 }
10801               else
10802                 {
10803                   if (reg != start_reg)
10804                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10805                                  start_reg, reg - start_reg,
10806                                  SP_REGNUM);
10807
10808                   start_reg = reg + 1;
10809                 }
10810             }
10811
10812           /* Just in case the last register checked also needs unstacking.  */
10813           if (reg != start_reg)
10814             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10815                          start_reg, reg - start_reg, SP_REGNUM);
10816         }
10817
10818       if (TARGET_HARD_FLOAT && TARGET_VFP)
10819         {
10820           start_reg = FIRST_VFP_REGNUM;
10821           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10822             {
10823               if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
10824                   && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
10825                 {
10826                   if (start_reg != reg)
10827                     vfp_output_fldmd (f, SP_REGNUM,
10828                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10829                                       (reg - start_reg) / 2);
10830                   start_reg = reg + 2;
10831                 }
10832             }
10833           if (start_reg != reg)
10834             vfp_output_fldmd (f, SP_REGNUM,
10835                               (start_reg - FIRST_VFP_REGNUM) / 2,
10836                               (reg - start_reg) / 2);
10837         }
10838       if (TARGET_IWMMXT)
10839         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10840           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
10841             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10842
10843       /* If we can, restore the LR into the PC.  */
10844       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
10845           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
10846           && !IS_STACKALIGN (func_type)
10847           && really_return
10848           && current_function_pretend_args_size == 0
10849           && saved_regs_mask & (1 << LR_REGNUM)
10850           && !current_function_calls_eh_return)
10851         {
10852           saved_regs_mask &= ~ (1 << LR_REGNUM);
10853           saved_regs_mask |=   (1 << PC_REGNUM);
10854           rfe = IS_INTERRUPT (func_type);
10855         }
10856       else
10857         rfe = 0;
10858
10859       /* Load the registers off the stack.  If we only have one register
10860          to load use the LDR instruction - it is faster.  For Thumb-2
10861          always use pop and the assembler will pick the best instruction.*/
10862       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
10863           && !IS_INTERRUPT(func_type))
10864         {
10865           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10866         }
10867       else if (saved_regs_mask)
10868         {
10869           if (saved_regs_mask & (1 << SP_REGNUM))
10870             /* Note - write back to the stack register is not enabled
10871                (i.e. "ldmfd sp!...").  We know that the stack pointer is
10872                in the list of registers and if we add writeback the
10873                instruction becomes UNPREDICTABLE.  */
10874             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
10875                              rfe);
10876           else if (TARGET_ARM)
10877             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
10878                              rfe);
10879           else
10880             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
10881         }
10882
10883       if (current_function_pretend_args_size)
10884         {
10885           /* Unwind the pre-pushed regs.  */
10886           operands[0] = operands[1] = stack_pointer_rtx;
10887           operands[2] = GEN_INT (current_function_pretend_args_size);
10888           output_add_immediate (operands);
10889         }
10890     }
10891
10892   /* We may have already restored PC directly from the stack.  */
10893   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10894     return "";
10895
10896   /* Stack adjustment for exception handler.  */
10897   if (current_function_calls_eh_return)
10898     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10899                  ARM_EH_STACKADJ_REGNUM);
10900
10901   /* Generate the return instruction.  */
10902   switch ((int) ARM_FUNC_TYPE (func_type))
10903     {
10904     case ARM_FT_ISR:
10905     case ARM_FT_FIQ:
10906       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10907       break;
10908
10909     case ARM_FT_EXCEPTION:
10910       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10911       break;
10912
10913     case ARM_FT_INTERWORKED:
10914       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10915       break;
10916
10917     default:
10918       if (IS_STACKALIGN (func_type))
10919         {
10920           /* See comment in arm_expand_prologue.  */
10921           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
10922         }
10923       if (arm_arch5 || arm_arch4t)
10924         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10925       else
10926         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10927       break;
10928     }
10929
10930   return "";
10931 }
10932
10933 static void
10934 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10935                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10936 {
10937   arm_stack_offsets *offsets;
10938
10939   if (TARGET_THUMB1)
10940     {
10941       int regno;
10942
10943       /* Emit any call-via-reg trampolines that are needed for v4t support
10944          of call_reg and call_value_reg type insns.  */
10945       for (regno = 0; regno < LR_REGNUM; regno++)
10946         {
10947           rtx label = cfun->machine->call_via[regno];
10948
10949           if (label != NULL)
10950             {
10951               switch_to_section (function_section (current_function_decl));
10952               targetm.asm_out.internal_label (asm_out_file, "L",
10953                                               CODE_LABEL_NUMBER (label));
10954               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10955             }
10956         }
10957
10958       /* ??? Probably not safe to set this here, since it assumes that a
10959          function will be emitted as assembly immediately after we generate
10960          RTL for it.  This does not happen for inline functions.  */
10961       return_used_this_function = 0;
10962     }
10963   else /* TARGET_32BIT */
10964     {
10965       /* We need to take into account any stack-frame rounding.  */
10966       offsets = arm_get_frame_offsets ();
10967
10968       gcc_assert (!use_return_insn (FALSE, NULL)
10969                   || !return_used_this_function
10970                   || offsets->saved_regs == offsets->outgoing_args
10971                   || frame_pointer_needed);
10972
10973       /* Reset the ARM-specific per-function variables.  */
10974       after_arm_reorg = 0;
10975     }
10976 }
10977
10978 /* Generate and emit an insn that we will recognize as a push_multi.
10979    Unfortunately, since this insn does not reflect very well the actual
10980    semantics of the operation, we need to annotate the insn for the benefit
10981    of DWARF2 frame unwind information.  */
10982 static rtx
10983 emit_multi_reg_push (unsigned long mask)
10984 {
10985   int num_regs = 0;
10986   int num_dwarf_regs;
10987   int i, j;
10988   rtx par;
10989   rtx dwarf;
10990   int dwarf_par_index;
10991   rtx tmp, reg;
10992
10993   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10994     if (mask & (1 << i))
10995       num_regs++;
10996
10997   gcc_assert (num_regs && num_regs <= 16);
10998
10999   /* We don't record the PC in the dwarf frame information.  */
11000   num_dwarf_regs = num_regs;
11001   if (mask & (1 << PC_REGNUM))
11002     num_dwarf_regs--;
11003
11004   /* For the body of the insn we are going to generate an UNSPEC in
11005      parallel with several USEs.  This allows the insn to be recognized
11006      by the push_multi pattern in the arm.md file.  The insn looks
11007      something like this:
11008
11009        (parallel [
11010            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
11011                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
11012            (use (reg:SI 11 fp))
11013            (use (reg:SI 12 ip))
11014            (use (reg:SI 14 lr))
11015            (use (reg:SI 15 pc))
11016         ])
11017
11018      For the frame note however, we try to be more explicit and actually
11019      show each register being stored into the stack frame, plus a (single)
11020      decrement of the stack pointer.  We do it this way in order to be
11021      friendly to the stack unwinding code, which only wants to see a single
11022      stack decrement per instruction.  The RTL we generate for the note looks
11023      something like this:
11024
11025       (sequence [
11026            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
11027            (set (mem:SI (reg:SI sp)) (reg:SI r4))
11028            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
11029            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
11030            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
11031         ])
11032
11033       This sequence is used both by the code to support stack unwinding for
11034       exceptions handlers and the code to generate dwarf2 frame debugging.  */
11035
11036   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
11037   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
11038   dwarf_par_index = 1;
11039
11040   for (i = 0; i <= LAST_ARM_REGNUM; i++)
11041     {
11042       if (mask & (1 << i))
11043         {
11044           reg = gen_rtx_REG (SImode, i);
11045
11046           XVECEXP (par, 0, 0)
11047             = gen_rtx_SET (VOIDmode,
11048                            gen_frame_mem (BLKmode,
11049                                           gen_rtx_PRE_DEC (BLKmode,
11050                                                            stack_pointer_rtx)),
11051                            gen_rtx_UNSPEC (BLKmode,
11052                                            gen_rtvec (1, reg),
11053                                            UNSPEC_PUSH_MULT));
11054
11055           if (i != PC_REGNUM)
11056             {
11057               tmp = gen_rtx_SET (VOIDmode,
11058                                  gen_frame_mem (SImode, stack_pointer_rtx),
11059                                  reg);
11060               RTX_FRAME_RELATED_P (tmp) = 1;
11061               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
11062               dwarf_par_index++;
11063             }
11064
11065           break;
11066         }
11067     }
11068
11069   for (j = 1, i++; j < num_regs; i++)
11070     {
11071       if (mask & (1 << i))
11072         {
11073           reg = gen_rtx_REG (SImode, i);
11074
11075           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
11076
11077           if (i != PC_REGNUM)
11078             {
11079               tmp
11080                 = gen_rtx_SET (VOIDmode,
11081                                gen_frame_mem (SImode,
11082                                               plus_constant (stack_pointer_rtx,
11083                                                              4 * j)),
11084                                reg);
11085               RTX_FRAME_RELATED_P (tmp) = 1;
11086               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
11087             }
11088
11089           j++;
11090         }
11091     }
11092
11093   par = emit_insn (par);
11094
11095   tmp = gen_rtx_SET (VOIDmode,
11096                      stack_pointer_rtx,
11097                      plus_constant (stack_pointer_rtx, -4 * num_regs));
11098   RTX_FRAME_RELATED_P (tmp) = 1;
11099   XVECEXP (dwarf, 0, 0) = tmp;
11100
11101   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11102                                        REG_NOTES (par));
11103   return par;
11104 }
11105
11106 /* Calculate the size of the return value that is passed in registers.  */
11107 static int
11108 arm_size_return_regs (void)
11109 {
11110   enum machine_mode mode;
11111
11112   if (current_function_return_rtx != 0)
11113     mode = GET_MODE (current_function_return_rtx);
11114   else
11115     mode = DECL_MODE (DECL_RESULT (current_function_decl));
11116
11117   return GET_MODE_SIZE (mode);
11118 }
11119
11120 static rtx
11121 emit_sfm (int base_reg, int count)
11122 {
11123   rtx par;
11124   rtx dwarf;
11125   rtx tmp, reg;
11126   int i;
11127
11128   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
11129   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
11130
11131   reg = gen_rtx_REG (XFmode, base_reg++);
11132
11133   XVECEXP (par, 0, 0)
11134     = gen_rtx_SET (VOIDmode,
11135                    gen_frame_mem (BLKmode,
11136                                   gen_rtx_PRE_DEC (BLKmode,
11137                                                    stack_pointer_rtx)),
11138                    gen_rtx_UNSPEC (BLKmode,
11139                                    gen_rtvec (1, reg),
11140                                    UNSPEC_PUSH_MULT));
11141   tmp = gen_rtx_SET (VOIDmode,
11142                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
11143   RTX_FRAME_RELATED_P (tmp) = 1;
11144   XVECEXP (dwarf, 0, 1) = tmp;
11145
11146   for (i = 1; i < count; i++)
11147     {
11148       reg = gen_rtx_REG (XFmode, base_reg++);
11149       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
11150
11151       tmp = gen_rtx_SET (VOIDmode,
11152                          gen_frame_mem (XFmode,
11153                                         plus_constant (stack_pointer_rtx,
11154                                                        i * 12)),
11155                          reg);
11156       RTX_FRAME_RELATED_P (tmp) = 1;
11157       XVECEXP (dwarf, 0, i + 1) = tmp;
11158     }
11159
11160   tmp = gen_rtx_SET (VOIDmode,
11161                      stack_pointer_rtx,
11162                      plus_constant (stack_pointer_rtx, -12 * count));
11163
11164   RTX_FRAME_RELATED_P (tmp) = 1;
11165   XVECEXP (dwarf, 0, 0) = tmp;
11166
11167   par = emit_insn (par);
11168   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11169                                        REG_NOTES (par));
11170   return par;
11171 }
11172
11173
11174 /* Return true if the current function needs to save/restore LR.  */
11175
11176 static bool
11177 thumb_force_lr_save (void)
11178 {
11179   return !cfun->machine->lr_save_eliminated
11180          && (!leaf_function_p ()
11181              || thumb_far_jump_used_p ()
11182              || df_regs_ever_live_p (LR_REGNUM));
11183 }
11184
11185
11186 /* Compute the distance from register FROM to register TO.
11187    These can be the arg pointer (26), the soft frame pointer (25),
11188    the stack pointer (13) or the hard frame pointer (11).
11189    In thumb mode r7 is used as the soft frame pointer, if needed.
11190    Typical stack layout looks like this:
11191
11192        old stack pointer -> |    |
11193                              ----
11194                             |    | \
11195                             |    |   saved arguments for
11196                             |    |   vararg functions
11197                             |    | /
11198                               --
11199    hard FP & arg pointer -> |    | \
11200                             |    |   stack
11201                             |    |   frame
11202                             |    | /
11203                               --
11204                             |    | \
11205                             |    |   call saved
11206                             |    |   registers
11207       soft frame pointer -> |    | /
11208                               --
11209                             |    | \
11210                             |    |   local
11211                             |    |   variables
11212      locals base pointer -> |    | /
11213                               --
11214                             |    | \
11215                             |    |   outgoing
11216                             |    |   arguments
11217    current stack pointer -> |    | /
11218                               --
11219
11220   For a given function some or all of these stack components
11221   may not be needed, giving rise to the possibility of
11222   eliminating some of the registers.
11223
11224   The values returned by this function must reflect the behavior
11225   of arm_expand_prologue() and arm_compute_save_reg_mask().
11226
11227   The sign of the number returned reflects the direction of stack
11228   growth, so the values are positive for all eliminations except
11229   from the soft frame pointer to the hard frame pointer.
11230
11231   SFP may point just inside the local variables block to ensure correct
11232   alignment.  */
11233
11234
11235 /* Calculate stack offsets.  These are used to calculate register elimination
11236    offsets and in prologue/epilogue code.  */
11237
11238 static arm_stack_offsets *
11239 arm_get_frame_offsets (void)
11240 {
11241   struct arm_stack_offsets *offsets;
11242   unsigned long func_type;
11243   int leaf;
11244   int saved;
11245   HOST_WIDE_INT frame_size;
11246
11247   offsets = &cfun->machine->stack_offsets;
11248
11249   /* We need to know if we are a leaf function.  Unfortunately, it
11250      is possible to be called after start_sequence has been called,
11251      which causes get_insns to return the insns for the sequence,
11252      not the function, which will cause leaf_function_p to return
11253      the incorrect result.
11254
11255      to know about leaf functions once reload has completed, and the
11256      frame size cannot be changed after that time, so we can safely
11257      use the cached value.  */
11258
11259   if (reload_completed)
11260     return offsets;
11261
11262   /* Initially this is the size of the local variables.  It will translated
11263      into an offset once we have determined the size of preceding data.  */
11264   frame_size = ROUND_UP_WORD (get_frame_size ());
11265
11266   leaf = leaf_function_p ();
11267
11268   /* Space for variadic functions.  */
11269   offsets->saved_args = current_function_pretend_args_size;
11270
11271   /* In Thumb mode this is incorrect, but never used.  */
11272   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
11273
11274   if (TARGET_32BIT)
11275     {
11276       unsigned int regno;
11277
11278       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
11279
11280       /* We know that SP will be doubleword aligned on entry, and we must
11281          preserve that condition at any subroutine call.  We also require the
11282          soft frame pointer to be doubleword aligned.  */
11283
11284       if (TARGET_REALLY_IWMMXT)
11285         {
11286           /* Check for the call-saved iWMMXt registers.  */
11287           for (regno = FIRST_IWMMXT_REGNUM;
11288                regno <= LAST_IWMMXT_REGNUM;
11289                regno++)
11290             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
11291               saved += 8;
11292         }
11293
11294       func_type = arm_current_func_type ();
11295       if (! IS_VOLATILE (func_type))
11296         {
11297           /* Space for saved FPA registers.  */
11298           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
11299             if (df_regs_ever_live_p (regno) && ! call_used_regs[regno])
11300             saved += 12;
11301
11302           /* Space for saved VFP registers.  */
11303           if (TARGET_HARD_FLOAT && TARGET_VFP)
11304             saved += arm_get_vfp_saved_size ();
11305         }
11306     }
11307   else /* TARGET_THUMB1 */
11308     {
11309       saved = bit_count (thumb1_compute_save_reg_mask ()) * 4;
11310       if (TARGET_BACKTRACE)
11311         saved += 16;
11312     }
11313
11314   /* Saved registers include the stack frame.  */
11315   offsets->saved_regs = offsets->saved_args + saved;
11316   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
11317   /* A leaf function does not need any stack alignment if it has nothing
11318      on the stack.  */
11319   if (leaf && frame_size == 0)
11320     {
11321       offsets->outgoing_args = offsets->soft_frame;
11322       offsets->locals_base = offsets->soft_frame;
11323       return offsets;
11324     }
11325
11326   /* Ensure SFP has the correct alignment.  */
11327   if (ARM_DOUBLEWORD_ALIGN
11328       && (offsets->soft_frame & 7))
11329     offsets->soft_frame += 4;
11330
11331   offsets->locals_base = offsets->soft_frame + frame_size;
11332   offsets->outgoing_args = (offsets->locals_base
11333                             + current_function_outgoing_args_size);
11334
11335   if (ARM_DOUBLEWORD_ALIGN)
11336     {
11337       /* Ensure SP remains doubleword aligned.  */
11338       if (offsets->outgoing_args & 7)
11339         offsets->outgoing_args += 4;
11340       gcc_assert (!(offsets->outgoing_args & 7));
11341     }
11342
11343   return offsets;
11344 }
11345
11346
11347 /* Calculate the relative offsets for the different stack pointers.  Positive
11348    offsets are in the direction of stack growth.  */
11349
11350 HOST_WIDE_INT
11351 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
11352 {
11353   arm_stack_offsets *offsets;
11354
11355   offsets = arm_get_frame_offsets ();
11356
11357   /* OK, now we have enough information to compute the distances.
11358      There must be an entry in these switch tables for each pair
11359      of registers in ELIMINABLE_REGS, even if some of the entries
11360      seem to be redundant or useless.  */
11361   switch (from)
11362     {
11363     case ARG_POINTER_REGNUM:
11364       switch (to)
11365         {
11366         case THUMB_HARD_FRAME_POINTER_REGNUM:
11367           return 0;
11368
11369         case FRAME_POINTER_REGNUM:
11370           /* This is the reverse of the soft frame pointer
11371              to hard frame pointer elimination below.  */
11372           return offsets->soft_frame - offsets->saved_args;
11373
11374         case ARM_HARD_FRAME_POINTER_REGNUM:
11375           /* If there is no stack frame then the hard
11376              frame pointer and the arg pointer coincide.  */
11377           if (offsets->frame == offsets->saved_regs)
11378             return 0;
11379           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
11380           return (frame_pointer_needed
11381                   && cfun->static_chain_decl != NULL
11382                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
11383
11384         case STACK_POINTER_REGNUM:
11385           /* If nothing has been pushed on the stack at all
11386              then this will return -4.  This *is* correct!  */
11387           return offsets->outgoing_args - (offsets->saved_args + 4);
11388
11389         default:
11390           gcc_unreachable ();
11391         }
11392       gcc_unreachable ();
11393
11394     case FRAME_POINTER_REGNUM:
11395       switch (to)
11396         {
11397         case THUMB_HARD_FRAME_POINTER_REGNUM:
11398           return 0;
11399
11400         case ARM_HARD_FRAME_POINTER_REGNUM:
11401           /* The hard frame pointer points to the top entry in the
11402              stack frame.  The soft frame pointer to the bottom entry
11403              in the stack frame.  If there is no stack frame at all,
11404              then they are identical.  */
11405
11406           return offsets->frame - offsets->soft_frame;
11407
11408         case STACK_POINTER_REGNUM:
11409           return offsets->outgoing_args - offsets->soft_frame;
11410
11411         default:
11412           gcc_unreachable ();
11413         }
11414       gcc_unreachable ();
11415
11416     default:
11417       /* You cannot eliminate from the stack pointer.
11418          In theory you could eliminate from the hard frame
11419          pointer to the stack pointer, but this will never
11420          happen, since if a stack frame is not needed the
11421          hard frame pointer will never be used.  */
11422       gcc_unreachable ();
11423     }
11424 }
11425
11426
11427 /* Emit RTL to save coprocessor registers on function entry.  Returns the
11428    number of bytes pushed.  */
11429
11430 static int
11431 arm_save_coproc_regs(void)
11432 {
11433   int saved_size = 0;
11434   unsigned reg;
11435   unsigned start_reg;
11436   rtx insn;
11437
11438   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
11439     if (df_regs_ever_live_p (reg) && ! call_used_regs[reg])
11440       {
11441         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
11442         insn = gen_rtx_MEM (V2SImode, insn);
11443         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
11444         RTX_FRAME_RELATED_P (insn) = 1;
11445         saved_size += 8;
11446       }
11447
11448   /* Save any floating point call-saved registers used by this
11449      function.  */
11450   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11451     {
11452       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11453         if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11454           {
11455             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
11456             insn = gen_rtx_MEM (XFmode, insn);
11457             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
11458             RTX_FRAME_RELATED_P (insn) = 1;
11459             saved_size += 12;
11460           }
11461     }
11462   else
11463     {
11464       start_reg = LAST_FPA_REGNUM;
11465
11466       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11467         {
11468           if (df_regs_ever_live_p (reg) && !call_used_regs[reg])
11469             {
11470               if (start_reg - reg == 3)
11471                 {
11472                   insn = emit_sfm (reg, 4);
11473                   RTX_FRAME_RELATED_P (insn) = 1;
11474                   saved_size += 48;
11475                   start_reg = reg - 1;
11476                 }
11477             }
11478           else
11479             {
11480               if (start_reg != reg)
11481                 {
11482                   insn = emit_sfm (reg + 1, start_reg - reg);
11483                   RTX_FRAME_RELATED_P (insn) = 1;
11484                   saved_size += (start_reg - reg) * 12;
11485                 }
11486               start_reg = reg - 1;
11487             }
11488         }
11489
11490       if (start_reg != reg)
11491         {
11492           insn = emit_sfm (reg + 1, start_reg - reg);
11493           saved_size += (start_reg - reg) * 12;
11494           RTX_FRAME_RELATED_P (insn) = 1;
11495         }
11496     }
11497   if (TARGET_HARD_FLOAT && TARGET_VFP)
11498     {
11499       start_reg = FIRST_VFP_REGNUM;
11500
11501       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11502         {
11503           if ((!df_regs_ever_live_p (reg) || call_used_regs[reg])
11504               && (!df_regs_ever_live_p (reg + 1) || call_used_regs[reg + 1]))
11505             {
11506               if (start_reg != reg)
11507                 saved_size += vfp_emit_fstmd (start_reg,
11508                                               (reg - start_reg) / 2);
11509               start_reg = reg + 2;
11510             }
11511         }
11512       if (start_reg != reg)
11513         saved_size += vfp_emit_fstmd (start_reg,
11514                                       (reg - start_reg) / 2);
11515     }
11516   return saved_size;
11517 }
11518
11519
11520 /* Set the Thumb frame pointer from the stack pointer.  */
11521
11522 static void
11523 thumb_set_frame_pointer (arm_stack_offsets *offsets)
11524 {
11525   HOST_WIDE_INT amount;
11526   rtx insn, dwarf;
11527
11528   amount = offsets->outgoing_args - offsets->locals_base;
11529   if (amount < 1024)
11530     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
11531                                   stack_pointer_rtx, GEN_INT (amount)));
11532   else
11533     {
11534       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
11535       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
11536                                     hard_frame_pointer_rtx,
11537                                     stack_pointer_rtx));
11538       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11539                            plus_constant (stack_pointer_rtx, amount));
11540       RTX_FRAME_RELATED_P (dwarf) = 1;
11541       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11542                                             REG_NOTES (insn));
11543     }
11544
11545   RTX_FRAME_RELATED_P (insn) = 1;
11546 }
11547
11548 /* Generate the prologue instructions for entry into an ARM or Thumb-2
11549    function.  */
11550 void
11551 arm_expand_prologue (void)
11552 {
11553   rtx amount;
11554   rtx insn;
11555   rtx ip_rtx;
11556   unsigned long live_regs_mask;
11557   unsigned long func_type;
11558   int fp_offset = 0;
11559   int saved_pretend_args = 0;
11560   int saved_regs = 0;
11561   unsigned HOST_WIDE_INT args_to_push;
11562   arm_stack_offsets *offsets;
11563
11564   func_type = arm_current_func_type ();
11565
11566   /* Naked functions don't have prologues.  */
11567   if (IS_NAKED (func_type))
11568     return;
11569
11570   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
11571   args_to_push = current_function_pretend_args_size;
11572
11573   /* Compute which register we will have to save onto the stack.  */
11574   live_regs_mask = arm_compute_save_reg_mask ();
11575
11576   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
11577
11578   if (IS_STACKALIGN (func_type))
11579     {
11580       rtx dwarf;
11581       rtx r0;
11582       rtx r1;
11583       /* Handle a word-aligned stack pointer.  We generate the following:
11584
11585           mov r0, sp
11586           bic r1, r0, #7
11587           mov sp, r1
11588           <save and restore r0 in normal prologue/epilogue>
11589           mov sp, r0
11590           bx lr
11591
11592          The unwinder doesn't need to know about the stack realignment.
11593          Just tell it we saved SP in r0.  */
11594       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
11595
11596       r0 = gen_rtx_REG (SImode, 0);
11597       r1 = gen_rtx_REG (SImode, 1);
11598       dwarf = gen_rtx_UNSPEC (SImode, NULL_RTVEC, UNSPEC_STACK_ALIGN);
11599       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
11600       insn = gen_movsi (r0, stack_pointer_rtx);
11601       RTX_FRAME_RELATED_P (insn) = 1;
11602       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11603                                             dwarf, REG_NOTES (insn));
11604       emit_insn (insn);
11605       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
11606       emit_insn (gen_movsi (stack_pointer_rtx, r1));
11607     }
11608
11609   if (frame_pointer_needed && TARGET_ARM)
11610     {
11611       if (IS_INTERRUPT (func_type))
11612         {
11613           /* Interrupt functions must not corrupt any registers.
11614              Creating a frame pointer however, corrupts the IP
11615              register, so we must push it first.  */
11616           insn = emit_multi_reg_push (1 << IP_REGNUM);
11617
11618           /* Do not set RTX_FRAME_RELATED_P on this insn.
11619              The dwarf stack unwinding code only wants to see one
11620              stack decrement per function, and this is not it.  If
11621              this instruction is labeled as being part of the frame
11622              creation sequence then dwarf2out_frame_debug_expr will
11623              die when it encounters the assignment of IP to FP
11624              later on, since the use of SP here establishes SP as
11625              the CFA register and not IP.
11626
11627              Anyway this instruction is not really part of the stack
11628              frame creation although it is part of the prologue.  */
11629         }
11630       else if (IS_NESTED (func_type))
11631         {
11632           /* The Static chain register is the same as the IP register
11633              used as a scratch register during stack frame creation.
11634              To get around this need to find somewhere to store IP
11635              whilst the frame is being created.  We try the following
11636              places in order:
11637
11638                1. The last argument register.
11639                2. A slot on the stack above the frame.  (This only
11640                   works if the function is not a varargs function).
11641                3. Register r3, after pushing the argument registers
11642                   onto the stack.
11643
11644              Note - we only need to tell the dwarf2 backend about the SP
11645              adjustment in the second variant; the static chain register
11646              doesn't need to be unwound, as it doesn't contain a value
11647              inherited from the caller.  */
11648
11649           if (df_regs_ever_live_p (3) == false)
11650             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
11651           else if (args_to_push == 0)
11652             {
11653               rtx dwarf;
11654
11655               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
11656               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
11657               fp_offset = 4;
11658
11659               /* Just tell the dwarf backend that we adjusted SP.  */
11660               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11661                                    plus_constant (stack_pointer_rtx,
11662                                                   -fp_offset));
11663               RTX_FRAME_RELATED_P (insn) = 1;
11664               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11665                                                     dwarf, REG_NOTES (insn));
11666             }
11667           else
11668             {
11669               /* Store the args on the stack.  */
11670               if (cfun->machine->uses_anonymous_args)
11671                 insn = emit_multi_reg_push
11672                   ((0xf0 >> (args_to_push / 4)) & 0xf);
11673               else
11674                 insn = emit_insn
11675                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11676                                GEN_INT (- args_to_push)));
11677
11678               RTX_FRAME_RELATED_P (insn) = 1;
11679
11680               saved_pretend_args = 1;
11681               fp_offset = args_to_push;
11682               args_to_push = 0;
11683
11684               /* Now reuse r3 to preserve IP.  */
11685               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
11686             }
11687         }
11688
11689       insn = emit_set_insn (ip_rtx,
11690                             plus_constant (stack_pointer_rtx, fp_offset));
11691       RTX_FRAME_RELATED_P (insn) = 1;
11692     }
11693
11694   if (args_to_push)
11695     {
11696       /* Push the argument registers, or reserve space for them.  */
11697       if (cfun->machine->uses_anonymous_args)
11698         insn = emit_multi_reg_push
11699           ((0xf0 >> (args_to_push / 4)) & 0xf);
11700       else
11701         insn = emit_insn
11702           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11703                        GEN_INT (- args_to_push)));
11704       RTX_FRAME_RELATED_P (insn) = 1;
11705     }
11706
11707   /* If this is an interrupt service routine, and the link register
11708      is going to be pushed, and we are not creating a stack frame,
11709      (which would involve an extra push of IP and a pop in the epilogue)
11710      subtracting four from LR now will mean that the function return
11711      can be done with a single instruction.  */
11712   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
11713       && (live_regs_mask & (1 << LR_REGNUM)) != 0
11714       && ! frame_pointer_needed
11715       && TARGET_ARM)
11716     {
11717       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
11718       
11719       emit_set_insn (lr, plus_constant (lr, -4));
11720     }
11721
11722   if (live_regs_mask)
11723     {
11724       insn = emit_multi_reg_push (live_regs_mask);
11725       saved_regs += bit_count (live_regs_mask) * 4;
11726       RTX_FRAME_RELATED_P (insn) = 1;
11727     }
11728
11729   if (! IS_VOLATILE (func_type))
11730     saved_regs += arm_save_coproc_regs ();
11731
11732   if (frame_pointer_needed && TARGET_ARM)
11733     {
11734       /* Create the new frame pointer.  */
11735         {
11736           insn = GEN_INT (-(4 + args_to_push + fp_offset));
11737           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
11738           RTX_FRAME_RELATED_P (insn) = 1;
11739
11740           if (IS_NESTED (func_type))
11741             {
11742               /* Recover the static chain register.  */
11743               if (!df_regs_ever_live_p (3)
11744                   || saved_pretend_args)
11745                 insn = gen_rtx_REG (SImode, 3);
11746               else /* if (current_function_pretend_args_size == 0) */
11747                 {
11748                   insn = plus_constant (hard_frame_pointer_rtx, 4);
11749                   insn = gen_frame_mem (SImode, insn);
11750                 }
11751               emit_set_insn (ip_rtx, insn);
11752               /* Add a USE to stop propagate_one_insn() from barfing.  */
11753               emit_insn (gen_prologue_use (ip_rtx));
11754             }
11755         }
11756     }
11757
11758   offsets = arm_get_frame_offsets ();
11759   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
11760     {
11761       /* This add can produce multiple insns for a large constant, so we
11762          need to get tricky.  */
11763       rtx last = get_last_insn ();
11764
11765       amount = GEN_INT (offsets->saved_args + saved_regs
11766                         - offsets->outgoing_args);
11767
11768       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11769                                     amount));
11770       do
11771         {
11772           last = last ? NEXT_INSN (last) : get_insns ();
11773           RTX_FRAME_RELATED_P (last) = 1;
11774         }
11775       while (last != insn);
11776
11777       /* If the frame pointer is needed, emit a special barrier that
11778          will prevent the scheduler from moving stores to the frame
11779          before the stack adjustment.  */
11780       if (frame_pointer_needed)
11781         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
11782                                          hard_frame_pointer_rtx));
11783     }
11784
11785
11786   if (frame_pointer_needed && TARGET_THUMB2)
11787     thumb_set_frame_pointer (offsets);
11788
11789   if (flag_pic && arm_pic_register != INVALID_REGNUM)
11790     {
11791       unsigned long mask;
11792
11793       mask = live_regs_mask;
11794       mask &= THUMB2_WORK_REGS;
11795       if (!IS_NESTED (func_type))
11796         mask |= (1 << IP_REGNUM);
11797       arm_load_pic_register (mask);
11798     }
11799
11800   /* If we are profiling, make sure no instructions are scheduled before
11801      the call to mcount.  Similarly if the user has requested no
11802      scheduling in the prolog.  Similarly if we want non-call exceptions
11803      using the EABI unwinder, to prevent faulting instructions from being
11804      swapped with a stack adjustment.  */
11805   if (current_function_profile || !TARGET_SCHED_PROLOG
11806       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
11807     emit_insn (gen_blockage ());
11808
11809   /* If the link register is being kept alive, with the return address in it,
11810      then make sure that it does not get reused by the ce2 pass.  */
11811   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
11812     cfun->machine->lr_save_eliminated = 1;
11813 }
11814 \f
11815 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
11816 static void
11817 arm_print_condition (FILE *stream)
11818 {
11819   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
11820     {
11821       /* Branch conversion is not implemented for Thumb-2.  */
11822       if (TARGET_THUMB)
11823         {
11824           output_operand_lossage ("predicated Thumb instruction");
11825           return;
11826         }
11827       if (current_insn_predicate != NULL)
11828         {
11829           output_operand_lossage
11830             ("predicated instruction in conditional sequence");
11831           return;
11832         }
11833
11834       fputs (arm_condition_codes[arm_current_cc], stream);
11835     }
11836   else if (current_insn_predicate)
11837     {
11838       enum arm_cond_code code;
11839
11840       if (TARGET_THUMB1)
11841         {
11842           output_operand_lossage ("predicated Thumb instruction");
11843           return;
11844         }
11845
11846       code = get_arm_condition_code (current_insn_predicate);
11847       fputs (arm_condition_codes[code], stream);
11848     }
11849 }
11850
11851
11852 /* If CODE is 'd', then the X is a condition operand and the instruction
11853    should only be executed if the condition is true.
11854    if CODE is 'D', then the X is a condition operand and the instruction
11855    should only be executed if the condition is false: however, if the mode
11856    of the comparison is CCFPEmode, then always execute the instruction -- we
11857    do this because in these circumstances !GE does not necessarily imply LT;
11858    in these cases the instruction pattern will take care to make sure that
11859    an instruction containing %d will follow, thereby undoing the effects of
11860    doing this instruction unconditionally.
11861    If CODE is 'N' then X is a floating point operand that must be negated
11862    before output.
11863    If CODE is 'B' then output a bitwise inverted value of X (a const int).
11864    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
11865 void
11866 arm_print_operand (FILE *stream, rtx x, int code)
11867 {
11868   switch (code)
11869     {
11870     case '@':
11871       fputs (ASM_COMMENT_START, stream);
11872       return;
11873
11874     case '_':
11875       fputs (user_label_prefix, stream);
11876       return;
11877
11878     case '|':
11879       fputs (REGISTER_PREFIX, stream);
11880       return;
11881
11882     case '?':
11883       arm_print_condition (stream);
11884       return;
11885
11886     case '(':
11887       /* Nothing in unified syntax, otherwise the current condition code.  */
11888       if (!TARGET_UNIFIED_ASM)
11889         arm_print_condition (stream);
11890       break;
11891
11892     case ')':
11893       /* The current condition code in unified syntax, otherwise nothing.  */
11894       if (TARGET_UNIFIED_ASM)
11895         arm_print_condition (stream);
11896       break;
11897   
11898     case '.':
11899       /* The current condition code for a condition code setting instruction.
11900          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
11901       if (TARGET_UNIFIED_ASM)
11902         {
11903           fputc('s', stream);
11904           arm_print_condition (stream);
11905         }
11906       else
11907         {
11908           arm_print_condition (stream);
11909           fputc('s', stream);
11910         }
11911       return;
11912
11913     case '!':
11914       /* If the instruction is conditionally executed then print
11915          the current condition code, otherwise print 's'.  */
11916       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
11917       if (current_insn_predicate)
11918         arm_print_condition (stream);
11919       else
11920         fputc('s', stream);
11921       break;
11922
11923     case 'N':
11924       {
11925         REAL_VALUE_TYPE r;
11926         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
11927         r = REAL_VALUE_NEGATE (r);
11928         fprintf (stream, "%s", fp_const_from_val (&r));
11929       }
11930       return;
11931
11932     case 'B':
11933       if (GET_CODE (x) == CONST_INT)
11934         {
11935           HOST_WIDE_INT val;
11936           val = ARM_SIGN_EXTEND (~INTVAL (x));
11937           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
11938         }
11939       else
11940         {
11941           putc ('~', stream);
11942           output_addr_const (stream, x);
11943         }
11944       return;
11945
11946     case 'L':
11947       /* The low 16 bits of an immediate constant.  */
11948       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
11949       return;
11950
11951     case 'i':
11952       fprintf (stream, "%s", arithmetic_instr (x, 1));
11953       return;
11954
11955     /* Truncate Cirrus shift counts.  */
11956     case 's':
11957       if (GET_CODE (x) == CONST_INT)
11958         {
11959           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
11960           return;
11961         }
11962       arm_print_operand (stream, x, 0);
11963       return;
11964
11965     case 'I':
11966       fprintf (stream, "%s", arithmetic_instr (x, 0));
11967       return;
11968
11969     case 'S':
11970       {
11971         HOST_WIDE_INT val;
11972         const char *shift;
11973
11974         if (!shift_operator (x, SImode))
11975           {
11976             output_operand_lossage ("invalid shift operand");
11977             break;
11978           }
11979
11980         shift = shift_op (x, &val);
11981
11982         if (shift)
11983           {
11984             fprintf (stream, ", %s ", shift);
11985             if (val == -1)
11986               arm_print_operand (stream, XEXP (x, 1), 0);
11987             else
11988               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11989           }
11990       }
11991       return;
11992
11993       /* An explanation of the 'Q', 'R' and 'H' register operands:
11994
11995          In a pair of registers containing a DI or DF value the 'Q'
11996          operand returns the register number of the register containing
11997          the least significant part of the value.  The 'R' operand returns
11998          the register number of the register containing the most
11999          significant part of the value.
12000
12001          The 'H' operand returns the higher of the two register numbers.
12002          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
12003          same as the 'Q' operand, since the most significant part of the
12004          value is held in the lower number register.  The reverse is true
12005          on systems where WORDS_BIG_ENDIAN is false.
12006
12007          The purpose of these operands is to distinguish between cases
12008          where the endian-ness of the values is important (for example
12009          when they are added together), and cases where the endian-ness
12010          is irrelevant, but the order of register operations is important.
12011          For example when loading a value from memory into a register
12012          pair, the endian-ness does not matter.  Provided that the value
12013          from the lower memory address is put into the lower numbered
12014          register, and the value from the higher address is put into the
12015          higher numbered register, the load will work regardless of whether
12016          the value being loaded is big-wordian or little-wordian.  The
12017          order of the two register loads can matter however, if the address
12018          of the memory location is actually held in one of the registers
12019          being overwritten by the load.  */
12020     case 'Q':
12021       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12022         {
12023           output_operand_lossage ("invalid operand for code '%c'", code);
12024           return;
12025         }
12026
12027       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
12028       return;
12029
12030     case 'R':
12031       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12032         {
12033           output_operand_lossage ("invalid operand for code '%c'", code);
12034           return;
12035         }
12036
12037       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
12038       return;
12039
12040     case 'H':
12041       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
12042         {
12043           output_operand_lossage ("invalid operand for code '%c'", code);
12044           return;
12045         }
12046
12047       asm_fprintf (stream, "%r", REGNO (x) + 1);
12048       return;
12049
12050     case 'm':
12051       asm_fprintf (stream, "%r",
12052                    GET_CODE (XEXP (x, 0)) == REG
12053                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
12054       return;
12055
12056     case 'M':
12057       asm_fprintf (stream, "{%r-%r}",
12058                    REGNO (x),
12059                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
12060       return;
12061
12062     case 'd':
12063       /* CONST_TRUE_RTX means always -- that's the default.  */
12064       if (x == const_true_rtx)
12065         return;
12066
12067       if (!COMPARISON_P (x))
12068         {
12069           output_operand_lossage ("invalid operand for code '%c'", code);
12070           return;
12071         }
12072
12073       fputs (arm_condition_codes[get_arm_condition_code (x)],
12074              stream);
12075       return;
12076
12077     case 'D':
12078       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
12079          want to do that.  */
12080       if (x == const_true_rtx)
12081         {
12082           output_operand_lossage ("instruction never executed");
12083           return;
12084         }
12085       if (!COMPARISON_P (x))
12086         {
12087           output_operand_lossage ("invalid operand for code '%c'", code);
12088           return;
12089         }
12090
12091       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
12092                                  (get_arm_condition_code (x))],
12093              stream);
12094       return;
12095
12096     /* Cirrus registers can be accessed in a variety of ways:
12097          single floating point (f)
12098          double floating point (d)
12099          32bit integer         (fx)
12100          64bit integer         (dx).  */
12101     case 'W':                   /* Cirrus register in F mode.  */
12102     case 'X':                   /* Cirrus register in D mode.  */
12103     case 'Y':                   /* Cirrus register in FX mode.  */
12104     case 'Z':                   /* Cirrus register in DX mode.  */
12105       gcc_assert (GET_CODE (x) == REG
12106                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
12107
12108       fprintf (stream, "mv%s%s",
12109                code == 'W' ? "f"
12110                : code == 'X' ? "d"
12111                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
12112
12113       return;
12114
12115     /* Print cirrus register in the mode specified by the register's mode.  */
12116     case 'V':
12117       {
12118         int mode = GET_MODE (x);
12119
12120         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
12121           {
12122             output_operand_lossage ("invalid operand for code '%c'", code);
12123             return;
12124           }
12125
12126         fprintf (stream, "mv%s%s",
12127                  mode == DFmode ? "d"
12128                  : mode == SImode ? "fx"
12129                  : mode == DImode ? "dx"
12130                  : "f", reg_names[REGNO (x)] + 2);
12131
12132         return;
12133       }
12134
12135     case 'U':
12136       if (GET_CODE (x) != REG
12137           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
12138           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
12139         /* Bad value for wCG register number.  */
12140         {
12141           output_operand_lossage ("invalid operand for code '%c'", code);
12142           return;
12143         }
12144
12145       else
12146         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
12147       return;
12148
12149       /* Print an iWMMXt control register name.  */
12150     case 'w':
12151       if (GET_CODE (x) != CONST_INT
12152           || INTVAL (x) < 0
12153           || INTVAL (x) >= 16)
12154         /* Bad value for wC register number.  */
12155         {
12156           output_operand_lossage ("invalid operand for code '%c'", code);
12157           return;
12158         }
12159
12160       else
12161         {
12162           static const char * wc_reg_names [16] =
12163             {
12164               "wCID",  "wCon",  "wCSSF", "wCASF",
12165               "wC4",   "wC5",   "wC6",   "wC7",
12166               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
12167               "wC12",  "wC13",  "wC14",  "wC15"
12168             };
12169
12170           fprintf (stream, wc_reg_names [INTVAL (x)]);
12171         }
12172       return;
12173
12174       /* Print a VFP double precision register name.  */
12175     case 'P':
12176       {
12177         int mode = GET_MODE (x);
12178         int num;
12179
12180         if (mode != DImode && mode != DFmode)
12181           {
12182             output_operand_lossage ("invalid operand for code '%c'", code);
12183             return;
12184           }
12185
12186         if (GET_CODE (x) != REG
12187             || !IS_VFP_REGNUM (REGNO (x)))
12188           {
12189             output_operand_lossage ("invalid operand for code '%c'", code);
12190             return;
12191           }
12192
12193         num = REGNO(x) - FIRST_VFP_REGNUM;
12194         if (num & 1)
12195           {
12196             output_operand_lossage ("invalid operand for code '%c'", code);
12197             return;
12198           }
12199
12200         fprintf (stream, "d%d", num >> 1);
12201       }
12202       return;
12203
12204     /* Print a VFPv3 floating-point constant, represented as an integer
12205        index.  */
12206     case 'G':
12207       {
12208         int index = vfp3_const_double_index (x);
12209         gcc_assert (index != -1);
12210         fprintf (stream, "%d", index);
12211       }
12212       return;
12213
12214     default:
12215       if (x == 0)
12216         {
12217           output_operand_lossage ("missing operand");
12218           return;
12219         }
12220
12221       switch (GET_CODE (x))
12222         {
12223         case REG:
12224           asm_fprintf (stream, "%r", REGNO (x));
12225           break;
12226
12227         case MEM:
12228           output_memory_reference_mode = GET_MODE (x);
12229           output_address (XEXP (x, 0));
12230           break;
12231
12232         case CONST_DOUBLE:
12233           fprintf (stream, "#%s", fp_immediate_constant (x));
12234           break;
12235
12236         default:
12237           gcc_assert (GET_CODE (x) != NEG);
12238           fputc ('#', stream);
12239           output_addr_const (stream, x);
12240           break;
12241         }
12242     }
12243 }
12244 \f
12245 #ifndef AOF_ASSEMBLER
12246 /* Target hook for assembling integer objects.  The ARM version needs to
12247    handle word-sized values specially.  */
12248 static bool
12249 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
12250 {
12251   if (size == UNITS_PER_WORD && aligned_p)
12252     {
12253       fputs ("\t.word\t", asm_out_file);
12254       output_addr_const (asm_out_file, x);
12255
12256       /* Mark symbols as position independent.  We only do this in the
12257          .text segment, not in the .data segment.  */
12258       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
12259           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
12260         {
12261           /* See legitimize_pic_address for an explanation of the
12262              TARGET_VXWORKS_RTP check.  */
12263           if (TARGET_VXWORKS_RTP
12264               || (GET_CODE (x) == SYMBOL_REF && !SYMBOL_REF_LOCAL_P (x)))
12265             fputs ("(GOT)", asm_out_file);
12266           else
12267             fputs ("(GOTOFF)", asm_out_file);
12268         }
12269       fputc ('\n', asm_out_file);
12270       return true;
12271     }
12272
12273   if (arm_vector_mode_supported_p (GET_MODE (x)))
12274     {
12275       int i, units;
12276
12277       gcc_assert (GET_CODE (x) == CONST_VECTOR);
12278
12279       units = CONST_VECTOR_NUNITS (x);
12280
12281       switch (GET_MODE (x))
12282         {
12283         case V2SImode: size = 4; break;
12284         case V4HImode: size = 2; break;
12285         case V8QImode: size = 1; break;
12286         default:
12287           gcc_unreachable ();
12288         }
12289
12290       for (i = 0; i < units; i++)
12291         {
12292           rtx elt;
12293
12294           elt = CONST_VECTOR_ELT (x, i);
12295           assemble_integer
12296             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
12297         }
12298
12299       return true;
12300     }
12301
12302   return default_assemble_integer (x, size, aligned_p);
12303 }
12304
12305 static void
12306 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
12307 {
12308   section *s;
12309
12310   if (!TARGET_AAPCS_BASED)
12311     {
12312       (is_ctor ? 
12313        default_named_section_asm_out_constructor 
12314        : default_named_section_asm_out_destructor) (symbol, priority);
12315       return;
12316     }
12317
12318   /* Put these in the .init_array section, using a special relocation.  */
12319   if (priority != DEFAULT_INIT_PRIORITY)
12320     {
12321       char buf[18];
12322       sprintf (buf, "%s.%.5u", 
12323                is_ctor ? ".init_array" : ".fini_array",
12324                priority);
12325       s = get_section (buf, SECTION_WRITE, NULL_TREE);
12326     }
12327   else if (is_ctor)
12328     s = ctors_section;
12329   else
12330     s = dtors_section;
12331
12332   switch_to_section (s);
12333   assemble_align (POINTER_SIZE);
12334   fputs ("\t.word\t", asm_out_file);
12335   output_addr_const (asm_out_file, symbol);
12336   fputs ("(target1)\n", asm_out_file);
12337 }
12338
12339 /* Add a function to the list of static constructors.  */
12340
12341 static void
12342 arm_elf_asm_constructor (rtx symbol, int priority)
12343 {
12344   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
12345 }
12346
12347 /* Add a function to the list of static destructors.  */
12348
12349 static void
12350 arm_elf_asm_destructor (rtx symbol, int priority)
12351 {
12352   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
12353 }
12354 #endif
12355 \f
12356 /* A finite state machine takes care of noticing whether or not instructions
12357    can be conditionally executed, and thus decrease execution time and code
12358    size by deleting branch instructions.  The fsm is controlled by
12359    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
12360
12361 /* The state of the fsm controlling condition codes are:
12362    0: normal, do nothing special
12363    1: make ASM_OUTPUT_OPCODE not output this instruction
12364    2: make ASM_OUTPUT_OPCODE not output this instruction
12365    3: make instructions conditional
12366    4: make instructions conditional
12367
12368    State transitions (state->state by whom under condition):
12369    0 -> 1 final_prescan_insn if the `target' is a label
12370    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
12371    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
12372    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
12373    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
12374           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
12375    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
12376           (the target insn is arm_target_insn).
12377
12378    If the jump clobbers the conditions then we use states 2 and 4.
12379
12380    A similar thing can be done with conditional return insns.
12381
12382    XXX In case the `target' is an unconditional branch, this conditionalising
12383    of the instructions always reduces code size, but not always execution
12384    time.  But then, I want to reduce the code size to somewhere near what
12385    /bin/cc produces.  */
12386
12387 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
12388    instructions.  When a COND_EXEC instruction is seen the subsequent
12389    instructions are scanned so that multiple conditional instructions can be
12390    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
12391    specify the length and true/false mask for the IT block.  These will be
12392    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
12393
12394 /* Returns the index of the ARM condition code string in
12395    `arm_condition_codes'.  COMPARISON should be an rtx like
12396    `(eq (...) (...))'.  */
12397 static enum arm_cond_code
12398 get_arm_condition_code (rtx comparison)
12399 {
12400   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
12401   int code;
12402   enum rtx_code comp_code = GET_CODE (comparison);
12403
12404   if (GET_MODE_CLASS (mode) != MODE_CC)
12405     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
12406                            XEXP (comparison, 1));
12407
12408   switch (mode)
12409     {
12410     case CC_DNEmode: code = ARM_NE; goto dominance;
12411     case CC_DEQmode: code = ARM_EQ; goto dominance;
12412     case CC_DGEmode: code = ARM_GE; goto dominance;
12413     case CC_DGTmode: code = ARM_GT; goto dominance;
12414     case CC_DLEmode: code = ARM_LE; goto dominance;
12415     case CC_DLTmode: code = ARM_LT; goto dominance;
12416     case CC_DGEUmode: code = ARM_CS; goto dominance;
12417     case CC_DGTUmode: code = ARM_HI; goto dominance;
12418     case CC_DLEUmode: code = ARM_LS; goto dominance;
12419     case CC_DLTUmode: code = ARM_CC;
12420
12421     dominance:
12422       gcc_assert (comp_code == EQ || comp_code == NE);
12423
12424       if (comp_code == EQ)
12425         return ARM_INVERSE_CONDITION_CODE (code);
12426       return code;
12427
12428     case CC_NOOVmode:
12429       switch (comp_code)
12430         {
12431         case NE: return ARM_NE;
12432         case EQ: return ARM_EQ;
12433         case GE: return ARM_PL;
12434         case LT: return ARM_MI;
12435         default: gcc_unreachable ();
12436         }
12437
12438     case CC_Zmode:
12439       switch (comp_code)
12440         {
12441         case NE: return ARM_NE;
12442         case EQ: return ARM_EQ;
12443         default: gcc_unreachable ();
12444         }
12445
12446     case CC_Nmode:
12447       switch (comp_code)
12448         {
12449         case NE: return ARM_MI;
12450         case EQ: return ARM_PL;
12451         default: gcc_unreachable ();
12452         }
12453
12454     case CCFPEmode:
12455     case CCFPmode:
12456       /* These encodings assume that AC=1 in the FPA system control
12457          byte.  This allows us to handle all cases except UNEQ and
12458          LTGT.  */
12459       switch (comp_code)
12460         {
12461         case GE: return ARM_GE;
12462         case GT: return ARM_GT;
12463         case LE: return ARM_LS;
12464         case LT: return ARM_MI;
12465         case NE: return ARM_NE;
12466         case EQ: return ARM_EQ;
12467         case ORDERED: return ARM_VC;
12468         case UNORDERED: return ARM_VS;
12469         case UNLT: return ARM_LT;
12470         case UNLE: return ARM_LE;
12471         case UNGT: return ARM_HI;
12472         case UNGE: return ARM_PL;
12473           /* UNEQ and LTGT do not have a representation.  */
12474         case UNEQ: /* Fall through.  */
12475         case LTGT: /* Fall through.  */
12476         default: gcc_unreachable ();
12477         }
12478
12479     case CC_SWPmode:
12480       switch (comp_code)
12481         {
12482         case NE: return ARM_NE;
12483         case EQ: return ARM_EQ;
12484         case GE: return ARM_LE;
12485         case GT: return ARM_LT;
12486         case LE: return ARM_GE;
12487         case LT: return ARM_GT;
12488         case GEU: return ARM_LS;
12489         case GTU: return ARM_CC;
12490         case LEU: return ARM_CS;
12491         case LTU: return ARM_HI;
12492         default: gcc_unreachable ();
12493         }
12494
12495     case CC_Cmode:
12496       switch (comp_code)
12497       {
12498       case LTU: return ARM_CS;
12499       case GEU: return ARM_CC;
12500       default: gcc_unreachable ();
12501       }
12502
12503     case CCmode:
12504       switch (comp_code)
12505         {
12506         case NE: return ARM_NE;
12507         case EQ: return ARM_EQ;
12508         case GE: return ARM_GE;
12509         case GT: return ARM_GT;
12510         case LE: return ARM_LE;
12511         case LT: return ARM_LT;
12512         case GEU: return ARM_CS;
12513         case GTU: return ARM_HI;
12514         case LEU: return ARM_LS;
12515         case LTU: return ARM_CC;
12516         default: gcc_unreachable ();
12517         }
12518
12519     default: gcc_unreachable ();
12520     }
12521 }
12522
12523 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
12524    instructions.  */
12525 void
12526 thumb2_final_prescan_insn (rtx insn)
12527 {
12528   rtx first_insn = insn;
12529   rtx body = PATTERN (insn);
12530   rtx predicate;
12531   enum arm_cond_code code;
12532   int n;
12533   int mask;
12534
12535   /* Remove the previous insn from the count of insns to be output.  */
12536   if (arm_condexec_count)
12537       arm_condexec_count--;
12538
12539   /* Nothing to do if we are already inside a conditional block.  */
12540   if (arm_condexec_count)
12541     return;
12542
12543   if (GET_CODE (body) != COND_EXEC)
12544     return;
12545
12546   /* Conditional jumps are implemented directly.  */
12547   if (GET_CODE (insn) == JUMP_INSN)
12548     return;
12549
12550   predicate = COND_EXEC_TEST (body);
12551   arm_current_cc = get_arm_condition_code (predicate);
12552
12553   n = get_attr_ce_count (insn);
12554   arm_condexec_count = 1;
12555   arm_condexec_mask = (1 << n) - 1;
12556   arm_condexec_masklen = n;
12557   /* See if subsequent instructions can be combined into the same block.  */
12558   for (;;)
12559     {
12560       insn = next_nonnote_insn (insn);
12561
12562       /* Jumping into the middle of an IT block is illegal, so a label or
12563          barrier terminates the block.  */
12564       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
12565         break;
12566
12567       body = PATTERN (insn);
12568       /* USE and CLOBBER aren't really insns, so just skip them.  */
12569       if (GET_CODE (body) == USE
12570           || GET_CODE (body) == CLOBBER)
12571         continue;
12572
12573       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
12574       if (GET_CODE (body) != COND_EXEC)
12575         break;
12576       /* Allow up to 4 conditionally executed instructions in a block.  */
12577       n = get_attr_ce_count (insn);
12578       if (arm_condexec_masklen + n > 4)
12579         break;
12580
12581       predicate = COND_EXEC_TEST (body);
12582       code = get_arm_condition_code (predicate);
12583       mask = (1 << n) - 1;
12584       if (arm_current_cc == code)
12585         arm_condexec_mask |= (mask << arm_condexec_masklen);
12586       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
12587         break;
12588
12589       arm_condexec_count++;
12590       arm_condexec_masklen += n;
12591
12592       /* A jump must be the last instruction in a conditional block.  */
12593       if (GET_CODE(insn) == JUMP_INSN)
12594         break;
12595     }
12596   /* Restore recog_data (getting the attributes of other insns can
12597      destroy this array, but final.c assumes that it remains intact
12598      across this call).  */
12599   extract_constrain_insn_cached (first_insn);
12600 }
12601
12602 void
12603 arm_final_prescan_insn (rtx insn)
12604 {
12605   /* BODY will hold the body of INSN.  */
12606   rtx body = PATTERN (insn);
12607
12608   /* This will be 1 if trying to repeat the trick, and things need to be
12609      reversed if it appears to fail.  */
12610   int reverse = 0;
12611
12612   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
12613      taken are clobbered, even if the rtl suggests otherwise.  It also
12614      means that we have to grub around within the jump expression to find
12615      out what the conditions are when the jump isn't taken.  */
12616   int jump_clobbers = 0;
12617
12618   /* If we start with a return insn, we only succeed if we find another one.  */
12619   int seeking_return = 0;
12620
12621   /* START_INSN will hold the insn from where we start looking.  This is the
12622      first insn after the following code_label if REVERSE is true.  */
12623   rtx start_insn = insn;
12624
12625   /* If in state 4, check if the target branch is reached, in order to
12626      change back to state 0.  */
12627   if (arm_ccfsm_state == 4)
12628     {
12629       if (insn == arm_target_insn)
12630         {
12631           arm_target_insn = NULL;
12632           arm_ccfsm_state = 0;
12633         }
12634       return;
12635     }
12636
12637   /* If in state 3, it is possible to repeat the trick, if this insn is an
12638      unconditional branch to a label, and immediately following this branch
12639      is the previous target label which is only used once, and the label this
12640      branch jumps to is not too far off.  */
12641   if (arm_ccfsm_state == 3)
12642     {
12643       if (simplejump_p (insn))
12644         {
12645           start_insn = next_nonnote_insn (start_insn);
12646           if (GET_CODE (start_insn) == BARRIER)
12647             {
12648               /* XXX Isn't this always a barrier?  */
12649               start_insn = next_nonnote_insn (start_insn);
12650             }
12651           if (GET_CODE (start_insn) == CODE_LABEL
12652               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
12653               && LABEL_NUSES (start_insn) == 1)
12654             reverse = TRUE;
12655           else
12656             return;
12657         }
12658       else if (GET_CODE (body) == RETURN)
12659         {
12660           start_insn = next_nonnote_insn (start_insn);
12661           if (GET_CODE (start_insn) == BARRIER)
12662             start_insn = next_nonnote_insn (start_insn);
12663           if (GET_CODE (start_insn) == CODE_LABEL
12664               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
12665               && LABEL_NUSES (start_insn) == 1)
12666             {
12667               reverse = TRUE;
12668               seeking_return = 1;
12669             }
12670           else
12671             return;
12672         }
12673       else
12674         return;
12675     }
12676
12677   gcc_assert (!arm_ccfsm_state || reverse);
12678   if (GET_CODE (insn) != JUMP_INSN)
12679     return;
12680
12681   /* This jump might be paralleled with a clobber of the condition codes
12682      the jump should always come first */
12683   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
12684     body = XVECEXP (body, 0, 0);
12685
12686   if (reverse
12687       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
12688           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
12689     {
12690       int insns_skipped;
12691       int fail = FALSE, succeed = FALSE;
12692       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
12693       int then_not_else = TRUE;
12694       rtx this_insn = start_insn, label = 0;
12695
12696       /* If the jump cannot be done with one instruction, we cannot
12697          conditionally execute the instruction in the inverse case.  */
12698       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
12699         {
12700           jump_clobbers = 1;
12701           return;
12702         }
12703
12704       /* Register the insn jumped to.  */
12705       if (reverse)
12706         {
12707           if (!seeking_return)
12708             label = XEXP (SET_SRC (body), 0);
12709         }
12710       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
12711         label = XEXP (XEXP (SET_SRC (body), 1), 0);
12712       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
12713         {
12714           label = XEXP (XEXP (SET_SRC (body), 2), 0);
12715           then_not_else = FALSE;
12716         }
12717       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
12718         seeking_return = 1;
12719       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
12720         {
12721           seeking_return = 1;
12722           then_not_else = FALSE;
12723         }
12724       else
12725         gcc_unreachable ();
12726
12727       /* See how many insns this branch skips, and what kind of insns.  If all
12728          insns are okay, and the label or unconditional branch to the same
12729          label is not too far away, succeed.  */
12730       for (insns_skipped = 0;
12731            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
12732         {
12733           rtx scanbody;
12734
12735           this_insn = next_nonnote_insn (this_insn);
12736           if (!this_insn)
12737             break;
12738
12739           switch (GET_CODE (this_insn))
12740             {
12741             case CODE_LABEL:
12742               /* Succeed if it is the target label, otherwise fail since
12743                  control falls in from somewhere else.  */
12744               if (this_insn == label)
12745                 {
12746                   if (jump_clobbers)
12747                     {
12748                       arm_ccfsm_state = 2;
12749                       this_insn = next_nonnote_insn (this_insn);
12750                     }
12751                   else
12752                     arm_ccfsm_state = 1;
12753                   succeed = TRUE;
12754                 }
12755               else
12756                 fail = TRUE;
12757               break;
12758
12759             case BARRIER:
12760               /* Succeed if the following insn is the target label.
12761                  Otherwise fail.
12762                  If return insns are used then the last insn in a function
12763                  will be a barrier.  */
12764               this_insn = next_nonnote_insn (this_insn);
12765               if (this_insn && this_insn == label)
12766                 {
12767                   if (jump_clobbers)
12768                     {
12769                       arm_ccfsm_state = 2;
12770                       this_insn = next_nonnote_insn (this_insn);
12771                     }
12772                   else
12773                     arm_ccfsm_state = 1;
12774                   succeed = TRUE;
12775                 }
12776               else
12777                 fail = TRUE;
12778               break;
12779
12780             case CALL_INSN:
12781               /* The AAPCS says that conditional calls should not be
12782                  used since they make interworking inefficient (the
12783                  linker can't transform BL<cond> into BLX).  That's
12784                  only a problem if the machine has BLX.  */
12785               if (arm_arch5)
12786                 {
12787                   fail = TRUE;
12788                   break;
12789                 }
12790
12791               /* Succeed if the following insn is the target label, or
12792                  if the following two insns are a barrier and the
12793                  target label.  */
12794               this_insn = next_nonnote_insn (this_insn);
12795               if (this_insn && GET_CODE (this_insn) == BARRIER)
12796                 this_insn = next_nonnote_insn (this_insn);
12797
12798               if (this_insn && this_insn == label
12799                   && insns_skipped < max_insns_skipped)
12800                 {
12801                   if (jump_clobbers)
12802                     {
12803                       arm_ccfsm_state = 2;
12804                       this_insn = next_nonnote_insn (this_insn);
12805                     }
12806                   else
12807                     arm_ccfsm_state = 1;
12808                   succeed = TRUE;
12809                 }
12810               else
12811                 fail = TRUE;
12812               break;
12813
12814             case JUMP_INSN:
12815               /* If this is an unconditional branch to the same label, succeed.
12816                  If it is to another label, do nothing.  If it is conditional,
12817                  fail.  */
12818               /* XXX Probably, the tests for SET and the PC are
12819                  unnecessary.  */
12820
12821               scanbody = PATTERN (this_insn);
12822               if (GET_CODE (scanbody) == SET
12823                   && GET_CODE (SET_DEST (scanbody)) == PC)
12824                 {
12825                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
12826                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
12827                     {
12828                       arm_ccfsm_state = 2;
12829                       succeed = TRUE;
12830                     }
12831                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
12832                     fail = TRUE;
12833                 }
12834               /* Fail if a conditional return is undesirable (e.g. on a
12835                  StrongARM), but still allow this if optimizing for size.  */
12836               else if (GET_CODE (scanbody) == RETURN
12837                        && !use_return_insn (TRUE, NULL)
12838                        && !optimize_size)
12839                 fail = TRUE;
12840               else if (GET_CODE (scanbody) == RETURN
12841                        && seeking_return)
12842                 {
12843                   arm_ccfsm_state = 2;
12844                   succeed = TRUE;
12845                 }
12846               else if (GET_CODE (scanbody) == PARALLEL)
12847                 {
12848                   switch (get_attr_conds (this_insn))
12849                     {
12850                     case CONDS_NOCOND:
12851                       break;
12852                     default:
12853                       fail = TRUE;
12854                       break;
12855                     }
12856                 }
12857               else
12858                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
12859
12860               break;
12861
12862             case INSN:
12863               /* Instructions using or affecting the condition codes make it
12864                  fail.  */
12865               scanbody = PATTERN (this_insn);
12866               if (!(GET_CODE (scanbody) == SET
12867                     || GET_CODE (scanbody) == PARALLEL)
12868                   || get_attr_conds (this_insn) != CONDS_NOCOND)
12869                 fail = TRUE;
12870
12871               /* A conditional cirrus instruction must be followed by
12872                  a non Cirrus instruction.  However, since we
12873                  conditionalize instructions in this function and by
12874                  the time we get here we can't add instructions
12875                  (nops), because shorten_branches() has already been
12876                  called, we will disable conditionalizing Cirrus
12877                  instructions to be safe.  */
12878               if (GET_CODE (scanbody) != USE
12879                   && GET_CODE (scanbody) != CLOBBER
12880                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
12881                 fail = TRUE;
12882               break;
12883
12884             default:
12885               break;
12886             }
12887         }
12888       if (succeed)
12889         {
12890           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
12891             arm_target_label = CODE_LABEL_NUMBER (label);
12892           else
12893             {
12894               gcc_assert (seeking_return || arm_ccfsm_state == 2);
12895
12896               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
12897                 {
12898                   this_insn = next_nonnote_insn (this_insn);
12899                   gcc_assert (!this_insn
12900                               || (GET_CODE (this_insn) != BARRIER
12901                                   && GET_CODE (this_insn) != CODE_LABEL));
12902                 }
12903               if (!this_insn)
12904                 {
12905                   /* Oh, dear! we ran off the end.. give up.  */
12906                   extract_constrain_insn_cached (insn);
12907                   arm_ccfsm_state = 0;
12908                   arm_target_insn = NULL;
12909                   return;
12910                 }
12911               arm_target_insn = this_insn;
12912             }
12913           if (jump_clobbers)
12914             {
12915               gcc_assert (!reverse);
12916               arm_current_cc =
12917                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
12918                                                             0), 0), 1));
12919               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
12920                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12921               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
12922                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12923             }
12924           else
12925             {
12926               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
12927                  what it was.  */
12928               if (!reverse)
12929                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
12930                                                                0));
12931             }
12932
12933           if (reverse || then_not_else)
12934             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12935         }
12936
12937       /* Restore recog_data (getting the attributes of other insns can
12938          destroy this array, but final.c assumes that it remains intact
12939          across this call.  */
12940       extract_constrain_insn_cached (insn);
12941     }
12942 }
12943
12944 /* Output IT instructions.  */
12945 void
12946 thumb2_asm_output_opcode (FILE * stream)
12947 {
12948   char buff[5];
12949   int n;
12950
12951   if (arm_condexec_mask)
12952     {
12953       for (n = 0; n < arm_condexec_masklen; n++)
12954         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
12955       buff[n] = 0;
12956       asm_fprintf(stream, "i%s\t%s\n\t", buff,
12957                   arm_condition_codes[arm_current_cc]);
12958       arm_condexec_mask = 0;
12959     }
12960 }
12961
12962 /* Returns true if REGNO is a valid register
12963    for holding a quantity of type MODE.  */
12964 int
12965 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
12966 {
12967   if (GET_MODE_CLASS (mode) == MODE_CC)
12968     return (regno == CC_REGNUM
12969             || (TARGET_HARD_FLOAT && TARGET_VFP
12970                 && regno == VFPCC_REGNUM));
12971
12972   if (TARGET_THUMB1)
12973     /* For the Thumb we only allow values bigger than SImode in
12974        registers 0 - 6, so that there is always a second low
12975        register available to hold the upper part of the value.
12976        We probably we ought to ensure that the register is the
12977        start of an even numbered register pair.  */
12978     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
12979
12980   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
12981       && IS_CIRRUS_REGNUM (regno))
12982     /* We have outlawed SI values in Cirrus registers because they
12983        reside in the lower 32 bits, but SF values reside in the
12984        upper 32 bits.  This causes gcc all sorts of grief.  We can't
12985        even split the registers into pairs because Cirrus SI values
12986        get sign extended to 64bits-- aldyh.  */
12987     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
12988
12989   if (TARGET_HARD_FLOAT && TARGET_VFP
12990       && IS_VFP_REGNUM (regno))
12991     {
12992       if (mode == SFmode || mode == SImode)
12993         return VFP_REGNO_OK_FOR_SINGLE (regno);
12994
12995       if (mode == DFmode)
12996         return VFP_REGNO_OK_FOR_DOUBLE (regno);
12997       return FALSE;
12998     }
12999
13000   if (TARGET_REALLY_IWMMXT)
13001     {
13002       if (IS_IWMMXT_GR_REGNUM (regno))
13003         return mode == SImode;
13004
13005       if (IS_IWMMXT_REGNUM (regno))
13006         return VALID_IWMMXT_REG_MODE (mode);
13007     }
13008   
13009   /* We allow any value to be stored in the general registers.
13010      Restrict doubleword quantities to even register pairs so that we can
13011      use ldrd.  */
13012   if (regno <= LAST_ARM_REGNUM)
13013     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
13014
13015   if (regno == FRAME_POINTER_REGNUM
13016       || regno == ARG_POINTER_REGNUM)
13017     /* We only allow integers in the fake hard registers.  */
13018     return GET_MODE_CLASS (mode) == MODE_INT;
13019
13020   /* The only registers left are the FPA registers
13021      which we only allow to hold FP values.  */
13022   return (TARGET_HARD_FLOAT && TARGET_FPA
13023           && GET_MODE_CLASS (mode) == MODE_FLOAT
13024           && regno >= FIRST_FPA_REGNUM
13025           && regno <= LAST_FPA_REGNUM);
13026 }
13027
13028 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
13029    not used in arm mode.  */
13030 int
13031 arm_regno_class (int regno)
13032 {
13033   if (TARGET_THUMB1)
13034     {
13035       if (regno == STACK_POINTER_REGNUM)
13036         return STACK_REG;
13037       if (regno == CC_REGNUM)
13038         return CC_REG;
13039       if (regno < 8)
13040         return LO_REGS;
13041       return HI_REGS;
13042     }
13043
13044   if (TARGET_THUMB2 && regno < 8)
13045     return LO_REGS;
13046
13047   if (   regno <= LAST_ARM_REGNUM
13048       || regno == FRAME_POINTER_REGNUM
13049       || regno == ARG_POINTER_REGNUM)
13050     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
13051
13052   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
13053     return TARGET_THUMB2 ? CC_REG : NO_REGS;
13054
13055   if (IS_CIRRUS_REGNUM (regno))
13056     return CIRRUS_REGS;
13057
13058   if (IS_VFP_REGNUM (regno))
13059     {
13060       if (regno <= D7_VFP_REGNUM)
13061         return VFP_D0_D7_REGS;
13062       else if (regno <= LAST_LO_VFP_REGNUM)
13063         return VFP_LO_REGS;
13064       else
13065         return VFP_HI_REGS;
13066     }
13067
13068   if (IS_IWMMXT_REGNUM (regno))
13069     return IWMMXT_REGS;
13070
13071   if (IS_IWMMXT_GR_REGNUM (regno))
13072     return IWMMXT_GR_REGS;
13073
13074   return FPA_REGS;
13075 }
13076
13077 /* Handle a special case when computing the offset
13078    of an argument from the frame pointer.  */
13079 int
13080 arm_debugger_arg_offset (int value, rtx addr)
13081 {
13082   rtx insn;
13083
13084   /* We are only interested if dbxout_parms() failed to compute the offset.  */
13085   if (value != 0)
13086     return 0;
13087
13088   /* We can only cope with the case where the address is held in a register.  */
13089   if (GET_CODE (addr) != REG)
13090     return 0;
13091
13092   /* If we are using the frame pointer to point at the argument, then
13093      an offset of 0 is correct.  */
13094   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
13095     return 0;
13096
13097   /* If we are using the stack pointer to point at the
13098      argument, then an offset of 0 is correct.  */
13099   /* ??? Check this is consistent with thumb2 frame layout.  */
13100   if ((TARGET_THUMB || !frame_pointer_needed)
13101       && REGNO (addr) == SP_REGNUM)
13102     return 0;
13103
13104   /* Oh dear.  The argument is pointed to by a register rather
13105      than being held in a register, or being stored at a known
13106      offset from the frame pointer.  Since GDB only understands
13107      those two kinds of argument we must translate the address
13108      held in the register into an offset from the frame pointer.
13109      We do this by searching through the insns for the function
13110      looking to see where this register gets its value.  If the
13111      register is initialized from the frame pointer plus an offset
13112      then we are in luck and we can continue, otherwise we give up.
13113
13114      This code is exercised by producing debugging information
13115      for a function with arguments like this:
13116
13117            double func (double a, double b, int c, double d) {return d;}
13118
13119      Without this code the stab for parameter 'd' will be set to
13120      an offset of 0 from the frame pointer, rather than 8.  */
13121
13122   /* The if() statement says:
13123
13124      If the insn is a normal instruction
13125      and if the insn is setting the value in a register
13126      and if the register being set is the register holding the address of the argument
13127      and if the address is computing by an addition
13128      that involves adding to a register
13129      which is the frame pointer
13130      a constant integer
13131
13132      then...  */
13133
13134   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13135     {
13136       if (   GET_CODE (insn) == INSN
13137           && GET_CODE (PATTERN (insn)) == SET
13138           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
13139           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
13140           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
13141           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
13142           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
13143              )
13144         {
13145           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
13146
13147           break;
13148         }
13149     }
13150
13151   if (value == 0)
13152     {
13153       debug_rtx (addr);
13154       warning (0, "unable to compute real location of stacked parameter");
13155       value = 8; /* XXX magic hack */
13156     }
13157
13158   return value;
13159 }
13160 \f
13161 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
13162   do                                                                    \
13163     {                                                                   \
13164       if ((MASK) & insn_flags)                                          \
13165         add_builtin_function ((NAME), (TYPE), (CODE),                   \
13166                              BUILT_IN_MD, NULL, NULL_TREE);             \
13167     }                                                                   \
13168   while (0)
13169
13170 struct builtin_description
13171 {
13172   const unsigned int       mask;
13173   const enum insn_code     icode;
13174   const char * const       name;
13175   const enum arm_builtins  code;
13176   const enum rtx_code      comparison;
13177   const unsigned int       flag;
13178 };
13179
13180 static const struct builtin_description bdesc_2arg[] =
13181 {
13182 #define IWMMXT_BUILTIN(code, string, builtin) \
13183   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
13184     ARM_BUILTIN_##builtin, 0, 0 },
13185
13186   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
13187   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
13188   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
13189   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
13190   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
13191   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
13192   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
13193   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
13194   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
13195   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
13196   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
13197   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
13198   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
13199   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
13200   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
13201   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
13202   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
13203   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
13204   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
13205   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
13206   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
13207   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
13208   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
13209   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
13210   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
13211   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
13212   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
13213   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
13214   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
13215   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
13216   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
13217   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
13218   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
13219   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
13220   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
13221   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
13222   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
13223   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
13224   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
13225   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
13226   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
13227   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
13228   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
13229   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
13230   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
13231   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
13232   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
13233   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
13234   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
13235   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
13236   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
13237   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
13238   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
13239   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
13240   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
13241   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
13242   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
13243   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
13244
13245 #define IWMMXT_BUILTIN2(code, builtin) \
13246   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
13247
13248   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
13249   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
13250   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
13251   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
13252   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
13253   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
13254   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
13255   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
13256   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
13257   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
13258   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
13259   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
13260   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
13261   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
13262   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
13263   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
13264   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
13265   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
13266   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
13267   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
13268   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
13269   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
13270   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
13271   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
13272   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
13273   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
13274   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
13275   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
13276   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
13277   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
13278   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
13279   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
13280 };
13281
13282 static const struct builtin_description bdesc_1arg[] =
13283 {
13284   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
13285   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
13286   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
13287   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
13288   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
13289   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
13290   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
13291   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
13292   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
13293   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
13294   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
13295   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
13296   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
13297   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
13298   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
13299   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
13300   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
13301   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
13302 };
13303
13304 /* Set up all the iWMMXt builtins.  This is
13305    not called if TARGET_IWMMXT is zero.  */
13306
13307 static void
13308 arm_init_iwmmxt_builtins (void)
13309 {
13310   const struct builtin_description * d;
13311   size_t i;
13312   tree endlink = void_list_node;
13313
13314   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
13315   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
13316   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
13317
13318   tree int_ftype_int
13319     = build_function_type (integer_type_node,
13320                            tree_cons (NULL_TREE, integer_type_node, endlink));
13321   tree v8qi_ftype_v8qi_v8qi_int
13322     = build_function_type (V8QI_type_node,
13323                            tree_cons (NULL_TREE, V8QI_type_node,
13324                                       tree_cons (NULL_TREE, V8QI_type_node,
13325                                                  tree_cons (NULL_TREE,
13326                                                             integer_type_node,
13327                                                             endlink))));
13328   tree v4hi_ftype_v4hi_int
13329     = build_function_type (V4HI_type_node,
13330                            tree_cons (NULL_TREE, V4HI_type_node,
13331                                       tree_cons (NULL_TREE, integer_type_node,
13332                                                  endlink)));
13333   tree v2si_ftype_v2si_int
13334     = build_function_type (V2SI_type_node,
13335                            tree_cons (NULL_TREE, V2SI_type_node,
13336                                       tree_cons (NULL_TREE, integer_type_node,
13337                                                  endlink)));
13338   tree v2si_ftype_di_di
13339     = build_function_type (V2SI_type_node,
13340                            tree_cons (NULL_TREE, long_long_integer_type_node,
13341                                       tree_cons (NULL_TREE, long_long_integer_type_node,
13342                                                  endlink)));
13343   tree di_ftype_di_int
13344     = build_function_type (long_long_integer_type_node,
13345                            tree_cons (NULL_TREE, long_long_integer_type_node,
13346                                       tree_cons (NULL_TREE, integer_type_node,
13347                                                  endlink)));
13348   tree di_ftype_di_int_int
13349     = build_function_type (long_long_integer_type_node,
13350                            tree_cons (NULL_TREE, long_long_integer_type_node,
13351                                       tree_cons (NULL_TREE, integer_type_node,
13352                                                  tree_cons (NULL_TREE,
13353                                                             integer_type_node,
13354                                                             endlink))));
13355   tree int_ftype_v8qi
13356     = build_function_type (integer_type_node,
13357                            tree_cons (NULL_TREE, V8QI_type_node,
13358                                       endlink));
13359   tree int_ftype_v4hi
13360     = build_function_type (integer_type_node,
13361                            tree_cons (NULL_TREE, V4HI_type_node,
13362                                       endlink));
13363   tree int_ftype_v2si
13364     = build_function_type (integer_type_node,
13365                            tree_cons (NULL_TREE, V2SI_type_node,
13366                                       endlink));
13367   tree int_ftype_v8qi_int
13368     = build_function_type (integer_type_node,
13369                            tree_cons (NULL_TREE, V8QI_type_node,
13370                                       tree_cons (NULL_TREE, integer_type_node,
13371                                                  endlink)));
13372   tree int_ftype_v4hi_int
13373     = build_function_type (integer_type_node,
13374                            tree_cons (NULL_TREE, V4HI_type_node,
13375                                       tree_cons (NULL_TREE, integer_type_node,
13376                                                  endlink)));
13377   tree int_ftype_v2si_int
13378     = build_function_type (integer_type_node,
13379                            tree_cons (NULL_TREE, V2SI_type_node,
13380                                       tree_cons (NULL_TREE, integer_type_node,
13381                                                  endlink)));
13382   tree v8qi_ftype_v8qi_int_int
13383     = build_function_type (V8QI_type_node,
13384                            tree_cons (NULL_TREE, V8QI_type_node,
13385                                       tree_cons (NULL_TREE, integer_type_node,
13386                                                  tree_cons (NULL_TREE,
13387                                                             integer_type_node,
13388                                                             endlink))));
13389   tree v4hi_ftype_v4hi_int_int
13390     = build_function_type (V4HI_type_node,
13391                            tree_cons (NULL_TREE, V4HI_type_node,
13392                                       tree_cons (NULL_TREE, integer_type_node,
13393                                                  tree_cons (NULL_TREE,
13394                                                             integer_type_node,
13395                                                             endlink))));
13396   tree v2si_ftype_v2si_int_int
13397     = build_function_type (V2SI_type_node,
13398                            tree_cons (NULL_TREE, V2SI_type_node,
13399                                       tree_cons (NULL_TREE, integer_type_node,
13400                                                  tree_cons (NULL_TREE,
13401                                                             integer_type_node,
13402                                                             endlink))));
13403   /* Miscellaneous.  */
13404   tree v8qi_ftype_v4hi_v4hi
13405     = build_function_type (V8QI_type_node,
13406                            tree_cons (NULL_TREE, V4HI_type_node,
13407                                       tree_cons (NULL_TREE, V4HI_type_node,
13408                                                  endlink)));
13409   tree v4hi_ftype_v2si_v2si
13410     = build_function_type (V4HI_type_node,
13411                            tree_cons (NULL_TREE, V2SI_type_node,
13412                                       tree_cons (NULL_TREE, V2SI_type_node,
13413                                                  endlink)));
13414   tree v2si_ftype_v4hi_v4hi
13415     = build_function_type (V2SI_type_node,
13416                            tree_cons (NULL_TREE, V4HI_type_node,
13417                                       tree_cons (NULL_TREE, V4HI_type_node,
13418                                                  endlink)));
13419   tree v2si_ftype_v8qi_v8qi
13420     = build_function_type (V2SI_type_node,
13421                            tree_cons (NULL_TREE, V8QI_type_node,
13422                                       tree_cons (NULL_TREE, V8QI_type_node,
13423                                                  endlink)));
13424   tree v4hi_ftype_v4hi_di
13425     = build_function_type (V4HI_type_node,
13426                            tree_cons (NULL_TREE, V4HI_type_node,
13427                                       tree_cons (NULL_TREE,
13428                                                  long_long_integer_type_node,
13429                                                  endlink)));
13430   tree v2si_ftype_v2si_di
13431     = build_function_type (V2SI_type_node,
13432                            tree_cons (NULL_TREE, V2SI_type_node,
13433                                       tree_cons (NULL_TREE,
13434                                                  long_long_integer_type_node,
13435                                                  endlink)));
13436   tree void_ftype_int_int
13437     = build_function_type (void_type_node,
13438                            tree_cons (NULL_TREE, integer_type_node,
13439                                       tree_cons (NULL_TREE, integer_type_node,
13440                                                  endlink)));
13441   tree di_ftype_void
13442     = build_function_type (long_long_unsigned_type_node, endlink);
13443   tree di_ftype_v8qi
13444     = build_function_type (long_long_integer_type_node,
13445                            tree_cons (NULL_TREE, V8QI_type_node,
13446                                       endlink));
13447   tree di_ftype_v4hi
13448     = build_function_type (long_long_integer_type_node,
13449                            tree_cons (NULL_TREE, V4HI_type_node,
13450                                       endlink));
13451   tree di_ftype_v2si
13452     = build_function_type (long_long_integer_type_node,
13453                            tree_cons (NULL_TREE, V2SI_type_node,
13454                                       endlink));
13455   tree v2si_ftype_v4hi
13456     = build_function_type (V2SI_type_node,
13457                            tree_cons (NULL_TREE, V4HI_type_node,
13458                                       endlink));
13459   tree v4hi_ftype_v8qi
13460     = build_function_type (V4HI_type_node,
13461                            tree_cons (NULL_TREE, V8QI_type_node,
13462                                       endlink));
13463
13464   tree di_ftype_di_v4hi_v4hi
13465     = build_function_type (long_long_unsigned_type_node,
13466                            tree_cons (NULL_TREE,
13467                                       long_long_unsigned_type_node,
13468                                       tree_cons (NULL_TREE, V4HI_type_node,
13469                                                  tree_cons (NULL_TREE,
13470                                                             V4HI_type_node,
13471                                                             endlink))));
13472
13473   tree di_ftype_v4hi_v4hi
13474     = build_function_type (long_long_unsigned_type_node,
13475                            tree_cons (NULL_TREE, V4HI_type_node,
13476                                       tree_cons (NULL_TREE, V4HI_type_node,
13477                                                  endlink)));
13478
13479   /* Normal vector binops.  */
13480   tree v8qi_ftype_v8qi_v8qi
13481     = build_function_type (V8QI_type_node,
13482                            tree_cons (NULL_TREE, V8QI_type_node,
13483                                       tree_cons (NULL_TREE, V8QI_type_node,
13484                                                  endlink)));
13485   tree v4hi_ftype_v4hi_v4hi
13486     = build_function_type (V4HI_type_node,
13487                            tree_cons (NULL_TREE, V4HI_type_node,
13488                                       tree_cons (NULL_TREE, V4HI_type_node,
13489                                                  endlink)));
13490   tree v2si_ftype_v2si_v2si
13491     = build_function_type (V2SI_type_node,
13492                            tree_cons (NULL_TREE, V2SI_type_node,
13493                                       tree_cons (NULL_TREE, V2SI_type_node,
13494                                                  endlink)));
13495   tree di_ftype_di_di
13496     = build_function_type (long_long_unsigned_type_node,
13497                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
13498                                       tree_cons (NULL_TREE,
13499                                                  long_long_unsigned_type_node,
13500                                                  endlink)));
13501
13502   /* Add all builtins that are more or less simple operations on two
13503      operands.  */
13504   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13505     {
13506       /* Use one of the operands; the target can have a different mode for
13507          mask-generating compares.  */
13508       enum machine_mode mode;
13509       tree type;
13510
13511       if (d->name == 0)
13512         continue;
13513
13514       mode = insn_data[d->icode].operand[1].mode;
13515
13516       switch (mode)
13517         {
13518         case V8QImode:
13519           type = v8qi_ftype_v8qi_v8qi;
13520           break;
13521         case V4HImode:
13522           type = v4hi_ftype_v4hi_v4hi;
13523           break;
13524         case V2SImode:
13525           type = v2si_ftype_v2si_v2si;
13526           break;
13527         case DImode:
13528           type = di_ftype_di_di;
13529           break;
13530
13531         default:
13532           gcc_unreachable ();
13533         }
13534
13535       def_mbuiltin (d->mask, d->name, type, d->code);
13536     }
13537
13538   /* Add the remaining MMX insns with somewhat more complicated types.  */
13539   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
13540   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
13541   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
13542
13543   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
13544   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
13545   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
13546   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
13547   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
13548   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
13549
13550   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
13551   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
13552   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
13553   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
13554   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
13555   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
13556
13557   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
13558   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
13559   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
13560   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
13561   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
13562   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
13563
13564   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
13565   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
13566   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
13567   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
13568   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
13569   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
13570
13571   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
13572
13573   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
13574   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
13575   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
13576   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
13577
13578   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
13579   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
13580   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
13581   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
13582   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
13583   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
13584   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
13585   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
13586   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
13587
13588   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
13589   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
13590   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
13591
13592   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
13593   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
13594   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
13595
13596   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
13597   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
13598   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
13599   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
13600   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
13601   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
13602
13603   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
13604   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
13605   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
13606   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
13607   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
13608   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
13609   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
13610   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
13611   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
13612   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
13613   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
13614   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
13615
13616   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
13617   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
13618   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
13619   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
13620
13621   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
13622   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
13623   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
13624   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
13625   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
13626   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
13627   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
13628 }
13629
13630 static void
13631 arm_init_tls_builtins (void)
13632 {
13633   tree ftype;
13634   tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
13635   tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
13636
13637   ftype = build_function_type (ptr_type_node, void_list_node);
13638   add_builtin_function ("__builtin_thread_pointer", ftype,
13639                         ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
13640                         NULL, const_nothrow);
13641 }
13642
13643 static void
13644 arm_init_builtins (void)
13645 {
13646   arm_init_tls_builtins ();
13647
13648   if (TARGET_REALLY_IWMMXT)
13649     arm_init_iwmmxt_builtins ();
13650 }
13651
13652 /* Errors in the source file can cause expand_expr to return const0_rtx
13653    where we expect a vector.  To avoid crashing, use one of the vector
13654    clear instructions.  */
13655
13656 static rtx
13657 safe_vector_operand (rtx x, enum machine_mode mode)
13658 {
13659   if (x != const0_rtx)
13660     return x;
13661   x = gen_reg_rtx (mode);
13662
13663   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
13664                                : gen_rtx_SUBREG (DImode, x, 0)));
13665   return x;
13666 }
13667
13668 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
13669
13670 static rtx
13671 arm_expand_binop_builtin (enum insn_code icode,
13672                           tree exp, rtx target)
13673 {
13674   rtx pat;
13675   tree arg0 = CALL_EXPR_ARG (exp, 0);
13676   tree arg1 = CALL_EXPR_ARG (exp, 1);
13677   rtx op0 = expand_normal (arg0);
13678   rtx op1 = expand_normal (arg1);
13679   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13680   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13681   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13682
13683   if (VECTOR_MODE_P (mode0))
13684     op0 = safe_vector_operand (op0, mode0);
13685   if (VECTOR_MODE_P (mode1))
13686     op1 = safe_vector_operand (op1, mode1);
13687
13688   if (! target
13689       || GET_MODE (target) != tmode
13690       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13691     target = gen_reg_rtx (tmode);
13692
13693   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
13694
13695   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13696     op0 = copy_to_mode_reg (mode0, op0);
13697   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13698     op1 = copy_to_mode_reg (mode1, op1);
13699
13700   pat = GEN_FCN (icode) (target, op0, op1);
13701   if (! pat)
13702     return 0;
13703   emit_insn (pat);
13704   return target;
13705 }
13706
13707 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
13708
13709 static rtx
13710 arm_expand_unop_builtin (enum insn_code icode,
13711                          tree exp, rtx target, int do_load)
13712 {
13713   rtx pat;
13714   tree arg0 = CALL_EXPR_ARG (exp, 0);
13715   rtx op0 = expand_normal (arg0);
13716   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13717   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13718
13719   if (! target
13720       || GET_MODE (target) != tmode
13721       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13722     target = gen_reg_rtx (tmode);
13723   if (do_load)
13724     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13725   else
13726     {
13727       if (VECTOR_MODE_P (mode0))
13728         op0 = safe_vector_operand (op0, mode0);
13729
13730       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13731         op0 = copy_to_mode_reg (mode0, op0);
13732     }
13733
13734   pat = GEN_FCN (icode) (target, op0);
13735   if (! pat)
13736     return 0;
13737   emit_insn (pat);
13738   return target;
13739 }
13740
13741 /* Expand an expression EXP that calls a built-in function,
13742    with result going to TARGET if that's convenient
13743    (and in mode MODE if that's convenient).
13744    SUBTARGET may be used as the target for computing one of EXP's operands.
13745    IGNORE is nonzero if the value is to be ignored.  */
13746
13747 static rtx
13748 arm_expand_builtin (tree exp,
13749                     rtx target,
13750                     rtx subtarget ATTRIBUTE_UNUSED,
13751                     enum machine_mode mode ATTRIBUTE_UNUSED,
13752                     int ignore ATTRIBUTE_UNUSED)
13753 {
13754   const struct builtin_description * d;
13755   enum insn_code    icode;
13756   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13757   tree              arg0;
13758   tree              arg1;
13759   tree              arg2;
13760   rtx               op0;
13761   rtx               op1;
13762   rtx               op2;
13763   rtx               pat;
13764   int               fcode = DECL_FUNCTION_CODE (fndecl);
13765   size_t            i;
13766   enum machine_mode tmode;
13767   enum machine_mode mode0;
13768   enum machine_mode mode1;
13769   enum machine_mode mode2;
13770
13771   switch (fcode)
13772     {
13773     case ARM_BUILTIN_TEXTRMSB:
13774     case ARM_BUILTIN_TEXTRMUB:
13775     case ARM_BUILTIN_TEXTRMSH:
13776     case ARM_BUILTIN_TEXTRMUH:
13777     case ARM_BUILTIN_TEXTRMSW:
13778     case ARM_BUILTIN_TEXTRMUW:
13779       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
13780                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
13781                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
13782                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
13783                : CODE_FOR_iwmmxt_textrmw);
13784
13785       arg0 = CALL_EXPR_ARG (exp, 0);
13786       arg1 = CALL_EXPR_ARG (exp, 1);
13787       op0 = expand_normal (arg0);
13788       op1 = expand_normal (arg1);
13789       tmode = insn_data[icode].operand[0].mode;
13790       mode0 = insn_data[icode].operand[1].mode;
13791       mode1 = insn_data[icode].operand[2].mode;
13792
13793       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13794         op0 = copy_to_mode_reg (mode0, op0);
13795       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13796         {
13797           /* @@@ better error message */
13798           error ("selector must be an immediate");
13799           return gen_reg_rtx (tmode);
13800         }
13801       if (target == 0
13802           || GET_MODE (target) != tmode
13803           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13804         target = gen_reg_rtx (tmode);
13805       pat = GEN_FCN (icode) (target, op0, op1);
13806       if (! pat)
13807         return 0;
13808       emit_insn (pat);
13809       return target;
13810
13811     case ARM_BUILTIN_TINSRB:
13812     case ARM_BUILTIN_TINSRH:
13813     case ARM_BUILTIN_TINSRW:
13814       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
13815                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
13816                : CODE_FOR_iwmmxt_tinsrw);
13817       arg0 = CALL_EXPR_ARG (exp, 0);
13818       arg1 = CALL_EXPR_ARG (exp, 1);
13819       arg2 = CALL_EXPR_ARG (exp, 2);
13820       op0 = expand_normal (arg0);
13821       op1 = expand_normal (arg1);
13822       op2 = expand_normal (arg2);
13823       tmode = insn_data[icode].operand[0].mode;
13824       mode0 = insn_data[icode].operand[1].mode;
13825       mode1 = insn_data[icode].operand[2].mode;
13826       mode2 = insn_data[icode].operand[3].mode;
13827
13828       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13829         op0 = copy_to_mode_reg (mode0, op0);
13830       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13831         op1 = copy_to_mode_reg (mode1, op1);
13832       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13833         {
13834           /* @@@ better error message */
13835           error ("selector must be an immediate");
13836           return const0_rtx;
13837         }
13838       if (target == 0
13839           || GET_MODE (target) != tmode
13840           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13841         target = gen_reg_rtx (tmode);
13842       pat = GEN_FCN (icode) (target, op0, op1, op2);
13843       if (! pat)
13844         return 0;
13845       emit_insn (pat);
13846       return target;
13847
13848     case ARM_BUILTIN_SETWCX:
13849       arg0 = CALL_EXPR_ARG (exp, 0);
13850       arg1 = CALL_EXPR_ARG (exp, 1);
13851       op0 = force_reg (SImode, expand_normal (arg0));
13852       op1 = expand_normal (arg1);
13853       emit_insn (gen_iwmmxt_tmcr (op1, op0));
13854       return 0;
13855
13856     case ARM_BUILTIN_GETWCX:
13857       arg0 = CALL_EXPR_ARG (exp, 0);
13858       op0 = expand_normal (arg0);
13859       target = gen_reg_rtx (SImode);
13860       emit_insn (gen_iwmmxt_tmrc (target, op0));
13861       return target;
13862
13863     case ARM_BUILTIN_WSHUFH:
13864       icode = CODE_FOR_iwmmxt_wshufh;
13865       arg0 = CALL_EXPR_ARG (exp, 0);
13866       arg1 = CALL_EXPR_ARG (exp, 1);
13867       op0 = expand_normal (arg0);
13868       op1 = expand_normal (arg1);
13869       tmode = insn_data[icode].operand[0].mode;
13870       mode1 = insn_data[icode].operand[1].mode;
13871       mode2 = insn_data[icode].operand[2].mode;
13872
13873       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13874         op0 = copy_to_mode_reg (mode1, op0);
13875       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13876         {
13877           /* @@@ better error message */
13878           error ("mask must be an immediate");
13879           return const0_rtx;
13880         }
13881       if (target == 0
13882           || GET_MODE (target) != tmode
13883           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13884         target = gen_reg_rtx (tmode);
13885       pat = GEN_FCN (icode) (target, op0, op1);
13886       if (! pat)
13887         return 0;
13888       emit_insn (pat);
13889       return target;
13890
13891     case ARM_BUILTIN_WSADB:
13892       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
13893     case ARM_BUILTIN_WSADH:
13894       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
13895     case ARM_BUILTIN_WSADBZ:
13896       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
13897     case ARM_BUILTIN_WSADHZ:
13898       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
13899
13900       /* Several three-argument builtins.  */
13901     case ARM_BUILTIN_WMACS:
13902     case ARM_BUILTIN_WMACU:
13903     case ARM_BUILTIN_WALIGN:
13904     case ARM_BUILTIN_TMIA:
13905     case ARM_BUILTIN_TMIAPH:
13906     case ARM_BUILTIN_TMIATT:
13907     case ARM_BUILTIN_TMIATB:
13908     case ARM_BUILTIN_TMIABT:
13909     case ARM_BUILTIN_TMIABB:
13910       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
13911                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
13912                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
13913                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
13914                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
13915                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
13916                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
13917                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
13918                : CODE_FOR_iwmmxt_walign);
13919       arg0 = CALL_EXPR_ARG (exp, 0);
13920       arg1 = CALL_EXPR_ARG (exp, 1);
13921       arg2 = CALL_EXPR_ARG (exp, 2);
13922       op0 = expand_normal (arg0);
13923       op1 = expand_normal (arg1);
13924       op2 = expand_normal (arg2);
13925       tmode = insn_data[icode].operand[0].mode;
13926       mode0 = insn_data[icode].operand[1].mode;
13927       mode1 = insn_data[icode].operand[2].mode;
13928       mode2 = insn_data[icode].operand[3].mode;
13929
13930       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13931         op0 = copy_to_mode_reg (mode0, op0);
13932       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13933         op1 = copy_to_mode_reg (mode1, op1);
13934       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13935         op2 = copy_to_mode_reg (mode2, op2);
13936       if (target == 0
13937           || GET_MODE (target) != tmode
13938           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13939         target = gen_reg_rtx (tmode);
13940       pat = GEN_FCN (icode) (target, op0, op1, op2);
13941       if (! pat)
13942         return 0;
13943       emit_insn (pat);
13944       return target;
13945
13946     case ARM_BUILTIN_WZERO:
13947       target = gen_reg_rtx (DImode);
13948       emit_insn (gen_iwmmxt_clrdi (target));
13949       return target;
13950
13951     case ARM_BUILTIN_THREAD_POINTER:
13952       return arm_load_tp (target);
13953
13954     default:
13955       break;
13956     }
13957
13958   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13959     if (d->code == (const enum arm_builtins) fcode)
13960       return arm_expand_binop_builtin (d->icode, exp, target);
13961
13962   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13963     if (d->code == (const enum arm_builtins) fcode)
13964       return arm_expand_unop_builtin (d->icode, exp, target, 0);
13965
13966   /* @@@ Should really do something sensible here.  */
13967   return NULL_RTX;
13968 }
13969 \f
13970 /* Return the number (counting from 0) of
13971    the least significant set bit in MASK.  */
13972
13973 inline static int
13974 number_of_first_bit_set (unsigned mask)
13975 {
13976   int bit;
13977
13978   for (bit = 0;
13979        (mask & (1 << bit)) == 0;
13980        ++bit)
13981     continue;
13982
13983   return bit;
13984 }
13985
13986 /* Emit code to push or pop registers to or from the stack.  F is the
13987    assembly file.  MASK is the registers to push or pop.  PUSH is
13988    nonzero if we should push, and zero if we should pop.  For debugging
13989    output, if pushing, adjust CFA_OFFSET by the amount of space added
13990    to the stack.  REAL_REGS should have the same number of bits set as
13991    MASK, and will be used instead (in the same order) to describe which
13992    registers were saved - this is used to mark the save slots when we
13993    push high registers after moving them to low registers.  */
13994 static void
13995 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
13996                unsigned long real_regs)
13997 {
13998   int regno;
13999   int lo_mask = mask & 0xFF;
14000   int pushed_words = 0;
14001
14002   gcc_assert (mask);
14003
14004   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
14005     {
14006       /* Special case.  Do not generate a POP PC statement here, do it in
14007          thumb_exit() */
14008       thumb_exit (f, -1);
14009       return;
14010     }
14011
14012   if (ARM_EABI_UNWIND_TABLES && push)
14013     {
14014       fprintf (f, "\t.save\t{");
14015       for (regno = 0; regno < 15; regno++)
14016         {
14017           if (real_regs & (1 << regno))
14018             {
14019               if (real_regs & ((1 << regno) -1))
14020                 fprintf (f, ", ");
14021               asm_fprintf (f, "%r", regno);
14022             }
14023         }
14024       fprintf (f, "}\n");
14025     }
14026
14027   fprintf (f, "\t%s\t{", push ? "push" : "pop");
14028
14029   /* Look at the low registers first.  */
14030   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
14031     {
14032       if (lo_mask & 1)
14033         {
14034           asm_fprintf (f, "%r", regno);
14035
14036           if ((lo_mask & ~1) != 0)
14037             fprintf (f, ", ");
14038
14039           pushed_words++;
14040         }
14041     }
14042
14043   if (push && (mask & (1 << LR_REGNUM)))
14044     {
14045       /* Catch pushing the LR.  */
14046       if (mask & 0xFF)
14047         fprintf (f, ", ");
14048
14049       asm_fprintf (f, "%r", LR_REGNUM);
14050
14051       pushed_words++;
14052     }
14053   else if (!push && (mask & (1 << PC_REGNUM)))
14054     {
14055       /* Catch popping the PC.  */
14056       if (TARGET_INTERWORK || TARGET_BACKTRACE
14057           || current_function_calls_eh_return)
14058         {
14059           /* The PC is never poped directly, instead
14060              it is popped into r3 and then BX is used.  */
14061           fprintf (f, "}\n");
14062
14063           thumb_exit (f, -1);
14064
14065           return;
14066         }
14067       else
14068         {
14069           if (mask & 0xFF)
14070             fprintf (f, ", ");
14071
14072           asm_fprintf (f, "%r", PC_REGNUM);
14073         }
14074     }
14075
14076   fprintf (f, "}\n");
14077
14078   if (push && pushed_words && dwarf2out_do_frame ())
14079     {
14080       char *l = dwarf2out_cfi_label ();
14081       int pushed_mask = real_regs;
14082
14083       *cfa_offset += pushed_words * 4;
14084       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
14085
14086       pushed_words = 0;
14087       pushed_mask = real_regs;
14088       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
14089         {
14090           if (pushed_mask & 1)
14091             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
14092         }
14093     }
14094 }
14095
14096 /* Generate code to return from a thumb function.
14097    If 'reg_containing_return_addr' is -1, then the return address is
14098    actually on the stack, at the stack pointer.  */
14099 static void
14100 thumb_exit (FILE *f, int reg_containing_return_addr)
14101 {
14102   unsigned regs_available_for_popping;
14103   unsigned regs_to_pop;
14104   int pops_needed;
14105   unsigned available;
14106   unsigned required;
14107   int mode;
14108   int size;
14109   int restore_a4 = FALSE;
14110
14111   /* Compute the registers we need to pop.  */
14112   regs_to_pop = 0;
14113   pops_needed = 0;
14114
14115   if (reg_containing_return_addr == -1)
14116     {
14117       regs_to_pop |= 1 << LR_REGNUM;
14118       ++pops_needed;
14119     }
14120
14121   if (TARGET_BACKTRACE)
14122     {
14123       /* Restore the (ARM) frame pointer and stack pointer.  */
14124       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
14125       pops_needed += 2;
14126     }
14127
14128   /* If there is nothing to pop then just emit the BX instruction and
14129      return.  */
14130   if (pops_needed == 0)
14131     {
14132       if (current_function_calls_eh_return)
14133         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
14134
14135       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
14136       return;
14137     }
14138   /* Otherwise if we are not supporting interworking and we have not created
14139      a backtrace structure and the function was not entered in ARM mode then
14140      just pop the return address straight into the PC.  */
14141   else if (!TARGET_INTERWORK
14142            && !TARGET_BACKTRACE
14143            && !is_called_in_ARM_mode (current_function_decl)
14144            && !current_function_calls_eh_return)
14145     {
14146       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
14147       return;
14148     }
14149
14150   /* Find out how many of the (return) argument registers we can corrupt.  */
14151   regs_available_for_popping = 0;
14152
14153   /* If returning via __builtin_eh_return, the bottom three registers
14154      all contain information needed for the return.  */
14155   if (current_function_calls_eh_return)
14156     size = 12;
14157   else
14158     {
14159       /* If we can deduce the registers used from the function's
14160          return value.  This is more reliable that examining
14161          df_regs_ever_live_p () because that will be set if the register is
14162          ever used in the function, not just if the register is used
14163          to hold a return value.  */
14164
14165       if (current_function_return_rtx != 0)
14166         mode = GET_MODE (current_function_return_rtx);
14167       else
14168         mode = DECL_MODE (DECL_RESULT (current_function_decl));
14169
14170       size = GET_MODE_SIZE (mode);
14171
14172       if (size == 0)
14173         {
14174           /* In a void function we can use any argument register.
14175              In a function that returns a structure on the stack
14176              we can use the second and third argument registers.  */
14177           if (mode == VOIDmode)
14178             regs_available_for_popping =
14179               (1 << ARG_REGISTER (1))
14180               | (1 << ARG_REGISTER (2))
14181               | (1 << ARG_REGISTER (3));
14182           else
14183             regs_available_for_popping =
14184               (1 << ARG_REGISTER (2))
14185               | (1 << ARG_REGISTER (3));
14186         }
14187       else if (size <= 4)
14188         regs_available_for_popping =
14189           (1 << ARG_REGISTER (2))
14190           | (1 << ARG_REGISTER (3));
14191       else if (size <= 8)
14192         regs_available_for_popping =
14193           (1 << ARG_REGISTER (3));
14194     }
14195
14196   /* Match registers to be popped with registers into which we pop them.  */
14197   for (available = regs_available_for_popping,
14198        required  = regs_to_pop;
14199        required != 0 && available != 0;
14200        available &= ~(available & - available),
14201        required  &= ~(required  & - required))
14202     -- pops_needed;
14203
14204   /* If we have any popping registers left over, remove them.  */
14205   if (available > 0)
14206     regs_available_for_popping &= ~available;
14207
14208   /* Otherwise if we need another popping register we can use
14209      the fourth argument register.  */
14210   else if (pops_needed)
14211     {
14212       /* If we have not found any free argument registers and
14213          reg a4 contains the return address, we must move it.  */
14214       if (regs_available_for_popping == 0
14215           && reg_containing_return_addr == LAST_ARG_REGNUM)
14216         {
14217           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
14218           reg_containing_return_addr = LR_REGNUM;
14219         }
14220       else if (size > 12)
14221         {
14222           /* Register a4 is being used to hold part of the return value,
14223              but we have dire need of a free, low register.  */
14224           restore_a4 = TRUE;
14225
14226           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
14227         }
14228
14229       if (reg_containing_return_addr != LAST_ARG_REGNUM)
14230         {
14231           /* The fourth argument register is available.  */
14232           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
14233
14234           --pops_needed;
14235         }
14236     }
14237
14238   /* Pop as many registers as we can.  */
14239   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
14240                  regs_available_for_popping);
14241
14242   /* Process the registers we popped.  */
14243   if (reg_containing_return_addr == -1)
14244     {
14245       /* The return address was popped into the lowest numbered register.  */
14246       regs_to_pop &= ~(1 << LR_REGNUM);
14247
14248       reg_containing_return_addr =
14249         number_of_first_bit_set (regs_available_for_popping);
14250
14251       /* Remove this register for the mask of available registers, so that
14252          the return address will not be corrupted by further pops.  */
14253       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
14254     }
14255
14256   /* If we popped other registers then handle them here.  */
14257   if (regs_available_for_popping)
14258     {
14259       int frame_pointer;
14260
14261       /* Work out which register currently contains the frame pointer.  */
14262       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
14263
14264       /* Move it into the correct place.  */
14265       asm_fprintf (f, "\tmov\t%r, %r\n",
14266                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
14267
14268       /* (Temporarily) remove it from the mask of popped registers.  */
14269       regs_available_for_popping &= ~(1 << frame_pointer);
14270       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
14271
14272       if (regs_available_for_popping)
14273         {
14274           int stack_pointer;
14275
14276           /* We popped the stack pointer as well,
14277              find the register that contains it.  */
14278           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
14279
14280           /* Move it into the stack register.  */
14281           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
14282
14283           /* At this point we have popped all necessary registers, so
14284              do not worry about restoring regs_available_for_popping
14285              to its correct value:
14286
14287              assert (pops_needed == 0)
14288              assert (regs_available_for_popping == (1 << frame_pointer))
14289              assert (regs_to_pop == (1 << STACK_POINTER))  */
14290         }
14291       else
14292         {
14293           /* Since we have just move the popped value into the frame
14294              pointer, the popping register is available for reuse, and
14295              we know that we still have the stack pointer left to pop.  */
14296           regs_available_for_popping |= (1 << frame_pointer);
14297         }
14298     }
14299
14300   /* If we still have registers left on the stack, but we no longer have
14301      any registers into which we can pop them, then we must move the return
14302      address into the link register and make available the register that
14303      contained it.  */
14304   if (regs_available_for_popping == 0 && pops_needed > 0)
14305     {
14306       regs_available_for_popping |= 1 << reg_containing_return_addr;
14307
14308       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
14309                    reg_containing_return_addr);
14310
14311       reg_containing_return_addr = LR_REGNUM;
14312     }
14313
14314   /* If we have registers left on the stack then pop some more.
14315      We know that at most we will want to pop FP and SP.  */
14316   if (pops_needed > 0)
14317     {
14318       int  popped_into;
14319       int  move_to;
14320
14321       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
14322                      regs_available_for_popping);
14323
14324       /* We have popped either FP or SP.
14325          Move whichever one it is into the correct register.  */
14326       popped_into = number_of_first_bit_set (regs_available_for_popping);
14327       move_to     = number_of_first_bit_set (regs_to_pop);
14328
14329       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
14330
14331       regs_to_pop &= ~(1 << move_to);
14332
14333       --pops_needed;
14334     }
14335
14336   /* If we still have not popped everything then we must have only
14337      had one register available to us and we are now popping the SP.  */
14338   if (pops_needed > 0)
14339     {
14340       int  popped_into;
14341
14342       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
14343                      regs_available_for_popping);
14344
14345       popped_into = number_of_first_bit_set (regs_available_for_popping);
14346
14347       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
14348       /*
14349         assert (regs_to_pop == (1 << STACK_POINTER))
14350         assert (pops_needed == 1)
14351       */
14352     }
14353
14354   /* If necessary restore the a4 register.  */
14355   if (restore_a4)
14356     {
14357       if (reg_containing_return_addr != LR_REGNUM)
14358         {
14359           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
14360           reg_containing_return_addr = LR_REGNUM;
14361         }
14362
14363       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
14364     }
14365
14366   if (current_function_calls_eh_return)
14367     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
14368
14369   /* Return to caller.  */
14370   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
14371 }
14372
14373 \f
14374 void
14375 thumb1_final_prescan_insn (rtx insn)
14376 {
14377   if (flag_print_asm_name)
14378     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
14379                  INSN_ADDRESSES (INSN_UID (insn)));
14380 }
14381
14382 int
14383 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
14384 {
14385   unsigned HOST_WIDE_INT mask = 0xff;
14386   int i;
14387
14388   if (val == 0) /* XXX */
14389     return 0;
14390
14391   for (i = 0; i < 25; i++)
14392     if ((val & (mask << i)) == val)
14393       return 1;
14394
14395   return 0;
14396 }
14397
14398 /* Returns nonzero if the current function contains,
14399    or might contain a far jump.  */
14400 static int
14401 thumb_far_jump_used_p (void)
14402 {
14403   rtx insn;
14404
14405   /* This test is only important for leaf functions.  */
14406   /* assert (!leaf_function_p ()); */
14407
14408   /* If we have already decided that far jumps may be used,
14409      do not bother checking again, and always return true even if
14410      it turns out that they are not being used.  Once we have made
14411      the decision that far jumps are present (and that hence the link
14412      register will be pushed onto the stack) we cannot go back on it.  */
14413   if (cfun->machine->far_jump_used)
14414     return 1;
14415
14416   /* If this function is not being called from the prologue/epilogue
14417      generation code then it must be being called from the
14418      INITIAL_ELIMINATION_OFFSET macro.  */
14419   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
14420     {
14421       /* In this case we know that we are being asked about the elimination
14422          of the arg pointer register.  If that register is not being used,
14423          then there are no arguments on the stack, and we do not have to
14424          worry that a far jump might force the prologue to push the link
14425          register, changing the stack offsets.  In this case we can just
14426          return false, since the presence of far jumps in the function will
14427          not affect stack offsets.
14428
14429          If the arg pointer is live (or if it was live, but has now been
14430          eliminated and so set to dead) then we do have to test to see if
14431          the function might contain a far jump.  This test can lead to some
14432          false negatives, since before reload is completed, then length of
14433          branch instructions is not known, so gcc defaults to returning their
14434          longest length, which in turn sets the far jump attribute to true.
14435
14436          A false negative will not result in bad code being generated, but it
14437          will result in a needless push and pop of the link register.  We
14438          hope that this does not occur too often.
14439
14440          If we need doubleword stack alignment this could affect the other
14441          elimination offsets so we can't risk getting it wrong.  */
14442       if (df_regs_ever_live_p (ARG_POINTER_REGNUM))
14443         cfun->machine->arg_pointer_live = 1;
14444       else if (!cfun->machine->arg_pointer_live)
14445         return 0;
14446     }
14447
14448   /* Check to see if the function contains a branch
14449      insn with the far jump attribute set.  */
14450   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14451     {
14452       if (GET_CODE (insn) == JUMP_INSN
14453           /* Ignore tablejump patterns.  */
14454           && GET_CODE (PATTERN (insn)) != ADDR_VEC
14455           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
14456           && get_attr_far_jump (insn) == FAR_JUMP_YES
14457           )
14458         {
14459           /* Record the fact that we have decided that
14460              the function does use far jumps.  */
14461           cfun->machine->far_jump_used = 1;
14462           return 1;
14463         }
14464     }
14465
14466   return 0;
14467 }
14468
14469 /* Return nonzero if FUNC must be entered in ARM mode.  */
14470 int
14471 is_called_in_ARM_mode (tree func)
14472 {
14473   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
14474
14475   /* Ignore the problem about functions whose address is taken.  */
14476   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
14477     return TRUE;
14478
14479 #ifdef ARM_PE
14480   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
14481 #else
14482   return FALSE;
14483 #endif
14484 }
14485
14486 /* The bits which aren't usefully expanded as rtl.  */
14487 const char *
14488 thumb_unexpanded_epilogue (void)
14489 {
14490   int regno;
14491   unsigned long live_regs_mask = 0;
14492   int high_regs_pushed = 0;
14493   int had_to_push_lr;
14494   int size;
14495
14496   if (return_used_this_function)
14497     return "";
14498
14499   if (IS_NAKED (arm_current_func_type ()))
14500     return "";
14501
14502   live_regs_mask = thumb1_compute_save_reg_mask ();
14503   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
14504
14505   /* If we can deduce the registers used from the function's return value.
14506      This is more reliable that examining df_regs_ever_live_p () because that
14507      will be set if the register is ever used in the function, not just if
14508      the register is used to hold a return value.  */
14509   size = arm_size_return_regs ();
14510
14511   /* The prolog may have pushed some high registers to use as
14512      work registers.  e.g. the testsuite file:
14513      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
14514      compiles to produce:
14515         push    {r4, r5, r6, r7, lr}
14516         mov     r7, r9
14517         mov     r6, r8
14518         push    {r6, r7}
14519      as part of the prolog.  We have to undo that pushing here.  */
14520
14521   if (high_regs_pushed)
14522     {
14523       unsigned long mask = live_regs_mask & 0xff;
14524       int next_hi_reg;
14525
14526       /* The available low registers depend on the size of the value we are
14527          returning.  */
14528       if (size <= 12)
14529         mask |=  1 << 3;
14530       if (size <= 8)
14531         mask |= 1 << 2;
14532
14533       if (mask == 0)
14534         /* Oh dear!  We have no low registers into which we can pop
14535            high registers!  */
14536         internal_error
14537           ("no low registers available for popping high registers");
14538
14539       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
14540         if (live_regs_mask & (1 << next_hi_reg))
14541           break;
14542
14543       while (high_regs_pushed)
14544         {
14545           /* Find lo register(s) into which the high register(s) can
14546              be popped.  */
14547           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
14548             {
14549               if (mask & (1 << regno))
14550                 high_regs_pushed--;
14551               if (high_regs_pushed == 0)
14552                 break;
14553             }
14554
14555           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
14556
14557           /* Pop the values into the low register(s).  */
14558           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
14559
14560           /* Move the value(s) into the high registers.  */
14561           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
14562             {
14563               if (mask & (1 << regno))
14564                 {
14565                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
14566                                regno);
14567
14568                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
14569                     if (live_regs_mask & (1 << next_hi_reg))
14570                       break;
14571                 }
14572             }
14573         }
14574       live_regs_mask &= ~0x0f00;
14575     }
14576
14577   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
14578   live_regs_mask &= 0xff;
14579
14580   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
14581     {
14582       /* Pop the return address into the PC.  */
14583       if (had_to_push_lr)
14584         live_regs_mask |= 1 << PC_REGNUM;
14585
14586       /* Either no argument registers were pushed or a backtrace
14587          structure was created which includes an adjusted stack
14588          pointer, so just pop everything.  */
14589       if (live_regs_mask)
14590         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
14591                        live_regs_mask);
14592
14593       /* We have either just popped the return address into the
14594          PC or it is was kept in LR for the entire function.  */
14595       if (!had_to_push_lr)
14596         thumb_exit (asm_out_file, LR_REGNUM);
14597     }
14598   else
14599     {
14600       /* Pop everything but the return address.  */
14601       if (live_regs_mask)
14602         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
14603                        live_regs_mask);
14604
14605       if (had_to_push_lr)
14606         {
14607           if (size > 12)
14608             {
14609               /* We have no free low regs, so save one.  */
14610               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
14611                            LAST_ARG_REGNUM);
14612             }
14613
14614           /* Get the return address into a temporary register.  */
14615           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
14616                          1 << LAST_ARG_REGNUM);
14617
14618           if (size > 12)
14619             {
14620               /* Move the return address to lr.  */
14621               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
14622                            LAST_ARG_REGNUM);
14623               /* Restore the low register.  */
14624               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
14625                            IP_REGNUM);
14626               regno = LR_REGNUM;
14627             }
14628           else
14629             regno = LAST_ARG_REGNUM;
14630         }
14631       else
14632         regno = LR_REGNUM;
14633
14634       /* Remove the argument registers that were pushed onto the stack.  */
14635       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
14636                    SP_REGNUM, SP_REGNUM,
14637                    current_function_pretend_args_size);
14638
14639       thumb_exit (asm_out_file, regno);
14640     }
14641
14642   return "";
14643 }
14644
14645 /* Functions to save and restore machine-specific function data.  */
14646 static struct machine_function *
14647 arm_init_machine_status (void)
14648 {
14649   struct machine_function *machine;
14650   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
14651
14652 #if ARM_FT_UNKNOWN != 0
14653   machine->func_type = ARM_FT_UNKNOWN;
14654 #endif
14655   return machine;
14656 }
14657
14658 /* Return an RTX indicating where the return address to the
14659    calling function can be found.  */
14660 rtx
14661 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
14662 {
14663   if (count != 0)
14664     return NULL_RTX;
14665
14666   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
14667 }
14668
14669 /* Do anything needed before RTL is emitted for each function.  */
14670 void
14671 arm_init_expanders (void)
14672 {
14673   /* Arrange to initialize and mark the machine per-function status.  */
14674   init_machine_status = arm_init_machine_status;
14675
14676   /* This is to stop the combine pass optimizing away the alignment
14677      adjustment of va_arg.  */
14678   /* ??? It is claimed that this should not be necessary.  */
14679   if (cfun)
14680     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
14681 }
14682
14683
14684 /* Like arm_compute_initial_elimination offset.  Simpler because there
14685    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
14686    to point at the base of the local variables after static stack
14687    space for a function has been allocated.  */
14688
14689 HOST_WIDE_INT
14690 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
14691 {
14692   arm_stack_offsets *offsets;
14693
14694   offsets = arm_get_frame_offsets ();
14695
14696   switch (from)
14697     {
14698     case ARG_POINTER_REGNUM:
14699       switch (to)
14700         {
14701         case STACK_POINTER_REGNUM:
14702           return offsets->outgoing_args - offsets->saved_args;
14703
14704         case FRAME_POINTER_REGNUM:
14705           return offsets->soft_frame - offsets->saved_args;
14706
14707         case ARM_HARD_FRAME_POINTER_REGNUM:
14708           return offsets->saved_regs - offsets->saved_args;
14709
14710         case THUMB_HARD_FRAME_POINTER_REGNUM:
14711           return offsets->locals_base - offsets->saved_args;
14712
14713         default:
14714           gcc_unreachable ();
14715         }
14716       break;
14717
14718     case FRAME_POINTER_REGNUM:
14719       switch (to)
14720         {
14721         case STACK_POINTER_REGNUM:
14722           return offsets->outgoing_args - offsets->soft_frame;
14723
14724         case ARM_HARD_FRAME_POINTER_REGNUM:
14725           return offsets->saved_regs - offsets->soft_frame;
14726
14727         case THUMB_HARD_FRAME_POINTER_REGNUM:
14728           return offsets->locals_base - offsets->soft_frame;
14729
14730         default:
14731           gcc_unreachable ();
14732         }
14733       break;
14734
14735     default:
14736       gcc_unreachable ();
14737     }
14738 }
14739
14740 /* Generate the rest of a function's prologue.  */
14741 void
14742 thumb1_expand_prologue (void)
14743 {
14744   rtx insn, dwarf;
14745
14746   HOST_WIDE_INT amount;
14747   arm_stack_offsets *offsets;
14748   unsigned long func_type;
14749   int regno;
14750   unsigned long live_regs_mask;
14751
14752   func_type = arm_current_func_type ();
14753
14754   /* Naked functions don't have prologues.  */
14755   if (IS_NAKED (func_type))
14756     return;
14757
14758   if (IS_INTERRUPT (func_type))
14759     {
14760       error ("interrupt Service Routines cannot be coded in Thumb mode");
14761       return;
14762     }
14763
14764   live_regs_mask = thumb1_compute_save_reg_mask ();
14765   /* Load the pic register before setting the frame pointer,
14766      so we can use r7 as a temporary work register.  */
14767   if (flag_pic && arm_pic_register != INVALID_REGNUM)
14768     arm_load_pic_register (live_regs_mask);
14769
14770   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
14771     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
14772                     stack_pointer_rtx);
14773
14774   offsets = arm_get_frame_offsets ();
14775   amount = offsets->outgoing_args - offsets->saved_regs;
14776   if (amount)
14777     {
14778       if (amount < 512)
14779         {
14780           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14781                                         GEN_INT (- amount)));
14782           RTX_FRAME_RELATED_P (insn) = 1;
14783         }
14784       else
14785         {
14786           rtx reg;
14787
14788           /* The stack decrement is too big for an immediate value in a single
14789              insn.  In theory we could issue multiple subtracts, but after
14790              three of them it becomes more space efficient to place the full
14791              value in the constant pool and load into a register.  (Also the
14792              ARM debugger really likes to see only one stack decrement per
14793              function).  So instead we look for a scratch register into which
14794              we can load the decrement, and then we subtract this from the
14795              stack pointer.  Unfortunately on the thumb the only available
14796              scratch registers are the argument registers, and we cannot use
14797              these as they may hold arguments to the function.  Instead we
14798              attempt to locate a call preserved register which is used by this
14799              function.  If we can find one, then we know that it will have
14800              been pushed at the start of the prologue and so we can corrupt
14801              it now.  */
14802           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
14803             if (live_regs_mask & (1 << regno)
14804                 && !(frame_pointer_needed
14805                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
14806               break;
14807
14808           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
14809             {
14810               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
14811
14812               /* Choose an arbitrary, non-argument low register.  */
14813               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
14814
14815               /* Save it by copying it into a high, scratch register.  */
14816               emit_insn (gen_movsi (spare, reg));
14817               /* Add a USE to stop propagate_one_insn() from barfing.  */
14818               emit_insn (gen_prologue_use (spare));
14819
14820               /* Decrement the stack.  */
14821               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
14822               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
14823                                             stack_pointer_rtx, reg));
14824               RTX_FRAME_RELATED_P (insn) = 1;
14825               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14826                                    plus_constant (stack_pointer_rtx,
14827                                                   -amount));
14828               RTX_FRAME_RELATED_P (dwarf) = 1;
14829               REG_NOTES (insn)
14830                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
14831                                      REG_NOTES (insn));
14832
14833               /* Restore the low register's original value.  */
14834               emit_insn (gen_movsi (reg, spare));
14835
14836               /* Emit a USE of the restored scratch register, so that flow
14837                  analysis will not consider the restore redundant.  The
14838                  register won't be used again in this function and isn't
14839                  restored by the epilogue.  */
14840               emit_insn (gen_prologue_use (reg));
14841             }
14842           else
14843             {
14844               reg = gen_rtx_REG (SImode, regno);
14845
14846               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
14847
14848               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
14849                                             stack_pointer_rtx, reg));
14850               RTX_FRAME_RELATED_P (insn) = 1;
14851               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14852                                    plus_constant (stack_pointer_rtx,
14853                                                   -amount));
14854               RTX_FRAME_RELATED_P (dwarf) = 1;
14855               REG_NOTES (insn)
14856                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
14857                                      REG_NOTES (insn));
14858             }
14859         }
14860     }
14861
14862   if (frame_pointer_needed)
14863     thumb_set_frame_pointer (offsets);
14864
14865   /* If we are profiling, make sure no instructions are scheduled before
14866      the call to mcount.  Similarly if the user has requested no
14867      scheduling in the prolog.  Similarly if we want non-call exceptions
14868      using the EABI unwinder, to prevent faulting instructions from being
14869      swapped with a stack adjustment.  */
14870   if (current_function_profile || !TARGET_SCHED_PROLOG
14871       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
14872     emit_insn (gen_blockage ());
14873
14874   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
14875   if (live_regs_mask & 0xff)
14876     cfun->machine->lr_save_eliminated = 0;
14877 }
14878
14879
14880 void
14881 thumb1_expand_epilogue (void)
14882 {
14883   HOST_WIDE_INT amount;
14884   arm_stack_offsets *offsets;
14885   int regno;
14886
14887   /* Naked functions don't have prologues.  */
14888   if (IS_NAKED (arm_current_func_type ()))
14889     return;
14890
14891   offsets = arm_get_frame_offsets ();
14892   amount = offsets->outgoing_args - offsets->saved_regs;
14893
14894   if (frame_pointer_needed)
14895     {
14896       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
14897       amount = offsets->locals_base - offsets->saved_regs;
14898     }
14899
14900   gcc_assert (amount >= 0);
14901   if (amount)
14902     {
14903       if (amount < 512)
14904         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14905                                GEN_INT (amount)));
14906       else
14907         {
14908           /* r3 is always free in the epilogue.  */
14909           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
14910
14911           emit_insn (gen_movsi (reg, GEN_INT (amount)));
14912           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
14913         }
14914     }
14915
14916   /* Emit a USE (stack_pointer_rtx), so that
14917      the stack adjustment will not be deleted.  */
14918   emit_insn (gen_prologue_use (stack_pointer_rtx));
14919
14920   if (current_function_profile || !TARGET_SCHED_PROLOG)
14921     emit_insn (gen_blockage ());
14922
14923   /* Emit a clobber for each insn that will be restored in the epilogue,
14924      so that flow2 will get register lifetimes correct.  */
14925   for (regno = 0; regno < 13; regno++)
14926     if (df_regs_ever_live_p (regno) && !call_used_regs[regno])
14927       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
14928
14929   if (! df_regs_ever_live_p (LR_REGNUM))
14930     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
14931 }
14932
14933 static void
14934 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
14935 {
14936   unsigned long live_regs_mask = 0;
14937   unsigned long l_mask;
14938   unsigned high_regs_pushed = 0;
14939   int cfa_offset = 0;
14940   int regno;
14941
14942   if (IS_NAKED (arm_current_func_type ()))
14943     return;
14944
14945   if (is_called_in_ARM_mode (current_function_decl))
14946     {
14947       const char * name;
14948
14949       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
14950       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
14951                   == SYMBOL_REF);
14952       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
14953
14954       /* Generate code sequence to switch us into Thumb mode.  */
14955       /* The .code 32 directive has already been emitted by
14956          ASM_DECLARE_FUNCTION_NAME.  */
14957       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
14958       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
14959
14960       /* Generate a label, so that the debugger will notice the
14961          change in instruction sets.  This label is also used by
14962          the assembler to bypass the ARM code when this function
14963          is called from a Thumb encoded function elsewhere in the
14964          same file.  Hence the definition of STUB_NAME here must
14965          agree with the definition in gas/config/tc-arm.c.  */
14966
14967 #define STUB_NAME ".real_start_of"
14968
14969       fprintf (f, "\t.code\t16\n");
14970 #ifdef ARM_PE
14971       if (arm_dllexport_name_p (name))
14972         name = arm_strip_name_encoding (name);
14973 #endif
14974       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
14975       fprintf (f, "\t.thumb_func\n");
14976       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
14977     }
14978
14979   if (current_function_pretend_args_size)
14980     {
14981       /* Output unwind directive for the stack adjustment.  */
14982       if (ARM_EABI_UNWIND_TABLES)
14983         fprintf (f, "\t.pad #%d\n",
14984                  current_function_pretend_args_size);
14985
14986       if (cfun->machine->uses_anonymous_args)
14987         {
14988           int num_pushes;
14989
14990           fprintf (f, "\tpush\t{");
14991
14992           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
14993
14994           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
14995                regno <= LAST_ARG_REGNUM;
14996                regno++)
14997             asm_fprintf (f, "%r%s", regno,
14998                          regno == LAST_ARG_REGNUM ? "" : ", ");
14999
15000           fprintf (f, "}\n");
15001         }
15002       else
15003         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
15004                      SP_REGNUM, SP_REGNUM,
15005                      current_function_pretend_args_size);
15006
15007       /* We don't need to record the stores for unwinding (would it
15008          help the debugger any if we did?), but record the change in
15009          the stack pointer.  */
15010       if (dwarf2out_do_frame ())
15011         {
15012           char *l = dwarf2out_cfi_label ();
15013
15014           cfa_offset = cfa_offset + current_function_pretend_args_size;
15015           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
15016         }
15017     }
15018
15019   /* Get the registers we are going to push.  */
15020   live_regs_mask = thumb1_compute_save_reg_mask ();
15021   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
15022   l_mask = live_regs_mask & 0x40ff;
15023   /* Then count how many other high registers will need to be pushed.  */
15024   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
15025
15026   if (TARGET_BACKTRACE)
15027     {
15028       unsigned offset;
15029       unsigned work_register;
15030
15031       /* We have been asked to create a stack backtrace structure.
15032          The code looks like this:
15033
15034          0   .align 2
15035          0   func:
15036          0     sub   SP, #16         Reserve space for 4 registers.
15037          2     push  {R7}            Push low registers.
15038          4     add   R7, SP, #20     Get the stack pointer before the push.
15039          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
15040          8     mov   R7, PC          Get hold of the start of this code plus 12.
15041         10     str   R7, [SP, #16]   Store it.
15042         12     mov   R7, FP          Get hold of the current frame pointer.
15043         14     str   R7, [SP, #4]    Store it.
15044         16     mov   R7, LR          Get hold of the current return address.
15045         18     str   R7, [SP, #12]   Store it.
15046         20     add   R7, SP, #16     Point at the start of the backtrace structure.
15047         22     mov   FP, R7          Put this value into the frame pointer.  */
15048
15049       work_register = thumb_find_work_register (live_regs_mask);
15050
15051       if (ARM_EABI_UNWIND_TABLES)
15052         asm_fprintf (f, "\t.pad #16\n");
15053
15054       asm_fprintf
15055         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
15056          SP_REGNUM, SP_REGNUM);
15057
15058       if (dwarf2out_do_frame ())
15059         {
15060           char *l = dwarf2out_cfi_label ();
15061
15062           cfa_offset = cfa_offset + 16;
15063           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
15064         }
15065
15066       if (l_mask)
15067         {
15068           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
15069           offset = bit_count (l_mask) * UNITS_PER_WORD;
15070         }
15071       else
15072         offset = 0;
15073
15074       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
15075                    offset + 16 + current_function_pretend_args_size);
15076
15077       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
15078                    offset + 4);
15079
15080       /* Make sure that the instruction fetching the PC is in the right place
15081          to calculate "start of backtrace creation code + 12".  */
15082       if (l_mask)
15083         {
15084           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
15085           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
15086                        offset + 12);
15087           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
15088                        ARM_HARD_FRAME_POINTER_REGNUM);
15089           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
15090                        offset);
15091         }
15092       else
15093         {
15094           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
15095                        ARM_HARD_FRAME_POINTER_REGNUM);
15096           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
15097                        offset);
15098           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
15099           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
15100                        offset + 12);
15101         }
15102
15103       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
15104       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
15105                    offset + 8);
15106       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
15107                    offset + 12);
15108       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
15109                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
15110     }
15111   /* Optimization:  If we are not pushing any low registers but we are going
15112      to push some high registers then delay our first push.  This will just
15113      be a push of LR and we can combine it with the push of the first high
15114      register.  */
15115   else if ((l_mask & 0xff) != 0
15116            || (high_regs_pushed == 0 && l_mask))
15117     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
15118
15119   if (high_regs_pushed)
15120     {
15121       unsigned pushable_regs;
15122       unsigned next_hi_reg;
15123
15124       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
15125         if (live_regs_mask & (1 << next_hi_reg))
15126           break;
15127
15128       pushable_regs = l_mask & 0xff;
15129
15130       if (pushable_regs == 0)
15131         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
15132
15133       while (high_regs_pushed > 0)
15134         {
15135           unsigned long real_regs_mask = 0;
15136
15137           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
15138             {
15139               if (pushable_regs & (1 << regno))
15140                 {
15141                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
15142
15143                   high_regs_pushed --;
15144                   real_regs_mask |= (1 << next_hi_reg);
15145
15146                   if (high_regs_pushed)
15147                     {
15148                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
15149                            next_hi_reg --)
15150                         if (live_regs_mask & (1 << next_hi_reg))
15151                           break;
15152                     }
15153                   else
15154                     {
15155                       pushable_regs &= ~((1 << regno) - 1);
15156                       break;
15157                     }
15158                 }
15159             }
15160
15161           /* If we had to find a work register and we have not yet
15162              saved the LR then add it to the list of regs to push.  */
15163           if (l_mask == (1 << LR_REGNUM))
15164             {
15165               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
15166                              1, &cfa_offset,
15167                              real_regs_mask | (1 << LR_REGNUM));
15168               l_mask = 0;
15169             }
15170           else
15171             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
15172         }
15173     }
15174 }
15175
15176 /* Handle the case of a double word load into a low register from
15177    a computed memory address.  The computed address may involve a
15178    register which is overwritten by the load.  */
15179 const char *
15180 thumb_load_double_from_address (rtx *operands)
15181 {
15182   rtx addr;
15183   rtx base;
15184   rtx offset;
15185   rtx arg1;
15186   rtx arg2;
15187
15188   gcc_assert (GET_CODE (operands[0]) == REG);
15189   gcc_assert (GET_CODE (operands[1]) == MEM);
15190
15191   /* Get the memory address.  */
15192   addr = XEXP (operands[1], 0);
15193
15194   /* Work out how the memory address is computed.  */
15195   switch (GET_CODE (addr))
15196     {
15197     case REG:
15198       operands[2] = adjust_address (operands[1], SImode, 4);
15199
15200       if (REGNO (operands[0]) == REGNO (addr))
15201         {
15202           output_asm_insn ("ldr\t%H0, %2", operands);
15203           output_asm_insn ("ldr\t%0, %1", operands);
15204         }
15205       else
15206         {
15207           output_asm_insn ("ldr\t%0, %1", operands);
15208           output_asm_insn ("ldr\t%H0, %2", operands);
15209         }
15210       break;
15211
15212     case CONST:
15213       /* Compute <address> + 4 for the high order load.  */
15214       operands[2] = adjust_address (operands[1], SImode, 4);
15215
15216       output_asm_insn ("ldr\t%0, %1", operands);
15217       output_asm_insn ("ldr\t%H0, %2", operands);
15218       break;
15219
15220     case PLUS:
15221       arg1   = XEXP (addr, 0);
15222       arg2   = XEXP (addr, 1);
15223
15224       if (CONSTANT_P (arg1))
15225         base = arg2, offset = arg1;
15226       else
15227         base = arg1, offset = arg2;
15228
15229       gcc_assert (GET_CODE (base) == REG);
15230
15231       /* Catch the case of <address> = <reg> + <reg> */
15232       if (GET_CODE (offset) == REG)
15233         {
15234           int reg_offset = REGNO (offset);
15235           int reg_base   = REGNO (base);
15236           int reg_dest   = REGNO (operands[0]);
15237
15238           /* Add the base and offset registers together into the
15239              higher destination register.  */
15240           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
15241                        reg_dest + 1, reg_base, reg_offset);
15242
15243           /* Load the lower destination register from the address in
15244              the higher destination register.  */
15245           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
15246                        reg_dest, reg_dest + 1);
15247
15248           /* Load the higher destination register from its own address
15249              plus 4.  */
15250           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
15251                        reg_dest + 1, reg_dest + 1);
15252         }
15253       else
15254         {
15255           /* Compute <address> + 4 for the high order load.  */
15256           operands[2] = adjust_address (operands[1], SImode, 4);
15257
15258           /* If the computed address is held in the low order register
15259              then load the high order register first, otherwise always
15260              load the low order register first.  */
15261           if (REGNO (operands[0]) == REGNO (base))
15262             {
15263               output_asm_insn ("ldr\t%H0, %2", operands);
15264               output_asm_insn ("ldr\t%0, %1", operands);
15265             }
15266           else
15267             {
15268               output_asm_insn ("ldr\t%0, %1", operands);
15269               output_asm_insn ("ldr\t%H0, %2", operands);
15270             }
15271         }
15272       break;
15273
15274     case LABEL_REF:
15275       /* With no registers to worry about we can just load the value
15276          directly.  */
15277       operands[2] = adjust_address (operands[1], SImode, 4);
15278
15279       output_asm_insn ("ldr\t%H0, %2", operands);
15280       output_asm_insn ("ldr\t%0, %1", operands);
15281       break;
15282
15283     default:
15284       gcc_unreachable ();
15285     }
15286
15287   return "";
15288 }
15289
15290 const char *
15291 thumb_output_move_mem_multiple (int n, rtx *operands)
15292 {
15293   rtx tmp;
15294
15295   switch (n)
15296     {
15297     case 2:
15298       if (REGNO (operands[4]) > REGNO (operands[5]))
15299         {
15300           tmp = operands[4];
15301           operands[4] = operands[5];
15302           operands[5] = tmp;
15303         }
15304       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
15305       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
15306       break;
15307
15308     case 3:
15309       if (REGNO (operands[4]) > REGNO (operands[5]))
15310         {
15311           tmp = operands[4];
15312           operands[4] = operands[5];
15313           operands[5] = tmp;
15314         }
15315       if (REGNO (operands[5]) > REGNO (operands[6]))
15316         {
15317           tmp = operands[5];
15318           operands[5] = operands[6];
15319           operands[6] = tmp;
15320         }
15321       if (REGNO (operands[4]) > REGNO (operands[5]))
15322         {
15323           tmp = operands[4];
15324           operands[4] = operands[5];
15325           operands[5] = tmp;
15326         }
15327
15328       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
15329       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
15330       break;
15331
15332     default:
15333       gcc_unreachable ();
15334     }
15335
15336   return "";
15337 }
15338
15339 /* Output a call-via instruction for thumb state.  */
15340 const char *
15341 thumb_call_via_reg (rtx reg)
15342 {
15343   int regno = REGNO (reg);
15344   rtx *labelp;
15345
15346   gcc_assert (regno < LR_REGNUM);
15347
15348   /* If we are in the normal text section we can use a single instance
15349      per compilation unit.  If we are doing function sections, then we need
15350      an entry per section, since we can't rely on reachability.  */
15351   if (in_section == text_section)
15352     {
15353       thumb_call_reg_needed = 1;
15354
15355       if (thumb_call_via_label[regno] == NULL)
15356         thumb_call_via_label[regno] = gen_label_rtx ();
15357       labelp = thumb_call_via_label + regno;
15358     }
15359   else
15360     {
15361       if (cfun->machine->call_via[regno] == NULL)
15362         cfun->machine->call_via[regno] = gen_label_rtx ();
15363       labelp = cfun->machine->call_via + regno;
15364     }
15365
15366   output_asm_insn ("bl\t%a0", labelp);
15367   return "";
15368 }
15369
15370 /* Routines for generating rtl.  */
15371 void
15372 thumb_expand_movmemqi (rtx *operands)
15373 {
15374   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
15375   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
15376   HOST_WIDE_INT len = INTVAL (operands[2]);
15377   HOST_WIDE_INT offset = 0;
15378
15379   while (len >= 12)
15380     {
15381       emit_insn (gen_movmem12b (out, in, out, in));
15382       len -= 12;
15383     }
15384
15385   if (len >= 8)
15386     {
15387       emit_insn (gen_movmem8b (out, in, out, in));
15388       len -= 8;
15389     }
15390
15391   if (len >= 4)
15392     {
15393       rtx reg = gen_reg_rtx (SImode);
15394       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
15395       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
15396       len -= 4;
15397       offset += 4;
15398     }
15399
15400   if (len >= 2)
15401     {
15402       rtx reg = gen_reg_rtx (HImode);
15403       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
15404                                               plus_constant (in, offset))));
15405       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
15406                             reg));
15407       len -= 2;
15408       offset += 2;
15409     }
15410
15411   if (len)
15412     {
15413       rtx reg = gen_reg_rtx (QImode);
15414       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
15415                                               plus_constant (in, offset))));
15416       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
15417                             reg));
15418     }
15419 }
15420
15421 void
15422 thumb_reload_out_hi (rtx *operands)
15423 {
15424   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
15425 }
15426
15427 /* Handle reading a half-word from memory during reload.  */
15428 void
15429 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
15430 {
15431   gcc_unreachable ();
15432 }
15433
15434 /* Return the length of a function name prefix
15435     that starts with the character 'c'.  */
15436 static int
15437 arm_get_strip_length (int c)
15438 {
15439   switch (c)
15440     {
15441     ARM_NAME_ENCODING_LENGTHS
15442       default: return 0;
15443     }
15444 }
15445
15446 /* Return a pointer to a function's name with any
15447    and all prefix encodings stripped from it.  */
15448 const char *
15449 arm_strip_name_encoding (const char *name)
15450 {
15451   int skip;
15452
15453   while ((skip = arm_get_strip_length (* name)))
15454     name += skip;
15455
15456   return name;
15457 }
15458
15459 /* If there is a '*' anywhere in the name's prefix, then
15460    emit the stripped name verbatim, otherwise prepend an
15461    underscore if leading underscores are being used.  */
15462 void
15463 arm_asm_output_labelref (FILE *stream, const char *name)
15464 {
15465   int skip;
15466   int verbatim = 0;
15467
15468   while ((skip = arm_get_strip_length (* name)))
15469     {
15470       verbatim |= (*name == '*');
15471       name += skip;
15472     }
15473
15474   if (verbatim)
15475     fputs (name, stream);
15476   else
15477     asm_fprintf (stream, "%U%s", name);
15478 }
15479
15480 static void
15481 arm_file_start (void)
15482 {
15483   int val;
15484
15485   if (TARGET_UNIFIED_ASM)
15486     asm_fprintf (asm_out_file, "\t.syntax unified\n");
15487
15488   if (TARGET_BPABI)
15489     {
15490       const char *fpu_name;
15491       if (arm_select[0].string)
15492         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
15493       else if (arm_select[1].string)
15494         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
15495       else
15496         asm_fprintf (asm_out_file, "\t.cpu %s\n",
15497                      all_cores[arm_default_cpu].name);
15498
15499       if (TARGET_SOFT_FLOAT)
15500         {
15501           if (TARGET_VFP)
15502             fpu_name = "softvfp";
15503           else
15504             fpu_name = "softfpa";
15505         }
15506       else
15507         {
15508           int set_float_abi_attributes = 0;
15509           switch (arm_fpu_arch)
15510             {
15511             case FPUTYPE_FPA:
15512               fpu_name = "fpa";
15513               break;
15514             case FPUTYPE_FPA_EMU2:
15515               fpu_name = "fpe2";
15516               break;
15517             case FPUTYPE_FPA_EMU3:
15518               fpu_name = "fpe3";
15519               break;
15520             case FPUTYPE_MAVERICK:
15521               fpu_name = "maverick";
15522               break;
15523             case FPUTYPE_VFP:
15524               fpu_name = "vfp";
15525               set_float_abi_attributes = 1;
15526               break;
15527             case FPUTYPE_VFP3:
15528               fpu_name = "vfp3";
15529               set_float_abi_attributes = 1;
15530               break;
15531             default:
15532               abort();
15533             }
15534           if (set_float_abi_attributes)
15535             {
15536               if (TARGET_HARD_FLOAT)
15537                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
15538               if (TARGET_HARD_FLOAT_ABI)
15539                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
15540             }
15541         }
15542       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
15543
15544       /* Some of these attributes only apply when the corresponding features
15545          are used.  However we don't have any easy way of figuring this out.
15546          Conservatively record the setting that would have been used.  */
15547
15548       /* Tag_ABI_PCS_wchar_t.  */
15549       asm_fprintf (asm_out_file, "\t.eabi_attribute 18, %d\n",
15550                    (int)WCHAR_TYPE_SIZE / BITS_PER_UNIT);
15551
15552       /* Tag_ABI_FP_rounding.  */
15553       if (flag_rounding_math)
15554         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
15555       if (!flag_unsafe_math_optimizations)
15556         {
15557           /* Tag_ABI_FP_denomal.  */
15558           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
15559           /* Tag_ABI_FP_exceptions.  */
15560           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
15561         }
15562       /* Tag_ABI_FP_user_exceptions.  */
15563       if (flag_signaling_nans)
15564         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
15565       /* Tag_ABI_FP_number_model.  */
15566       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
15567                    flag_finite_math_only ? 1 : 3);
15568
15569       /* Tag_ABI_align8_needed.  */
15570       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
15571       /* Tag_ABI_align8_preserved.  */
15572       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
15573       /* Tag_ABI_enum_size.  */
15574       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
15575                    flag_short_enums ? 1 : 2);
15576
15577       /* Tag_ABI_optimization_goals.  */
15578       if (optimize_size)
15579         val = 4;
15580       else if (optimize >= 2)
15581         val = 2;
15582       else if (optimize)
15583         val = 1;
15584       else
15585         val = 6;
15586       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
15587     }
15588   default_file_start();
15589 }
15590
15591 static void
15592 arm_file_end (void)
15593 {
15594   int regno;
15595
15596   if (NEED_INDICATE_EXEC_STACK)
15597     /* Add .note.GNU-stack.  */
15598     file_end_indicate_exec_stack ();
15599
15600   if (! thumb_call_reg_needed)
15601     return;
15602
15603   switch_to_section (text_section);
15604   asm_fprintf (asm_out_file, "\t.code 16\n");
15605   ASM_OUTPUT_ALIGN (asm_out_file, 1);
15606
15607   for (regno = 0; regno < LR_REGNUM; regno++)
15608     {
15609       rtx label = thumb_call_via_label[regno];
15610
15611       if (label != 0)
15612         {
15613           targetm.asm_out.internal_label (asm_out_file, "L",
15614                                           CODE_LABEL_NUMBER (label));
15615           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
15616         }
15617     }
15618 }
15619
15620 rtx aof_pic_label;
15621
15622 #ifdef AOF_ASSEMBLER
15623 /* Special functions only needed when producing AOF syntax assembler.  */
15624
15625 struct pic_chain
15626 {
15627   struct pic_chain * next;
15628   const char * symname;
15629 };
15630
15631 static struct pic_chain * aof_pic_chain = NULL;
15632
15633 rtx
15634 aof_pic_entry (rtx x)
15635 {
15636   struct pic_chain ** chainp;
15637   int offset;
15638
15639   if (aof_pic_label == NULL_RTX)
15640     {
15641       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
15642     }
15643
15644   for (offset = 0, chainp = &aof_pic_chain; *chainp;
15645        offset += 4, chainp = &(*chainp)->next)
15646     if ((*chainp)->symname == XSTR (x, 0))
15647       return plus_constant (aof_pic_label, offset);
15648
15649   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
15650   (*chainp)->next = NULL;
15651   (*chainp)->symname = XSTR (x, 0);
15652   return plus_constant (aof_pic_label, offset);
15653 }
15654
15655 void
15656 aof_dump_pic_table (FILE *f)
15657 {
15658   struct pic_chain * chain;
15659
15660   if (aof_pic_chain == NULL)
15661     return;
15662
15663   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
15664                PIC_OFFSET_TABLE_REGNUM,
15665                PIC_OFFSET_TABLE_REGNUM);
15666   fputs ("|x$adcons|\n", f);
15667
15668   for (chain = aof_pic_chain; chain; chain = chain->next)
15669     {
15670       fputs ("\tDCD\t", f);
15671       assemble_name (f, chain->symname);
15672       fputs ("\n", f);
15673     }
15674 }
15675
15676 int arm_text_section_count = 1;
15677
15678 /* A get_unnamed_section callback for switching to the text section.  */
15679
15680 static void
15681 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
15682 {
15683   fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
15684            arm_text_section_count++);
15685   if (flag_pic)
15686     fprintf (asm_out_file, ", PIC, REENTRANT");
15687   fprintf (asm_out_file, "\n");
15688 }
15689
15690 static int arm_data_section_count = 1;
15691
15692 /* A get_unnamed_section callback for switching to the data section.  */
15693
15694 static void
15695 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
15696 {
15697   fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
15698            arm_data_section_count++);
15699 }
15700
15701 /* Implement TARGET_ASM_INIT_SECTIONS.
15702
15703    AOF Assembler syntax is a nightmare when it comes to areas, since once
15704    we change from one area to another, we can't go back again.  Instead,
15705    we must create a new area with the same attributes and add the new output
15706    to that.  Unfortunately, there is nothing we can do here to guarantee that
15707    two areas with the same attributes will be linked adjacently in the
15708    resulting executable, so we have to be careful not to do pc-relative
15709    addressing across such boundaries.  */
15710
15711 static void
15712 aof_asm_init_sections (void)
15713 {
15714   text_section = get_unnamed_section (SECTION_CODE,
15715                                       aof_output_text_section_asm_op, NULL);
15716   data_section = get_unnamed_section (SECTION_WRITE,
15717                                       aof_output_data_section_asm_op, NULL);
15718   readonly_data_section = text_section;
15719 }
15720
15721 void
15722 zero_init_section (void)
15723 {
15724   static int zero_init_count = 1;
15725
15726   fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
15727   in_section = NULL;
15728 }
15729
15730 /* The AOF assembler is religiously strict about declarations of
15731    imported and exported symbols, so that it is impossible to declare
15732    a function as imported near the beginning of the file, and then to
15733    export it later on.  It is, however, possible to delay the decision
15734    until all the functions in the file have been compiled.  To get
15735    around this, we maintain a list of the imports and exports, and
15736    delete from it any that are subsequently defined.  At the end of
15737    compilation we spit the remainder of the list out before the END
15738    directive.  */
15739
15740 struct import
15741 {
15742   struct import * next;
15743   const char * name;
15744 };
15745
15746 static struct import * imports_list = NULL;
15747
15748 void
15749 aof_add_import (const char *name)
15750 {
15751   struct import * new;
15752
15753   for (new = imports_list; new; new = new->next)
15754     if (new->name == name)
15755       return;
15756
15757   new = (struct import *) xmalloc (sizeof (struct import));
15758   new->next = imports_list;
15759   imports_list = new;
15760   new->name = name;
15761 }
15762
15763 void
15764 aof_delete_import (const char *name)
15765 {
15766   struct import ** old;
15767
15768   for (old = &imports_list; *old; old = & (*old)->next)
15769     {
15770       if ((*old)->name == name)
15771         {
15772           *old = (*old)->next;
15773           return;
15774         }
15775     }
15776 }
15777
15778 int arm_main_function = 0;
15779
15780 static void
15781 aof_dump_imports (FILE *f)
15782 {
15783   /* The AOF assembler needs this to cause the startup code to be extracted
15784      from the library.  Brining in __main causes the whole thing to work
15785      automagically.  */
15786   if (arm_main_function)
15787     {
15788       switch_to_section (text_section);
15789       fputs ("\tIMPORT __main\n", f);
15790       fputs ("\tDCD __main\n", f);
15791     }
15792
15793   /* Now dump the remaining imports.  */
15794   while (imports_list)
15795     {
15796       fprintf (f, "\tIMPORT\t");
15797       assemble_name (f, imports_list->name);
15798       fputc ('\n', f);
15799       imports_list = imports_list->next;
15800     }
15801 }
15802
15803 static void
15804 aof_globalize_label (FILE *stream, const char *name)
15805 {
15806   default_globalize_label (stream, name);
15807   if (! strcmp (name, "main"))
15808     arm_main_function = 1;
15809 }
15810
15811 static void
15812 aof_file_start (void)
15813 {
15814   fputs ("__r0\tRN\t0\n", asm_out_file);
15815   fputs ("__a1\tRN\t0\n", asm_out_file);
15816   fputs ("__a2\tRN\t1\n", asm_out_file);
15817   fputs ("__a3\tRN\t2\n", asm_out_file);
15818   fputs ("__a4\tRN\t3\n", asm_out_file);
15819   fputs ("__v1\tRN\t4\n", asm_out_file);
15820   fputs ("__v2\tRN\t5\n", asm_out_file);
15821   fputs ("__v3\tRN\t6\n", asm_out_file);
15822   fputs ("__v4\tRN\t7\n", asm_out_file);
15823   fputs ("__v5\tRN\t8\n", asm_out_file);
15824   fputs ("__v6\tRN\t9\n", asm_out_file);
15825   fputs ("__sl\tRN\t10\n", asm_out_file);
15826   fputs ("__fp\tRN\t11\n", asm_out_file);
15827   fputs ("__ip\tRN\t12\n", asm_out_file);
15828   fputs ("__sp\tRN\t13\n", asm_out_file);
15829   fputs ("__lr\tRN\t14\n", asm_out_file);
15830   fputs ("__pc\tRN\t15\n", asm_out_file);
15831   fputs ("__f0\tFN\t0\n", asm_out_file);
15832   fputs ("__f1\tFN\t1\n", asm_out_file);
15833   fputs ("__f2\tFN\t2\n", asm_out_file);
15834   fputs ("__f3\tFN\t3\n", asm_out_file);
15835   fputs ("__f4\tFN\t4\n", asm_out_file);
15836   fputs ("__f5\tFN\t5\n", asm_out_file);
15837   fputs ("__f6\tFN\t6\n", asm_out_file);
15838   fputs ("__f7\tFN\t7\n", asm_out_file);
15839   switch_to_section (text_section);
15840 }
15841
15842 static void
15843 aof_file_end (void)
15844 {
15845   if (flag_pic)
15846     aof_dump_pic_table (asm_out_file);
15847   arm_file_end ();
15848   aof_dump_imports (asm_out_file);
15849   fputs ("\tEND\n", asm_out_file);
15850 }
15851 #endif /* AOF_ASSEMBLER */
15852
15853 #ifndef ARM_PE
15854 /* Symbols in the text segment can be accessed without indirecting via the
15855    constant pool; it may take an extra binary operation, but this is still
15856    faster than indirecting via memory.  Don't do this when not optimizing,
15857    since we won't be calculating al of the offsets necessary to do this
15858    simplification.  */
15859
15860 static void
15861 arm_encode_section_info (tree decl, rtx rtl, int first)
15862 {
15863   /* This doesn't work with AOF syntax, since the string table may be in
15864      a different AREA.  */
15865 #ifndef AOF_ASSEMBLER
15866   if (optimize > 0 && TREE_CONSTANT (decl))
15867     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
15868 #endif
15869
15870   default_encode_section_info (decl, rtl, first);
15871 }
15872 #endif /* !ARM_PE */
15873
15874 static void
15875 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
15876 {
15877   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
15878       && !strcmp (prefix, "L"))
15879     {
15880       arm_ccfsm_state = 0;
15881       arm_target_insn = NULL;
15882     }
15883   default_internal_label (stream, prefix, labelno);
15884 }
15885
15886 /* Output code to add DELTA to the first argument, and then jump
15887    to FUNCTION.  Used for C++ multiple inheritance.  */
15888 static void
15889 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
15890                      HOST_WIDE_INT delta,
15891                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
15892                      tree function)
15893 {
15894   static int thunk_label = 0;
15895   char label[256];
15896   char labelpc[256];
15897   int mi_delta = delta;
15898   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
15899   int shift = 0;
15900   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
15901                     ? 1 : 0);
15902   if (mi_delta < 0)
15903     mi_delta = - mi_delta;
15904   /* When generating 16-bit thumb code, thunks are entered in arm mode.  */
15905   if (TARGET_THUMB1)
15906     {
15907       int labelno = thunk_label++;
15908       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
15909       fputs ("\tldr\tr12, ", file);
15910       assemble_name (file, label);
15911       fputc ('\n', file);
15912       if (flag_pic)
15913         {
15914           /* If we are generating PIC, the ldr instruction below loads
15915              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
15916              the address of the add + 8, so we have:
15917
15918              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
15919                  = target + 1.
15920
15921              Note that we have "+ 1" because some versions of GNU ld
15922              don't set the low bit of the result for R_ARM_REL32
15923              relocations against thumb function symbols.  */
15924           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
15925           assemble_name (file, labelpc);
15926           fputs (":\n", file);
15927           fputs ("\tadd\tr12, pc, r12\n", file);
15928         }
15929     }
15930   /* TODO: Use movw/movt for large constants when available.  */
15931   while (mi_delta != 0)
15932     {
15933       if ((mi_delta & (3 << shift)) == 0)
15934         shift += 2;
15935       else
15936         {
15937           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
15938                        mi_op, this_regno, this_regno,
15939                        mi_delta & (0xff << shift));
15940           mi_delta &= ~(0xff << shift);
15941           shift += 8;
15942         }
15943     }
15944   if (TARGET_THUMB1)
15945     {
15946       fprintf (file, "\tbx\tr12\n");
15947       ASM_OUTPUT_ALIGN (file, 2);
15948       assemble_name (file, label);
15949       fputs (":\n", file);
15950       if (flag_pic)
15951         {
15952           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
15953           rtx tem = XEXP (DECL_RTL (function), 0);
15954           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
15955           tem = gen_rtx_MINUS (GET_MODE (tem),
15956                                tem,
15957                                gen_rtx_SYMBOL_REF (Pmode,
15958                                                    ggc_strdup (labelpc)));
15959           assemble_integer (tem, 4, BITS_PER_WORD, 1);
15960         }
15961       else
15962         /* Output ".word .LTHUNKn".  */
15963         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
15964     }
15965   else
15966     {
15967       fputs ("\tb\t", file);
15968       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
15969       if (NEED_PLT_RELOC)
15970         fputs ("(PLT)", file);
15971       fputc ('\n', file);
15972     }
15973 }
15974
15975 int
15976 arm_emit_vector_const (FILE *file, rtx x)
15977 {
15978   int i;
15979   const char * pattern;
15980
15981   gcc_assert (GET_CODE (x) == CONST_VECTOR);
15982
15983   switch (GET_MODE (x))
15984     {
15985     case V2SImode: pattern = "%08x"; break;
15986     case V4HImode: pattern = "%04x"; break;
15987     case V8QImode: pattern = "%02x"; break;
15988     default:       gcc_unreachable ();
15989     }
15990
15991   fprintf (file, "0x");
15992   for (i = CONST_VECTOR_NUNITS (x); i--;)
15993     {
15994       rtx element;
15995
15996       element = CONST_VECTOR_ELT (x, i);
15997       fprintf (file, pattern, INTVAL (element));
15998     }
15999
16000   return 1;
16001 }
16002
16003 const char *
16004 arm_output_load_gr (rtx *operands)
16005 {
16006   rtx reg;
16007   rtx offset;
16008   rtx wcgr;
16009   rtx sum;
16010
16011   if (GET_CODE (operands [1]) != MEM
16012       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
16013       || GET_CODE (reg = XEXP (sum, 0)) != REG
16014       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
16015       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
16016     return "wldrw%?\t%0, %1";
16017
16018   /* Fix up an out-of-range load of a GR register.  */
16019   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
16020   wcgr = operands[0];
16021   operands[0] = reg;
16022   output_asm_insn ("ldr%?\t%0, %1", operands);
16023
16024   operands[0] = wcgr;
16025   operands[1] = reg;
16026   output_asm_insn ("tmcr%?\t%0, %1", operands);
16027   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
16028
16029   return "";
16030 }
16031
16032 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
16033
16034    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
16035    named arg and all anonymous args onto the stack.
16036    XXX I know the prologue shouldn't be pushing registers, but it is faster
16037    that way.  */
16038
16039 static void
16040 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
16041                             enum machine_mode mode ATTRIBUTE_UNUSED,
16042                             tree type ATTRIBUTE_UNUSED,
16043                             int *pretend_size,
16044                             int second_time ATTRIBUTE_UNUSED)
16045 {
16046   cfun->machine->uses_anonymous_args = 1;
16047   if (cum->nregs < NUM_ARG_REGS)
16048     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
16049 }
16050
16051 /* Return nonzero if the CONSUMER instruction (a store) does not need
16052    PRODUCER's value to calculate the address.  */
16053
16054 int
16055 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
16056 {
16057   rtx value = PATTERN (producer);
16058   rtx addr = PATTERN (consumer);
16059
16060   if (GET_CODE (value) == COND_EXEC)
16061     value = COND_EXEC_CODE (value);
16062   if (GET_CODE (value) == PARALLEL)
16063     value = XVECEXP (value, 0, 0);
16064   value = XEXP (value, 0);
16065   if (GET_CODE (addr) == COND_EXEC)
16066     addr = COND_EXEC_CODE (addr);
16067   if (GET_CODE (addr) == PARALLEL)
16068     addr = XVECEXP (addr, 0, 0);
16069   addr = XEXP (addr, 0);
16070
16071   return !reg_overlap_mentioned_p (value, addr);
16072 }
16073
16074 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
16075    have an early register shift value or amount dependency on the
16076    result of PRODUCER.  */
16077
16078 int
16079 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
16080 {
16081   rtx value = PATTERN (producer);
16082   rtx op = PATTERN (consumer);
16083   rtx early_op;
16084
16085   if (GET_CODE (value) == COND_EXEC)
16086     value = COND_EXEC_CODE (value);
16087   if (GET_CODE (value) == PARALLEL)
16088     value = XVECEXP (value, 0, 0);
16089   value = XEXP (value, 0);
16090   if (GET_CODE (op) == COND_EXEC)
16091     op = COND_EXEC_CODE (op);
16092   if (GET_CODE (op) == PARALLEL)
16093     op = XVECEXP (op, 0, 0);
16094   op = XEXP (op, 1);
16095
16096   early_op = XEXP (op, 0);
16097   /* This is either an actual independent shift, or a shift applied to
16098      the first operand of another operation.  We want the whole shift
16099      operation.  */
16100   if (GET_CODE (early_op) == REG)
16101     early_op = op;
16102
16103   return !reg_overlap_mentioned_p (value, early_op);
16104 }
16105
16106 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
16107    have an early register shift value dependency on the result of
16108    PRODUCER.  */
16109
16110 int
16111 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
16112 {
16113   rtx value = PATTERN (producer);
16114   rtx op = PATTERN (consumer);
16115   rtx early_op;
16116
16117   if (GET_CODE (value) == COND_EXEC)
16118     value = COND_EXEC_CODE (value);
16119   if (GET_CODE (value) == PARALLEL)
16120     value = XVECEXP (value, 0, 0);
16121   value = XEXP (value, 0);
16122   if (GET_CODE (op) == COND_EXEC)
16123     op = COND_EXEC_CODE (op);
16124   if (GET_CODE (op) == PARALLEL)
16125     op = XVECEXP (op, 0, 0);
16126   op = XEXP (op, 1);
16127
16128   early_op = XEXP (op, 0);
16129
16130   /* This is either an actual independent shift, or a shift applied to
16131      the first operand of another operation.  We want the value being
16132      shifted, in either case.  */
16133   if (GET_CODE (early_op) != REG)
16134     early_op = XEXP (early_op, 0);
16135
16136   return !reg_overlap_mentioned_p (value, early_op);
16137 }
16138
16139 /* Return nonzero if the CONSUMER (a mul or mac op) does not
16140    have an early register mult dependency on the result of
16141    PRODUCER.  */
16142
16143 int
16144 arm_no_early_mul_dep (rtx producer, rtx consumer)
16145 {
16146   rtx value = PATTERN (producer);
16147   rtx op = PATTERN (consumer);
16148
16149   if (GET_CODE (value) == COND_EXEC)
16150     value = COND_EXEC_CODE (value);
16151   if (GET_CODE (value) == PARALLEL)
16152     value = XVECEXP (value, 0, 0);
16153   value = XEXP (value, 0);
16154   if (GET_CODE (op) == COND_EXEC)
16155     op = COND_EXEC_CODE (op);
16156   if (GET_CODE (op) == PARALLEL)
16157     op = XVECEXP (op, 0, 0);
16158   op = XEXP (op, 1);
16159
16160   return (GET_CODE (op) == PLUS
16161           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
16162 }
16163
16164
16165 /* We can't rely on the caller doing the proper promotion when
16166    using APCS or ATPCS.  */
16167
16168 static bool
16169 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
16170 {
16171     return !TARGET_AAPCS_BASED;
16172 }
16173
16174
16175 /* AAPCS based ABIs use short enums by default.  */
16176
16177 static bool
16178 arm_default_short_enums (void)
16179 {
16180   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
16181 }
16182
16183
16184 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
16185
16186 static bool
16187 arm_align_anon_bitfield (void)
16188 {
16189   return TARGET_AAPCS_BASED;
16190 }
16191
16192
16193 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
16194
16195 static tree
16196 arm_cxx_guard_type (void)
16197 {
16198   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
16199 }
16200
16201
16202 /* The EABI says test the least significant bit of a guard variable.  */
16203
16204 static bool
16205 arm_cxx_guard_mask_bit (void)
16206 {
16207   return TARGET_AAPCS_BASED;
16208 }
16209
16210
16211 /* The EABI specifies that all array cookies are 8 bytes long.  */
16212
16213 static tree
16214 arm_get_cookie_size (tree type)
16215 {
16216   tree size;
16217
16218   if (!TARGET_AAPCS_BASED)
16219     return default_cxx_get_cookie_size (type);
16220
16221   size = build_int_cst (sizetype, 8);
16222   return size;
16223 }
16224
16225
16226 /* The EABI says that array cookies should also contain the element size.  */
16227
16228 static bool
16229 arm_cookie_has_size (void)
16230 {
16231   return TARGET_AAPCS_BASED;
16232 }
16233
16234
16235 /* The EABI says constructors and destructors should return a pointer to
16236    the object constructed/destroyed.  */
16237
16238 static bool
16239 arm_cxx_cdtor_returns_this (void)
16240 {
16241   return TARGET_AAPCS_BASED;
16242 }
16243
16244 /* The EABI says that an inline function may never be the key
16245    method.  */
16246
16247 static bool
16248 arm_cxx_key_method_may_be_inline (void)
16249 {
16250   return !TARGET_AAPCS_BASED;
16251 }
16252
16253 static void
16254 arm_cxx_determine_class_data_visibility (tree decl)
16255 {
16256   if (!TARGET_AAPCS_BASED)
16257     return;
16258
16259   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
16260      is exported.  However, on systems without dynamic vague linkage,
16261      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
16262   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
16263     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
16264   else
16265     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
16266   DECL_VISIBILITY_SPECIFIED (decl) = 1;
16267 }
16268
16269 static bool
16270 arm_cxx_class_data_always_comdat (void)
16271 {
16272   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
16273      vague linkage if the class has no key function.  */
16274   return !TARGET_AAPCS_BASED;
16275 }
16276
16277
16278 /* The EABI says __aeabi_atexit should be used to register static
16279    destructors.  */
16280
16281 static bool
16282 arm_cxx_use_aeabi_atexit (void)
16283 {
16284   return TARGET_AAPCS_BASED;
16285 }
16286
16287
16288 void
16289 arm_set_return_address (rtx source, rtx scratch)
16290 {
16291   arm_stack_offsets *offsets;
16292   HOST_WIDE_INT delta;
16293   rtx addr;
16294   unsigned long saved_regs;
16295
16296   saved_regs = arm_compute_save_reg_mask ();
16297
16298   if ((saved_regs & (1 << LR_REGNUM)) == 0)
16299     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
16300   else
16301     {
16302       if (frame_pointer_needed)
16303         addr = plus_constant(hard_frame_pointer_rtx, -4);
16304       else
16305         {
16306           /* LR will be the first saved register.  */
16307           offsets = arm_get_frame_offsets ();
16308           delta = offsets->outgoing_args - (offsets->frame + 4);
16309
16310
16311           if (delta >= 4096)
16312             {
16313               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
16314                                      GEN_INT (delta & ~4095)));
16315               addr = scratch;
16316               delta &= 4095;
16317             }
16318           else
16319             addr = stack_pointer_rtx;
16320
16321           addr = plus_constant (addr, delta);
16322         }
16323       emit_move_insn (gen_frame_mem (Pmode, addr), source);
16324     }
16325 }
16326
16327
16328 void
16329 thumb_set_return_address (rtx source, rtx scratch)
16330 {
16331   arm_stack_offsets *offsets;
16332   HOST_WIDE_INT delta;
16333   HOST_WIDE_INT limit;
16334   int reg;
16335   rtx addr;
16336   unsigned long mask;
16337
16338   emit_insn (gen_rtx_USE (VOIDmode, source));
16339
16340   mask = thumb1_compute_save_reg_mask ();
16341   if (mask & (1 << LR_REGNUM))
16342     {
16343       offsets = arm_get_frame_offsets ();
16344
16345       limit = 1024;
16346       /* Find the saved regs.  */
16347       if (frame_pointer_needed)
16348         {
16349           delta = offsets->soft_frame - offsets->saved_args;
16350           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
16351           if (TARGET_THUMB1)
16352             limit = 128;
16353         }
16354       else
16355         {
16356           delta = offsets->outgoing_args - offsets->saved_args;
16357           reg = SP_REGNUM;
16358         }
16359       /* Allow for the stack frame.  */
16360       if (TARGET_THUMB1 && TARGET_BACKTRACE)
16361         delta -= 16;
16362       /* The link register is always the first saved register.  */
16363       delta -= 4;
16364
16365       /* Construct the address.  */
16366       addr = gen_rtx_REG (SImode, reg);
16367       if (delta > limit)
16368         {
16369           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
16370           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
16371           addr = scratch;
16372         }
16373       else
16374         addr = plus_constant (addr, delta);
16375
16376       emit_move_insn (gen_frame_mem (Pmode, addr), source);
16377     }
16378   else
16379     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
16380 }
16381
16382 /* Implements target hook vector_mode_supported_p.  */
16383 bool
16384 arm_vector_mode_supported_p (enum machine_mode mode)
16385 {
16386   if ((mode == V2SImode)
16387       || (mode == V4HImode)
16388       || (mode == V8QImode))
16389     return true;
16390
16391   return false;
16392 }
16393
16394 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
16395    ARM insns and therefore guarantee that the shift count is modulo 256.
16396    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
16397    guarantee no particular behavior for out-of-range counts.  */
16398
16399 static unsigned HOST_WIDE_INT
16400 arm_shift_truncation_mask (enum machine_mode mode)
16401 {
16402   return mode == SImode ? 255 : 0;
16403 }
16404
16405
16406 /* Map internal gcc register numbers to DWARF2 register numbers.  */
16407
16408 unsigned int
16409 arm_dbx_register_number (unsigned int regno)
16410 {
16411   if (regno < 16)
16412     return regno;
16413
16414   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
16415      compatibility.  The EABI defines them as registers 96-103.  */
16416   if (IS_FPA_REGNUM (regno))
16417     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
16418
16419   /* FIXME: VFPv3 register numbering.  */
16420   if (IS_VFP_REGNUM (regno))
16421     return 64 + regno - FIRST_VFP_REGNUM;
16422
16423   if (IS_IWMMXT_GR_REGNUM (regno))
16424     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
16425
16426   if (IS_IWMMXT_REGNUM (regno))
16427     return 112 + regno - FIRST_IWMMXT_REGNUM;
16428
16429   gcc_unreachable ();
16430 }
16431
16432
16433 #ifdef TARGET_UNWIND_INFO
16434 /* Emit unwind directives for a store-multiple instruction or stack pointer
16435    push during alignment.
16436    These should only ever be generated by the function prologue code, so
16437    expect them to have a particular form.  */
16438
16439 static void
16440 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
16441 {
16442   int i;
16443   HOST_WIDE_INT offset;
16444   HOST_WIDE_INT nregs;
16445   int reg_size;
16446   unsigned reg;
16447   unsigned lastreg;
16448   rtx e;
16449
16450   e = XVECEXP (p, 0, 0);
16451   if (GET_CODE (e) != SET)
16452     abort ();
16453
16454   /* First insn will adjust the stack pointer.  */
16455   if (GET_CODE (e) != SET
16456       || GET_CODE (XEXP (e, 0)) != REG
16457       || REGNO (XEXP (e, 0)) != SP_REGNUM
16458       || GET_CODE (XEXP (e, 1)) != PLUS)
16459     abort ();
16460
16461   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
16462   nregs = XVECLEN (p, 0) - 1;
16463
16464   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
16465   if (reg < 16)
16466     {
16467       /* The function prologue may also push pc, but not annotate it as it is
16468          never restored.  We turn this into a stack pointer adjustment.  */
16469       if (nregs * 4 == offset - 4)
16470         {
16471           fprintf (asm_out_file, "\t.pad #4\n");
16472           offset -= 4;
16473         }
16474       reg_size = 4;
16475       fprintf (asm_out_file, "\t.save {");
16476     }
16477   else if (IS_VFP_REGNUM (reg))
16478     {
16479       reg_size = 8;
16480       fprintf (asm_out_file, "\t.vsave {");
16481     }
16482   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
16483     {
16484       /* FPA registers are done differently.  */
16485       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
16486       return;
16487     }
16488   else
16489     /* Unknown register type.  */
16490     abort ();
16491
16492   /* If the stack increment doesn't match the size of the saved registers,
16493      something has gone horribly wrong.  */
16494   if (offset != nregs * reg_size)
16495     abort ();
16496
16497   offset = 0;
16498   lastreg = 0;
16499   /* The remaining insns will describe the stores.  */
16500   for (i = 1; i <= nregs; i++)
16501     {
16502       /* Expect (set (mem <addr>) (reg)).
16503          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
16504       e = XVECEXP (p, 0, i);
16505       if (GET_CODE (e) != SET
16506           || GET_CODE (XEXP (e, 0)) != MEM
16507           || GET_CODE (XEXP (e, 1)) != REG)
16508         abort ();
16509
16510       reg = REGNO (XEXP (e, 1));
16511       if (reg < lastreg)
16512         abort ();
16513
16514       if (i != 1)
16515         fprintf (asm_out_file, ", ");
16516       /* We can't use %r for vfp because we need to use the
16517          double precision register names.  */
16518       if (IS_VFP_REGNUM (reg))
16519         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
16520       else
16521         asm_fprintf (asm_out_file, "%r", reg);
16522
16523 #ifdef ENABLE_CHECKING
16524       /* Check that the addresses are consecutive.  */
16525       e = XEXP (XEXP (e, 0), 0);
16526       if (GET_CODE (e) == PLUS)
16527         {
16528           offset += reg_size;
16529           if (GET_CODE (XEXP (e, 0)) != REG
16530               || REGNO (XEXP (e, 0)) != SP_REGNUM
16531               || GET_CODE (XEXP (e, 1)) != CONST_INT
16532               || offset != INTVAL (XEXP (e, 1)))
16533             abort ();
16534         }
16535       else if (i != 1
16536                || GET_CODE (e) != REG
16537                || REGNO (e) != SP_REGNUM)
16538         abort ();
16539 #endif
16540     }
16541   fprintf (asm_out_file, "}\n");
16542 }
16543
16544 /*  Emit unwind directives for a SET.  */
16545
16546 static void
16547 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
16548 {
16549   rtx e0;
16550   rtx e1;
16551   unsigned reg;
16552
16553   e0 = XEXP (p, 0);
16554   e1 = XEXP (p, 1);
16555   switch (GET_CODE (e0))
16556     {
16557     case MEM:
16558       /* Pushing a single register.  */
16559       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
16560           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
16561           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
16562         abort ();
16563
16564       asm_fprintf (asm_out_file, "\t.save ");
16565       if (IS_VFP_REGNUM (REGNO (e1)))
16566         asm_fprintf(asm_out_file, "{d%d}\n",
16567                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
16568       else
16569         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
16570       break;
16571
16572     case REG:
16573       if (REGNO (e0) == SP_REGNUM)
16574         {
16575           /* A stack increment.  */
16576           if (GET_CODE (e1) != PLUS
16577               || GET_CODE (XEXP (e1, 0)) != REG
16578               || REGNO (XEXP (e1, 0)) != SP_REGNUM
16579               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
16580             abort ();
16581
16582           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
16583                        -INTVAL (XEXP (e1, 1)));
16584         }
16585       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
16586         {
16587           HOST_WIDE_INT offset;
16588
16589           if (GET_CODE (e1) == PLUS)
16590             {
16591               if (GET_CODE (XEXP (e1, 0)) != REG
16592                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
16593                 abort ();
16594               reg = REGNO (XEXP (e1, 0));
16595               offset = INTVAL (XEXP (e1, 1));
16596               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
16597                            HARD_FRAME_POINTER_REGNUM, reg,
16598                            INTVAL (XEXP (e1, 1)));
16599             }
16600           else if (GET_CODE (e1) == REG)
16601             {
16602               reg = REGNO (e1);
16603               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
16604                            HARD_FRAME_POINTER_REGNUM, reg);
16605             }
16606           else
16607             abort ();
16608         }
16609       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
16610         {
16611           /* Move from sp to reg.  */
16612           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
16613         }
16614      else if (GET_CODE (e1) == PLUS
16615               && GET_CODE (XEXP (e1, 0)) == REG
16616               && REGNO (XEXP (e1, 0)) == SP_REGNUM
16617               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
16618         {
16619           /* Set reg to offset from sp.  */
16620           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
16621                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
16622         }
16623       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
16624         {
16625           /* Stack pointer save before alignment.  */
16626           reg = REGNO (e0);
16627           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
16628                        reg + 0x90, reg);
16629         }
16630       else
16631         abort ();
16632       break;
16633
16634     default:
16635       abort ();
16636     }
16637 }
16638
16639
16640 /* Emit unwind directives for the given insn.  */
16641
16642 static void
16643 arm_unwind_emit (FILE * asm_out_file, rtx insn)
16644 {
16645   rtx pat;
16646
16647   if (!ARM_EABI_UNWIND_TABLES)
16648     return;
16649
16650   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
16651     return;
16652
16653   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
16654   if (pat)
16655     pat = XEXP (pat, 0);
16656   else
16657     pat = PATTERN (insn);
16658
16659   switch (GET_CODE (pat))
16660     {
16661     case SET:
16662       arm_unwind_emit_set (asm_out_file, pat);
16663       break;
16664
16665     case SEQUENCE:
16666       /* Store multiple.  */
16667       arm_unwind_emit_sequence (asm_out_file, pat);
16668       break;
16669
16670     default:
16671       abort();
16672     }
16673 }
16674
16675
16676 /* Output a reference from a function exception table to the type_info
16677    object X.  The EABI specifies that the symbol should be relocated by
16678    an R_ARM_TARGET2 relocation.  */
16679
16680 static bool
16681 arm_output_ttype (rtx x)
16682 {
16683   fputs ("\t.word\t", asm_out_file);
16684   output_addr_const (asm_out_file, x);
16685   /* Use special relocations for symbol references.  */
16686   if (GET_CODE (x) != CONST_INT)
16687     fputs ("(TARGET2)", asm_out_file);
16688   fputc ('\n', asm_out_file);
16689
16690   return TRUE;
16691 }
16692 #endif /* TARGET_UNWIND_INFO */
16693
16694
16695 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
16696    stack alignment.  */
16697
16698 static void
16699 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
16700 {
16701   rtx unspec = SET_SRC (pattern);
16702   gcc_assert (GET_CODE (unspec) == UNSPEC);
16703
16704   switch (index)
16705     {
16706     case UNSPEC_STACK_ALIGN:
16707       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
16708          put anything on the stack, so hopefully it won't matter.
16709          CFA = SP will be correct after alignment.  */
16710       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
16711                               SET_DEST (pattern));
16712       break;
16713     default:
16714       gcc_unreachable ();
16715     }
16716 }
16717
16718
16719 /* Output unwind directives for the start/end of a function.  */
16720
16721 void
16722 arm_output_fn_unwind (FILE * f, bool prologue)
16723 {
16724   if (!ARM_EABI_UNWIND_TABLES)
16725     return;
16726
16727   if (prologue)
16728     fputs ("\t.fnstart\n", f);
16729   else
16730     fputs ("\t.fnend\n", f);
16731 }
16732
16733 static bool
16734 arm_emit_tls_decoration (FILE *fp, rtx x)
16735 {
16736   enum tls_reloc reloc;
16737   rtx val;
16738
16739   val = XVECEXP (x, 0, 0);
16740   reloc = INTVAL (XVECEXP (x, 0, 1));
16741
16742   output_addr_const (fp, val);
16743
16744   switch (reloc)
16745     {
16746     case TLS_GD32:
16747       fputs ("(tlsgd)", fp);
16748       break;
16749     case TLS_LDM32:
16750       fputs ("(tlsldm)", fp);
16751       break;
16752     case TLS_LDO32:
16753       fputs ("(tlsldo)", fp);
16754       break;
16755     case TLS_IE32:
16756       fputs ("(gottpoff)", fp);
16757       break;
16758     case TLS_LE32:
16759       fputs ("(tpoff)", fp);
16760       break;
16761     default:
16762       gcc_unreachable ();
16763     }
16764
16765   switch (reloc)
16766     {
16767     case TLS_GD32:
16768     case TLS_LDM32:
16769     case TLS_IE32:
16770       fputs (" + (. - ", fp);
16771       output_addr_const (fp, XVECEXP (x, 0, 2));
16772       fputs (" - ", fp);
16773       output_addr_const (fp, XVECEXP (x, 0, 3));
16774       fputc (')', fp);
16775       break;
16776     default:
16777       break;
16778     }
16779
16780   return TRUE;
16781 }
16782
16783 /* ARM implementation of TARGET_ASM_OUTPUT_DWARF_DTPREL.  */
16784
16785 static void
16786 arm_output_dwarf_dtprel (FILE *file, int size, rtx x)
16787 {
16788   gcc_assert (size == 4);
16789   fputs ("\t.word\t", file);
16790   output_addr_const (file, x);
16791   fputs ("(tlsldo)", file);
16792 }
16793
16794 bool
16795 arm_output_addr_const_extra (FILE *fp, rtx x)
16796 {
16797   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
16798     return arm_emit_tls_decoration (fp, x);
16799   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
16800     {
16801       char label[256];
16802       int labelno = INTVAL (XVECEXP (x, 0, 0));
16803
16804       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
16805       assemble_name_raw (fp, label);
16806
16807       return TRUE;
16808     }
16809   else if (GET_CODE (x) == CONST_VECTOR)
16810     return arm_emit_vector_const (fp, x);
16811
16812   return FALSE;
16813 }
16814
16815 /* Output assembly for a shift instruction.
16816    SET_FLAGS determines how the instruction modifies the condition codes.
16817    0 - Do not set condition codes.
16818    1 - Set condition codes.
16819    2 - Use smallest instruction.  */
16820 const char *
16821 arm_output_shift(rtx * operands, int set_flags)
16822 {
16823   char pattern[100];
16824   static const char flag_chars[3] = {'?', '.', '!'};
16825   const char *shift;
16826   HOST_WIDE_INT val;
16827   char c;
16828   
16829   c = flag_chars[set_flags];
16830   if (TARGET_UNIFIED_ASM)
16831     {
16832       shift = shift_op(operands[3], &val);
16833       if (shift)
16834         {
16835           if (val != -1)
16836             operands[2] = GEN_INT(val);
16837           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
16838         }
16839       else
16840         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
16841     }
16842   else
16843     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
16844   output_asm_insn (pattern, operands);
16845   return "";
16846 }
16847
16848 /* Output a Thumb-2 casesi instruction.  */
16849 const char *
16850 thumb2_output_casesi (rtx *operands)
16851 {
16852   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
16853
16854   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
16855
16856   output_asm_insn ("cmp\t%0, %1", operands);
16857   output_asm_insn ("bhi\t%l3", operands);
16858   switch (GET_MODE(diff_vec))
16859     {
16860     case QImode:
16861       return "tbb\t[%|pc, %0]";
16862     case HImode:
16863       return "tbh\t[%|pc, %0, lsl #1]";
16864     case SImode:
16865       if (flag_pic)
16866         {
16867           output_asm_insn ("adr\t%4, %l2", operands);
16868           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
16869           output_asm_insn ("add\t%4, %4, %5", operands);
16870           return "bx\t%4";
16871         }
16872       else
16873         {
16874           output_asm_insn ("adr\t%4, %l2", operands);
16875           return "ldr\t%|pc, [%4, %0, lsl #2]";
16876         }
16877     default:
16878       gcc_unreachable ();
16879     }
16880 }
16881
16882 #include "gt-arm.h"