OSDN Git Service

* c-typeck.c, config/arm/arm.c, config/darwin.c,
[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
56 /* Forward definitions of types.  */
57 typedef struct minipool_node    Mnode;
58 typedef struct minipool_fixup   Mfix;
59
60 const struct attribute_spec arm_attribute_table[];
61
62 /* Forward function declarations.  */
63 static arm_stack_offsets *arm_get_frame_offsets (void);
64 static void arm_add_gc_roots (void);
65 static int arm_gen_constant (enum rtx_code, enum machine_mode, rtx,
66                              HOST_WIDE_INT, rtx, rtx, int, int);
67 static unsigned bit_count (unsigned long);
68 static int arm_address_register_rtx_p (rtx, int);
69 static int arm_legitimate_index_p (enum machine_mode, rtx, RTX_CODE, int);
70 static int thumb2_legitimate_index_p (enum machine_mode, rtx, int);
71 static int thumb1_base_register_rtx_p (rtx, enum machine_mode, int);
72 inline static int thumb1_index_register_rtx_p (rtx, int);
73 static int thumb_far_jump_used_p (void);
74 static bool thumb_force_lr_save (void);
75 static unsigned long thumb1_compute_save_reg_mask (void);
76 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
77 static rtx emit_sfm (int, int);
78 static int arm_size_return_regs (void);
79 #ifndef AOF_ASSEMBLER
80 static bool arm_assemble_integer (rtx, unsigned int, int);
81 #endif
82 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
83 static arm_cc get_arm_condition_code (rtx);
84 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
85 static rtx is_jump_table (rtx);
86 static const char *output_multi_immediate (rtx *, const char *, const char *,
87                                            int, HOST_WIDE_INT);
88 static const char *shift_op (rtx, HOST_WIDE_INT *);
89 static struct machine_function *arm_init_machine_status (void);
90 static void thumb_exit (FILE *, int);
91 static rtx is_jump_table (rtx);
92 static HOST_WIDE_INT get_jump_table_size (rtx);
93 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_forward_ref (Mfix *);
95 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
96 static Mnode *add_minipool_backward_ref (Mfix *);
97 static void assign_minipool_offsets (Mfix *);
98 static void arm_print_value (FILE *, rtx);
99 static void dump_minipool (rtx);
100 static int arm_barrier_cost (rtx);
101 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
102 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
103 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
104                                rtx);
105 static void arm_reorg (void);
106 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
107 static unsigned long arm_compute_save_reg0_reg12_mask (void);
108 static unsigned long arm_compute_save_reg_mask (void);
109 static unsigned long arm_isr_value (tree);
110 static unsigned long arm_compute_func_type (void);
111 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
112 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
113 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
114 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
115 #endif
116 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
117 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
118 static void thumb1_output_function_prologue (FILE *, HOST_WIDE_INT);
119 static int arm_comp_type_attributes (tree, tree);
120 static void arm_set_default_type_attributes (tree);
121 static int arm_adjust_cost (rtx, rtx, rtx, int);
122 static int count_insns_for_constant (HOST_WIDE_INT, int);
123 static int arm_get_strip_length (int);
124 static bool arm_function_ok_for_sibcall (tree, tree);
125 static void arm_internal_label (FILE *, const char *, unsigned long);
126 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
127                                  tree);
128 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
129 static bool arm_size_rtx_costs (rtx, int, int, int *);
130 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
131 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
132 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
133 static bool arm_9e_rtx_costs (rtx, int, int, int *);
134 static int arm_address_cost (rtx);
135 static bool arm_memory_load_p (rtx);
136 static bool arm_cirrus_insn_p (rtx);
137 static void cirrus_reorg (rtx);
138 static void arm_init_builtins (void);
139 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
140 static void arm_init_iwmmxt_builtins (void);
141 static rtx safe_vector_operand (rtx, enum machine_mode);
142 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
143 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
144 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
145 static void emit_constant_insn (rtx cond, rtx pattern);
146 static rtx emit_set_insn (rtx, rtx);
147 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
148                                   tree, bool);
149
150 #ifdef OBJECT_FORMAT_ELF
151 static void arm_elf_asm_constructor (rtx, int);
152 static void arm_elf_asm_destructor (rtx, int);
153 #endif
154 #ifndef ARM_PE
155 static void arm_encode_section_info (tree, rtx, int);
156 #endif
157
158 static void arm_file_end (void);
159 static void arm_file_start (void);
160
161 #ifdef AOF_ASSEMBLER
162 static void aof_globalize_label (FILE *, const char *);
163 static void aof_dump_imports (FILE *);
164 static void aof_dump_pic_table (FILE *);
165 static void aof_file_start (void);
166 static void aof_file_end (void);
167 static void aof_asm_init_sections (void);
168 #endif
169 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
170                                         tree, int *, int);
171 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
172                                    enum machine_mode, tree, bool);
173 static bool arm_promote_prototypes (tree);
174 static bool arm_default_short_enums (void);
175 static bool arm_align_anon_bitfield (void);
176 static bool arm_return_in_msb (tree);
177 static bool arm_must_pass_in_stack (enum machine_mode, tree);
178 #ifdef TARGET_UNWIND_INFO
179 static void arm_unwind_emit (FILE *, rtx);
180 static bool arm_output_ttype (rtx);
181 #endif
182 static void arm_dwarf_handle_frame_unspec (const char *, rtx, int);
183
184 static tree arm_cxx_guard_type (void);
185 static bool arm_cxx_guard_mask_bit (void);
186 static tree arm_get_cookie_size (tree);
187 static bool arm_cookie_has_size (void);
188 static bool arm_cxx_cdtor_returns_this (void);
189 static bool arm_cxx_key_method_may_be_inline (void);
190 static void arm_cxx_determine_class_data_visibility (tree);
191 static bool arm_cxx_class_data_always_comdat (void);
192 static bool arm_cxx_use_aeabi_atexit (void);
193 static void arm_init_libfuncs (void);
194 static bool arm_handle_option (size_t, const char *, int);
195 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
196 static bool arm_cannot_copy_insn_p (rtx);
197 static bool arm_tls_symbol_p (rtx x);
198
199 \f
200 /* Initialize the GCC target structure.  */
201 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
202 #undef  TARGET_MERGE_DECL_ATTRIBUTES
203 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
204 #endif
205
206 #undef  TARGET_ATTRIBUTE_TABLE
207 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
208
209 #undef TARGET_ASM_FILE_START
210 #define TARGET_ASM_FILE_START arm_file_start
211 #undef TARGET_ASM_FILE_END
212 #define TARGET_ASM_FILE_END arm_file_end
213
214 #ifdef AOF_ASSEMBLER
215 #undef  TARGET_ASM_BYTE_OP
216 #define TARGET_ASM_BYTE_OP "\tDCB\t"
217 #undef  TARGET_ASM_ALIGNED_HI_OP
218 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
219 #undef  TARGET_ASM_ALIGNED_SI_OP
220 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
221 #undef TARGET_ASM_GLOBALIZE_LABEL
222 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
223 #undef TARGET_ASM_FILE_START
224 #define TARGET_ASM_FILE_START aof_file_start
225 #undef TARGET_ASM_FILE_END
226 #define TARGET_ASM_FILE_END aof_file_end
227 #else
228 #undef  TARGET_ASM_ALIGNED_SI_OP
229 #define TARGET_ASM_ALIGNED_SI_OP NULL
230 #undef  TARGET_ASM_INTEGER
231 #define TARGET_ASM_INTEGER arm_assemble_integer
232 #endif
233
234 #undef  TARGET_ASM_FUNCTION_PROLOGUE
235 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
236
237 #undef  TARGET_ASM_FUNCTION_EPILOGUE
238 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
239
240 #undef  TARGET_DEFAULT_TARGET_FLAGS
241 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
242 #undef  TARGET_HANDLE_OPTION
243 #define TARGET_HANDLE_OPTION arm_handle_option
244
245 #undef  TARGET_COMP_TYPE_ATTRIBUTES
246 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
247
248 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
249 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
250
251 #undef  TARGET_SCHED_ADJUST_COST
252 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
253
254 #undef TARGET_ENCODE_SECTION_INFO
255 #ifdef ARM_PE
256 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
257 #else
258 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
259 #endif
260
261 #undef  TARGET_STRIP_NAME_ENCODING
262 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
263
264 #undef  TARGET_ASM_INTERNAL_LABEL
265 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
266
267 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
268 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
269
270 #undef  TARGET_ASM_OUTPUT_MI_THUNK
271 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
272 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
273 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
274
275 /* This will be overridden in arm_override_options.  */
276 #undef  TARGET_RTX_COSTS
277 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
278 #undef  TARGET_ADDRESS_COST
279 #define TARGET_ADDRESS_COST arm_address_cost
280
281 #undef TARGET_SHIFT_TRUNCATION_MASK
282 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
283 #undef TARGET_VECTOR_MODE_SUPPORTED_P
284 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
285
286 #undef  TARGET_MACHINE_DEPENDENT_REORG
287 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
288
289 #undef  TARGET_INIT_BUILTINS
290 #define TARGET_INIT_BUILTINS  arm_init_builtins
291 #undef  TARGET_EXPAND_BUILTIN
292 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
293
294 #undef TARGET_INIT_LIBFUNCS
295 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
296
297 #undef TARGET_PROMOTE_FUNCTION_ARGS
298 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
299 #undef TARGET_PROMOTE_FUNCTION_RETURN
300 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
301 #undef TARGET_PROMOTE_PROTOTYPES
302 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
303 #undef TARGET_PASS_BY_REFERENCE
304 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
305 #undef TARGET_ARG_PARTIAL_BYTES
306 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
307
308 #undef  TARGET_SETUP_INCOMING_VARARGS
309 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
310
311 #undef TARGET_DEFAULT_SHORT_ENUMS
312 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
313
314 #undef TARGET_ALIGN_ANON_BITFIELD
315 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
316
317 #undef TARGET_NARROW_VOLATILE_BITFIELD
318 #define TARGET_NARROW_VOLATILE_BITFIELD hook_bool_void_false
319
320 #undef TARGET_CXX_GUARD_TYPE
321 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
322
323 #undef TARGET_CXX_GUARD_MASK_BIT
324 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
325
326 #undef TARGET_CXX_GET_COOKIE_SIZE
327 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
328
329 #undef TARGET_CXX_COOKIE_HAS_SIZE
330 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
331
332 #undef TARGET_CXX_CDTOR_RETURNS_THIS
333 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
334
335 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
336 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
337
338 #undef TARGET_CXX_USE_AEABI_ATEXIT
339 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
340
341 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
342 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
343   arm_cxx_determine_class_data_visibility
344
345 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
346 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
347
348 #undef TARGET_RETURN_IN_MSB
349 #define TARGET_RETURN_IN_MSB arm_return_in_msb
350
351 #undef TARGET_MUST_PASS_IN_STACK
352 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
353
354 #ifdef TARGET_UNWIND_INFO
355 #undef TARGET_UNWIND_EMIT
356 #define TARGET_UNWIND_EMIT arm_unwind_emit
357
358 /* EABI unwinding tables use a different format for the typeinfo tables.  */
359 #undef TARGET_ASM_TTYPE
360 #define TARGET_ASM_TTYPE arm_output_ttype
361
362 #undef TARGET_ARM_EABI_UNWINDER
363 #define TARGET_ARM_EABI_UNWINDER true
364 #endif /* TARGET_UNWIND_INFO */
365
366 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
367 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC arm_dwarf_handle_frame_unspec
368
369 #undef  TARGET_CANNOT_COPY_INSN_P
370 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
371
372 #ifdef HAVE_AS_TLS
373 #undef TARGET_HAVE_TLS
374 #define TARGET_HAVE_TLS true
375 #endif
376
377 #undef TARGET_CANNOT_FORCE_CONST_MEM
378 #define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
379
380 struct gcc_target targetm = TARGET_INITIALIZER;
381 \f
382 /* Obstack for minipool constant handling.  */
383 static struct obstack minipool_obstack;
384 static char *         minipool_startobj;
385
386 /* The maximum number of insns skipped which
387    will be conditionalised if possible.  */
388 static int max_insns_skipped = 5;
389
390 extern FILE * asm_out_file;
391
392 /* True if we are currently building a constant table.  */
393 int making_const_table;
394
395 /* Define the information needed to generate branch insns.  This is
396    stored from the compare operation.  */
397 rtx arm_compare_op0, arm_compare_op1;
398
399 /* The processor for which instructions should be scheduled.  */
400 enum processor_type arm_tune = arm_none;
401
402 /* The default processor used if not overridden by commandline.  */
403 static enum processor_type arm_default_cpu = arm_none;
404
405 /* Which floating point model to use.  */
406 enum arm_fp_model arm_fp_model;
407
408 /* Which floating point hardware is available.  */
409 enum fputype arm_fpu_arch;
410
411 /* Which floating point hardware to schedule for.  */
412 enum fputype arm_fpu_tune;
413
414 /* Whether to use floating point hardware.  */
415 enum float_abi_type arm_float_abi;
416
417 /* Which ABI to use.  */
418 enum arm_abi_type arm_abi;
419
420 /* Which thread pointer model to use.  */
421 enum arm_tp_type target_thread_pointer = TP_AUTO;
422
423 /* Used to parse -mstructure_size_boundary command line option.  */
424 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
425
426 /* Used for Thumb call_via trampolines.  */
427 rtx thumb_call_via_label[14];
428 static int thumb_call_reg_needed;
429
430 /* Bit values used to identify processor capabilities.  */
431 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
432 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
433 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
434 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
435 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
436 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
437 #define FL_THUMB      (1 << 6)        /* Thumb aware */
438 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
439 #define FL_STRONG     (1 << 8)        /* StrongARM */
440 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
441 #define FL_XSCALE     (1 << 10)       /* XScale */
442 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
443 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
444                                          media instructions.  */
445 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
446 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
447                                          Note: ARM6 & 7 derivatives only.  */
448 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
449 #define FL_THUMB2     (1 << 16)       /* Thumb-2.  */
450 #define FL_NOTM       (1 << 17)       /* Instructions not present in the 'M'
451                                          profile.  */
452 #define FL_DIV        (1 << 18)       /* Hardware divide.  */
453
454 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
455
456 #define FL_FOR_ARCH2    FL_NOTM
457 #define FL_FOR_ARCH3    (FL_FOR_ARCH2 | FL_MODE32)
458 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
459 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
460 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
461 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
462 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
463 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
464 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
465 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
466 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
467 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
468 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
469 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
470 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
471 #define FL_FOR_ARCH6T2  (FL_FOR_ARCH6 | FL_THUMB2)
472 #define FL_FOR_ARCH7    (FL_FOR_ARCH6T2 &~ FL_NOTM)
473 #define FL_FOR_ARCH7A   (FL_FOR_ARCH7 | FL_NOTM)
474 #define FL_FOR_ARCH7R   (FL_FOR_ARCH7A | FL_DIV)
475 #define FL_FOR_ARCH7M   (FL_FOR_ARCH7 | FL_DIV)
476
477 /* The bits in this mask specify which
478    instructions we are allowed to generate.  */
479 static unsigned long insn_flags = 0;
480
481 /* The bits in this mask specify which instruction scheduling options should
482    be used.  */
483 static unsigned long tune_flags = 0;
484
485 /* The following are used in the arm.md file as equivalents to bits
486    in the above two flag variables.  */
487
488 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
489 int arm_arch3m = 0;
490
491 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
492 int arm_arch4 = 0;
493
494 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
495 int arm_arch4t = 0;
496
497 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
498 int arm_arch5 = 0;
499
500 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
501 int arm_arch5e = 0;
502
503 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
504 int arm_arch6 = 0;
505
506 /* Nonzero if this chip supports the ARM 6K extensions.  */
507 int arm_arch6k = 0;
508
509 /* Nonzero if instructions not present in the 'M' profile can be used.  */
510 int arm_arch_notm = 0;
511
512 /* Nonzero if this chip can benefit from load scheduling.  */
513 int arm_ld_sched = 0;
514
515 /* Nonzero if this chip is a StrongARM.  */
516 int arm_tune_strongarm = 0;
517
518 /* Nonzero if this chip is a Cirrus variant.  */
519 int arm_arch_cirrus = 0;
520
521 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
522 int arm_arch_iwmmxt = 0;
523
524 /* Nonzero if this chip is an XScale.  */
525 int arm_arch_xscale = 0;
526
527 /* Nonzero if tuning for XScale  */
528 int arm_tune_xscale = 0;
529
530 /* Nonzero if we want to tune for stores that access the write-buffer.
531    This typically means an ARM6 or ARM7 with MMU or MPU.  */
532 int arm_tune_wbuf = 0;
533
534 /* Nonzero if generating Thumb instructions.  */
535 int thumb_code = 0;
536
537 /* Nonzero if we should define __THUMB_INTERWORK__ in the
538    preprocessor.
539    XXX This is a bit of a hack, it's intended to help work around
540    problems in GLD which doesn't understand that armv5t code is
541    interworking clean.  */
542 int arm_cpp_interwork = 0;
543
544 /* Nonzero if chip supports Thumb 2.  */
545 int arm_arch_thumb2;
546
547 /* Nonzero if chip supports integer division instruction.  */
548 int arm_arch_hwdiv;
549
550 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
551    must report the mode of the memory reference from PRINT_OPERAND to
552    PRINT_OPERAND_ADDRESS.  */
553 enum machine_mode output_memory_reference_mode;
554
555 /* The register number to be used for the PIC offset register.  */
556 unsigned arm_pic_register = INVALID_REGNUM;
557
558 /* Set to 1 when a return insn is output, this means that the epilogue
559    is not needed.  */
560 int return_used_this_function;
561
562 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
563    the next function.  */
564 static int after_arm_reorg = 0;
565
566 /* The maximum number of insns to be used when loading a constant.  */
567 static int arm_constant_limit = 3;
568
569 /* For an explanation of these variables, see final_prescan_insn below.  */
570 int arm_ccfsm_state;
571 /* arm_current_cc is also used for Thumb-2 cond_exec blocks.  */
572 enum arm_cond_code arm_current_cc;
573 rtx arm_target_insn;
574 int arm_target_label;
575 /* The number of conditionally executed insns, including the current insn.  */
576 int arm_condexec_count = 0;
577 /* A bitmask specifying the patterns for the IT block.
578    Zero means do not output an IT block before this insn. */
579 int arm_condexec_mask = 0;
580 /* The number of bits used in arm_condexec_mask.  */
581 int arm_condexec_masklen = 0;
582
583 /* The condition codes of the ARM, and the inverse function.  */
584 static const char * const arm_condition_codes[] =
585 {
586   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
587   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
588 };
589
590 #define ARM_LSL_NAME (TARGET_UNIFIED_ASM ? "lsl" : "asl")
591 #define streq(string1, string2) (strcmp (string1, string2) == 0)
592
593 #define THUMB2_WORK_REGS (0xff & ~(  (1 << THUMB_HARD_FRAME_POINTER_REGNUM) \
594                                    | (1 << SP_REGNUM) | (1 << PC_REGNUM) \
595                                    | (1 << PIC_OFFSET_TABLE_REGNUM)))
596 \f
597 /* Initialization code.  */
598
599 struct processors
600 {
601   const char *const name;
602   enum processor_type core;
603   const char *arch;
604   const unsigned long flags;
605   bool (* rtx_costs) (rtx, int, int, int *);
606 };
607
608 /* Not all of these give usefully different compilation alternatives,
609    but there is no simple way of generalizing them.  */
610 static const struct processors all_cores[] =
611 {
612   /* ARM Cores */
613 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
614   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
615 #include "arm-cores.def"
616 #undef ARM_CORE
617   {NULL, arm_none, NULL, 0, NULL}
618 };
619
620 static const struct processors all_architectures[] =
621 {
622   /* ARM Architectures */
623   /* We don't specify rtx_costs here as it will be figured out
624      from the core.  */
625
626   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
627   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
628   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
629   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
630   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
631   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
632      implementations that support it, so we will leave it out for now.  */
633   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
634   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
635   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
636   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
637   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
638   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
639   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
640   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
641   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
642   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
643   {"armv6t2", arm1156t2s, "6T2", FL_CO_PROC |             FL_FOR_ARCH6T2, NULL},
644   {"armv7",   cortexa8,   "7",   FL_CO_PROC |             FL_FOR_ARCH7, NULL},
645   {"armv7-a", cortexa8,   "7A",  FL_CO_PROC |             FL_FOR_ARCH7A, NULL},
646   {"armv7-r", cortexr4,   "7R",  FL_CO_PROC |             FL_FOR_ARCH7R, NULL},
647   {"armv7-m", cortexm3,   "7M",  FL_CO_PROC |             FL_FOR_ARCH7M, NULL},
648   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
649   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
650   {NULL, arm_none, NULL, 0 , NULL}
651 };
652
653 struct arm_cpu_select
654 {
655   const char *              string;
656   const char *              name;
657   const struct processors * processors;
658 };
659
660 /* This is a magic structure.  The 'string' field is magically filled in
661    with a pointer to the value specified by the user on the command line
662    assuming that the user has specified such a value.  */
663
664 static struct arm_cpu_select arm_select[] =
665 {
666   /* string       name            processors  */
667   { NULL,       "-mcpu=",       all_cores  },
668   { NULL,       "-march=",      all_architectures },
669   { NULL,       "-mtune=",      all_cores }
670 };
671
672 /* Defines representing the indexes into the above table.  */
673 #define ARM_OPT_SET_CPU 0
674 #define ARM_OPT_SET_ARCH 1
675 #define ARM_OPT_SET_TUNE 2
676
677 /* The name of the preprocessor macro to define for this architecture.  */
678
679 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
680
681 struct fpu_desc
682 {
683   const char * name;
684   enum fputype fpu;
685 };
686
687
688 /* Available values for -mfpu=.  */
689
690 static const struct fpu_desc all_fpus[] =
691 {
692   {"fpa",       FPUTYPE_FPA},
693   {"fpe2",      FPUTYPE_FPA_EMU2},
694   {"fpe3",      FPUTYPE_FPA_EMU2},
695   {"maverick",  FPUTYPE_MAVERICK},
696   {"vfp",       FPUTYPE_VFP}
697 };
698
699
700 /* Floating point models used by the different hardware.
701    See fputype in arm.h.  */
702
703 static const enum fputype fp_model_for_fpu[] =
704 {
705   /* No FP hardware.  */
706   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
707   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
708   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
709   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
710   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
711   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
712 };
713
714
715 struct float_abi
716 {
717   const char * name;
718   enum float_abi_type abi_type;
719 };
720
721
722 /* Available values for -mfloat-abi=.  */
723
724 static const struct float_abi all_float_abis[] =
725 {
726   {"soft",      ARM_FLOAT_ABI_SOFT},
727   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
728   {"hard",      ARM_FLOAT_ABI_HARD}
729 };
730
731
732 struct abi_name
733 {
734   const char *name;
735   enum arm_abi_type abi_type;
736 };
737
738
739 /* Available values for -mabi=.  */
740
741 static const struct abi_name arm_all_abis[] =
742 {
743   {"apcs-gnu",    ARM_ABI_APCS},
744   {"atpcs",   ARM_ABI_ATPCS},
745   {"aapcs",   ARM_ABI_AAPCS},
746   {"iwmmxt",  ARM_ABI_IWMMXT},
747   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
748 };
749
750 /* Supported TLS relocations.  */
751
752 enum tls_reloc {
753   TLS_GD32,
754   TLS_LDM32,
755   TLS_LDO32,
756   TLS_IE32,
757   TLS_LE32
758 };
759
760 /* Emit an insn that's a simple single-set.  Both the operands must be known
761    to be valid.  */
762 inline static rtx
763 emit_set_insn (rtx x, rtx y)
764 {
765   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
766 }
767
768 /* Return the number of bits set in VALUE.  */
769 static unsigned
770 bit_count (unsigned long value)
771 {
772   unsigned long count = 0;
773
774   while (value)
775     {
776       count++;
777       value &= value - 1;  /* Clear the least-significant set bit.  */
778     }
779
780   return count;
781 }
782
783 /* Set up library functions unique to ARM.  */
784
785 static void
786 arm_init_libfuncs (void)
787 {
788   /* There are no special library functions unless we are using the
789      ARM BPABI.  */
790   if (!TARGET_BPABI)
791     return;
792
793   /* The functions below are described in Section 4 of the "Run-Time
794      ABI for the ARM architecture", Version 1.0.  */
795
796   /* Double-precision floating-point arithmetic.  Table 2.  */
797   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
798   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
799   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
800   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
801   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
802
803   /* Double-precision comparisons.  Table 3.  */
804   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
805   set_optab_libfunc (ne_optab, DFmode, NULL);
806   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
807   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
808   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
809   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
810   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
811
812   /* Single-precision floating-point arithmetic.  Table 4.  */
813   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
814   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
815   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
816   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
817   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
818
819   /* Single-precision comparisons.  Table 5.  */
820   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
821   set_optab_libfunc (ne_optab, SFmode, NULL);
822   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
823   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
824   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
825   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
826   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
827
828   /* Floating-point to integer conversions.  Table 6.  */
829   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
830   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
831   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
832   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
833   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
834   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
835   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
836   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
837
838   /* Conversions between floating types.  Table 7.  */
839   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
840   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
841
842   /* Integer to floating-point conversions.  Table 8.  */
843   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
844   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
845   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
846   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
847   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
848   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
849   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
850   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
851
852   /* Long long.  Table 9.  */
853   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
854   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
855   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
856   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
857   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
858   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
859   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
860   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
861
862   /* Integer (32/32->32) division.  \S 4.3.1.  */
863   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
864   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
865
866   /* The divmod functions are designed so that they can be used for
867      plain division, even though they return both the quotient and the
868      remainder.  The quotient is returned in the usual location (i.e.,
869      r0 for SImode, {r0, r1} for DImode), just as would be expected
870      for an ordinary division routine.  Because the AAPCS calling
871      conventions specify that all of { r0, r1, r2, r3 } are
872      callee-saved registers, there is no need to tell the compiler
873      explicitly that those registers are clobbered by these
874      routines.  */
875   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
876   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
877
878   /* For SImode division the ABI provides div-without-mod routines,
879      which are faster.  */
880   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
881   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
882
883   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
884      divmod libcalls instead.  */
885   set_optab_libfunc (smod_optab, DImode, NULL);
886   set_optab_libfunc (umod_optab, DImode, NULL);
887   set_optab_libfunc (smod_optab, SImode, NULL);
888   set_optab_libfunc (umod_optab, SImode, NULL);
889 }
890
891 /* Implement TARGET_HANDLE_OPTION.  */
892
893 static bool
894 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
895 {
896   switch (code)
897     {
898     case OPT_march_:
899       arm_select[1].string = arg;
900       return true;
901
902     case OPT_mcpu_:
903       arm_select[0].string = arg;
904       return true;
905
906     case OPT_mhard_float:
907       target_float_abi_name = "hard";
908       return true;
909
910     case OPT_msoft_float:
911       target_float_abi_name = "soft";
912       return true;
913
914     case OPT_mtune_:
915       arm_select[2].string = arg;
916       return true;
917
918     default:
919       return true;
920     }
921 }
922
923 /* Fix up any incompatible options that the user has specified.
924    This has now turned into a maze.  */
925 void
926 arm_override_options (void)
927 {
928   unsigned i;
929   enum processor_type target_arch_cpu = arm_none;
930
931   /* Set up the flags based on the cpu/architecture selected by the user.  */
932   for (i = ARRAY_SIZE (arm_select); i--;)
933     {
934       struct arm_cpu_select * ptr = arm_select + i;
935
936       if (ptr->string != NULL && ptr->string[0] != '\0')
937         {
938           const struct processors * sel;
939
940           for (sel = ptr->processors; sel->name != NULL; sel++)
941             if (streq (ptr->string, sel->name))
942               {
943                 /* Set the architecture define.  */
944                 if (i != ARM_OPT_SET_TUNE)
945                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
946
947                 /* Determine the processor core for which we should
948                    tune code-generation.  */
949                 if (/* -mcpu= is a sensible default.  */
950                     i == ARM_OPT_SET_CPU
951                     /* -mtune= overrides -mcpu= and -march=.  */
952                     || i == ARM_OPT_SET_TUNE)
953                   arm_tune = (enum processor_type) (sel - ptr->processors);
954
955                 /* Remember the CPU associated with this architecture.
956                    If no other option is used to set the CPU type,
957                    we'll use this to guess the most suitable tuning
958                    options.  */
959                 if (i == ARM_OPT_SET_ARCH)
960                   target_arch_cpu = sel->core;
961
962                 if (i != ARM_OPT_SET_TUNE)
963                   {
964                     /* If we have been given an architecture and a processor
965                        make sure that they are compatible.  We only generate
966                        a warning though, and we prefer the CPU over the
967                        architecture.  */
968                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
969                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
970                                ptr->string);
971
972                     insn_flags = sel->flags;
973                   }
974
975                 break;
976               }
977
978           if (sel->name == NULL)
979             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
980         }
981     }
982
983   /* Guess the tuning options from the architecture if necessary.  */
984   if (arm_tune == arm_none)
985     arm_tune = target_arch_cpu;
986
987   /* If the user did not specify a processor, choose one for them.  */
988   if (insn_flags == 0)
989     {
990       const struct processors * sel;
991       unsigned int        sought;
992       enum processor_type cpu;
993
994       cpu = TARGET_CPU_DEFAULT;
995       if (cpu == arm_none)
996         {
997 #ifdef SUBTARGET_CPU_DEFAULT
998           /* Use the subtarget default CPU if none was specified by
999              configure.  */
1000           cpu = SUBTARGET_CPU_DEFAULT;
1001 #endif
1002           /* Default to ARM6.  */
1003           if (cpu == arm_none)
1004             cpu = arm6;
1005         }
1006       sel = &all_cores[cpu];
1007
1008       insn_flags = sel->flags;
1009
1010       /* Now check to see if the user has specified some command line
1011          switch that require certain abilities from the cpu.  */
1012       sought = 0;
1013
1014       if (TARGET_INTERWORK || TARGET_THUMB)
1015         {
1016           sought |= (FL_THUMB | FL_MODE32);
1017
1018           /* There are no ARM processors that support both APCS-26 and
1019              interworking.  Therefore we force FL_MODE26 to be removed
1020              from insn_flags here (if it was set), so that the search
1021              below will always be able to find a compatible processor.  */
1022           insn_flags &= ~FL_MODE26;
1023         }
1024
1025       if (sought != 0 && ((sought & insn_flags) != sought))
1026         {
1027           /* Try to locate a CPU type that supports all of the abilities
1028              of the default CPU, plus the extra abilities requested by
1029              the user.  */
1030           for (sel = all_cores; sel->name != NULL; sel++)
1031             if ((sel->flags & sought) == (sought | insn_flags))
1032               break;
1033
1034           if (sel->name == NULL)
1035             {
1036               unsigned current_bit_count = 0;
1037               const struct processors * best_fit = NULL;
1038
1039               /* Ideally we would like to issue an error message here
1040                  saying that it was not possible to find a CPU compatible
1041                  with the default CPU, but which also supports the command
1042                  line options specified by the programmer, and so they
1043                  ought to use the -mcpu=<name> command line option to
1044                  override the default CPU type.
1045
1046                  If we cannot find a cpu that has both the
1047                  characteristics of the default cpu and the given
1048                  command line options we scan the array again looking
1049                  for a best match.  */
1050               for (sel = all_cores; sel->name != NULL; sel++)
1051                 if ((sel->flags & sought) == sought)
1052                   {
1053                     unsigned count;
1054
1055                     count = bit_count (sel->flags & insn_flags);
1056
1057                     if (count >= current_bit_count)
1058                       {
1059                         best_fit = sel;
1060                         current_bit_count = count;
1061                       }
1062                   }
1063
1064               gcc_assert (best_fit);
1065               sel = best_fit;
1066             }
1067
1068           insn_flags = sel->flags;
1069         }
1070       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1071       arm_default_cpu = (enum processor_type) (sel - all_cores);
1072       if (arm_tune == arm_none)
1073         arm_tune = arm_default_cpu;
1074     }
1075
1076   /* The processor for which we should tune should now have been
1077      chosen.  */
1078   gcc_assert (arm_tune != arm_none);
1079
1080   tune_flags = all_cores[(int)arm_tune].flags;
1081   if (optimize_size)
1082     targetm.rtx_costs = arm_size_rtx_costs;
1083   else
1084     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1085
1086   /* Make sure that the processor choice does not conflict with any of the
1087      other command line choices.  */
1088   if (TARGET_ARM && !(insn_flags & FL_NOTM))
1089     error ("target CPU does not support ARM mode");
1090
1091   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1092     {
1093       warning (0, "target CPU does not support interworking" );
1094       target_flags &= ~MASK_INTERWORK;
1095     }
1096
1097   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1098     {
1099       warning (0, "target CPU does not support THUMB instructions");
1100       target_flags &= ~MASK_THUMB;
1101     }
1102
1103   if (TARGET_APCS_FRAME && TARGET_THUMB)
1104     {
1105       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1106       target_flags &= ~MASK_APCS_FRAME;
1107     }
1108
1109   /* Callee super interworking implies thumb interworking.  Adding
1110      this to the flags here simplifies the logic elsewhere.  */
1111   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1112       target_flags |= MASK_INTERWORK;
1113
1114   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1115      from here where no function is being compiled currently.  */
1116   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1117     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1118
1119   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1120     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1121
1122   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1123     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1124
1125   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1126     {
1127       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1128       target_flags |= MASK_APCS_FRAME;
1129     }
1130
1131   if (TARGET_POKE_FUNCTION_NAME)
1132     target_flags |= MASK_APCS_FRAME;
1133
1134   if (TARGET_APCS_REENT && flag_pic)
1135     error ("-fpic and -mapcs-reent are incompatible");
1136
1137   if (TARGET_APCS_REENT)
1138     warning (0, "APCS reentrant code not supported.  Ignored");
1139
1140   /* If this target is normally configured to use APCS frames, warn if they
1141      are turned off and debugging is turned on.  */
1142   if (TARGET_ARM
1143       && write_symbols != NO_DEBUG
1144       && !TARGET_APCS_FRAME
1145       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1146     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1147
1148   if (TARGET_APCS_FLOAT)
1149     warning (0, "passing floating point arguments in fp regs not yet supported");
1150
1151   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1152   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1153   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1154   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1155   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1156   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1157   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1158   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1159   arm_arch_notm = (insn_flags & FL_NOTM) != 0;
1160   arm_arch_thumb2 = (insn_flags & FL_THUMB2) != 0;
1161   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1162   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1163
1164   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1165   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1166   thumb_code = (TARGET_ARM == 0);
1167   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1168   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1169   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1170   arm_arch_hwdiv = (insn_flags & FL_DIV) != 0;
1171
1172   /* V5 code we generate is completely interworking capable, so we turn off
1173      TARGET_INTERWORK here to avoid many tests later on.  */
1174
1175   /* XXX However, we must pass the right pre-processor defines to CPP
1176      or GLD can get confused.  This is a hack.  */
1177   if (TARGET_INTERWORK)
1178     arm_cpp_interwork = 1;
1179
1180   if (arm_arch5)
1181     target_flags &= ~MASK_INTERWORK;
1182
1183   if (target_abi_name)
1184     {
1185       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1186         {
1187           if (streq (arm_all_abis[i].name, target_abi_name))
1188             {
1189               arm_abi = arm_all_abis[i].abi_type;
1190               break;
1191             }
1192         }
1193       if (i == ARRAY_SIZE (arm_all_abis))
1194         error ("invalid ABI option: -mabi=%s", target_abi_name);
1195     }
1196   else
1197     arm_abi = ARM_DEFAULT_ABI;
1198
1199   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1200     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1201
1202   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1203     error ("iwmmxt abi requires an iwmmxt capable cpu");
1204
1205   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1206   if (target_fpu_name == NULL && target_fpe_name != NULL)
1207     {
1208       if (streq (target_fpe_name, "2"))
1209         target_fpu_name = "fpe2";
1210       else if (streq (target_fpe_name, "3"))
1211         target_fpu_name = "fpe3";
1212       else
1213         error ("invalid floating point emulation option: -mfpe=%s",
1214                target_fpe_name);
1215     }
1216   if (target_fpu_name != NULL)
1217     {
1218       /* The user specified a FPU.  */
1219       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1220         {
1221           if (streq (all_fpus[i].name, target_fpu_name))
1222             {
1223               arm_fpu_arch = all_fpus[i].fpu;
1224               arm_fpu_tune = arm_fpu_arch;
1225               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1226               break;
1227             }
1228         }
1229       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1230         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1231     }
1232   else
1233     {
1234 #ifdef FPUTYPE_DEFAULT
1235       /* Use the default if it is specified for this platform.  */
1236       arm_fpu_arch = FPUTYPE_DEFAULT;
1237       arm_fpu_tune = FPUTYPE_DEFAULT;
1238 #else
1239       /* Pick one based on CPU type.  */
1240       /* ??? Some targets assume FPA is the default.
1241       if ((insn_flags & FL_VFP) != 0)
1242         arm_fpu_arch = FPUTYPE_VFP;
1243       else
1244       */
1245       if (arm_arch_cirrus)
1246         arm_fpu_arch = FPUTYPE_MAVERICK;
1247       else
1248         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1249 #endif
1250       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1251         arm_fpu_tune = FPUTYPE_FPA;
1252       else
1253         arm_fpu_tune = arm_fpu_arch;
1254       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1255       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1256     }
1257
1258   if (target_float_abi_name != NULL)
1259     {
1260       /* The user specified a FP ABI.  */
1261       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1262         {
1263           if (streq (all_float_abis[i].name, target_float_abi_name))
1264             {
1265               arm_float_abi = all_float_abis[i].abi_type;
1266               break;
1267             }
1268         }
1269       if (i == ARRAY_SIZE (all_float_abis))
1270         error ("invalid floating point abi: -mfloat-abi=%s",
1271                target_float_abi_name);
1272     }
1273   else
1274     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1275
1276   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1277     sorry ("-mfloat-abi=hard and VFP");
1278
1279   /* FPA and iWMMXt are incompatible because the insn encodings overlap.
1280      VFP and iWMMXt can theoretically coexist, but it's unlikely such silicon
1281      will ever exist.  GCC makes no attempt to support this combination.  */
1282   if (TARGET_IWMMXT && !TARGET_SOFT_FLOAT)
1283     sorry ("iWMMXt and hardware floating point");
1284
1285   /* ??? iWMMXt insn patterns need auditing for Thumb-2.  */
1286   if (TARGET_THUMB2 && TARGET_IWMMXT)
1287     sorry ("Thumb-2 iWMMXt");
1288
1289   /* If soft-float is specified then don't use FPU.  */
1290   if (TARGET_SOFT_FLOAT)
1291     arm_fpu_arch = FPUTYPE_NONE;
1292
1293   /* For arm2/3 there is no need to do any scheduling if there is only
1294      a floating point emulator, or we are doing software floating-point.  */
1295   if ((TARGET_SOFT_FLOAT
1296        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1297        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1298       && (tune_flags & FL_MODE32) == 0)
1299     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1300
1301   if (target_thread_switch)
1302     {
1303       if (strcmp (target_thread_switch, "soft") == 0)
1304         target_thread_pointer = TP_SOFT;
1305       else if (strcmp (target_thread_switch, "auto") == 0)
1306         target_thread_pointer = TP_AUTO;
1307       else if (strcmp (target_thread_switch, "cp15") == 0)
1308         target_thread_pointer = TP_CP15;
1309       else
1310         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1311     }
1312
1313   /* Use the cp15 method if it is available.  */
1314   if (target_thread_pointer == TP_AUTO)
1315     {
1316       if (arm_arch6k && !TARGET_THUMB)
1317         target_thread_pointer = TP_CP15;
1318       else
1319         target_thread_pointer = TP_SOFT;
1320     }
1321
1322   if (TARGET_HARD_TP && TARGET_THUMB1)
1323     error ("can not use -mtp=cp15 with 16-bit Thumb");
1324
1325   /* Override the default structure alignment for AAPCS ABI.  */
1326   if (TARGET_AAPCS_BASED)
1327     arm_structure_size_boundary = 8;
1328
1329   if (structure_size_string != NULL)
1330     {
1331       int size = strtol (structure_size_string, NULL, 0);
1332
1333       if (size == 8 || size == 32
1334           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1335         arm_structure_size_boundary = size;
1336       else
1337         warning (0, "structure size boundary can only be set to %s",
1338                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1339     }
1340
1341   /* If stack checking is disabled, we can use r10 as the PIC register,
1342      which keeps r9 available.  The EABI specifies r9 as the PIC register.  */
1343   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1344     arm_pic_register = (TARGET_APCS_STACK || TARGET_AAPCS_BASED) ? 9 : 10;
1345
1346   if (arm_pic_register_string != NULL)
1347     {
1348       int pic_register = decode_reg_name (arm_pic_register_string);
1349
1350       if (!flag_pic)
1351         warning (0, "-mpic-register= is useless without -fpic");
1352
1353       /* Prevent the user from choosing an obviously stupid PIC register.  */
1354       else if (pic_register < 0 || call_used_regs[pic_register]
1355                || pic_register == HARD_FRAME_POINTER_REGNUM
1356                || pic_register == STACK_POINTER_REGNUM
1357                || pic_register >= PC_REGNUM)
1358         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1359       else
1360         arm_pic_register = pic_register;
1361     }
1362
1363   /* ??? We might want scheduling for thumb2.  */
1364   if (TARGET_THUMB && flag_schedule_insns)
1365     {
1366       /* Don't warn since it's on by default in -O2.  */
1367       flag_schedule_insns = 0;
1368     }
1369
1370   if (optimize_size)
1371     {
1372       arm_constant_limit = 1;
1373
1374       /* If optimizing for size, bump the number of instructions that we
1375          are prepared to conditionally execute (even on a StrongARM).  */
1376       max_insns_skipped = 6;
1377     }
1378   else
1379     {
1380       /* For processors with load scheduling, it never costs more than
1381          2 cycles to load a constant, and the load scheduler may well
1382          reduce that to 1.  */
1383       if (arm_ld_sched)
1384         arm_constant_limit = 1;
1385
1386       /* On XScale the longer latency of a load makes it more difficult
1387          to achieve a good schedule, so it's faster to synthesize
1388          constants that can be done in two insns.  */
1389       if (arm_tune_xscale)
1390         arm_constant_limit = 2;
1391
1392       /* StrongARM has early execution of branches, so a sequence
1393          that is worth skipping is shorter.  */
1394       if (arm_tune_strongarm)
1395         max_insns_skipped = 3;
1396     }
1397
1398   /* Register global variables with the garbage collector.  */
1399   arm_add_gc_roots ();
1400 }
1401
1402 static void
1403 arm_add_gc_roots (void)
1404 {
1405   gcc_obstack_init(&minipool_obstack);
1406   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1407 }
1408 \f
1409 /* A table of known ARM exception types.
1410    For use with the interrupt function attribute.  */
1411
1412 typedef struct
1413 {
1414   const char *const arg;
1415   const unsigned long return_value;
1416 }
1417 isr_attribute_arg;
1418
1419 static const isr_attribute_arg isr_attribute_args [] =
1420 {
1421   { "IRQ",   ARM_FT_ISR },
1422   { "irq",   ARM_FT_ISR },
1423   { "FIQ",   ARM_FT_FIQ },
1424   { "fiq",   ARM_FT_FIQ },
1425   { "ABORT", ARM_FT_ISR },
1426   { "abort", ARM_FT_ISR },
1427   { "ABORT", ARM_FT_ISR },
1428   { "abort", ARM_FT_ISR },
1429   { "UNDEF", ARM_FT_EXCEPTION },
1430   { "undef", ARM_FT_EXCEPTION },
1431   { "SWI",   ARM_FT_EXCEPTION },
1432   { "swi",   ARM_FT_EXCEPTION },
1433   { NULL,    ARM_FT_NORMAL }
1434 };
1435
1436 /* Returns the (interrupt) function type of the current
1437    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1438
1439 static unsigned long
1440 arm_isr_value (tree argument)
1441 {
1442   const isr_attribute_arg * ptr;
1443   const char *              arg;
1444
1445   if (!arm_arch_notm)
1446     return ARM_FT_NORMAL | ARM_FT_STACKALIGN;
1447
1448   /* No argument - default to IRQ.  */
1449   if (argument == NULL_TREE)
1450     return ARM_FT_ISR;
1451
1452   /* Get the value of the argument.  */
1453   if (TREE_VALUE (argument) == NULL_TREE
1454       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1455     return ARM_FT_UNKNOWN;
1456
1457   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1458
1459   /* Check it against the list of known arguments.  */
1460   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1461     if (streq (arg, ptr->arg))
1462       return ptr->return_value;
1463
1464   /* An unrecognized interrupt type.  */
1465   return ARM_FT_UNKNOWN;
1466 }
1467
1468 /* Computes the type of the current function.  */
1469
1470 static unsigned long
1471 arm_compute_func_type (void)
1472 {
1473   unsigned long type = ARM_FT_UNKNOWN;
1474   tree a;
1475   tree attr;
1476
1477   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1478
1479   /* Decide if the current function is volatile.  Such functions
1480      never return, and many memory cycles can be saved by not storing
1481      register values that will never be needed again.  This optimization
1482      was added to speed up context switching in a kernel application.  */
1483   if (optimize > 0
1484       && (TREE_NOTHROW (current_function_decl)
1485           || !(flag_unwind_tables
1486                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1487       && TREE_THIS_VOLATILE (current_function_decl))
1488     type |= ARM_FT_VOLATILE;
1489
1490   if (cfun->static_chain_decl != NULL)
1491     type |= ARM_FT_NESTED;
1492
1493   attr = DECL_ATTRIBUTES (current_function_decl);
1494
1495   a = lookup_attribute ("naked", attr);
1496   if (a != NULL_TREE)
1497     type |= ARM_FT_NAKED;
1498
1499   a = lookup_attribute ("isr", attr);
1500   if (a == NULL_TREE)
1501     a = lookup_attribute ("interrupt", attr);
1502
1503   if (a == NULL_TREE)
1504     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1505   else
1506     type |= arm_isr_value (TREE_VALUE (a));
1507
1508   return type;
1509 }
1510
1511 /* Returns the type of the current function.  */
1512
1513 unsigned long
1514 arm_current_func_type (void)
1515 {
1516   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1517     cfun->machine->func_type = arm_compute_func_type ();
1518
1519   return cfun->machine->func_type;
1520 }
1521 \f
1522 /* Return 1 if it is possible to return using a single instruction.
1523    If SIBLING is non-null, this is a test for a return before a sibling
1524    call.  SIBLING is the call insn, so we can examine its register usage.  */
1525
1526 int
1527 use_return_insn (int iscond, rtx sibling)
1528 {
1529   int regno;
1530   unsigned int func_type;
1531   unsigned long saved_int_regs;
1532   unsigned HOST_WIDE_INT stack_adjust;
1533   arm_stack_offsets *offsets;
1534
1535   /* Never use a return instruction before reload has run.  */
1536   if (!reload_completed)
1537     return 0;
1538
1539   func_type = arm_current_func_type ();
1540
1541   /* Naked, volatile and stack alignment functions need special
1542      consideration.  */
1543   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED | ARM_FT_STACKALIGN))
1544     return 0;
1545
1546   /* So do interrupt functions that use the frame pointer and Thumb
1547      interrupt functions.  */
1548   if (IS_INTERRUPT (func_type) && (frame_pointer_needed || TARGET_THUMB))
1549     return 0;
1550
1551   offsets = arm_get_frame_offsets ();
1552   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1553
1554   /* As do variadic functions.  */
1555   if (current_function_pretend_args_size
1556       || cfun->machine->uses_anonymous_args
1557       /* Or if the function calls __builtin_eh_return () */
1558       || current_function_calls_eh_return
1559       /* Or if the function calls alloca */
1560       || current_function_calls_alloca
1561       /* Or if there is a stack adjustment.  However, if the stack pointer
1562          is saved on the stack, we can use a pre-incrementing stack load.  */
1563       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1564     return 0;
1565
1566   saved_int_regs = arm_compute_save_reg_mask ();
1567
1568   /* Unfortunately, the insn
1569
1570        ldmib sp, {..., sp, ...}
1571
1572      triggers a bug on most SA-110 based devices, such that the stack
1573      pointer won't be correctly restored if the instruction takes a
1574      page fault.  We work around this problem by popping r3 along with
1575      the other registers, since that is never slower than executing
1576      another instruction.
1577
1578      We test for !arm_arch5 here, because code for any architecture
1579      less than this could potentially be run on one of the buggy
1580      chips.  */
1581   if (stack_adjust == 4 && !arm_arch5 && TARGET_ARM)
1582     {
1583       /* Validate that r3 is a call-clobbered register (always true in
1584          the default abi) ...  */
1585       if (!call_used_regs[3])
1586         return 0;
1587
1588       /* ... that it isn't being used for a return value ... */
1589       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1590         return 0;
1591
1592       /* ... or for a tail-call argument ...  */
1593       if (sibling)
1594         {
1595           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1596
1597           if (find_regno_fusage (sibling, USE, 3))
1598             return 0;
1599         }
1600
1601       /* ... and that there are no call-saved registers in r0-r2
1602          (always true in the default ABI).  */
1603       if (saved_int_regs & 0x7)
1604         return 0;
1605     }
1606
1607   /* Can't be done if interworking with Thumb, and any registers have been
1608      stacked.  */
1609   if (TARGET_INTERWORK && saved_int_regs != 0 && !IS_INTERRUPT(func_type))
1610     return 0;
1611
1612   /* On StrongARM, conditional returns are expensive if they aren't
1613      taken and multiple registers have been stacked.  */
1614   if (iscond && arm_tune_strongarm)
1615     {
1616       /* Conditional return when just the LR is stored is a simple
1617          conditional-load instruction, that's not expensive.  */
1618       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1619         return 0;
1620
1621       if (flag_pic 
1622           && arm_pic_register != INVALID_REGNUM
1623           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1624         return 0;
1625     }
1626
1627   /* If there are saved registers but the LR isn't saved, then we need
1628      two instructions for the return.  */
1629   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1630     return 0;
1631
1632   /* Can't be done if any of the FPA regs are pushed,
1633      since this also requires an insn.  */
1634   if (TARGET_HARD_FLOAT && TARGET_FPA)
1635     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1636       if (regs_ever_live[regno] && !call_used_regs[regno])
1637         return 0;
1638
1639   /* Likewise VFP regs.  */
1640   if (TARGET_HARD_FLOAT && TARGET_VFP)
1641     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1642       if (regs_ever_live[regno] && !call_used_regs[regno])
1643         return 0;
1644
1645   if (TARGET_REALLY_IWMMXT)
1646     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1647       if (regs_ever_live[regno] && ! call_used_regs [regno])
1648         return 0;
1649
1650   return 1;
1651 }
1652
1653 /* Return TRUE if int I is a valid immediate ARM constant.  */
1654
1655 int
1656 const_ok_for_arm (HOST_WIDE_INT i)
1657 {
1658   int lowbit;
1659
1660   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1661      be all zero, or all one.  */
1662   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1663       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1664           != ((~(unsigned HOST_WIDE_INT) 0)
1665               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1666     return FALSE;
1667
1668   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1669
1670   /* Fast return for 0 and small values.  We must do this for zero, since
1671      the code below can't handle that one case.  */
1672   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1673     return TRUE;
1674
1675   /* Get the number of trailing zeros.  */
1676   lowbit = ffs((int) i) - 1;
1677   
1678   /* Only even shifts are allowed in ARM mode so round down to the
1679      nearest even number.  */
1680   if (TARGET_ARM)
1681     lowbit &= ~1;
1682
1683   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1684     return TRUE;
1685
1686   if (TARGET_ARM)
1687     {
1688       /* Allow rotated constants in ARM mode.  */
1689       if (lowbit <= 4
1690            && ((i & ~0xc000003f) == 0
1691                || (i & ~0xf000000f) == 0
1692                || (i & ~0xfc000003) == 0))
1693         return TRUE;
1694     }
1695   else
1696     {
1697       HOST_WIDE_INT v;
1698
1699       /* Allow repeated pattern.  */
1700       v = i & 0xff;
1701       v |= v << 16;
1702       if (i == v || i == (v | (v << 8)))
1703         return TRUE;
1704     }
1705
1706   return FALSE;
1707 }
1708
1709 /* Return true if I is a valid constant for the operation CODE.  */
1710 static int
1711 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1712 {
1713   if (const_ok_for_arm (i))
1714     return 1;
1715
1716   switch (code)
1717     {
1718     case PLUS:
1719       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1720
1721     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1722     case XOR:
1723     case IOR:
1724       return 0;
1725
1726     case AND:
1727       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1728
1729     default:
1730       gcc_unreachable ();
1731     }
1732 }
1733
1734 /* Emit a sequence of insns to handle a large constant.
1735    CODE is the code of the operation required, it can be any of SET, PLUS,
1736    IOR, AND, XOR, MINUS;
1737    MODE is the mode in which the operation is being performed;
1738    VAL is the integer to operate on;
1739    SOURCE is the other operand (a register, or a null-pointer for SET);
1740    SUBTARGETS means it is safe to create scratch registers if that will
1741    either produce a simpler sequence, or we will want to cse the values.
1742    Return value is the number of insns emitted.  */
1743
1744 /* ??? Tweak this for thumb2.  */
1745 int
1746 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1747                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1748 {
1749   rtx cond;
1750
1751   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1752     cond = COND_EXEC_TEST (PATTERN (insn));
1753   else
1754     cond = NULL_RTX;
1755
1756   if (subtargets || code == SET
1757       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1758           && REGNO (target) != REGNO (source)))
1759     {
1760       /* After arm_reorg has been called, we can't fix up expensive
1761          constants by pushing them into memory so we must synthesize
1762          them in-line, regardless of the cost.  This is only likely to
1763          be more costly on chips that have load delay slots and we are
1764          compiling without running the scheduler (so no splitting
1765          occurred before the final instruction emission).
1766
1767          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1768       */
1769       if (!after_arm_reorg
1770           && !cond
1771           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1772                                 1, 0)
1773               > arm_constant_limit + (code != SET)))
1774         {
1775           if (code == SET)
1776             {
1777               /* Currently SET is the only monadic value for CODE, all
1778                  the rest are diadic.  */
1779               emit_set_insn (target, GEN_INT (val));
1780               return 1;
1781             }
1782           else
1783             {
1784               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1785
1786               emit_set_insn (temp, GEN_INT (val));
1787               /* For MINUS, the value is subtracted from, since we never
1788                  have subtraction of a constant.  */
1789               if (code == MINUS)
1790                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1791               else
1792                 emit_set_insn (target,
1793                                gen_rtx_fmt_ee (code, mode, source, temp));
1794               return 2;
1795             }
1796         }
1797     }
1798
1799   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1800                            1);
1801 }
1802
1803 /* Return the number of ARM instructions required to synthesize the given
1804    constant.  */
1805 static int
1806 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1807 {
1808   HOST_WIDE_INT temp1;
1809   int num_insns = 0;
1810   do
1811     {
1812       int end;
1813
1814       if (i <= 0)
1815         i += 32;
1816       if (remainder & (3 << (i - 2)))
1817         {
1818           end = i - 8;
1819           if (end < 0)
1820             end += 32;
1821           temp1 = remainder & ((0x0ff << end)
1822                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1823           remainder &= ~temp1;
1824           num_insns++;
1825           i -= 6;
1826         }
1827       i -= 2;
1828     } while (remainder);
1829   return num_insns;
1830 }
1831
1832 /* Emit an instruction with the indicated PATTERN.  If COND is
1833    non-NULL, conditionalize the execution of the instruction on COND
1834    being true.  */
1835
1836 static void
1837 emit_constant_insn (rtx cond, rtx pattern)
1838 {
1839   if (cond)
1840     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1841   emit_insn (pattern);
1842 }
1843
1844 /* As above, but extra parameter GENERATE which, if clear, suppresses
1845    RTL generation.  */
1846 /* ??? This needs more work for thumb2.  */
1847
1848 static int
1849 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1850                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1851                   int generate)
1852 {
1853   int can_invert = 0;
1854   int can_negate = 0;
1855   int can_negate_initial = 0;
1856   int can_shift = 0;
1857   int i;
1858   int num_bits_set = 0;
1859   int set_sign_bit_copies = 0;
1860   int clear_sign_bit_copies = 0;
1861   int clear_zero_bit_copies = 0;
1862   int set_zero_bit_copies = 0;
1863   int insns = 0;
1864   unsigned HOST_WIDE_INT temp1, temp2;
1865   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1866
1867   /* Find out which operations are safe for a given CODE.  Also do a quick
1868      check for degenerate cases; these can occur when DImode operations
1869      are split.  */
1870   switch (code)
1871     {
1872     case SET:
1873       can_invert = 1;
1874       can_shift = 1;
1875       can_negate = 1;
1876       break;
1877
1878     case PLUS:
1879       can_negate = 1;
1880       can_negate_initial = 1;
1881       break;
1882
1883     case IOR:
1884       if (remainder == 0xffffffff)
1885         {
1886           if (generate)
1887             emit_constant_insn (cond,
1888                                 gen_rtx_SET (VOIDmode, target,
1889                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1890           return 1;
1891         }
1892       if (remainder == 0)
1893         {
1894           if (reload_completed && rtx_equal_p (target, source))
1895             return 0;
1896           if (generate)
1897             emit_constant_insn (cond,
1898                                 gen_rtx_SET (VOIDmode, target, source));
1899           return 1;
1900         }
1901       break;
1902
1903     case AND:
1904       if (remainder == 0)
1905         {
1906           if (generate)
1907             emit_constant_insn (cond,
1908                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1909           return 1;
1910         }
1911       if (remainder == 0xffffffff)
1912         {
1913           if (reload_completed && rtx_equal_p (target, source))
1914             return 0;
1915           if (generate)
1916             emit_constant_insn (cond,
1917                                 gen_rtx_SET (VOIDmode, target, source));
1918           return 1;
1919         }
1920       can_invert = 1;
1921       break;
1922
1923     case XOR:
1924       if (remainder == 0)
1925         {
1926           if (reload_completed && rtx_equal_p (target, source))
1927             return 0;
1928           if (generate)
1929             emit_constant_insn (cond,
1930                                 gen_rtx_SET (VOIDmode, target, source));
1931           return 1;
1932         }
1933
1934       /* We don't know how to handle other cases yet.  */
1935       gcc_assert (remainder == 0xffffffff);
1936
1937       if (generate)
1938         emit_constant_insn (cond,
1939                             gen_rtx_SET (VOIDmode, target,
1940                                          gen_rtx_NOT (mode, source)));
1941       return 1;
1942
1943     case MINUS:
1944       /* We treat MINUS as (val - source), since (source - val) is always
1945          passed as (source + (-val)).  */
1946       if (remainder == 0)
1947         {
1948           if (generate)
1949             emit_constant_insn (cond,
1950                                 gen_rtx_SET (VOIDmode, target,
1951                                              gen_rtx_NEG (mode, source)));
1952           return 1;
1953         }
1954       if (const_ok_for_arm (val))
1955         {
1956           if (generate)
1957             emit_constant_insn (cond,
1958                                 gen_rtx_SET (VOIDmode, target,
1959                                              gen_rtx_MINUS (mode, GEN_INT (val),
1960                                                             source)));
1961           return 1;
1962         }
1963       can_negate = 1;
1964
1965       break;
1966
1967     default:
1968       gcc_unreachable ();
1969     }
1970
1971   /* If we can do it in one insn get out quickly.  */
1972   if (const_ok_for_arm (val)
1973       || (can_negate_initial && const_ok_for_arm (-val))
1974       || (can_invert && const_ok_for_arm (~val)))
1975     {
1976       if (generate)
1977         emit_constant_insn (cond,
1978                             gen_rtx_SET (VOIDmode, target,
1979                                          (source
1980                                           ? gen_rtx_fmt_ee (code, mode, source,
1981                                                             GEN_INT (val))
1982                                           : GEN_INT (val))));
1983       return 1;
1984     }
1985
1986   /* Calculate a few attributes that may be useful for specific
1987      optimizations.  */
1988   for (i = 31; i >= 0; i--)
1989     {
1990       if ((remainder & (1 << i)) == 0)
1991         clear_sign_bit_copies++;
1992       else
1993         break;
1994     }
1995
1996   for (i = 31; i >= 0; i--)
1997     {
1998       if ((remainder & (1 << i)) != 0)
1999         set_sign_bit_copies++;
2000       else
2001         break;
2002     }
2003
2004   for (i = 0; i <= 31; i++)
2005     {
2006       if ((remainder & (1 << i)) == 0)
2007         clear_zero_bit_copies++;
2008       else
2009         break;
2010     }
2011
2012   for (i = 0; i <= 31; i++)
2013     {
2014       if ((remainder & (1 << i)) != 0)
2015         set_zero_bit_copies++;
2016       else
2017         break;
2018     }
2019
2020   switch (code)
2021     {
2022     case SET:
2023       /* See if we can use movw.  */
2024       if (arm_arch_thumb2 && (remainder & 0xffff0000) == 0)
2025         {
2026           if (generate)
2027             emit_constant_insn (cond, gen_rtx_SET (VOIDmode, target,
2028                                                    GEN_INT (val)));
2029           return 1;
2030         }
2031
2032       /* See if we can do this by sign_extending a constant that is known
2033          to be negative.  This is a good, way of doing it, since the shift
2034          may well merge into a subsequent insn.  */
2035       if (set_sign_bit_copies > 1)
2036         {
2037           if (const_ok_for_arm
2038               (temp1 = ARM_SIGN_EXTEND (remainder
2039                                         << (set_sign_bit_copies - 1))))
2040             {
2041               if (generate)
2042                 {
2043                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2044                   emit_constant_insn (cond,
2045                                       gen_rtx_SET (VOIDmode, new_src,
2046                                                    GEN_INT (temp1)));
2047                   emit_constant_insn (cond,
2048                                       gen_ashrsi3 (target, new_src,
2049                                                    GEN_INT (set_sign_bit_copies - 1)));
2050                 }
2051               return 2;
2052             }
2053           /* For an inverted constant, we will need to set the low bits,
2054              these will be shifted out of harm's way.  */
2055           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
2056           if (const_ok_for_arm (~temp1))
2057             {
2058               if (generate)
2059                 {
2060                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2061                   emit_constant_insn (cond,
2062                                       gen_rtx_SET (VOIDmode, new_src,
2063                                                    GEN_INT (temp1)));
2064                   emit_constant_insn (cond,
2065                                       gen_ashrsi3 (target, new_src,
2066                                                    GEN_INT (set_sign_bit_copies - 1)));
2067                 }
2068               return 2;
2069             }
2070         }
2071
2072       /* See if we can calculate the value as the difference between two
2073          valid immediates.  */
2074       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
2075         {
2076           int topshift = clear_sign_bit_copies & ~1;
2077
2078           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
2079                                    & (0xff000000 >> topshift));
2080
2081           /* If temp1 is zero, then that means the 9 most significant
2082              bits of remainder were 1 and we've caused it to overflow.
2083              When topshift is 0 we don't need to do anything since we
2084              can borrow from 'bit 32'.  */
2085           if (temp1 == 0 && topshift != 0)
2086             temp1 = 0x80000000 >> (topshift - 1);
2087
2088           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
2089
2090           if (const_ok_for_arm (temp2))
2091             {
2092               if (generate)
2093                 {
2094                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2095                   emit_constant_insn (cond,
2096                                       gen_rtx_SET (VOIDmode, new_src,
2097                                                    GEN_INT (temp1)));
2098                   emit_constant_insn (cond,
2099                                       gen_addsi3 (target, new_src,
2100                                                   GEN_INT (-temp2)));
2101                 }
2102
2103               return 2;
2104             }
2105         }
2106
2107       /* See if we can generate this by setting the bottom (or the top)
2108          16 bits, and then shifting these into the other half of the
2109          word.  We only look for the simplest cases, to do more would cost
2110          too much.  Be careful, however, not to generate this when the
2111          alternative would take fewer insns.  */
2112       if (val & 0xffff0000)
2113         {
2114           temp1 = remainder & 0xffff0000;
2115           temp2 = remainder & 0x0000ffff;
2116
2117           /* Overlaps outside this range are best done using other methods.  */
2118           for (i = 9; i < 24; i++)
2119             {
2120               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2121                   && !const_ok_for_arm (temp2))
2122                 {
2123                   rtx new_src = (subtargets
2124                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2125                                  : target);
2126                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2127                                             source, subtargets, generate);
2128                   source = new_src;
2129                   if (generate)
2130                     emit_constant_insn
2131                       (cond,
2132                        gen_rtx_SET
2133                        (VOIDmode, target,
2134                         gen_rtx_IOR (mode,
2135                                      gen_rtx_ASHIFT (mode, source,
2136                                                      GEN_INT (i)),
2137                                      source)));
2138                   return insns + 1;
2139                 }
2140             }
2141
2142           /* Don't duplicate cases already considered.  */
2143           for (i = 17; i < 24; i++)
2144             {
2145               if (((temp1 | (temp1 >> i)) == remainder)
2146                   && !const_ok_for_arm (temp1))
2147                 {
2148                   rtx new_src = (subtargets
2149                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2150                                  : target);
2151                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2152                                             source, subtargets, generate);
2153                   source = new_src;
2154                   if (generate)
2155                     emit_constant_insn
2156                       (cond,
2157                        gen_rtx_SET (VOIDmode, target,
2158                                     gen_rtx_IOR
2159                                     (mode,
2160                                      gen_rtx_LSHIFTRT (mode, source,
2161                                                        GEN_INT (i)),
2162                                      source)));
2163                   return insns + 1;
2164                 }
2165             }
2166         }
2167       break;
2168
2169     case IOR:
2170     case XOR:
2171       /* If we have IOR or XOR, and the constant can be loaded in a
2172          single instruction, and we can find a temporary to put it in,
2173          then this can be done in two instructions instead of 3-4.  */
2174       if (subtargets
2175           /* TARGET can't be NULL if SUBTARGETS is 0 */
2176           || (reload_completed && !reg_mentioned_p (target, source)))
2177         {
2178           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2179             {
2180               if (generate)
2181                 {
2182                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2183
2184                   emit_constant_insn (cond,
2185                                       gen_rtx_SET (VOIDmode, sub,
2186                                                    GEN_INT (val)));
2187                   emit_constant_insn (cond,
2188                                       gen_rtx_SET (VOIDmode, target,
2189                                                    gen_rtx_fmt_ee (code, mode,
2190                                                                    source, sub)));
2191                 }
2192               return 2;
2193             }
2194         }
2195
2196       if (code == XOR)
2197         break;
2198
2199       if (set_sign_bit_copies > 8
2200           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2201         {
2202           if (generate)
2203             {
2204               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2205               rtx shift = GEN_INT (set_sign_bit_copies);
2206
2207               emit_constant_insn
2208                 (cond,
2209                  gen_rtx_SET (VOIDmode, sub,
2210                               gen_rtx_NOT (mode,
2211                                            gen_rtx_ASHIFT (mode,
2212                                                            source,
2213                                                            shift))));
2214               emit_constant_insn
2215                 (cond,
2216                  gen_rtx_SET (VOIDmode, target,
2217                               gen_rtx_NOT (mode,
2218                                            gen_rtx_LSHIFTRT (mode, sub,
2219                                                              shift))));
2220             }
2221           return 2;
2222         }
2223
2224       if (set_zero_bit_copies > 8
2225           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2226         {
2227           if (generate)
2228             {
2229               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2230               rtx shift = GEN_INT (set_zero_bit_copies);
2231
2232               emit_constant_insn
2233                 (cond,
2234                  gen_rtx_SET (VOIDmode, sub,
2235                               gen_rtx_NOT (mode,
2236                                            gen_rtx_LSHIFTRT (mode,
2237                                                              source,
2238                                                              shift))));
2239               emit_constant_insn
2240                 (cond,
2241                  gen_rtx_SET (VOIDmode, target,
2242                               gen_rtx_NOT (mode,
2243                                            gen_rtx_ASHIFT (mode, sub,
2244                                                            shift))));
2245             }
2246           return 2;
2247         }
2248
2249       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2250         {
2251           if (generate)
2252             {
2253               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2254               emit_constant_insn (cond,
2255                                   gen_rtx_SET (VOIDmode, sub,
2256                                                gen_rtx_NOT (mode, source)));
2257               source = sub;
2258               if (subtargets)
2259                 sub = gen_reg_rtx (mode);
2260               emit_constant_insn (cond,
2261                                   gen_rtx_SET (VOIDmode, sub,
2262                                                gen_rtx_AND (mode, source,
2263                                                             GEN_INT (temp1))));
2264               emit_constant_insn (cond,
2265                                   gen_rtx_SET (VOIDmode, target,
2266                                                gen_rtx_NOT (mode, sub)));
2267             }
2268           return 3;
2269         }
2270       break;
2271
2272     case AND:
2273       /* See if two shifts will do 2 or more insn's worth of work.  */
2274       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2275         {
2276           HOST_WIDE_INT shift_mask = ((0xffffffff
2277                                        << (32 - clear_sign_bit_copies))
2278                                       & 0xffffffff);
2279
2280           if ((remainder | shift_mask) != 0xffffffff)
2281             {
2282               if (generate)
2283                 {
2284                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2285                   insns = arm_gen_constant (AND, mode, cond,
2286                                             remainder | shift_mask,
2287                                             new_src, source, subtargets, 1);
2288                   source = new_src;
2289                 }
2290               else
2291                 {
2292                   rtx targ = subtargets ? NULL_RTX : target;
2293                   insns = arm_gen_constant (AND, mode, cond,
2294                                             remainder | shift_mask,
2295                                             targ, source, subtargets, 0);
2296                 }
2297             }
2298
2299           if (generate)
2300             {
2301               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2302               rtx shift = GEN_INT (clear_sign_bit_copies);
2303
2304               emit_insn (gen_ashlsi3 (new_src, source, shift));
2305               emit_insn (gen_lshrsi3 (target, new_src, shift));
2306             }
2307
2308           return insns + 2;
2309         }
2310
2311       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2312         {
2313           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2314
2315           if ((remainder | shift_mask) != 0xffffffff)
2316             {
2317               if (generate)
2318                 {
2319                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2320
2321                   insns = arm_gen_constant (AND, mode, cond,
2322                                             remainder | shift_mask,
2323                                             new_src, source, subtargets, 1);
2324                   source = new_src;
2325                 }
2326               else
2327                 {
2328                   rtx targ = subtargets ? NULL_RTX : target;
2329
2330                   insns = arm_gen_constant (AND, mode, cond,
2331                                             remainder | shift_mask,
2332                                             targ, source, subtargets, 0);
2333                 }
2334             }
2335
2336           if (generate)
2337             {
2338               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2339               rtx shift = GEN_INT (clear_zero_bit_copies);
2340
2341               emit_insn (gen_lshrsi3 (new_src, source, shift));
2342               emit_insn (gen_ashlsi3 (target, new_src, shift));
2343             }
2344
2345           return insns + 2;
2346         }
2347
2348       break;
2349
2350     default:
2351       break;
2352     }
2353
2354   for (i = 0; i < 32; i++)
2355     if (remainder & (1 << i))
2356       num_bits_set++;
2357
2358   if (code == AND || (can_invert && num_bits_set > 16))
2359     remainder = (~remainder) & 0xffffffff;
2360   else if (code == PLUS && num_bits_set > 16)
2361     remainder = (-remainder) & 0xffffffff;
2362   else
2363     {
2364       can_invert = 0;
2365       can_negate = 0;
2366     }
2367
2368   /* Now try and find a way of doing the job in either two or three
2369      instructions.
2370      We start by looking for the largest block of zeros that are aligned on
2371      a 2-bit boundary, we then fill up the temps, wrapping around to the
2372      top of the word when we drop off the bottom.
2373      In the worst case this code should produce no more than four insns.
2374      Thumb-2 constants are shifted, not rotated, so the MSB is always the
2375      best place to start.  */
2376
2377   /* ??? Use thumb2 replicated constants when the high and low halfwords are
2378      the same.  */
2379   {
2380     int best_start = 0;
2381     if (!TARGET_THUMB2)
2382       {
2383         int best_consecutive_zeros = 0;
2384
2385         for (i = 0; i < 32; i += 2)
2386           {
2387             int consecutive_zeros = 0;
2388
2389             if (!(remainder & (3 << i)))
2390               {
2391                 while ((i < 32) && !(remainder & (3 << i)))
2392                   {
2393                     consecutive_zeros += 2;
2394                     i += 2;
2395                   }
2396                 if (consecutive_zeros > best_consecutive_zeros)
2397                   {
2398                     best_consecutive_zeros = consecutive_zeros;
2399                     best_start = i - consecutive_zeros;
2400                   }
2401                 i -= 2;
2402               }
2403           }
2404
2405         /* So long as it won't require any more insns to do so, it's
2406            desirable to emit a small constant (in bits 0...9) in the last
2407            insn.  This way there is more chance that it can be combined with
2408            a later addressing insn to form a pre-indexed load or store
2409            operation.  Consider:
2410
2411                    *((volatile int *)0xe0000100) = 1;
2412                    *((volatile int *)0xe0000110) = 2;
2413
2414            We want this to wind up as:
2415
2416                     mov rA, #0xe0000000
2417                     mov rB, #1
2418                     str rB, [rA, #0x100]
2419                     mov rB, #2
2420                     str rB, [rA, #0x110]
2421
2422            rather than having to synthesize both large constants from scratch.
2423
2424            Therefore, we calculate how many insns would be required to emit
2425            the constant starting from `best_start', and also starting from
2426            zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2427            yield a shorter sequence, we may as well use zero.  */
2428         if (best_start != 0
2429             && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2430             && (count_insns_for_constant (remainder, 0) <=
2431                 count_insns_for_constant (remainder, best_start)))
2432           best_start = 0;
2433       }
2434
2435     /* Now start emitting the insns.  */
2436     i = best_start;
2437     do
2438       {
2439         int end;
2440
2441         if (i <= 0)
2442           i += 32;
2443         if (remainder & (3 << (i - 2)))
2444           {
2445             end = i - 8;
2446             if (end < 0)
2447               end += 32;
2448             temp1 = remainder & ((0x0ff << end)
2449                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2450             remainder &= ~temp1;
2451
2452             if (generate)
2453               {
2454                 rtx new_src, temp1_rtx;
2455
2456                 if (code == SET || code == MINUS)
2457                   {
2458                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2459                     if (can_invert && code != MINUS)
2460                       temp1 = ~temp1;
2461                   }
2462                 else
2463                   {
2464                     if (remainder && subtargets)
2465                       new_src = gen_reg_rtx (mode);
2466                     else
2467                       new_src = target;
2468                     if (can_invert)
2469                       temp1 = ~temp1;
2470                     else if (can_negate)
2471                       temp1 = -temp1;
2472                   }
2473
2474                 temp1 = trunc_int_for_mode (temp1, mode);
2475                 temp1_rtx = GEN_INT (temp1);
2476
2477                 if (code == SET)
2478                   ;
2479                 else if (code == MINUS)
2480                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2481                 else
2482                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2483
2484                 emit_constant_insn (cond,
2485                                     gen_rtx_SET (VOIDmode, new_src,
2486                                                  temp1_rtx));
2487                 source = new_src;
2488               }
2489
2490             if (code == SET)
2491               {
2492                 can_invert = 0;
2493                 code = PLUS;
2494               }
2495             else if (code == MINUS)
2496               code = PLUS;
2497
2498             insns++;
2499             if (TARGET_ARM)
2500               i -= 6;
2501             else
2502               i -= 7;
2503           }
2504         /* Arm allows rotates by a multiple of two. Thumb-2 allows arbitrary
2505            shifts.  */
2506         if (TARGET_ARM)
2507           i -= 2;
2508         else
2509           i--;
2510       }
2511     while (remainder);
2512   }
2513
2514   return insns;
2515 }
2516
2517 /* Canonicalize a comparison so that we are more likely to recognize it.
2518    This can be done for a few constant compares, where we can make the
2519    immediate value easier to load.  */
2520
2521 enum rtx_code
2522 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2523                              rtx * op1)
2524 {
2525   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2526   unsigned HOST_WIDE_INT maxval;
2527   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2528
2529   switch (code)
2530     {
2531     case EQ:
2532     case NE:
2533       return code;
2534
2535     case GT:
2536     case LE:
2537       if (i != maxval
2538           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2539         {
2540           *op1 = GEN_INT (i + 1);
2541           return code == GT ? GE : LT;
2542         }
2543       break;
2544
2545     case GE:
2546     case LT:
2547       if (i != ~maxval
2548           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2549         {
2550           *op1 = GEN_INT (i - 1);
2551           return code == GE ? GT : LE;
2552         }
2553       break;
2554
2555     case GTU:
2556     case LEU:
2557       if (i != ~((unsigned HOST_WIDE_INT) 0)
2558           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2559         {
2560           *op1 = GEN_INT (i + 1);
2561           return code == GTU ? GEU : LTU;
2562         }
2563       break;
2564
2565     case GEU:
2566     case LTU:
2567       if (i != 0
2568           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2569         {
2570           *op1 = GEN_INT (i - 1);
2571           return code == GEU ? GTU : LEU;
2572         }
2573       break;
2574
2575     default:
2576       gcc_unreachable ();
2577     }
2578
2579   return code;
2580 }
2581
2582
2583 /* Define how to find the value returned by a function.  */
2584
2585 rtx
2586 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2587 {
2588   enum machine_mode mode;
2589   int unsignedp ATTRIBUTE_UNUSED;
2590   rtx r ATTRIBUTE_UNUSED;
2591
2592   mode = TYPE_MODE (type);
2593   /* Promote integer types.  */
2594   if (INTEGRAL_TYPE_P (type))
2595     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2596
2597   /* Promotes small structs returned in a register to full-word size
2598      for big-endian AAPCS.  */
2599   if (arm_return_in_msb (type))
2600     {
2601       HOST_WIDE_INT size = int_size_in_bytes (type);
2602       if (size % UNITS_PER_WORD != 0)
2603         {
2604           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2605           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2606         }
2607     }
2608
2609   return LIBCALL_VALUE(mode);
2610 }
2611
2612 /* Determine the amount of memory needed to store the possible return
2613    registers of an untyped call.  */
2614 int
2615 arm_apply_result_size (void)
2616 {
2617   int size = 16;
2618
2619   if (TARGET_ARM)
2620     {
2621       if (TARGET_HARD_FLOAT_ABI)
2622         {
2623           if (TARGET_FPA)
2624             size += 12;
2625           if (TARGET_MAVERICK)
2626             size += 8;
2627         }
2628       if (TARGET_IWMMXT_ABI)
2629         size += 8;
2630     }
2631
2632   return size;
2633 }
2634
2635 /* Decide whether a type should be returned in memory (true)
2636    or in a register (false).  This is called by the macro
2637    RETURN_IN_MEMORY.  */
2638 int
2639 arm_return_in_memory (tree type)
2640 {
2641   HOST_WIDE_INT size;
2642
2643   if (!AGGREGATE_TYPE_P (type) &&
2644       (TREE_CODE (type) != VECTOR_TYPE) &&
2645       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2646     /* All simple types are returned in registers.
2647        For AAPCS, complex types are treated the same as aggregates.  */
2648     return 0;
2649
2650   size = int_size_in_bytes (type);
2651
2652   if (arm_abi != ARM_ABI_APCS)
2653     {
2654       /* ATPCS and later return aggregate types in memory only if they are
2655          larger than a word (or are variable size).  */
2656       return (size < 0 || size > UNITS_PER_WORD);
2657     }
2658
2659   /* To maximize backwards compatibility with previous versions of gcc,
2660      return vectors up to 4 words in registers.  */
2661   if (TREE_CODE (type) == VECTOR_TYPE)
2662     return (size < 0 || size > (4 * UNITS_PER_WORD));
2663
2664   /* For the arm-wince targets we choose to be compatible with Microsoft's
2665      ARM and Thumb compilers, which always return aggregates in memory.  */
2666 #ifndef ARM_WINCE
2667   /* All structures/unions bigger than one word are returned in memory.
2668      Also catch the case where int_size_in_bytes returns -1.  In this case
2669      the aggregate is either huge or of variable size, and in either case
2670      we will want to return it via memory and not in a register.  */
2671   if (size < 0 || size > UNITS_PER_WORD)
2672     return 1;
2673
2674   if (TREE_CODE (type) == RECORD_TYPE)
2675     {
2676       tree field;
2677
2678       /* For a struct the APCS says that we only return in a register
2679          if the type is 'integer like' and every addressable element
2680          has an offset of zero.  For practical purposes this means
2681          that the structure can have at most one non bit-field element
2682          and that this element must be the first one in the structure.  */
2683
2684       /* Find the first field, ignoring non FIELD_DECL things which will
2685          have been created by C++.  */
2686       for (field = TYPE_FIELDS (type);
2687            field && TREE_CODE (field) != FIELD_DECL;
2688            field = TREE_CHAIN (field))
2689         continue;
2690
2691       if (field == NULL)
2692         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2693
2694       /* Check that the first field is valid for returning in a register.  */
2695
2696       /* ... Floats are not allowed */
2697       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2698         return 1;
2699
2700       /* ... Aggregates that are not themselves valid for returning in
2701          a register are not allowed.  */
2702       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2703         return 1;
2704
2705       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2706          since they are not addressable.  */
2707       for (field = TREE_CHAIN (field);
2708            field;
2709            field = TREE_CHAIN (field))
2710         {
2711           if (TREE_CODE (field) != FIELD_DECL)
2712             continue;
2713
2714           if (!DECL_BIT_FIELD_TYPE (field))
2715             return 1;
2716         }
2717
2718       return 0;
2719     }
2720
2721   if (TREE_CODE (type) == UNION_TYPE)
2722     {
2723       tree field;
2724
2725       /* Unions can be returned in registers if every element is
2726          integral, or can be returned in an integer register.  */
2727       for (field = TYPE_FIELDS (type);
2728            field;
2729            field = TREE_CHAIN (field))
2730         {
2731           if (TREE_CODE (field) != FIELD_DECL)
2732             continue;
2733
2734           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2735             return 1;
2736
2737           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2738             return 1;
2739         }
2740
2741       return 0;
2742     }
2743 #endif /* not ARM_WINCE */
2744
2745   /* Return all other types in memory.  */
2746   return 1;
2747 }
2748
2749 /* Indicate whether or not words of a double are in big-endian order.  */
2750
2751 int
2752 arm_float_words_big_endian (void)
2753 {
2754   if (TARGET_MAVERICK)
2755     return 0;
2756
2757   /* For FPA, float words are always big-endian.  For VFP, floats words
2758      follow the memory system mode.  */
2759
2760   if (TARGET_FPA)
2761     {
2762       return 1;
2763     }
2764
2765   if (TARGET_VFP)
2766     return (TARGET_BIG_END ? 1 : 0);
2767
2768   return 1;
2769 }
2770
2771 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2772    for a call to a function whose data type is FNTYPE.
2773    For a library call, FNTYPE is NULL.  */
2774 void
2775 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2776                           rtx libname  ATTRIBUTE_UNUSED,
2777                           tree fndecl ATTRIBUTE_UNUSED)
2778 {
2779   /* On the ARM, the offset starts at 0.  */
2780   pcum->nregs = 0;
2781   pcum->iwmmxt_nregs = 0;
2782   pcum->can_split = true;
2783
2784   /* Varargs vectors are treated the same as long long.
2785      named_count avoids having to change the way arm handles 'named' */
2786   pcum->named_count = 0;
2787   pcum->nargs = 0;
2788
2789   if (TARGET_REALLY_IWMMXT && fntype)
2790     {
2791       tree fn_arg;
2792
2793       for (fn_arg = TYPE_ARG_TYPES (fntype);
2794            fn_arg;
2795            fn_arg = TREE_CHAIN (fn_arg))
2796         pcum->named_count += 1;
2797
2798       if (! pcum->named_count)
2799         pcum->named_count = INT_MAX;
2800     }
2801 }
2802
2803
2804 /* Return true if mode/type need doubleword alignment.  */
2805 bool
2806 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2807 {
2808   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2809           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2810 }
2811
2812
2813 /* Determine where to put an argument to a function.
2814    Value is zero to push the argument on the stack,
2815    or a hard register in which to store the argument.
2816
2817    MODE is the argument's machine mode.
2818    TYPE is the data type of the argument (as a tree).
2819     This is null for libcalls where that information may
2820     not be available.
2821    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2822     the preceding args and about the function being called.
2823    NAMED is nonzero if this argument is a named parameter
2824     (otherwise it is an extra parameter matching an ellipsis).  */
2825
2826 rtx
2827 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2828                   tree type, int named)
2829 {
2830   int nregs;
2831
2832   /* Varargs vectors are treated the same as long long.
2833      named_count avoids having to change the way arm handles 'named' */
2834   if (TARGET_IWMMXT_ABI
2835       && arm_vector_mode_supported_p (mode)
2836       && pcum->named_count > pcum->nargs + 1)
2837     {
2838       if (pcum->iwmmxt_nregs <= 9)
2839         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2840       else
2841         {
2842           pcum->can_split = false;
2843           return NULL_RTX;
2844         }
2845     }
2846
2847   /* Put doubleword aligned quantities in even register pairs.  */
2848   if (pcum->nregs & 1
2849       && ARM_DOUBLEWORD_ALIGN
2850       && arm_needs_doubleword_align (mode, type))
2851     pcum->nregs++;
2852
2853   if (mode == VOIDmode)
2854     /* Pick an arbitrary value for operand 2 of the call insn.  */
2855     return const0_rtx;
2856
2857   /* Only allow splitting an arg between regs and memory if all preceding
2858      args were allocated to regs.  For args passed by reference we only count
2859      the reference pointer.  */
2860   if (pcum->can_split)
2861     nregs = 1;
2862   else
2863     nregs = ARM_NUM_REGS2 (mode, type);
2864
2865   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2866     return NULL_RTX;
2867
2868   return gen_rtx_REG (mode, pcum->nregs);
2869 }
2870
2871 static int
2872 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2873                        tree type, bool named ATTRIBUTE_UNUSED)
2874 {
2875   int nregs = pcum->nregs;
2876
2877   if (arm_vector_mode_supported_p (mode))
2878     return 0;
2879
2880   if (NUM_ARG_REGS > nregs
2881       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2882       && pcum->can_split)
2883     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2884
2885   return 0;
2886 }
2887
2888 /* Variable sized types are passed by reference.  This is a GCC
2889    extension to the ARM ABI.  */
2890
2891 static bool
2892 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2893                        enum machine_mode mode ATTRIBUTE_UNUSED,
2894                        tree type, bool named ATTRIBUTE_UNUSED)
2895 {
2896   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2897 }
2898 \f
2899 /* Encode the current state of the #pragma [no_]long_calls.  */
2900 typedef enum
2901 {
2902   OFF,          /* No #pragma [no_]long_calls is in effect.  */
2903   LONG,         /* #pragma long_calls is in effect.  */
2904   SHORT         /* #pragma no_long_calls is in effect.  */
2905 } arm_pragma_enum;
2906
2907 static arm_pragma_enum arm_pragma_long_calls = OFF;
2908
2909 void
2910 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2911 {
2912   arm_pragma_long_calls = LONG;
2913 }
2914
2915 void
2916 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2917 {
2918   arm_pragma_long_calls = SHORT;
2919 }
2920
2921 void
2922 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2923 {
2924   arm_pragma_long_calls = OFF;
2925 }
2926 \f
2927 /* Table of machine attributes.  */
2928 const struct attribute_spec arm_attribute_table[] =
2929 {
2930   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2931   /* Function calls made to this symbol must be done indirectly, because
2932      it may lie outside of the 26 bit addressing range of a normal function
2933      call.  */
2934   { "long_call",    0, 0, false, true,  true,  NULL },
2935   /* Whereas these functions are always known to reside within the 26 bit
2936      addressing range.  */
2937   { "short_call",   0, 0, false, true,  true,  NULL },
2938   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2939   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2940   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2941   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2942 #ifdef ARM_PE
2943   /* ARM/PE has three new attributes:
2944      interfacearm - ?
2945      dllexport - for exporting a function/variable that will live in a dll
2946      dllimport - for importing a function/variable from a dll
2947
2948      Microsoft allows multiple declspecs in one __declspec, separating
2949      them with spaces.  We do NOT support this.  Instead, use __declspec
2950      multiple times.
2951   */
2952   { "dllimport",    0, 0, true,  false, false, NULL },
2953   { "dllexport",    0, 0, true,  false, false, NULL },
2954   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2955 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2956   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2957   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2958   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2959 #endif
2960   { NULL,           0, 0, false, false, false, NULL }
2961 };
2962
2963 /* Handle an attribute requiring a FUNCTION_DECL;
2964    arguments as in struct attribute_spec.handler.  */
2965 static tree
2966 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2967                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2968 {
2969   if (TREE_CODE (*node) != FUNCTION_DECL)
2970     {
2971       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2972                IDENTIFIER_POINTER (name));
2973       *no_add_attrs = true;
2974     }
2975
2976   return NULL_TREE;
2977 }
2978
2979 /* Handle an "interrupt" or "isr" attribute;
2980    arguments as in struct attribute_spec.handler.  */
2981 static tree
2982 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2983                           bool *no_add_attrs)
2984 {
2985   if (DECL_P (*node))
2986     {
2987       if (TREE_CODE (*node) != FUNCTION_DECL)
2988         {
2989           warning (OPT_Wattributes, "%qs attribute only applies to functions",
2990                    IDENTIFIER_POINTER (name));
2991           *no_add_attrs = true;
2992         }
2993       /* FIXME: the argument if any is checked for type attributes;
2994          should it be checked for decl ones?  */
2995     }
2996   else
2997     {
2998       if (TREE_CODE (*node) == FUNCTION_TYPE
2999           || TREE_CODE (*node) == METHOD_TYPE)
3000         {
3001           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
3002             {
3003               warning (OPT_Wattributes, "%qs attribute ignored",
3004                        IDENTIFIER_POINTER (name));
3005               *no_add_attrs = true;
3006             }
3007         }
3008       else if (TREE_CODE (*node) == POINTER_TYPE
3009                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
3010                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
3011                && arm_isr_value (args) != ARM_FT_UNKNOWN)
3012         {
3013           *node = build_variant_type_copy (*node);
3014           TREE_TYPE (*node) = build_type_attribute_variant
3015             (TREE_TYPE (*node),
3016              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
3017           *no_add_attrs = true;
3018         }
3019       else
3020         {
3021           /* Possibly pass this attribute on from the type to a decl.  */
3022           if (flags & ((int) ATTR_FLAG_DECL_NEXT
3023                        | (int) ATTR_FLAG_FUNCTION_NEXT
3024                        | (int) ATTR_FLAG_ARRAY_NEXT))
3025             {
3026               *no_add_attrs = true;
3027               return tree_cons (name, args, NULL_TREE);
3028             }
3029           else
3030             {
3031               warning (OPT_Wattributes, "%qs attribute ignored",
3032                        IDENTIFIER_POINTER (name));
3033             }
3034         }
3035     }
3036
3037   return NULL_TREE;
3038 }
3039
3040 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
3041 /* Handle the "notshared" attribute.  This attribute is another way of
3042    requesting hidden visibility.  ARM's compiler supports
3043    "__declspec(notshared)"; we support the same thing via an
3044    attribute.  */
3045
3046 static tree
3047 arm_handle_notshared_attribute (tree *node,
3048                                 tree name ATTRIBUTE_UNUSED,
3049                                 tree args ATTRIBUTE_UNUSED,
3050                                 int flags ATTRIBUTE_UNUSED,
3051                                 bool *no_add_attrs)
3052 {
3053   tree decl = TYPE_NAME (*node);
3054
3055   if (decl)
3056     {
3057       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
3058       DECL_VISIBILITY_SPECIFIED (decl) = 1;
3059       *no_add_attrs = false;
3060     }
3061   return NULL_TREE;
3062 }
3063 #endif
3064
3065 /* Return 0 if the attributes for two types are incompatible, 1 if they
3066    are compatible, and 2 if they are nearly compatible (which causes a
3067    warning to be generated).  */
3068 static int
3069 arm_comp_type_attributes (tree type1, tree type2)
3070 {
3071   int l1, l2, s1, s2;
3072
3073   /* Check for mismatch of non-default calling convention.  */
3074   if (TREE_CODE (type1) != FUNCTION_TYPE)
3075     return 1;
3076
3077   /* Check for mismatched call attributes.  */
3078   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
3079   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
3080   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
3081   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
3082
3083   /* Only bother to check if an attribute is defined.  */
3084   if (l1 | l2 | s1 | s2)
3085     {
3086       /* If one type has an attribute, the other must have the same attribute.  */
3087       if ((l1 != l2) || (s1 != s2))
3088         return 0;
3089
3090       /* Disallow mixed attributes.  */
3091       if ((l1 & s2) || (l2 & s1))
3092         return 0;
3093     }
3094
3095   /* Check for mismatched ISR attribute.  */
3096   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
3097   if (! l1)
3098     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
3099   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
3100   if (! l2)
3101     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3102   if (l1 != l2)
3103     return 0;
3104
3105   return 1;
3106 }
3107
3108 /*  Assigns default attributes to newly defined type.  This is used to
3109     set short_call/long_call attributes for function types of
3110     functions defined inside corresponding #pragma scopes.  */
3111 static void
3112 arm_set_default_type_attributes (tree type)
3113 {
3114   /* Add __attribute__ ((long_call)) to all functions, when
3115      inside #pragma long_calls or __attribute__ ((short_call)),
3116      when inside #pragma no_long_calls.  */
3117   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3118     {
3119       tree type_attr_list, attr_name;
3120       type_attr_list = TYPE_ATTRIBUTES (type);
3121
3122       if (arm_pragma_long_calls == LONG)
3123         attr_name = get_identifier ("long_call");
3124       else if (arm_pragma_long_calls == SHORT)
3125         attr_name = get_identifier ("short_call");
3126       else
3127         return;
3128
3129       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3130       TYPE_ATTRIBUTES (type) = type_attr_list;
3131     }
3132 }
3133 \f
3134 /* Return true if DECL is known to be linked into section SECTION.  */
3135
3136 static bool
3137 arm_function_in_section_p (tree decl, section *section)
3138 {
3139   /* We can only be certain about functions defined in the same
3140      compilation unit.  */
3141   if (!TREE_STATIC (decl))
3142     return false;
3143
3144   /* Make sure that SYMBOL always binds to the definition in this
3145      compilation unit.  */
3146   if (!targetm.binds_local_p (decl))
3147     return false;
3148
3149   /* If DECL_SECTION_NAME is set, assume it is trustworthy.  */
3150   if (!DECL_SECTION_NAME (decl))
3151     {
3152       /* Only cater for unit-at-a-time mode, where we know that the user
3153          cannot later specify a section for DECL.  */
3154       if (!flag_unit_at_a_time)
3155         return false;
3156
3157       /* Make sure that we will not create a unique section for DECL.  */
3158       if (flag_function_sections || DECL_ONE_ONLY (decl))
3159         return false;
3160     }
3161
3162   return function_section (decl) == section;
3163 }
3164
3165 /* Return nonzero if a 32-bit "long_call" should be generated for
3166    a call from the current function to DECL.  We generate a long_call
3167    if the function:
3168
3169         a.  has an __attribute__((long call))
3170      or b.  is within the scope of a #pragma long_calls
3171      or c.  the -mlong-calls command line switch has been specified
3172
3173    However we do not generate a long call if the function:
3174
3175         d.  has an __attribute__ ((short_call))
3176      or e.  is inside the scope of a #pragma no_long_calls
3177      or f.  is defined in the same section as the current function.  */
3178
3179 bool
3180 arm_is_long_call_p (tree decl)
3181 {
3182   tree attrs;
3183
3184   if (!decl)
3185     return TARGET_LONG_CALLS;
3186
3187   attrs = TYPE_ATTRIBUTES (TREE_TYPE (decl));
3188   if (lookup_attribute ("short_call", attrs))
3189     return false;
3190
3191   /* For "f", be conservative, and only cater for cases in which the
3192      whole of the current function is placed in the same section.  */
3193   if (!flag_reorder_blocks_and_partition
3194       && arm_function_in_section_p (decl, current_function_section ()))
3195     return false;
3196
3197   if (lookup_attribute ("long_call", attrs))
3198     return true;
3199
3200   return TARGET_LONG_CALLS;
3201 }
3202
3203 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3204 static bool
3205 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3206 {
3207   unsigned long func_type;
3208
3209   if (cfun->machine->sibcall_blocked)
3210     return false;
3211
3212   /* Never tailcall something for which we have no decl, or if we
3213      are in Thumb mode.  */
3214   if (decl == NULL || TARGET_THUMB)
3215     return false;
3216
3217   /* Cannot tail-call to long calls, since these are out of range of
3218      a branch instruction.  */
3219   if (arm_is_long_call_p (decl))
3220     return false;
3221
3222   /* If we are interworking and the function is not declared static
3223      then we can't tail-call it unless we know that it exists in this
3224      compilation unit (since it might be a Thumb routine).  */
3225   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3226     return false;
3227
3228   func_type = arm_current_func_type ();
3229   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3230   if (IS_INTERRUPT (func_type))
3231     return false;
3232
3233   /* Never tailcall if function may be called with a misaligned SP.  */
3234   if (IS_STACKALIGN (func_type))
3235     return false;
3236
3237   /* Everything else is ok.  */
3238   return true;
3239 }
3240
3241 \f
3242 /* Addressing mode support functions.  */
3243
3244 /* Return nonzero if X is a legitimate immediate operand when compiling
3245    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3246 int
3247 legitimate_pic_operand_p (rtx x)
3248 {
3249   if (GET_CODE (x) == SYMBOL_REF
3250       || (GET_CODE (x) == CONST
3251           && GET_CODE (XEXP (x, 0)) == PLUS
3252           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3253     return 0;
3254
3255   return 1;
3256 }
3257
3258 rtx
3259 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3260 {
3261   if (GET_CODE (orig) == SYMBOL_REF
3262       || GET_CODE (orig) == LABEL_REF)
3263     {
3264 #ifndef AOF_ASSEMBLER
3265       rtx pic_ref, address;
3266 #endif
3267       rtx insn;
3268       int subregs = 0;
3269
3270       /* If this function doesn't have a pic register, create one now.
3271          A lot of the logic here is made obscure by the fact that this
3272          routine gets called as part of the rtx cost estimation
3273          process.  We don't want those calls to affect any assumptions
3274          about the real function; and further, we can't call
3275          entry_of_function() until we start the real expansion
3276          process.  */
3277       if (!current_function_uses_pic_offset_table)
3278         {
3279           gcc_assert (!no_new_pseudos);
3280           if (arm_pic_register != INVALID_REGNUM)
3281             {
3282               cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3283
3284               /* Play games to avoid marking the function as needing pic
3285                  if we are being called as part of the cost-estimation
3286                  process.  */
3287               if (current_ir_type () != IR_GIMPLE)
3288                 current_function_uses_pic_offset_table = 1;
3289             }
3290           else
3291             {
3292               rtx seq;
3293
3294               cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3295
3296               /* Play games to avoid marking the function as needing pic
3297                  if we are being called as part of the cost-estimation
3298                  process.  */
3299               if (current_ir_type () != IR_GIMPLE)
3300                 {
3301                   current_function_uses_pic_offset_table = 1;
3302                   start_sequence ();
3303
3304                   arm_load_pic_register (0UL);
3305
3306                   seq = get_insns ();
3307                   end_sequence ();
3308                   emit_insn_after (seq, entry_of_function ());
3309                 }
3310             }
3311         }
3312
3313       if (reg == 0)
3314         {
3315           gcc_assert (!no_new_pseudos);
3316           reg = gen_reg_rtx (Pmode);
3317
3318           subregs = 1;
3319         }
3320
3321 #ifdef AOF_ASSEMBLER
3322       /* The AOF assembler can generate relocations for these directly, and
3323          understands that the PIC register has to be added into the offset.  */
3324       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3325 #else
3326       if (subregs)
3327         address = gen_reg_rtx (Pmode);
3328       else
3329         address = reg;
3330
3331       if (TARGET_ARM)
3332         emit_insn (gen_pic_load_addr_arm (address, orig));
3333       else if (TARGET_THUMB2)
3334         emit_insn (gen_pic_load_addr_thumb2 (address, orig));
3335       else /* TARGET_THUMB1 */
3336         emit_insn (gen_pic_load_addr_thumb1 (address, orig));
3337
3338       if ((GET_CODE (orig) == LABEL_REF
3339            || (GET_CODE (orig) == SYMBOL_REF &&
3340                SYMBOL_REF_LOCAL_P (orig)))
3341           && NEED_GOT_RELOC)
3342         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3343       else
3344         {
3345           pic_ref = gen_const_mem (Pmode,
3346                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3347                                                  address));
3348         }
3349
3350       insn = emit_move_insn (reg, pic_ref);
3351 #endif
3352       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3353          by loop.  */
3354       set_unique_reg_note (insn, REG_EQUAL, orig);
3355
3356       return reg;
3357     }
3358   else if (GET_CODE (orig) == CONST)
3359     {
3360       rtx base, offset;
3361
3362       if (GET_CODE (XEXP (orig, 0)) == PLUS
3363           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3364         return orig;
3365
3366       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3367           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3368         return orig;
3369
3370       if (reg == 0)
3371         {
3372           gcc_assert (!no_new_pseudos);
3373           reg = gen_reg_rtx (Pmode);
3374         }
3375
3376       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3377
3378       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3379       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3380                                        base == reg ? 0 : reg);
3381
3382       if (GET_CODE (offset) == CONST_INT)
3383         {
3384           /* The base register doesn't really matter, we only want to
3385              test the index for the appropriate mode.  */
3386           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3387             {
3388               gcc_assert (!no_new_pseudos);
3389               offset = force_reg (Pmode, offset);
3390             }
3391
3392           if (GET_CODE (offset) == CONST_INT)
3393             return plus_constant (base, INTVAL (offset));
3394         }
3395
3396       if (GET_MODE_SIZE (mode) > 4
3397           && (GET_MODE_CLASS (mode) == MODE_INT
3398               || TARGET_SOFT_FLOAT))
3399         {
3400           emit_insn (gen_addsi3 (reg, base, offset));
3401           return reg;
3402         }
3403
3404       return gen_rtx_PLUS (Pmode, base, offset);
3405     }
3406
3407   return orig;
3408 }
3409
3410
3411 /* Find a spare register to use during the prolog of a function.  */
3412
3413 static int
3414 thumb_find_work_register (unsigned long pushed_regs_mask)
3415 {
3416   int reg;
3417
3418   /* Check the argument registers first as these are call-used.  The
3419      register allocation order means that sometimes r3 might be used
3420      but earlier argument registers might not, so check them all.  */
3421   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3422     if (!regs_ever_live[reg])
3423       return reg;
3424
3425   /* Before going on to check the call-saved registers we can try a couple
3426      more ways of deducing that r3 is available.  The first is when we are
3427      pushing anonymous arguments onto the stack and we have less than 4
3428      registers worth of fixed arguments(*).  In this case r3 will be part of
3429      the variable argument list and so we can be sure that it will be
3430      pushed right at the start of the function.  Hence it will be available
3431      for the rest of the prologue.
3432      (*): ie current_function_pretend_args_size is greater than 0.  */
3433   if (cfun->machine->uses_anonymous_args
3434       && current_function_pretend_args_size > 0)
3435     return LAST_ARG_REGNUM;
3436
3437   /* The other case is when we have fixed arguments but less than 4 registers
3438      worth.  In this case r3 might be used in the body of the function, but
3439      it is not being used to convey an argument into the function.  In theory
3440      we could just check current_function_args_size to see how many bytes are
3441      being passed in argument registers, but it seems that it is unreliable.
3442      Sometimes it will have the value 0 when in fact arguments are being
3443      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3444      check the args_info.nregs field as well.  The problem with this field is
3445      that it makes no allowances for arguments that are passed to the
3446      function but which are not used.  Hence we could miss an opportunity
3447      when a function has an unused argument in r3.  But it is better to be
3448      safe than to be sorry.  */
3449   if (! cfun->machine->uses_anonymous_args
3450       && current_function_args_size >= 0
3451       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3452       && cfun->args_info.nregs < 4)
3453     return LAST_ARG_REGNUM;
3454
3455   /* Otherwise look for a call-saved register that is going to be pushed.  */
3456   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3457     if (pushed_regs_mask & (1 << reg))
3458       return reg;
3459
3460   if (TARGET_THUMB2)
3461     {
3462       /* Thumb-2 can use high regs.  */
3463       for (reg = FIRST_HI_REGNUM; reg < 15; reg ++)
3464         if (pushed_regs_mask & (1 << reg))
3465           return reg;
3466     }
3467   /* Something went wrong - thumb_compute_save_reg_mask()
3468      should have arranged for a suitable register to be pushed.  */
3469   gcc_unreachable ();
3470 }
3471
3472 static GTY(()) int pic_labelno;
3473
3474 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3475    low register.  */
3476
3477 void
3478 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3479 {
3480 #ifndef AOF_ASSEMBLER
3481   rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3482   rtx global_offset_table;
3483
3484   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3485     return;
3486
3487   gcc_assert (flag_pic);
3488
3489   /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3490      in the code stream.  */
3491
3492   labelno = GEN_INT (pic_labelno++);
3493   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3494   l1 = gen_rtx_CONST (VOIDmode, l1);
3495
3496   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3497   /* On the ARM the PC register contains 'dot + 8' at the time of the
3498      addition, on the Thumb it is 'dot + 4'.  */
3499   pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3500   if (GOT_PCREL)
3501     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3502                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3503   else
3504     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3505
3506   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3507
3508   if (TARGET_ARM)
3509     {
3510       emit_insn (gen_pic_load_addr_arm (cfun->machine->pic_reg, pic_rtx));
3511       emit_insn (gen_pic_add_dot_plus_eight (cfun->machine->pic_reg,
3512                                              cfun->machine->pic_reg, labelno));
3513     }
3514   else if (TARGET_THUMB2)
3515     {
3516       /* Thumb-2 only allows very limited access to the PC.  Calculate the
3517          address in a temporary register.  */
3518       if (arm_pic_register != INVALID_REGNUM)
3519         {
3520           pic_tmp = gen_rtx_REG (SImode,
3521                                  thumb_find_work_register (saved_regs));
3522         }
3523       else
3524         {
3525           gcc_assert (!no_new_pseudos);
3526           pic_tmp = gen_reg_rtx (Pmode);
3527         }
3528
3529       emit_insn (gen_pic_load_addr_thumb2 (cfun->machine->pic_reg, pic_rtx));
3530       emit_insn (gen_pic_load_dot_plus_four (pic_tmp, labelno));
3531       emit_insn (gen_addsi3(cfun->machine->pic_reg, cfun->machine->pic_reg,
3532                             pic_tmp));
3533     }
3534   else /* TARGET_THUMB1 */
3535     {
3536       if (arm_pic_register != INVALID_REGNUM
3537           && REGNO (cfun->machine->pic_reg) > LAST_LO_REGNUM)
3538         {
3539           /* We will have pushed the pic register, so we should always be
3540              able to find a work register.  */
3541           pic_tmp = gen_rtx_REG (SImode,
3542                                  thumb_find_work_register (saved_regs));
3543           emit_insn (gen_pic_load_addr_thumb1 (pic_tmp, pic_rtx));
3544           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3545         }
3546       else
3547         emit_insn (gen_pic_load_addr_thumb1 (cfun->machine->pic_reg, pic_rtx));
3548       emit_insn (gen_pic_add_dot_plus_four (cfun->machine->pic_reg,
3549                                             cfun->machine->pic_reg, labelno));
3550     }
3551
3552   /* Need to emit this whether or not we obey regdecls,
3553      since setjmp/longjmp can cause life info to screw up.  */
3554   emit_insn (gen_rtx_USE (VOIDmode, cfun->machine->pic_reg));
3555 #endif /* AOF_ASSEMBLER */
3556 }
3557
3558
3559 /* Return nonzero if X is valid as an ARM state addressing register.  */
3560 static int
3561 arm_address_register_rtx_p (rtx x, int strict_p)
3562 {
3563   int regno;
3564
3565   if (GET_CODE (x) != REG)
3566     return 0;
3567
3568   regno = REGNO (x);
3569
3570   if (strict_p)
3571     return ARM_REGNO_OK_FOR_BASE_P (regno);
3572
3573   return (regno <= LAST_ARM_REGNUM
3574           || regno >= FIRST_PSEUDO_REGISTER
3575           || regno == FRAME_POINTER_REGNUM
3576           || regno == ARG_POINTER_REGNUM);
3577 }
3578
3579 /* Return TRUE if this rtx is the difference of a symbol and a label,
3580    and will reduce to a PC-relative relocation in the object file.
3581    Expressions like this can be left alone when generating PIC, rather
3582    than forced through the GOT.  */
3583 static int
3584 pcrel_constant_p (rtx x)
3585 {
3586   if (GET_CODE (x) == MINUS)
3587     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3588
3589   return FALSE;
3590 }
3591
3592 /* Return nonzero if X is a valid ARM state address operand.  */
3593 int
3594 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3595                           int strict_p)
3596 {
3597   bool use_ldrd;
3598   enum rtx_code code = GET_CODE (x);
3599
3600   if (arm_address_register_rtx_p (x, strict_p))
3601     return 1;
3602
3603   use_ldrd = (TARGET_LDRD
3604               && (mode == DImode
3605                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3606
3607   if (code == POST_INC || code == PRE_DEC
3608       || ((code == PRE_INC || code == POST_DEC)
3609           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3610     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3611
3612   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3613            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3614            && GET_CODE (XEXP (x, 1)) == PLUS
3615            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3616     {
3617       rtx addend = XEXP (XEXP (x, 1), 1);
3618
3619       /* Don't allow ldrd post increment by register because it's hard
3620          to fixup invalid register choices.  */
3621       if (use_ldrd
3622           && GET_CODE (x) == POST_MODIFY
3623           && GET_CODE (addend) == REG)
3624         return 0;
3625
3626       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3627               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3628     }
3629
3630   /* After reload constants split into minipools will have addresses
3631      from a LABEL_REF.  */
3632   else if (reload_completed
3633            && (code == LABEL_REF
3634                || (code == CONST
3635                    && GET_CODE (XEXP (x, 0)) == PLUS
3636                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3637                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3638     return 1;
3639
3640   else if (mode == TImode)
3641     return 0;
3642
3643   else if (code == PLUS)
3644     {
3645       rtx xop0 = XEXP (x, 0);
3646       rtx xop1 = XEXP (x, 1);
3647
3648       return ((arm_address_register_rtx_p (xop0, strict_p)
3649                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3650               || (arm_address_register_rtx_p (xop1, strict_p)
3651                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3652     }
3653
3654 #if 0
3655   /* Reload currently can't handle MINUS, so disable this for now */
3656   else if (GET_CODE (x) == MINUS)
3657     {
3658       rtx xop0 = XEXP (x, 0);
3659       rtx xop1 = XEXP (x, 1);
3660
3661       return (arm_address_register_rtx_p (xop0, strict_p)
3662               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3663     }
3664 #endif
3665
3666   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3667            && code == SYMBOL_REF
3668            && CONSTANT_POOL_ADDRESS_P (x)
3669            && ! (flag_pic
3670                  && symbol_mentioned_p (get_pool_constant (x))
3671                  && ! pcrel_constant_p (get_pool_constant (x))))
3672     return 1;
3673
3674   return 0;
3675 }
3676
3677 /* Return nonzero if X is a valid Thumb-2 address operand.  */
3678 int
3679 thumb2_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3680 {
3681   bool use_ldrd;
3682   enum rtx_code code = GET_CODE (x);
3683   
3684   if (arm_address_register_rtx_p (x, strict_p))
3685     return 1;
3686
3687   use_ldrd = (TARGET_LDRD
3688               && (mode == DImode
3689                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3690
3691   if (code == POST_INC || code == PRE_DEC
3692       || ((code == PRE_INC || code == POST_DEC)
3693           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3694     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3695
3696   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3697            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3698            && GET_CODE (XEXP (x, 1)) == PLUS
3699            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3700     {
3701       /* Thumb-2 only has autoincrement by constant.  */
3702       rtx addend = XEXP (XEXP (x, 1), 1);
3703       HOST_WIDE_INT offset;
3704
3705       if (GET_CODE (addend) != CONST_INT)
3706         return 0;
3707
3708       offset = INTVAL(addend);
3709       if (GET_MODE_SIZE (mode) <= 4)
3710         return (offset > -256 && offset < 256);
3711       
3712       return (use_ldrd && offset > -1024 && offset < 1024
3713               && (offset & 3) == 0);
3714     }
3715
3716   /* After reload constants split into minipools will have addresses
3717      from a LABEL_REF.  */
3718   else if (reload_completed
3719            && (code == LABEL_REF
3720                || (code == CONST
3721                    && GET_CODE (XEXP (x, 0)) == PLUS
3722                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3723                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3724     return 1;
3725
3726   else if (mode == TImode)
3727     return 0;
3728
3729   else if (code == PLUS)
3730     {
3731       rtx xop0 = XEXP (x, 0);
3732       rtx xop1 = XEXP (x, 1);
3733
3734       return ((arm_address_register_rtx_p (xop0, strict_p)
3735                && thumb2_legitimate_index_p (mode, xop1, strict_p))
3736               || (arm_address_register_rtx_p (xop1, strict_p)
3737                   && thumb2_legitimate_index_p (mode, xop0, strict_p)));
3738     }
3739
3740   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3741            && code == SYMBOL_REF
3742            && CONSTANT_POOL_ADDRESS_P (x)
3743            && ! (flag_pic
3744                  && symbol_mentioned_p (get_pool_constant (x))
3745                  && ! pcrel_constant_p (get_pool_constant (x))))
3746     return 1;
3747
3748   return 0;
3749 }
3750
3751 /* Return nonzero if INDEX is valid for an address index operand in
3752    ARM state.  */
3753 static int
3754 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3755                         int strict_p)
3756 {
3757   HOST_WIDE_INT range;
3758   enum rtx_code code = GET_CODE (index);
3759
3760   /* Standard coprocessor addressing modes.  */
3761   if (TARGET_HARD_FLOAT
3762       && (TARGET_FPA || TARGET_MAVERICK)
3763       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3764           || (TARGET_MAVERICK && mode == DImode)))
3765     return (code == CONST_INT && INTVAL (index) < 1024
3766             && INTVAL (index) > -1024
3767             && (INTVAL (index) & 3) == 0);
3768
3769   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3770     return (code == CONST_INT
3771             && INTVAL (index) < 1024
3772             && INTVAL (index) > -1024
3773             && (INTVAL (index) & 3) == 0);
3774
3775   if (arm_address_register_rtx_p (index, strict_p)
3776       && (GET_MODE_SIZE (mode) <= 4))
3777     return 1;
3778
3779   if (mode == DImode || mode == DFmode)
3780     {
3781       if (code == CONST_INT)
3782         {
3783           HOST_WIDE_INT val = INTVAL (index);
3784
3785           if (TARGET_LDRD)
3786             return val > -256 && val < 256;
3787           else
3788             return val > -4096 && val < 4092;
3789         }
3790
3791       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3792     }
3793
3794   if (GET_MODE_SIZE (mode) <= 4
3795       && ! (arm_arch4
3796             && (mode == HImode
3797                 || (mode == QImode && outer == SIGN_EXTEND))))
3798     {
3799       if (code == MULT)
3800         {
3801           rtx xiop0 = XEXP (index, 0);
3802           rtx xiop1 = XEXP (index, 1);
3803
3804           return ((arm_address_register_rtx_p (xiop0, strict_p)
3805                    && power_of_two_operand (xiop1, SImode))
3806                   || (arm_address_register_rtx_p (xiop1, strict_p)
3807                       && power_of_two_operand (xiop0, SImode)));
3808         }
3809       else if (code == LSHIFTRT || code == ASHIFTRT
3810                || code == ASHIFT || code == ROTATERT)
3811         {
3812           rtx op = XEXP (index, 1);
3813
3814           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3815                   && GET_CODE (op) == CONST_INT
3816                   && INTVAL (op) > 0
3817                   && INTVAL (op) <= 31);
3818         }
3819     }
3820
3821   /* For ARM v4 we may be doing a sign-extend operation during the
3822      load.  */
3823   if (arm_arch4)
3824     {
3825       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3826         range = 256;
3827       else
3828         range = 4096;
3829     }
3830   else
3831     range = (mode == HImode) ? 4095 : 4096;
3832
3833   return (code == CONST_INT
3834           && INTVAL (index) < range
3835           && INTVAL (index) > -range);
3836 }
3837
3838 /* Return true if OP is a valid index scaling factor for Thumb-2 address
3839    index operand.  i.e. 1, 2, 4 or 8.  */
3840 static bool
3841 thumb2_index_mul_operand (rtx op)
3842 {
3843   HOST_WIDE_INT val;
3844   
3845   if (GET_CODE(op) != CONST_INT)
3846     return false;
3847
3848   val = INTVAL(op);
3849   return (val == 1 || val == 2 || val == 4 || val == 8);
3850 }
3851   
3852 /* Return nonzero if INDEX is a valid Thumb-2 address index operand.  */
3853 static int
3854 thumb2_legitimate_index_p (enum machine_mode mode, rtx index, int strict_p)
3855 {
3856   enum rtx_code code = GET_CODE (index);
3857
3858   /* ??? Combine arm and thumb2 coprocessor addressing modes.  */
3859   /* Standard coprocessor addressing modes.  */
3860   if (TARGET_HARD_FLOAT
3861       && (TARGET_FPA || TARGET_MAVERICK)
3862       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3863           || (TARGET_MAVERICK && mode == DImode)))
3864     return (code == CONST_INT && INTVAL (index) < 1024
3865             && INTVAL (index) > -1024
3866             && (INTVAL (index) & 3) == 0);
3867
3868   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3869     {
3870       /* For DImode assume values will usually live in core regs
3871          and only allow LDRD addressing modes.  */
3872       if (!TARGET_LDRD || mode != DImode)
3873         return (code == CONST_INT
3874                 && INTVAL (index) < 1024
3875                 && INTVAL (index) > -1024
3876                 && (INTVAL (index) & 3) == 0);
3877     }
3878
3879   if (arm_address_register_rtx_p (index, strict_p)
3880       && (GET_MODE_SIZE (mode) <= 4))
3881     return 1;
3882
3883   if (mode == DImode || mode == DFmode)
3884     {
3885       HOST_WIDE_INT val = INTVAL (index);
3886       /* ??? Can we assume ldrd for thumb2?  */
3887       /* Thumb-2 ldrd only has reg+const addressing modes.  */
3888       if (code != CONST_INT)
3889         return 0;
3890
3891       /* ldrd supports offsets of +-1020.
3892          However the ldr fallback does not.  */
3893       return val > -256 && val < 256 && (val & 3) == 0;
3894     }
3895
3896   if (code == MULT)
3897     {
3898       rtx xiop0 = XEXP (index, 0);
3899       rtx xiop1 = XEXP (index, 1);
3900
3901       return ((arm_address_register_rtx_p (xiop0, strict_p)
3902                && thumb2_index_mul_operand (xiop1))
3903               || (arm_address_register_rtx_p (xiop1, strict_p)
3904                   && thumb2_index_mul_operand (xiop0)));
3905     }
3906   else if (code == ASHIFT)
3907     {
3908       rtx op = XEXP (index, 1);
3909
3910       return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3911               && GET_CODE (op) == CONST_INT
3912               && INTVAL (op) > 0
3913               && INTVAL (op) <= 3);
3914     }
3915
3916   return (code == CONST_INT
3917           && INTVAL (index) < 4096
3918           && INTVAL (index) > -256);
3919 }
3920
3921 /* Return nonzero if X is valid as a 16-bit Thumb state base register.  */
3922 static int
3923 thumb1_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3924 {
3925   int regno;
3926
3927   if (GET_CODE (x) != REG)
3928     return 0;
3929
3930   regno = REGNO (x);
3931
3932   if (strict_p)
3933     return THUMB1_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3934
3935   return (regno <= LAST_LO_REGNUM
3936           || regno > LAST_VIRTUAL_REGISTER
3937           || regno == FRAME_POINTER_REGNUM
3938           || (GET_MODE_SIZE (mode) >= 4
3939               && (regno == STACK_POINTER_REGNUM
3940                   || regno >= FIRST_PSEUDO_REGISTER
3941                   || x == hard_frame_pointer_rtx
3942                   || x == arg_pointer_rtx)));
3943 }
3944
3945 /* Return nonzero if x is a legitimate index register.  This is the case
3946    for any base register that can access a QImode object.  */
3947 inline static int
3948 thumb1_index_register_rtx_p (rtx x, int strict_p)
3949 {
3950   return thumb1_base_register_rtx_p (x, QImode, strict_p);
3951 }
3952
3953 /* Return nonzero if x is a legitimate 16-bit Thumb-state address.
3954
3955    The AP may be eliminated to either the SP or the FP, so we use the
3956    least common denominator, e.g. SImode, and offsets from 0 to 64.
3957
3958    ??? Verify whether the above is the right approach.
3959
3960    ??? Also, the FP may be eliminated to the SP, so perhaps that
3961    needs special handling also.
3962
3963    ??? Look at how the mips16 port solves this problem.  It probably uses
3964    better ways to solve some of these problems.
3965
3966    Although it is not incorrect, we don't accept QImode and HImode
3967    addresses based on the frame pointer or arg pointer until the
3968    reload pass starts.  This is so that eliminating such addresses
3969    into stack based ones won't produce impossible code.  */
3970 int
3971 thumb1_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3972 {
3973   /* ??? Not clear if this is right.  Experiment.  */
3974   if (GET_MODE_SIZE (mode) < 4
3975       && !(reload_in_progress || reload_completed)
3976       && (reg_mentioned_p (frame_pointer_rtx, x)
3977           || reg_mentioned_p (arg_pointer_rtx, x)
3978           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3979           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3980           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3981           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3982     return 0;
3983
3984   /* Accept any base register.  SP only in SImode or larger.  */
3985   else if (thumb1_base_register_rtx_p (x, mode, strict_p))
3986     return 1;
3987
3988   /* This is PC relative data before arm_reorg runs.  */
3989   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3990            && GET_CODE (x) == SYMBOL_REF
3991            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
3992     return 1;
3993
3994   /* This is PC relative data after arm_reorg runs.  */
3995   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3996            && (GET_CODE (x) == LABEL_REF
3997                || (GET_CODE (x) == CONST
3998                    && GET_CODE (XEXP (x, 0)) == PLUS
3999                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
4000                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
4001     return 1;
4002
4003   /* Post-inc indexing only supported for SImode and larger.  */
4004   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
4005            && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p))
4006     return 1;
4007
4008   else if (GET_CODE (x) == PLUS)
4009     {
4010       /* REG+REG address can be any two index registers.  */
4011       /* We disallow FRAME+REG addressing since we know that FRAME
4012          will be replaced with STACK, and SP relative addressing only
4013          permits SP+OFFSET.  */
4014       if (GET_MODE_SIZE (mode) <= 4
4015           && XEXP (x, 0) != frame_pointer_rtx
4016           && XEXP (x, 1) != frame_pointer_rtx
4017           && thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4018           && thumb1_index_register_rtx_p (XEXP (x, 1), strict_p))
4019         return 1;
4020
4021       /* REG+const has 5-7 bit offset for non-SP registers.  */
4022       else if ((thumb1_index_register_rtx_p (XEXP (x, 0), strict_p)
4023                 || XEXP (x, 0) == arg_pointer_rtx)
4024                && GET_CODE (XEXP (x, 1)) == CONST_INT
4025                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4026         return 1;
4027
4028       /* REG+const has 10-bit offset for SP, but only SImode and
4029          larger is supported.  */
4030       /* ??? Should probably check for DI/DFmode overflow here
4031          just like GO_IF_LEGITIMATE_OFFSET does.  */
4032       else if (GET_CODE (XEXP (x, 0)) == REG
4033                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
4034                && GET_MODE_SIZE (mode) >= 4
4035                && GET_CODE (XEXP (x, 1)) == CONST_INT
4036                && INTVAL (XEXP (x, 1)) >= 0
4037                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
4038                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4039         return 1;
4040
4041       else if (GET_CODE (XEXP (x, 0)) == REG
4042                && (REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
4043                    || REGNO (XEXP (x, 0)) == ARG_POINTER_REGNUM
4044                    || (REGNO (XEXP (x, 0)) >= FIRST_VIRTUAL_REGISTER
4045                        && REGNO (XEXP (x, 0)) <= LAST_VIRTUAL_REGISTER))
4046                && GET_MODE_SIZE (mode) >= 4
4047                && GET_CODE (XEXP (x, 1)) == CONST_INT
4048                && (INTVAL (XEXP (x, 1)) & 3) == 0)
4049         return 1;
4050     }
4051
4052   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
4053            && GET_MODE_SIZE (mode) == 4
4054            && GET_CODE (x) == SYMBOL_REF
4055            && CONSTANT_POOL_ADDRESS_P (x)
4056            && ! (flag_pic
4057                  && symbol_mentioned_p (get_pool_constant (x))
4058                  && ! pcrel_constant_p (get_pool_constant (x))))
4059     return 1;
4060
4061   return 0;
4062 }
4063
4064 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
4065    instruction of mode MODE.  */
4066 int
4067 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
4068 {
4069   switch (GET_MODE_SIZE (mode))
4070     {
4071     case 1:
4072       return val >= 0 && val < 32;
4073
4074     case 2:
4075       return val >= 0 && val < 64 && (val & 1) == 0;
4076
4077     default:
4078       return (val >= 0
4079               && (val + GET_MODE_SIZE (mode)) <= 128
4080               && (val & 3) == 0);
4081     }
4082 }
4083
4084 /* Build the SYMBOL_REF for __tls_get_addr.  */
4085
4086 static GTY(()) rtx tls_get_addr_libfunc;
4087
4088 static rtx
4089 get_tls_get_addr (void)
4090 {
4091   if (!tls_get_addr_libfunc)
4092     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
4093   return tls_get_addr_libfunc;
4094 }
4095
4096 static rtx
4097 arm_load_tp (rtx target)
4098 {
4099   if (!target)
4100     target = gen_reg_rtx (SImode);
4101
4102   if (TARGET_HARD_TP)
4103     {
4104       /* Can return in any reg.  */
4105       emit_insn (gen_load_tp_hard (target));
4106     }
4107   else
4108     {
4109       /* Always returned in r0.  Immediately copy the result into a pseudo,
4110          otherwise other uses of r0 (e.g. setting up function arguments) may
4111          clobber the value.  */
4112
4113       rtx tmp;
4114
4115       emit_insn (gen_load_tp_soft ());
4116
4117       tmp = gen_rtx_REG (SImode, 0);
4118       emit_move_insn (target, tmp);
4119     }
4120   return target;
4121 }
4122
4123 static rtx
4124 load_tls_operand (rtx x, rtx reg)
4125 {
4126   rtx tmp;
4127
4128   if (reg == NULL_RTX)
4129     reg = gen_reg_rtx (SImode);
4130
4131   tmp = gen_rtx_CONST (SImode, x);
4132
4133   emit_move_insn (reg, tmp);
4134
4135   return reg;
4136 }
4137
4138 static rtx
4139 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
4140 {
4141   rtx insns, label, labelno, sum;
4142
4143   start_sequence ();
4144
4145   labelno = GEN_INT (pic_labelno++);
4146   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4147   label = gen_rtx_CONST (VOIDmode, label);
4148
4149   sum = gen_rtx_UNSPEC (Pmode,
4150                         gen_rtvec (4, x, GEN_INT (reloc), label,
4151                                    GEN_INT (TARGET_ARM ? 8 : 4)),
4152                         UNSPEC_TLS);
4153   reg = load_tls_operand (sum, reg);
4154
4155   if (TARGET_ARM)
4156     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
4157   else if (TARGET_THUMB2)
4158     {
4159       rtx tmp;
4160       /* Thumb-2 only allows very limited access to the PC.  Calculate
4161          the address in a temporary register.  */
4162       tmp = gen_reg_rtx (SImode);
4163       emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4164       emit_insn (gen_addsi3(reg, reg, tmp));
4165     }
4166   else /* TARGET_THUMB1 */
4167     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4168
4169   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
4170                                      Pmode, 1, reg, Pmode);
4171
4172   insns = get_insns ();
4173   end_sequence ();
4174
4175   return insns;
4176 }
4177
4178 rtx
4179 legitimize_tls_address (rtx x, rtx reg)
4180 {
4181   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
4182   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
4183
4184   switch (model)
4185     {
4186     case TLS_MODEL_GLOBAL_DYNAMIC:
4187       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
4188       dest = gen_reg_rtx (Pmode);
4189       emit_libcall_block (insns, dest, ret, x);
4190       return dest;
4191
4192     case TLS_MODEL_LOCAL_DYNAMIC:
4193       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
4194
4195       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
4196          share the LDM result with other LD model accesses.  */
4197       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
4198                             UNSPEC_TLS);
4199       dest = gen_reg_rtx (Pmode);
4200       emit_libcall_block (insns, dest, ret, eqv);
4201
4202       /* Load the addend.  */
4203       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
4204                                UNSPEC_TLS);
4205       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
4206       return gen_rtx_PLUS (Pmode, dest, addend);
4207
4208     case TLS_MODEL_INITIAL_EXEC:
4209       labelno = GEN_INT (pic_labelno++);
4210       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
4211       label = gen_rtx_CONST (VOIDmode, label);
4212       sum = gen_rtx_UNSPEC (Pmode,
4213                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
4214                                        GEN_INT (TARGET_ARM ? 8 : 4)),
4215                             UNSPEC_TLS);
4216       reg = load_tls_operand (sum, reg);
4217
4218       if (TARGET_ARM)
4219         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
4220       else if (TARGET_THUMB2)
4221         {
4222           rtx tmp;
4223           /* Thumb-2 only allows very limited access to the PC.  Calculate
4224              the address in a temporary register.  */
4225           tmp = gen_reg_rtx (SImode);
4226           emit_insn (gen_pic_load_dot_plus_four (tmp, labelno));
4227           emit_insn (gen_addsi3(reg, reg, tmp));
4228           emit_move_insn (reg, gen_const_mem (SImode, reg));
4229         }
4230       else
4231         {
4232           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
4233           emit_move_insn (reg, gen_const_mem (SImode, reg));
4234         }
4235
4236       tp = arm_load_tp (NULL_RTX);
4237
4238       return gen_rtx_PLUS (Pmode, tp, reg);
4239
4240     case TLS_MODEL_LOCAL_EXEC:
4241       tp = arm_load_tp (NULL_RTX);
4242
4243       reg = gen_rtx_UNSPEC (Pmode,
4244                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
4245                             UNSPEC_TLS);
4246       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
4247
4248       return gen_rtx_PLUS (Pmode, tp, reg);
4249
4250     default:
4251       abort ();
4252     }
4253 }
4254
4255 /* Try machine-dependent ways of modifying an illegitimate address
4256    to be legitimate.  If we find one, return the new, valid address.  */
4257 rtx
4258 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4259 {
4260   if (arm_tls_symbol_p (x))
4261     return legitimize_tls_address (x, NULL_RTX);
4262
4263   if (GET_CODE (x) == PLUS)
4264     {
4265       rtx xop0 = XEXP (x, 0);
4266       rtx xop1 = XEXP (x, 1);
4267
4268       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
4269         xop0 = force_reg (SImode, xop0);
4270
4271       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
4272         xop1 = force_reg (SImode, xop1);
4273
4274       if (ARM_BASE_REGISTER_RTX_P (xop0)
4275           && GET_CODE (xop1) == CONST_INT)
4276         {
4277           HOST_WIDE_INT n, low_n;
4278           rtx base_reg, val;
4279           n = INTVAL (xop1);
4280
4281           /* VFP addressing modes actually allow greater offsets, but for
4282              now we just stick with the lowest common denominator.  */
4283           if (mode == DImode
4284               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4285             {
4286               low_n = n & 0x0f;
4287               n &= ~0x0f;
4288               if (low_n > 4)
4289                 {
4290                   n += 16;
4291                   low_n -= 16;
4292                 }
4293             }
4294           else
4295             {
4296               low_n = ((mode) == TImode ? 0
4297                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4298               n -= low_n;
4299             }
4300
4301           base_reg = gen_reg_rtx (SImode);
4302           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4303           emit_move_insn (base_reg, val);
4304           x = plus_constant (base_reg, low_n);
4305         }
4306       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4307         x = gen_rtx_PLUS (SImode, xop0, xop1);
4308     }
4309
4310   /* XXX We don't allow MINUS any more -- see comment in
4311      arm_legitimate_address_p ().  */
4312   else if (GET_CODE (x) == MINUS)
4313     {
4314       rtx xop0 = XEXP (x, 0);
4315       rtx xop1 = XEXP (x, 1);
4316
4317       if (CONSTANT_P (xop0))
4318         xop0 = force_reg (SImode, xop0);
4319
4320       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4321         xop1 = force_reg (SImode, xop1);
4322
4323       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4324         x = gen_rtx_MINUS (SImode, xop0, xop1);
4325     }
4326
4327   /* Make sure to take full advantage of the pre-indexed addressing mode
4328      with absolute addresses which often allows for the base register to
4329      be factorized for multiple adjacent memory references, and it might
4330      even allows for the mini pool to be avoided entirely. */
4331   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4332     {
4333       unsigned int bits;
4334       HOST_WIDE_INT mask, base, index;
4335       rtx base_reg;
4336
4337       /* ldr and ldrb can use a 12-bit index, ldrsb and the rest can only
4338          use a 8-bit index. So let's use a 12-bit index for SImode only and
4339          hope that arm_gen_constant will enable ldrb to use more bits. */
4340       bits = (mode == SImode) ? 12 : 8;
4341       mask = (1 << bits) - 1;
4342       base = INTVAL (x) & ~mask;
4343       index = INTVAL (x) & mask;
4344       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4345         {
4346           /* It'll most probably be more efficient to generate the base
4347              with more bits set and use a negative index instead. */
4348           base |= mask;
4349           index -= mask;
4350         }
4351       base_reg = force_reg (SImode, GEN_INT (base));
4352       x = plus_constant (base_reg, index);
4353     }
4354
4355   if (flag_pic)
4356     {
4357       /* We need to find and carefully transform any SYMBOL and LABEL
4358          references; so go back to the original address expression.  */
4359       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4360
4361       if (new_x != orig_x)
4362         x = new_x;
4363     }
4364
4365   return x;
4366 }
4367
4368
4369 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4370    to be legitimate.  If we find one, return the new, valid address.  */
4371 rtx
4372 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4373 {
4374   if (arm_tls_symbol_p (x))
4375     return legitimize_tls_address (x, NULL_RTX);
4376
4377   if (GET_CODE (x) == PLUS
4378       && GET_CODE (XEXP (x, 1)) == CONST_INT
4379       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4380           || INTVAL (XEXP (x, 1)) < 0))
4381     {
4382       rtx xop0 = XEXP (x, 0);
4383       rtx xop1 = XEXP (x, 1);
4384       HOST_WIDE_INT offset = INTVAL (xop1);
4385
4386       /* Try and fold the offset into a biasing of the base register and
4387          then offsetting that.  Don't do this when optimizing for space
4388          since it can cause too many CSEs.  */
4389       if (optimize_size && offset >= 0
4390           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4391         {
4392           HOST_WIDE_INT delta;
4393
4394           if (offset >= 256)
4395             delta = offset - (256 - GET_MODE_SIZE (mode));
4396           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4397             delta = 31 * GET_MODE_SIZE (mode);
4398           else
4399             delta = offset & (~31 * GET_MODE_SIZE (mode));
4400
4401           xop0 = force_operand (plus_constant (xop0, offset - delta),
4402                                 NULL_RTX);
4403           x = plus_constant (xop0, delta);
4404         }
4405       else if (offset < 0 && offset > -256)
4406         /* Small negative offsets are best done with a subtract before the
4407            dereference, forcing these into a register normally takes two
4408            instructions.  */
4409         x = force_operand (x, NULL_RTX);
4410       else
4411         {
4412           /* For the remaining cases, force the constant into a register.  */
4413           xop1 = force_reg (SImode, xop1);
4414           x = gen_rtx_PLUS (SImode, xop0, xop1);
4415         }
4416     }
4417   else if (GET_CODE (x) == PLUS
4418            && s_register_operand (XEXP (x, 1), SImode)
4419            && !s_register_operand (XEXP (x, 0), SImode))
4420     {
4421       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4422
4423       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4424     }
4425
4426   if (flag_pic)
4427     {
4428       /* We need to find and carefully transform any SYMBOL and LABEL
4429          references; so go back to the original address expression.  */
4430       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4431
4432       if (new_x != orig_x)
4433         x = new_x;
4434     }
4435
4436   return x;
4437 }
4438
4439 rtx
4440 thumb_legitimize_reload_address (rtx *x_p,
4441                                  enum machine_mode mode,
4442                                  int opnum, int type,
4443                                  int ind_levels ATTRIBUTE_UNUSED)
4444 {
4445   rtx x = *x_p;
4446
4447   if (GET_CODE (x) == PLUS
4448       && GET_MODE_SIZE (mode) < 4
4449       && REG_P (XEXP (x, 0))
4450       && XEXP (x, 0) == stack_pointer_rtx
4451       && GET_CODE (XEXP (x, 1)) == CONST_INT
4452       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4453     {
4454       rtx orig_x = x;
4455
4456       x = copy_rtx (x);
4457       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4458                    Pmode, VOIDmode, 0, 0, opnum, type);
4459       return x;
4460     }
4461
4462   /* If both registers are hi-regs, then it's better to reload the
4463      entire expression rather than each register individually.  That
4464      only requires one reload register rather than two.  */
4465   if (GET_CODE (x) == PLUS
4466       && REG_P (XEXP (x, 0))
4467       && REG_P (XEXP (x, 1))
4468       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4469       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4470     {
4471       rtx orig_x = x;
4472
4473       x = copy_rtx (x);
4474       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4475                    Pmode, VOIDmode, 0, 0, opnum, type);
4476       return x;
4477     }
4478
4479   return NULL;
4480 }
4481
4482 /* Test for various thread-local symbols.  */
4483
4484 /* Return TRUE if X is a thread-local symbol.  */
4485
4486 static bool
4487 arm_tls_symbol_p (rtx x)
4488 {
4489   if (! TARGET_HAVE_TLS)
4490     return false;
4491
4492   if (GET_CODE (x) != SYMBOL_REF)
4493     return false;
4494
4495   return SYMBOL_REF_TLS_MODEL (x) != 0;
4496 }
4497
4498 /* Helper for arm_tls_referenced_p.  */
4499
4500 static int
4501 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4502 {
4503   if (GET_CODE (*x) == SYMBOL_REF)
4504     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4505
4506   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4507      TLS offsets, not real symbol references.  */
4508   if (GET_CODE (*x) == UNSPEC
4509       && XINT (*x, 1) == UNSPEC_TLS)
4510     return -1;
4511
4512   return 0;
4513 }
4514
4515 /* Return TRUE if X contains any TLS symbol references.  */
4516
4517 bool
4518 arm_tls_referenced_p (rtx x)
4519 {
4520   if (! TARGET_HAVE_TLS)
4521     return false;
4522
4523   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4524 }
4525 \f
4526 #define REG_OR_SUBREG_REG(X)                                            \
4527   (GET_CODE (X) == REG                                                  \
4528    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4529
4530 #define REG_OR_SUBREG_RTX(X)                    \
4531    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4532
4533 #ifndef COSTS_N_INSNS
4534 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4535 #endif
4536 static inline int
4537 thumb1_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4538 {
4539   enum machine_mode mode = GET_MODE (x);
4540
4541   switch (code)
4542     {
4543     case ASHIFT:
4544     case ASHIFTRT:
4545     case LSHIFTRT:
4546     case ROTATERT:
4547     case PLUS:
4548     case MINUS:
4549     case COMPARE:
4550     case NEG:
4551     case NOT:
4552       return COSTS_N_INSNS (1);
4553
4554     case MULT:
4555       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4556         {
4557           int cycles = 0;
4558           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4559
4560           while (i)
4561             {
4562               i >>= 2;
4563               cycles++;
4564             }
4565           return COSTS_N_INSNS (2) + cycles;
4566         }
4567       return COSTS_N_INSNS (1) + 16;
4568
4569     case SET:
4570       return (COSTS_N_INSNS (1)
4571               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4572                      + GET_CODE (SET_DEST (x)) == MEM));
4573
4574     case CONST_INT:
4575       if (outer == SET)
4576         {
4577           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4578             return 0;
4579           if (thumb_shiftable_const (INTVAL (x)))
4580             return COSTS_N_INSNS (2);
4581           return COSTS_N_INSNS (3);
4582         }
4583       else if ((outer == PLUS || outer == COMPARE)
4584                && INTVAL (x) < 256 && INTVAL (x) > -256)
4585         return 0;
4586       else if (outer == AND
4587                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4588         return COSTS_N_INSNS (1);
4589       else if (outer == ASHIFT || outer == ASHIFTRT
4590                || outer == LSHIFTRT)
4591         return 0;
4592       return COSTS_N_INSNS (2);
4593
4594     case CONST:
4595     case CONST_DOUBLE:
4596     case LABEL_REF:
4597     case SYMBOL_REF:
4598       return COSTS_N_INSNS (3);
4599
4600     case UDIV:
4601     case UMOD:
4602     case DIV:
4603     case MOD:
4604       return 100;
4605
4606     case TRUNCATE:
4607       return 99;
4608
4609     case AND:
4610     case XOR:
4611     case IOR:
4612       /* XXX guess.  */
4613       return 8;
4614
4615     case MEM:
4616       /* XXX another guess.  */
4617       /* Memory costs quite a lot for the first word, but subsequent words
4618          load at the equivalent of a single insn each.  */
4619       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4620               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4621                  ? 4 : 0));
4622
4623     case IF_THEN_ELSE:
4624       /* XXX a guess.  */
4625       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4626         return 14;
4627       return 2;
4628
4629     case ZERO_EXTEND:
4630       /* XXX still guessing.  */
4631       switch (GET_MODE (XEXP (x, 0)))
4632         {
4633         case QImode:
4634           return (1 + (mode == DImode ? 4 : 0)
4635                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4636
4637         case HImode:
4638           return (4 + (mode == DImode ? 4 : 0)
4639                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4640
4641         case SImode:
4642           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4643
4644         default:
4645           return 99;
4646         }
4647
4648     default:
4649       return 99;
4650     }
4651 }
4652
4653
4654 /* Worker routine for arm_rtx_costs.  */
4655 /* ??? This needs updating for thumb2.  */
4656 static inline int
4657 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4658 {
4659   enum machine_mode mode = GET_MODE (x);
4660   enum rtx_code subcode;
4661   int extra_cost;
4662
4663   switch (code)
4664     {
4665     case MEM:
4666       /* Memory costs quite a lot for the first word, but subsequent words
4667          load at the equivalent of a single insn each.  */
4668       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4669               + (GET_CODE (x) == SYMBOL_REF
4670                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4671
4672     case DIV:
4673     case MOD:
4674     case UDIV:
4675     case UMOD:
4676       return optimize_size ? COSTS_N_INSNS (2) : 100;
4677
4678     case ROTATE:
4679       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4680         return 4;
4681       /* Fall through */
4682     case ROTATERT:
4683       if (mode != SImode)
4684         return 8;
4685       /* Fall through */
4686     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4687       if (mode == DImode)
4688         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4689                 + ((GET_CODE (XEXP (x, 0)) == REG
4690                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4691                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4692                    ? 0 : 8));
4693       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4694                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4695                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4696                    ? 0 : 4)
4697               + ((GET_CODE (XEXP (x, 1)) == REG
4698                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4699                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4700                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4701                  ? 0 : 4));
4702
4703     case MINUS:
4704       if (GET_CODE (XEXP (x, 1)) == MULT && mode == SImode && arm_arch_thumb2)
4705         {
4706           extra_cost = rtx_cost (XEXP (x, 1), code);
4707           if (!REG_OR_SUBREG_REG (XEXP (x, 0)))
4708             extra_cost += 4 * ARM_NUM_REGS (mode);
4709           return extra_cost;
4710         }
4711
4712       if (mode == DImode)
4713         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4714                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4715                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4716                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4717                    ? 0 : 8));
4718
4719       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4720         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4721                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4722                           && arm_const_double_rtx (XEXP (x, 1))))
4723                      ? 0 : 8)
4724                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4725                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4726                         && arm_const_double_rtx (XEXP (x, 0))))
4727                    ? 0 : 8));
4728
4729       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4730             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4731             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4732           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4733                || subcode == ASHIFTRT || subcode == LSHIFTRT
4734                || subcode == ROTATE || subcode == ROTATERT
4735                || (subcode == MULT
4736                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4737                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4738                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4739               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4740               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4741                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4742               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4743         return 1;
4744       /* Fall through */
4745
4746     case PLUS:
4747       if (GET_CODE (XEXP (x, 0)) == MULT)
4748         {
4749           extra_cost = rtx_cost (XEXP (x, 0), code);
4750           if (!REG_OR_SUBREG_REG (XEXP (x, 1)))
4751             extra_cost += 4 * ARM_NUM_REGS (mode);
4752           return extra_cost;
4753         }
4754
4755       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4756         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4757                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4758                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4759                         && arm_const_double_rtx (XEXP (x, 1))))
4760                    ? 0 : 8));
4761
4762       /* Fall through */
4763     case AND: case XOR: case IOR:
4764       extra_cost = 0;
4765
4766       /* Normally the frame registers will be spilt into reg+const during
4767          reload, so it is a bad idea to combine them with other instructions,
4768          since then they might not be moved outside of loops.  As a compromise
4769          we allow integration with ops that have a constant as their second
4770          operand.  */
4771       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4772            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4773            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4774           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4775               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4776         extra_cost = 4;
4777
4778       if (mode == DImode)
4779         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4780                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4781                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4782                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4783                    ? 0 : 8));
4784
4785       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4786         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4787                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4788                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4789                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4790                    ? 0 : 4));
4791
4792       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4793         return (1 + extra_cost
4794                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4795                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4796                      || subcode == ROTATE || subcode == ROTATERT
4797                      || (subcode == MULT
4798                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4799                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4800                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4801                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4802                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4803                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4804                    ? 0 : 4));
4805
4806       return 8;
4807
4808     case MULT:
4809       /* This should have been handled by the CPU specific routines.  */
4810       gcc_unreachable ();
4811
4812     case TRUNCATE:
4813       if (arm_arch3m && mode == SImode
4814           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4815           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4816           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4817               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4818           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4819               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4820         return 8;
4821       return 99;
4822
4823     case NEG:
4824       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4825         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4826       /* Fall through */
4827     case NOT:
4828       if (mode == DImode)
4829         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4830
4831       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4832
4833     case IF_THEN_ELSE:
4834       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4835         return 14;
4836       return 2;
4837
4838     case COMPARE:
4839       return 1;
4840
4841     case ABS:
4842       return 4 + (mode == DImode ? 4 : 0);
4843
4844     case SIGN_EXTEND:
4845       /* ??? value extensions are cheaper on armv6. */
4846       if (GET_MODE (XEXP (x, 0)) == QImode)
4847         return (4 + (mode == DImode ? 4 : 0)
4848                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4849       /* Fall through */
4850     case ZERO_EXTEND:
4851       switch (GET_MODE (XEXP (x, 0)))
4852         {
4853         case QImode:
4854           return (1 + (mode == DImode ? 4 : 0)
4855                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4856
4857         case HImode:
4858           return (4 + (mode == DImode ? 4 : 0)
4859                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4860
4861         case SImode:
4862           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4863
4864         case V8QImode:
4865         case V4HImode:
4866         case V2SImode:
4867         case V4QImode:
4868         case V2HImode:
4869             return 1;
4870
4871         default:
4872           gcc_unreachable ();
4873         }
4874       gcc_unreachable ();
4875
4876     case CONST_INT:
4877       if (const_ok_for_arm (INTVAL (x)))
4878         return outer == SET ? 2 : -1;
4879       else if (outer == AND
4880                && const_ok_for_arm (~INTVAL (x)))
4881         return -1;
4882       else if ((outer == COMPARE
4883                 || outer == PLUS || outer == MINUS)
4884                && const_ok_for_arm (-INTVAL (x)))
4885         return -1;
4886       else
4887         return 5;
4888
4889     case CONST:
4890     case LABEL_REF:
4891     case SYMBOL_REF:
4892       return 6;
4893
4894     case CONST_DOUBLE:
4895       if (arm_const_double_rtx (x))
4896         return outer == SET ? 2 : -1;
4897       else if ((outer == COMPARE || outer == PLUS)
4898                && neg_const_double_rtx_ok_for_fpa (x))
4899         return -1;
4900       return 7;
4901
4902     default:
4903       return 99;
4904     }
4905 }
4906
4907 /* RTX costs when optimizing for size.  */
4908 static bool
4909 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4910 {
4911   enum machine_mode mode = GET_MODE (x);
4912
4913   if (TARGET_THUMB)
4914     {
4915       /* XXX TBD.  For now, use the standard costs.  */
4916       *total = thumb1_rtx_costs (x, code, outer_code);
4917       return true;
4918     }
4919
4920   switch (code)
4921     {
4922     case MEM:
4923       /* A memory access costs 1 insn if the mode is small, or the address is
4924          a single register, otherwise it costs one insn per word.  */
4925       if (REG_P (XEXP (x, 0)))
4926         *total = COSTS_N_INSNS (1);
4927       else
4928         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4929       return true;
4930
4931     case DIV:
4932     case MOD:
4933     case UDIV:
4934     case UMOD:
4935       /* Needs a libcall, so it costs about this.  */
4936       *total = COSTS_N_INSNS (2);
4937       return false;
4938
4939     case ROTATE:
4940       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4941         {
4942           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4943           return true;
4944         }
4945       /* Fall through */
4946     case ROTATERT:
4947     case ASHIFT:
4948     case LSHIFTRT:
4949     case ASHIFTRT:
4950       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4951         {
4952           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4953           return true;
4954         }
4955       else if (mode == SImode)
4956         {
4957           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4958           /* Slightly disparage register shifts, but not by much.  */
4959           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4960             *total += 1 + rtx_cost (XEXP (x, 1), code);
4961           return true;
4962         }
4963
4964       /* Needs a libcall.  */
4965       *total = COSTS_N_INSNS (2);
4966       return false;
4967
4968     case MINUS:
4969       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4970         {
4971           *total = COSTS_N_INSNS (1);
4972           return false;
4973         }
4974
4975       if (mode == SImode)
4976         {
4977           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4978           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4979
4980           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4981               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4982               || subcode1 == ROTATE || subcode1 == ROTATERT
4983               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4984               || subcode1 == ASHIFTRT)
4985             {
4986               /* It's just the cost of the two operands.  */
4987               *total = 0;
4988               return false;
4989             }
4990
4991           *total = COSTS_N_INSNS (1);
4992           return false;
4993         }
4994
4995       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4996       return false;
4997
4998     case PLUS:
4999       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5000         {
5001           *total = COSTS_N_INSNS (1);
5002           return false;
5003         }
5004
5005       /* Fall through */
5006     case AND: case XOR: case IOR:
5007       if (mode == SImode)
5008         {
5009           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
5010
5011           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
5012               || subcode == LSHIFTRT || subcode == ASHIFTRT
5013               || (code == AND && subcode == NOT))
5014             {
5015               /* It's just the cost of the two operands.  */
5016               *total = 0;
5017               return false;
5018             }
5019         }
5020
5021       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5022       return false;
5023
5024     case MULT:
5025       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5026       return false;
5027
5028     case NEG:
5029       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5030         *total = COSTS_N_INSNS (1);
5031       /* Fall through */
5032     case NOT:
5033       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5034
5035       return false;
5036
5037     case IF_THEN_ELSE:
5038       *total = 0;
5039       return false;
5040
5041     case COMPARE:
5042       if (cc_register (XEXP (x, 0), VOIDmode))
5043         * total = 0;
5044       else
5045         *total = COSTS_N_INSNS (1);
5046       return false;
5047
5048     case ABS:
5049       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
5050         *total = COSTS_N_INSNS (1);
5051       else
5052         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
5053       return false;
5054
5055     case SIGN_EXTEND:
5056       *total = 0;
5057       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
5058         {
5059           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5060             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5061         }
5062       if (mode == DImode)
5063         *total += COSTS_N_INSNS (1);
5064       return false;
5065
5066     case ZERO_EXTEND:
5067       *total = 0;
5068       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
5069         {
5070           switch (GET_MODE (XEXP (x, 0)))
5071             {
5072             case QImode:
5073               *total += COSTS_N_INSNS (1);
5074               break;
5075
5076             case HImode:
5077               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
5078
5079             case SImode:
5080               break;
5081
5082             default:
5083               *total += COSTS_N_INSNS (2);
5084             }
5085         }
5086
5087       if (mode == DImode)
5088         *total += COSTS_N_INSNS (1);
5089
5090       return false;
5091
5092     case CONST_INT:
5093       if (const_ok_for_arm (INTVAL (x)))
5094         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
5095       else if (const_ok_for_arm (~INTVAL (x)))
5096         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
5097       else if (const_ok_for_arm (-INTVAL (x)))
5098         {
5099           if (outer_code == COMPARE || outer_code == PLUS
5100               || outer_code == MINUS)
5101             *total = 0;
5102           else
5103             *total = COSTS_N_INSNS (1);
5104         }
5105       else
5106         *total = COSTS_N_INSNS (2);
5107       return true;
5108
5109     case CONST:
5110     case LABEL_REF:
5111     case SYMBOL_REF:
5112       *total = COSTS_N_INSNS (2);
5113       return true;
5114
5115     case CONST_DOUBLE:
5116       *total = COSTS_N_INSNS (4);
5117       return true;
5118
5119     default:
5120       if (mode != VOIDmode)
5121         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
5122       else
5123         *total = COSTS_N_INSNS (4); /* How knows?  */
5124       return false;
5125     }
5126 }
5127
5128 /* RTX costs for cores with a slow MUL implementation.  Thumb-2 is not
5129    supported on any "slowmul" cores, so it can be ignored.  */
5130
5131 static bool
5132 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5133 {
5134   enum machine_mode mode = GET_MODE (x);
5135
5136   if (TARGET_THUMB)
5137     {
5138       *total = thumb1_rtx_costs (x, code, outer_code);
5139       return true;
5140     }
5141
5142   switch (code)
5143     {
5144     case MULT:
5145       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5146           || mode == DImode)
5147         {
5148           *total = 30;
5149           return true;
5150         }
5151
5152       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5153         {
5154           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5155                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5156           int cost, const_ok = const_ok_for_arm (i);
5157           int j, booth_unit_size;
5158
5159           /* Tune as appropriate.  */
5160           cost = const_ok ? 4 : 8;
5161           booth_unit_size = 2;
5162           for (j = 0; i && j < 32; j += booth_unit_size)
5163             {
5164               i >>= booth_unit_size;
5165               cost += 2;
5166             }
5167
5168           *total = cost;
5169           return true;
5170         }
5171
5172       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5173                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5174       return true;
5175
5176     default:
5177       *total = arm_rtx_costs_1 (x, code, outer_code);
5178       return true;
5179     }
5180 }
5181
5182
5183 /* RTX cost for cores with a fast multiply unit (M variants).  */
5184
5185 static bool
5186 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
5187 {
5188   enum machine_mode mode = GET_MODE (x);
5189
5190   if (TARGET_THUMB1)
5191     {
5192       *total = thumb1_rtx_costs (x, code, outer_code);
5193       return true;
5194     }
5195
5196   /* ??? should thumb2 use different costs?  */
5197   switch (code)
5198     {
5199     case MULT:
5200       /* There is no point basing this on the tuning, since it is always the
5201          fast variant if it exists at all.  */
5202       if (mode == DImode
5203           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5204           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5205               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5206         {
5207           *total = 8;
5208           return true;
5209         }
5210
5211
5212       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5213           || mode == DImode)
5214         {
5215           *total = 30;
5216           return true;
5217         }
5218
5219       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5220         {
5221           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5222                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5223           int cost, const_ok = const_ok_for_arm (i);
5224           int j, booth_unit_size;
5225
5226           /* Tune as appropriate.  */
5227           cost = const_ok ? 4 : 8;
5228           booth_unit_size = 8;
5229           for (j = 0; i && j < 32; j += booth_unit_size)
5230             {
5231               i >>= booth_unit_size;
5232               cost += 2;
5233             }
5234
5235           *total = cost;
5236           return true;
5237         }
5238
5239       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5240                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5241       return true;
5242
5243     default:
5244       *total = arm_rtx_costs_1 (x, code, outer_code);
5245       return true;
5246     }
5247 }
5248
5249
5250 /* RTX cost for XScale CPUs.  Thumb-2 is not supported on any xscale cores,
5251    so it can be ignored.  */
5252
5253 static bool
5254 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
5255 {
5256   enum machine_mode mode = GET_MODE (x);
5257
5258   if (TARGET_THUMB)
5259     {
5260       *total = thumb1_rtx_costs (x, code, outer_code);
5261       return true;
5262     }
5263
5264   switch (code)
5265     {
5266     case MULT:
5267       /* There is no point basing this on the tuning, since it is always the
5268          fast variant if it exists at all.  */
5269       if (mode == DImode
5270           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5271           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5272               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5273         {
5274           *total = 8;
5275           return true;
5276         }
5277
5278
5279       if (GET_MODE_CLASS (mode) == MODE_FLOAT
5280           || mode == DImode)
5281         {
5282           *total = 30;
5283           return true;
5284         }
5285
5286       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5287         {
5288           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
5289                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
5290           int cost, const_ok = const_ok_for_arm (i);
5291           unsigned HOST_WIDE_INT masked_const;
5292
5293           /* The cost will be related to two insns.
5294              First a load of the constant (MOV or LDR), then a multiply.  */
5295           cost = 2;
5296           if (! const_ok)
5297             cost += 1;      /* LDR is probably more expensive because
5298                                of longer result latency.  */
5299           masked_const = i & 0xffff8000;
5300           if (masked_const != 0 && masked_const != 0xffff8000)
5301             {
5302               masked_const = i & 0xf8000000;
5303               if (masked_const == 0 || masked_const == 0xf8000000)
5304                 cost += 1;
5305               else
5306                 cost += 2;
5307             }
5308           *total = cost;
5309           return true;
5310         }
5311
5312       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5313                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5314       return true;
5315
5316     case COMPARE:
5317       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5318          will stall until the multiplication is complete.  */
5319       if (GET_CODE (XEXP (x, 0)) == MULT)
5320         *total = 4 + rtx_cost (XEXP (x, 0), code);
5321       else
5322         *total = arm_rtx_costs_1 (x, code, outer_code);
5323       return true;
5324
5325     default:
5326       *total = arm_rtx_costs_1 (x, code, outer_code);
5327       return true;
5328     }
5329 }
5330
5331
5332 /* RTX costs for 9e (and later) cores.  */
5333
5334 static bool
5335 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5336 {
5337   enum machine_mode mode = GET_MODE (x);
5338   int nonreg_cost;
5339   int cost;
5340
5341   if (TARGET_THUMB1)
5342     {
5343       switch (code)
5344         {
5345         case MULT:
5346           *total = COSTS_N_INSNS (3);
5347           return true;
5348
5349         default:
5350           *total = thumb1_rtx_costs (x, code, outer_code);
5351           return true;
5352         }
5353     }
5354
5355   switch (code)
5356     {
5357     case MULT:
5358       /* There is no point basing this on the tuning, since it is always the
5359          fast variant if it exists at all.  */
5360       if (mode == DImode
5361           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5362           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5363               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5364         {
5365           *total = 3;
5366           return true;
5367         }
5368
5369
5370       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5371         {
5372           *total = 30;
5373           return true;
5374         }
5375       if (mode == DImode)
5376         {
5377           cost = 7;
5378           nonreg_cost = 8;
5379         }
5380       else
5381         {
5382           cost = 2;
5383           nonreg_cost = 4;
5384         }
5385
5386
5387       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5388                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5389       return true;
5390
5391     default:
5392       *total = arm_rtx_costs_1 (x, code, outer_code);
5393       return true;
5394     }
5395 }
5396 /* All address computations that can be done are free, but rtx cost returns
5397    the same for practically all of them.  So we weight the different types
5398    of address here in the order (most pref first):
5399    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5400 static inline int
5401 arm_arm_address_cost (rtx x)
5402 {
5403   enum rtx_code c  = GET_CODE (x);
5404
5405   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5406     return 0;
5407   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5408     return 10;
5409
5410   if (c == PLUS || c == MINUS)
5411     {
5412       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5413         return 2;
5414
5415       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5416         return 3;
5417
5418       return 4;
5419     }
5420
5421   return 6;
5422 }
5423
5424 static inline int
5425 arm_thumb_address_cost (rtx x)
5426 {
5427   enum rtx_code c  = GET_CODE (x);
5428
5429   if (c == REG)
5430     return 1;
5431   if (c == PLUS
5432       && GET_CODE (XEXP (x, 0)) == REG
5433       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5434     return 1;
5435
5436   return 2;
5437 }
5438
5439 static int
5440 arm_address_cost (rtx x)
5441 {
5442   return TARGET_32BIT ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5443 }
5444
5445 static int
5446 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5447 {
5448   rtx i_pat, d_pat;
5449
5450   /* Some true dependencies can have a higher cost depending
5451      on precisely how certain input operands are used.  */
5452   if (arm_tune_xscale
5453       && REG_NOTE_KIND (link) == 0
5454       && recog_memoized (insn) >= 0
5455       && recog_memoized (dep) >= 0)
5456     {
5457       int shift_opnum = get_attr_shift (insn);
5458       enum attr_type attr_type = get_attr_type (dep);
5459
5460       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5461          operand for INSN.  If we have a shifted input operand and the
5462          instruction we depend on is another ALU instruction, then we may
5463          have to account for an additional stall.  */
5464       if (shift_opnum != 0
5465           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5466         {
5467           rtx shifted_operand;
5468           int opno;
5469
5470           /* Get the shifted operand.  */
5471           extract_insn (insn);
5472           shifted_operand = recog_data.operand[shift_opnum];
5473
5474           /* Iterate over all the operands in DEP.  If we write an operand
5475              that overlaps with SHIFTED_OPERAND, then we have increase the
5476              cost of this dependency.  */
5477           extract_insn (dep);
5478           preprocess_constraints ();
5479           for (opno = 0; opno < recog_data.n_operands; opno++)
5480             {
5481               /* We can ignore strict inputs.  */
5482               if (recog_data.operand_type[opno] == OP_IN)
5483                 continue;
5484
5485               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5486                                            shifted_operand))
5487                 return 2;
5488             }
5489         }
5490     }
5491
5492   /* XXX This is not strictly true for the FPA.  */
5493   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5494       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5495     return 0;
5496
5497   /* Call insns don't incur a stall, even if they follow a load.  */
5498   if (REG_NOTE_KIND (link) == 0
5499       && GET_CODE (insn) == CALL_INSN)
5500     return 1;
5501
5502   if ((i_pat = single_set (insn)) != NULL
5503       && GET_CODE (SET_SRC (i_pat)) == MEM
5504       && (d_pat = single_set (dep)) != NULL
5505       && GET_CODE (SET_DEST (d_pat)) == MEM)
5506     {
5507       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5508       /* This is a load after a store, there is no conflict if the load reads
5509          from a cached area.  Assume that loads from the stack, and from the
5510          constant pool are cached, and that others will miss.  This is a
5511          hack.  */
5512
5513       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5514           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5515           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5516           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5517         return 1;
5518     }
5519
5520   return cost;
5521 }
5522
5523 static int fp_consts_inited = 0;
5524
5525 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5526 static const char * const strings_fp[8] =
5527 {
5528   "0",   "1",   "2",   "3",
5529   "4",   "5",   "0.5", "10"
5530 };
5531
5532 static REAL_VALUE_TYPE values_fp[8];
5533
5534 static void
5535 init_fp_table (void)
5536 {
5537   int i;
5538   REAL_VALUE_TYPE r;
5539
5540   if (TARGET_VFP)
5541     fp_consts_inited = 1;
5542   else
5543     fp_consts_inited = 8;
5544
5545   for (i = 0; i < fp_consts_inited; i++)
5546     {
5547       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5548       values_fp[i] = r;
5549     }
5550 }
5551
5552 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5553 int
5554 arm_const_double_rtx (rtx x)
5555 {
5556   REAL_VALUE_TYPE r;
5557   int i;
5558
5559   if (!fp_consts_inited)
5560     init_fp_table ();
5561
5562   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5563   if (REAL_VALUE_MINUS_ZERO (r))
5564     return 0;
5565
5566   for (i = 0; i < fp_consts_inited; i++)
5567     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5568       return 1;
5569
5570   return 0;
5571 }
5572
5573 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5574 int
5575 neg_const_double_rtx_ok_for_fpa (rtx x)
5576 {
5577   REAL_VALUE_TYPE r;
5578   int i;
5579
5580   if (!fp_consts_inited)
5581     init_fp_table ();
5582
5583   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5584   r = REAL_VALUE_NEGATE (r);
5585   if (REAL_VALUE_MINUS_ZERO (r))
5586     return 0;
5587
5588   for (i = 0; i < 8; i++)
5589     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5590       return 1;
5591
5592   return 0;
5593 }
5594 \f
5595 /* Predicates for `match_operand' and `match_operator'.  */
5596
5597 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
5598 int
5599 cirrus_memory_offset (rtx op)
5600 {
5601   /* Reject eliminable registers.  */
5602   if (! (reload_in_progress || reload_completed)
5603       && (   reg_mentioned_p (frame_pointer_rtx, op)
5604           || reg_mentioned_p (arg_pointer_rtx, op)
5605           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5606           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5607           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5608           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5609     return 0;
5610
5611   if (GET_CODE (op) == MEM)
5612     {
5613       rtx ind;
5614
5615       ind = XEXP (op, 0);
5616
5617       /* Match: (mem (reg)).  */
5618       if (GET_CODE (ind) == REG)
5619         return 1;
5620
5621       /* Match:
5622          (mem (plus (reg)
5623                     (const))).  */
5624       if (GET_CODE (ind) == PLUS
5625           && GET_CODE (XEXP (ind, 0)) == REG
5626           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5627           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5628         return 1;
5629     }
5630
5631   return 0;
5632 }
5633
5634 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5635    WB is true if full writeback address modes are allowed and is false
5636    if limited writeback address modes (POST_INC and PRE_DEC) are
5637    allowed.  */
5638
5639 int
5640 arm_coproc_mem_operand (rtx op, bool wb)
5641 {
5642   rtx ind;
5643
5644   /* Reject eliminable registers.  */
5645   if (! (reload_in_progress || reload_completed)
5646       && (   reg_mentioned_p (frame_pointer_rtx, op)
5647           || reg_mentioned_p (arg_pointer_rtx, op)
5648           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5649           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5650           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5651           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5652     return FALSE;
5653
5654   /* Constants are converted into offsets from labels.  */
5655   if (GET_CODE (op) != MEM)
5656     return FALSE;
5657
5658   ind = XEXP (op, 0);
5659
5660   if (reload_completed
5661       && (GET_CODE (ind) == LABEL_REF
5662           || (GET_CODE (ind) == CONST
5663               && GET_CODE (XEXP (ind, 0)) == PLUS
5664               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5665               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5666     return TRUE;
5667
5668   /* Match: (mem (reg)).  */
5669   if (GET_CODE (ind) == REG)
5670     return arm_address_register_rtx_p (ind, 0);
5671
5672   /* Autoincremment addressing modes.  POST_INC and PRE_DEC are
5673      acceptable in any case (subject to verification by
5674      arm_address_register_rtx_p).  We need WB to be true to accept
5675      PRE_INC and POST_DEC.  */
5676   if (GET_CODE (ind) == POST_INC
5677       || GET_CODE (ind) == PRE_DEC
5678       || (wb
5679           && (GET_CODE (ind) == PRE_INC
5680               || GET_CODE (ind) == POST_DEC)))
5681     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5682
5683   if (wb
5684       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5685       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5686       && GET_CODE (XEXP (ind, 1)) == PLUS
5687       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5688     ind = XEXP (ind, 1);
5689
5690   /* Match:
5691      (plus (reg)
5692            (const)).  */
5693   if (GET_CODE (ind) == PLUS
5694       && GET_CODE (XEXP (ind, 0)) == REG
5695       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5696       && GET_CODE (XEXP (ind, 1)) == CONST_INT
5697       && INTVAL (XEXP (ind, 1)) > -1024
5698       && INTVAL (XEXP (ind, 1)) <  1024
5699       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5700     return TRUE;
5701
5702   return FALSE;
5703 }
5704
5705 /* Return true if X is a register that will be eliminated later on.  */
5706 int
5707 arm_eliminable_register (rtx x)
5708 {
5709   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5710                        || REGNO (x) == ARG_POINTER_REGNUM
5711                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5712                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5713 }
5714
5715 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5716    coprocessor registers.  Otherwise return NO_REGS.  */
5717
5718 enum reg_class
5719 coproc_secondary_reload_class (enum machine_mode mode, rtx x, bool wb)
5720 {
5721   if (arm_coproc_mem_operand (x, wb) || s_register_operand (x, mode))
5722     return NO_REGS;
5723
5724   return GENERAL_REGS;
5725 }
5726
5727 /* Values which must be returned in the most-significant end of the return
5728    register.  */
5729
5730 static bool
5731 arm_return_in_msb (tree valtype)
5732 {
5733   return (TARGET_AAPCS_BASED
5734           && BYTES_BIG_ENDIAN
5735           && (AGGREGATE_TYPE_P (valtype)
5736               || TREE_CODE (valtype) == COMPLEX_TYPE));
5737 }
5738
5739 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5740    Use by the Cirrus Maverick code which has to workaround
5741    a hardware bug triggered by such instructions.  */
5742 static bool
5743 arm_memory_load_p (rtx insn)
5744 {
5745   rtx body, lhs, rhs;;
5746
5747   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5748     return false;
5749
5750   body = PATTERN (insn);
5751
5752   if (GET_CODE (body) != SET)
5753     return false;
5754
5755   lhs = XEXP (body, 0);
5756   rhs = XEXP (body, 1);
5757
5758   lhs = REG_OR_SUBREG_RTX (lhs);
5759
5760   /* If the destination is not a general purpose
5761      register we do not have to worry.  */
5762   if (GET_CODE (lhs) != REG
5763       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5764     return false;
5765
5766   /* As well as loads from memory we also have to react
5767      to loads of invalid constants which will be turned
5768      into loads from the minipool.  */
5769   return (GET_CODE (rhs) == MEM
5770           || GET_CODE (rhs) == SYMBOL_REF
5771           || note_invalid_constants (insn, -1, false));
5772 }
5773
5774 /* Return TRUE if INSN is a Cirrus instruction.  */
5775 static bool
5776 arm_cirrus_insn_p (rtx insn)
5777 {
5778   enum attr_cirrus attr;
5779
5780   /* get_attr cannot accept USE or CLOBBER.  */
5781   if (!insn
5782       || GET_CODE (insn) != INSN
5783       || GET_CODE (PATTERN (insn)) == USE
5784       || GET_CODE (PATTERN (insn)) == CLOBBER)
5785     return 0;
5786
5787   attr = get_attr_cirrus (insn);
5788
5789   return attr != CIRRUS_NOT;
5790 }
5791
5792 /* Cirrus reorg for invalid instruction combinations.  */
5793 static void
5794 cirrus_reorg (rtx first)
5795 {
5796   enum attr_cirrus attr;
5797   rtx body = PATTERN (first);
5798   rtx t;
5799   int nops;
5800
5801   /* Any branch must be followed by 2 non Cirrus instructions.  */
5802   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5803     {
5804       nops = 0;
5805       t = next_nonnote_insn (first);
5806
5807       if (arm_cirrus_insn_p (t))
5808         ++ nops;
5809
5810       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5811         ++ nops;
5812
5813       while (nops --)
5814         emit_insn_after (gen_nop (), first);
5815
5816       return;
5817     }
5818
5819   /* (float (blah)) is in parallel with a clobber.  */
5820   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5821     body = XVECEXP (body, 0, 0);
5822
5823   if (GET_CODE (body) == SET)
5824     {
5825       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5826
5827       /* cfldrd, cfldr64, cfstrd, cfstr64 must
5828          be followed by a non Cirrus insn.  */
5829       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5830         {
5831           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5832             emit_insn_after (gen_nop (), first);
5833
5834           return;
5835         }
5836       else if (arm_memory_load_p (first))
5837         {
5838           unsigned int arm_regno;
5839
5840           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5841              ldr/cfmv64hr combination where the Rd field is the same
5842              in both instructions must be split with a non Cirrus
5843              insn.  Example:
5844
5845              ldr r0, blah
5846              nop
5847              cfmvsr mvf0, r0.  */
5848
5849           /* Get Arm register number for ldr insn.  */
5850           if (GET_CODE (lhs) == REG)
5851             arm_regno = REGNO (lhs);
5852           else
5853             {
5854               gcc_assert (GET_CODE (rhs) == REG);
5855               arm_regno = REGNO (rhs);
5856             }
5857
5858           /* Next insn.  */
5859           first = next_nonnote_insn (first);
5860
5861           if (! arm_cirrus_insn_p (first))
5862             return;
5863
5864           body = PATTERN (first);
5865
5866           /* (float (blah)) is in parallel with a clobber.  */
5867           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5868             body = XVECEXP (body, 0, 0);
5869
5870           if (GET_CODE (body) == FLOAT)
5871             body = XEXP (body, 0);
5872
5873           if (get_attr_cirrus (first) == CIRRUS_MOVE
5874               && GET_CODE (XEXP (body, 1)) == REG
5875               && arm_regno == REGNO (XEXP (body, 1)))
5876             emit_insn_after (gen_nop (), first);
5877
5878           return;
5879         }
5880     }
5881
5882   /* get_attr cannot accept USE or CLOBBER.  */
5883   if (!first
5884       || GET_CODE (first) != INSN
5885       || GET_CODE (PATTERN (first)) == USE
5886       || GET_CODE (PATTERN (first)) == CLOBBER)
5887     return;
5888
5889   attr = get_attr_cirrus (first);
5890
5891   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5892      must be followed by a non-coprocessor instruction.  */
5893   if (attr == CIRRUS_COMPARE)
5894     {
5895       nops = 0;
5896
5897       t = next_nonnote_insn (first);
5898
5899       if (arm_cirrus_insn_p (t))
5900         ++ nops;
5901
5902       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5903         ++ nops;
5904
5905       while (nops --)
5906         emit_insn_after (gen_nop (), first);
5907
5908       return;
5909     }
5910 }
5911
5912 /* Return TRUE if X references a SYMBOL_REF.  */
5913 int
5914 symbol_mentioned_p (rtx x)
5915 {
5916   const char * fmt;
5917   int i;
5918
5919   if (GET_CODE (x) == SYMBOL_REF)
5920     return 1;
5921
5922   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5923      are constant offsets, not symbols.  */
5924   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5925     return 0;
5926
5927   fmt = GET_RTX_FORMAT (GET_CODE (x));
5928
5929   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5930     {
5931       if (fmt[i] == 'E')
5932         {
5933           int j;
5934
5935           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5936             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5937               return 1;
5938         }
5939       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5940         return 1;
5941     }
5942
5943   return 0;
5944 }
5945
5946 /* Return TRUE if X references a LABEL_REF.  */
5947 int
5948 label_mentioned_p (rtx x)
5949 {
5950   const char * fmt;
5951   int i;
5952
5953   if (GET_CODE (x) == LABEL_REF)
5954     return 1;
5955
5956   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
5957      instruction, but they are constant offsets, not symbols.  */
5958   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5959     return 0;
5960
5961   fmt = GET_RTX_FORMAT (GET_CODE (x));
5962   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5963     {
5964       if (fmt[i] == 'E')
5965         {
5966           int j;
5967
5968           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5969             if (label_mentioned_p (XVECEXP (x, i, j)))
5970               return 1;
5971         }
5972       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5973         return 1;
5974     }
5975
5976   return 0;
5977 }
5978
5979 int
5980 tls_mentioned_p (rtx x)
5981 {
5982   switch (GET_CODE (x))
5983     {
5984     case CONST:
5985       return tls_mentioned_p (XEXP (x, 0));
5986
5987     case UNSPEC:
5988       if (XINT (x, 1) == UNSPEC_TLS)
5989         return 1;
5990
5991     default:
5992       return 0;
5993     }
5994 }
5995
5996 /* Must not copy a SET whose source operand is PC-relative.  */
5997
5998 static bool
5999 arm_cannot_copy_insn_p (rtx insn)
6000 {
6001   rtx pat = PATTERN (insn);
6002
6003   if (GET_CODE (pat) == PARALLEL
6004       && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
6005     {
6006       rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
6007
6008       if (GET_CODE (rhs) == UNSPEC
6009           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
6010         return TRUE;
6011
6012       if (GET_CODE (rhs) == MEM
6013           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
6014           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
6015         return TRUE;
6016     }
6017
6018   return FALSE;
6019 }
6020
6021 enum rtx_code
6022 minmax_code (rtx x)
6023 {
6024   enum rtx_code code = GET_CODE (x);
6025
6026   switch (code)
6027     {
6028     case SMAX:
6029       return GE;
6030     case SMIN:
6031       return LE;
6032     case UMIN:
6033       return LEU;
6034     case UMAX:
6035       return GEU;
6036     default:
6037       gcc_unreachable ();
6038     }
6039 }
6040
6041 /* Return 1 if memory locations are adjacent.  */
6042 int
6043 adjacent_mem_locations (rtx a, rtx b)
6044 {
6045   /* We don't guarantee to preserve the order of these memory refs.  */
6046   if (volatile_refs_p (a) || volatile_refs_p (b))
6047     return 0;
6048
6049   if ((GET_CODE (XEXP (a, 0)) == REG
6050        || (GET_CODE (XEXP (a, 0)) == PLUS
6051            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
6052       && (GET_CODE (XEXP (b, 0)) == REG
6053           || (GET_CODE (XEXP (b, 0)) == PLUS
6054               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
6055     {
6056       HOST_WIDE_INT val0 = 0, val1 = 0;
6057       rtx reg0, reg1;
6058       int val_diff;
6059
6060       if (GET_CODE (XEXP (a, 0)) == PLUS)
6061         {
6062           reg0 = XEXP (XEXP (a, 0), 0);
6063           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
6064         }
6065       else
6066         reg0 = XEXP (a, 0);
6067
6068       if (GET_CODE (XEXP (b, 0)) == PLUS)
6069         {
6070           reg1 = XEXP (XEXP (b, 0), 0);
6071           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
6072         }
6073       else
6074         reg1 = XEXP (b, 0);
6075
6076       /* Don't accept any offset that will require multiple
6077          instructions to handle, since this would cause the
6078          arith_adjacentmem pattern to output an overlong sequence.  */
6079       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
6080         return 0;
6081
6082       /* Don't allow an eliminable register: register elimination can make
6083          the offset too large.  */
6084       if (arm_eliminable_register (reg0))
6085         return 0;
6086
6087       val_diff = val1 - val0;
6088
6089       if (arm_ld_sched)
6090         {
6091           /* If the target has load delay slots, then there's no benefit
6092              to using an ldm instruction unless the offset is zero and
6093              we are optimizing for size.  */
6094           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
6095                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
6096                   && (val_diff == 4 || val_diff == -4));
6097         }
6098
6099       return ((REGNO (reg0) == REGNO (reg1))
6100               && (val_diff == 4 || val_diff == -4));
6101     }
6102
6103   return 0;
6104 }
6105
6106 int
6107 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6108                         HOST_WIDE_INT *load_offset)
6109 {
6110   int unsorted_regs[4];
6111   HOST_WIDE_INT unsorted_offsets[4];
6112   int order[4];
6113   int base_reg = -1;
6114   int i;
6115
6116   /* Can only handle 2, 3, or 4 insns at present,
6117      though could be easily extended if required.  */
6118   gcc_assert (nops >= 2 && nops <= 4);
6119
6120   /* Loop over the operands and check that the memory references are
6121      suitable (i.e. immediate offsets from the same base register).  At
6122      the same time, extract the target register, and the memory
6123      offsets.  */
6124   for (i = 0; i < nops; i++)
6125     {
6126       rtx reg;
6127       rtx offset;
6128
6129       /* Convert a subreg of a mem into the mem itself.  */
6130       if (GET_CODE (operands[nops + i]) == SUBREG)
6131         operands[nops + i] = alter_subreg (operands + (nops + i));
6132
6133       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6134
6135       /* Don't reorder volatile memory references; it doesn't seem worth
6136          looking for the case where the order is ok anyway.  */
6137       if (MEM_VOLATILE_P (operands[nops + i]))
6138         return 0;
6139
6140       offset = const0_rtx;
6141
6142       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6143            || (GET_CODE (reg) == SUBREG
6144                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6145           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6146               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6147                    == REG)
6148                   || (GET_CODE (reg) == SUBREG
6149                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6150               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6151                   == CONST_INT)))
6152         {
6153           if (i == 0)
6154             {
6155               base_reg = REGNO (reg);
6156               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6157                                   ? REGNO (operands[i])
6158                                   : REGNO (SUBREG_REG (operands[i])));
6159               order[0] = 0;
6160             }
6161           else
6162             {
6163               if (base_reg != (int) REGNO (reg))
6164                 /* Not addressed from the same base register.  */
6165                 return 0;
6166
6167               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6168                                   ? REGNO (operands[i])
6169                                   : REGNO (SUBREG_REG (operands[i])));
6170               if (unsorted_regs[i] < unsorted_regs[order[0]])
6171                 order[0] = i;
6172             }
6173
6174           /* If it isn't an integer register, or if it overwrites the
6175              base register but isn't the last insn in the list, then
6176              we can't do this.  */
6177           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
6178               || (i != nops - 1 && unsorted_regs[i] == base_reg))
6179             return 0;
6180
6181           unsorted_offsets[i] = INTVAL (offset);
6182         }
6183       else
6184         /* Not a suitable memory address.  */
6185         return 0;
6186     }
6187
6188   /* All the useful information has now been extracted from the
6189      operands into unsorted_regs and unsorted_offsets; additionally,
6190      order[0] has been set to the lowest numbered register in the
6191      list.  Sort the registers into order, and check that the memory
6192      offsets are ascending and adjacent.  */
6193
6194   for (i = 1; i < nops; i++)
6195     {
6196       int j;
6197
6198       order[i] = order[i - 1];
6199       for (j = 0; j < nops; j++)
6200         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6201             && (order[i] == order[i - 1]
6202                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6203           order[i] = j;
6204
6205       /* Have we found a suitable register? if not, one must be used more
6206          than once.  */
6207       if (order[i] == order[i - 1])
6208         return 0;
6209
6210       /* Is the memory address adjacent and ascending? */
6211       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6212         return 0;
6213     }
6214
6215   if (base)
6216     {
6217       *base = base_reg;
6218
6219       for (i = 0; i < nops; i++)
6220         regs[i] = unsorted_regs[order[i]];
6221
6222       *load_offset = unsorted_offsets[order[0]];
6223     }
6224
6225   if (unsorted_offsets[order[0]] == 0)
6226     return 1; /* ldmia */
6227
6228   if (TARGET_ARM && unsorted_offsets[order[0]] == 4)
6229     return 2; /* ldmib */
6230
6231   if (TARGET_ARM && unsorted_offsets[order[nops - 1]] == 0)
6232     return 3; /* ldmda */
6233
6234   if (unsorted_offsets[order[nops - 1]] == -4)
6235     return 4; /* ldmdb */
6236
6237   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
6238      if the offset isn't small enough.  The reason 2 ldrs are faster
6239      is because these ARMs are able to do more than one cache access
6240      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
6241      whilst the ARM8 has a double bandwidth cache.  This means that
6242      these cores can do both an instruction fetch and a data fetch in
6243      a single cycle, so the trick of calculating the address into a
6244      scratch register (one of the result regs) and then doing a load
6245      multiple actually becomes slower (and no smaller in code size).
6246      That is the transformation
6247
6248         ldr     rd1, [rbase + offset]
6249         ldr     rd2, [rbase + offset + 4]
6250
6251      to
6252
6253         add     rd1, rbase, offset
6254         ldmia   rd1, {rd1, rd2}
6255
6256      produces worse code -- '3 cycles + any stalls on rd2' instead of
6257      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
6258      access per cycle, the first sequence could never complete in less
6259      than 6 cycles, whereas the ldm sequence would only take 5 and
6260      would make better use of sequential accesses if not hitting the
6261      cache.
6262
6263      We cheat here and test 'arm_ld_sched' which we currently know to
6264      only be true for the ARM8, ARM9 and StrongARM.  If this ever
6265      changes, then the test below needs to be reworked.  */
6266   if (nops == 2 && arm_ld_sched)
6267     return 0;
6268
6269   /* Can't do it without setting up the offset, only do this if it takes
6270      no more than one insn.  */
6271   return (const_ok_for_arm (unsorted_offsets[order[0]])
6272           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
6273 }
6274
6275 const char *
6276 emit_ldm_seq (rtx *operands, int nops)
6277 {
6278   int regs[4];
6279   int base_reg;
6280   HOST_WIDE_INT offset;
6281   char buf[100];
6282   int i;
6283
6284   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6285     {
6286     case 1:
6287       strcpy (buf, "ldm%(ia%)\t");
6288       break;
6289
6290     case 2:
6291       strcpy (buf, "ldm%(ib%)\t");
6292       break;
6293
6294     case 3:
6295       strcpy (buf, "ldm%(da%)\t");
6296       break;
6297
6298     case 4:
6299       strcpy (buf, "ldm%(db%)\t");
6300       break;
6301
6302     case 5:
6303       if (offset >= 0)
6304         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6305                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6306                  (long) offset);
6307       else
6308         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6309                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6310                  (long) -offset);
6311       output_asm_insn (buf, operands);
6312       base_reg = regs[0];
6313       strcpy (buf, "ldm%(ia%)\t");
6314       break;
6315
6316     default:
6317       gcc_unreachable ();
6318     }
6319
6320   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6321            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6322
6323   for (i = 1; i < nops; i++)
6324     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6325              reg_names[regs[i]]);
6326
6327   strcat (buf, "}\t%@ phole ldm");
6328
6329   output_asm_insn (buf, operands);
6330   return "";
6331 }
6332
6333 int
6334 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6335                          HOST_WIDE_INT * load_offset)
6336 {
6337   int unsorted_regs[4];
6338   HOST_WIDE_INT unsorted_offsets[4];
6339   int order[4];
6340   int base_reg = -1;
6341   int i;
6342
6343   /* Can only handle 2, 3, or 4 insns at present, though could be easily
6344      extended if required.  */
6345   gcc_assert (nops >= 2 && nops <= 4);
6346
6347   /* Loop over the operands and check that the memory references are
6348      suitable (i.e. immediate offsets from the same base register).  At
6349      the same time, extract the target register, and the memory
6350      offsets.  */
6351   for (i = 0; i < nops; i++)
6352     {
6353       rtx reg;
6354       rtx offset;
6355
6356       /* Convert a subreg of a mem into the mem itself.  */
6357       if (GET_CODE (operands[nops + i]) == SUBREG)
6358         operands[nops + i] = alter_subreg (operands + (nops + i));
6359
6360       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6361
6362       /* Don't reorder volatile memory references; it doesn't seem worth
6363          looking for the case where the order is ok anyway.  */
6364       if (MEM_VOLATILE_P (operands[nops + i]))
6365         return 0;
6366
6367       offset = const0_rtx;
6368
6369       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6370            || (GET_CODE (reg) == SUBREG
6371                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6372           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6373               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6374                    == REG)
6375                   || (GET_CODE (reg) == SUBREG
6376                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6377               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6378                   == CONST_INT)))
6379         {
6380           if (i == 0)
6381             {
6382               base_reg = REGNO (reg);
6383               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6384                                   ? REGNO (operands[i])
6385                                   : REGNO (SUBREG_REG (operands[i])));
6386               order[0] = 0;
6387             }
6388           else
6389             {
6390               if (base_reg != (int) REGNO (reg))
6391                 /* Not addressed from the same base register.  */
6392                 return 0;
6393
6394               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6395                                   ? REGNO (operands[i])
6396                                   : REGNO (SUBREG_REG (operands[i])));
6397               if (unsorted_regs[i] < unsorted_regs[order[0]])
6398                 order[0] = i;
6399             }
6400
6401           /* If it isn't an integer register, then we can't do this.  */
6402           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6403             return 0;
6404
6405           unsorted_offsets[i] = INTVAL (offset);
6406         }
6407       else
6408         /* Not a suitable memory address.  */
6409         return 0;
6410     }
6411
6412   /* All the useful information has now been extracted from the
6413      operands into unsorted_regs and unsorted_offsets; additionally,
6414      order[0] has been set to the lowest numbered register in the
6415      list.  Sort the registers into order, and check that the memory
6416      offsets are ascending and adjacent.  */
6417
6418   for (i = 1; i < nops; i++)
6419     {
6420       int j;
6421
6422       order[i] = order[i - 1];
6423       for (j = 0; j < nops; j++)
6424         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6425             && (order[i] == order[i - 1]
6426                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6427           order[i] = j;
6428
6429       /* Have we found a suitable register? if not, one must be used more
6430          than once.  */
6431       if (order[i] == order[i - 1])
6432         return 0;
6433
6434       /* Is the memory address adjacent and ascending? */
6435       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6436         return 0;
6437     }
6438
6439   if (base)
6440     {
6441       *base = base_reg;
6442
6443       for (i = 0; i < nops; i++)
6444         regs[i] = unsorted_regs[order[i]];
6445
6446       *load_offset = unsorted_offsets[order[0]];
6447     }
6448
6449   if (unsorted_offsets[order[0]] == 0)
6450     return 1; /* stmia */
6451
6452   if (unsorted_offsets[order[0]] == 4)
6453     return 2; /* stmib */
6454
6455   if (unsorted_offsets[order[nops - 1]] == 0)
6456     return 3; /* stmda */
6457
6458   if (unsorted_offsets[order[nops - 1]] == -4)
6459     return 4; /* stmdb */
6460
6461   return 0;
6462 }
6463
6464 const char *
6465 emit_stm_seq (rtx *operands, int nops)
6466 {
6467   int regs[4];
6468   int base_reg;
6469   HOST_WIDE_INT offset;
6470   char buf[100];
6471   int i;
6472
6473   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6474     {
6475     case 1:
6476       strcpy (buf, "stm%(ia%)\t");
6477       break;
6478
6479     case 2:
6480       strcpy (buf, "stm%(ib%)\t");
6481       break;
6482
6483     case 3:
6484       strcpy (buf, "stm%(da%)\t");
6485       break;
6486
6487     case 4:
6488       strcpy (buf, "stm%(db%)\t");
6489       break;
6490
6491     default:
6492       gcc_unreachable ();
6493     }
6494
6495   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6496            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6497
6498   for (i = 1; i < nops; i++)
6499     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6500              reg_names[regs[i]]);
6501
6502   strcat (buf, "}\t%@ phole stm");
6503
6504   output_asm_insn (buf, operands);
6505   return "";
6506 }
6507 \f
6508 /* Routines for use in generating RTL.  */
6509
6510 rtx
6511 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6512                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6513 {
6514   HOST_WIDE_INT offset = *offsetp;
6515   int i = 0, j;
6516   rtx result;
6517   int sign = up ? 1 : -1;
6518   rtx mem, addr;
6519
6520   /* XScale has load-store double instructions, but they have stricter
6521      alignment requirements than load-store multiple, so we cannot
6522      use them.
6523
6524      For XScale ldm requires 2 + NREGS cycles to complete and blocks
6525      the pipeline until completion.
6526
6527         NREGS           CYCLES
6528           1               3
6529           2               4
6530           3               5
6531           4               6
6532
6533      An ldr instruction takes 1-3 cycles, but does not block the
6534      pipeline.
6535
6536         NREGS           CYCLES
6537           1              1-3
6538           2              2-6
6539           3              3-9
6540           4              4-12
6541
6542      Best case ldr will always win.  However, the more ldr instructions
6543      we issue, the less likely we are to be able to schedule them well.
6544      Using ldr instructions also increases code size.
6545
6546      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6547      for counts of 3 or 4 regs.  */
6548   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6549     {
6550       rtx seq;
6551
6552       start_sequence ();
6553
6554       for (i = 0; i < count; i++)
6555         {
6556           addr = plus_constant (from, i * 4 * sign);
6557           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6558           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6559           offset += 4 * sign;
6560         }
6561
6562       if (write_back)
6563         {
6564           emit_move_insn (from, plus_constant (from, count * 4 * sign));
6565           *offsetp = offset;
6566         }
6567
6568       seq = get_insns ();
6569       end_sequence ();
6570
6571       return seq;
6572     }
6573
6574   result = gen_rtx_PARALLEL (VOIDmode,
6575                              rtvec_alloc (count + (write_back ? 1 : 0)));
6576   if (write_back)
6577     {
6578       XVECEXP (result, 0, 0)
6579         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6580       i = 1;
6581       count++;
6582     }
6583
6584   for (j = 0; i < count; i++, j++)
6585     {
6586       addr = plus_constant (from, j * 4 * sign);
6587       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6588       XVECEXP (result, 0, i)
6589         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6590       offset += 4 * sign;
6591     }
6592
6593   if (write_back)
6594     *offsetp = offset;
6595
6596   return result;
6597 }
6598
6599 rtx
6600 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6601                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6602 {
6603   HOST_WIDE_INT offset = *offsetp;
6604   int i = 0, j;
6605   rtx result;
6606   int sign = up ? 1 : -1;
6607   rtx mem, addr;
6608
6609   /* See arm_gen_load_multiple for discussion of
6610      the pros/cons of ldm/stm usage for XScale.  */
6611   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6612     {
6613       rtx seq;
6614
6615       start_sequence ();
6616
6617       for (i = 0; i < count; i++)
6618         {
6619           addr = plus_constant (to, i * 4 * sign);
6620           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6621           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6622           offset += 4 * sign;
6623         }
6624
6625       if (write_back)
6626         {
6627           emit_move_insn (to, plus_constant (to, count * 4 * sign));
6628           *offsetp = offset;
6629         }
6630
6631       seq = get_insns ();
6632       end_sequence ();
6633
6634       return seq;
6635     }
6636
6637   result = gen_rtx_PARALLEL (VOIDmode,
6638                              rtvec_alloc (count + (write_back ? 1 : 0)));
6639   if (write_back)
6640     {
6641       XVECEXP (result, 0, 0)
6642         = gen_rtx_SET (VOIDmode, to,
6643                        plus_constant (to, count * 4 * sign));
6644       i = 1;
6645       count++;
6646     }
6647
6648   for (j = 0; i < count; i++, j++)
6649     {
6650       addr = plus_constant (to, j * 4 * sign);
6651       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6652       XVECEXP (result, 0, i)
6653         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6654       offset += 4 * sign;
6655     }
6656
6657   if (write_back)
6658     *offsetp = offset;
6659
6660   return result;
6661 }
6662
6663 int
6664 arm_gen_movmemqi (rtx *operands)
6665 {
6666   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6667   HOST_WIDE_INT srcoffset, dstoffset;
6668   int i;
6669   rtx src, dst, srcbase, dstbase;
6670   rtx part_bytes_reg = NULL;
6671   rtx mem;
6672
6673   if (GET_CODE (operands[2]) != CONST_INT
6674       || GET_CODE (operands[3]) != CONST_INT
6675       || INTVAL (operands[2]) > 64
6676       || INTVAL (operands[3]) & 3)
6677     return 0;
6678
6679   dstbase = operands[0];
6680   srcbase = operands[1];
6681
6682   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6683   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6684
6685   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6686   out_words_to_go = INTVAL (operands[2]) / 4;
6687   last_bytes = INTVAL (operands[2]) & 3;
6688   dstoffset = srcoffset = 0;
6689
6690   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6691     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6692
6693   for (i = 0; in_words_to_go >= 2; i+=4)
6694     {
6695       if (in_words_to_go > 4)
6696         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6697                                           srcbase, &srcoffset));
6698       else
6699         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6700                                           FALSE, srcbase, &srcoffset));
6701
6702       if (out_words_to_go)
6703         {
6704           if (out_words_to_go > 4)
6705             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6706                                                dstbase, &dstoffset));
6707           else if (out_words_to_go != 1)
6708             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6709                                                dst, TRUE,
6710                                                (last_bytes == 0
6711                                                 ? FALSE : TRUE),
6712                                                dstbase, &dstoffset));
6713           else
6714             {
6715               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6716               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6717               if (last_bytes != 0)
6718                 {
6719                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6720                   dstoffset += 4;
6721                 }
6722             }
6723         }
6724
6725       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6726       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6727     }
6728
6729   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6730   if (out_words_to_go)
6731     {
6732       rtx sreg;
6733
6734       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6735       sreg = copy_to_reg (mem);
6736
6737       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6738       emit_move_insn (mem, sreg);
6739       in_words_to_go--;
6740
6741       gcc_assert (!in_words_to_go);     /* Sanity check */
6742     }
6743
6744   if (in_words_to_go)
6745     {
6746       gcc_assert (in_words_to_go > 0);
6747
6748       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6749       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6750     }
6751
6752   gcc_assert (!last_bytes || part_bytes_reg);
6753
6754   if (BYTES_BIG_ENDIAN && last_bytes)
6755     {
6756       rtx tmp = gen_reg_rtx (SImode);
6757
6758       /* The bytes we want are in the top end of the word.  */
6759       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6760                               GEN_INT (8 * (4 - last_bytes))));
6761       part_bytes_reg = tmp;
6762
6763       while (last_bytes)
6764         {
6765           mem = adjust_automodify_address (dstbase, QImode,
6766                                            plus_constant (dst, last_bytes - 1),
6767                                            dstoffset + last_bytes - 1);
6768           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6769
6770           if (--last_bytes)
6771             {
6772               tmp = gen_reg_rtx (SImode);
6773               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6774               part_bytes_reg = tmp;
6775             }
6776         }
6777
6778     }
6779   else
6780     {
6781       if (last_bytes > 1)
6782         {
6783           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6784           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6785           last_bytes -= 2;
6786           if (last_bytes)
6787             {
6788               rtx tmp = gen_reg_rtx (SImode);
6789               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6790               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6791               part_bytes_reg = tmp;
6792               dstoffset += 2;
6793             }
6794         }
6795
6796       if (last_bytes)
6797         {
6798           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6799           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6800         }
6801     }
6802
6803   return 1;
6804 }
6805
6806 /* Select a dominance comparison mode if possible for a test of the general
6807    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6808    COND_OR == DOM_CC_X_AND_Y => (X && Y)
6809    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6810    COND_OR == DOM_CC_X_OR_Y => (X || Y)
6811    In all cases OP will be either EQ or NE, but we don't need to know which
6812    here.  If we are unable to support a dominance comparison we return
6813    CC mode.  This will then fail to match for the RTL expressions that
6814    generate this call.  */
6815 enum machine_mode
6816 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6817 {
6818   enum rtx_code cond1, cond2;
6819   int swapped = 0;
6820
6821   /* Currently we will probably get the wrong result if the individual
6822      comparisons are not simple.  This also ensures that it is safe to
6823      reverse a comparison if necessary.  */
6824   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6825        != CCmode)
6826       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6827           != CCmode))
6828     return CCmode;
6829
6830   /* The if_then_else variant of this tests the second condition if the
6831      first passes, but is true if the first fails.  Reverse the first
6832      condition to get a true "inclusive-or" expression.  */
6833   if (cond_or == DOM_CC_NX_OR_Y)
6834     cond1 = reverse_condition (cond1);
6835
6836   /* If the comparisons are not equal, and one doesn't dominate the other,
6837      then we can't do this.  */
6838   if (cond1 != cond2
6839       && !comparison_dominates_p (cond1, cond2)
6840       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6841     return CCmode;
6842
6843   if (swapped)
6844     {
6845       enum rtx_code temp = cond1;
6846       cond1 = cond2;
6847       cond2 = temp;
6848     }
6849
6850   switch (cond1)
6851     {
6852     case EQ:
6853       if (cond_or == DOM_CC_X_AND_Y)
6854         return CC_DEQmode;
6855
6856       switch (cond2)
6857         {
6858         case EQ: return CC_DEQmode;
6859         case LE: return CC_DLEmode;
6860         case LEU: return CC_DLEUmode;
6861         case GE: return CC_DGEmode;
6862         case GEU: return CC_DGEUmode;
6863         default: gcc_unreachable ();
6864         }
6865
6866     case LT:
6867       if (cond_or == DOM_CC_X_AND_Y)
6868         return CC_DLTmode;
6869
6870       switch (cond2)
6871         {
6872         case  LT:
6873             return CC_DLTmode;
6874         case LE:
6875           return CC_DLEmode;
6876         case NE:
6877           return CC_DNEmode;
6878         default:
6879           gcc_unreachable ();
6880         }
6881
6882     case GT:
6883       if (cond_or == DOM_CC_X_AND_Y)
6884         return CC_DGTmode;
6885
6886       switch (cond2)
6887         {
6888         case GT:
6889           return CC_DGTmode;
6890         case GE:
6891           return CC_DGEmode;
6892         case NE:
6893           return CC_DNEmode;
6894         default:
6895           gcc_unreachable ();
6896         }
6897
6898     case LTU:
6899       if (cond_or == DOM_CC_X_AND_Y)
6900         return CC_DLTUmode;
6901
6902       switch (cond2)
6903         {
6904         case LTU:
6905           return CC_DLTUmode;
6906         case LEU:
6907           return CC_DLEUmode;
6908         case NE:
6909           return CC_DNEmode;
6910         default:
6911           gcc_unreachable ();
6912         }
6913
6914     case GTU:
6915       if (cond_or == DOM_CC_X_AND_Y)
6916         return CC_DGTUmode;
6917
6918       switch (cond2)
6919         {
6920         case GTU:
6921           return CC_DGTUmode;
6922         case GEU:
6923           return CC_DGEUmode;
6924         case NE:
6925           return CC_DNEmode;
6926         default:
6927           gcc_unreachable ();
6928         }
6929
6930     /* The remaining cases only occur when both comparisons are the
6931        same.  */
6932     case NE:
6933       gcc_assert (cond1 == cond2);
6934       return CC_DNEmode;
6935
6936     case LE:
6937       gcc_assert (cond1 == cond2);
6938       return CC_DLEmode;
6939
6940     case GE:
6941       gcc_assert (cond1 == cond2);
6942       return CC_DGEmode;
6943
6944     case LEU:
6945       gcc_assert (cond1 == cond2);
6946       return CC_DLEUmode;
6947
6948     case GEU:
6949       gcc_assert (cond1 == cond2);
6950       return CC_DGEUmode;
6951
6952     default:
6953       gcc_unreachable ();
6954     }
6955 }
6956
6957 enum machine_mode
6958 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6959 {
6960   /* All floating point compares return CCFP if it is an equality
6961      comparison, and CCFPE otherwise.  */
6962   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6963     {
6964       switch (op)
6965         {
6966         case EQ:
6967         case NE:
6968         case UNORDERED:
6969         case ORDERED:
6970         case UNLT:
6971         case UNLE:
6972         case UNGT:
6973         case UNGE:
6974         case UNEQ:
6975         case LTGT:
6976           return CCFPmode;
6977
6978         case LT:
6979         case LE:
6980         case GT:
6981         case GE:
6982           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6983             return CCFPmode;
6984           return CCFPEmode;
6985
6986         default:
6987           gcc_unreachable ();
6988         }
6989     }
6990
6991   /* A compare with a shifted operand.  Because of canonicalization, the
6992      comparison will have to be swapped when we emit the assembler.  */
6993   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6994       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6995           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6996           || GET_CODE (x) == ROTATERT))
6997     return CC_SWPmode;
6998
6999   /* This operation is performed swapped, but since we only rely on the Z
7000      flag we don't need an additional mode.  */
7001   if (GET_MODE (y) == SImode && REG_P (y)
7002       && GET_CODE (x) == NEG
7003       && (op == EQ || op == NE))
7004     return CC_Zmode;
7005
7006   /* This is a special case that is used by combine to allow a
7007      comparison of a shifted byte load to be split into a zero-extend
7008      followed by a comparison of the shifted integer (only valid for
7009      equalities and unsigned inequalities).  */
7010   if (GET_MODE (x) == SImode
7011       && GET_CODE (x) == ASHIFT
7012       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
7013       && GET_CODE (XEXP (x, 0)) == SUBREG
7014       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
7015       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
7016       && (op == EQ || op == NE
7017           || op == GEU || op == GTU || op == LTU || op == LEU)
7018       && GET_CODE (y) == CONST_INT)
7019     return CC_Zmode;
7020
7021   /* A construct for a conditional compare, if the false arm contains
7022      0, then both conditions must be true, otherwise either condition
7023      must be true.  Not all conditions are possible, so CCmode is
7024      returned if it can't be done.  */
7025   if (GET_CODE (x) == IF_THEN_ELSE
7026       && (XEXP (x, 2) == const0_rtx
7027           || XEXP (x, 2) == const1_rtx)
7028       && COMPARISON_P (XEXP (x, 0))
7029       && COMPARISON_P (XEXP (x, 1)))
7030     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7031                                          INTVAL (XEXP (x, 2)));
7032
7033   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
7034   if (GET_CODE (x) == AND
7035       && COMPARISON_P (XEXP (x, 0))
7036       && COMPARISON_P (XEXP (x, 1)))
7037     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7038                                          DOM_CC_X_AND_Y);
7039
7040   if (GET_CODE (x) == IOR
7041       && COMPARISON_P (XEXP (x, 0))
7042       && COMPARISON_P (XEXP (x, 1)))
7043     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
7044                                          DOM_CC_X_OR_Y);
7045
7046   /* An operation (on Thumb) where we want to test for a single bit.
7047      This is done by shifting that bit up into the top bit of a
7048      scratch register; we can then branch on the sign bit.  */
7049   if (TARGET_THUMB1
7050       && GET_MODE (x) == SImode
7051       && (op == EQ || op == NE)
7052       && GET_CODE (x) == ZERO_EXTRACT
7053       && XEXP (x, 1) == const1_rtx)
7054     return CC_Nmode;
7055
7056   /* An operation that sets the condition codes as a side-effect, the
7057      V flag is not set correctly, so we can only use comparisons where
7058      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
7059      instead.)  */
7060   /* ??? Does the ZERO_EXTRACT case really apply to thumb2?  */
7061   if (GET_MODE (x) == SImode
7062       && y == const0_rtx
7063       && (op == EQ || op == NE || op == LT || op == GE)
7064       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
7065           || GET_CODE (x) == AND || GET_CODE (x) == IOR
7066           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
7067           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
7068           || GET_CODE (x) == LSHIFTRT
7069           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
7070           || GET_CODE (x) == ROTATERT
7071           || (TARGET_32BIT && GET_CODE (x) == ZERO_EXTRACT)))
7072     return CC_NOOVmode;
7073
7074   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
7075     return CC_Zmode;
7076
7077   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
7078       && GET_CODE (x) == PLUS
7079       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
7080     return CC_Cmode;
7081
7082   return CCmode;
7083 }
7084
7085 /* X and Y are two things to compare using CODE.  Emit the compare insn and
7086    return the rtx for register 0 in the proper mode.  FP means this is a
7087    floating point compare: I don't think that it is needed on the arm.  */
7088 rtx
7089 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
7090 {
7091   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
7092   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
7093
7094   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
7095
7096   return cc_reg;
7097 }
7098
7099 /* Generate a sequence of insns that will generate the correct return
7100    address mask depending on the physical architecture that the program
7101    is running on.  */
7102 rtx
7103 arm_gen_return_addr_mask (void)
7104 {
7105   rtx reg = gen_reg_rtx (Pmode);
7106
7107   emit_insn (gen_return_addr_mask (reg));
7108   return reg;
7109 }
7110
7111 void
7112 arm_reload_in_hi (rtx *operands)
7113 {
7114   rtx ref = operands[1];
7115   rtx base, scratch;
7116   HOST_WIDE_INT offset = 0;
7117
7118   if (GET_CODE (ref) == SUBREG)
7119     {
7120       offset = SUBREG_BYTE (ref);
7121       ref = SUBREG_REG (ref);
7122     }
7123
7124   if (GET_CODE (ref) == REG)
7125     {
7126       /* We have a pseudo which has been spilt onto the stack; there
7127          are two cases here: the first where there is a simple
7128          stack-slot replacement and a second where the stack-slot is
7129          out of range, or is used as a subreg.  */
7130       if (reg_equiv_mem[REGNO (ref)])
7131         {
7132           ref = reg_equiv_mem[REGNO (ref)];
7133           base = find_replacement (&XEXP (ref, 0));
7134         }
7135       else
7136         /* The slot is out of range, or was dressed up in a SUBREG.  */
7137         base = reg_equiv_address[REGNO (ref)];
7138     }
7139   else
7140     base = find_replacement (&XEXP (ref, 0));
7141
7142   /* Handle the case where the address is too complex to be offset by 1.  */
7143   if (GET_CODE (base) == MINUS
7144       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7145     {
7146       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7147
7148       emit_set_insn (base_plus, base);
7149       base = base_plus;
7150     }
7151   else if (GET_CODE (base) == PLUS)
7152     {
7153       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7154       HOST_WIDE_INT hi, lo;
7155
7156       offset += INTVAL (XEXP (base, 1));
7157       base = XEXP (base, 0);
7158
7159       /* Rework the address into a legal sequence of insns.  */
7160       /* Valid range for lo is -4095 -> 4095 */
7161       lo = (offset >= 0
7162             ? (offset & 0xfff)
7163             : -((-offset) & 0xfff));
7164
7165       /* Corner case, if lo is the max offset then we would be out of range
7166          once we have added the additional 1 below, so bump the msb into the
7167          pre-loading insn(s).  */
7168       if (lo == 4095)
7169         lo &= 0x7ff;
7170
7171       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7172              ^ (HOST_WIDE_INT) 0x80000000)
7173             - (HOST_WIDE_INT) 0x80000000);
7174
7175       gcc_assert (hi + lo == offset);
7176
7177       if (hi != 0)
7178         {
7179           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7180
7181           /* Get the base address; addsi3 knows how to handle constants
7182              that require more than one insn.  */
7183           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7184           base = base_plus;
7185           offset = lo;
7186         }
7187     }
7188
7189   /* Operands[2] may overlap operands[0] (though it won't overlap
7190      operands[1]), that's why we asked for a DImode reg -- so we can
7191      use the bit that does not overlap.  */
7192   if (REGNO (operands[2]) == REGNO (operands[0]))
7193     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7194   else
7195     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7196
7197   emit_insn (gen_zero_extendqisi2 (scratch,
7198                                    gen_rtx_MEM (QImode,
7199                                                 plus_constant (base,
7200                                                                offset))));
7201   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
7202                                    gen_rtx_MEM (QImode,
7203                                                 plus_constant (base,
7204                                                                offset + 1))));
7205   if (!BYTES_BIG_ENDIAN)
7206     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
7207                    gen_rtx_IOR (SImode,
7208                                 gen_rtx_ASHIFT
7209                                 (SImode,
7210                                  gen_rtx_SUBREG (SImode, operands[0], 0),
7211                                  GEN_INT (8)),
7212                                 scratch));
7213   else
7214     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
7215                    gen_rtx_IOR (SImode,
7216                                 gen_rtx_ASHIFT (SImode, scratch,
7217                                                 GEN_INT (8)),
7218                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
7219 }
7220
7221 /* Handle storing a half-word to memory during reload by synthesizing as two
7222    byte stores.  Take care not to clobber the input values until after we
7223    have moved them somewhere safe.  This code assumes that if the DImode
7224    scratch in operands[2] overlaps either the input value or output address
7225    in some way, then that value must die in this insn (we absolutely need
7226    two scratch registers for some corner cases).  */
7227 void
7228 arm_reload_out_hi (rtx *operands)
7229 {
7230   rtx ref = operands[0];
7231   rtx outval = operands[1];
7232   rtx base, scratch;
7233   HOST_WIDE_INT offset = 0;
7234
7235   if (GET_CODE (ref) == SUBREG)
7236     {
7237       offset = SUBREG_BYTE (ref);
7238       ref = SUBREG_REG (ref);
7239     }
7240
7241   if (GET_CODE (ref) == REG)
7242     {
7243       /* We have a pseudo which has been spilt onto the stack; there
7244          are two cases here: the first where there is a simple
7245          stack-slot replacement and a second where the stack-slot is
7246          out of range, or is used as a subreg.  */
7247       if (reg_equiv_mem[REGNO (ref)])
7248         {
7249           ref = reg_equiv_mem[REGNO (ref)];
7250           base = find_replacement (&XEXP (ref, 0));
7251         }
7252       else
7253         /* The slot is out of range, or was dressed up in a SUBREG.  */
7254         base = reg_equiv_address[REGNO (ref)];
7255     }
7256   else
7257     base = find_replacement (&XEXP (ref, 0));
7258
7259   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
7260
7261   /* Handle the case where the address is too complex to be offset by 1.  */
7262   if (GET_CODE (base) == MINUS
7263       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
7264     {
7265       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7266
7267       /* Be careful not to destroy OUTVAL.  */
7268       if (reg_overlap_mentioned_p (base_plus, outval))
7269         {
7270           /* Updating base_plus might destroy outval, see if we can
7271              swap the scratch and base_plus.  */
7272           if (!reg_overlap_mentioned_p (scratch, outval))
7273             {
7274               rtx tmp = scratch;
7275               scratch = base_plus;
7276               base_plus = tmp;
7277             }
7278           else
7279             {
7280               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7281
7282               /* Be conservative and copy OUTVAL into the scratch now,
7283                  this should only be necessary if outval is a subreg
7284                  of something larger than a word.  */
7285               /* XXX Might this clobber base?  I can't see how it can,
7286                  since scratch is known to overlap with OUTVAL, and
7287                  must be wider than a word.  */
7288               emit_insn (gen_movhi (scratch_hi, outval));
7289               outval = scratch_hi;
7290             }
7291         }
7292
7293       emit_set_insn (base_plus, base);
7294       base = base_plus;
7295     }
7296   else if (GET_CODE (base) == PLUS)
7297     {
7298       /* The addend must be CONST_INT, or we would have dealt with it above.  */
7299       HOST_WIDE_INT hi, lo;
7300
7301       offset += INTVAL (XEXP (base, 1));
7302       base = XEXP (base, 0);
7303
7304       /* Rework the address into a legal sequence of insns.  */
7305       /* Valid range for lo is -4095 -> 4095 */
7306       lo = (offset >= 0
7307             ? (offset & 0xfff)
7308             : -((-offset) & 0xfff));
7309
7310       /* Corner case, if lo is the max offset then we would be out of range
7311          once we have added the additional 1 below, so bump the msb into the
7312          pre-loading insn(s).  */
7313       if (lo == 4095)
7314         lo &= 0x7ff;
7315
7316       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7317              ^ (HOST_WIDE_INT) 0x80000000)
7318             - (HOST_WIDE_INT) 0x80000000);
7319
7320       gcc_assert (hi + lo == offset);
7321
7322       if (hi != 0)
7323         {
7324           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7325
7326           /* Be careful not to destroy OUTVAL.  */
7327           if (reg_overlap_mentioned_p (base_plus, outval))
7328             {
7329               /* Updating base_plus might destroy outval, see if we
7330                  can swap the scratch and base_plus.  */
7331               if (!reg_overlap_mentioned_p (scratch, outval))
7332                 {
7333                   rtx tmp = scratch;
7334                   scratch = base_plus;
7335                   base_plus = tmp;
7336                 }
7337               else
7338                 {
7339                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7340
7341                   /* Be conservative and copy outval into scratch now,
7342                      this should only be necessary if outval is a
7343                      subreg of something larger than a word.  */
7344                   /* XXX Might this clobber base?  I can't see how it
7345                      can, since scratch is known to overlap with
7346                      outval.  */
7347                   emit_insn (gen_movhi (scratch_hi, outval));
7348                   outval = scratch_hi;
7349                 }
7350             }
7351
7352           /* Get the base address; addsi3 knows how to handle constants
7353              that require more than one insn.  */
7354           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7355           base = base_plus;
7356           offset = lo;
7357         }
7358     }
7359
7360   if (BYTES_BIG_ENDIAN)
7361     {
7362       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7363                                          plus_constant (base, offset + 1)),
7364                             gen_lowpart (QImode, outval)));
7365       emit_insn (gen_lshrsi3 (scratch,
7366                               gen_rtx_SUBREG (SImode, outval, 0),
7367                               GEN_INT (8)));
7368       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7369                             gen_lowpart (QImode, scratch)));
7370     }
7371   else
7372     {
7373       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7374                             gen_lowpart (QImode, outval)));
7375       emit_insn (gen_lshrsi3 (scratch,
7376                               gen_rtx_SUBREG (SImode, outval, 0),
7377                               GEN_INT (8)));
7378       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7379                                          plus_constant (base, offset + 1)),
7380                             gen_lowpart (QImode, scratch)));
7381     }
7382 }
7383
7384 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7385    (padded to the size of a word) should be passed in a register.  */
7386
7387 static bool
7388 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7389 {
7390   if (TARGET_AAPCS_BASED)
7391     return must_pass_in_stack_var_size (mode, type);
7392   else
7393     return must_pass_in_stack_var_size_or_pad (mode, type);
7394 }
7395
7396
7397 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7398    Return true if an argument passed on the stack should be padded upwards,
7399    i.e. if the least-significant byte has useful data.
7400    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
7401    aggregate types are placed in the lowest memory address.  */
7402
7403 bool
7404 arm_pad_arg_upward (enum machine_mode mode, tree type)
7405 {
7406   if (!TARGET_AAPCS_BASED)
7407     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7408
7409   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7410     return false;
7411
7412   return true;
7413 }
7414
7415
7416 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7417    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7418    byte of the register has useful data, and return the opposite if the
7419    most significant byte does.
7420    For AAPCS, small aggregates and small complex types are always padded
7421    upwards.  */
7422
7423 bool
7424 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7425                     tree type, int first ATTRIBUTE_UNUSED)
7426 {
7427   if (TARGET_AAPCS_BASED
7428       && BYTES_BIG_ENDIAN
7429       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7430       && int_size_in_bytes (type) <= 4)
7431     return true;
7432
7433   /* Otherwise, use default padding.  */
7434   return !BYTES_BIG_ENDIAN;
7435 }
7436
7437 \f
7438 /* Print a symbolic form of X to the debug file, F.  */
7439 static void
7440 arm_print_value (FILE *f, rtx x)
7441 {
7442   switch (GET_CODE (x))
7443     {
7444     case CONST_INT:
7445       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7446       return;
7447
7448     case CONST_DOUBLE:
7449       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7450       return;
7451
7452     case CONST_VECTOR:
7453       {
7454         int i;
7455
7456         fprintf (f, "<");
7457         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7458           {
7459             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7460             if (i < (CONST_VECTOR_NUNITS (x) - 1))
7461               fputc (',', f);
7462           }
7463         fprintf (f, ">");
7464       }
7465       return;
7466
7467     case CONST_STRING:
7468       fprintf (f, "\"%s\"", XSTR (x, 0));
7469       return;
7470
7471     case SYMBOL_REF:
7472       fprintf (f, "`%s'", XSTR (x, 0));
7473       return;
7474
7475     case LABEL_REF:
7476       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7477       return;
7478
7479     case CONST:
7480       arm_print_value (f, XEXP (x, 0));
7481       return;
7482
7483     case PLUS:
7484       arm_print_value (f, XEXP (x, 0));
7485       fprintf (f, "+");
7486       arm_print_value (f, XEXP (x, 1));
7487       return;
7488
7489     case PC:
7490       fprintf (f, "pc");
7491       return;
7492
7493     default:
7494       fprintf (f, "????");
7495       return;
7496     }
7497 }
7498 \f
7499 /* Routines for manipulation of the constant pool.  */
7500
7501 /* Arm instructions cannot load a large constant directly into a
7502    register; they have to come from a pc relative load.  The constant
7503    must therefore be placed in the addressable range of the pc
7504    relative load.  Depending on the precise pc relative load
7505    instruction the range is somewhere between 256 bytes and 4k.  This
7506    means that we often have to dump a constant inside a function, and
7507    generate code to branch around it.
7508
7509    It is important to minimize this, since the branches will slow
7510    things down and make the code larger.
7511
7512    Normally we can hide the table after an existing unconditional
7513    branch so that there is no interruption of the flow, but in the
7514    worst case the code looks like this:
7515
7516         ldr     rn, L1
7517         ...
7518         b       L2
7519         align
7520         L1:     .long value
7521         L2:
7522         ...
7523
7524         ldr     rn, L3
7525         ...
7526         b       L4
7527         align
7528         L3:     .long value
7529         L4:
7530         ...
7531
7532    We fix this by performing a scan after scheduling, which notices
7533    which instructions need to have their operands fetched from the
7534    constant table and builds the table.
7535
7536    The algorithm starts by building a table of all the constants that
7537    need fixing up and all the natural barriers in the function (places
7538    where a constant table can be dropped without breaking the flow).
7539    For each fixup we note how far the pc-relative replacement will be
7540    able to reach and the offset of the instruction into the function.
7541
7542    Having built the table we then group the fixes together to form
7543    tables that are as large as possible (subject to addressing
7544    constraints) and emit each table of constants after the last
7545    barrier that is within range of all the instructions in the group.
7546    If a group does not contain a barrier, then we forcibly create one
7547    by inserting a jump instruction into the flow.  Once the table has
7548    been inserted, the insns are then modified to reference the
7549    relevant entry in the pool.
7550
7551    Possible enhancements to the algorithm (not implemented) are:
7552
7553    1) For some processors and object formats, there may be benefit in
7554    aligning the pools to the start of cache lines; this alignment
7555    would need to be taken into account when calculating addressability
7556    of a pool.  */
7557
7558 /* These typedefs are located at the start of this file, so that
7559    they can be used in the prototypes there.  This comment is to
7560    remind readers of that fact so that the following structures
7561    can be understood more easily.
7562
7563      typedef struct minipool_node    Mnode;
7564      typedef struct minipool_fixup   Mfix;  */
7565
7566 struct minipool_node
7567 {
7568   /* Doubly linked chain of entries.  */
7569   Mnode * next;
7570   Mnode * prev;
7571   /* The maximum offset into the code that this entry can be placed.  While
7572      pushing fixes for forward references, all entries are sorted in order
7573      of increasing max_address.  */
7574   HOST_WIDE_INT max_address;
7575   /* Similarly for an entry inserted for a backwards ref.  */
7576   HOST_WIDE_INT min_address;
7577   /* The number of fixes referencing this entry.  This can become zero
7578      if we "unpush" an entry.  In this case we ignore the entry when we
7579      come to emit the code.  */
7580   int refcount;
7581   /* The offset from the start of the minipool.  */
7582   HOST_WIDE_INT offset;
7583   /* The value in table.  */
7584   rtx value;
7585   /* The mode of value.  */
7586   enum machine_mode mode;
7587   /* The size of the value.  With iWMMXt enabled
7588      sizes > 4 also imply an alignment of 8-bytes.  */
7589   int fix_size;
7590 };
7591
7592 struct minipool_fixup
7593 {
7594   Mfix *            next;
7595   rtx               insn;
7596   HOST_WIDE_INT     address;
7597   rtx *             loc;
7598   enum machine_mode mode;
7599   int               fix_size;
7600   rtx               value;
7601   Mnode *           minipool;
7602   HOST_WIDE_INT     forwards;
7603   HOST_WIDE_INT     backwards;
7604 };
7605
7606 /* Fixes less than a word need padding out to a word boundary.  */
7607 #define MINIPOOL_FIX_SIZE(mode) \
7608   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7609
7610 static Mnode *  minipool_vector_head;
7611 static Mnode *  minipool_vector_tail;
7612 static rtx      minipool_vector_label;
7613 static int      minipool_pad;
7614
7615 /* The linked list of all minipool fixes required for this function.  */
7616 Mfix *          minipool_fix_head;
7617 Mfix *          minipool_fix_tail;
7618 /* The fix entry for the current minipool, once it has been placed.  */
7619 Mfix *          minipool_barrier;
7620
7621 /* Determines if INSN is the start of a jump table.  Returns the end
7622    of the TABLE or NULL_RTX.  */
7623 static rtx
7624 is_jump_table (rtx insn)
7625 {
7626   rtx table;
7627
7628   if (GET_CODE (insn) == JUMP_INSN
7629       && JUMP_LABEL (insn) != NULL
7630       && ((table = next_real_insn (JUMP_LABEL (insn)))
7631           == next_real_insn (insn))
7632       && table != NULL
7633       && GET_CODE (table) == JUMP_INSN
7634       && (GET_CODE (PATTERN (table)) == ADDR_VEC
7635           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7636     return table;
7637
7638   return NULL_RTX;
7639 }
7640
7641 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7642 #define JUMP_TABLES_IN_TEXT_SECTION 0
7643 #endif
7644
7645 static HOST_WIDE_INT
7646 get_jump_table_size (rtx insn)
7647 {
7648   /* ADDR_VECs only take room if read-only data does into the text
7649      section.  */
7650   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7651     {
7652       rtx body = PATTERN (insn);
7653       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7654       HOST_WIDE_INT size;
7655       HOST_WIDE_INT modesize;
7656
7657       modesize = GET_MODE_SIZE (GET_MODE (body));
7658       size = modesize * XVECLEN (body, elt);
7659       switch (modesize)
7660         {
7661         case 1:
7662           /* Round up size  of TBB table to a halfword boundary.  */
7663           size = (size + 1) & ~(HOST_WIDE_INT)1;
7664           break;
7665         case 2:
7666           /* No padding necessary for TBH.  */
7667           break;
7668         case 4:
7669           /* Add two bytes for alignment on Thumb.  */
7670           if (TARGET_THUMB)
7671             size += 2;
7672           break;
7673         default:
7674           gcc_unreachable ();
7675         }
7676       return size;
7677     }
7678
7679   return 0;
7680 }
7681
7682 /* Move a minipool fix MP from its current location to before MAX_MP.
7683    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7684    constraints may need updating.  */
7685 static Mnode *
7686 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7687                                HOST_WIDE_INT max_address)
7688 {
7689   /* The code below assumes these are different.  */
7690   gcc_assert (mp != max_mp);
7691
7692   if (max_mp == NULL)
7693     {
7694       if (max_address < mp->max_address)
7695         mp->max_address = max_address;
7696     }
7697   else
7698     {
7699       if (max_address > max_mp->max_address - mp->fix_size)
7700         mp->max_address = max_mp->max_address - mp->fix_size;
7701       else
7702         mp->max_address = max_address;
7703
7704       /* Unlink MP from its current position.  Since max_mp is non-null,
7705        mp->prev must be non-null.  */
7706       mp->prev->next = mp->next;
7707       if (mp->next != NULL)
7708         mp->next->prev = mp->prev;
7709       else
7710         minipool_vector_tail = mp->prev;
7711
7712       /* Re-insert it before MAX_MP.  */
7713       mp->next = max_mp;
7714       mp->prev = max_mp->prev;
7715       max_mp->prev = mp;
7716
7717       if (mp->prev != NULL)
7718         mp->prev->next = mp;
7719       else
7720         minipool_vector_head = mp;
7721     }
7722
7723   /* Save the new entry.  */
7724   max_mp = mp;
7725
7726   /* Scan over the preceding entries and adjust their addresses as
7727      required.  */
7728   while (mp->prev != NULL
7729          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7730     {
7731       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7732       mp = mp->prev;
7733     }
7734
7735   return max_mp;
7736 }
7737
7738 /* Add a constant to the minipool for a forward reference.  Returns the
7739    node added or NULL if the constant will not fit in this pool.  */
7740 static Mnode *
7741 add_minipool_forward_ref (Mfix *fix)
7742 {
7743   /* If set, max_mp is the first pool_entry that has a lower
7744      constraint than the one we are trying to add.  */
7745   Mnode *       max_mp = NULL;
7746   HOST_WIDE_INT max_address = fix->address + fix->forwards - minipool_pad;
7747   Mnode *       mp;
7748
7749   /* If the minipool starts before the end of FIX->INSN then this FIX
7750      can not be placed into the current pool.  Furthermore, adding the
7751      new constant pool entry may cause the pool to start FIX_SIZE bytes
7752      earlier.  */
7753   if (minipool_vector_head &&
7754       (fix->address + get_attr_length (fix->insn)
7755        >= minipool_vector_head->max_address - fix->fix_size))
7756     return NULL;
7757
7758   /* Scan the pool to see if a constant with the same value has
7759      already been added.  While we are doing this, also note the
7760      location where we must insert the constant if it doesn't already
7761      exist.  */
7762   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7763     {
7764       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7765           && fix->mode == mp->mode
7766           && (GET_CODE (fix->value) != CODE_LABEL
7767               || (CODE_LABEL_NUMBER (fix->value)
7768                   == CODE_LABEL_NUMBER (mp->value)))
7769           && rtx_equal_p (fix->value, mp->value))
7770         {
7771           /* More than one fix references this entry.  */
7772           mp->refcount++;
7773           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7774         }
7775
7776       /* Note the insertion point if necessary.  */
7777       if (max_mp == NULL
7778           && mp->max_address > max_address)
7779         max_mp = mp;
7780
7781       /* If we are inserting an 8-bytes aligned quantity and
7782          we have not already found an insertion point, then
7783          make sure that all such 8-byte aligned quantities are
7784          placed at the start of the pool.  */
7785       if (ARM_DOUBLEWORD_ALIGN
7786           && max_mp == NULL
7787           && fix->fix_size == 8
7788           && mp->fix_size != 8)
7789         {
7790           max_mp = mp;
7791           max_address = mp->max_address;
7792         }
7793     }
7794
7795   /* The value is not currently in the minipool, so we need to create
7796      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7797      the end of the list since the placement is less constrained than
7798      any existing entry.  Otherwise, we insert the new fix before
7799      MAX_MP and, if necessary, adjust the constraints on the other
7800      entries.  */
7801   mp = XNEW (Mnode);
7802   mp->fix_size = fix->fix_size;
7803   mp->mode = fix->mode;
7804   mp->value = fix->value;
7805   mp->refcount = 1;
7806   /* Not yet required for a backwards ref.  */
7807   mp->min_address = -65536;
7808
7809   if (max_mp == NULL)
7810     {
7811       mp->max_address = max_address;
7812       mp->next = NULL;
7813       mp->prev = minipool_vector_tail;
7814
7815       if (mp->prev == NULL)
7816         {
7817           minipool_vector_head = mp;
7818           minipool_vector_label = gen_label_rtx ();
7819         }
7820       else
7821         mp->prev->next = mp;
7822
7823       minipool_vector_tail = mp;
7824     }
7825   else
7826     {
7827       if (max_address > max_mp->max_address - mp->fix_size)
7828         mp->max_address = max_mp->max_address - mp->fix_size;
7829       else
7830         mp->max_address = max_address;
7831
7832       mp->next = max_mp;
7833       mp->prev = max_mp->prev;
7834       max_mp->prev = mp;
7835       if (mp->prev != NULL)
7836         mp->prev->next = mp;
7837       else
7838         minipool_vector_head = mp;
7839     }
7840
7841   /* Save the new entry.  */
7842   max_mp = mp;
7843
7844   /* Scan over the preceding entries and adjust their addresses as
7845      required.  */
7846   while (mp->prev != NULL
7847          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7848     {
7849       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7850       mp = mp->prev;
7851     }
7852
7853   return max_mp;
7854 }
7855
7856 static Mnode *
7857 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7858                                 HOST_WIDE_INT  min_address)
7859 {
7860   HOST_WIDE_INT offset;
7861
7862   /* The code below assumes these are different.  */
7863   gcc_assert (mp != min_mp);
7864
7865   if (min_mp == NULL)
7866     {
7867       if (min_address > mp->min_address)
7868         mp->min_address = min_address;
7869     }
7870   else
7871     {
7872       /* We will adjust this below if it is too loose.  */
7873       mp->min_address = min_address;
7874
7875       /* Unlink MP from its current position.  Since min_mp is non-null,
7876          mp->next must be non-null.  */
7877       mp->next->prev = mp->prev;
7878       if (mp->prev != NULL)
7879         mp->prev->next = mp->next;
7880       else
7881         minipool_vector_head = mp->next;
7882
7883       /* Reinsert it after MIN_MP.  */
7884       mp->prev = min_mp;
7885       mp->next = min_mp->next;
7886       min_mp->next = mp;
7887       if (mp->next != NULL)
7888         mp->next->prev = mp;
7889       else
7890         minipool_vector_tail = mp;
7891     }
7892
7893   min_mp = mp;
7894
7895   offset = 0;
7896   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7897     {
7898       mp->offset = offset;
7899       if (mp->refcount > 0)
7900         offset += mp->fix_size;
7901
7902       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7903         mp->next->min_address = mp->min_address + mp->fix_size;
7904     }
7905
7906   return min_mp;
7907 }
7908
7909 /* Add a constant to the minipool for a backward reference.  Returns the
7910    node added or NULL if the constant will not fit in this pool.
7911
7912    Note that the code for insertion for a backwards reference can be
7913    somewhat confusing because the calculated offsets for each fix do
7914    not take into account the size of the pool (which is still under
7915    construction.  */
7916 static Mnode *
7917 add_minipool_backward_ref (Mfix *fix)
7918 {
7919   /* If set, min_mp is the last pool_entry that has a lower constraint
7920      than the one we are trying to add.  */
7921   Mnode *min_mp = NULL;
7922   /* This can be negative, since it is only a constraint.  */
7923   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7924   Mnode *mp;
7925
7926   /* If we can't reach the current pool from this insn, or if we can't
7927      insert this entry at the end of the pool without pushing other
7928      fixes out of range, then we don't try.  This ensures that we
7929      can't fail later on.  */
7930   if (min_address >= minipool_barrier->address
7931       || (minipool_vector_tail->min_address + fix->fix_size
7932           >= minipool_barrier->address))
7933     return NULL;
7934
7935   /* Scan the pool to see if a constant with the same value has
7936      already been added.  While we are doing this, also note the
7937      location where we must insert the constant if it doesn't already
7938      exist.  */
7939   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7940     {
7941       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7942           && fix->mode == mp->mode
7943           && (GET_CODE (fix->value) != CODE_LABEL
7944               || (CODE_LABEL_NUMBER (fix->value)
7945                   == CODE_LABEL_NUMBER (mp->value)))
7946           && rtx_equal_p (fix->value, mp->value)
7947           /* Check that there is enough slack to move this entry to the
7948              end of the table (this is conservative).  */
7949           && (mp->max_address
7950               > (minipool_barrier->address
7951                  + minipool_vector_tail->offset
7952                  + minipool_vector_tail->fix_size)))
7953         {
7954           mp->refcount++;
7955           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7956         }
7957
7958       if (min_mp != NULL)
7959         mp->min_address += fix->fix_size;
7960       else
7961         {
7962           /* Note the insertion point if necessary.  */
7963           if (mp->min_address < min_address)
7964             {
7965               /* For now, we do not allow the insertion of 8-byte alignment
7966                  requiring nodes anywhere but at the start of the pool.  */
7967               if (ARM_DOUBLEWORD_ALIGN
7968                   && fix->fix_size == 8 && mp->fix_size != 8)
7969                 return NULL;
7970               else
7971                 min_mp = mp;
7972             }
7973           else if (mp->max_address
7974                    < minipool_barrier->address + mp->offset + fix->fix_size)
7975             {
7976               /* Inserting before this entry would push the fix beyond
7977                  its maximum address (which can happen if we have
7978                  re-located a forwards fix); force the new fix to come
7979                  after it.  */
7980               min_mp = mp;
7981               min_address = mp->min_address + fix->fix_size;
7982             }
7983           /* If we are inserting an 8-bytes aligned quantity and
7984              we have not already found an insertion point, then
7985              make sure that all such 8-byte aligned quantities are
7986              placed at the start of the pool.  */
7987           else if (ARM_DOUBLEWORD_ALIGN
7988                    && min_mp == NULL
7989                    && fix->fix_size == 8
7990                    && mp->fix_size < 8)
7991             {
7992               min_mp = mp;
7993               min_address = mp->min_address + fix->fix_size;
7994             }
7995         }
7996     }
7997
7998   /* We need to create a new entry.  */
7999   mp = XNEW (Mnode);
8000   mp->fix_size = fix->fix_size;
8001   mp->mode = fix->mode;
8002   mp->value = fix->value;
8003   mp->refcount = 1;
8004   mp->max_address = minipool_barrier->address + 65536;
8005
8006   mp->min_address = min_address;
8007
8008   if (min_mp == NULL)
8009     {
8010       mp->prev = NULL;
8011       mp->next = minipool_vector_head;
8012
8013       if (mp->next == NULL)
8014         {
8015           minipool_vector_tail = mp;
8016           minipool_vector_label = gen_label_rtx ();
8017         }
8018       else
8019         mp->next->prev = mp;
8020
8021       minipool_vector_head = mp;
8022     }
8023   else
8024     {
8025       mp->next = min_mp->next;
8026       mp->prev = min_mp;
8027       min_mp->next = mp;
8028
8029       if (mp->next != NULL)
8030         mp->next->prev = mp;
8031       else
8032         minipool_vector_tail = mp;
8033     }
8034
8035   /* Save the new entry.  */
8036   min_mp = mp;
8037
8038   if (mp->prev)
8039     mp = mp->prev;
8040   else
8041     mp->offset = 0;
8042
8043   /* Scan over the following entries and adjust their offsets.  */
8044   while (mp->next != NULL)
8045     {
8046       if (mp->next->min_address < mp->min_address + mp->fix_size)
8047         mp->next->min_address = mp->min_address + mp->fix_size;
8048
8049       if (mp->refcount)
8050         mp->next->offset = mp->offset + mp->fix_size;
8051       else
8052         mp->next->offset = mp->offset;
8053
8054       mp = mp->next;
8055     }
8056
8057   return min_mp;
8058 }
8059
8060 static void
8061 assign_minipool_offsets (Mfix *barrier)
8062 {
8063   HOST_WIDE_INT offset = 0;
8064   Mnode *mp;
8065
8066   minipool_barrier = barrier;
8067
8068   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8069     {
8070       mp->offset = offset;
8071
8072       if (mp->refcount > 0)
8073         offset += mp->fix_size;
8074     }
8075 }
8076
8077 /* Output the literal table */
8078 static void
8079 dump_minipool (rtx scan)
8080 {
8081   Mnode * mp;
8082   Mnode * nmp;
8083   int align64 = 0;
8084
8085   if (ARM_DOUBLEWORD_ALIGN)
8086     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
8087       if (mp->refcount > 0 && mp->fix_size == 8)
8088         {
8089           align64 = 1;
8090           break;
8091         }
8092
8093   if (dump_file)
8094     fprintf (dump_file,
8095              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
8096              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
8097
8098   scan = emit_label_after (gen_label_rtx (), scan);
8099   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
8100   scan = emit_label_after (minipool_vector_label, scan);
8101
8102   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
8103     {
8104       if (mp->refcount > 0)
8105         {
8106           if (dump_file)
8107             {
8108               fprintf (dump_file,
8109                        ";;  Offset %u, min %ld, max %ld ",
8110                        (unsigned) mp->offset, (unsigned long) mp->min_address,
8111                        (unsigned long) mp->max_address);
8112               arm_print_value (dump_file, mp->value);
8113               fputc ('\n', dump_file);
8114             }
8115
8116           switch (mp->fix_size)
8117             {
8118 #ifdef HAVE_consttable_1
8119             case 1:
8120               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
8121               break;
8122
8123 #endif
8124 #ifdef HAVE_consttable_2
8125             case 2:
8126               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
8127               break;
8128
8129 #endif
8130 #ifdef HAVE_consttable_4
8131             case 4:
8132               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
8133               break;
8134
8135 #endif
8136 #ifdef HAVE_consttable_8
8137             case 8:
8138               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
8139               break;
8140
8141 #endif
8142             default:
8143               gcc_unreachable ();
8144             }
8145         }
8146
8147       nmp = mp->next;
8148       free (mp);
8149     }
8150
8151   minipool_vector_head = minipool_vector_tail = NULL;
8152   scan = emit_insn_after (gen_consttable_end (), scan);
8153   scan = emit_barrier_after (scan);
8154 }
8155
8156 /* Return the cost of forcibly inserting a barrier after INSN.  */
8157 static int
8158 arm_barrier_cost (rtx insn)
8159 {
8160   /* Basing the location of the pool on the loop depth is preferable,
8161      but at the moment, the basic block information seems to be
8162      corrupt by this stage of the compilation.  */
8163   int base_cost = 50;
8164   rtx next = next_nonnote_insn (insn);
8165
8166   if (next != NULL && GET_CODE (next) == CODE_LABEL)
8167     base_cost -= 20;
8168
8169   switch (GET_CODE (insn))
8170     {
8171     case CODE_LABEL:
8172       /* It will always be better to place the table before the label, rather
8173          than after it.  */
8174       return 50;
8175
8176     case INSN:
8177     case CALL_INSN:
8178       return base_cost;
8179
8180     case JUMP_INSN:
8181       return base_cost - 10;
8182
8183     default:
8184       return base_cost + 10;
8185     }
8186 }
8187
8188 /* Find the best place in the insn stream in the range
8189    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
8190    Create the barrier by inserting a jump and add a new fix entry for
8191    it.  */
8192 static Mfix *
8193 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
8194 {
8195   HOST_WIDE_INT count = 0;
8196   rtx barrier;
8197   rtx from = fix->insn;
8198   /* The instruction after which we will insert the jump.  */
8199   rtx selected = NULL;
8200   int selected_cost;
8201   /* The address at which the jump instruction will be placed.  */
8202   HOST_WIDE_INT selected_address;
8203   Mfix * new_fix;
8204   HOST_WIDE_INT max_count = max_address - fix->address;
8205   rtx label = gen_label_rtx ();
8206
8207   selected_cost = arm_barrier_cost (from);
8208   selected_address = fix->address;
8209
8210   while (from && count < max_count)
8211     {
8212       rtx tmp;
8213       int new_cost;
8214
8215       /* This code shouldn't have been called if there was a natural barrier
8216          within range.  */
8217       gcc_assert (GET_CODE (from) != BARRIER);
8218
8219       /* Count the length of this insn.  */
8220       count += get_attr_length (from);
8221
8222       /* If there is a jump table, add its length.  */
8223       tmp = is_jump_table (from);
8224       if (tmp != NULL)
8225         {
8226           count += get_jump_table_size (tmp);
8227
8228           /* Jump tables aren't in a basic block, so base the cost on
8229              the dispatch insn.  If we select this location, we will
8230              still put the pool after the table.  */
8231           new_cost = arm_barrier_cost (from);
8232
8233           if (count < max_count 
8234               && (!selected || new_cost <= selected_cost))
8235             {
8236               selected = tmp;
8237               selected_cost = new_cost;
8238               selected_address = fix->address + count;
8239             }
8240
8241           /* Continue after the dispatch table.  */
8242           from = NEXT_INSN (tmp);
8243           continue;
8244         }
8245
8246       new_cost = arm_barrier_cost (from);
8247
8248       if (count < max_count
8249           && (!selected || new_cost <= selected_cost))
8250         {
8251           selected = from;
8252           selected_cost = new_cost;
8253           selected_address = fix->address + count;
8254         }
8255
8256       from = NEXT_INSN (from);
8257     }
8258
8259   /* Make sure that we found a place to insert the jump.  */
8260   gcc_assert (selected);
8261
8262   /* Create a new JUMP_INSN that branches around a barrier.  */
8263   from = emit_jump_insn_after (gen_jump (label), selected);
8264   JUMP_LABEL (from) = label;
8265   barrier = emit_barrier_after (from);
8266   emit_label_after (label, barrier);
8267
8268   /* Create a minipool barrier entry for the new barrier.  */
8269   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
8270   new_fix->insn = barrier;
8271   new_fix->address = selected_address;
8272   new_fix->next = fix->next;
8273   fix->next = new_fix;
8274
8275   return new_fix;
8276 }
8277
8278 /* Record that there is a natural barrier in the insn stream at
8279    ADDRESS.  */
8280 static void
8281 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
8282 {
8283   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8284
8285   fix->insn = insn;
8286   fix->address = address;
8287
8288   fix->next = NULL;
8289   if (minipool_fix_head != NULL)
8290     minipool_fix_tail->next = fix;
8291   else
8292     minipool_fix_head = fix;
8293
8294   minipool_fix_tail = fix;
8295 }
8296
8297 /* Record INSN, which will need fixing up to load a value from the
8298    minipool.  ADDRESS is the offset of the insn since the start of the
8299    function; LOC is a pointer to the part of the insn which requires
8300    fixing; VALUE is the constant that must be loaded, which is of type
8301    MODE.  */
8302 static void
8303 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
8304                    enum machine_mode mode, rtx value)
8305 {
8306   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
8307
8308 #ifdef AOF_ASSEMBLER
8309   /* PIC symbol references need to be converted into offsets into the
8310      based area.  */
8311   /* XXX This shouldn't be done here.  */
8312   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
8313     value = aof_pic_entry (value);
8314 #endif /* AOF_ASSEMBLER */
8315
8316   fix->insn = insn;
8317   fix->address = address;
8318   fix->loc = loc;
8319   fix->mode = mode;
8320   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
8321   fix->value = value;
8322   fix->forwards = get_attr_pool_range (insn);
8323   fix->backwards = get_attr_neg_pool_range (insn);
8324   fix->minipool = NULL;
8325
8326   /* If an insn doesn't have a range defined for it, then it isn't
8327      expecting to be reworked by this code.  Better to stop now than
8328      to generate duff assembly code.  */
8329   gcc_assert (fix->forwards || fix->backwards);
8330
8331   /* If an entry requires 8-byte alignment then assume all constant pools
8332      require 4 bytes of padding.  Trying to do this later on a per-pool
8333      basis is awkward because existing pool entries have to be modified.  */
8334   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8335     minipool_pad = 4;
8336
8337   if (dump_file)
8338     {
8339       fprintf (dump_file,
8340                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8341                GET_MODE_NAME (mode),
8342                INSN_UID (insn), (unsigned long) address,
8343                -1 * (long)fix->backwards, (long)fix->forwards);
8344       arm_print_value (dump_file, fix->value);
8345       fprintf (dump_file, "\n");
8346     }
8347
8348   /* Add it to the chain of fixes.  */
8349   fix->next = NULL;
8350
8351   if (minipool_fix_head != NULL)
8352     minipool_fix_tail->next = fix;
8353   else
8354     minipool_fix_head = fix;
8355
8356   minipool_fix_tail = fix;
8357 }
8358
8359 /* Return the cost of synthesizing a 64-bit constant VAL inline.
8360    Returns the number of insns needed, or 99 if we don't know how to
8361    do it.  */
8362 int
8363 arm_const_double_inline_cost (rtx val)
8364 {
8365   rtx lowpart, highpart;
8366   enum machine_mode mode;
8367
8368   mode = GET_MODE (val);
8369
8370   if (mode == VOIDmode)
8371     mode = DImode;
8372
8373   gcc_assert (GET_MODE_SIZE (mode) == 8);
8374
8375   lowpart = gen_lowpart (SImode, val);
8376   highpart = gen_highpart_mode (SImode, mode, val);
8377
8378   gcc_assert (GET_CODE (lowpart) == CONST_INT);
8379   gcc_assert (GET_CODE (highpart) == CONST_INT);
8380
8381   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8382                             NULL_RTX, NULL_RTX, 0, 0)
8383           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8384                               NULL_RTX, NULL_RTX, 0, 0));
8385 }
8386
8387 /* Return true if it is worthwhile to split a 64-bit constant into two
8388    32-bit operations.  This is the case if optimizing for size, or
8389    if we have load delay slots, or if one 32-bit part can be done with
8390    a single data operation.  */
8391 bool
8392 arm_const_double_by_parts (rtx val)
8393 {
8394   enum machine_mode mode = GET_MODE (val);
8395   rtx part;
8396
8397   if (optimize_size || arm_ld_sched)
8398     return true;
8399
8400   if (mode == VOIDmode)
8401     mode = DImode;
8402
8403   part = gen_highpart_mode (SImode, mode, val);
8404
8405   gcc_assert (GET_CODE (part) == CONST_INT);
8406
8407   if (const_ok_for_arm (INTVAL (part))
8408       || const_ok_for_arm (~INTVAL (part)))
8409     return true;
8410
8411   part = gen_lowpart (SImode, val);
8412
8413   gcc_assert (GET_CODE (part) == CONST_INT);
8414
8415   if (const_ok_for_arm (INTVAL (part))
8416       || const_ok_for_arm (~INTVAL (part)))
8417     return true;
8418
8419   return false;
8420 }
8421
8422 /* Scan INSN and note any of its operands that need fixing.
8423    If DO_PUSHES is false we do not actually push any of the fixups
8424    needed.  The function returns TRUE if any fixups were needed/pushed.
8425    This is used by arm_memory_load_p() which needs to know about loads
8426    of constants that will be converted into minipool loads.  */
8427 static bool
8428 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8429 {
8430   bool result = false;
8431   int opno;
8432
8433   extract_insn (insn);
8434
8435   if (!constrain_operands (1))
8436     fatal_insn_not_found (insn);
8437
8438   if (recog_data.n_alternatives == 0)
8439     return false;
8440
8441   /* Fill in recog_op_alt with information about the constraints of
8442      this insn.  */
8443   preprocess_constraints ();
8444
8445   for (opno = 0; opno < recog_data.n_operands; opno++)
8446     {
8447       /* Things we need to fix can only occur in inputs.  */
8448       if (recog_data.operand_type[opno] != OP_IN)
8449         continue;
8450
8451       /* If this alternative is a memory reference, then any mention
8452          of constants in this alternative is really to fool reload
8453          into allowing us to accept one there.  We need to fix them up
8454          now so that we output the right code.  */
8455       if (recog_op_alt[opno][which_alternative].memory_ok)
8456         {
8457           rtx op = recog_data.operand[opno];
8458
8459           if (CONSTANT_P (op))
8460             {
8461               if (do_pushes)
8462                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8463                                    recog_data.operand_mode[opno], op);
8464               result = true;
8465             }
8466           else if (GET_CODE (op) == MEM
8467                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8468                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8469             {
8470               if (do_pushes)
8471                 {
8472                   rtx cop = avoid_constant_pool_reference (op);
8473
8474                   /* Casting the address of something to a mode narrower
8475                      than a word can cause avoid_constant_pool_reference()
8476                      to return the pool reference itself.  That's no good to
8477                      us here.  Lets just hope that we can use the
8478                      constant pool value directly.  */
8479                   if (op == cop)
8480                     cop = get_pool_constant (XEXP (op, 0));
8481
8482                   push_minipool_fix (insn, address,
8483                                      recog_data.operand_loc[opno],
8484                                      recog_data.operand_mode[opno], cop);
8485                 }
8486
8487               result = true;
8488             }
8489         }
8490     }
8491
8492   return result;
8493 }
8494
8495 /* Gcc puts the pool in the wrong place for ARM, since we can only
8496    load addresses a limited distance around the pc.  We do some
8497    special munging to move the constant pool values to the correct
8498    point in the code.  */
8499 static void
8500 arm_reorg (void)
8501 {
8502   rtx insn;
8503   HOST_WIDE_INT address = 0;
8504   Mfix * fix;
8505
8506   minipool_fix_head = minipool_fix_tail = NULL;
8507
8508   /* The first insn must always be a note, or the code below won't
8509      scan it properly.  */
8510   insn = get_insns ();
8511   gcc_assert (GET_CODE (insn) == NOTE);
8512   minipool_pad = 0;
8513
8514   /* Scan all the insns and record the operands that will need fixing.  */
8515   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8516     {
8517       if (TARGET_CIRRUS_FIX_INVALID_INSNS
8518           && (arm_cirrus_insn_p (insn)
8519               || GET_CODE (insn) == JUMP_INSN
8520               || arm_memory_load_p (insn)))
8521         cirrus_reorg (insn);
8522
8523       if (GET_CODE (insn) == BARRIER)
8524         push_minipool_barrier (insn, address);
8525       else if (INSN_P (insn))
8526         {
8527           rtx table;
8528
8529           note_invalid_constants (insn, address, true);
8530           address += get_attr_length (insn);
8531
8532           /* If the insn is a vector jump, add the size of the table
8533              and skip the table.  */
8534           if ((table = is_jump_table (insn)) != NULL)
8535             {
8536               address += get_jump_table_size (table);
8537               insn = table;
8538             }
8539         }
8540     }
8541
8542   fix = minipool_fix_head;
8543
8544   /* Now scan the fixups and perform the required changes.  */
8545   while (fix)
8546     {
8547       Mfix * ftmp;
8548       Mfix * fdel;
8549       Mfix *  last_added_fix;
8550       Mfix * last_barrier = NULL;
8551       Mfix * this_fix;
8552
8553       /* Skip any further barriers before the next fix.  */
8554       while (fix && GET_CODE (fix->insn) == BARRIER)
8555         fix = fix->next;
8556
8557       /* No more fixes.  */
8558       if (fix == NULL)
8559         break;
8560
8561       last_added_fix = NULL;
8562
8563       for (ftmp = fix; ftmp; ftmp = ftmp->next)
8564         {
8565           if (GET_CODE (ftmp->insn) == BARRIER)
8566             {
8567               if (ftmp->address >= minipool_vector_head->max_address)
8568                 break;
8569
8570               last_barrier = ftmp;
8571             }
8572           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8573             break;
8574
8575           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
8576         }
8577
8578       /* If we found a barrier, drop back to that; any fixes that we
8579          could have reached but come after the barrier will now go in
8580          the next mini-pool.  */
8581       if (last_barrier != NULL)
8582         {
8583           /* Reduce the refcount for those fixes that won't go into this
8584              pool after all.  */
8585           for (fdel = last_barrier->next;
8586                fdel && fdel != ftmp;
8587                fdel = fdel->next)
8588             {
8589               fdel->minipool->refcount--;
8590               fdel->minipool = NULL;
8591             }
8592
8593           ftmp = last_barrier;
8594         }
8595       else
8596         {
8597           /* ftmp is first fix that we can't fit into this pool and
8598              there no natural barriers that we could use.  Insert a
8599              new barrier in the code somewhere between the previous
8600              fix and this one, and arrange to jump around it.  */
8601           HOST_WIDE_INT max_address;
8602
8603           /* The last item on the list of fixes must be a barrier, so
8604              we can never run off the end of the list of fixes without
8605              last_barrier being set.  */
8606           gcc_assert (ftmp);
8607
8608           max_address = minipool_vector_head->max_address;
8609           /* Check that there isn't another fix that is in range that
8610              we couldn't fit into this pool because the pool was
8611              already too large: we need to put the pool before such an
8612              instruction.  The pool itself may come just after the
8613              fix because create_fix_barrier also allows space for a
8614              jump instruction.  */
8615           if (ftmp->address < max_address)
8616             max_address = ftmp->address + 1;
8617
8618           last_barrier = create_fix_barrier (last_added_fix, max_address);
8619         }
8620
8621       assign_minipool_offsets (last_barrier);
8622
8623       while (ftmp)
8624         {
8625           if (GET_CODE (ftmp->insn) != BARRIER
8626               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8627                   == NULL))
8628             break;
8629
8630           ftmp = ftmp->next;
8631         }
8632
8633       /* Scan over the fixes we have identified for this pool, fixing them
8634          up and adding the constants to the pool itself.  */
8635       for (this_fix = fix; this_fix && ftmp != this_fix;
8636            this_fix = this_fix->next)
8637         if (GET_CODE (this_fix->insn) != BARRIER)
8638           {
8639             rtx addr
8640               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8641                                                   minipool_vector_label),
8642                                this_fix->minipool->offset);
8643             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8644           }
8645
8646       dump_minipool (last_barrier->insn);
8647       fix = ftmp;
8648     }
8649
8650   /* From now on we must synthesize any constants that we can't handle
8651      directly.  This can happen if the RTL gets split during final
8652      instruction generation.  */
8653   after_arm_reorg = 1;
8654
8655   /* Free the minipool memory.  */
8656   obstack_free (&minipool_obstack, minipool_startobj);
8657 }
8658 \f
8659 /* Routines to output assembly language.  */
8660
8661 /* If the rtx is the correct value then return the string of the number.
8662    In this way we can ensure that valid double constants are generated even
8663    when cross compiling.  */
8664 const char *
8665 fp_immediate_constant (rtx x)
8666 {
8667   REAL_VALUE_TYPE r;
8668   int i;
8669
8670   if (!fp_consts_inited)
8671     init_fp_table ();
8672
8673   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8674   for (i = 0; i < 8; i++)
8675     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8676       return strings_fp[i];
8677
8678   gcc_unreachable ();
8679 }
8680
8681 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
8682 static const char *
8683 fp_const_from_val (REAL_VALUE_TYPE *r)
8684 {
8685   int i;
8686
8687   if (!fp_consts_inited)
8688     init_fp_table ();
8689
8690   for (i = 0; i < 8; i++)
8691     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8692       return strings_fp[i];
8693
8694   gcc_unreachable ();
8695 }
8696
8697 /* Output the operands of a LDM/STM instruction to STREAM.
8698    MASK is the ARM register set mask of which only bits 0-15 are important.
8699    REG is the base register, either the frame pointer or the stack pointer,
8700    INSTR is the possibly suffixed load or store instruction.
8701    RFE is nonzero if the instruction should also copy spsr to cpsr.  */
8702
8703 static void
8704 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8705                  unsigned long mask, int rfe)
8706 {
8707   unsigned i;
8708   bool not_first = FALSE;
8709
8710   gcc_assert (!rfe || (mask & (1 << PC_REGNUM)));
8711   fputc ('\t', stream);
8712   asm_fprintf (stream, instr, reg);
8713   fputc ('{', stream);
8714
8715   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8716     if (mask & (1 << i))
8717       {
8718         if (not_first)
8719           fprintf (stream, ", ");
8720
8721         asm_fprintf (stream, "%r", i);
8722         not_first = TRUE;
8723       }
8724
8725   if (rfe)
8726     fprintf (stream, "}^\n");
8727   else
8728     fprintf (stream, "}\n");
8729 }
8730
8731
8732 /* Output a FLDMD instruction to STREAM.
8733    BASE if the register containing the address.
8734    REG and COUNT specify the register range.
8735    Extra registers may be added to avoid hardware bugs.
8736
8737    We output FLDMD even for ARMv5 VFP implementations.  Although
8738    FLDMD is technically not supported until ARMv6, it is believed
8739    that all VFP implementations support its use in this context.  */
8740
8741 static void
8742 vfp_output_fldmd (FILE * stream, unsigned int base, int reg, int count)
8743 {
8744   int i;
8745
8746   /* Workaround ARM10 VFPr1 bug.  */
8747   if (count == 2 && !arm_arch6)
8748     {
8749       if (reg == 15)
8750         reg--;
8751       count++;
8752     }
8753
8754   fputc ('\t', stream);
8755   asm_fprintf (stream, "fldmfdd\t%r!, {", base);
8756
8757   for (i = reg; i < reg + count; i++)
8758     {
8759       if (i > reg)
8760         fputs (", ", stream);
8761       asm_fprintf (stream, "d%d", i);
8762     }
8763   fputs ("}\n", stream);
8764
8765 }
8766
8767
8768 /* Output the assembly for a store multiple.  */
8769
8770 const char *
8771 vfp_output_fstmd (rtx * operands)
8772 {
8773   char pattern[100];
8774   int p;
8775   int base;
8776   int i;
8777
8778   strcpy (pattern, "fstmfdd\t%m0!, {%P1");
8779   p = strlen (pattern);
8780
8781   gcc_assert (GET_CODE (operands[1]) == REG);
8782
8783   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8784   for (i = 1; i < XVECLEN (operands[2], 0); i++)
8785     {
8786       p += sprintf (&pattern[p], ", d%d", base + i);
8787     }
8788   strcpy (&pattern[p], "}");
8789
8790   output_asm_insn (pattern, operands);
8791   return "";
8792 }
8793
8794
8795 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
8796    number of bytes pushed.  */
8797
8798 static int
8799 vfp_emit_fstmd (int base_reg, int count)
8800 {
8801   rtx par;
8802   rtx dwarf;
8803   rtx tmp, reg;
8804   int i;
8805
8806   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8807      register pairs are stored by a store multiple insn.  We avoid this
8808      by pushing an extra pair.  */
8809   if (count == 2 && !arm_arch6)
8810     {
8811       if (base_reg == LAST_VFP_REGNUM - 3)
8812         base_reg -= 2;
8813       count++;
8814     }
8815
8816   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8817   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8818
8819   reg = gen_rtx_REG (DFmode, base_reg);
8820   base_reg += 2;
8821
8822   XVECEXP (par, 0, 0)
8823     = gen_rtx_SET (VOIDmode,
8824                    gen_frame_mem (BLKmode,
8825                                   gen_rtx_PRE_DEC (BLKmode,
8826                                                    stack_pointer_rtx)),
8827                    gen_rtx_UNSPEC (BLKmode,
8828                                    gen_rtvec (1, reg),
8829                                    UNSPEC_PUSH_MULT));
8830
8831   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8832                      plus_constant (stack_pointer_rtx, -(count * 8)));
8833   RTX_FRAME_RELATED_P (tmp) = 1;
8834   XVECEXP (dwarf, 0, 0) = tmp;
8835
8836   tmp = gen_rtx_SET (VOIDmode,
8837                      gen_frame_mem (DFmode, stack_pointer_rtx),
8838                      reg);
8839   RTX_FRAME_RELATED_P (tmp) = 1;
8840   XVECEXP (dwarf, 0, 1) = tmp;
8841
8842   for (i = 1; i < count; i++)
8843     {
8844       reg = gen_rtx_REG (DFmode, base_reg);
8845       base_reg += 2;
8846       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8847
8848       tmp = gen_rtx_SET (VOIDmode,
8849                          gen_frame_mem (DFmode,
8850                                         plus_constant (stack_pointer_rtx,
8851                                                        i * 8)),
8852                          reg);
8853       RTX_FRAME_RELATED_P (tmp) = 1;
8854       XVECEXP (dwarf, 0, i + 1) = tmp;
8855     }
8856
8857   par = emit_insn (par);
8858   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8859                                        REG_NOTES (par));
8860   RTX_FRAME_RELATED_P (par) = 1;
8861
8862   return count * 8;
8863 }
8864
8865
8866 /* Output a 'call' insn.  */
8867 const char *
8868 output_call (rtx *operands)
8869 {
8870   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8871
8872   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8873   if (REGNO (operands[0]) == LR_REGNUM)
8874     {
8875       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8876       output_asm_insn ("mov%?\t%0, %|lr", operands);
8877     }
8878
8879   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8880
8881   if (TARGET_INTERWORK || arm_arch4t)
8882     output_asm_insn ("bx%?\t%0", operands);
8883   else
8884     output_asm_insn ("mov%?\t%|pc, %0", operands);
8885
8886   return "";
8887 }
8888
8889 /* Output a 'call' insn that is a reference in memory.  */
8890 const char *
8891 output_call_mem (rtx *operands)
8892 {
8893   if (TARGET_INTERWORK && !arm_arch5)
8894     {
8895       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8896       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8897       output_asm_insn ("bx%?\t%|ip", operands);
8898     }
8899   else if (regno_use_in (LR_REGNUM, operands[0]))
8900     {
8901       /* LR is used in the memory address.  We load the address in the
8902          first instruction.  It's safe to use IP as the target of the
8903          load since the call will kill it anyway.  */
8904       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8905       if (arm_arch5)
8906         output_asm_insn ("blx%?\t%|ip", operands);
8907       else
8908         {
8909           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8910           if (arm_arch4t)
8911             output_asm_insn ("bx%?\t%|ip", operands);
8912           else
8913             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8914         }
8915     }
8916   else
8917     {
8918       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8919       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8920     }
8921
8922   return "";
8923 }
8924
8925
8926 /* Output a move from arm registers to an fpa registers.
8927    OPERANDS[0] is an fpa register.
8928    OPERANDS[1] is the first registers of an arm register pair.  */
8929 const char *
8930 output_mov_long_double_fpa_from_arm (rtx *operands)
8931 {
8932   int arm_reg0 = REGNO (operands[1]);
8933   rtx ops[3];
8934
8935   gcc_assert (arm_reg0 != IP_REGNUM);
8936
8937   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8938   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8939   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8940
8941   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
8942   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8943
8944   return "";
8945 }
8946
8947 /* Output a move from an fpa register to arm registers.
8948    OPERANDS[0] is the first registers of an arm register pair.
8949    OPERANDS[1] is an fpa register.  */
8950 const char *
8951 output_mov_long_double_arm_from_fpa (rtx *operands)
8952 {
8953   int arm_reg0 = REGNO (operands[0]);
8954   rtx ops[3];
8955
8956   gcc_assert (arm_reg0 != IP_REGNUM);
8957
8958   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8959   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8960   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8961
8962   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8963   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1, %2}", ops);
8964   return "";
8965 }
8966
8967 /* Output a move from arm registers to arm registers of a long double
8968    OPERANDS[0] is the destination.
8969    OPERANDS[1] is the source.  */
8970 const char *
8971 output_mov_long_double_arm_from_arm (rtx *operands)
8972 {
8973   /* We have to be careful here because the two might overlap.  */
8974   int dest_start = REGNO (operands[0]);
8975   int src_start = REGNO (operands[1]);
8976   rtx ops[2];
8977   int i;
8978
8979   if (dest_start < src_start)
8980     {
8981       for (i = 0; i < 3; i++)
8982         {
8983           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8984           ops[1] = gen_rtx_REG (SImode, src_start + i);
8985           output_asm_insn ("mov%?\t%0, %1", ops);
8986         }
8987     }
8988   else
8989     {
8990       for (i = 2; i >= 0; i--)
8991         {
8992           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8993           ops[1] = gen_rtx_REG (SImode, src_start + i);
8994           output_asm_insn ("mov%?\t%0, %1", ops);
8995         }
8996     }
8997
8998   return "";
8999 }
9000
9001
9002 /* Output a move from arm registers to an fpa registers.
9003    OPERANDS[0] is an fpa register.
9004    OPERANDS[1] is the first registers of an arm register pair.  */
9005 const char *
9006 output_mov_double_fpa_from_arm (rtx *operands)
9007 {
9008   int arm_reg0 = REGNO (operands[1]);
9009   rtx ops[2];
9010
9011   gcc_assert (arm_reg0 != IP_REGNUM);
9012
9013   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9014   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9015   output_asm_insn ("stm%(fd%)\t%|sp!, {%0, %1}", ops);
9016   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
9017   return "";
9018 }
9019
9020 /* Output a move from an fpa register to arm registers.
9021    OPERANDS[0] is the first registers of an arm register pair.
9022    OPERANDS[1] is an fpa register.  */
9023 const char *
9024 output_mov_double_arm_from_fpa (rtx *operands)
9025 {
9026   int arm_reg0 = REGNO (operands[0]);
9027   rtx ops[2];
9028
9029   gcc_assert (arm_reg0 != IP_REGNUM);
9030
9031   ops[0] = gen_rtx_REG (SImode, arm_reg0);
9032   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
9033   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
9034   output_asm_insn ("ldm%(fd%)\t%|sp!, {%0, %1}", ops);
9035   return "";
9036 }
9037
9038 /* Output a move between double words.
9039    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
9040    or MEM<-REG and all MEMs must be offsettable addresses.  */
9041 const char *
9042 output_move_double (rtx *operands)
9043 {
9044   enum rtx_code code0 = GET_CODE (operands[0]);
9045   enum rtx_code code1 = GET_CODE (operands[1]);
9046   rtx otherops[3];
9047
9048   if (code0 == REG)
9049     {
9050       int reg0 = REGNO (operands[0]);
9051
9052       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9053
9054       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
9055
9056       switch (GET_CODE (XEXP (operands[1], 0)))
9057         {
9058         case REG:
9059           output_asm_insn ("ldm%(ia%)\t%m1, %M0", operands);
9060           break;
9061
9062         case PRE_INC:
9063           gcc_assert (TARGET_LDRD);
9064           output_asm_insn ("ldr%(d%)\t%0, [%m1, #8]!", operands);
9065           break;
9066
9067         case PRE_DEC:
9068           if (TARGET_LDRD)
9069             output_asm_insn ("ldr%(d%)\t%0, [%m1, #-8]!", operands);
9070           else
9071             output_asm_insn ("ldm%(db%)\t%m1!, %M0", operands);
9072           break;
9073
9074         case POST_INC:
9075           output_asm_insn ("ldm%(ia%)\t%m1!, %M0", operands);
9076           break;
9077
9078         case POST_DEC:
9079           gcc_assert (TARGET_LDRD);
9080           output_asm_insn ("ldr%(d%)\t%0, [%m1], #-8", operands);
9081           break;
9082
9083         case PRE_MODIFY:
9084         case POST_MODIFY:
9085           otherops[0] = operands[0];
9086           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
9087           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
9088
9089           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
9090             {
9091               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9092                 {
9093                   /* Registers overlap so split out the increment.  */
9094                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
9095                   output_asm_insn ("ldr%(d%)\t%0, [%1] @split", otherops);
9096                 }
9097               else
9098                 {
9099                   /* IWMMXT allows offsets larger than ldrd can handle,
9100                      fix these up with a pair of ldr.  */
9101                   if (GET_CODE (otherops[2]) == CONST_INT
9102                       && (INTVAL(otherops[2]) <= -256
9103                           || INTVAL(otherops[2]) >= 256))
9104                     {
9105                       output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
9106                       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9107                       output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9108                     }
9109                   else
9110                     output_asm_insn ("ldr%(d%)\t%0, [%1, %2]!", otherops);
9111                 }
9112             }
9113           else
9114             {
9115               /* IWMMXT allows offsets larger than ldrd can handle,
9116                  fix these up with a pair of ldr.  */
9117               if (GET_CODE (otherops[2]) == CONST_INT
9118                   && (INTVAL(otherops[2]) <= -256
9119                       || INTVAL(otherops[2]) >= 256))
9120                 {
9121                   otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
9122                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9123                   otherops[0] = operands[0];
9124                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
9125                 }
9126               else
9127                 /* We only allow constant increments, so this is safe.  */
9128                 output_asm_insn ("ldr%(d%)\t%0, [%1], %2", otherops);
9129             }
9130           break;
9131
9132         case LABEL_REF:
9133         case CONST:
9134           output_asm_insn ("adr%?\t%0, %1", operands);
9135           output_asm_insn ("ldm%(ia%)\t%0, %M0", operands);
9136           break;
9137
9138           /* ??? This needs checking for thumb2.  */
9139         default:
9140           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
9141                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
9142             {
9143               otherops[0] = operands[0];
9144               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
9145               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
9146
9147               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
9148                 {
9149                   if (GET_CODE (otherops[2]) == CONST_INT)
9150                     {
9151                       switch ((int) INTVAL (otherops[2]))
9152                         {
9153                         case -8:
9154                           output_asm_insn ("ldm%(db%)\t%1, %M0", otherops);
9155                           return "";
9156                         case -4:
9157                           if (TARGET_THUMB2)
9158                             break;
9159                           output_asm_insn ("ldm%(da%)\t%1, %M0", otherops);
9160                           return "";
9161                         case 4:
9162                           if (TARGET_THUMB2)
9163                             break;
9164                           output_asm_insn ("ldm%(ib%)\t%1, %M0", otherops);
9165                           return "";
9166                         }
9167                     }
9168                   if (TARGET_LDRD
9169                       && (GET_CODE (otherops[2]) == REG
9170                           || (GET_CODE (otherops[2]) == CONST_INT
9171                               && INTVAL (otherops[2]) > -256
9172                               && INTVAL (otherops[2]) < 256)))
9173                     {
9174                       if (reg_overlap_mentioned_p (otherops[0],
9175                                                    otherops[2]))
9176                         {
9177                           /* Swap base and index registers over to
9178                              avoid a conflict.  */
9179                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
9180                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
9181                         }
9182                       /* If both registers conflict, it will usually
9183                          have been fixed by a splitter.  */
9184                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
9185                         {
9186                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
9187                           output_asm_insn ("ldr%(d%)\t%0, [%1]",
9188                                            otherops);
9189                         }
9190                       else
9191                         output_asm_insn ("ldr%(d%)\t%0, [%1, %2]", otherops);
9192                       return "";
9193                     }
9194
9195                   if (GET_CODE (otherops[2]) == CONST_INT)
9196                     {
9197                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
9198                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
9199                       else
9200                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
9201                     }
9202                   else
9203                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
9204                 }
9205               else
9206                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
9207
9208               return "ldm%(ia%)\t%0, %M0";
9209             }
9210           else
9211             {
9212               otherops[1] = adjust_address (operands[1], SImode, 4);
9213               /* Take care of overlapping base/data reg.  */
9214               if (reg_mentioned_p (operands[0], operands[1]))
9215                 {
9216                   output_asm_insn ("ldr%?\t%0, %1", otherops);
9217                   output_asm_insn ("ldr%?\t%0, %1", operands);
9218                 }
9219               else
9220                 {
9221                   output_asm_insn ("ldr%?\t%0, %1", operands);
9222                   output_asm_insn ("ldr%?\t%0, %1", otherops);
9223                 }
9224             }
9225         }
9226     }
9227   else
9228     {
9229       /* Constraints should ensure this.  */
9230       gcc_assert (code0 == MEM && code1 == REG);
9231       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
9232
9233       switch (GET_CODE (XEXP (operands[0], 0)))
9234         {
9235         case REG:
9236           output_asm_insn ("stm%(ia%)\t%m0, %M1", operands);
9237           break;
9238
9239         case PRE_INC:
9240           gcc_assert (TARGET_LDRD);
9241           output_asm_insn ("str%(d%)\t%1, [%m0, #8]!", operands);
9242           break;
9243
9244         case PRE_DEC:
9245           if (TARGET_LDRD)
9246             output_asm_insn ("str%(d%)\t%1, [%m0, #-8]!", operands);
9247           else
9248             output_asm_insn ("stm%(db%)\t%m0!, %M1", operands);
9249           break;
9250
9251         case POST_INC:
9252           output_asm_insn ("stm%(ia%)\t%m0!, %M1", operands);
9253           break;
9254
9255         case POST_DEC:
9256           gcc_assert (TARGET_LDRD);
9257           output_asm_insn ("str%(d%)\t%1, [%m0], #-8", operands);
9258           break;
9259
9260         case PRE_MODIFY:
9261         case POST_MODIFY:
9262           otherops[0] = operands[1];
9263           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
9264           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
9265
9266           /* IWMMXT allows offsets larger than ldrd can handle,
9267              fix these up with a pair of ldr.  */
9268           if (GET_CODE (otherops[2]) == CONST_INT
9269               && (INTVAL(otherops[2]) <= -256
9270                   || INTVAL(otherops[2]) >= 256))
9271             {
9272               rtx reg1;
9273               reg1 = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9274               if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
9275                 {
9276                   output_asm_insn ("ldr%?\t%0, [%1, %2]!", otherops);
9277                   otherops[0] = reg1;
9278                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9279                 }
9280               else
9281                 {
9282                   otherops[0] = reg1;
9283                   output_asm_insn ("ldr%?\t%0, [%1, #4]", otherops);
9284                   otherops[0] = operands[1];
9285                   output_asm_insn ("ldr%?\t%0, [%1], %2", otherops);
9286                 }
9287             }
9288           else if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
9289             output_asm_insn ("str%(d%)\t%0, [%1, %2]!", otherops);
9290           else
9291             output_asm_insn ("str%(d%)\t%0, [%1], %2", otherops);
9292           break;
9293
9294         case PLUS:
9295           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
9296           if (GET_CODE (otherops[2]) == CONST_INT)
9297             {
9298               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
9299                 {
9300                 case -8:
9301                   output_asm_insn ("stm%(db%)\t%m0, %M1", operands);
9302                   return "";
9303
9304                 case -4:
9305                   if (TARGET_THUMB2)
9306                     break;
9307                   output_asm_insn ("stm%(da%)\t%m0, %M1", operands);
9308                   return "";
9309
9310                 case 4:
9311                   if (TARGET_THUMB2)
9312                     break;
9313                   output_asm_insn ("stm%(ib%)\t%m0, %M1", operands);
9314                   return "";
9315                 }
9316             }
9317           if (TARGET_LDRD
9318               && (GET_CODE (otherops[2]) == REG
9319                   || (GET_CODE (otherops[2]) == CONST_INT
9320                       && INTVAL (otherops[2]) > -256
9321                       && INTVAL (otherops[2]) < 256)))
9322             {
9323               otherops[0] = operands[1];
9324               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
9325               output_asm_insn ("str%(d%)\t%0, [%1, %2]", otherops);
9326               return "";
9327             }
9328           /* Fall through */
9329
9330         default:
9331           otherops[0] = adjust_address (operands[0], SImode, 4);
9332           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
9333           output_asm_insn ("str%?\t%1, %0", operands);
9334           output_asm_insn ("str%?\t%1, %0", otherops);
9335         }
9336     }
9337
9338   return "";
9339 }
9340
9341 /* Output a VFP load or store instruction.  */
9342
9343 const char *
9344 output_move_vfp (rtx *operands)
9345 {
9346   rtx reg, mem, addr, ops[2];
9347   int load = REG_P (operands[0]);
9348   int dp = GET_MODE_SIZE (GET_MODE (operands[0])) == 8;
9349   int integer_p = GET_MODE_CLASS (GET_MODE (operands[0])) == MODE_INT;
9350   const char *template;
9351   char buff[50];
9352
9353   reg = operands[!load];
9354   mem = operands[load];
9355
9356   gcc_assert (REG_P (reg));
9357   gcc_assert (IS_VFP_REGNUM (REGNO (reg)));
9358   gcc_assert (GET_MODE (reg) == SFmode
9359               || GET_MODE (reg) == DFmode
9360               || GET_MODE (reg) == SImode
9361               || GET_MODE (reg) == DImode);
9362   gcc_assert (MEM_P (mem));
9363
9364   addr = XEXP (mem, 0);
9365
9366   switch (GET_CODE (addr))
9367     {
9368     case PRE_DEC:
9369       template = "f%smdb%c%%?\t%%0!, {%%%s1}%s";
9370       ops[0] = XEXP (addr, 0);
9371       ops[1] = reg;
9372       break;
9373
9374     case POST_INC:
9375       template = "f%smia%c%%?\t%%0!, {%%%s1}%s";
9376       ops[0] = XEXP (addr, 0);
9377       ops[1] = reg;
9378       break;
9379
9380     default:
9381       template = "f%s%c%%?\t%%%s0, %%1%s";
9382       ops[0] = reg;
9383       ops[1] = mem;
9384       break;
9385     }
9386
9387   sprintf (buff, template,
9388            load ? "ld" : "st",
9389            dp ? 'd' : 's',
9390            dp ? "P" : "",
9391            integer_p ? "\t%@ int" : "");
9392   output_asm_insn (buff, ops);
9393
9394   return "";
9395 }
9396
9397 /* Output an ADD r, s, #n where n may be too big for one instruction.
9398    If adding zero to one register, output nothing.  */
9399 const char *
9400 output_add_immediate (rtx *operands)
9401 {
9402   HOST_WIDE_INT n = INTVAL (operands[2]);
9403
9404   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
9405     {
9406       if (n < 0)
9407         output_multi_immediate (operands,
9408                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
9409                                 -n);
9410       else
9411         output_multi_immediate (operands,
9412                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
9413                                 n);
9414     }
9415
9416   return "";
9417 }
9418
9419 /* Output a multiple immediate operation.
9420    OPERANDS is the vector of operands referred to in the output patterns.
9421    INSTR1 is the output pattern to use for the first constant.
9422    INSTR2 is the output pattern to use for subsequent constants.
9423    IMMED_OP is the index of the constant slot in OPERANDS.
9424    N is the constant value.  */
9425 static const char *
9426 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
9427                         int immed_op, HOST_WIDE_INT n)
9428 {
9429 #if HOST_BITS_PER_WIDE_INT > 32
9430   n &= 0xffffffff;
9431 #endif
9432
9433   if (n == 0)
9434     {
9435       /* Quick and easy output.  */
9436       operands[immed_op] = const0_rtx;
9437       output_asm_insn (instr1, operands);
9438     }
9439   else
9440     {
9441       int i;
9442       const char * instr = instr1;
9443
9444       /* Note that n is never zero here (which would give no output).  */
9445       for (i = 0; i < 32; i += 2)
9446         {
9447           if (n & (3 << i))
9448             {
9449               operands[immed_op] = GEN_INT (n & (255 << i));
9450               output_asm_insn (instr, operands);
9451               instr = instr2;
9452               i += 6;
9453             }
9454         }
9455     }
9456
9457   return "";
9458 }
9459
9460 /* Return the name of a shifter operation.  */
9461 static const char *
9462 arm_shift_nmem(enum rtx_code code)
9463 {
9464   switch (code)
9465     {
9466     case ASHIFT:
9467       return ARM_LSL_NAME;
9468
9469     case ASHIFTRT:
9470       return "asr";
9471
9472     case LSHIFTRT:
9473       return "lsr";
9474
9475     case ROTATERT:
9476       return "ror";
9477
9478     default:
9479       abort();
9480     }
9481 }
9482
9483 /* Return the appropriate ARM instruction for the operation code.
9484    The returned result should not be overwritten.  OP is the rtx of the
9485    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9486    was shifted.  */
9487 const char *
9488 arithmetic_instr (rtx op, int shift_first_arg)
9489 {
9490   switch (GET_CODE (op))
9491     {
9492     case PLUS:
9493       return "add";
9494
9495     case MINUS:
9496       return shift_first_arg ? "rsb" : "sub";
9497
9498     case IOR:
9499       return "orr";
9500
9501     case XOR:
9502       return "eor";
9503
9504     case AND:
9505       return "and";
9506
9507     case ASHIFT:
9508     case ASHIFTRT:
9509     case LSHIFTRT:
9510     case ROTATERT:
9511       return arm_shift_nmem(GET_CODE(op));
9512
9513     default:
9514       gcc_unreachable ();
9515     }
9516 }
9517
9518 /* Ensure valid constant shifts and return the appropriate shift mnemonic
9519    for the operation code.  The returned result should not be overwritten.
9520    OP is the rtx code of the shift.
9521    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9522    shift.  */
9523 static const char *
9524 shift_op (rtx op, HOST_WIDE_INT *amountp)
9525 {
9526   const char * mnem;
9527   enum rtx_code code = GET_CODE (op);
9528
9529   switch (GET_CODE (XEXP (op, 1)))
9530     {
9531     case REG:
9532     case SUBREG:
9533       *amountp = -1;
9534       break;
9535
9536     case CONST_INT:
9537       *amountp = INTVAL (XEXP (op, 1));
9538       break;
9539
9540     default:
9541       gcc_unreachable ();
9542     }
9543
9544   switch (code)
9545     {
9546     case ROTATE:
9547       gcc_assert (*amountp != -1);
9548       *amountp = 32 - *amountp;
9549       code = ROTATERT;
9550
9551       /* Fall through.  */
9552
9553     case ASHIFT:
9554     case ASHIFTRT:
9555     case LSHIFTRT:
9556     case ROTATERT:
9557       mnem = arm_shift_nmem(code);
9558       break;
9559
9560     case MULT:
9561       /* We never have to worry about the amount being other than a
9562          power of 2, since this case can never be reloaded from a reg.  */
9563       gcc_assert (*amountp != -1);
9564       *amountp = int_log2 (*amountp);
9565       return ARM_LSL_NAME;
9566
9567     default:
9568       gcc_unreachable ();
9569     }
9570
9571   if (*amountp != -1)
9572     {
9573       /* This is not 100% correct, but follows from the desire to merge
9574          multiplication by a power of 2 with the recognizer for a
9575          shift.  >=32 is not a valid shift for "lsl", so we must try and
9576          output a shift that produces the correct arithmetical result.
9577          Using lsr #32 is identical except for the fact that the carry bit
9578          is not set correctly if we set the flags; but we never use the
9579          carry bit from such an operation, so we can ignore that.  */
9580       if (code == ROTATERT)
9581         /* Rotate is just modulo 32.  */
9582         *amountp &= 31;
9583       else if (*amountp != (*amountp & 31))
9584         {
9585           if (code == ASHIFT)
9586             mnem = "lsr";
9587           *amountp = 32;
9588         }
9589
9590       /* Shifts of 0 are no-ops.  */
9591       if (*amountp == 0)
9592         return NULL;
9593     }
9594
9595   return mnem;
9596 }
9597
9598 /* Obtain the shift from the POWER of two.  */
9599
9600 static HOST_WIDE_INT
9601 int_log2 (HOST_WIDE_INT power)
9602 {
9603   HOST_WIDE_INT shift = 0;
9604
9605   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9606     {
9607       gcc_assert (shift <= 31);
9608       shift++;
9609     }
9610
9611   return shift;
9612 }
9613
9614 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
9615    because /bin/as is horribly restrictive.  The judgement about
9616    whether or not each character is 'printable' (and can be output as
9617    is) or not (and must be printed with an octal escape) must be made
9618    with reference to the *host* character set -- the situation is
9619    similar to that discussed in the comments above pp_c_char in
9620    c-pretty-print.c.  */
9621
9622 #define MAX_ASCII_LEN 51
9623
9624 void
9625 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9626 {
9627   int i;
9628   int len_so_far = 0;
9629
9630   fputs ("\t.ascii\t\"", stream);
9631
9632   for (i = 0; i < len; i++)
9633     {
9634       int c = p[i];
9635
9636       if (len_so_far >= MAX_ASCII_LEN)
9637         {
9638           fputs ("\"\n\t.ascii\t\"", stream);
9639           len_so_far = 0;
9640         }
9641
9642       if (ISPRINT (c))
9643         {
9644           if (c == '\\' || c == '\"')
9645             {
9646               putc ('\\', stream);
9647               len_so_far++;
9648             }
9649           putc (c, stream);
9650           len_so_far++;
9651         }
9652       else
9653         {
9654           fprintf (stream, "\\%03o", c);
9655           len_so_far += 4;
9656         }
9657     }
9658
9659   fputs ("\"\n", stream);
9660 }
9661 \f
9662 /* Compute the register save mask for registers 0 through 12
9663    inclusive.  This code is used by arm_compute_save_reg_mask.  */
9664
9665 static unsigned long
9666 arm_compute_save_reg0_reg12_mask (void)
9667 {
9668   unsigned long func_type = arm_current_func_type ();
9669   unsigned long save_reg_mask = 0;
9670   unsigned int reg;
9671
9672   if (IS_INTERRUPT (func_type))
9673     {
9674       unsigned int max_reg;
9675       /* Interrupt functions must not corrupt any registers,
9676          even call clobbered ones.  If this is a leaf function
9677          we can just examine the registers used by the RTL, but
9678          otherwise we have to assume that whatever function is
9679          called might clobber anything, and so we have to save
9680          all the call-clobbered registers as well.  */
9681       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9682         /* FIQ handlers have registers r8 - r12 banked, so
9683            we only need to check r0 - r7, Normal ISRs only
9684            bank r14 and r15, so we must check up to r12.
9685            r13 is the stack pointer which is always preserved,
9686            so we do not need to consider it here.  */
9687         max_reg = 7;
9688       else
9689         max_reg = 12;
9690
9691       for (reg = 0; reg <= max_reg; reg++)
9692         if (regs_ever_live[reg]
9693             || (! current_function_is_leaf && call_used_regs [reg]))
9694           save_reg_mask |= (1 << reg);
9695
9696       /* Also save the pic base register if necessary.  */
9697       if (flag_pic
9698           && !TARGET_SINGLE_PIC_BASE
9699           && arm_pic_register != INVALID_REGNUM
9700           && current_function_uses_pic_offset_table)
9701         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9702     }
9703   else
9704     {
9705       /* In arm mode we handle r11 (FP) as a special case.  */
9706       unsigned last_reg = TARGET_ARM ? 10 : 11;
9707       
9708       /* In the normal case we only need to save those registers
9709          which are call saved and which are used by this function.  */
9710       for (reg = 0; reg <= last_reg; reg++)
9711         if (regs_ever_live[reg] && ! call_used_regs [reg])
9712           save_reg_mask |= (1 << reg);
9713
9714       /* Handle the frame pointer as a special case.  */
9715       if (TARGET_THUMB2 && frame_pointer_needed)
9716         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9717       else if (! TARGET_APCS_FRAME
9718                && ! frame_pointer_needed
9719                && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9720                && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9721         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9722
9723       /* If we aren't loading the PIC register,
9724          don't stack it even though it may be live.  */
9725       if (flag_pic
9726           && !TARGET_SINGLE_PIC_BASE
9727           && arm_pic_register != INVALID_REGNUM
9728           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9729               || current_function_uses_pic_offset_table))
9730         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9731
9732       /* The prologue will copy SP into R0, so save it.  */
9733       if (IS_STACKALIGN (func_type))
9734         save_reg_mask |= 1;
9735     }
9736
9737   /* Save registers so the exception handler can modify them.  */
9738   if (current_function_calls_eh_return)
9739     {
9740       unsigned int i;
9741
9742       for (i = 0; ; i++)
9743         {
9744           reg = EH_RETURN_DATA_REGNO (i);
9745           if (reg == INVALID_REGNUM)
9746             break;
9747           save_reg_mask |= 1 << reg;
9748         }
9749     }
9750
9751   return save_reg_mask;
9752 }
9753
9754
9755 /* Compute a bit mask of which registers need to be
9756    saved on the stack for the current function.  */
9757
9758 static unsigned long
9759 arm_compute_save_reg_mask (void)
9760 {
9761   unsigned int save_reg_mask = 0;
9762   unsigned long func_type = arm_current_func_type ();
9763   unsigned int reg;
9764
9765   if (IS_NAKED (func_type))
9766     /* This should never really happen.  */
9767     return 0;
9768
9769   /* If we are creating a stack frame, then we must save the frame pointer,
9770      IP (which will hold the old stack pointer), LR and the PC.  */
9771   if (frame_pointer_needed && TARGET_ARM)
9772     save_reg_mask |=
9773       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9774       | (1 << IP_REGNUM)
9775       | (1 << LR_REGNUM)
9776       | (1 << PC_REGNUM);
9777
9778   /* Volatile functions do not return, so there
9779      is no need to save any other registers.  */
9780   if (IS_VOLATILE (func_type))
9781     return save_reg_mask;
9782
9783   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9784
9785   /* Decide if we need to save the link register.
9786      Interrupt routines have their own banked link register,
9787      so they never need to save it.
9788      Otherwise if we do not use the link register we do not need to save
9789      it.  If we are pushing other registers onto the stack however, we
9790      can save an instruction in the epilogue by pushing the link register
9791      now and then popping it back into the PC.  This incurs extra memory
9792      accesses though, so we only do it when optimizing for size, and only
9793      if we know that we will not need a fancy return sequence.  */
9794   if (regs_ever_live [LR_REGNUM]
9795           || (save_reg_mask
9796               && optimize_size
9797               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9798               && !current_function_calls_eh_return))
9799     save_reg_mask |= 1 << LR_REGNUM;
9800
9801   if (cfun->machine->lr_save_eliminated)
9802     save_reg_mask &= ~ (1 << LR_REGNUM);
9803
9804   if (TARGET_REALLY_IWMMXT
9805       && ((bit_count (save_reg_mask)
9806            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9807     {
9808       /* The total number of registers that are going to be pushed
9809          onto the stack is odd.  We need to ensure that the stack
9810          is 64-bit aligned before we start to save iWMMXt registers,
9811          and also before we start to create locals.  (A local variable
9812          might be a double or long long which we will load/store using
9813          an iWMMXt instruction).  Therefore we need to push another
9814          ARM register, so that the stack will be 64-bit aligned.  We
9815          try to avoid using the arg registers (r0 -r3) as they might be
9816          used to pass values in a tail call.  */
9817       for (reg = 4; reg <= 12; reg++)
9818         if ((save_reg_mask & (1 << reg)) == 0)
9819           break;
9820
9821       if (reg <= 12)
9822         save_reg_mask |= (1 << reg);
9823       else
9824         {
9825           cfun->machine->sibcall_blocked = 1;
9826           save_reg_mask |= (1 << 3);
9827         }
9828     }
9829
9830   /* We may need to push an additional register for use initializing the
9831      PIC base register.  */
9832   if (TARGET_THUMB2 && IS_NESTED (func_type) && flag_pic
9833       && (save_reg_mask & THUMB2_WORK_REGS) == 0)
9834     {
9835       reg = thumb_find_work_register (1 << 4);
9836       if (!call_used_regs[reg])
9837         save_reg_mask |= (1 << reg);
9838     }
9839
9840   return save_reg_mask;
9841 }
9842
9843
9844 /* Compute a bit mask of which registers need to be
9845    saved on the stack for the current function.  */
9846 static unsigned long
9847 thumb1_compute_save_reg_mask (void)
9848 {
9849   unsigned long mask;
9850   unsigned reg;
9851
9852   mask = 0;
9853   for (reg = 0; reg < 12; reg ++)
9854     if (regs_ever_live[reg] && !call_used_regs[reg])
9855       mask |= 1 << reg;
9856
9857   if (flag_pic
9858       && !TARGET_SINGLE_PIC_BASE
9859       && arm_pic_register != INVALID_REGNUM
9860       && current_function_uses_pic_offset_table)
9861     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9862
9863   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
9864   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9865     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9866
9867   /* LR will also be pushed if any lo regs are pushed.  */
9868   if (mask & 0xff || thumb_force_lr_save ())
9869     mask |= (1 << LR_REGNUM);
9870
9871   /* Make sure we have a low work register if we need one.
9872      We will need one if we are going to push a high register,
9873      but we are not currently intending to push a low register.  */
9874   if ((mask & 0xff) == 0
9875       && ((mask & 0x0f00) || TARGET_BACKTRACE))
9876     {
9877       /* Use thumb_find_work_register to choose which register
9878          we will use.  If the register is live then we will
9879          have to push it.  Use LAST_LO_REGNUM as our fallback
9880          choice for the register to select.  */
9881       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9882
9883       if (! call_used_regs[reg])
9884         mask |= 1 << reg;
9885     }
9886
9887   return mask;
9888 }
9889
9890
9891 /* Return the number of bytes required to save VFP registers.  */
9892 static int
9893 arm_get_vfp_saved_size (void)
9894 {
9895   unsigned int regno;
9896   int count;
9897   int saved;
9898
9899   saved = 0;
9900   /* Space for saved VFP registers.  */
9901   if (TARGET_HARD_FLOAT && TARGET_VFP)
9902     {
9903       count = 0;
9904       for (regno = FIRST_VFP_REGNUM;
9905            regno < LAST_VFP_REGNUM;
9906            regno += 2)
9907         {
9908           if ((!regs_ever_live[regno] || call_used_regs[regno])
9909               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9910             {
9911               if (count > 0)
9912                 {
9913                   /* Workaround ARM10 VFPr1 bug.  */
9914                   if (count == 2 && !arm_arch6)
9915                     count++;
9916                   saved += count * 8;
9917                 }
9918               count = 0;
9919             }
9920           else
9921             count++;
9922         }
9923       if (count > 0)
9924         {
9925           if (count == 2 && !arm_arch6)
9926             count++;
9927           saved += count * 8;
9928         }
9929     }
9930   return saved;
9931 }
9932
9933
9934 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9935    everything bar the final return instruction.  */
9936 const char *
9937 output_return_instruction (rtx operand, int really_return, int reverse)
9938 {
9939   char conditional[10];
9940   char instr[100];
9941   unsigned reg;
9942   unsigned long live_regs_mask;
9943   unsigned long func_type;
9944   arm_stack_offsets *offsets;
9945
9946   func_type = arm_current_func_type ();
9947
9948   if (IS_NAKED (func_type))
9949     return "";
9950
9951   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9952     {
9953       /* If this function was declared non-returning, and we have
9954          found a tail call, then we have to trust that the called
9955          function won't return.  */
9956       if (really_return)
9957         {
9958           rtx ops[2];
9959
9960           /* Otherwise, trap an attempted return by aborting.  */
9961           ops[0] = operand;
9962           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9963                                        : "abort");
9964           assemble_external_libcall (ops[1]);
9965           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9966         }
9967
9968       return "";
9969     }
9970
9971   gcc_assert (!current_function_calls_alloca || really_return);
9972
9973   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9974
9975   return_used_this_function = 1;
9976
9977   live_regs_mask = arm_compute_save_reg_mask ();
9978
9979   if (live_regs_mask)
9980     {
9981       const char * return_reg;
9982
9983       /* If we do not have any special requirements for function exit
9984          (e.g. interworking) then we can load the return address
9985          directly into the PC.  Otherwise we must load it into LR.  */
9986       if (really_return
9987           && (IS_INTERRUPT (func_type) || !TARGET_INTERWORK))
9988         return_reg = reg_names[PC_REGNUM];
9989       else
9990         return_reg = reg_names[LR_REGNUM];
9991
9992       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9993         {
9994           /* There are three possible reasons for the IP register
9995              being saved.  1) a stack frame was created, in which case
9996              IP contains the old stack pointer, or 2) an ISR routine
9997              corrupted it, or 3) it was saved to align the stack on
9998              iWMMXt.  In case 1, restore IP into SP, otherwise just
9999              restore IP.  */
10000           if (frame_pointer_needed)
10001             {
10002               live_regs_mask &= ~ (1 << IP_REGNUM);
10003               live_regs_mask |=   (1 << SP_REGNUM);
10004             }
10005           else
10006             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
10007         }
10008
10009       /* On some ARM architectures it is faster to use LDR rather than
10010          LDM to load a single register.  On other architectures, the
10011          cost is the same.  In 26 bit mode, or for exception handlers,
10012          we have to use LDM to load the PC so that the CPSR is also
10013          restored.  */
10014       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
10015         if (live_regs_mask == (1U << reg))
10016           break;
10017
10018       if (reg <= LAST_ARM_REGNUM
10019           && (reg != LR_REGNUM
10020               || ! really_return
10021               || ! IS_INTERRUPT (func_type)))
10022         {
10023           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
10024                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
10025         }
10026       else
10027         {
10028           char *p;
10029           int first = 1;
10030
10031           /* Generate the load multiple instruction to restore the
10032              registers.  Note we can get here, even if
10033              frame_pointer_needed is true, but only if sp already
10034              points to the base of the saved core registers.  */
10035           if (live_regs_mask & (1 << SP_REGNUM))
10036             {
10037               unsigned HOST_WIDE_INT stack_adjust;
10038
10039               offsets = arm_get_frame_offsets ();
10040               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
10041               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
10042
10043               if (stack_adjust && arm_arch5 && TARGET_ARM)
10044                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
10045               else
10046                 {
10047                   /* If we can't use ldmib (SA110 bug),
10048                      then try to pop r3 instead.  */
10049                   if (stack_adjust)
10050                     live_regs_mask |= 1 << 3;
10051                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
10052                 }
10053             }
10054           else
10055             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
10056
10057           p = instr + strlen (instr);
10058
10059           for (reg = 0; reg <= SP_REGNUM; reg++)
10060             if (live_regs_mask & (1 << reg))
10061               {
10062                 int l = strlen (reg_names[reg]);
10063
10064                 if (first)
10065                   first = 0;
10066                 else
10067                   {
10068                     memcpy (p, ", ", 2);
10069                     p += 2;
10070                   }
10071
10072                 memcpy (p, "%|", 2);
10073                 memcpy (p + 2, reg_names[reg], l);
10074                 p += l + 2;
10075               }
10076
10077           if (live_regs_mask & (1 << LR_REGNUM))
10078             {
10079               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
10080               /* If returning from an interrupt, restore the CPSR.  */
10081               if (IS_INTERRUPT (func_type))
10082                 strcat (p, "^");
10083             }
10084           else
10085             strcpy (p, "}");
10086         }
10087
10088       output_asm_insn (instr, & operand);
10089
10090       /* See if we need to generate an extra instruction to
10091          perform the actual function return.  */
10092       if (really_return
10093           && func_type != ARM_FT_INTERWORKED
10094           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
10095         {
10096           /* The return has already been handled
10097              by loading the LR into the PC.  */
10098           really_return = 0;
10099         }
10100     }
10101
10102   if (really_return)
10103     {
10104       switch ((int) ARM_FUNC_TYPE (func_type))
10105         {
10106         case ARM_FT_ISR:
10107         case ARM_FT_FIQ:
10108           /* ??? This is wrong for unified assembly syntax.  */
10109           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
10110           break;
10111
10112         case ARM_FT_INTERWORKED:
10113           sprintf (instr, "bx%s\t%%|lr", conditional);
10114           break;
10115
10116         case ARM_FT_EXCEPTION:
10117           /* ??? This is wrong for unified assembly syntax.  */
10118           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
10119           break;
10120
10121         default:
10122           /* Use bx if it's available.  */
10123           if (arm_arch5 || arm_arch4t)
10124             sprintf (instr, "bx%s\t%%|lr", conditional);
10125           else
10126             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
10127           break;
10128         }
10129
10130       output_asm_insn (instr, & operand);
10131     }
10132
10133   return "";
10134 }
10135
10136 /* Write the function name into the code section, directly preceding
10137    the function prologue.
10138
10139    Code will be output similar to this:
10140      t0
10141          .ascii "arm_poke_function_name", 0
10142          .align
10143      t1
10144          .word 0xff000000 + (t1 - t0)
10145      arm_poke_function_name
10146          mov     ip, sp
10147          stmfd   sp!, {fp, ip, lr, pc}
10148          sub     fp, ip, #4
10149
10150    When performing a stack backtrace, code can inspect the value
10151    of 'pc' stored at 'fp' + 0.  If the trace function then looks
10152    at location pc - 12 and the top 8 bits are set, then we know
10153    that there is a function name embedded immediately preceding this
10154    location and has length ((pc[-3]) & 0xff000000).
10155
10156    We assume that pc is declared as a pointer to an unsigned long.
10157
10158    It is of no benefit to output the function name if we are assembling
10159    a leaf function.  These function types will not contain a stack
10160    backtrace structure, therefore it is not possible to determine the
10161    function name.  */
10162 void
10163 arm_poke_function_name (FILE *stream, const char *name)
10164 {
10165   unsigned long alignlength;
10166   unsigned long length;
10167   rtx           x;
10168
10169   length      = strlen (name) + 1;
10170   alignlength = ROUND_UP_WORD (length);
10171
10172   ASM_OUTPUT_ASCII (stream, name, length);
10173   ASM_OUTPUT_ALIGN (stream, 2);
10174   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
10175   assemble_aligned_integer (UNITS_PER_WORD, x);
10176 }
10177
10178 /* Place some comments into the assembler stream
10179    describing the current function.  */
10180 static void
10181 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
10182 {
10183   unsigned long func_type;
10184
10185   if (TARGET_THUMB1)
10186     {
10187       thumb1_output_function_prologue (f, frame_size);
10188       return;
10189     }
10190
10191   /* Sanity check.  */
10192   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
10193
10194   func_type = arm_current_func_type ();
10195
10196   switch ((int) ARM_FUNC_TYPE (func_type))
10197     {
10198     default:
10199     case ARM_FT_NORMAL:
10200       break;
10201     case ARM_FT_INTERWORKED:
10202       asm_fprintf (f, "\t%@ Function supports interworking.\n");
10203       break;
10204     case ARM_FT_ISR:
10205       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
10206       break;
10207     case ARM_FT_FIQ:
10208       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
10209       break;
10210     case ARM_FT_EXCEPTION:
10211       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
10212       break;
10213     }
10214
10215   if (IS_NAKED (func_type))
10216     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
10217
10218   if (IS_VOLATILE (func_type))
10219     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
10220
10221   if (IS_NESTED (func_type))
10222     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
10223   if (IS_STACKALIGN (func_type))
10224     asm_fprintf (f, "\t%@ Stack Align: May be called with mis-aligned SP.\n");
10225
10226   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
10227                current_function_args_size,
10228                current_function_pretend_args_size, frame_size);
10229
10230   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
10231                frame_pointer_needed,
10232                cfun->machine->uses_anonymous_args);
10233
10234   if (cfun->machine->lr_save_eliminated)
10235     asm_fprintf (f, "\t%@ link register save eliminated.\n");
10236
10237   if (current_function_calls_eh_return)
10238     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
10239
10240 #ifdef AOF_ASSEMBLER
10241   if (flag_pic)
10242     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
10243 #endif
10244
10245   return_used_this_function = 0;
10246 }
10247
10248 const char *
10249 arm_output_epilogue (rtx sibling)
10250 {
10251   int reg;
10252   unsigned long saved_regs_mask;
10253   unsigned long func_type;
10254   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
10255      frame that is $fp + 4 for a non-variadic function.  */
10256   int floats_offset = 0;
10257   rtx operands[3];
10258   FILE * f = asm_out_file;
10259   unsigned int lrm_count = 0;
10260   int really_return = (sibling == NULL);
10261   int start_reg;
10262   arm_stack_offsets *offsets;
10263
10264   /* If we have already generated the return instruction
10265      then it is futile to generate anything else.  */
10266   if (use_return_insn (FALSE, sibling) && return_used_this_function)
10267     return "";
10268
10269   func_type = arm_current_func_type ();
10270
10271   if (IS_NAKED (func_type))
10272     /* Naked functions don't have epilogues.  */
10273     return "";
10274
10275   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
10276     {
10277       rtx op;
10278
10279       /* A volatile function should never return.  Call abort.  */
10280       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
10281       assemble_external_libcall (op);
10282       output_asm_insn ("bl\t%a0", &op);
10283
10284       return "";
10285     }
10286
10287   /* If we are throwing an exception, then we really must be doing a
10288      return, so we can't tail-call.  */
10289   gcc_assert (!current_function_calls_eh_return || really_return);
10290
10291   offsets = arm_get_frame_offsets ();
10292   saved_regs_mask = arm_compute_save_reg_mask ();
10293
10294   if (TARGET_IWMMXT)
10295     lrm_count = bit_count (saved_regs_mask);
10296
10297   floats_offset = offsets->saved_args;
10298   /* Compute how far away the floats will be.  */
10299   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
10300     if (saved_regs_mask & (1 << reg))
10301       floats_offset += 4;
10302
10303   if (frame_pointer_needed && TARGET_ARM)
10304     {
10305       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
10306       int vfp_offset = offsets->frame;
10307
10308       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10309         {
10310           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10311             if (regs_ever_live[reg] && !call_used_regs[reg])
10312               {
10313                 floats_offset += 12;
10314                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
10315                              reg, FP_REGNUM, floats_offset - vfp_offset);
10316               }
10317         }
10318       else
10319         {
10320           start_reg = LAST_FPA_REGNUM;
10321
10322           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10323             {
10324               if (regs_ever_live[reg] && !call_used_regs[reg])
10325                 {
10326                   floats_offset += 12;
10327
10328                   /* We can't unstack more than four registers at once.  */
10329                   if (start_reg - reg == 3)
10330                     {
10331                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
10332                                    reg, FP_REGNUM, floats_offset - vfp_offset);
10333                       start_reg = reg - 1;
10334                     }
10335                 }
10336               else
10337                 {
10338                   if (reg != start_reg)
10339                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
10340                                  reg + 1, start_reg - reg,
10341                                  FP_REGNUM, floats_offset - vfp_offset);
10342                   start_reg = reg - 1;
10343                 }
10344             }
10345
10346           /* Just in case the last register checked also needs unstacking.  */
10347           if (reg != start_reg)
10348             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
10349                          reg + 1, start_reg - reg,
10350                          FP_REGNUM, floats_offset - vfp_offset);
10351         }
10352
10353       if (TARGET_HARD_FLOAT && TARGET_VFP)
10354         {
10355           int saved_size;
10356
10357           /* The fldmd insns do not have base+offset addressing
10358              modes, so we use IP to hold the address.  */
10359           saved_size = arm_get_vfp_saved_size ();
10360
10361           if (saved_size > 0)
10362             {
10363               floats_offset += saved_size;
10364               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
10365                            FP_REGNUM, floats_offset - vfp_offset);
10366             }
10367           start_reg = FIRST_VFP_REGNUM;
10368           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10369             {
10370               if ((!regs_ever_live[reg] || call_used_regs[reg])
10371                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10372                 {
10373                   if (start_reg != reg)
10374                     vfp_output_fldmd (f, IP_REGNUM,
10375                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10376                                       (reg - start_reg) / 2);
10377                   start_reg = reg + 2;
10378                 }
10379             }
10380           if (start_reg != reg)
10381             vfp_output_fldmd (f, IP_REGNUM,
10382                               (start_reg - FIRST_VFP_REGNUM) / 2,
10383                               (reg - start_reg) / 2);
10384         }
10385
10386       if (TARGET_IWMMXT)
10387         {
10388           /* The frame pointer is guaranteed to be non-double-word aligned.
10389              This is because it is set to (old_stack_pointer - 4) and the
10390              old_stack_pointer was double word aligned.  Thus the offset to
10391              the iWMMXt registers to be loaded must also be non-double-word
10392              sized, so that the resultant address *is* double-word aligned.
10393              We can ignore floats_offset since that was already included in
10394              the live_regs_mask.  */
10395           lrm_count += (lrm_count % 2 ? 2 : 1);
10396
10397           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10398             if (regs_ever_live[reg] && !call_used_regs[reg])
10399               {
10400                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
10401                              reg, FP_REGNUM, lrm_count * 4);
10402                 lrm_count += 2;
10403               }
10404         }
10405
10406       /* saved_regs_mask should contain the IP, which at the time of stack
10407          frame generation actually contains the old stack pointer.  So a
10408          quick way to unwind the stack is just pop the IP register directly
10409          into the stack pointer.  */
10410       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
10411       saved_regs_mask &= ~ (1 << IP_REGNUM);
10412       saved_regs_mask |=   (1 << SP_REGNUM);
10413
10414       /* There are two registers left in saved_regs_mask - LR and PC.  We
10415          only need to restore the LR register (the return address), but to
10416          save time we can load it directly into the PC, unless we need a
10417          special function exit sequence, or we are not really returning.  */
10418       if (really_return
10419           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10420           && !current_function_calls_eh_return)
10421         /* Delete the LR from the register mask, so that the LR on
10422            the stack is loaded into the PC in the register mask.  */
10423         saved_regs_mask &= ~ (1 << LR_REGNUM);
10424       else
10425         saved_regs_mask &= ~ (1 << PC_REGNUM);
10426
10427       /* We must use SP as the base register, because SP is one of the
10428          registers being restored.  If an interrupt or page fault
10429          happens in the ldm instruction, the SP might or might not
10430          have been restored.  That would be bad, as then SP will no
10431          longer indicate the safe area of stack, and we can get stack
10432          corruption.  Using SP as the base register means that it will
10433          be reset correctly to the original value, should an interrupt
10434          occur.  If the stack pointer already points at the right
10435          place, then omit the subtraction.  */
10436       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
10437           || current_function_calls_alloca)
10438         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
10439                      4 * bit_count (saved_regs_mask));
10440       print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask, 0);
10441
10442       if (IS_INTERRUPT (func_type))
10443         /* Interrupt handlers will have pushed the
10444            IP onto the stack, so restore it now.  */
10445         print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, 1 << IP_REGNUM, 0);
10446     }
10447   else
10448     {
10449       HOST_WIDE_INT amount;
10450       int rfe;
10451       /* Restore stack pointer if necessary.  */
10452       if (frame_pointer_needed)
10453         {
10454           /* For Thumb-2 restore sp from the frame pointer.
10455              Operand restrictions mean we have to increment FP, then copy
10456              to SP.  */
10457           amount = offsets->locals_base - offsets->saved_regs;
10458           operands[0] = hard_frame_pointer_rtx;
10459         }
10460       else
10461         {
10462           operands[0] = stack_pointer_rtx;
10463           amount = offsets->outgoing_args - offsets->saved_regs;
10464         }
10465
10466       if (amount)
10467         {
10468           operands[1] = operands[0];
10469           operands[2] = GEN_INT (amount);
10470           output_add_immediate (operands);
10471         }
10472       if (frame_pointer_needed)
10473         asm_fprintf (f, "\tmov\t%r, %r\n",
10474                      SP_REGNUM, HARD_FRAME_POINTER_REGNUM);
10475
10476       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10477         {
10478           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10479             if (regs_ever_live[reg] && !call_used_regs[reg])
10480               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
10481                            reg, SP_REGNUM);
10482         }
10483       else
10484         {
10485           start_reg = FIRST_FPA_REGNUM;
10486
10487           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
10488             {
10489               if (regs_ever_live[reg] && !call_used_regs[reg])
10490                 {
10491                   if (reg - start_reg == 3)
10492                     {
10493                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
10494                                    start_reg, SP_REGNUM);
10495                       start_reg = reg + 1;
10496                     }
10497                 }
10498               else
10499                 {
10500                   if (reg != start_reg)
10501                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10502                                  start_reg, reg - start_reg,
10503                                  SP_REGNUM);
10504
10505                   start_reg = reg + 1;
10506                 }
10507             }
10508
10509           /* Just in case the last register checked also needs unstacking.  */
10510           if (reg != start_reg)
10511             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
10512                          start_reg, reg - start_reg, SP_REGNUM);
10513         }
10514
10515       if (TARGET_HARD_FLOAT && TARGET_VFP)
10516         {
10517           start_reg = FIRST_VFP_REGNUM;
10518           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10519             {
10520               if ((!regs_ever_live[reg] || call_used_regs[reg])
10521                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10522                 {
10523                   if (start_reg != reg)
10524                     vfp_output_fldmd (f, SP_REGNUM,
10525                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10526                                       (reg - start_reg) / 2);
10527                   start_reg = reg + 2;
10528                 }
10529             }
10530           if (start_reg != reg)
10531             vfp_output_fldmd (f, SP_REGNUM,
10532                               (start_reg - FIRST_VFP_REGNUM) / 2,
10533                               (reg - start_reg) / 2);
10534         }
10535       if (TARGET_IWMMXT)
10536         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10537           if (regs_ever_live[reg] && !call_used_regs[reg])
10538             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10539
10540       /* If we can, restore the LR into the PC.  */
10541       if (ARM_FUNC_TYPE (func_type) != ARM_FT_INTERWORKED
10542           && (TARGET_ARM || ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL)
10543           && !IS_STACKALIGN (func_type)
10544           && really_return
10545           && current_function_pretend_args_size == 0
10546           && saved_regs_mask & (1 << LR_REGNUM)
10547           && !current_function_calls_eh_return)
10548         {
10549           saved_regs_mask &= ~ (1 << LR_REGNUM);
10550           saved_regs_mask |=   (1 << PC_REGNUM);
10551           rfe = IS_INTERRUPT (func_type);
10552         }
10553       else
10554         rfe = 0;
10555
10556       /* Load the registers off the stack.  If we only have one register
10557          to load use the LDR instruction - it is faster.  For Thumb-2
10558          always use pop and the assembler will pick the best instruction.*/
10559       if (TARGET_ARM && saved_regs_mask == (1 << LR_REGNUM)
10560           && !IS_INTERRUPT(func_type))
10561         {
10562           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10563         }
10564       else if (saved_regs_mask)
10565         {
10566           if (saved_regs_mask & (1 << SP_REGNUM))
10567             /* Note - write back to the stack register is not enabled
10568                (i.e. "ldmfd sp!...").  We know that the stack pointer is
10569                in the list of registers and if we add writeback the
10570                instruction becomes UNPREDICTABLE.  */
10571             print_multi_reg (f, "ldmfd\t%r, ", SP_REGNUM, saved_regs_mask,
10572                              rfe);
10573           else if (TARGET_ARM)
10574             print_multi_reg (f, "ldmfd\t%r!, ", SP_REGNUM, saved_regs_mask,
10575                              rfe);
10576           else
10577             print_multi_reg (f, "pop\t", SP_REGNUM, saved_regs_mask, 0);
10578         }
10579
10580       if (current_function_pretend_args_size)
10581         {
10582           /* Unwind the pre-pushed regs.  */
10583           operands[0] = operands[1] = stack_pointer_rtx;
10584           operands[2] = GEN_INT (current_function_pretend_args_size);
10585           output_add_immediate (operands);
10586         }
10587     }
10588
10589   /* We may have already restored PC directly from the stack.  */
10590   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10591     return "";
10592
10593   /* Stack adjustment for exception handler.  */
10594   if (current_function_calls_eh_return)
10595     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10596                  ARM_EH_STACKADJ_REGNUM);
10597
10598   /* Generate the return instruction.  */
10599   switch ((int) ARM_FUNC_TYPE (func_type))
10600     {
10601     case ARM_FT_ISR:
10602     case ARM_FT_FIQ:
10603       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10604       break;
10605
10606     case ARM_FT_EXCEPTION:
10607       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10608       break;
10609
10610     case ARM_FT_INTERWORKED:
10611       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10612       break;
10613
10614     default:
10615       if (IS_STACKALIGN (func_type))
10616         {
10617           /* See comment in arm_expand_prologue.  */
10618           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, 0);
10619         }
10620       if (arm_arch5 || arm_arch4t)
10621         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10622       else
10623         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10624       break;
10625     }
10626
10627   return "";
10628 }
10629
10630 static void
10631 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10632                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10633 {
10634   arm_stack_offsets *offsets;
10635
10636   if (TARGET_THUMB1)
10637     {
10638       int regno;
10639
10640       /* Emit any call-via-reg trampolines that are needed for v4t support
10641          of call_reg and call_value_reg type insns.  */
10642       for (regno = 0; regno < LR_REGNUM; regno++)
10643         {
10644           rtx label = cfun->machine->call_via[regno];
10645
10646           if (label != NULL)
10647             {
10648               switch_to_section (function_section (current_function_decl));
10649               targetm.asm_out.internal_label (asm_out_file, "L",
10650                                               CODE_LABEL_NUMBER (label));
10651               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10652             }
10653         }
10654
10655       /* ??? Probably not safe to set this here, since it assumes that a
10656          function will be emitted as assembly immediately after we generate
10657          RTL for it.  This does not happen for inline functions.  */
10658       return_used_this_function = 0;
10659     }
10660   else /* TARGET_32BIT */
10661     {
10662       /* We need to take into account any stack-frame rounding.  */
10663       offsets = arm_get_frame_offsets ();
10664
10665       gcc_assert (!use_return_insn (FALSE, NULL)
10666                   || !return_used_this_function
10667                   || offsets->saved_regs == offsets->outgoing_args
10668                   || frame_pointer_needed);
10669
10670       /* Reset the ARM-specific per-function variables.  */
10671       after_arm_reorg = 0;
10672     }
10673 }
10674
10675 /* Generate and emit an insn that we will recognize as a push_multi.
10676    Unfortunately, since this insn does not reflect very well the actual
10677    semantics of the operation, we need to annotate the insn for the benefit
10678    of DWARF2 frame unwind information.  */
10679 static rtx
10680 emit_multi_reg_push (unsigned long mask)
10681 {
10682   int num_regs = 0;
10683   int num_dwarf_regs;
10684   int i, j;
10685   rtx par;
10686   rtx dwarf;
10687   int dwarf_par_index;
10688   rtx tmp, reg;
10689
10690   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10691     if (mask & (1 << i))
10692       num_regs++;
10693
10694   gcc_assert (num_regs && num_regs <= 16);
10695
10696   /* We don't record the PC in the dwarf frame information.  */
10697   num_dwarf_regs = num_regs;
10698   if (mask & (1 << PC_REGNUM))
10699     num_dwarf_regs--;
10700
10701   /* For the body of the insn we are going to generate an UNSPEC in
10702      parallel with several USEs.  This allows the insn to be recognized
10703      by the push_multi pattern in the arm.md file.  The insn looks
10704      something like this:
10705
10706        (parallel [
10707            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10708                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10709            (use (reg:SI 11 fp))
10710            (use (reg:SI 12 ip))
10711            (use (reg:SI 14 lr))
10712            (use (reg:SI 15 pc))
10713         ])
10714
10715      For the frame note however, we try to be more explicit and actually
10716      show each register being stored into the stack frame, plus a (single)
10717      decrement of the stack pointer.  We do it this way in order to be
10718      friendly to the stack unwinding code, which only wants to see a single
10719      stack decrement per instruction.  The RTL we generate for the note looks
10720      something like this:
10721
10722       (sequence [
10723            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10724            (set (mem:SI (reg:SI sp)) (reg:SI r4))
10725            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10726            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10727            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10728         ])
10729
10730       This sequence is used both by the code to support stack unwinding for
10731       exceptions handlers and the code to generate dwarf2 frame debugging.  */
10732
10733   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10734   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10735   dwarf_par_index = 1;
10736
10737   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10738     {
10739       if (mask & (1 << i))
10740         {
10741           reg = gen_rtx_REG (SImode, i);
10742
10743           XVECEXP (par, 0, 0)
10744             = gen_rtx_SET (VOIDmode,
10745                            gen_frame_mem (BLKmode,
10746                                           gen_rtx_PRE_DEC (BLKmode,
10747                                                            stack_pointer_rtx)),
10748                            gen_rtx_UNSPEC (BLKmode,
10749                                            gen_rtvec (1, reg),
10750                                            UNSPEC_PUSH_MULT));
10751
10752           if (i != PC_REGNUM)
10753             {
10754               tmp = gen_rtx_SET (VOIDmode,
10755                                  gen_frame_mem (SImode, stack_pointer_rtx),
10756                                  reg);
10757               RTX_FRAME_RELATED_P (tmp) = 1;
10758               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10759               dwarf_par_index++;
10760             }
10761
10762           break;
10763         }
10764     }
10765
10766   for (j = 1, i++; j < num_regs; i++)
10767     {
10768       if (mask & (1 << i))
10769         {
10770           reg = gen_rtx_REG (SImode, i);
10771
10772           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10773
10774           if (i != PC_REGNUM)
10775             {
10776               tmp
10777                 = gen_rtx_SET (VOIDmode,
10778                                gen_frame_mem (SImode,
10779                                               plus_constant (stack_pointer_rtx,
10780                                                              4 * j)),
10781                                reg);
10782               RTX_FRAME_RELATED_P (tmp) = 1;
10783               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10784             }
10785
10786           j++;
10787         }
10788     }
10789
10790   par = emit_insn (par);
10791
10792   tmp = gen_rtx_SET (VOIDmode,
10793                      stack_pointer_rtx,
10794                      plus_constant (stack_pointer_rtx, -4 * num_regs));
10795   RTX_FRAME_RELATED_P (tmp) = 1;
10796   XVECEXP (dwarf, 0, 0) = tmp;
10797
10798   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10799                                        REG_NOTES (par));
10800   return par;
10801 }
10802
10803 /* Calculate the size of the return value that is passed in registers.  */
10804 static int
10805 arm_size_return_regs (void)
10806 {
10807   enum machine_mode mode;
10808
10809   if (current_function_return_rtx != 0)
10810     mode = GET_MODE (current_function_return_rtx);
10811   else
10812     mode = DECL_MODE (DECL_RESULT (current_function_decl));
10813
10814   return GET_MODE_SIZE (mode);
10815 }
10816
10817 static rtx
10818 emit_sfm (int base_reg, int count)
10819 {
10820   rtx par;
10821   rtx dwarf;
10822   rtx tmp, reg;
10823   int i;
10824
10825   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10826   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10827
10828   reg = gen_rtx_REG (XFmode, base_reg++);
10829
10830   XVECEXP (par, 0, 0)
10831     = gen_rtx_SET (VOIDmode,
10832                    gen_frame_mem (BLKmode,
10833                                   gen_rtx_PRE_DEC (BLKmode,
10834                                                    stack_pointer_rtx)),
10835                    gen_rtx_UNSPEC (BLKmode,
10836                                    gen_rtvec (1, reg),
10837                                    UNSPEC_PUSH_MULT));
10838   tmp = gen_rtx_SET (VOIDmode,
10839                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10840   RTX_FRAME_RELATED_P (tmp) = 1;
10841   XVECEXP (dwarf, 0, 1) = tmp;
10842
10843   for (i = 1; i < count; i++)
10844     {
10845       reg = gen_rtx_REG (XFmode, base_reg++);
10846       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10847
10848       tmp = gen_rtx_SET (VOIDmode,
10849                          gen_frame_mem (XFmode,
10850                                         plus_constant (stack_pointer_rtx,
10851                                                        i * 12)),
10852                          reg);
10853       RTX_FRAME_RELATED_P (tmp) = 1;
10854       XVECEXP (dwarf, 0, i + 1) = tmp;
10855     }
10856
10857   tmp = gen_rtx_SET (VOIDmode,
10858                      stack_pointer_rtx,
10859                      plus_constant (stack_pointer_rtx, -12 * count));
10860
10861   RTX_FRAME_RELATED_P (tmp) = 1;
10862   XVECEXP (dwarf, 0, 0) = tmp;
10863
10864   par = emit_insn (par);
10865   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10866                                        REG_NOTES (par));
10867   return par;
10868 }
10869
10870
10871 /* Return true if the current function needs to save/restore LR.  */
10872
10873 static bool
10874 thumb_force_lr_save (void)
10875 {
10876   return !cfun->machine->lr_save_eliminated
10877          && (!leaf_function_p ()
10878              || thumb_far_jump_used_p ()
10879              || regs_ever_live [LR_REGNUM]);
10880 }
10881
10882
10883 /* Compute the distance from register FROM to register TO.
10884    These can be the arg pointer (26), the soft frame pointer (25),
10885    the stack pointer (13) or the hard frame pointer (11).
10886    In thumb mode r7 is used as the soft frame pointer, if needed.
10887    Typical stack layout looks like this:
10888
10889        old stack pointer -> |    |
10890                              ----
10891                             |    | \
10892                             |    |   saved arguments for
10893                             |    |   vararg functions
10894                             |    | /
10895                               --
10896    hard FP & arg pointer -> |    | \
10897                             |    |   stack
10898                             |    |   frame
10899                             |    | /
10900                               --
10901                             |    | \
10902                             |    |   call saved
10903                             |    |   registers
10904       soft frame pointer -> |    | /
10905                               --
10906                             |    | \
10907                             |    |   local
10908                             |    |   variables
10909      locals base pointer -> |    | /
10910                               --
10911                             |    | \
10912                             |    |   outgoing
10913                             |    |   arguments
10914    current stack pointer -> |    | /
10915                               --
10916
10917   For a given function some or all of these stack components
10918   may not be needed, giving rise to the possibility of
10919   eliminating some of the registers.
10920
10921   The values returned by this function must reflect the behavior
10922   of arm_expand_prologue() and arm_compute_save_reg_mask().
10923
10924   The sign of the number returned reflects the direction of stack
10925   growth, so the values are positive for all eliminations except
10926   from the soft frame pointer to the hard frame pointer.
10927
10928   SFP may point just inside the local variables block to ensure correct
10929   alignment.  */
10930
10931
10932 /* Calculate stack offsets.  These are used to calculate register elimination
10933    offsets and in prologue/epilogue code.  */
10934
10935 static arm_stack_offsets *
10936 arm_get_frame_offsets (void)
10937 {
10938   struct arm_stack_offsets *offsets;
10939   unsigned long func_type;
10940   int leaf;
10941   int saved;
10942   HOST_WIDE_INT frame_size;
10943
10944   offsets = &cfun->machine->stack_offsets;
10945
10946   /* We need to know if we are a leaf function.  Unfortunately, it
10947      is possible to be called after start_sequence has been called,
10948      which causes get_insns to return the insns for the sequence,
10949      not the function, which will cause leaf_function_p to return
10950      the incorrect result.
10951
10952      to know about leaf functions once reload has completed, and the
10953      frame size cannot be changed after that time, so we can safely
10954      use the cached value.  */
10955
10956   if (reload_completed)
10957     return offsets;
10958
10959   /* Initially this is the size of the local variables.  It will translated
10960      into an offset once we have determined the size of preceding data.  */
10961   frame_size = ROUND_UP_WORD (get_frame_size ());
10962
10963   leaf = leaf_function_p ();
10964
10965   /* Space for variadic functions.  */
10966   offsets->saved_args = current_function_pretend_args_size;
10967
10968   /* In Thumb mode this is incorrect, but never used.  */
10969   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10970
10971   if (TARGET_32BIT)
10972     {
10973       unsigned int regno;
10974
10975       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10976
10977       /* We know that SP will be doubleword aligned on entry, and we must
10978          preserve that condition at any subroutine call.  We also require the
10979          soft frame pointer to be doubleword aligned.  */
10980
10981       if (TARGET_REALLY_IWMMXT)
10982         {
10983           /* Check for the call-saved iWMMXt registers.  */
10984           for (regno = FIRST_IWMMXT_REGNUM;
10985                regno <= LAST_IWMMXT_REGNUM;
10986                regno++)
10987             if (regs_ever_live [regno] && ! call_used_regs [regno])
10988               saved += 8;
10989         }
10990
10991       func_type = arm_current_func_type ();
10992       if (! IS_VOLATILE (func_type))
10993         {
10994           /* Space for saved FPA registers.  */
10995           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10996           if (regs_ever_live[regno] && ! call_used_regs[regno])
10997             saved += 12;
10998
10999           /* Space for saved VFP registers.  */
11000           if (TARGET_HARD_FLOAT && TARGET_VFP)
11001             saved += arm_get_vfp_saved_size ();
11002         }
11003     }
11004   else /* TARGET_THUMB1 */
11005     {
11006       saved = bit_count (thumb1_compute_save_reg_mask ()) * 4;
11007       if (TARGET_BACKTRACE)
11008         saved += 16;
11009     }
11010
11011   /* Saved registers include the stack frame.  */
11012   offsets->saved_regs = offsets->saved_args + saved;
11013   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
11014   /* A leaf function does not need any stack alignment if it has nothing
11015      on the stack.  */
11016   if (leaf && frame_size == 0)
11017     {
11018       offsets->outgoing_args = offsets->soft_frame;
11019       return offsets;
11020     }
11021
11022   /* Ensure SFP has the correct alignment.  */
11023   if (ARM_DOUBLEWORD_ALIGN
11024       && (offsets->soft_frame & 7))
11025     offsets->soft_frame += 4;
11026
11027   offsets->locals_base = offsets->soft_frame + frame_size;
11028   offsets->outgoing_args = (offsets->locals_base
11029                             + current_function_outgoing_args_size);
11030
11031   if (ARM_DOUBLEWORD_ALIGN)
11032     {
11033       /* Ensure SP remains doubleword aligned.  */
11034       if (offsets->outgoing_args & 7)
11035         offsets->outgoing_args += 4;
11036       gcc_assert (!(offsets->outgoing_args & 7));
11037     }
11038
11039   return offsets;
11040 }
11041
11042
11043 /* Calculate the relative offsets for the different stack pointers.  Positive
11044    offsets are in the direction of stack growth.  */
11045
11046 HOST_WIDE_INT
11047 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
11048 {
11049   arm_stack_offsets *offsets;
11050
11051   offsets = arm_get_frame_offsets ();
11052
11053   /* OK, now we have enough information to compute the distances.
11054      There must be an entry in these switch tables for each pair
11055      of registers in ELIMINABLE_REGS, even if some of the entries
11056      seem to be redundant or useless.  */
11057   switch (from)
11058     {
11059     case ARG_POINTER_REGNUM:
11060       switch (to)
11061         {
11062         case THUMB_HARD_FRAME_POINTER_REGNUM:
11063           return 0;
11064
11065         case FRAME_POINTER_REGNUM:
11066           /* This is the reverse of the soft frame pointer
11067              to hard frame pointer elimination below.  */
11068           return offsets->soft_frame - offsets->saved_args;
11069
11070         case ARM_HARD_FRAME_POINTER_REGNUM:
11071           /* If there is no stack frame then the hard
11072              frame pointer and the arg pointer coincide.  */
11073           if (offsets->frame == offsets->saved_regs)
11074             return 0;
11075           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
11076           return (frame_pointer_needed
11077                   && cfun->static_chain_decl != NULL
11078                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
11079
11080         case STACK_POINTER_REGNUM:
11081           /* If nothing has been pushed on the stack at all
11082              then this will return -4.  This *is* correct!  */
11083           return offsets->outgoing_args - (offsets->saved_args + 4);
11084
11085         default:
11086           gcc_unreachable ();
11087         }
11088       gcc_unreachable ();
11089
11090     case FRAME_POINTER_REGNUM:
11091       switch (to)
11092         {
11093         case THUMB_HARD_FRAME_POINTER_REGNUM:
11094           return 0;
11095
11096         case ARM_HARD_FRAME_POINTER_REGNUM:
11097           /* The hard frame pointer points to the top entry in the
11098              stack frame.  The soft frame pointer to the bottom entry
11099              in the stack frame.  If there is no stack frame at all,
11100              then they are identical.  */
11101
11102           return offsets->frame - offsets->soft_frame;
11103
11104         case STACK_POINTER_REGNUM:
11105           return offsets->outgoing_args - offsets->soft_frame;
11106
11107         default:
11108           gcc_unreachable ();
11109         }
11110       gcc_unreachable ();
11111
11112     default:
11113       /* You cannot eliminate from the stack pointer.
11114          In theory you could eliminate from the hard frame
11115          pointer to the stack pointer, but this will never
11116          happen, since if a stack frame is not needed the
11117          hard frame pointer will never be used.  */
11118       gcc_unreachable ();
11119     }
11120 }
11121
11122
11123 /* Emit RTL to save coprocessor registers on function entry.  Returns the
11124    number of bytes pushed.  */
11125
11126 static int
11127 arm_save_coproc_regs(void)
11128 {
11129   int saved_size = 0;
11130   unsigned reg;
11131   unsigned start_reg;
11132   rtx insn;
11133
11134   for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
11135     if (regs_ever_live[reg] && ! call_used_regs [reg])
11136       {
11137         insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
11138         insn = gen_rtx_MEM (V2SImode, insn);
11139         insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
11140         RTX_FRAME_RELATED_P (insn) = 1;
11141         saved_size += 8;
11142       }
11143
11144   /* Save any floating point call-saved registers used by this
11145      function.  */
11146   if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
11147     {
11148       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11149         if (regs_ever_live[reg] && !call_used_regs[reg])
11150           {
11151             insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
11152             insn = gen_rtx_MEM (XFmode, insn);
11153             insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
11154             RTX_FRAME_RELATED_P (insn) = 1;
11155             saved_size += 12;
11156           }
11157     }
11158   else
11159     {
11160       start_reg = LAST_FPA_REGNUM;
11161
11162       for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
11163         {
11164           if (regs_ever_live[reg] && !call_used_regs[reg])
11165             {
11166               if (start_reg - reg == 3)
11167                 {
11168                   insn = emit_sfm (reg, 4);
11169                   RTX_FRAME_RELATED_P (insn) = 1;
11170                   saved_size += 48;
11171                   start_reg = reg - 1;
11172                 }
11173             }
11174           else
11175             {
11176               if (start_reg != reg)
11177                 {
11178                   insn = emit_sfm (reg + 1, start_reg - reg);
11179                   RTX_FRAME_RELATED_P (insn) = 1;
11180                   saved_size += (start_reg - reg) * 12;
11181                 }
11182               start_reg = reg - 1;
11183             }
11184         }
11185
11186       if (start_reg != reg)
11187         {
11188           insn = emit_sfm (reg + 1, start_reg - reg);
11189           saved_size += (start_reg - reg) * 12;
11190           RTX_FRAME_RELATED_P (insn) = 1;
11191         }
11192     }
11193   if (TARGET_HARD_FLOAT && TARGET_VFP)
11194     {
11195       start_reg = FIRST_VFP_REGNUM;
11196
11197       for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
11198         {
11199           if ((!regs_ever_live[reg] || call_used_regs[reg])
11200               && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
11201             {
11202               if (start_reg != reg)
11203                 saved_size += vfp_emit_fstmd (start_reg,
11204                                               (reg - start_reg) / 2);
11205               start_reg = reg + 2;
11206             }
11207         }
11208       if (start_reg != reg)
11209         saved_size += vfp_emit_fstmd (start_reg,
11210                                       (reg - start_reg) / 2);
11211     }
11212   return saved_size;
11213 }
11214
11215
11216 /* Set the Thumb frame pointer from the stack pointer.  */
11217
11218 static void
11219 thumb_set_frame_pointer (arm_stack_offsets *offsets)
11220 {
11221   HOST_WIDE_INT amount;
11222   rtx insn, dwarf;
11223
11224   amount = offsets->outgoing_args - offsets->locals_base;
11225   if (amount < 1024)
11226     insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
11227                                   stack_pointer_rtx, GEN_INT (amount)));
11228   else
11229     {
11230       emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
11231       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
11232                                     hard_frame_pointer_rtx,
11233                                     stack_pointer_rtx));
11234       dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
11235                            plus_constant (stack_pointer_rtx, amount));
11236       RTX_FRAME_RELATED_P (dwarf) = 1;
11237       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
11238                                             REG_NOTES (insn));
11239     }
11240
11241   RTX_FRAME_RELATED_P (insn) = 1;
11242 }
11243
11244 /* Generate the prologue instructions for entry into an ARM or Thumb-2
11245    function.  */
11246 void
11247 arm_expand_prologue (void)
11248 {
11249   rtx amount;
11250   rtx insn;
11251   rtx ip_rtx;
11252   unsigned long live_regs_mask;
11253   unsigned long func_type;
11254   int fp_offset = 0;
11255   int saved_pretend_args = 0;
11256   int saved_regs = 0;
11257   unsigned HOST_WIDE_INT args_to_push;
11258   arm_stack_offsets *offsets;
11259
11260   func_type = arm_current_func_type ();
11261
11262   /* Naked functions don't have prologues.  */
11263   if (IS_NAKED (func_type))
11264     return;
11265
11266   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
11267   args_to_push = current_function_pretend_args_size;
11268
11269   /* Compute which register we will have to save onto the stack.  */
11270   live_regs_mask = arm_compute_save_reg_mask ();
11271
11272   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
11273
11274   if (IS_STACKALIGN (func_type))
11275     {
11276       rtx dwarf;
11277       rtx r0;
11278       rtx r1;
11279       /* Handle a word-aligned stack pointer.  We generate the following:
11280
11281           mov r0, sp
11282           bic r1, r0, #7
11283           mov sp, r1
11284           <save and restore r0 in normal prologue/epilogue>
11285           mov sp, r0
11286           bx lr
11287
11288          The unwinder doesn't need to know about the stack realignment.
11289          Just tell it we saved SP in r0.  */
11290       gcc_assert (TARGET_THUMB2 && !arm_arch_notm && args_to_push == 0);
11291
11292       r0 = gen_rtx_REG (SImode, 0);
11293       r1 = gen_rtx_REG (SImode, 1);
11294       dwarf = gen_rtx_UNSPEC (SImode, NULL_RTVEC, UNSPEC_STACK_ALIGN);
11295       dwarf = gen_rtx_SET (VOIDmode, r0, dwarf);
11296       insn = gen_movsi (r0, stack_pointer_rtx);
11297       RTX_FRAME_RELATED_P (insn) = 1;
11298       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11299                                             dwarf, REG_NOTES (insn));
11300       emit_insn (insn);
11301       emit_insn (gen_andsi3 (r1, r0, GEN_INT (~(HOST_WIDE_INT)7)));
11302       emit_insn (gen_movsi (stack_pointer_rtx, r1));
11303     }
11304
11305   if (frame_pointer_needed && TARGET_ARM)
11306     {
11307       if (IS_INTERRUPT (func_type))
11308         {
11309           /* Interrupt functions must not corrupt any registers.
11310              Creating a frame pointer however, corrupts the IP
11311              register, so we must push it first.  */
11312           insn = emit_multi_reg_push (1 << IP_REGNUM);
11313
11314           /* Do not set RTX_FRAME_RELATED_P on this insn.
11315              The dwarf stack unwinding code only wants to see one
11316              stack decrement per function, and this is not it.  If
11317              this instruction is labeled as being part of the frame
11318              creation sequence then dwarf2out_frame_debug_expr will
11319              die when it encounters the assignment of IP to FP
11320              later on, since the use of SP here establishes SP as
11321              the CFA register and not IP.
11322
11323              Anyway this instruction is not really part of the stack
11324              frame creation although it is part of the prologue.  */
11325         }
11326       else if (IS_NESTED (func_type))
11327         {
11328           /* The Static chain register is the same as the IP register
11329              used as a scratch register during stack frame creation.
11330              To get around this need to find somewhere to store IP
11331              whilst the frame is being created.  We try the following
11332              places in order:
11333
11334                1. The last argument register.
11335                2. A slot on the stack above the frame.  (This only
11336                   works if the function is not a varargs function).
11337                3. Register r3, after pushing the argument registers
11338                   onto the stack.
11339
11340              Note - we only need to tell the dwarf2 backend about the SP
11341              adjustment in the second variant; the static chain register
11342              doesn't need to be unwound, as it doesn't contain a value
11343              inherited from the caller.  */
11344
11345           if (regs_ever_live[3] == 0)
11346             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
11347           else if (args_to_push == 0)
11348             {
11349               rtx dwarf;
11350
11351               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
11352               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
11353               fp_offset = 4;
11354
11355               /* Just tell the dwarf backend that we adjusted SP.  */
11356               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
11357                                    plus_constant (stack_pointer_rtx,
11358                                                   -fp_offset));
11359               RTX_FRAME_RELATED_P (insn) = 1;
11360               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
11361                                                     dwarf, REG_NOTES (insn));
11362             }
11363           else
11364             {
11365               /* Store the args on the stack.  */
11366               if (cfun->machine->uses_anonymous_args)
11367                 insn = emit_multi_reg_push
11368                   ((0xf0 >> (args_to_push / 4)) & 0xf);
11369               else
11370                 insn = emit_insn
11371                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11372                                GEN_INT (- args_to_push)));
11373
11374               RTX_FRAME_RELATED_P (insn) = 1;
11375
11376               saved_pretend_args = 1;
11377               fp_offset = args_to_push;
11378               args_to_push = 0;
11379
11380               /* Now reuse r3 to preserve IP.  */
11381               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
11382             }
11383         }
11384
11385       insn = emit_set_insn (ip_rtx,
11386                             plus_constant (stack_pointer_rtx, fp_offset));
11387       RTX_FRAME_RELATED_P (insn) = 1;
11388     }
11389
11390   if (args_to_push)
11391     {
11392       /* Push the argument registers, or reserve space for them.  */
11393       if (cfun->machine->uses_anonymous_args)
11394         insn = emit_multi_reg_push
11395           ((0xf0 >> (args_to_push / 4)) & 0xf);
11396       else
11397         insn = emit_insn
11398           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11399                        GEN_INT (- args_to_push)));
11400       RTX_FRAME_RELATED_P (insn) = 1;
11401     }
11402
11403   /* If this is an interrupt service routine, and the link register
11404      is going to be pushed, and we are not creating a stack frame,
11405      (which would involve an extra push of IP and a pop in the epilogue)
11406      subtracting four from LR now will mean that the function return
11407      can be done with a single instruction.  */
11408   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
11409       && (live_regs_mask & (1 << LR_REGNUM)) != 0
11410       && ! frame_pointer_needed
11411       && TARGET_ARM)
11412     {
11413       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
11414       
11415       emit_set_insn (lr, plus_constant (lr, -4));
11416     }
11417
11418   if (live_regs_mask)
11419     {
11420       insn = emit_multi_reg_push (live_regs_mask);
11421       saved_regs += bit_count (live_regs_mask) * 4;
11422       RTX_FRAME_RELATED_P (insn) = 1;
11423     }
11424
11425   if (! IS_VOLATILE (func_type))
11426     saved_regs += arm_save_coproc_regs ();
11427
11428   if (frame_pointer_needed && TARGET_ARM)
11429     {
11430       /* Create the new frame pointer.  */
11431         {
11432           insn = GEN_INT (-(4 + args_to_push + fp_offset));
11433           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
11434           RTX_FRAME_RELATED_P (insn) = 1;
11435
11436           if (IS_NESTED (func_type))
11437             {
11438               /* Recover the static chain register.  */
11439               if (regs_ever_live [3] == 0
11440                   || saved_pretend_args)
11441                 insn = gen_rtx_REG (SImode, 3);
11442               else /* if (current_function_pretend_args_size == 0) */
11443                 {
11444                   insn = plus_constant (hard_frame_pointer_rtx, 4);
11445                   insn = gen_frame_mem (SImode, insn);
11446                 }
11447               emit_set_insn (ip_rtx, insn);
11448               /* Add a USE to stop propagate_one_insn() from barfing.  */
11449               emit_insn (gen_prologue_use (ip_rtx));
11450             }
11451         }
11452     }
11453
11454   offsets = arm_get_frame_offsets ();
11455   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
11456     {
11457       /* This add can produce multiple insns for a large constant, so we
11458          need to get tricky.  */
11459       rtx last = get_last_insn ();
11460
11461       amount = GEN_INT (offsets->saved_args + saved_regs
11462                         - offsets->outgoing_args);
11463
11464       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
11465                                     amount));
11466       do
11467         {
11468           last = last ? NEXT_INSN (last) : get_insns ();
11469           RTX_FRAME_RELATED_P (last) = 1;
11470         }
11471       while (last != insn);
11472
11473       /* If the frame pointer is needed, emit a special barrier that
11474          will prevent the scheduler from moving stores to the frame
11475          before the stack adjustment.  */
11476       if (frame_pointer_needed)
11477         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
11478                                          hard_frame_pointer_rtx));
11479     }
11480
11481
11482   if (frame_pointer_needed && TARGET_THUMB2)
11483     thumb_set_frame_pointer (offsets);
11484
11485   if (flag_pic && arm_pic_register != INVALID_REGNUM)
11486     {
11487       unsigned long mask;
11488
11489       mask = live_regs_mask;
11490       mask &= THUMB2_WORK_REGS;
11491       if (!IS_NESTED (func_type))
11492         mask |= (1 << IP_REGNUM);
11493       arm_load_pic_register (mask);
11494     }
11495
11496   /* If we are profiling, make sure no instructions are scheduled before
11497      the call to mcount.  Similarly if the user has requested no
11498      scheduling in the prolog.  Similarly if we want non-call exceptions
11499      using the EABI unwinder, to prevent faulting instructions from being
11500      swapped with a stack adjustment.  */
11501   if (current_function_profile || !TARGET_SCHED_PROLOG
11502       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
11503     emit_insn (gen_blockage ());
11504
11505   /* If the link register is being kept alive, with the return address in it,
11506      then make sure that it does not get reused by the ce2 pass.  */
11507   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
11508     {
11509       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
11510       cfun->machine->lr_save_eliminated = 1;
11511     }
11512 }
11513 \f
11514 /* Print condition code to STREAM.  Helper function for arm_print_operand.  */
11515 static void
11516 arm_print_condition (FILE *stream)
11517 {
11518   if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
11519     {
11520       /* Branch conversion is not implemented for Thumb-2.  */
11521       if (TARGET_THUMB)
11522         {
11523           output_operand_lossage ("predicated Thumb instruction");
11524           return;
11525         }
11526       if (current_insn_predicate != NULL)
11527         {
11528           output_operand_lossage
11529             ("predicated instruction in conditional sequence");
11530           return;
11531         }
11532
11533       fputs (arm_condition_codes[arm_current_cc], stream);
11534     }
11535   else if (current_insn_predicate)
11536     {
11537       enum arm_cond_code code;
11538
11539       if (TARGET_THUMB1)
11540         {
11541           output_operand_lossage ("predicated Thumb instruction");
11542           return;
11543         }
11544
11545       code = get_arm_condition_code (current_insn_predicate);
11546       fputs (arm_condition_codes[code], stream);
11547     }
11548 }
11549
11550
11551 /* If CODE is 'd', then the X is a condition operand and the instruction
11552    should only be executed if the condition is true.
11553    if CODE is 'D', then the X is a condition operand and the instruction
11554    should only be executed if the condition is false: however, if the mode
11555    of the comparison is CCFPEmode, then always execute the instruction -- we
11556    do this because in these circumstances !GE does not necessarily imply LT;
11557    in these cases the instruction pattern will take care to make sure that
11558    an instruction containing %d will follow, thereby undoing the effects of
11559    doing this instruction unconditionally.
11560    If CODE is 'N' then X is a floating point operand that must be negated
11561    before output.
11562    If CODE is 'B' then output a bitwise inverted value of X (a const int).
11563    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
11564 void
11565 arm_print_operand (FILE *stream, rtx x, int code)
11566 {
11567   switch (code)
11568     {
11569     case '@':
11570       fputs (ASM_COMMENT_START, stream);
11571       return;
11572
11573     case '_':
11574       fputs (user_label_prefix, stream);
11575       return;
11576
11577     case '|':
11578       fputs (REGISTER_PREFIX, stream);
11579       return;
11580
11581     case '?':
11582       arm_print_condition (stream);
11583       return;
11584
11585     case '(':
11586       /* Nothing in unified syntax, otherwise the current condition code.  */
11587       if (!TARGET_UNIFIED_ASM)
11588         arm_print_condition (stream);
11589       break;
11590
11591     case ')':
11592       /* The current condition code in unified syntax, otherwise nothing.  */
11593       if (TARGET_UNIFIED_ASM)
11594         arm_print_condition (stream);
11595       break;
11596   
11597     case '.':
11598       /* The current condition code for a condition code setting instruction.
11599          Preceded by 's' in unified syntax, otherwise followed by 's'.  */
11600       if (TARGET_UNIFIED_ASM)
11601         {
11602           fputc('s', stream);
11603           arm_print_condition (stream);
11604         }
11605       else
11606         {
11607           arm_print_condition (stream);
11608           fputc('s', stream);
11609         }
11610       return;
11611
11612     case '!':
11613       /* If the instruction is conditionally executed then print
11614          the current condition code, otherwise print 's'.  */
11615       gcc_assert (TARGET_THUMB2 && TARGET_UNIFIED_ASM);
11616       if (current_insn_predicate)
11617         arm_print_condition (stream);
11618       else
11619         fputc('s', stream);
11620       break;
11621
11622     case 'N':
11623       {
11624         REAL_VALUE_TYPE r;
11625         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
11626         r = REAL_VALUE_NEGATE (r);
11627         fprintf (stream, "%s", fp_const_from_val (&r));
11628       }
11629       return;
11630
11631     case 'B':
11632       if (GET_CODE (x) == CONST_INT)
11633         {
11634           HOST_WIDE_INT val;
11635           val = ARM_SIGN_EXTEND (~INTVAL (x));
11636           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
11637         }
11638       else
11639         {
11640           putc ('~', stream);
11641           output_addr_const (stream, x);
11642         }
11643       return;
11644
11645     case 'L':
11646       /* The low 16 bits of an immediate constant.  */
11647       fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL(x) & 0xffff);
11648       return;
11649
11650     case 'i':
11651       fprintf (stream, "%s", arithmetic_instr (x, 1));
11652       return;
11653
11654     /* Truncate Cirrus shift counts.  */
11655     case 's':
11656       if (GET_CODE (x) == CONST_INT)
11657         {
11658           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
11659           return;
11660         }
11661       arm_print_operand (stream, x, 0);
11662       return;
11663
11664     case 'I':
11665       fprintf (stream, "%s", arithmetic_instr (x, 0));
11666       return;
11667
11668     case 'S':
11669       {
11670         HOST_WIDE_INT val;
11671         const char *shift;
11672
11673         if (!shift_operator (x, SImode))
11674           {
11675             output_operand_lossage ("invalid shift operand");
11676             break;
11677           }
11678
11679         shift = shift_op (x, &val);
11680
11681         if (shift)
11682           {
11683             fprintf (stream, ", %s ", shift);
11684             if (val == -1)
11685               arm_print_operand (stream, XEXP (x, 1), 0);
11686             else
11687               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11688           }
11689       }
11690       return;
11691
11692       /* An explanation of the 'Q', 'R' and 'H' register operands:
11693
11694          In a pair of registers containing a DI or DF value the 'Q'
11695          operand returns the register number of the register containing
11696          the least significant part of the value.  The 'R' operand returns
11697          the register number of the register containing the most
11698          significant part of the value.
11699
11700          The 'H' operand returns the higher of the two register numbers.
11701          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
11702          same as the 'Q' operand, since the most significant part of the
11703          value is held in the lower number register.  The reverse is true
11704          on systems where WORDS_BIG_ENDIAN is false.
11705
11706          The purpose of these operands is to distinguish between cases
11707          where the endian-ness of the values is important (for example
11708          when they are added together), and cases where the endian-ness
11709          is irrelevant, but the order of register operations is important.
11710          For example when loading a value from memory into a register
11711          pair, the endian-ness does not matter.  Provided that the value
11712          from the lower memory address is put into the lower numbered
11713          register, and the value from the higher address is put into the
11714          higher numbered register, the load will work regardless of whether
11715          the value being loaded is big-wordian or little-wordian.  The
11716          order of the two register loads can matter however, if the address
11717          of the memory location is actually held in one of the registers
11718          being overwritten by the load.  */
11719     case 'Q':
11720       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11721         {
11722           output_operand_lossage ("invalid operand for code '%c'", code);
11723           return;
11724         }
11725
11726       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11727       return;
11728
11729     case 'R':
11730       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11731         {
11732           output_operand_lossage ("invalid operand for code '%c'", code);
11733           return;
11734         }
11735
11736       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11737       return;
11738
11739     case 'H':
11740       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11741         {
11742           output_operand_lossage ("invalid operand for code '%c'", code);
11743           return;
11744         }
11745
11746       asm_fprintf (stream, "%r", REGNO (x) + 1);
11747       return;
11748
11749     case 'm':
11750       asm_fprintf (stream, "%r",
11751                    GET_CODE (XEXP (x, 0)) == REG
11752                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11753       return;
11754
11755     case 'M':
11756       asm_fprintf (stream, "{%r-%r}",
11757                    REGNO (x),
11758                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11759       return;
11760
11761     case 'd':
11762       /* CONST_TRUE_RTX means always -- that's the default.  */
11763       if (x == const_true_rtx)
11764         return;
11765
11766       if (!COMPARISON_P (x))
11767         {
11768           output_operand_lossage ("invalid operand for code '%c'", code);
11769           return;
11770         }
11771
11772       fputs (arm_condition_codes[get_arm_condition_code (x)],
11773              stream);
11774       return;
11775
11776     case 'D':
11777       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
11778          want to do that.  */
11779       if (x == const_true_rtx)
11780         {
11781           output_operand_lossage ("instruction never executed");
11782           return;
11783         }
11784       if (!COMPARISON_P (x))
11785         {
11786           output_operand_lossage ("invalid operand for code '%c'", code);
11787           return;
11788         }
11789
11790       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11791                                  (get_arm_condition_code (x))],
11792              stream);
11793       return;
11794
11795     /* Cirrus registers can be accessed in a variety of ways:
11796          single floating point (f)
11797          double floating point (d)
11798          32bit integer         (fx)
11799          64bit integer         (dx).  */
11800     case 'W':                   /* Cirrus register in F mode.  */
11801     case 'X':                   /* Cirrus register in D mode.  */
11802     case 'Y':                   /* Cirrus register in FX mode.  */
11803     case 'Z':                   /* Cirrus register in DX mode.  */
11804       gcc_assert (GET_CODE (x) == REG
11805                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11806
11807       fprintf (stream, "mv%s%s",
11808                code == 'W' ? "f"
11809                : code == 'X' ? "d"
11810                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11811
11812       return;
11813
11814     /* Print cirrus register in the mode specified by the register's mode.  */
11815     case 'V':
11816       {
11817         int mode = GET_MODE (x);
11818
11819         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11820           {
11821             output_operand_lossage ("invalid operand for code '%c'", code);
11822             return;
11823           }
11824
11825         fprintf (stream, "mv%s%s",
11826                  mode == DFmode ? "d"
11827                  : mode == SImode ? "fx"
11828                  : mode == DImode ? "dx"
11829                  : "f", reg_names[REGNO (x)] + 2);
11830
11831         return;
11832       }
11833
11834     case 'U':
11835       if (GET_CODE (x) != REG
11836           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11837           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11838         /* Bad value for wCG register number.  */
11839         {
11840           output_operand_lossage ("invalid operand for code '%c'", code);
11841           return;
11842         }
11843
11844       else
11845         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11846       return;
11847
11848       /* Print an iWMMXt control register name.  */
11849     case 'w':
11850       if (GET_CODE (x) != CONST_INT
11851           || INTVAL (x) < 0
11852           || INTVAL (x) >= 16)
11853         /* Bad value for wC register number.  */
11854         {
11855           output_operand_lossage ("invalid operand for code '%c'", code);
11856           return;
11857         }
11858
11859       else
11860         {
11861           static const char * wc_reg_names [16] =
11862             {
11863               "wCID",  "wCon",  "wCSSF", "wCASF",
11864               "wC4",   "wC5",   "wC6",   "wC7",
11865               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11866               "wC12",  "wC13",  "wC14",  "wC15"
11867             };
11868
11869           fprintf (stream, wc_reg_names [INTVAL (x)]);
11870         }
11871       return;
11872
11873       /* Print a VFP double precision register name.  */
11874     case 'P':
11875       {
11876         int mode = GET_MODE (x);
11877         int num;
11878
11879         if (mode != DImode && mode != DFmode)
11880           {
11881             output_operand_lossage ("invalid operand for code '%c'", code);
11882             return;
11883           }
11884
11885         if (GET_CODE (x) != REG
11886             || !IS_VFP_REGNUM (REGNO (x)))
11887           {
11888             output_operand_lossage ("invalid operand for code '%c'", code);
11889             return;
11890           }
11891
11892         num = REGNO(x) - FIRST_VFP_REGNUM;
11893         if (num & 1)
11894           {
11895             output_operand_lossage ("invalid operand for code '%c'", code);
11896             return;
11897           }
11898
11899         fprintf (stream, "d%d", num >> 1);
11900       }
11901       return;
11902
11903     default:
11904       if (x == 0)
11905         {
11906           output_operand_lossage ("missing operand");
11907           return;
11908         }
11909
11910       switch (GET_CODE (x))
11911         {
11912         case REG:
11913           asm_fprintf (stream, "%r", REGNO (x));
11914           break;
11915
11916         case MEM:
11917           output_memory_reference_mode = GET_MODE (x);
11918           output_address (XEXP (x, 0));
11919           break;
11920
11921         case CONST_DOUBLE:
11922           fprintf (stream, "#%s", fp_immediate_constant (x));
11923           break;
11924
11925         default:
11926           gcc_assert (GET_CODE (x) != NEG);
11927           fputc ('#', stream);
11928           output_addr_const (stream, x);
11929           break;
11930         }
11931     }
11932 }
11933 \f
11934 #ifndef AOF_ASSEMBLER
11935 /* Target hook for assembling integer objects.  The ARM version needs to
11936    handle word-sized values specially.  */
11937 static bool
11938 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11939 {
11940   if (size == UNITS_PER_WORD && aligned_p)
11941     {
11942       fputs ("\t.word\t", asm_out_file);
11943       output_addr_const (asm_out_file, x);
11944
11945       /* Mark symbols as position independent.  We only do this in the
11946          .text segment, not in the .data segment.  */
11947       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11948           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11949         {
11950           if (GET_CODE (x) == SYMBOL_REF
11951               && (CONSTANT_POOL_ADDRESS_P (x)
11952                   || SYMBOL_REF_LOCAL_P (x)))
11953             fputs ("(GOTOFF)", asm_out_file);
11954           else if (GET_CODE (x) == LABEL_REF)
11955             fputs ("(GOTOFF)", asm_out_file);
11956           else
11957             fputs ("(GOT)", asm_out_file);
11958         }
11959       fputc ('\n', asm_out_file);
11960       return true;
11961     }
11962
11963   if (arm_vector_mode_supported_p (GET_MODE (x)))
11964     {
11965       int i, units;
11966
11967       gcc_assert (GET_CODE (x) == CONST_VECTOR);
11968
11969       units = CONST_VECTOR_NUNITS (x);
11970
11971       switch (GET_MODE (x))
11972         {
11973         case V2SImode: size = 4; break;
11974         case V4HImode: size = 2; break;
11975         case V8QImode: size = 1; break;
11976         default:
11977           gcc_unreachable ();
11978         }
11979
11980       for (i = 0; i < units; i++)
11981         {
11982           rtx elt;
11983
11984           elt = CONST_VECTOR_ELT (x, i);
11985           assemble_integer
11986             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11987         }
11988
11989       return true;
11990     }
11991
11992   return default_assemble_integer (x, size, aligned_p);
11993 }
11994
11995 static void
11996 arm_elf_asm_cdtor (rtx symbol, int priority, bool is_ctor)
11997 {
11998   section *s;
11999
12000   if (!TARGET_AAPCS_BASED)
12001     {
12002       (is_ctor ? 
12003        default_named_section_asm_out_constructor 
12004        : default_named_section_asm_out_destructor) (symbol, priority);
12005       return;
12006     }
12007
12008   /* Put these in the .init_array section, using a special relocation.  */
12009   if (priority != DEFAULT_INIT_PRIORITY)
12010     {
12011       char buf[18];
12012       sprintf (buf, "%s.%.5u", 
12013                is_ctor ? ".init_array" : ".fini_array",
12014                priority);
12015       s = get_section (buf, SECTION_WRITE, NULL_TREE);
12016     }
12017   else if (is_ctor)
12018     s = ctors_section;
12019   else
12020     s = dtors_section;
12021
12022   switch_to_section (s);
12023   assemble_align (POINTER_SIZE);
12024   fputs ("\t.word\t", asm_out_file);
12025   output_addr_const (asm_out_file, symbol);
12026   fputs ("(target1)\n", asm_out_file);
12027 }
12028
12029 /* Add a function to the list of static constructors.  */
12030
12031 static void
12032 arm_elf_asm_constructor (rtx symbol, int priority)
12033 {
12034   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/true);
12035 }
12036
12037 /* Add a function to the list of static destructors.  */
12038
12039 static void
12040 arm_elf_asm_destructor (rtx symbol, int priority)
12041 {
12042   arm_elf_asm_cdtor (symbol, priority, /*is_ctor=*/false);
12043 }
12044 #endif
12045 \f
12046 /* A finite state machine takes care of noticing whether or not instructions
12047    can be conditionally executed, and thus decrease execution time and code
12048    size by deleting branch instructions.  The fsm is controlled by
12049    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
12050
12051 /* The state of the fsm controlling condition codes are:
12052    0: normal, do nothing special
12053    1: make ASM_OUTPUT_OPCODE not output this instruction
12054    2: make ASM_OUTPUT_OPCODE not output this instruction
12055    3: make instructions conditional
12056    4: make instructions conditional
12057
12058    State transitions (state->state by whom under condition):
12059    0 -> 1 final_prescan_insn if the `target' is a label
12060    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
12061    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
12062    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
12063    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
12064           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
12065    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
12066           (the target insn is arm_target_insn).
12067
12068    If the jump clobbers the conditions then we use states 2 and 4.
12069
12070    A similar thing can be done with conditional return insns.
12071
12072    XXX In case the `target' is an unconditional branch, this conditionalising
12073    of the instructions always reduces code size, but not always execution
12074    time.  But then, I want to reduce the code size to somewhere near what
12075    /bin/cc produces.  */
12076
12077 /* In addition to this, state is maintained for Thumb-2 COND_EXEC
12078    instructions.  When a COND_EXEC instruction is seen the subsequent
12079    instructions are scanned so that multiple conditional instructions can be
12080    combined into a single IT block.  arm_condexec_count and arm_condexec_mask
12081    specify the length and true/false mask for the IT block.  These will be
12082    decremented/zeroed by arm_asm_output_opcode as the insns are output.  */
12083
12084 /* Returns the index of the ARM condition code string in
12085    `arm_condition_codes'.  COMPARISON should be an rtx like
12086    `(eq (...) (...))'.  */
12087 static enum arm_cond_code
12088 get_arm_condition_code (rtx comparison)
12089 {
12090   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
12091   int code;
12092   enum rtx_code comp_code = GET_CODE (comparison);
12093
12094   if (GET_MODE_CLASS (mode) != MODE_CC)
12095     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
12096                            XEXP (comparison, 1));
12097
12098   switch (mode)
12099     {
12100     case CC_DNEmode: code = ARM_NE; goto dominance;
12101     case CC_DEQmode: code = ARM_EQ; goto dominance;
12102     case CC_DGEmode: code = ARM_GE; goto dominance;
12103     case CC_DGTmode: code = ARM_GT; goto dominance;
12104     case CC_DLEmode: code = ARM_LE; goto dominance;
12105     case CC_DLTmode: code = ARM_LT; goto dominance;
12106     case CC_DGEUmode: code = ARM_CS; goto dominance;
12107     case CC_DGTUmode: code = ARM_HI; goto dominance;
12108     case CC_DLEUmode: code = ARM_LS; goto dominance;
12109     case CC_DLTUmode: code = ARM_CC;
12110
12111     dominance:
12112       gcc_assert (comp_code == EQ || comp_code == NE);
12113
12114       if (comp_code == EQ)
12115         return ARM_INVERSE_CONDITION_CODE (code);
12116       return code;
12117
12118     case CC_NOOVmode:
12119       switch (comp_code)
12120         {
12121         case NE: return ARM_NE;
12122         case EQ: return ARM_EQ;
12123         case GE: return ARM_PL;
12124         case LT: return ARM_MI;
12125         default: gcc_unreachable ();
12126         }
12127
12128     case CC_Zmode:
12129       switch (comp_code)
12130         {
12131         case NE: return ARM_NE;
12132         case EQ: return ARM_EQ;
12133         default: gcc_unreachable ();
12134         }
12135
12136     case CC_Nmode:
12137       switch (comp_code)
12138         {
12139         case NE: return ARM_MI;
12140         case EQ: return ARM_PL;
12141         default: gcc_unreachable ();
12142         }
12143
12144     case CCFPEmode:
12145     case CCFPmode:
12146       /* These encodings assume that AC=1 in the FPA system control
12147          byte.  This allows us to handle all cases except UNEQ and
12148          LTGT.  */
12149       switch (comp_code)
12150         {
12151         case GE: return ARM_GE;
12152         case GT: return ARM_GT;
12153         case LE: return ARM_LS;
12154         case LT: return ARM_MI;
12155         case NE: return ARM_NE;
12156         case EQ: return ARM_EQ;
12157         case ORDERED: return ARM_VC;
12158         case UNORDERED: return ARM_VS;
12159         case UNLT: return ARM_LT;
12160         case UNLE: return ARM_LE;
12161         case UNGT: return ARM_HI;
12162         case UNGE: return ARM_PL;
12163           /* UNEQ and LTGT do not have a representation.  */
12164         case UNEQ: /* Fall through.  */
12165         case LTGT: /* Fall through.  */
12166         default: gcc_unreachable ();
12167         }
12168
12169     case CC_SWPmode:
12170       switch (comp_code)
12171         {
12172         case NE: return ARM_NE;
12173         case EQ: return ARM_EQ;
12174         case GE: return ARM_LE;
12175         case GT: return ARM_LT;
12176         case LE: return ARM_GE;
12177         case LT: return ARM_GT;
12178         case GEU: return ARM_LS;
12179         case GTU: return ARM_CC;
12180         case LEU: return ARM_CS;
12181         case LTU: return ARM_HI;
12182         default: gcc_unreachable ();
12183         }
12184
12185     case CC_Cmode:
12186       switch (comp_code)
12187       {
12188       case LTU: return ARM_CS;
12189       case GEU: return ARM_CC;
12190       default: gcc_unreachable ();
12191       }
12192
12193     case CCmode:
12194       switch (comp_code)
12195         {
12196         case NE: return ARM_NE;
12197         case EQ: return ARM_EQ;
12198         case GE: return ARM_GE;
12199         case GT: return ARM_GT;
12200         case LE: return ARM_LE;
12201         case LT: return ARM_LT;
12202         case GEU: return ARM_CS;
12203         case GTU: return ARM_HI;
12204         case LEU: return ARM_LS;
12205         case LTU: return ARM_CC;
12206         default: gcc_unreachable ();
12207         }
12208
12209     default: gcc_unreachable ();
12210     }
12211 }
12212
12213 /* Tell arm_asm_output_opcode to output IT blocks for conditionally executed
12214    instructions.  */
12215 void
12216 thumb2_final_prescan_insn (rtx insn)
12217 {
12218   rtx first_insn = insn;
12219   rtx body = PATTERN (insn);
12220   rtx predicate;
12221   enum arm_cond_code code;
12222   int n;
12223   int mask;
12224
12225   /* Remove the previous insn from the count of insns to be output.  */
12226   if (arm_condexec_count)
12227       arm_condexec_count--;
12228
12229   /* Nothing to do if we are already inside a conditional block.  */
12230   if (arm_condexec_count)
12231     return;
12232
12233   if (GET_CODE (body) != COND_EXEC)
12234     return;
12235
12236   /* Conditional jumps are implemented directly.  */
12237   if (GET_CODE (insn) == JUMP_INSN)
12238     return;
12239
12240   predicate = COND_EXEC_TEST (body);
12241   arm_current_cc = get_arm_condition_code (predicate);
12242
12243   n = get_attr_ce_count (insn);
12244   arm_condexec_count = 1;
12245   arm_condexec_mask = (1 << n) - 1;
12246   arm_condexec_masklen = n;
12247   /* See if subsequent instructions can be combined into the same block.  */
12248   for (;;)
12249     {
12250       insn = next_nonnote_insn (insn);
12251
12252       /* Jumping into the middle of an IT block is illegal, so a label or
12253          barrier terminates the block.  */
12254       if (GET_CODE (insn) != INSN && GET_CODE(insn) != JUMP_INSN)
12255         break;
12256
12257       body = PATTERN (insn);
12258       /* USE and CLOBBER aren't really insns, so just skip them.  */
12259       if (GET_CODE (body) == USE
12260           || GET_CODE (body) == CLOBBER)
12261         continue;
12262
12263       /* ??? Recognize conditional jumps, and combine them with IT blocks.  */
12264       if (GET_CODE (body) != COND_EXEC)
12265         break;
12266       /* Allow up to 4 conditionally executed instructions in a block.  */
12267       n = get_attr_ce_count (insn);
12268       if (arm_condexec_masklen + n > 4)
12269         break;
12270
12271       predicate = COND_EXEC_TEST (body);
12272       code = get_arm_condition_code (predicate);
12273       mask = (1 << n) - 1;
12274       if (arm_current_cc == code)
12275         arm_condexec_mask |= (mask << arm_condexec_masklen);
12276       else if (arm_current_cc != ARM_INVERSE_CONDITION_CODE(code))
12277         break;
12278
12279       arm_condexec_count++;
12280       arm_condexec_masklen += n;
12281
12282       /* A jump must be the last instruction in a conditional block.  */
12283       if (GET_CODE(insn) == JUMP_INSN)
12284         break;
12285     }
12286   /* Restore recog_data (getting the attributes of other insns can
12287      destroy this array, but final.c assumes that it remains intact
12288      across this call).  */
12289   extract_constrain_insn_cached (first_insn);
12290 }
12291
12292 void
12293 arm_final_prescan_insn (rtx insn)
12294 {
12295   /* BODY will hold the body of INSN.  */
12296   rtx body = PATTERN (insn);
12297
12298   /* This will be 1 if trying to repeat the trick, and things need to be
12299      reversed if it appears to fail.  */
12300   int reverse = 0;
12301
12302   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
12303      taken are clobbered, even if the rtl suggests otherwise.  It also
12304      means that we have to grub around within the jump expression to find
12305      out what the conditions are when the jump isn't taken.  */
12306   int jump_clobbers = 0;
12307
12308   /* If we start with a return insn, we only succeed if we find another one.  */
12309   int seeking_return = 0;
12310
12311   /* START_INSN will hold the insn from where we start looking.  This is the
12312      first insn after the following code_label if REVERSE is true.  */
12313   rtx start_insn = insn;
12314
12315   /* If in state 4, check if the target branch is reached, in order to
12316      change back to state 0.  */
12317   if (arm_ccfsm_state == 4)
12318     {
12319       if (insn == arm_target_insn)
12320         {
12321           arm_target_insn = NULL;
12322           arm_ccfsm_state = 0;
12323         }
12324       return;
12325     }
12326
12327   /* If in state 3, it is possible to repeat the trick, if this insn is an
12328      unconditional branch to a label, and immediately following this branch
12329      is the previous target label which is only used once, and the label this
12330      branch jumps to is not too far off.  */
12331   if (arm_ccfsm_state == 3)
12332     {
12333       if (simplejump_p (insn))
12334         {
12335           start_insn = next_nonnote_insn (start_insn);
12336           if (GET_CODE (start_insn) == BARRIER)
12337             {
12338               /* XXX Isn't this always a barrier?  */
12339               start_insn = next_nonnote_insn (start_insn);
12340             }
12341           if (GET_CODE (start_insn) == CODE_LABEL
12342               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
12343               && LABEL_NUSES (start_insn) == 1)
12344             reverse = TRUE;
12345           else
12346             return;
12347         }
12348       else if (GET_CODE (body) == RETURN)
12349         {
12350           start_insn = next_nonnote_insn (start_insn);
12351           if (GET_CODE (start_insn) == BARRIER)
12352             start_insn = next_nonnote_insn (start_insn);
12353           if (GET_CODE (start_insn) == CODE_LABEL
12354               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
12355               && LABEL_NUSES (start_insn) == 1)
12356             {
12357               reverse = TRUE;
12358               seeking_return = 1;
12359             }
12360           else
12361             return;
12362         }
12363       else
12364         return;
12365     }
12366
12367   gcc_assert (!arm_ccfsm_state || reverse);
12368   if (GET_CODE (insn) != JUMP_INSN)
12369     return;
12370
12371   /* This jump might be paralleled with a clobber of the condition codes
12372      the jump should always come first */
12373   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
12374     body = XVECEXP (body, 0, 0);
12375
12376   if (reverse
12377       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
12378           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
12379     {
12380       int insns_skipped;
12381       int fail = FALSE, succeed = FALSE;
12382       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
12383       int then_not_else = TRUE;
12384       rtx this_insn = start_insn, label = 0;
12385
12386       /* If the jump cannot be done with one instruction, we cannot
12387          conditionally execute the instruction in the inverse case.  */
12388       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
12389         {
12390           jump_clobbers = 1;
12391           return;
12392         }
12393
12394       /* Register the insn jumped to.  */
12395       if (reverse)
12396         {
12397           if (!seeking_return)
12398             label = XEXP (SET_SRC (body), 0);
12399         }
12400       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
12401         label = XEXP (XEXP (SET_SRC (body), 1), 0);
12402       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
12403         {
12404           label = XEXP (XEXP (SET_SRC (body), 2), 0);
12405           then_not_else = FALSE;
12406         }
12407       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
12408         seeking_return = 1;
12409       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
12410         {
12411           seeking_return = 1;
12412           then_not_else = FALSE;
12413         }
12414       else
12415         gcc_unreachable ();
12416
12417       /* See how many insns this branch skips, and what kind of insns.  If all
12418          insns are okay, and the label or unconditional branch to the same
12419          label is not too far away, succeed.  */
12420       for (insns_skipped = 0;
12421            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
12422         {
12423           rtx scanbody;
12424
12425           this_insn = next_nonnote_insn (this_insn);
12426           if (!this_insn)
12427             break;
12428
12429           switch (GET_CODE (this_insn))
12430             {
12431             case CODE_LABEL:
12432               /* Succeed if it is the target label, otherwise fail since
12433                  control falls in from somewhere else.  */
12434               if (this_insn == label)
12435                 {
12436                   if (jump_clobbers)
12437                     {
12438                       arm_ccfsm_state = 2;
12439                       this_insn = next_nonnote_insn (this_insn);
12440                     }
12441                   else
12442                     arm_ccfsm_state = 1;
12443                   succeed = TRUE;
12444                 }
12445               else
12446                 fail = TRUE;
12447               break;
12448
12449             case BARRIER:
12450               /* Succeed if the following insn is the target label.
12451                  Otherwise fail.
12452                  If return insns are used then the last insn in a function
12453                  will be a barrier.  */
12454               this_insn = next_nonnote_insn (this_insn);
12455               if (this_insn && this_insn == label)
12456                 {
12457                   if (jump_clobbers)
12458                     {
12459                       arm_ccfsm_state = 2;
12460                       this_insn = next_nonnote_insn (this_insn);
12461                     }
12462                   else
12463                     arm_ccfsm_state = 1;
12464                   succeed = TRUE;
12465                 }
12466               else
12467                 fail = TRUE;
12468               break;
12469
12470             case CALL_INSN:
12471               /* The AAPCS says that conditional calls should not be
12472                  used since they make interworking inefficient (the
12473                  linker can't transform BL<cond> into BLX).  That's
12474                  only a problem if the machine has BLX.  */
12475               if (arm_arch5)
12476                 {
12477                   fail = TRUE;
12478                   break;
12479                 }
12480
12481               /* Succeed if the following insn is the target label, or
12482                  if the following two insns are a barrier and the
12483                  target label.  */
12484               this_insn = next_nonnote_insn (this_insn);
12485               if (this_insn && GET_CODE (this_insn) == BARRIER)
12486                 this_insn = next_nonnote_insn (this_insn);
12487
12488               if (this_insn && this_insn == label
12489                   && insns_skipped < max_insns_skipped)
12490                 {
12491                   if (jump_clobbers)
12492                     {
12493                       arm_ccfsm_state = 2;
12494                       this_insn = next_nonnote_insn (this_insn);
12495                     }
12496                   else
12497                     arm_ccfsm_state = 1;
12498                   succeed = TRUE;
12499                 }
12500               else
12501                 fail = TRUE;
12502               break;
12503
12504             case JUMP_INSN:
12505               /* If this is an unconditional branch to the same label, succeed.
12506                  If it is to another label, do nothing.  If it is conditional,
12507                  fail.  */
12508               /* XXX Probably, the tests for SET and the PC are
12509                  unnecessary.  */
12510
12511               scanbody = PATTERN (this_insn);
12512               if (GET_CODE (scanbody) == SET
12513                   && GET_CODE (SET_DEST (scanbody)) == PC)
12514                 {
12515                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
12516                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
12517                     {
12518                       arm_ccfsm_state = 2;
12519                       succeed = TRUE;
12520                     }
12521                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
12522                     fail = TRUE;
12523                 }
12524               /* Fail if a conditional return is undesirable (e.g. on a
12525                  StrongARM), but still allow this if optimizing for size.  */
12526               else if (GET_CODE (scanbody) == RETURN
12527                        && !use_return_insn (TRUE, NULL)
12528                        && !optimize_size)
12529                 fail = TRUE;
12530               else if (GET_CODE (scanbody) == RETURN
12531                        && seeking_return)
12532                 {
12533                   arm_ccfsm_state = 2;
12534                   succeed = TRUE;
12535                 }
12536               else if (GET_CODE (scanbody) == PARALLEL)
12537                 {
12538                   switch (get_attr_conds (this_insn))
12539                     {
12540                     case CONDS_NOCOND:
12541                       break;
12542                     default:
12543                       fail = TRUE;
12544                       break;
12545                     }
12546                 }
12547               else
12548                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
12549
12550               break;
12551
12552             case INSN:
12553               /* Instructions using or affecting the condition codes make it
12554                  fail.  */
12555               scanbody = PATTERN (this_insn);
12556               if (!(GET_CODE (scanbody) == SET
12557                     || GET_CODE (scanbody) == PARALLEL)
12558                   || get_attr_conds (this_insn) != CONDS_NOCOND)
12559                 fail = TRUE;
12560
12561               /* A conditional cirrus instruction must be followed by
12562                  a non Cirrus instruction.  However, since we
12563                  conditionalize instructions in this function and by
12564                  the time we get here we can't add instructions
12565                  (nops), because shorten_branches() has already been
12566                  called, we will disable conditionalizing Cirrus
12567                  instructions to be safe.  */
12568               if (GET_CODE (scanbody) != USE
12569                   && GET_CODE (scanbody) != CLOBBER
12570                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
12571                 fail = TRUE;
12572               break;
12573
12574             default:
12575               break;
12576             }
12577         }
12578       if (succeed)
12579         {
12580           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
12581             arm_target_label = CODE_LABEL_NUMBER (label);
12582           else
12583             {
12584               gcc_assert (seeking_return || arm_ccfsm_state == 2);
12585
12586               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
12587                 {
12588                   this_insn = next_nonnote_insn (this_insn);
12589                   gcc_assert (!this_insn
12590                               || (GET_CODE (this_insn) != BARRIER
12591                                   && GET_CODE (this_insn) != CODE_LABEL));
12592                 }
12593               if (!this_insn)
12594                 {
12595                   /* Oh, dear! we ran off the end.. give up.  */
12596                   extract_constrain_insn_cached (insn);
12597                   arm_ccfsm_state = 0;
12598                   arm_target_insn = NULL;
12599                   return;
12600                 }
12601               arm_target_insn = this_insn;
12602             }
12603           if (jump_clobbers)
12604             {
12605               gcc_assert (!reverse);
12606               arm_current_cc =
12607                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
12608                                                             0), 0), 1));
12609               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
12610                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12611               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
12612                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12613             }
12614           else
12615             {
12616               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
12617                  what it was.  */
12618               if (!reverse)
12619                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
12620                                                                0));
12621             }
12622
12623           if (reverse || then_not_else)
12624             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
12625         }
12626
12627       /* Restore recog_data (getting the attributes of other insns can
12628          destroy this array, but final.c assumes that it remains intact
12629          across this call.  */
12630       extract_constrain_insn_cached (insn);
12631     }
12632 }
12633
12634 /* Output IT instructions.  */
12635 void
12636 thumb2_asm_output_opcode (FILE * stream)
12637 {
12638   char buff[5];
12639   int n;
12640
12641   if (arm_condexec_mask)
12642     {
12643       for (n = 0; n < arm_condexec_masklen; n++)
12644         buff[n] = (arm_condexec_mask & (1 << n)) ? 't' : 'e';
12645       buff[n] = 0;
12646       asm_fprintf(stream, "i%s\t%s\n\t", buff,
12647                   arm_condition_codes[arm_current_cc]);
12648       arm_condexec_mask = 0;
12649     }
12650 }
12651
12652 /* Returns true if REGNO is a valid register
12653    for holding a quantity of type MODE.  */
12654 int
12655 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
12656 {
12657   if (GET_MODE_CLASS (mode) == MODE_CC)
12658     return (regno == CC_REGNUM
12659             || (TARGET_HARD_FLOAT && TARGET_VFP
12660                 && regno == VFPCC_REGNUM));
12661
12662   if (TARGET_THUMB1)
12663     /* For the Thumb we only allow values bigger than SImode in
12664        registers 0 - 6, so that there is always a second low
12665        register available to hold the upper part of the value.
12666        We probably we ought to ensure that the register is the
12667        start of an even numbered register pair.  */
12668     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
12669
12670   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
12671       && IS_CIRRUS_REGNUM (regno))
12672     /* We have outlawed SI values in Cirrus registers because they
12673        reside in the lower 32 bits, but SF values reside in the
12674        upper 32 bits.  This causes gcc all sorts of grief.  We can't
12675        even split the registers into pairs because Cirrus SI values
12676        get sign extended to 64bits-- aldyh.  */
12677     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
12678
12679   if (TARGET_HARD_FLOAT && TARGET_VFP
12680       && IS_VFP_REGNUM (regno))
12681     {
12682       if (mode == SFmode || mode == SImode)
12683         return TRUE;
12684
12685       /* DFmode values are only valid in even register pairs.  */
12686       if (mode == DFmode)
12687         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
12688       return FALSE;
12689     }
12690
12691   if (TARGET_REALLY_IWMMXT)
12692     {
12693       if (IS_IWMMXT_GR_REGNUM (regno))
12694         return mode == SImode;
12695
12696       if (IS_IWMMXT_REGNUM (regno))
12697         return VALID_IWMMXT_REG_MODE (mode);
12698     }
12699   
12700   /* We allow any value to be stored in the general registers.
12701      Restrict doubleword quantities to even register pairs so that we can
12702      use ldrd.  */
12703   if (regno <= LAST_ARM_REGNUM)
12704     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
12705
12706   if (regno == FRAME_POINTER_REGNUM
12707       || regno == ARG_POINTER_REGNUM)
12708     /* We only allow integers in the fake hard registers.  */
12709     return GET_MODE_CLASS (mode) == MODE_INT;
12710
12711   /* The only registers left are the FPA registers
12712      which we only allow to hold FP values.  */
12713   return (TARGET_HARD_FLOAT && TARGET_FPA
12714           && GET_MODE_CLASS (mode) == MODE_FLOAT
12715           && regno >= FIRST_FPA_REGNUM
12716           && regno <= LAST_FPA_REGNUM);
12717 }
12718
12719 /* For efficiency and historical reasons LO_REGS, HI_REGS and CC_REGS are
12720    not used in arm mode.  */
12721 int
12722 arm_regno_class (int regno)
12723 {
12724   if (TARGET_THUMB1)
12725     {
12726       if (regno == STACK_POINTER_REGNUM)
12727         return STACK_REG;
12728       if (regno == CC_REGNUM)
12729         return CC_REG;
12730       if (regno < 8)
12731         return LO_REGS;
12732       return HI_REGS;
12733     }
12734
12735   if (TARGET_THUMB2 && regno < 8)
12736     return LO_REGS;
12737
12738   if (   regno <= LAST_ARM_REGNUM
12739       || regno == FRAME_POINTER_REGNUM
12740       || regno == ARG_POINTER_REGNUM)
12741     return TARGET_THUMB2 ? HI_REGS : GENERAL_REGS;
12742
12743   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
12744     return TARGET_THUMB2 ? CC_REG : NO_REGS;
12745
12746   if (IS_CIRRUS_REGNUM (regno))
12747     return CIRRUS_REGS;
12748
12749   if (IS_VFP_REGNUM (regno))
12750     return VFP_REGS;
12751
12752   if (IS_IWMMXT_REGNUM (regno))
12753     return IWMMXT_REGS;
12754
12755   if (IS_IWMMXT_GR_REGNUM (regno))
12756     return IWMMXT_GR_REGS;
12757
12758   return FPA_REGS;
12759 }
12760
12761 /* Handle a special case when computing the offset
12762    of an argument from the frame pointer.  */
12763 int
12764 arm_debugger_arg_offset (int value, rtx addr)
12765 {
12766   rtx insn;
12767
12768   /* We are only interested if dbxout_parms() failed to compute the offset.  */
12769   if (value != 0)
12770     return 0;
12771
12772   /* We can only cope with the case where the address is held in a register.  */
12773   if (GET_CODE (addr) != REG)
12774     return 0;
12775
12776   /* If we are using the frame pointer to point at the argument, then
12777      an offset of 0 is correct.  */
12778   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
12779     return 0;
12780
12781   /* If we are using the stack pointer to point at the
12782      argument, then an offset of 0 is correct.  */
12783   /* ??? Check this is consistent with thumb2 frame layout.  */
12784   if ((TARGET_THUMB || !frame_pointer_needed)
12785       && REGNO (addr) == SP_REGNUM)
12786     return 0;
12787
12788   /* Oh dear.  The argument is pointed to by a register rather
12789      than being held in a register, or being stored at a known
12790      offset from the frame pointer.  Since GDB only understands
12791      those two kinds of argument we must translate the address
12792      held in the register into an offset from the frame pointer.
12793      We do this by searching through the insns for the function
12794      looking to see where this register gets its value.  If the
12795      register is initialized from the frame pointer plus an offset
12796      then we are in luck and we can continue, otherwise we give up.
12797
12798      This code is exercised by producing debugging information
12799      for a function with arguments like this:
12800
12801            double func (double a, double b, int c, double d) {return d;}
12802
12803      Without this code the stab for parameter 'd' will be set to
12804      an offset of 0 from the frame pointer, rather than 8.  */
12805
12806   /* The if() statement says:
12807
12808      If the insn is a normal instruction
12809      and if the insn is setting the value in a register
12810      and if the register being set is the register holding the address of the argument
12811      and if the address is computing by an addition
12812      that involves adding to a register
12813      which is the frame pointer
12814      a constant integer
12815
12816      then...  */
12817
12818   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12819     {
12820       if (   GET_CODE (insn) == INSN
12821           && GET_CODE (PATTERN (insn)) == SET
12822           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
12823           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
12824           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
12825           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
12826           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
12827              )
12828         {
12829           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
12830
12831           break;
12832         }
12833     }
12834
12835   if (value == 0)
12836     {
12837       debug_rtx (addr);
12838       warning (0, "unable to compute real location of stacked parameter");
12839       value = 8; /* XXX magic hack */
12840     }
12841
12842   return value;
12843 }
12844 \f
12845 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
12846   do                                                                    \
12847     {                                                                   \
12848       if ((MASK) & insn_flags)                                          \
12849         add_builtin_function ((NAME), (TYPE), (CODE),                   \
12850                              BUILT_IN_MD, NULL, NULL_TREE);             \
12851     }                                                                   \
12852   while (0)
12853
12854 struct builtin_description
12855 {
12856   const unsigned int       mask;
12857   const enum insn_code     icode;
12858   const char * const       name;
12859   const enum arm_builtins  code;
12860   const enum rtx_code      comparison;
12861   const unsigned int       flag;
12862 };
12863
12864 static const struct builtin_description bdesc_2arg[] =
12865 {
12866 #define IWMMXT_BUILTIN(code, string, builtin) \
12867   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12868     ARM_BUILTIN_##builtin, 0, 0 },
12869
12870   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12871   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12872   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12873   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12874   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12875   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12876   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12877   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12878   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12879   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12880   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12881   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12882   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12883   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12884   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12885   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12886   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12887   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12888   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12889   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12890   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12891   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12892   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12893   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12894   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12895   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12896   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12897   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12898   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12899   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12900   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12901   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12902   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12903   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12904   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12905   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12906   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12907   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12908   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12909   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12910   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12911   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12912   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12913   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12914   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12915   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12916   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12917   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12918   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12919   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12920   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12921   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12922   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12923   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12924   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12925   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12926   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12927   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12928
12929 #define IWMMXT_BUILTIN2(code, builtin) \
12930   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12931
12932   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12933   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12934   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12935   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12936   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12937   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12938   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
12939   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
12940   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
12941   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
12942   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
12943   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
12944   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
12945   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
12946   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
12947   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
12948   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
12949   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
12950   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
12951   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
12952   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
12953   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
12954   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
12955   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
12956   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
12957   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
12958   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
12959   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
12960   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
12961   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
12962   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
12963   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
12964 };
12965
12966 static const struct builtin_description bdesc_1arg[] =
12967 {
12968   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12969   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12970   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12971   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12972   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12973   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12974   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12975   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12976   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12977   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12978   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12979   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12980   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12981   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12982   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12983   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12984   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12985   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12986 };
12987
12988 /* Set up all the iWMMXt builtins.  This is
12989    not called if TARGET_IWMMXT is zero.  */
12990
12991 static void
12992 arm_init_iwmmxt_builtins (void)
12993 {
12994   const struct builtin_description * d;
12995   size_t i;
12996   tree endlink = void_list_node;
12997
12998   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12999   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
13000   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
13001
13002   tree int_ftype_int
13003     = build_function_type (integer_type_node,
13004                            tree_cons (NULL_TREE, integer_type_node, endlink));
13005   tree v8qi_ftype_v8qi_v8qi_int
13006     = build_function_type (V8QI_type_node,
13007                            tree_cons (NULL_TREE, V8QI_type_node,
13008                                       tree_cons (NULL_TREE, V8QI_type_node,
13009                                                  tree_cons (NULL_TREE,
13010                                                             integer_type_node,
13011                                                             endlink))));
13012   tree v4hi_ftype_v4hi_int
13013     = build_function_type (V4HI_type_node,
13014                            tree_cons (NULL_TREE, V4HI_type_node,
13015                                       tree_cons (NULL_TREE, integer_type_node,
13016                                                  endlink)));
13017   tree v2si_ftype_v2si_int
13018     = build_function_type (V2SI_type_node,
13019                            tree_cons (NULL_TREE, V2SI_type_node,
13020                                       tree_cons (NULL_TREE, integer_type_node,
13021                                                  endlink)));
13022   tree v2si_ftype_di_di
13023     = build_function_type (V2SI_type_node,
13024                            tree_cons (NULL_TREE, long_long_integer_type_node,
13025                                       tree_cons (NULL_TREE, long_long_integer_type_node,
13026                                                  endlink)));
13027   tree di_ftype_di_int
13028     = build_function_type (long_long_integer_type_node,
13029                            tree_cons (NULL_TREE, long_long_integer_type_node,
13030                                       tree_cons (NULL_TREE, integer_type_node,
13031                                                  endlink)));
13032   tree di_ftype_di_int_int
13033     = build_function_type (long_long_integer_type_node,
13034                            tree_cons (NULL_TREE, long_long_integer_type_node,
13035                                       tree_cons (NULL_TREE, integer_type_node,
13036                                                  tree_cons (NULL_TREE,
13037                                                             integer_type_node,
13038                                                             endlink))));
13039   tree int_ftype_v8qi
13040     = build_function_type (integer_type_node,
13041                            tree_cons (NULL_TREE, V8QI_type_node,
13042                                       endlink));
13043   tree int_ftype_v4hi
13044     = build_function_type (integer_type_node,
13045                            tree_cons (NULL_TREE, V4HI_type_node,
13046                                       endlink));
13047   tree int_ftype_v2si
13048     = build_function_type (integer_type_node,
13049                            tree_cons (NULL_TREE, V2SI_type_node,
13050                                       endlink));
13051   tree int_ftype_v8qi_int
13052     = build_function_type (integer_type_node,
13053                            tree_cons (NULL_TREE, V8QI_type_node,
13054                                       tree_cons (NULL_TREE, integer_type_node,
13055                                                  endlink)));
13056   tree int_ftype_v4hi_int
13057     = build_function_type (integer_type_node,
13058                            tree_cons (NULL_TREE, V4HI_type_node,
13059                                       tree_cons (NULL_TREE, integer_type_node,
13060                                                  endlink)));
13061   tree int_ftype_v2si_int
13062     = build_function_type (integer_type_node,
13063                            tree_cons (NULL_TREE, V2SI_type_node,
13064                                       tree_cons (NULL_TREE, integer_type_node,
13065                                                  endlink)));
13066   tree v8qi_ftype_v8qi_int_int
13067     = build_function_type (V8QI_type_node,
13068                            tree_cons (NULL_TREE, V8QI_type_node,
13069                                       tree_cons (NULL_TREE, integer_type_node,
13070                                                  tree_cons (NULL_TREE,
13071                                                             integer_type_node,
13072                                                             endlink))));
13073   tree v4hi_ftype_v4hi_int_int
13074     = build_function_type (V4HI_type_node,
13075                            tree_cons (NULL_TREE, V4HI_type_node,
13076                                       tree_cons (NULL_TREE, integer_type_node,
13077                                                  tree_cons (NULL_TREE,
13078                                                             integer_type_node,
13079                                                             endlink))));
13080   tree v2si_ftype_v2si_int_int
13081     = build_function_type (V2SI_type_node,
13082                            tree_cons (NULL_TREE, V2SI_type_node,
13083                                       tree_cons (NULL_TREE, integer_type_node,
13084                                                  tree_cons (NULL_TREE,
13085                                                             integer_type_node,
13086                                                             endlink))));
13087   /* Miscellaneous.  */
13088   tree v8qi_ftype_v4hi_v4hi
13089     = build_function_type (V8QI_type_node,
13090                            tree_cons (NULL_TREE, V4HI_type_node,
13091                                       tree_cons (NULL_TREE, V4HI_type_node,
13092                                                  endlink)));
13093   tree v4hi_ftype_v2si_v2si
13094     = build_function_type (V4HI_type_node,
13095                            tree_cons (NULL_TREE, V2SI_type_node,
13096                                       tree_cons (NULL_TREE, V2SI_type_node,
13097                                                  endlink)));
13098   tree v2si_ftype_v4hi_v4hi
13099     = build_function_type (V2SI_type_node,
13100                            tree_cons (NULL_TREE, V4HI_type_node,
13101                                       tree_cons (NULL_TREE, V4HI_type_node,
13102                                                  endlink)));
13103   tree v2si_ftype_v8qi_v8qi
13104     = build_function_type (V2SI_type_node,
13105                            tree_cons (NULL_TREE, V8QI_type_node,
13106                                       tree_cons (NULL_TREE, V8QI_type_node,
13107                                                  endlink)));
13108   tree v4hi_ftype_v4hi_di
13109     = build_function_type (V4HI_type_node,
13110                            tree_cons (NULL_TREE, V4HI_type_node,
13111                                       tree_cons (NULL_TREE,
13112                                                  long_long_integer_type_node,
13113                                                  endlink)));
13114   tree v2si_ftype_v2si_di
13115     = build_function_type (V2SI_type_node,
13116                            tree_cons (NULL_TREE, V2SI_type_node,
13117                                       tree_cons (NULL_TREE,
13118                                                  long_long_integer_type_node,
13119                                                  endlink)));
13120   tree void_ftype_int_int
13121     = build_function_type (void_type_node,
13122                            tree_cons (NULL_TREE, integer_type_node,
13123                                       tree_cons (NULL_TREE, integer_type_node,
13124                                                  endlink)));
13125   tree di_ftype_void
13126     = build_function_type (long_long_unsigned_type_node, endlink);
13127   tree di_ftype_v8qi
13128     = build_function_type (long_long_integer_type_node,
13129                            tree_cons (NULL_TREE, V8QI_type_node,
13130                                       endlink));
13131   tree di_ftype_v4hi
13132     = build_function_type (long_long_integer_type_node,
13133                            tree_cons (NULL_TREE, V4HI_type_node,
13134                                       endlink));
13135   tree di_ftype_v2si
13136     = build_function_type (long_long_integer_type_node,
13137                            tree_cons (NULL_TREE, V2SI_type_node,
13138                                       endlink));
13139   tree v2si_ftype_v4hi
13140     = build_function_type (V2SI_type_node,
13141                            tree_cons (NULL_TREE, V4HI_type_node,
13142                                       endlink));
13143   tree v4hi_ftype_v8qi
13144     = build_function_type (V4HI_type_node,
13145                            tree_cons (NULL_TREE, V8QI_type_node,
13146                                       endlink));
13147
13148   tree di_ftype_di_v4hi_v4hi
13149     = build_function_type (long_long_unsigned_type_node,
13150                            tree_cons (NULL_TREE,
13151                                       long_long_unsigned_type_node,
13152                                       tree_cons (NULL_TREE, V4HI_type_node,
13153                                                  tree_cons (NULL_TREE,
13154                                                             V4HI_type_node,
13155                                                             endlink))));
13156
13157   tree di_ftype_v4hi_v4hi
13158     = build_function_type (long_long_unsigned_type_node,
13159                            tree_cons (NULL_TREE, V4HI_type_node,
13160                                       tree_cons (NULL_TREE, V4HI_type_node,
13161                                                  endlink)));
13162
13163   /* Normal vector binops.  */
13164   tree v8qi_ftype_v8qi_v8qi
13165     = build_function_type (V8QI_type_node,
13166                            tree_cons (NULL_TREE, V8QI_type_node,
13167                                       tree_cons (NULL_TREE, V8QI_type_node,
13168                                                  endlink)));
13169   tree v4hi_ftype_v4hi_v4hi
13170     = build_function_type (V4HI_type_node,
13171                            tree_cons (NULL_TREE, V4HI_type_node,
13172                                       tree_cons (NULL_TREE, V4HI_type_node,
13173                                                  endlink)));
13174   tree v2si_ftype_v2si_v2si
13175     = build_function_type (V2SI_type_node,
13176                            tree_cons (NULL_TREE, V2SI_type_node,
13177                                       tree_cons (NULL_TREE, V2SI_type_node,
13178                                                  endlink)));
13179   tree di_ftype_di_di
13180     = build_function_type (long_long_unsigned_type_node,
13181                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
13182                                       tree_cons (NULL_TREE,
13183                                                  long_long_unsigned_type_node,
13184                                                  endlink)));
13185
13186   /* Add all builtins that are more or less simple operations on two
13187      operands.  */
13188   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13189     {
13190       /* Use one of the operands; the target can have a different mode for
13191          mask-generating compares.  */
13192       enum machine_mode mode;
13193       tree type;
13194
13195       if (d->name == 0)
13196         continue;
13197
13198       mode = insn_data[d->icode].operand[1].mode;
13199
13200       switch (mode)
13201         {
13202         case V8QImode:
13203           type = v8qi_ftype_v8qi_v8qi;
13204           break;
13205         case V4HImode:
13206           type = v4hi_ftype_v4hi_v4hi;
13207           break;
13208         case V2SImode:
13209           type = v2si_ftype_v2si_v2si;
13210           break;
13211         case DImode:
13212           type = di_ftype_di_di;
13213           break;
13214
13215         default:
13216           gcc_unreachable ();
13217         }
13218
13219       def_mbuiltin (d->mask, d->name, type, d->code);
13220     }
13221
13222   /* Add the remaining MMX insns with somewhat more complicated types.  */
13223   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
13224   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
13225   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
13226
13227   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
13228   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
13229   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
13230   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
13231   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
13232   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
13233
13234   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
13235   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
13236   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
13237   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
13238   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
13239   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
13240
13241   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
13242   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
13243   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
13244   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
13245   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
13246   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
13247
13248   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
13249   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
13250   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
13251   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
13252   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
13253   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
13254
13255   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
13256
13257   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
13258   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
13259   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
13260   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
13261
13262   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
13263   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
13264   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
13265   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
13266   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
13267   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
13268   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
13269   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
13270   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
13271
13272   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
13273   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
13274   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
13275
13276   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
13277   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
13278   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
13279
13280   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
13281   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
13282   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
13283   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
13284   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
13285   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
13286
13287   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
13288   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
13289   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
13290   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
13291   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
13292   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
13293   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
13294   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
13295   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
13296   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
13297   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
13298   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
13299
13300   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
13301   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
13302   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
13303   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
13304
13305   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
13306   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
13307   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
13308   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
13309   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
13310   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
13311   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
13312 }
13313
13314 static void
13315 arm_init_tls_builtins (void)
13316 {
13317   tree ftype;
13318   tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
13319   tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
13320
13321   ftype = build_function_type (ptr_type_node, void_list_node);
13322   add_builtin_function ("__builtin_thread_pointer", ftype,
13323                         ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
13324                         NULL, const_nothrow);
13325 }
13326
13327 static void
13328 arm_init_builtins (void)
13329 {
13330   arm_init_tls_builtins ();
13331
13332   if (TARGET_REALLY_IWMMXT)
13333     arm_init_iwmmxt_builtins ();
13334 }
13335
13336 /* Errors in the source file can cause expand_expr to return const0_rtx
13337    where we expect a vector.  To avoid crashing, use one of the vector
13338    clear instructions.  */
13339
13340 static rtx
13341 safe_vector_operand (rtx x, enum machine_mode mode)
13342 {
13343   if (x != const0_rtx)
13344     return x;
13345   x = gen_reg_rtx (mode);
13346
13347   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
13348                                : gen_rtx_SUBREG (DImode, x, 0)));
13349   return x;
13350 }
13351
13352 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
13353
13354 static rtx
13355 arm_expand_binop_builtin (enum insn_code icode,
13356                           tree exp, rtx target)
13357 {
13358   rtx pat;
13359   tree arg0 = CALL_EXPR_ARG (exp, 0);
13360   tree arg1 = CALL_EXPR_ARG (exp, 1);
13361   rtx op0 = expand_normal (arg0);
13362   rtx op1 = expand_normal (arg1);
13363   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13364   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13365   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
13366
13367   if (VECTOR_MODE_P (mode0))
13368     op0 = safe_vector_operand (op0, mode0);
13369   if (VECTOR_MODE_P (mode1))
13370     op1 = safe_vector_operand (op1, mode1);
13371
13372   if (! target
13373       || GET_MODE (target) != tmode
13374       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13375     target = gen_reg_rtx (tmode);
13376
13377   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
13378
13379   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13380     op0 = copy_to_mode_reg (mode0, op0);
13381   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13382     op1 = copy_to_mode_reg (mode1, op1);
13383
13384   pat = GEN_FCN (icode) (target, op0, op1);
13385   if (! pat)
13386     return 0;
13387   emit_insn (pat);
13388   return target;
13389 }
13390
13391 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
13392
13393 static rtx
13394 arm_expand_unop_builtin (enum insn_code icode,
13395                          tree exp, rtx target, int do_load)
13396 {
13397   rtx pat;
13398   tree arg0 = CALL_EXPR_ARG (exp, 0);
13399   rtx op0 = expand_normal (arg0);
13400   enum machine_mode tmode = insn_data[icode].operand[0].mode;
13401   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
13402
13403   if (! target
13404       || GET_MODE (target) != tmode
13405       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13406     target = gen_reg_rtx (tmode);
13407   if (do_load)
13408     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
13409   else
13410     {
13411       if (VECTOR_MODE_P (mode0))
13412         op0 = safe_vector_operand (op0, mode0);
13413
13414       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13415         op0 = copy_to_mode_reg (mode0, op0);
13416     }
13417
13418   pat = GEN_FCN (icode) (target, op0);
13419   if (! pat)
13420     return 0;
13421   emit_insn (pat);
13422   return target;
13423 }
13424
13425 /* Expand an expression EXP that calls a built-in function,
13426    with result going to TARGET if that's convenient
13427    (and in mode MODE if that's convenient).
13428    SUBTARGET may be used as the target for computing one of EXP's operands.
13429    IGNORE is nonzero if the value is to be ignored.  */
13430
13431 static rtx
13432 arm_expand_builtin (tree exp,
13433                     rtx target,
13434                     rtx subtarget ATTRIBUTE_UNUSED,
13435                     enum machine_mode mode ATTRIBUTE_UNUSED,
13436                     int ignore ATTRIBUTE_UNUSED)
13437 {
13438   const struct builtin_description * d;
13439   enum insn_code    icode;
13440   tree              fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
13441   tree              arg0;
13442   tree              arg1;
13443   tree              arg2;
13444   rtx               op0;
13445   rtx               op1;
13446   rtx               op2;
13447   rtx               pat;
13448   int               fcode = DECL_FUNCTION_CODE (fndecl);
13449   size_t            i;
13450   enum machine_mode tmode;
13451   enum machine_mode mode0;
13452   enum machine_mode mode1;
13453   enum machine_mode mode2;
13454
13455   switch (fcode)
13456     {
13457     case ARM_BUILTIN_TEXTRMSB:
13458     case ARM_BUILTIN_TEXTRMUB:
13459     case ARM_BUILTIN_TEXTRMSH:
13460     case ARM_BUILTIN_TEXTRMUH:
13461     case ARM_BUILTIN_TEXTRMSW:
13462     case ARM_BUILTIN_TEXTRMUW:
13463       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
13464                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
13465                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
13466                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
13467                : CODE_FOR_iwmmxt_textrmw);
13468
13469       arg0 = CALL_EXPR_ARG (exp, 0);
13470       arg1 = CALL_EXPR_ARG (exp, 1);
13471       op0 = expand_normal (arg0);
13472       op1 = expand_normal (arg1);
13473       tmode = insn_data[icode].operand[0].mode;
13474       mode0 = insn_data[icode].operand[1].mode;
13475       mode1 = insn_data[icode].operand[2].mode;
13476
13477       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13478         op0 = copy_to_mode_reg (mode0, op0);
13479       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13480         {
13481           /* @@@ better error message */
13482           error ("selector must be an immediate");
13483           return gen_reg_rtx (tmode);
13484         }
13485       if (target == 0
13486           || GET_MODE (target) != tmode
13487           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13488         target = gen_reg_rtx (tmode);
13489       pat = GEN_FCN (icode) (target, op0, op1);
13490       if (! pat)
13491         return 0;
13492       emit_insn (pat);
13493       return target;
13494
13495     case ARM_BUILTIN_TINSRB:
13496     case ARM_BUILTIN_TINSRH:
13497     case ARM_BUILTIN_TINSRW:
13498       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
13499                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
13500                : CODE_FOR_iwmmxt_tinsrw);
13501       arg0 = CALL_EXPR_ARG (exp, 0);
13502       arg1 = CALL_EXPR_ARG (exp, 1);
13503       arg2 = CALL_EXPR_ARG (exp, 2);
13504       op0 = expand_normal (arg0);
13505       op1 = expand_normal (arg1);
13506       op2 = expand_normal (arg2);
13507       tmode = insn_data[icode].operand[0].mode;
13508       mode0 = insn_data[icode].operand[1].mode;
13509       mode1 = insn_data[icode].operand[2].mode;
13510       mode2 = insn_data[icode].operand[3].mode;
13511
13512       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13513         op0 = copy_to_mode_reg (mode0, op0);
13514       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13515         op1 = copy_to_mode_reg (mode1, op1);
13516       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13517         {
13518           /* @@@ better error message */
13519           error ("selector must be an immediate");
13520           return const0_rtx;
13521         }
13522       if (target == 0
13523           || GET_MODE (target) != tmode
13524           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13525         target = gen_reg_rtx (tmode);
13526       pat = GEN_FCN (icode) (target, op0, op1, op2);
13527       if (! pat)
13528         return 0;
13529       emit_insn (pat);
13530       return target;
13531
13532     case ARM_BUILTIN_SETWCX:
13533       arg0 = CALL_EXPR_ARG (exp, 0);
13534       arg1 = CALL_EXPR_ARG (exp, 1);
13535       op0 = force_reg (SImode, expand_normal (arg0));
13536       op1 = expand_normal (arg1);
13537       emit_insn (gen_iwmmxt_tmcr (op1, op0));
13538       return 0;
13539
13540     case ARM_BUILTIN_GETWCX:
13541       arg0 = CALL_EXPR_ARG (exp, 0);
13542       op0 = expand_normal (arg0);
13543       target = gen_reg_rtx (SImode);
13544       emit_insn (gen_iwmmxt_tmrc (target, op0));
13545       return target;
13546
13547     case ARM_BUILTIN_WSHUFH:
13548       icode = CODE_FOR_iwmmxt_wshufh;
13549       arg0 = CALL_EXPR_ARG (exp, 0);
13550       arg1 = CALL_EXPR_ARG (exp, 1);
13551       op0 = expand_normal (arg0);
13552       op1 = expand_normal (arg1);
13553       tmode = insn_data[icode].operand[0].mode;
13554       mode1 = insn_data[icode].operand[1].mode;
13555       mode2 = insn_data[icode].operand[2].mode;
13556
13557       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
13558         op0 = copy_to_mode_reg (mode1, op0);
13559       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
13560         {
13561           /* @@@ better error message */
13562           error ("mask must be an immediate");
13563           return const0_rtx;
13564         }
13565       if (target == 0
13566           || GET_MODE (target) != tmode
13567           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13568         target = gen_reg_rtx (tmode);
13569       pat = GEN_FCN (icode) (target, op0, op1);
13570       if (! pat)
13571         return 0;
13572       emit_insn (pat);
13573       return target;
13574
13575     case ARM_BUILTIN_WSADB:
13576       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, exp, target);
13577     case ARM_BUILTIN_WSADH:
13578       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, exp, target);
13579     case ARM_BUILTIN_WSADBZ:
13580       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, exp, target);
13581     case ARM_BUILTIN_WSADHZ:
13582       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, exp, target);
13583
13584       /* Several three-argument builtins.  */
13585     case ARM_BUILTIN_WMACS:
13586     case ARM_BUILTIN_WMACU:
13587     case ARM_BUILTIN_WALIGN:
13588     case ARM_BUILTIN_TMIA:
13589     case ARM_BUILTIN_TMIAPH:
13590     case ARM_BUILTIN_TMIATT:
13591     case ARM_BUILTIN_TMIATB:
13592     case ARM_BUILTIN_TMIABT:
13593     case ARM_BUILTIN_TMIABB:
13594       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
13595                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
13596                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
13597                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
13598                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
13599                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
13600                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
13601                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
13602                : CODE_FOR_iwmmxt_walign);
13603       arg0 = CALL_EXPR_ARG (exp, 0);
13604       arg1 = CALL_EXPR_ARG (exp, 1);
13605       arg2 = CALL_EXPR_ARG (exp, 2);
13606       op0 = expand_normal (arg0);
13607       op1 = expand_normal (arg1);
13608       op2 = expand_normal (arg2);
13609       tmode = insn_data[icode].operand[0].mode;
13610       mode0 = insn_data[icode].operand[1].mode;
13611       mode1 = insn_data[icode].operand[2].mode;
13612       mode2 = insn_data[icode].operand[3].mode;
13613
13614       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
13615         op0 = copy_to_mode_reg (mode0, op0);
13616       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
13617         op1 = copy_to_mode_reg (mode1, op1);
13618       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
13619         op2 = copy_to_mode_reg (mode2, op2);
13620       if (target == 0
13621           || GET_MODE (target) != tmode
13622           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
13623         target = gen_reg_rtx (tmode);
13624       pat = GEN_FCN (icode) (target, op0, op1, op2);
13625       if (! pat)
13626         return 0;
13627       emit_insn (pat);
13628       return target;
13629
13630     case ARM_BUILTIN_WZERO:
13631       target = gen_reg_rtx (DImode);
13632       emit_insn (gen_iwmmxt_clrdi (target));
13633       return target;
13634
13635     case ARM_BUILTIN_THREAD_POINTER:
13636       return arm_load_tp (target);
13637
13638     default:
13639       break;
13640     }
13641
13642   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13643     if (d->code == (const enum arm_builtins) fcode)
13644       return arm_expand_binop_builtin (d->icode, exp, target);
13645
13646   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
13647     if (d->code == (const enum arm_builtins) fcode)
13648       return arm_expand_unop_builtin (d->icode, exp, target, 0);
13649
13650   /* @@@ Should really do something sensible here.  */
13651   return NULL_RTX;
13652 }
13653 \f
13654 /* Return the number (counting from 0) of
13655    the least significant set bit in MASK.  */
13656
13657 inline static int
13658 number_of_first_bit_set (unsigned mask)
13659 {
13660   int bit;
13661
13662   for (bit = 0;
13663        (mask & (1 << bit)) == 0;
13664        ++bit)
13665     continue;
13666
13667   return bit;
13668 }
13669
13670 /* Emit code to push or pop registers to or from the stack.  F is the
13671    assembly file.  MASK is the registers to push or pop.  PUSH is
13672    nonzero if we should push, and zero if we should pop.  For debugging
13673    output, if pushing, adjust CFA_OFFSET by the amount of space added
13674    to the stack.  REAL_REGS should have the same number of bits set as
13675    MASK, and will be used instead (in the same order) to describe which
13676    registers were saved - this is used to mark the save slots when we
13677    push high registers after moving them to low registers.  */
13678 static void
13679 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
13680                unsigned long real_regs)
13681 {
13682   int regno;
13683   int lo_mask = mask & 0xFF;
13684   int pushed_words = 0;
13685
13686   gcc_assert (mask);
13687
13688   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
13689     {
13690       /* Special case.  Do not generate a POP PC statement here, do it in
13691          thumb_exit() */
13692       thumb_exit (f, -1);
13693       return;
13694     }
13695
13696   if (ARM_EABI_UNWIND_TABLES && push)
13697     {
13698       fprintf (f, "\t.save\t{");
13699       for (regno = 0; regno < 15; regno++)
13700         {
13701           if (real_regs & (1 << regno))
13702             {
13703               if (real_regs & ((1 << regno) -1))
13704                 fprintf (f, ", ");
13705               asm_fprintf (f, "%r", regno);
13706             }
13707         }
13708       fprintf (f, "}\n");
13709     }
13710
13711   fprintf (f, "\t%s\t{", push ? "push" : "pop");
13712
13713   /* Look at the low registers first.  */
13714   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
13715     {
13716       if (lo_mask & 1)
13717         {
13718           asm_fprintf (f, "%r", regno);
13719
13720           if ((lo_mask & ~1) != 0)
13721             fprintf (f, ", ");
13722
13723           pushed_words++;
13724         }
13725     }
13726
13727   if (push && (mask & (1 << LR_REGNUM)))
13728     {
13729       /* Catch pushing the LR.  */
13730       if (mask & 0xFF)
13731         fprintf (f, ", ");
13732
13733       asm_fprintf (f, "%r", LR_REGNUM);
13734
13735       pushed_words++;
13736     }
13737   else if (!push && (mask & (1 << PC_REGNUM)))
13738     {
13739       /* Catch popping the PC.  */
13740       if (TARGET_INTERWORK || TARGET_BACKTRACE
13741           || current_function_calls_eh_return)
13742         {
13743           /* The PC is never poped directly, instead
13744              it is popped into r3 and then BX is used.  */
13745           fprintf (f, "}\n");
13746
13747           thumb_exit (f, -1);
13748
13749           return;
13750         }
13751       else
13752         {
13753           if (mask & 0xFF)
13754             fprintf (f, ", ");
13755
13756           asm_fprintf (f, "%r", PC_REGNUM);
13757         }
13758     }
13759
13760   fprintf (f, "}\n");
13761
13762   if (push && pushed_words && dwarf2out_do_frame ())
13763     {
13764       char *l = dwarf2out_cfi_label ();
13765       int pushed_mask = real_regs;
13766
13767       *cfa_offset += pushed_words * 4;
13768       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
13769
13770       pushed_words = 0;
13771       pushed_mask = real_regs;
13772       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
13773         {
13774           if (pushed_mask & 1)
13775             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
13776         }
13777     }
13778 }
13779
13780 /* Generate code to return from a thumb function.
13781    If 'reg_containing_return_addr' is -1, then the return address is
13782    actually on the stack, at the stack pointer.  */
13783 static void
13784 thumb_exit (FILE *f, int reg_containing_return_addr)
13785 {
13786   unsigned regs_available_for_popping;
13787   unsigned regs_to_pop;
13788   int pops_needed;
13789   unsigned available;
13790   unsigned required;
13791   int mode;
13792   int size;
13793   int restore_a4 = FALSE;
13794
13795   /* Compute the registers we need to pop.  */
13796   regs_to_pop = 0;
13797   pops_needed = 0;
13798
13799   if (reg_containing_return_addr == -1)
13800     {
13801       regs_to_pop |= 1 << LR_REGNUM;
13802       ++pops_needed;
13803     }
13804
13805   if (TARGET_BACKTRACE)
13806     {
13807       /* Restore the (ARM) frame pointer and stack pointer.  */
13808       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
13809       pops_needed += 2;
13810     }
13811
13812   /* If there is nothing to pop then just emit the BX instruction and
13813      return.  */
13814   if (pops_needed == 0)
13815     {
13816       if (current_function_calls_eh_return)
13817         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13818
13819       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13820       return;
13821     }
13822   /* Otherwise if we are not supporting interworking and we have not created
13823      a backtrace structure and the function was not entered in ARM mode then
13824      just pop the return address straight into the PC.  */
13825   else if (!TARGET_INTERWORK
13826            && !TARGET_BACKTRACE
13827            && !is_called_in_ARM_mode (current_function_decl)
13828            && !current_function_calls_eh_return)
13829     {
13830       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
13831       return;
13832     }
13833
13834   /* Find out how many of the (return) argument registers we can corrupt.  */
13835   regs_available_for_popping = 0;
13836
13837   /* If returning via __builtin_eh_return, the bottom three registers
13838      all contain information needed for the return.  */
13839   if (current_function_calls_eh_return)
13840     size = 12;
13841   else
13842     {
13843       /* If we can deduce the registers used from the function's
13844          return value.  This is more reliable that examining
13845          regs_ever_live[] because that will be set if the register is
13846          ever used in the function, not just if the register is used
13847          to hold a return value.  */
13848
13849       if (current_function_return_rtx != 0)
13850         mode = GET_MODE (current_function_return_rtx);
13851       else
13852         mode = DECL_MODE (DECL_RESULT (current_function_decl));
13853
13854       size = GET_MODE_SIZE (mode);
13855
13856       if (size == 0)
13857         {
13858           /* In a void function we can use any argument register.
13859              In a function that returns a structure on the stack
13860              we can use the second and third argument registers.  */
13861           if (mode == VOIDmode)
13862             regs_available_for_popping =
13863               (1 << ARG_REGISTER (1))
13864               | (1 << ARG_REGISTER (2))
13865               | (1 << ARG_REGISTER (3));
13866           else
13867             regs_available_for_popping =
13868               (1 << ARG_REGISTER (2))
13869               | (1 << ARG_REGISTER (3));
13870         }
13871       else if (size <= 4)
13872         regs_available_for_popping =
13873           (1 << ARG_REGISTER (2))
13874           | (1 << ARG_REGISTER (3));
13875       else if (size <= 8)
13876         regs_available_for_popping =
13877           (1 << ARG_REGISTER (3));
13878     }
13879
13880   /* Match registers to be popped with registers into which we pop them.  */
13881   for (available = regs_available_for_popping,
13882        required  = regs_to_pop;
13883        required != 0 && available != 0;
13884        available &= ~(available & - available),
13885        required  &= ~(required  & - required))
13886     -- pops_needed;
13887
13888   /* If we have any popping registers left over, remove them.  */
13889   if (available > 0)
13890     regs_available_for_popping &= ~available;
13891
13892   /* Otherwise if we need another popping register we can use
13893      the fourth argument register.  */
13894   else if (pops_needed)
13895     {
13896       /* If we have not found any free argument registers and
13897          reg a4 contains the return address, we must move it.  */
13898       if (regs_available_for_popping == 0
13899           && reg_containing_return_addr == LAST_ARG_REGNUM)
13900         {
13901           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13902           reg_containing_return_addr = LR_REGNUM;
13903         }
13904       else if (size > 12)
13905         {
13906           /* Register a4 is being used to hold part of the return value,
13907              but we have dire need of a free, low register.  */
13908           restore_a4 = TRUE;
13909
13910           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13911         }
13912
13913       if (reg_containing_return_addr != LAST_ARG_REGNUM)
13914         {
13915           /* The fourth argument register is available.  */
13916           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13917
13918           --pops_needed;
13919         }
13920     }
13921
13922   /* Pop as many registers as we can.  */
13923   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13924                  regs_available_for_popping);
13925
13926   /* Process the registers we popped.  */
13927   if (reg_containing_return_addr == -1)
13928     {
13929       /* The return address was popped into the lowest numbered register.  */
13930       regs_to_pop &= ~(1 << LR_REGNUM);
13931
13932       reg_containing_return_addr =
13933         number_of_first_bit_set (regs_available_for_popping);
13934
13935       /* Remove this register for the mask of available registers, so that
13936          the return address will not be corrupted by further pops.  */
13937       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13938     }
13939
13940   /* If we popped other registers then handle them here.  */
13941   if (regs_available_for_popping)
13942     {
13943       int frame_pointer;
13944
13945       /* Work out which register currently contains the frame pointer.  */
13946       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13947
13948       /* Move it into the correct place.  */
13949       asm_fprintf (f, "\tmov\t%r, %r\n",
13950                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13951
13952       /* (Temporarily) remove it from the mask of popped registers.  */
13953       regs_available_for_popping &= ~(1 << frame_pointer);
13954       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13955
13956       if (regs_available_for_popping)
13957         {
13958           int stack_pointer;
13959
13960           /* We popped the stack pointer as well,
13961              find the register that contains it.  */
13962           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13963
13964           /* Move it into the stack register.  */
13965           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13966
13967           /* At this point we have popped all necessary registers, so
13968              do not worry about restoring regs_available_for_popping
13969              to its correct value:
13970
13971              assert (pops_needed == 0)
13972              assert (regs_available_for_popping == (1 << frame_pointer))
13973              assert (regs_to_pop == (1 << STACK_POINTER))  */
13974         }
13975       else
13976         {
13977           /* Since we have just move the popped value into the frame
13978              pointer, the popping register is available for reuse, and
13979              we know that we still have the stack pointer left to pop.  */
13980           regs_available_for_popping |= (1 << frame_pointer);
13981         }
13982     }
13983
13984   /* If we still have registers left on the stack, but we no longer have
13985      any registers into which we can pop them, then we must move the return
13986      address into the link register and make available the register that
13987      contained it.  */
13988   if (regs_available_for_popping == 0 && pops_needed > 0)
13989     {
13990       regs_available_for_popping |= 1 << reg_containing_return_addr;
13991
13992       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13993                    reg_containing_return_addr);
13994
13995       reg_containing_return_addr = LR_REGNUM;
13996     }
13997
13998   /* If we have registers left on the stack then pop some more.
13999      We know that at most we will want to pop FP and SP.  */
14000   if (pops_needed > 0)
14001     {
14002       int  popped_into;
14003       int  move_to;
14004
14005       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
14006                      regs_available_for_popping);
14007
14008       /* We have popped either FP or SP.
14009          Move whichever one it is into the correct register.  */
14010       popped_into = number_of_first_bit_set (regs_available_for_popping);
14011       move_to     = number_of_first_bit_set (regs_to_pop);
14012
14013       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
14014
14015       regs_to_pop &= ~(1 << move_to);
14016
14017       --pops_needed;
14018     }
14019
14020   /* If we still have not popped everything then we must have only
14021      had one register available to us and we are now popping the SP.  */
14022   if (pops_needed > 0)
14023     {
14024       int  popped_into;
14025
14026       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
14027                      regs_available_for_popping);
14028
14029       popped_into = number_of_first_bit_set (regs_available_for_popping);
14030
14031       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
14032       /*
14033         assert (regs_to_pop == (1 << STACK_POINTER))
14034         assert (pops_needed == 1)
14035       */
14036     }
14037
14038   /* If necessary restore the a4 register.  */
14039   if (restore_a4)
14040     {
14041       if (reg_containing_return_addr != LR_REGNUM)
14042         {
14043           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
14044           reg_containing_return_addr = LR_REGNUM;
14045         }
14046
14047       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
14048     }
14049
14050   if (current_function_calls_eh_return)
14051     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
14052
14053   /* Return to caller.  */
14054   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
14055 }
14056
14057 \f
14058 void
14059 thumb1_final_prescan_insn (rtx insn)
14060 {
14061   if (flag_print_asm_name)
14062     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
14063                  INSN_ADDRESSES (INSN_UID (insn)));
14064 }
14065
14066 int
14067 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
14068 {
14069   unsigned HOST_WIDE_INT mask = 0xff;
14070   int i;
14071
14072   if (val == 0) /* XXX */
14073     return 0;
14074
14075   for (i = 0; i < 25; i++)
14076     if ((val & (mask << i)) == val)
14077       return 1;
14078
14079   return 0;
14080 }
14081
14082 /* Returns nonzero if the current function contains,
14083    or might contain a far jump.  */
14084 static int
14085 thumb_far_jump_used_p (void)
14086 {
14087   rtx insn;
14088
14089   /* This test is only important for leaf functions.  */
14090   /* assert (!leaf_function_p ()); */
14091
14092   /* If we have already decided that far jumps may be used,
14093      do not bother checking again, and always return true even if
14094      it turns out that they are not being used.  Once we have made
14095      the decision that far jumps are present (and that hence the link
14096      register will be pushed onto the stack) we cannot go back on it.  */
14097   if (cfun->machine->far_jump_used)
14098     return 1;
14099
14100   /* If this function is not being called from the prologue/epilogue
14101      generation code then it must be being called from the
14102      INITIAL_ELIMINATION_OFFSET macro.  */
14103   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
14104     {
14105       /* In this case we know that we are being asked about the elimination
14106          of the arg pointer register.  If that register is not being used,
14107          then there are no arguments on the stack, and we do not have to
14108          worry that a far jump might force the prologue to push the link
14109          register, changing the stack offsets.  In this case we can just
14110          return false, since the presence of far jumps in the function will
14111          not affect stack offsets.
14112
14113          If the arg pointer is live (or if it was live, but has now been
14114          eliminated and so set to dead) then we do have to test to see if
14115          the function might contain a far jump.  This test can lead to some
14116          false negatives, since before reload is completed, then length of
14117          branch instructions is not known, so gcc defaults to returning their
14118          longest length, which in turn sets the far jump attribute to true.
14119
14120          A false negative will not result in bad code being generated, but it
14121          will result in a needless push and pop of the link register.  We
14122          hope that this does not occur too often.
14123
14124          If we need doubleword stack alignment this could affect the other
14125          elimination offsets so we can't risk getting it wrong.  */
14126       if (regs_ever_live [ARG_POINTER_REGNUM])
14127         cfun->machine->arg_pointer_live = 1;
14128       else if (!cfun->machine->arg_pointer_live)
14129         return 0;
14130     }
14131
14132   /* Check to see if the function contains a branch
14133      insn with the far jump attribute set.  */
14134   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
14135     {
14136       if (GET_CODE (insn) == JUMP_INSN
14137           /* Ignore tablejump patterns.  */
14138           && GET_CODE (PATTERN (insn)) != ADDR_VEC
14139           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
14140           && get_attr_far_jump (insn) == FAR_JUMP_YES
14141           )
14142         {
14143           /* Record the fact that we have decided that
14144              the function does use far jumps.  */
14145           cfun->machine->far_jump_used = 1;
14146           return 1;
14147         }
14148     }
14149
14150   return 0;
14151 }
14152
14153 /* Return nonzero if FUNC must be entered in ARM mode.  */
14154 int
14155 is_called_in_ARM_mode (tree func)
14156 {
14157   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
14158
14159   /* Ignore the problem about functions whose address is taken.  */
14160   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
14161     return TRUE;
14162
14163 #ifdef ARM_PE
14164   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
14165 #else
14166   return FALSE;
14167 #endif
14168 }
14169
14170 /* The bits which aren't usefully expanded as rtl.  */
14171 const char *
14172 thumb_unexpanded_epilogue (void)
14173 {
14174   int regno;
14175   unsigned long live_regs_mask = 0;
14176   int high_regs_pushed = 0;
14177   int had_to_push_lr;
14178   int size;
14179
14180   if (return_used_this_function)
14181     return "";
14182
14183   if (IS_NAKED (arm_current_func_type ()))
14184     return "";
14185
14186   live_regs_mask = thumb1_compute_save_reg_mask ();
14187   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
14188
14189   /* If we can deduce the registers used from the function's return value.
14190      This is more reliable that examining regs_ever_live[] because that
14191      will be set if the register is ever used in the function, not just if
14192      the register is used to hold a return value.  */
14193   size = arm_size_return_regs ();
14194
14195   /* The prolog may have pushed some high registers to use as
14196      work registers.  e.g. the testsuite file:
14197      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
14198      compiles to produce:
14199         push    {r4, r5, r6, r7, lr}
14200         mov     r7, r9
14201         mov     r6, r8
14202         push    {r6, r7}
14203      as part of the prolog.  We have to undo that pushing here.  */
14204
14205   if (high_regs_pushed)
14206     {
14207       unsigned long mask = live_regs_mask & 0xff;
14208       int next_hi_reg;
14209
14210       /* The available low registers depend on the size of the value we are
14211          returning.  */
14212       if (size <= 12)
14213         mask |=  1 << 3;
14214       if (size <= 8)
14215         mask |= 1 << 2;
14216
14217       if (mask == 0)
14218         /* Oh dear!  We have no low registers into which we can pop
14219            high registers!  */
14220         internal_error
14221           ("no low registers available for popping high registers");
14222
14223       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
14224         if (live_regs_mask & (1 << next_hi_reg))
14225           break;
14226
14227       while (high_regs_pushed)
14228         {
14229           /* Find lo register(s) into which the high register(s) can
14230              be popped.  */
14231           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
14232             {
14233               if (mask & (1 << regno))
14234                 high_regs_pushed--;
14235               if (high_regs_pushed == 0)
14236                 break;
14237             }
14238
14239           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
14240
14241           /* Pop the values into the low register(s).  */
14242           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
14243
14244           /* Move the value(s) into the high registers.  */
14245           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
14246             {
14247               if (mask & (1 << regno))
14248                 {
14249                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
14250                                regno);
14251
14252                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
14253                     if (live_regs_mask & (1 << next_hi_reg))
14254                       break;
14255                 }
14256             }
14257         }
14258       live_regs_mask &= ~0x0f00;
14259     }
14260
14261   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
14262   live_regs_mask &= 0xff;
14263
14264   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
14265     {
14266       /* Pop the return address into the PC.  */
14267       if (had_to_push_lr)
14268         live_regs_mask |= 1 << PC_REGNUM;
14269
14270       /* Either no argument registers were pushed or a backtrace
14271          structure was created which includes an adjusted stack
14272          pointer, so just pop everything.  */
14273       if (live_regs_mask)
14274         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
14275                        live_regs_mask);
14276
14277       /* We have either just popped the return address into the
14278          PC or it is was kept in LR for the entire function.  */
14279       if (!had_to_push_lr)
14280         thumb_exit (asm_out_file, LR_REGNUM);
14281     }
14282   else
14283     {
14284       /* Pop everything but the return address.  */
14285       if (live_regs_mask)
14286         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
14287                        live_regs_mask);
14288
14289       if (had_to_push_lr)
14290         {
14291           if (size > 12)
14292             {
14293               /* We have no free low regs, so save one.  */
14294               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
14295                            LAST_ARG_REGNUM);
14296             }
14297
14298           /* Get the return address into a temporary register.  */
14299           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
14300                          1 << LAST_ARG_REGNUM);
14301
14302           if (size > 12)
14303             {
14304               /* Move the return address to lr.  */
14305               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
14306                            LAST_ARG_REGNUM);
14307               /* Restore the low register.  */
14308               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
14309                            IP_REGNUM);
14310               regno = LR_REGNUM;
14311             }
14312           else
14313             regno = LAST_ARG_REGNUM;
14314         }
14315       else
14316         regno = LR_REGNUM;
14317
14318       /* Remove the argument registers that were pushed onto the stack.  */
14319       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
14320                    SP_REGNUM, SP_REGNUM,
14321                    current_function_pretend_args_size);
14322
14323       thumb_exit (asm_out_file, regno);
14324     }
14325
14326   return "";
14327 }
14328
14329 /* Functions to save and restore machine-specific function data.  */
14330 static struct machine_function *
14331 arm_init_machine_status (void)
14332 {
14333   struct machine_function *machine;
14334   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
14335
14336 #if ARM_FT_UNKNOWN != 0
14337   machine->func_type = ARM_FT_UNKNOWN;
14338 #endif
14339   return machine;
14340 }
14341
14342 /* Return an RTX indicating where the return address to the
14343    calling function can be found.  */
14344 rtx
14345 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
14346 {
14347   if (count != 0)
14348     return NULL_RTX;
14349
14350   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
14351 }
14352
14353 /* Do anything needed before RTL is emitted for each function.  */
14354 void
14355 arm_init_expanders (void)
14356 {
14357   /* Arrange to initialize and mark the machine per-function status.  */
14358   init_machine_status = arm_init_machine_status;
14359
14360   /* This is to stop the combine pass optimizing away the alignment
14361      adjustment of va_arg.  */
14362   /* ??? It is claimed that this should not be necessary.  */
14363   if (cfun)
14364     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
14365 }
14366
14367
14368 /* Like arm_compute_initial_elimination offset.  Simpler because there
14369    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
14370    to point at the base of the local variables after static stack
14371    space for a function has been allocated.  */
14372
14373 HOST_WIDE_INT
14374 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
14375 {
14376   arm_stack_offsets *offsets;
14377
14378   offsets = arm_get_frame_offsets ();
14379
14380   switch (from)
14381     {
14382     case ARG_POINTER_REGNUM:
14383       switch (to)
14384         {
14385         case STACK_POINTER_REGNUM:
14386           return offsets->outgoing_args - offsets->saved_args;
14387
14388         case FRAME_POINTER_REGNUM:
14389           return offsets->soft_frame - offsets->saved_args;
14390
14391         case ARM_HARD_FRAME_POINTER_REGNUM:
14392           return offsets->saved_regs - offsets->saved_args;
14393
14394         case THUMB_HARD_FRAME_POINTER_REGNUM:
14395           return offsets->locals_base - offsets->saved_args;
14396
14397         default:
14398           gcc_unreachable ();
14399         }
14400       break;
14401
14402     case FRAME_POINTER_REGNUM:
14403       switch (to)
14404         {
14405         case STACK_POINTER_REGNUM:
14406           return offsets->outgoing_args - offsets->soft_frame;
14407
14408         case ARM_HARD_FRAME_POINTER_REGNUM:
14409           return offsets->saved_regs - offsets->soft_frame;
14410
14411         case THUMB_HARD_FRAME_POINTER_REGNUM:
14412           return offsets->locals_base - offsets->soft_frame;
14413
14414         default:
14415           gcc_unreachable ();
14416         }
14417       break;
14418
14419     default:
14420       gcc_unreachable ();
14421     }
14422 }
14423
14424 /* Generate the rest of a function's prologue.  */
14425 void
14426 thumb1_expand_prologue (void)
14427 {
14428   rtx insn, dwarf;
14429
14430   HOST_WIDE_INT amount;
14431   arm_stack_offsets *offsets;
14432   unsigned long func_type;
14433   int regno;
14434   unsigned long live_regs_mask;
14435
14436   func_type = arm_current_func_type ();
14437
14438   /* Naked functions don't have prologues.  */
14439   if (IS_NAKED (func_type))
14440     return;
14441
14442   if (IS_INTERRUPT (func_type))
14443     {
14444       error ("interrupt Service Routines cannot be coded in Thumb mode");
14445       return;
14446     }
14447
14448   live_regs_mask = thumb1_compute_save_reg_mask ();
14449   /* Load the pic register before setting the frame pointer,
14450      so we can use r7 as a temporary work register.  */
14451   if (flag_pic && arm_pic_register != INVALID_REGNUM)
14452     arm_load_pic_register (live_regs_mask);
14453
14454   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
14455     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
14456                     stack_pointer_rtx);
14457
14458   offsets = arm_get_frame_offsets ();
14459   amount = offsets->outgoing_args - offsets->saved_regs;
14460   if (amount)
14461     {
14462       if (amount < 512)
14463         {
14464           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14465                                         GEN_INT (- amount)));
14466           RTX_FRAME_RELATED_P (insn) = 1;
14467         }
14468       else
14469         {
14470           rtx reg;
14471
14472           /* The stack decrement is too big for an immediate value in a single
14473              insn.  In theory we could issue multiple subtracts, but after
14474              three of them it becomes more space efficient to place the full
14475              value in the constant pool and load into a register.  (Also the
14476              ARM debugger really likes to see only one stack decrement per
14477              function).  So instead we look for a scratch register into which
14478              we can load the decrement, and then we subtract this from the
14479              stack pointer.  Unfortunately on the thumb the only available
14480              scratch registers are the argument registers, and we cannot use
14481              these as they may hold arguments to the function.  Instead we
14482              attempt to locate a call preserved register which is used by this
14483              function.  If we can find one, then we know that it will have
14484              been pushed at the start of the prologue and so we can corrupt
14485              it now.  */
14486           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
14487             if (live_regs_mask & (1 << regno)
14488                 && !(frame_pointer_needed
14489                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
14490               break;
14491
14492           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
14493             {
14494               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
14495
14496               /* Choose an arbitrary, non-argument low register.  */
14497               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
14498
14499               /* Save it by copying it into a high, scratch register.  */
14500               emit_insn (gen_movsi (spare, reg));
14501               /* Add a USE to stop propagate_one_insn() from barfing.  */
14502               emit_insn (gen_prologue_use (spare));
14503
14504               /* Decrement the stack.  */
14505               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
14506               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
14507                                             stack_pointer_rtx, reg));
14508               RTX_FRAME_RELATED_P (insn) = 1;
14509               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14510                                    plus_constant (stack_pointer_rtx,
14511                                                   -amount));
14512               RTX_FRAME_RELATED_P (dwarf) = 1;
14513               REG_NOTES (insn)
14514                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
14515                                      REG_NOTES (insn));
14516
14517               /* Restore the low register's original value.  */
14518               emit_insn (gen_movsi (reg, spare));
14519
14520               /* Emit a USE of the restored scratch register, so that flow
14521                  analysis will not consider the restore redundant.  The
14522                  register won't be used again in this function and isn't
14523                  restored by the epilogue.  */
14524               emit_insn (gen_prologue_use (reg));
14525             }
14526           else
14527             {
14528               reg = gen_rtx_REG (SImode, regno);
14529
14530               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
14531
14532               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
14533                                             stack_pointer_rtx, reg));
14534               RTX_FRAME_RELATED_P (insn) = 1;
14535               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
14536                                    plus_constant (stack_pointer_rtx,
14537                                                   -amount));
14538               RTX_FRAME_RELATED_P (dwarf) = 1;
14539               REG_NOTES (insn)
14540                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
14541                                      REG_NOTES (insn));
14542             }
14543         }
14544     }
14545
14546   if (frame_pointer_needed)
14547     thumb_set_frame_pointer (offsets);
14548
14549   /* If we are profiling, make sure no instructions are scheduled before
14550      the call to mcount.  Similarly if the user has requested no
14551      scheduling in the prolog.  Similarly if we want non-call exceptions
14552      using the EABI unwinder, to prevent faulting instructions from being
14553      swapped with a stack adjustment.  */
14554   if (current_function_profile || !TARGET_SCHED_PROLOG
14555       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
14556     emit_insn (gen_blockage ());
14557
14558   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
14559   if (live_regs_mask & 0xff)
14560     cfun->machine->lr_save_eliminated = 0;
14561
14562   /* If the link register is being kept alive, with the return address in it,
14563      then make sure that it does not get reused by the ce2 pass.  */
14564   if (cfun->machine->lr_save_eliminated)
14565     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
14566 }
14567
14568
14569 void
14570 thumb1_expand_epilogue (void)
14571 {
14572   HOST_WIDE_INT amount;
14573   arm_stack_offsets *offsets;
14574   int regno;
14575
14576   /* Naked functions don't have prologues.  */
14577   if (IS_NAKED (arm_current_func_type ()))
14578     return;
14579
14580   offsets = arm_get_frame_offsets ();
14581   amount = offsets->outgoing_args - offsets->saved_regs;
14582
14583   if (frame_pointer_needed)
14584     {
14585       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
14586       amount = offsets->locals_base - offsets->saved_regs;
14587     }
14588
14589   if (amount)
14590     {
14591       if (amount < 512)
14592         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
14593                                GEN_INT (amount)));
14594       else
14595         {
14596           /* r3 is always free in the epilogue.  */
14597           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
14598
14599           emit_insn (gen_movsi (reg, GEN_INT (amount)));
14600           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
14601         }
14602     }
14603
14604   /* Emit a USE (stack_pointer_rtx), so that
14605      the stack adjustment will not be deleted.  */
14606   emit_insn (gen_prologue_use (stack_pointer_rtx));
14607
14608   if (current_function_profile || !TARGET_SCHED_PROLOG)
14609     emit_insn (gen_blockage ());
14610
14611   /* Emit a clobber for each insn that will be restored in the epilogue,
14612      so that flow2 will get register lifetimes correct.  */
14613   for (regno = 0; regno < 13; regno++)
14614     if (regs_ever_live[regno] && !call_used_regs[regno])
14615       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
14616
14617   if (! regs_ever_live[LR_REGNUM])
14618     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
14619 }
14620
14621 static void
14622 thumb1_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
14623 {
14624   unsigned long live_regs_mask = 0;
14625   unsigned long l_mask;
14626   unsigned high_regs_pushed = 0;
14627   int cfa_offset = 0;
14628   int regno;
14629
14630   if (IS_NAKED (arm_current_func_type ()))
14631     return;
14632
14633   if (is_called_in_ARM_mode (current_function_decl))
14634     {
14635       const char * name;
14636
14637       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
14638       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
14639                   == SYMBOL_REF);
14640       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
14641
14642       /* Generate code sequence to switch us into Thumb mode.  */
14643       /* The .code 32 directive has already been emitted by
14644          ASM_DECLARE_FUNCTION_NAME.  */
14645       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
14646       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
14647
14648       /* Generate a label, so that the debugger will notice the
14649          change in instruction sets.  This label is also used by
14650          the assembler to bypass the ARM code when this function
14651          is called from a Thumb encoded function elsewhere in the
14652          same file.  Hence the definition of STUB_NAME here must
14653          agree with the definition in gas/config/tc-arm.c.  */
14654
14655 #define STUB_NAME ".real_start_of"
14656
14657       fprintf (f, "\t.code\t16\n");
14658 #ifdef ARM_PE
14659       if (arm_dllexport_name_p (name))
14660         name = arm_strip_name_encoding (name);
14661 #endif
14662       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
14663       fprintf (f, "\t.thumb_func\n");
14664       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
14665     }
14666
14667   if (current_function_pretend_args_size)
14668     {
14669       /* Output unwind directive for the stack adjustment.  */
14670       if (ARM_EABI_UNWIND_TABLES)
14671         fprintf (f, "\t.pad #%d\n",
14672                  current_function_pretend_args_size);
14673
14674       if (cfun->machine->uses_anonymous_args)
14675         {
14676           int num_pushes;
14677
14678           fprintf (f, "\tpush\t{");
14679
14680           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
14681
14682           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
14683                regno <= LAST_ARG_REGNUM;
14684                regno++)
14685             asm_fprintf (f, "%r%s", regno,
14686                          regno == LAST_ARG_REGNUM ? "" : ", ");
14687
14688           fprintf (f, "}\n");
14689         }
14690       else
14691         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
14692                      SP_REGNUM, SP_REGNUM,
14693                      current_function_pretend_args_size);
14694
14695       /* We don't need to record the stores for unwinding (would it
14696          help the debugger any if we did?), but record the change in
14697          the stack pointer.  */
14698       if (dwarf2out_do_frame ())
14699         {
14700           char *l = dwarf2out_cfi_label ();
14701
14702           cfa_offset = cfa_offset + current_function_pretend_args_size;
14703           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
14704         }
14705     }
14706
14707   /* Get the registers we are going to push.  */
14708   live_regs_mask = thumb1_compute_save_reg_mask ();
14709   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
14710   l_mask = live_regs_mask & 0x40ff;
14711   /* Then count how many other high registers will need to be pushed.  */
14712   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
14713
14714   if (TARGET_BACKTRACE)
14715     {
14716       unsigned offset;
14717       unsigned work_register;
14718
14719       /* We have been asked to create a stack backtrace structure.
14720          The code looks like this:
14721
14722          0   .align 2
14723          0   func:
14724          0     sub   SP, #16         Reserve space for 4 registers.
14725          2     push  {R7}            Push low registers.
14726          4     add   R7, SP, #20     Get the stack pointer before the push.
14727          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
14728          8     mov   R7, PC          Get hold of the start of this code plus 12.
14729         10     str   R7, [SP, #16]   Store it.
14730         12     mov   R7, FP          Get hold of the current frame pointer.
14731         14     str   R7, [SP, #4]    Store it.
14732         16     mov   R7, LR          Get hold of the current return address.
14733         18     str   R7, [SP, #12]   Store it.
14734         20     add   R7, SP, #16     Point at the start of the backtrace structure.
14735         22     mov   FP, R7          Put this value into the frame pointer.  */
14736
14737       work_register = thumb_find_work_register (live_regs_mask);
14738
14739       if (ARM_EABI_UNWIND_TABLES)
14740         asm_fprintf (f, "\t.pad #16\n");
14741
14742       asm_fprintf
14743         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
14744          SP_REGNUM, SP_REGNUM);
14745
14746       if (dwarf2out_do_frame ())
14747         {
14748           char *l = dwarf2out_cfi_label ();
14749
14750           cfa_offset = cfa_offset + 16;
14751           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
14752         }
14753
14754       if (l_mask)
14755         {
14756           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14757           offset = bit_count (l_mask) * UNITS_PER_WORD;
14758         }
14759       else
14760         offset = 0;
14761
14762       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14763                    offset + 16 + current_function_pretend_args_size);
14764
14765       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14766                    offset + 4);
14767
14768       /* Make sure that the instruction fetching the PC is in the right place
14769          to calculate "start of backtrace creation code + 12".  */
14770       if (l_mask)
14771         {
14772           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14773           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14774                        offset + 12);
14775           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14776                        ARM_HARD_FRAME_POINTER_REGNUM);
14777           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14778                        offset);
14779         }
14780       else
14781         {
14782           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
14783                        ARM_HARD_FRAME_POINTER_REGNUM);
14784           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14785                        offset);
14786           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
14787           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14788                        offset + 12);
14789         }
14790
14791       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
14792       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
14793                    offset + 8);
14794       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14795                    offset + 12);
14796       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
14797                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
14798     }
14799   /* Optimization:  If we are not pushing any low registers but we are going
14800      to push some high registers then delay our first push.  This will just
14801      be a push of LR and we can combine it with the push of the first high
14802      register.  */
14803   else if ((l_mask & 0xff) != 0
14804            || (high_regs_pushed == 0 && l_mask))
14805     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14806
14807   if (high_regs_pushed)
14808     {
14809       unsigned pushable_regs;
14810       unsigned next_hi_reg;
14811
14812       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
14813         if (live_regs_mask & (1 << next_hi_reg))
14814           break;
14815
14816       pushable_regs = l_mask & 0xff;
14817
14818       if (pushable_regs == 0)
14819         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
14820
14821       while (high_regs_pushed > 0)
14822         {
14823           unsigned long real_regs_mask = 0;
14824
14825           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14826             {
14827               if (pushable_regs & (1 << regno))
14828                 {
14829                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14830
14831                   high_regs_pushed --;
14832                   real_regs_mask |= (1 << next_hi_reg);
14833
14834                   if (high_regs_pushed)
14835                     {
14836                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14837                            next_hi_reg --)
14838                         if (live_regs_mask & (1 << next_hi_reg))
14839                           break;
14840                     }
14841                   else
14842                     {
14843                       pushable_regs &= ~((1 << regno) - 1);
14844                       break;
14845                     }
14846                 }
14847             }
14848
14849           /* If we had to find a work register and we have not yet
14850              saved the LR then add it to the list of regs to push.  */
14851           if (l_mask == (1 << LR_REGNUM))
14852             {
14853               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14854                              1, &cfa_offset,
14855                              real_regs_mask | (1 << LR_REGNUM));
14856               l_mask = 0;
14857             }
14858           else
14859             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14860         }
14861     }
14862 }
14863
14864 /* Handle the case of a double word load into a low register from
14865    a computed memory address.  The computed address may involve a
14866    register which is overwritten by the load.  */
14867 const char *
14868 thumb_load_double_from_address (rtx *operands)
14869 {
14870   rtx addr;
14871   rtx base;
14872   rtx offset;
14873   rtx arg1;
14874   rtx arg2;
14875
14876   gcc_assert (GET_CODE (operands[0]) == REG);
14877   gcc_assert (GET_CODE (operands[1]) == MEM);
14878
14879   /* Get the memory address.  */
14880   addr = XEXP (operands[1], 0);
14881
14882   /* Work out how the memory address is computed.  */
14883   switch (GET_CODE (addr))
14884     {
14885     case REG:
14886       operands[2] = adjust_address (operands[1], SImode, 4);
14887
14888       if (REGNO (operands[0]) == REGNO (addr))
14889         {
14890           output_asm_insn ("ldr\t%H0, %2", operands);
14891           output_asm_insn ("ldr\t%0, %1", operands);
14892         }
14893       else
14894         {
14895           output_asm_insn ("ldr\t%0, %1", operands);
14896           output_asm_insn ("ldr\t%H0, %2", operands);
14897         }
14898       break;
14899
14900     case CONST:
14901       /* Compute <address> + 4 for the high order load.  */
14902       operands[2] = adjust_address (operands[1], SImode, 4);
14903
14904       output_asm_insn ("ldr\t%0, %1", operands);
14905       output_asm_insn ("ldr\t%H0, %2", operands);
14906       break;
14907
14908     case PLUS:
14909       arg1   = XEXP (addr, 0);
14910       arg2   = XEXP (addr, 1);
14911
14912       if (CONSTANT_P (arg1))
14913         base = arg2, offset = arg1;
14914       else
14915         base = arg1, offset = arg2;
14916
14917       gcc_assert (GET_CODE (base) == REG);
14918
14919       /* Catch the case of <address> = <reg> + <reg> */
14920       if (GET_CODE (offset) == REG)
14921         {
14922           int reg_offset = REGNO (offset);
14923           int reg_base   = REGNO (base);
14924           int reg_dest   = REGNO (operands[0]);
14925
14926           /* Add the base and offset registers together into the
14927              higher destination register.  */
14928           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14929                        reg_dest + 1, reg_base, reg_offset);
14930
14931           /* Load the lower destination register from the address in
14932              the higher destination register.  */
14933           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14934                        reg_dest, reg_dest + 1);
14935
14936           /* Load the higher destination register from its own address
14937              plus 4.  */
14938           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14939                        reg_dest + 1, reg_dest + 1);
14940         }
14941       else
14942         {
14943           /* Compute <address> + 4 for the high order load.  */
14944           operands[2] = adjust_address (operands[1], SImode, 4);
14945
14946           /* If the computed address is held in the low order register
14947              then load the high order register first, otherwise always
14948              load the low order register first.  */
14949           if (REGNO (operands[0]) == REGNO (base))
14950             {
14951               output_asm_insn ("ldr\t%H0, %2", operands);
14952               output_asm_insn ("ldr\t%0, %1", operands);
14953             }
14954           else
14955             {
14956               output_asm_insn ("ldr\t%0, %1", operands);
14957               output_asm_insn ("ldr\t%H0, %2", operands);
14958             }
14959         }
14960       break;
14961
14962     case LABEL_REF:
14963       /* With no registers to worry about we can just load the value
14964          directly.  */
14965       operands[2] = adjust_address (operands[1], SImode, 4);
14966
14967       output_asm_insn ("ldr\t%H0, %2", operands);
14968       output_asm_insn ("ldr\t%0, %1", operands);
14969       break;
14970
14971     default:
14972       gcc_unreachable ();
14973     }
14974
14975   return "";
14976 }
14977
14978 const char *
14979 thumb_output_move_mem_multiple (int n, rtx *operands)
14980 {
14981   rtx tmp;
14982
14983   switch (n)
14984     {
14985     case 2:
14986       if (REGNO (operands[4]) > REGNO (operands[5]))
14987         {
14988           tmp = operands[4];
14989           operands[4] = operands[5];
14990           operands[5] = tmp;
14991         }
14992       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14993       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14994       break;
14995
14996     case 3:
14997       if (REGNO (operands[4]) > REGNO (operands[5]))
14998         {
14999           tmp = operands[4];
15000           operands[4] = operands[5];
15001           operands[5] = tmp;
15002         }
15003       if (REGNO (operands[5]) > REGNO (operands[6]))
15004         {
15005           tmp = operands[5];
15006           operands[5] = operands[6];
15007           operands[6] = tmp;
15008         }
15009       if (REGNO (operands[4]) > REGNO (operands[5]))
15010         {
15011           tmp = operands[4];
15012           operands[4] = operands[5];
15013           operands[5] = tmp;
15014         }
15015
15016       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
15017       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
15018       break;
15019
15020     default:
15021       gcc_unreachable ();
15022     }
15023
15024   return "";
15025 }
15026
15027 /* Output a call-via instruction for thumb state.  */
15028 const char *
15029 thumb_call_via_reg (rtx reg)
15030 {
15031   int regno = REGNO (reg);
15032   rtx *labelp;
15033
15034   gcc_assert (regno < LR_REGNUM);
15035
15036   /* If we are in the normal text section we can use a single instance
15037      per compilation unit.  If we are doing function sections, then we need
15038      an entry per section, since we can't rely on reachability.  */
15039   if (in_section == text_section)
15040     {
15041       thumb_call_reg_needed = 1;
15042
15043       if (thumb_call_via_label[regno] == NULL)
15044         thumb_call_via_label[regno] = gen_label_rtx ();
15045       labelp = thumb_call_via_label + regno;
15046     }
15047   else
15048     {
15049       if (cfun->machine->call_via[regno] == NULL)
15050         cfun->machine->call_via[regno] = gen_label_rtx ();
15051       labelp = cfun->machine->call_via + regno;
15052     }
15053
15054   output_asm_insn ("bl\t%a0", labelp);
15055   return "";
15056 }
15057
15058 /* Routines for generating rtl.  */
15059 void
15060 thumb_expand_movmemqi (rtx *operands)
15061 {
15062   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
15063   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
15064   HOST_WIDE_INT len = INTVAL (operands[2]);
15065   HOST_WIDE_INT offset = 0;
15066
15067   while (len >= 12)
15068     {
15069       emit_insn (gen_movmem12b (out, in, out, in));
15070       len -= 12;
15071     }
15072
15073   if (len >= 8)
15074     {
15075       emit_insn (gen_movmem8b (out, in, out, in));
15076       len -= 8;
15077     }
15078
15079   if (len >= 4)
15080     {
15081       rtx reg = gen_reg_rtx (SImode);
15082       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
15083       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
15084       len -= 4;
15085       offset += 4;
15086     }
15087
15088   if (len >= 2)
15089     {
15090       rtx reg = gen_reg_rtx (HImode);
15091       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
15092                                               plus_constant (in, offset))));
15093       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
15094                             reg));
15095       len -= 2;
15096       offset += 2;
15097     }
15098
15099   if (len)
15100     {
15101       rtx reg = gen_reg_rtx (QImode);
15102       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
15103                                               plus_constant (in, offset))));
15104       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
15105                             reg));
15106     }
15107 }
15108
15109 void
15110 thumb_reload_out_hi (rtx *operands)
15111 {
15112   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
15113 }
15114
15115 /* Handle reading a half-word from memory during reload.  */
15116 void
15117 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
15118 {
15119   gcc_unreachable ();
15120 }
15121
15122 /* Return the length of a function name prefix
15123     that starts with the character 'c'.  */
15124 static int
15125 arm_get_strip_length (int c)
15126 {
15127   switch (c)
15128     {
15129     ARM_NAME_ENCODING_LENGTHS
15130       default: return 0;
15131     }
15132 }
15133
15134 /* Return a pointer to a function's name with any
15135    and all prefix encodings stripped from it.  */
15136 const char *
15137 arm_strip_name_encoding (const char *name)
15138 {
15139   int skip;
15140
15141   while ((skip = arm_get_strip_length (* name)))
15142     name += skip;
15143
15144   return name;
15145 }
15146
15147 /* If there is a '*' anywhere in the name's prefix, then
15148    emit the stripped name verbatim, otherwise prepend an
15149    underscore if leading underscores are being used.  */
15150 void
15151 arm_asm_output_labelref (FILE *stream, const char *name)
15152 {
15153   int skip;
15154   int verbatim = 0;
15155
15156   while ((skip = arm_get_strip_length (* name)))
15157     {
15158       verbatim |= (*name == '*');
15159       name += skip;
15160     }
15161
15162   if (verbatim)
15163     fputs (name, stream);
15164   else
15165     asm_fprintf (stream, "%U%s", name);
15166 }
15167
15168 static void
15169 arm_file_start (void)
15170 {
15171   int val;
15172
15173   if (TARGET_UNIFIED_ASM)
15174     asm_fprintf (asm_out_file, "\t.syntax unified\n");
15175
15176   if (TARGET_BPABI)
15177     {
15178       const char *fpu_name;
15179       if (arm_select[0].string)
15180         asm_fprintf (asm_out_file, "\t.cpu %s\n", arm_select[0].string);
15181       else if (arm_select[1].string)
15182         asm_fprintf (asm_out_file, "\t.arch %s\n", arm_select[1].string);
15183       else
15184         asm_fprintf (asm_out_file, "\t.cpu %s\n",
15185                      all_cores[arm_default_cpu].name);
15186
15187       if (TARGET_SOFT_FLOAT)
15188         {
15189           if (TARGET_VFP)
15190             fpu_name = "softvfp";
15191           else
15192             fpu_name = "softfpa";
15193         }
15194       else
15195         {
15196           switch (arm_fpu_arch)
15197             {
15198             case FPUTYPE_FPA:
15199               fpu_name = "fpa";
15200               break;
15201             case FPUTYPE_FPA_EMU2:
15202               fpu_name = "fpe2";
15203               break;
15204             case FPUTYPE_FPA_EMU3:
15205               fpu_name = "fpe3";
15206               break;
15207             case FPUTYPE_MAVERICK:
15208               fpu_name = "maverick";
15209               break;
15210             case FPUTYPE_VFP:
15211               if (TARGET_HARD_FLOAT)
15212                 asm_fprintf (asm_out_file, "\t.eabi_attribute 27, 3\n");
15213               if (TARGET_HARD_FLOAT_ABI)
15214                 asm_fprintf (asm_out_file, "\t.eabi_attribute 28, 1\n");
15215               fpu_name = "vfp";
15216               break;
15217             default:
15218               abort();
15219             }
15220         }
15221       asm_fprintf (asm_out_file, "\t.fpu %s\n", fpu_name);
15222
15223       /* Some of these attributes only apply when the corresponding features
15224          are used.  However we don't have any easy way of figuring this out.
15225          Conservatively record the setting that would have been used.  */
15226
15227       /* Tag_ABI_PCS_wchar_t.  */
15228       asm_fprintf (asm_out_file, "\t.eabi_attribute 18, %d\n",
15229                    (int)WCHAR_TYPE_SIZE / BITS_PER_UNIT);
15230
15231       /* Tag_ABI_FP_rounding.  */
15232       if (flag_rounding_math)
15233         asm_fprintf (asm_out_file, "\t.eabi_attribute 19, 1\n");
15234       if (!flag_unsafe_math_optimizations)
15235         {
15236           /* Tag_ABI_FP_denomal.  */
15237           asm_fprintf (asm_out_file, "\t.eabi_attribute 20, 1\n");
15238           /* Tag_ABI_FP_exceptions.  */
15239           asm_fprintf (asm_out_file, "\t.eabi_attribute 21, 1\n");
15240         }
15241       /* Tag_ABI_FP_user_exceptions.  */
15242       if (flag_signaling_nans)
15243         asm_fprintf (asm_out_file, "\t.eabi_attribute 22, 1\n");
15244       /* Tag_ABI_FP_number_model.  */
15245       asm_fprintf (asm_out_file, "\t.eabi_attribute 23, %d\n", 
15246                    flag_finite_math_only ? 1 : 3);
15247
15248       /* Tag_ABI_align8_needed.  */
15249       asm_fprintf (asm_out_file, "\t.eabi_attribute 24, 1\n");
15250       /* Tag_ABI_align8_preserved.  */
15251       asm_fprintf (asm_out_file, "\t.eabi_attribute 25, 1\n");
15252       /* Tag_ABI_enum_size.  */
15253       asm_fprintf (asm_out_file, "\t.eabi_attribute 26, %d\n",
15254                    flag_short_enums ? 1 : 2);
15255
15256       /* Tag_ABI_optimization_goals.  */
15257       if (optimize_size)
15258         val = 4;
15259       else if (optimize >= 2)
15260         val = 2;
15261       else if (optimize)
15262         val = 1;
15263       else
15264         val = 6;
15265       asm_fprintf (asm_out_file, "\t.eabi_attribute 30, %d\n", val);
15266     }
15267   default_file_start();
15268 }
15269
15270 static void
15271 arm_file_end (void)
15272 {
15273   int regno;
15274
15275   if (NEED_INDICATE_EXEC_STACK)
15276     /* Add .note.GNU-stack.  */
15277     file_end_indicate_exec_stack ();
15278
15279   if (! thumb_call_reg_needed)
15280     return;
15281
15282   switch_to_section (text_section);
15283   asm_fprintf (asm_out_file, "\t.code 16\n");
15284   ASM_OUTPUT_ALIGN (asm_out_file, 1);
15285
15286   for (regno = 0; regno < LR_REGNUM; regno++)
15287     {
15288       rtx label = thumb_call_via_label[regno];
15289
15290       if (label != 0)
15291         {
15292           targetm.asm_out.internal_label (asm_out_file, "L",
15293                                           CODE_LABEL_NUMBER (label));
15294           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
15295         }
15296     }
15297 }
15298
15299 rtx aof_pic_label;
15300
15301 #ifdef AOF_ASSEMBLER
15302 /* Special functions only needed when producing AOF syntax assembler.  */
15303
15304 struct pic_chain
15305 {
15306   struct pic_chain * next;
15307   const char * symname;
15308 };
15309
15310 static struct pic_chain * aof_pic_chain = NULL;
15311
15312 rtx
15313 aof_pic_entry (rtx x)
15314 {
15315   struct pic_chain ** chainp;
15316   int offset;
15317
15318   if (aof_pic_label == NULL_RTX)
15319     {
15320       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
15321     }
15322
15323   for (offset = 0, chainp = &aof_pic_chain; *chainp;
15324        offset += 4, chainp = &(*chainp)->next)
15325     if ((*chainp)->symname == XSTR (x, 0))
15326       return plus_constant (aof_pic_label, offset);
15327
15328   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
15329   (*chainp)->next = NULL;
15330   (*chainp)->symname = XSTR (x, 0);
15331   return plus_constant (aof_pic_label, offset);
15332 }
15333
15334 void
15335 aof_dump_pic_table (FILE *f)
15336 {
15337   struct pic_chain * chain;
15338
15339   if (aof_pic_chain == NULL)
15340     return;
15341
15342   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
15343                PIC_OFFSET_TABLE_REGNUM,
15344                PIC_OFFSET_TABLE_REGNUM);
15345   fputs ("|x$adcons|\n", f);
15346
15347   for (chain = aof_pic_chain; chain; chain = chain->next)
15348     {
15349       fputs ("\tDCD\t", f);
15350       assemble_name (f, chain->symname);
15351       fputs ("\n", f);
15352     }
15353 }
15354
15355 int arm_text_section_count = 1;
15356
15357 /* A get_unnamed_section callback for switching to the text section.  */
15358
15359 static void
15360 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
15361 {
15362   fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
15363            arm_text_section_count++);
15364   if (flag_pic)
15365     fprintf (asm_out_file, ", PIC, REENTRANT");
15366   fprintf (asm_out_file, "\n");
15367 }
15368
15369 static int arm_data_section_count = 1;
15370
15371 /* A get_unnamed_section callback for switching to the data section.  */
15372
15373 static void
15374 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
15375 {
15376   fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
15377            arm_data_section_count++);
15378 }
15379
15380 /* Implement TARGET_ASM_INIT_SECTIONS.
15381
15382    AOF Assembler syntax is a nightmare when it comes to areas, since once
15383    we change from one area to another, we can't go back again.  Instead,
15384    we must create a new area with the same attributes and add the new output
15385    to that.  Unfortunately, there is nothing we can do here to guarantee that
15386    two areas with the same attributes will be linked adjacently in the
15387    resulting executable, so we have to be careful not to do pc-relative
15388    addressing across such boundaries.  */
15389
15390 static void
15391 aof_asm_init_sections (void)
15392 {
15393   text_section = get_unnamed_section (SECTION_CODE,
15394                                       aof_output_text_section_asm_op, NULL);
15395   data_section = get_unnamed_section (SECTION_WRITE,
15396                                       aof_output_data_section_asm_op, NULL);
15397   readonly_data_section = text_section;
15398 }
15399
15400 void
15401 zero_init_section (void)
15402 {
15403   static int zero_init_count = 1;
15404
15405   fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
15406   in_section = NULL;
15407 }
15408
15409 /* The AOF assembler is religiously strict about declarations of
15410    imported and exported symbols, so that it is impossible to declare
15411    a function as imported near the beginning of the file, and then to
15412    export it later on.  It is, however, possible to delay the decision
15413    until all the functions in the file have been compiled.  To get
15414    around this, we maintain a list of the imports and exports, and
15415    delete from it any that are subsequently defined.  At the end of
15416    compilation we spit the remainder of the list out before the END
15417    directive.  */
15418
15419 struct import
15420 {
15421   struct import * next;
15422   const char * name;
15423 };
15424
15425 static struct import * imports_list = NULL;
15426
15427 void
15428 aof_add_import (const char *name)
15429 {
15430   struct import * new;
15431
15432   for (new = imports_list; new; new = new->next)
15433     if (new->name == name)
15434       return;
15435
15436   new = (struct import *) xmalloc (sizeof (struct import));
15437   new->next = imports_list;
15438   imports_list = new;
15439   new->name = name;
15440 }
15441
15442 void
15443 aof_delete_import (const char *name)
15444 {
15445   struct import ** old;
15446
15447   for (old = &imports_list; *old; old = & (*old)->next)
15448     {
15449       if ((*old)->name == name)
15450         {
15451           *old = (*old)->next;
15452           return;
15453         }
15454     }
15455 }
15456
15457 int arm_main_function = 0;
15458
15459 static void
15460 aof_dump_imports (FILE *f)
15461 {
15462   /* The AOF assembler needs this to cause the startup code to be extracted
15463      from the library.  Brining in __main causes the whole thing to work
15464      automagically.  */
15465   if (arm_main_function)
15466     {
15467       switch_to_section (text_section);
15468       fputs ("\tIMPORT __main\n", f);
15469       fputs ("\tDCD __main\n", f);
15470     }
15471
15472   /* Now dump the remaining imports.  */
15473   while (imports_list)
15474     {
15475       fprintf (f, "\tIMPORT\t");
15476       assemble_name (f, imports_list->name);
15477       fputc ('\n', f);
15478       imports_list = imports_list->next;
15479     }
15480 }
15481
15482 static void
15483 aof_globalize_label (FILE *stream, const char *name)
15484 {
15485   default_globalize_label (stream, name);
15486   if (! strcmp (name, "main"))
15487     arm_main_function = 1;
15488 }
15489
15490 static void
15491 aof_file_start (void)
15492 {
15493   fputs ("__r0\tRN\t0\n", asm_out_file);
15494   fputs ("__a1\tRN\t0\n", asm_out_file);
15495   fputs ("__a2\tRN\t1\n", asm_out_file);
15496   fputs ("__a3\tRN\t2\n", asm_out_file);
15497   fputs ("__a4\tRN\t3\n", asm_out_file);
15498   fputs ("__v1\tRN\t4\n", asm_out_file);
15499   fputs ("__v2\tRN\t5\n", asm_out_file);
15500   fputs ("__v3\tRN\t6\n", asm_out_file);
15501   fputs ("__v4\tRN\t7\n", asm_out_file);
15502   fputs ("__v5\tRN\t8\n", asm_out_file);
15503   fputs ("__v6\tRN\t9\n", asm_out_file);
15504   fputs ("__sl\tRN\t10\n", asm_out_file);
15505   fputs ("__fp\tRN\t11\n", asm_out_file);
15506   fputs ("__ip\tRN\t12\n", asm_out_file);
15507   fputs ("__sp\tRN\t13\n", asm_out_file);
15508   fputs ("__lr\tRN\t14\n", asm_out_file);
15509   fputs ("__pc\tRN\t15\n", asm_out_file);
15510   fputs ("__f0\tFN\t0\n", asm_out_file);
15511   fputs ("__f1\tFN\t1\n", asm_out_file);
15512   fputs ("__f2\tFN\t2\n", asm_out_file);
15513   fputs ("__f3\tFN\t3\n", asm_out_file);
15514   fputs ("__f4\tFN\t4\n", asm_out_file);
15515   fputs ("__f5\tFN\t5\n", asm_out_file);
15516   fputs ("__f6\tFN\t6\n", asm_out_file);
15517   fputs ("__f7\tFN\t7\n", asm_out_file);
15518   switch_to_section (text_section);
15519 }
15520
15521 static void
15522 aof_file_end (void)
15523 {
15524   if (flag_pic)
15525     aof_dump_pic_table (asm_out_file);
15526   arm_file_end ();
15527   aof_dump_imports (asm_out_file);
15528   fputs ("\tEND\n", asm_out_file);
15529 }
15530 #endif /* AOF_ASSEMBLER */
15531
15532 #ifndef ARM_PE
15533 /* Symbols in the text segment can be accessed without indirecting via the
15534    constant pool; it may take an extra binary operation, but this is still
15535    faster than indirecting via memory.  Don't do this when not optimizing,
15536    since we won't be calculating al of the offsets necessary to do this
15537    simplification.  */
15538
15539 static void
15540 arm_encode_section_info (tree decl, rtx rtl, int first)
15541 {
15542   /* This doesn't work with AOF syntax, since the string table may be in
15543      a different AREA.  */
15544 #ifndef AOF_ASSEMBLER
15545   if (optimize > 0 && TREE_CONSTANT (decl))
15546     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
15547 #endif
15548
15549   default_encode_section_info (decl, rtl, first);
15550 }
15551 #endif /* !ARM_PE */
15552
15553 static void
15554 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
15555 {
15556   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
15557       && !strcmp (prefix, "L"))
15558     {
15559       arm_ccfsm_state = 0;
15560       arm_target_insn = NULL;
15561     }
15562   default_internal_label (stream, prefix, labelno);
15563 }
15564
15565 /* Output code to add DELTA to the first argument, and then jump
15566    to FUNCTION.  Used for C++ multiple inheritance.  */
15567 static void
15568 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
15569                      HOST_WIDE_INT delta,
15570                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
15571                      tree function)
15572 {
15573   static int thunk_label = 0;
15574   char label[256];
15575   char labelpc[256];
15576   int mi_delta = delta;
15577   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
15578   int shift = 0;
15579   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
15580                     ? 1 : 0);
15581   if (mi_delta < 0)
15582     mi_delta = - mi_delta;
15583   /* When generating 16-bit thumb code, thunks are entered in arm mode.  */
15584   if (TARGET_THUMB1)
15585     {
15586       int labelno = thunk_label++;
15587       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
15588       fputs ("\tldr\tr12, ", file);
15589       assemble_name (file, label);
15590       fputc ('\n', file);
15591       if (flag_pic)
15592         {
15593           /* If we are generating PIC, the ldr instruction below loads
15594              "(target - 7) - .LTHUNKPCn" into r12.  The pc reads as
15595              the address of the add + 8, so we have:
15596
15597              r12 = (target - 7) - .LTHUNKPCn + (.LTHUNKPCn + 8)
15598                  = target + 1.
15599
15600              Note that we have "+ 1" because some versions of GNU ld
15601              don't set the low bit of the result for R_ARM_REL32
15602              relocations against thumb function symbols.  */
15603           ASM_GENERATE_INTERNAL_LABEL (labelpc, "LTHUNKPC", labelno);
15604           assemble_name (file, labelpc);
15605           fputs (":\n", file);
15606           fputs ("\tadd\tr12, pc, r12\n", file);
15607         }
15608     }
15609   /* TODO: Use movw/movt for large constants when available.  */
15610   while (mi_delta != 0)
15611     {
15612       if ((mi_delta & (3 << shift)) == 0)
15613         shift += 2;
15614       else
15615         {
15616           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
15617                        mi_op, this_regno, this_regno,
15618                        mi_delta & (0xff << shift));
15619           mi_delta &= ~(0xff << shift);
15620           shift += 8;
15621         }
15622     }
15623   if (TARGET_THUMB1)
15624     {
15625       fprintf (file, "\tbx\tr12\n");
15626       ASM_OUTPUT_ALIGN (file, 2);
15627       assemble_name (file, label);
15628       fputs (":\n", file);
15629       if (flag_pic)
15630         {
15631           /* Output ".word .LTHUNKn-7-.LTHUNKPCn".  */
15632           rtx tem = XEXP (DECL_RTL (function), 0);
15633           tem = gen_rtx_PLUS (GET_MODE (tem), tem, GEN_INT (-7));
15634           tem = gen_rtx_MINUS (GET_MODE (tem),
15635                                tem,
15636                                gen_rtx_SYMBOL_REF (Pmode,
15637                                                    ggc_strdup (labelpc)));
15638           assemble_integer (tem, 4, BITS_PER_WORD, 1);
15639         }
15640       else
15641         /* Output ".word .LTHUNKn".  */
15642         assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
15643     }
15644   else
15645     {
15646       fputs ("\tb\t", file);
15647       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
15648       if (NEED_PLT_RELOC)
15649         fputs ("(PLT)", file);
15650       fputc ('\n', file);
15651     }
15652 }
15653
15654 int
15655 arm_emit_vector_const (FILE *file, rtx x)
15656 {
15657   int i;
15658   const char * pattern;
15659
15660   gcc_assert (GET_CODE (x) == CONST_VECTOR);
15661
15662   switch (GET_MODE (x))
15663     {
15664     case V2SImode: pattern = "%08x"; break;
15665     case V4HImode: pattern = "%04x"; break;
15666     case V8QImode: pattern = "%02x"; break;
15667     default:       gcc_unreachable ();
15668     }
15669
15670   fprintf (file, "0x");
15671   for (i = CONST_VECTOR_NUNITS (x); i--;)
15672     {
15673       rtx element;
15674
15675       element = CONST_VECTOR_ELT (x, i);
15676       fprintf (file, pattern, INTVAL (element));
15677     }
15678
15679   return 1;
15680 }
15681
15682 const char *
15683 arm_output_load_gr (rtx *operands)
15684 {
15685   rtx reg;
15686   rtx offset;
15687   rtx wcgr;
15688   rtx sum;
15689
15690   if (GET_CODE (operands [1]) != MEM
15691       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
15692       || GET_CODE (reg = XEXP (sum, 0)) != REG
15693       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
15694       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
15695     return "wldrw%?\t%0, %1";
15696
15697   /* Fix up an out-of-range load of a GR register.  */
15698   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
15699   wcgr = operands[0];
15700   operands[0] = reg;
15701   output_asm_insn ("ldr%?\t%0, %1", operands);
15702
15703   operands[0] = wcgr;
15704   operands[1] = reg;
15705   output_asm_insn ("tmcr%?\t%0, %1", operands);
15706   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
15707
15708   return "";
15709 }
15710
15711 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
15712
15713    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
15714    named arg and all anonymous args onto the stack.
15715    XXX I know the prologue shouldn't be pushing registers, but it is faster
15716    that way.  */
15717
15718 static void
15719 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
15720                             enum machine_mode mode ATTRIBUTE_UNUSED,
15721                             tree type ATTRIBUTE_UNUSED,
15722                             int *pretend_size,
15723                             int second_time ATTRIBUTE_UNUSED)
15724 {
15725   cfun->machine->uses_anonymous_args = 1;
15726   if (cum->nregs < NUM_ARG_REGS)
15727     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
15728 }
15729
15730 /* Return nonzero if the CONSUMER instruction (a store) does not need
15731    PRODUCER's value to calculate the address.  */
15732
15733 int
15734 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
15735 {
15736   rtx value = PATTERN (producer);
15737   rtx addr = PATTERN (consumer);
15738
15739   if (GET_CODE (value) == COND_EXEC)
15740     value = COND_EXEC_CODE (value);
15741   if (GET_CODE (value) == PARALLEL)
15742     value = XVECEXP (value, 0, 0);
15743   value = XEXP (value, 0);
15744   if (GET_CODE (addr) == COND_EXEC)
15745     addr = COND_EXEC_CODE (addr);
15746   if (GET_CODE (addr) == PARALLEL)
15747     addr = XVECEXP (addr, 0, 0);
15748   addr = XEXP (addr, 0);
15749
15750   return !reg_overlap_mentioned_p (value, addr);
15751 }
15752
15753 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
15754    have an early register shift value or amount dependency on the
15755    result of PRODUCER.  */
15756
15757 int
15758 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
15759 {
15760   rtx value = PATTERN (producer);
15761   rtx op = PATTERN (consumer);
15762   rtx early_op;
15763
15764   if (GET_CODE (value) == COND_EXEC)
15765     value = COND_EXEC_CODE (value);
15766   if (GET_CODE (value) == PARALLEL)
15767     value = XVECEXP (value, 0, 0);
15768   value = XEXP (value, 0);
15769   if (GET_CODE (op) == COND_EXEC)
15770     op = COND_EXEC_CODE (op);
15771   if (GET_CODE (op) == PARALLEL)
15772     op = XVECEXP (op, 0, 0);
15773   op = XEXP (op, 1);
15774
15775   early_op = XEXP (op, 0);
15776   /* This is either an actual independent shift, or a shift applied to
15777      the first operand of another operation.  We want the whole shift
15778      operation.  */
15779   if (GET_CODE (early_op) == REG)
15780     early_op = op;
15781
15782   return !reg_overlap_mentioned_p (value, early_op);
15783 }
15784
15785 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
15786    have an early register shift value dependency on the result of
15787    PRODUCER.  */
15788
15789 int
15790 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
15791 {
15792   rtx value = PATTERN (producer);
15793   rtx op = PATTERN (consumer);
15794   rtx early_op;
15795
15796   if (GET_CODE (value) == COND_EXEC)
15797     value = COND_EXEC_CODE (value);
15798   if (GET_CODE (value) == PARALLEL)
15799     value = XVECEXP (value, 0, 0);
15800   value = XEXP (value, 0);
15801   if (GET_CODE (op) == COND_EXEC)
15802     op = COND_EXEC_CODE (op);
15803   if (GET_CODE (op) == PARALLEL)
15804     op = XVECEXP (op, 0, 0);
15805   op = XEXP (op, 1);
15806
15807   early_op = XEXP (op, 0);
15808
15809   /* This is either an actual independent shift, or a shift applied to
15810      the first operand of another operation.  We want the value being
15811      shifted, in either case.  */
15812   if (GET_CODE (early_op) != REG)
15813     early_op = XEXP (early_op, 0);
15814
15815   return !reg_overlap_mentioned_p (value, early_op);
15816 }
15817
15818 /* Return nonzero if the CONSUMER (a mul or mac op) does not
15819    have an early register mult dependency on the result of
15820    PRODUCER.  */
15821
15822 int
15823 arm_no_early_mul_dep (rtx producer, rtx consumer)
15824 {
15825   rtx value = PATTERN (producer);
15826   rtx op = PATTERN (consumer);
15827
15828   if (GET_CODE (value) == COND_EXEC)
15829     value = COND_EXEC_CODE (value);
15830   if (GET_CODE (value) == PARALLEL)
15831     value = XVECEXP (value, 0, 0);
15832   value = XEXP (value, 0);
15833   if (GET_CODE (op) == COND_EXEC)
15834     op = COND_EXEC_CODE (op);
15835   if (GET_CODE (op) == PARALLEL)
15836     op = XVECEXP (op, 0, 0);
15837   op = XEXP (op, 1);
15838
15839   return (GET_CODE (op) == PLUS
15840           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
15841 }
15842
15843
15844 /* We can't rely on the caller doing the proper promotion when
15845    using APCS or ATPCS.  */
15846
15847 static bool
15848 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
15849 {
15850     return !TARGET_AAPCS_BASED;
15851 }
15852
15853
15854 /* AAPCS based ABIs use short enums by default.  */
15855
15856 static bool
15857 arm_default_short_enums (void)
15858 {
15859   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
15860 }
15861
15862
15863 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
15864
15865 static bool
15866 arm_align_anon_bitfield (void)
15867 {
15868   return TARGET_AAPCS_BASED;
15869 }
15870
15871
15872 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
15873
15874 static tree
15875 arm_cxx_guard_type (void)
15876 {
15877   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
15878 }
15879
15880
15881 /* The EABI says test the least significant bit of a guard variable.  */
15882
15883 static bool
15884 arm_cxx_guard_mask_bit (void)
15885 {
15886   return TARGET_AAPCS_BASED;
15887 }
15888
15889
15890 /* The EABI specifies that all array cookies are 8 bytes long.  */
15891
15892 static tree
15893 arm_get_cookie_size (tree type)
15894 {
15895   tree size;
15896
15897   if (!TARGET_AAPCS_BASED)
15898     return default_cxx_get_cookie_size (type);
15899
15900   size = build_int_cst (sizetype, 8);
15901   return size;
15902 }
15903
15904
15905 /* The EABI says that array cookies should also contain the element size.  */
15906
15907 static bool
15908 arm_cookie_has_size (void)
15909 {
15910   return TARGET_AAPCS_BASED;
15911 }
15912
15913
15914 /* The EABI says constructors and destructors should return a pointer to
15915    the object constructed/destroyed.  */
15916
15917 static bool
15918 arm_cxx_cdtor_returns_this (void)
15919 {
15920   return TARGET_AAPCS_BASED;
15921 }
15922
15923 /* The EABI says that an inline function may never be the key
15924    method.  */
15925
15926 static bool
15927 arm_cxx_key_method_may_be_inline (void)
15928 {
15929   return !TARGET_AAPCS_BASED;
15930 }
15931
15932 static void
15933 arm_cxx_determine_class_data_visibility (tree decl)
15934 {
15935   if (!TARGET_AAPCS_BASED)
15936     return;
15937
15938   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
15939      is exported.  However, on systems without dynamic vague linkage,
15940      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
15941   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
15942     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
15943   else
15944     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
15945   DECL_VISIBILITY_SPECIFIED (decl) = 1;
15946 }
15947
15948 static bool
15949 arm_cxx_class_data_always_comdat (void)
15950 {
15951   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
15952      vague linkage if the class has no key function.  */
15953   return !TARGET_AAPCS_BASED;
15954 }
15955
15956
15957 /* The EABI says __aeabi_atexit should be used to register static
15958    destructors.  */
15959
15960 static bool
15961 arm_cxx_use_aeabi_atexit (void)
15962 {
15963   return TARGET_AAPCS_BASED;
15964 }
15965
15966
15967 void
15968 arm_set_return_address (rtx source, rtx scratch)
15969 {
15970   arm_stack_offsets *offsets;
15971   HOST_WIDE_INT delta;
15972   rtx addr;
15973   unsigned long saved_regs;
15974
15975   saved_regs = arm_compute_save_reg_mask ();
15976
15977   if ((saved_regs & (1 << LR_REGNUM)) == 0)
15978     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15979   else
15980     {
15981       if (frame_pointer_needed)
15982         addr = plus_constant(hard_frame_pointer_rtx, -4);
15983       else
15984         {
15985           /* LR will be the first saved register.  */
15986           offsets = arm_get_frame_offsets ();
15987           delta = offsets->outgoing_args - (offsets->frame + 4);
15988
15989
15990           if (delta >= 4096)
15991             {
15992               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15993                                      GEN_INT (delta & ~4095)));
15994               addr = scratch;
15995               delta &= 4095;
15996             }
15997           else
15998             addr = stack_pointer_rtx;
15999
16000           addr = plus_constant (addr, delta);
16001         }
16002       emit_move_insn (gen_frame_mem (Pmode, addr), source);
16003     }
16004 }
16005
16006
16007 void
16008 thumb_set_return_address (rtx source, rtx scratch)
16009 {
16010   arm_stack_offsets *offsets;
16011   HOST_WIDE_INT delta;
16012   HOST_WIDE_INT limit;
16013   int reg;
16014   rtx addr;
16015   unsigned long mask;
16016
16017   emit_insn (gen_rtx_USE (VOIDmode, source));
16018
16019   mask = thumb1_compute_save_reg_mask ();
16020   if (mask & (1 << LR_REGNUM))
16021     {
16022       offsets = arm_get_frame_offsets ();
16023
16024       limit = 1024;
16025       /* Find the saved regs.  */
16026       if (frame_pointer_needed)
16027         {
16028           delta = offsets->soft_frame - offsets->saved_args;
16029           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
16030           if (TARGET_THUMB1)
16031             limit = 128;
16032         }
16033       else
16034         {
16035           delta = offsets->outgoing_args - offsets->saved_args;
16036           reg = SP_REGNUM;
16037         }
16038       /* Allow for the stack frame.  */
16039       if (TARGET_THUMB1 && TARGET_BACKTRACE)
16040         delta -= 16;
16041       /* The link register is always the first saved register.  */
16042       delta -= 4;
16043
16044       /* Construct the address.  */
16045       addr = gen_rtx_REG (SImode, reg);
16046       if (delta > limit)
16047         {
16048           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
16049           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
16050           addr = scratch;
16051         }
16052       else
16053         addr = plus_constant (addr, delta);
16054
16055       emit_move_insn (gen_frame_mem (Pmode, addr), source);
16056     }
16057   else
16058     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
16059 }
16060
16061 /* Implements target hook vector_mode_supported_p.  */
16062 bool
16063 arm_vector_mode_supported_p (enum machine_mode mode)
16064 {
16065   if ((mode == V2SImode)
16066       || (mode == V4HImode)
16067       || (mode == V8QImode))
16068     return true;
16069
16070   return false;
16071 }
16072
16073 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
16074    ARM insns and therefore guarantee that the shift count is modulo 256.
16075    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
16076    guarantee no particular behavior for out-of-range counts.  */
16077
16078 static unsigned HOST_WIDE_INT
16079 arm_shift_truncation_mask (enum machine_mode mode)
16080 {
16081   return mode == SImode ? 255 : 0;
16082 }
16083
16084
16085 /* Map internal gcc register numbers to DWARF2 register numbers.  */
16086
16087 unsigned int
16088 arm_dbx_register_number (unsigned int regno)
16089 {
16090   if (regno < 16)
16091     return regno;
16092
16093   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
16094      compatibility.  The EABI defines them as registers 96-103.  */
16095   if (IS_FPA_REGNUM (regno))
16096     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
16097
16098   if (IS_VFP_REGNUM (regno))
16099     return 64 + regno - FIRST_VFP_REGNUM;
16100
16101   if (IS_IWMMXT_GR_REGNUM (regno))
16102     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
16103
16104   if (IS_IWMMXT_REGNUM (regno))
16105     return 112 + regno - FIRST_IWMMXT_REGNUM;
16106
16107   gcc_unreachable ();
16108 }
16109
16110
16111 #ifdef TARGET_UNWIND_INFO
16112 /* Emit unwind directives for a store-multiple instruction or stack pointer
16113    push during alignment.
16114    These should only ever be generated by the function prologue code, so
16115    expect them to have a particular form.  */
16116
16117 static void
16118 arm_unwind_emit_sequence (FILE * asm_out_file, rtx p)
16119 {
16120   int i;
16121   HOST_WIDE_INT offset;
16122   HOST_WIDE_INT nregs;
16123   int reg_size;
16124   unsigned reg;
16125   unsigned lastreg;
16126   rtx e;
16127
16128   e = XVECEXP (p, 0, 0);
16129   if (GET_CODE (e) != SET)
16130     abort ();
16131
16132   /* First insn will adjust the stack pointer.  */
16133   if (GET_CODE (e) != SET
16134       || GET_CODE (XEXP (e, 0)) != REG
16135       || REGNO (XEXP (e, 0)) != SP_REGNUM
16136       || GET_CODE (XEXP (e, 1)) != PLUS)
16137     abort ();
16138
16139   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
16140   nregs = XVECLEN (p, 0) - 1;
16141
16142   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
16143   if (reg < 16)
16144     {
16145       /* The function prologue may also push pc, but not annotate it as it is
16146          never restored.  We turn this into a stack pointer adjustment.  */
16147       if (nregs * 4 == offset - 4)
16148         {
16149           fprintf (asm_out_file, "\t.pad #4\n");
16150           offset -= 4;
16151         }
16152       reg_size = 4;
16153       fprintf (asm_out_file, "\t.save {");
16154     }
16155   else if (IS_VFP_REGNUM (reg))
16156     {
16157       reg_size = 8;
16158       fprintf (asm_out_file, "\t.vsave {");
16159     }
16160   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
16161     {
16162       /* FPA registers are done differently.  */
16163       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
16164       return;
16165     }
16166   else
16167     /* Unknown register type.  */
16168     abort ();
16169
16170   /* If the stack increment doesn't match the size of the saved registers,
16171      something has gone horribly wrong.  */
16172   if (offset != nregs * reg_size)
16173     abort ();
16174
16175   offset = 0;
16176   lastreg = 0;
16177   /* The remaining insns will describe the stores.  */
16178   for (i = 1; i <= nregs; i++)
16179     {
16180       /* Expect (set (mem <addr>) (reg)).
16181          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
16182       e = XVECEXP (p, 0, i);
16183       if (GET_CODE (e) != SET
16184           || GET_CODE (XEXP (e, 0)) != MEM
16185           || GET_CODE (XEXP (e, 1)) != REG)
16186         abort ();
16187
16188       reg = REGNO (XEXP (e, 1));
16189       if (reg < lastreg)
16190         abort ();
16191
16192       if (i != 1)
16193         fprintf (asm_out_file, ", ");
16194       /* We can't use %r for vfp because we need to use the
16195          double precision register names.  */
16196       if (IS_VFP_REGNUM (reg))
16197         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
16198       else
16199         asm_fprintf (asm_out_file, "%r", reg);
16200
16201 #ifdef ENABLE_CHECKING
16202       /* Check that the addresses are consecutive.  */
16203       e = XEXP (XEXP (e, 0), 0);
16204       if (GET_CODE (e) == PLUS)
16205         {
16206           offset += reg_size;
16207           if (GET_CODE (XEXP (e, 0)) != REG
16208               || REGNO (XEXP (e, 0)) != SP_REGNUM
16209               || GET_CODE (XEXP (e, 1)) != CONST_INT
16210               || offset != INTVAL (XEXP (e, 1)))
16211             abort ();
16212         }
16213       else if (i != 1
16214                || GET_CODE (e) != REG
16215                || REGNO (e) != SP_REGNUM)
16216         abort ();
16217 #endif
16218     }
16219   fprintf (asm_out_file, "}\n");
16220 }
16221
16222 /*  Emit unwind directives for a SET.  */
16223
16224 static void
16225 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
16226 {
16227   rtx e0;
16228   rtx e1;
16229   unsigned reg;
16230
16231   e0 = XEXP (p, 0);
16232   e1 = XEXP (p, 1);
16233   switch (GET_CODE (e0))
16234     {
16235     case MEM:
16236       /* Pushing a single register.  */
16237       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
16238           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
16239           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
16240         abort ();
16241
16242       asm_fprintf (asm_out_file, "\t.save ");
16243       if (IS_VFP_REGNUM (REGNO (e1)))
16244         asm_fprintf(asm_out_file, "{d%d}\n",
16245                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
16246       else
16247         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
16248       break;
16249
16250     case REG:
16251       if (REGNO (e0) == SP_REGNUM)
16252         {
16253           /* A stack increment.  */
16254           if (GET_CODE (e1) != PLUS
16255               || GET_CODE (XEXP (e1, 0)) != REG
16256               || REGNO (XEXP (e1, 0)) != SP_REGNUM
16257               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
16258             abort ();
16259
16260           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
16261                        -INTVAL (XEXP (e1, 1)));
16262         }
16263       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
16264         {
16265           HOST_WIDE_INT offset;
16266
16267           if (GET_CODE (e1) == PLUS)
16268             {
16269               if (GET_CODE (XEXP (e1, 0)) != REG
16270                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
16271                 abort ();
16272               reg = REGNO (XEXP (e1, 0));
16273               offset = INTVAL (XEXP (e1, 1));
16274               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
16275                            HARD_FRAME_POINTER_REGNUM, reg,
16276                            INTVAL (XEXP (e1, 1)));
16277             }
16278           else if (GET_CODE (e1) == REG)
16279             {
16280               reg = REGNO (e1);
16281               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
16282                            HARD_FRAME_POINTER_REGNUM, reg);
16283             }
16284           else
16285             abort ();
16286         }
16287       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
16288         {
16289           /* Move from sp to reg.  */
16290           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
16291         }
16292      else if (GET_CODE (e1) == PLUS
16293               && GET_CODE (XEXP (e1, 0)) == REG
16294               && REGNO (XEXP (e1, 0)) == SP_REGNUM
16295               && GET_CODE (XEXP (e1, 1)) == CONST_INT)
16296         {
16297           /* Set reg to offset from sp.  */
16298           asm_fprintf (asm_out_file, "\t.movsp %r, #%d\n",
16299                        REGNO (e0), (int)INTVAL(XEXP (e1, 1)));
16300         }
16301       else if (GET_CODE (e1) == UNSPEC && XINT (e1, 1) == UNSPEC_STACK_ALIGN)
16302         {
16303           /* Stack pointer save before alignment.  */
16304           reg = REGNO (e0);
16305           asm_fprintf (asm_out_file, "\t.unwind_raw 0, 0x%x @ vsp = r%d\n",
16306                        reg + 0x90, reg);
16307         }
16308       else
16309         abort ();
16310       break;
16311
16312     default:
16313       abort ();
16314     }
16315 }
16316
16317
16318 /* Emit unwind directives for the given insn.  */
16319
16320 static void
16321 arm_unwind_emit (FILE * asm_out_file, rtx insn)
16322 {
16323   rtx pat;
16324
16325   if (!ARM_EABI_UNWIND_TABLES)
16326     return;
16327
16328   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
16329     return;
16330
16331   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
16332   if (pat)
16333     pat = XEXP (pat, 0);
16334   else
16335     pat = PATTERN (insn);
16336
16337   switch (GET_CODE (pat))
16338     {
16339     case SET:
16340       arm_unwind_emit_set (asm_out_file, pat);
16341       break;
16342
16343     case SEQUENCE:
16344       /* Store multiple.  */
16345       arm_unwind_emit_sequence (asm_out_file, pat);
16346       break;
16347
16348     default:
16349       abort();
16350     }
16351 }
16352
16353
16354 /* Output a reference from a function exception table to the type_info
16355    object X.  The EABI specifies that the symbol should be relocated by
16356    an R_ARM_TARGET2 relocation.  */
16357
16358 static bool
16359 arm_output_ttype (rtx x)
16360 {
16361   fputs ("\t.word\t", asm_out_file);
16362   output_addr_const (asm_out_file, x);
16363   /* Use special relocations for symbol references.  */
16364   if (GET_CODE (x) != CONST_INT)
16365     fputs ("(TARGET2)", asm_out_file);
16366   fputc ('\n', asm_out_file);
16367
16368   return TRUE;
16369 }
16370 #endif /* TARGET_UNWIND_INFO */
16371
16372
16373 /* Handle UNSPEC DWARF call frame instructions.  These are needed for dynamic
16374    stack alignment.  */
16375
16376 static void
16377 arm_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
16378 {
16379   rtx unspec = SET_SRC (pattern);
16380   gcc_assert (GET_CODE (unspec) == UNSPEC);
16381
16382   switch (index)
16383     {
16384     case UNSPEC_STACK_ALIGN:
16385       /* ??? We should set the CFA = (SP & ~7).  At this point we haven't
16386          put anything on the stack, so hopefully it won't matter.
16387          CFA = SP will be correct after alignment.  */
16388       dwarf2out_reg_save_reg (label, stack_pointer_rtx,
16389                               SET_DEST (pattern));
16390       break;
16391     default:
16392       gcc_unreachable ();
16393     }
16394 }
16395
16396
16397 /* Output unwind directives for the start/end of a function.  */
16398
16399 void
16400 arm_output_fn_unwind (FILE * f, bool prologue)
16401 {
16402   if (!ARM_EABI_UNWIND_TABLES)
16403     return;
16404
16405   if (prologue)
16406     fputs ("\t.fnstart\n", f);
16407   else
16408     fputs ("\t.fnend\n", f);
16409 }
16410
16411 static bool
16412 arm_emit_tls_decoration (FILE *fp, rtx x)
16413 {
16414   enum tls_reloc reloc;
16415   rtx val;
16416
16417   val = XVECEXP (x, 0, 0);
16418   reloc = INTVAL (XVECEXP (x, 0, 1));
16419
16420   output_addr_const (fp, val);
16421
16422   switch (reloc)
16423     {
16424     case TLS_GD32:
16425       fputs ("(tlsgd)", fp);
16426       break;
16427     case TLS_LDM32:
16428       fputs ("(tlsldm)", fp);
16429       break;
16430     case TLS_LDO32:
16431       fputs ("(tlsldo)", fp);
16432       break;
16433     case TLS_IE32:
16434       fputs ("(gottpoff)", fp);
16435       break;
16436     case TLS_LE32:
16437       fputs ("(tpoff)", fp);
16438       break;
16439     default:
16440       gcc_unreachable ();
16441     }
16442
16443   switch (reloc)
16444     {
16445     case TLS_GD32:
16446     case TLS_LDM32:
16447     case TLS_IE32:
16448       fputs (" + (. - ", fp);
16449       output_addr_const (fp, XVECEXP (x, 0, 2));
16450       fputs (" - ", fp);
16451       output_addr_const (fp, XVECEXP (x, 0, 3));
16452       fputc (')', fp);
16453       break;
16454     default:
16455       break;
16456     }
16457
16458   return TRUE;
16459 }
16460
16461 bool
16462 arm_output_addr_const_extra (FILE *fp, rtx x)
16463 {
16464   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
16465     return arm_emit_tls_decoration (fp, x);
16466   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
16467     {
16468       char label[256];
16469       int labelno = INTVAL (XVECEXP (x, 0, 0));
16470
16471       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
16472       assemble_name_raw (fp, label);
16473
16474       return TRUE;
16475     }
16476   else if (GET_CODE (x) == CONST_VECTOR)
16477     return arm_emit_vector_const (fp, x);
16478
16479   return FALSE;
16480 }
16481
16482 /* Output assembly for a shift instruction.
16483    SET_FLAGS determines how the instruction modifies the condition codes.
16484    0 - Do not set condition codes.
16485    1 - Set condition codes.
16486    2 - Use smallest instruction.  */
16487 const char *
16488 arm_output_shift(rtx * operands, int set_flags)
16489 {
16490   char pattern[100];
16491   static const char flag_chars[3] = {'?', '.', '!'};
16492   const char *shift;
16493   HOST_WIDE_INT val;
16494   char c;
16495   
16496   c = flag_chars[set_flags];
16497   if (TARGET_UNIFIED_ASM)
16498     {
16499       shift = shift_op(operands[3], &val);
16500       if (shift)
16501         {
16502           if (val != -1)
16503             operands[2] = GEN_INT(val);
16504           sprintf (pattern, "%s%%%c\t%%0, %%1, %%2", shift, c);
16505         }
16506       else
16507         sprintf (pattern, "mov%%%c\t%%0, %%1", c);
16508     }
16509   else
16510     sprintf (pattern, "mov%%%c\t%%0, %%1%%S3", c);
16511   output_asm_insn (pattern, operands);
16512   return "";
16513 }
16514
16515 /* Output a Thumb-2 casesi instruction.  */
16516 const char *
16517 thumb2_output_casesi (rtx *operands)
16518 {
16519   rtx diff_vec = PATTERN (next_real_insn (operands[2]));
16520
16521   gcc_assert (GET_CODE (diff_vec) == ADDR_DIFF_VEC);
16522
16523   output_asm_insn ("cmp\t%0, %1", operands);
16524   output_asm_insn ("bhi\t%l3", operands);
16525   switch (GET_MODE(diff_vec))
16526     {
16527     case QImode:
16528       return "tbb\t[%|pc, %0]";
16529     case HImode:
16530       return "tbh\t[%|pc, %0, lsl #1]";
16531     case SImode:
16532       if (flag_pic)
16533         {
16534           output_asm_insn ("adr\t%4, %l2", operands);
16535           output_asm_insn ("ldr\t%5, [%4, %0, lsl #2]", operands);
16536           output_asm_insn ("add\t%4, %4, %5", operands);
16537           return "bx\t%4";
16538         }
16539       else
16540         {
16541           output_asm_insn ("adr\t%4, %l2", operands);
16542           return "ldr\t%|pc, [%4, %0, lsl #2]";
16543         }
16544     default:
16545       gcc_unreachable ();
16546     }
16547 }
16548
16549 #include "gt-arm.h"