OSDN Git Service

PR target/24914
[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  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 thumb_base_register_rtx_p (rtx, enum machine_mode, int);
71 inline static int thumb_index_register_rtx_p (rtx, int);
72 static int thumb_far_jump_used_p (void);
73 static bool thumb_force_lr_save (void);
74 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
75 static rtx emit_sfm (int, int);
76 static int arm_size_return_regs (void);
77 #ifndef AOF_ASSEMBLER
78 static bool arm_assemble_integer (rtx, unsigned int, int);
79 #endif
80 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
81 static arm_cc get_arm_condition_code (rtx);
82 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
83 static rtx is_jump_table (rtx);
84 static const char *output_multi_immediate (rtx *, const char *, const char *,
85                                            int, HOST_WIDE_INT);
86 static const char *shift_op (rtx, HOST_WIDE_INT *);
87 static struct machine_function *arm_init_machine_status (void);
88 static void thumb_exit (FILE *, int);
89 static rtx is_jump_table (rtx);
90 static HOST_WIDE_INT get_jump_table_size (rtx);
91 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
92 static Mnode *add_minipool_forward_ref (Mfix *);
93 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
94 static Mnode *add_minipool_backward_ref (Mfix *);
95 static void assign_minipool_offsets (Mfix *);
96 static void arm_print_value (FILE *, rtx);
97 static void dump_minipool (rtx);
98 static int arm_barrier_cost (rtx);
99 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
100 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
101 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
102                                rtx);
103 static void arm_reorg (void);
104 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
105 static int current_file_function_operand (rtx);
106 static unsigned long arm_compute_save_reg0_reg12_mask (void);
107 static unsigned long arm_compute_save_reg_mask (void);
108 static unsigned long arm_isr_value (tree);
109 static unsigned long arm_compute_func_type (void);
110 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
111 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
112 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
113 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
114 #endif
115 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
116 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
117 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
118 static int arm_comp_type_attributes (tree, tree);
119 static void arm_set_default_type_attributes (tree);
120 static int arm_adjust_cost (rtx, rtx, rtx, int);
121 static int count_insns_for_constant (HOST_WIDE_INT, int);
122 static int arm_get_strip_length (int);
123 static bool arm_function_ok_for_sibcall (tree, tree);
124 static void arm_internal_label (FILE *, const char *, unsigned long);
125 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
126                                  tree);
127 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
128 static bool arm_size_rtx_costs (rtx, int, int, int *);
129 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
130 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
131 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
132 static bool arm_9e_rtx_costs (rtx, int, int, int *);
133 static int arm_address_cost (rtx);
134 static bool arm_memory_load_p (rtx);
135 static bool arm_cirrus_insn_p (rtx);
136 static void cirrus_reorg (rtx);
137 static void arm_init_builtins (void);
138 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
139 static void arm_init_iwmmxt_builtins (void);
140 static rtx safe_vector_operand (rtx, enum machine_mode);
141 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
142 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
143 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
144 static void emit_constant_insn (rtx cond, rtx pattern);
145 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
146                                   tree, bool);
147
148 #ifdef OBJECT_FORMAT_ELF
149 static void arm_elf_asm_constructor (rtx, int);
150 #endif
151 #ifndef ARM_PE
152 static void arm_encode_section_info (tree, rtx, int);
153 #endif
154
155 static void arm_file_end (void);
156
157 #ifdef AOF_ASSEMBLER
158 static void aof_globalize_label (FILE *, const char *);
159 static void aof_dump_imports (FILE *);
160 static void aof_dump_pic_table (FILE *);
161 static void aof_file_start (void);
162 static void aof_file_end (void);
163 #endif
164 static rtx arm_struct_value_rtx (tree, int);
165 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
166                                         tree, int *, int);
167 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
168                                    enum machine_mode, tree, bool);
169 static bool arm_promote_prototypes (tree);
170 static bool arm_default_short_enums (void);
171 static bool arm_align_anon_bitfield (void);
172 static bool arm_return_in_msb (tree);
173 static bool arm_must_pass_in_stack (enum machine_mode, tree);
174 #ifdef TARGET_UNWIND_INFO
175 static void arm_unwind_emit (FILE *, rtx);
176 static bool arm_output_ttype (rtx);
177 #endif
178
179 static tree arm_cxx_guard_type (void);
180 static bool arm_cxx_guard_mask_bit (void);
181 static tree arm_get_cookie_size (tree);
182 static bool arm_cookie_has_size (void);
183 static bool arm_cxx_cdtor_returns_this (void);
184 static bool arm_cxx_key_method_may_be_inline (void);
185 static void arm_cxx_determine_class_data_visibility (tree);
186 static bool arm_cxx_class_data_always_comdat (void);
187 static bool arm_cxx_use_aeabi_atexit (void);
188 static void arm_init_libfuncs (void);
189 static bool arm_handle_option (size_t, const char *, int);
190 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
191 static bool arm_cannot_copy_insn_p (rtx);
192 static bool arm_tls_symbol_p (rtx x);
193
194 \f
195 /* Initialize the GCC target structure.  */
196 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
197 #undef  TARGET_MERGE_DECL_ATTRIBUTES
198 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
199 #endif
200
201 #undef  TARGET_ATTRIBUTE_TABLE
202 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
203
204 #undef TARGET_ASM_FILE_END
205 #define TARGET_ASM_FILE_END arm_file_end
206
207 #ifdef AOF_ASSEMBLER
208 #undef  TARGET_ASM_BYTE_OP
209 #define TARGET_ASM_BYTE_OP "\tDCB\t"
210 #undef  TARGET_ASM_ALIGNED_HI_OP
211 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
212 #undef  TARGET_ASM_ALIGNED_SI_OP
213 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
214 #undef TARGET_ASM_GLOBALIZE_LABEL
215 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
216 #undef TARGET_ASM_FILE_START
217 #define TARGET_ASM_FILE_START aof_file_start
218 #undef TARGET_ASM_FILE_END
219 #define TARGET_ASM_FILE_END aof_file_end
220 #else
221 #undef  TARGET_ASM_ALIGNED_SI_OP
222 #define TARGET_ASM_ALIGNED_SI_OP NULL
223 #undef  TARGET_ASM_INTEGER
224 #define TARGET_ASM_INTEGER arm_assemble_integer
225 #endif
226
227 #undef  TARGET_ASM_FUNCTION_PROLOGUE
228 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
229
230 #undef  TARGET_ASM_FUNCTION_EPILOGUE
231 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
232
233 #undef  TARGET_DEFAULT_TARGET_FLAGS
234 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
235 #undef  TARGET_HANDLE_OPTION
236 #define TARGET_HANDLE_OPTION arm_handle_option
237
238 #undef  TARGET_COMP_TYPE_ATTRIBUTES
239 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
240
241 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
242 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
243
244 #undef  TARGET_SCHED_ADJUST_COST
245 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
246
247 #undef TARGET_ENCODE_SECTION_INFO
248 #ifdef ARM_PE
249 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
250 #else
251 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
252 #endif
253
254 #undef  TARGET_STRIP_NAME_ENCODING
255 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
256
257 #undef  TARGET_ASM_INTERNAL_LABEL
258 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
259
260 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
261 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
262
263 #undef  TARGET_ASM_OUTPUT_MI_THUNK
264 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
265 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
266 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
267
268 /* This will be overridden in arm_override_options.  */
269 #undef  TARGET_RTX_COSTS
270 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
271 #undef  TARGET_ADDRESS_COST
272 #define TARGET_ADDRESS_COST arm_address_cost
273
274 #undef TARGET_SHIFT_TRUNCATION_MASK
275 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
276 #undef TARGET_VECTOR_MODE_SUPPORTED_P
277 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
278
279 #undef  TARGET_MACHINE_DEPENDENT_REORG
280 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
281
282 #undef  TARGET_INIT_BUILTINS
283 #define TARGET_INIT_BUILTINS  arm_init_builtins
284 #undef  TARGET_EXPAND_BUILTIN
285 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
286
287 #undef TARGET_INIT_LIBFUNCS
288 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
289
290 #undef TARGET_PROMOTE_FUNCTION_ARGS
291 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
292 #undef TARGET_PROMOTE_FUNCTION_RETURN
293 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
294 #undef TARGET_PROMOTE_PROTOTYPES
295 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
296 #undef TARGET_PASS_BY_REFERENCE
297 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
298 #undef TARGET_ARG_PARTIAL_BYTES
299 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
300
301 #undef TARGET_STRUCT_VALUE_RTX
302 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
303
304 #undef  TARGET_SETUP_INCOMING_VARARGS
305 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
306
307 #undef TARGET_DEFAULT_SHORT_ENUMS
308 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
309
310 #undef TARGET_ALIGN_ANON_BITFIELD
311 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
312
313 #undef TARGET_CXX_GUARD_TYPE
314 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
315
316 #undef TARGET_CXX_GUARD_MASK_BIT
317 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
318
319 #undef TARGET_CXX_GET_COOKIE_SIZE
320 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
321
322 #undef TARGET_CXX_COOKIE_HAS_SIZE
323 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
324
325 #undef TARGET_CXX_CDTOR_RETURNS_THIS
326 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
327
328 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
329 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
330
331 #undef TARGET_CXX_USE_AEABI_ATEXIT
332 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
333
334 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
335 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
336   arm_cxx_determine_class_data_visibility
337
338 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
339 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
340
341 #undef TARGET_RETURN_IN_MSB
342 #define TARGET_RETURN_IN_MSB arm_return_in_msb
343
344 #undef TARGET_MUST_PASS_IN_STACK
345 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
346
347 #ifdef TARGET_UNWIND_INFO
348 #undef TARGET_UNWIND_EMIT
349 #define TARGET_UNWIND_EMIT arm_unwind_emit
350
351 /* EABI unwinding tables use a different format for the typeinfo tables.  */
352 #undef TARGET_ASM_TTYPE
353 #define TARGET_ASM_TTYPE arm_output_ttype
354
355 #undef TARGET_ARM_EABI_UNWINDER
356 #define TARGET_ARM_EABI_UNWINDER true
357 #endif /* TARGET_UNWIND_INFO */
358
359 #undef  TARGET_CANNOT_COPY_INSN_P
360 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
361
362 #ifdef HAVE_AS_TLS
363 #undef TARGET_HAVE_TLS
364 #define TARGET_HAVE_TLS true
365 #endif
366
367 #undef TARGET_CANNOT_FORCE_CONST_MEM
368 #define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
369
370 struct gcc_target targetm = TARGET_INITIALIZER;
371 \f
372 /* Obstack for minipool constant handling.  */
373 static struct obstack minipool_obstack;
374 static char *         minipool_startobj;
375
376 /* The maximum number of insns skipped which
377    will be conditionalised if possible.  */
378 static int max_insns_skipped = 5;
379
380 extern FILE * asm_out_file;
381
382 /* True if we are currently building a constant table.  */
383 int making_const_table;
384
385 /* Define the information needed to generate branch insns.  This is
386    stored from the compare operation.  */
387 rtx arm_compare_op0, arm_compare_op1;
388
389 /* The processor for which instructions should be scheduled.  */
390 enum processor_type arm_tune = arm_none;
391
392 /* Which floating point model to use.  */
393 enum arm_fp_model arm_fp_model;
394
395 /* Which floating point hardware is available.  */
396 enum fputype arm_fpu_arch;
397
398 /* Which floating point hardware to schedule for.  */
399 enum fputype arm_fpu_tune;
400
401 /* Whether to use floating point hardware.  */
402 enum float_abi_type arm_float_abi;
403
404 /* Which ABI to use.  */
405 enum arm_abi_type arm_abi;
406
407 /* Which thread pointer model to use.  */
408 enum arm_tp_type target_thread_pointer = TP_AUTO;
409
410 /* Used to parse -mstructure_size_boundary command line option.  */
411 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
412
413 /* Used for Thumb call_via trampolines.  */
414 rtx thumb_call_via_label[14];
415 static int thumb_call_reg_needed;
416
417 /* Bit values used to identify processor capabilities.  */
418 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
419 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
420 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
421 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
422 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
423 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
424 #define FL_THUMB      (1 << 6)        /* Thumb aware */
425 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
426 #define FL_STRONG     (1 << 8)        /* StrongARM */
427 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
428 #define FL_XSCALE     (1 << 10)       /* XScale */
429 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
430 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
431                                          media instructions.  */
432 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
433 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
434                                          Note: ARM6 & 7 derivatives only.  */
435 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
436
437 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
438
439 #define FL_FOR_ARCH2    0
440 #define FL_FOR_ARCH3    FL_MODE32
441 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
442 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
443 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
444 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
445 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
446 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
447 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
448 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
449 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
450 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
451 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
452 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
453 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
454
455 /* The bits in this mask specify which
456    instructions we are allowed to generate.  */
457 static unsigned long insn_flags = 0;
458
459 /* The bits in this mask specify which instruction scheduling options should
460    be used.  */
461 static unsigned long tune_flags = 0;
462
463 /* The following are used in the arm.md file as equivalents to bits
464    in the above two flag variables.  */
465
466 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
467 int arm_arch3m = 0;
468
469 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
470 int arm_arch4 = 0;
471
472 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
473 int arm_arch4t = 0;
474
475 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
476 int arm_arch5 = 0;
477
478 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
479 int arm_arch5e = 0;
480
481 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
482 int arm_arch6 = 0;
483
484 /* Nonzero if this chip supports the ARM 6K extensions.  */
485 int arm_arch6k = 0;
486
487 /* Nonzero if this chip can benefit from load scheduling.  */
488 int arm_ld_sched = 0;
489
490 /* Nonzero if this chip is a StrongARM.  */
491 int arm_tune_strongarm = 0;
492
493 /* Nonzero if this chip is a Cirrus variant.  */
494 int arm_arch_cirrus = 0;
495
496 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
497 int arm_arch_iwmmxt = 0;
498
499 /* Nonzero if this chip is an XScale.  */
500 int arm_arch_xscale = 0;
501
502 /* Nonzero if tuning for XScale  */
503 int arm_tune_xscale = 0;
504
505 /* Nonzero if we want to tune for stores that access the write-buffer.
506    This typically means an ARM6 or ARM7 with MMU or MPU.  */
507 int arm_tune_wbuf = 0;
508
509 /* Nonzero if generating Thumb instructions.  */
510 int thumb_code = 0;
511
512 /* Nonzero if we should define __THUMB_INTERWORK__ in the
513    preprocessor.
514    XXX This is a bit of a hack, it's intended to help work around
515    problems in GLD which doesn't understand that armv5t code is
516    interworking clean.  */
517 int arm_cpp_interwork = 0;
518
519 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
520    must report the mode of the memory reference from PRINT_OPERAND to
521    PRINT_OPERAND_ADDRESS.  */
522 enum machine_mode output_memory_reference_mode;
523
524 /* The register number to be used for the PIC offset register.  */
525 int arm_pic_register = INVALID_REGNUM;
526
527 /* Set to 1 when a return insn is output, this means that the epilogue
528    is not needed.  */
529 int return_used_this_function;
530
531 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
532    the next function.  */
533 static int after_arm_reorg = 0;
534
535 /* The maximum number of insns to be used when loading a constant.  */
536 static int arm_constant_limit = 3;
537
538 /* For an explanation of these variables, see final_prescan_insn below.  */
539 int arm_ccfsm_state;
540 enum arm_cond_code arm_current_cc;
541 rtx arm_target_insn;
542 int arm_target_label;
543
544 /* The condition codes of the ARM, and the inverse function.  */
545 static const char * const arm_condition_codes[] =
546 {
547   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
548   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
549 };
550
551 #define streq(string1, string2) (strcmp (string1, string2) == 0)
552 \f
553 /* Initialization code.  */
554
555 struct processors
556 {
557   const char *const name;
558   enum processor_type core;
559   const char *arch;
560   const unsigned long flags;
561   bool (* rtx_costs) (rtx, int, int, int *);
562 };
563
564 /* Not all of these give usefully different compilation alternatives,
565    but there is no simple way of generalizing them.  */
566 static const struct processors all_cores[] =
567 {
568   /* ARM Cores */
569 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
570   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
571 #include "arm-cores.def"
572 #undef ARM_CORE
573   {NULL, arm_none, NULL, 0, NULL}
574 };
575
576 static const struct processors all_architectures[] =
577 {
578   /* ARM Architectures */
579   /* We don't specify rtx_costs here as it will be figured out
580      from the core.  */
581
582   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
583   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
584   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
585   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
586   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
587   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
588      implementations that support it, so we will leave it out for now.  */
589   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
590   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
591   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
592   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
593   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
594   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
595   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
596   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
597   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
598   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
599   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
600   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
601   {NULL, arm_none, NULL, 0 , NULL}
602 };
603
604 struct arm_cpu_select
605 {
606   const char *              string;
607   const char *              name;
608   const struct processors * processors;
609 };
610
611 /* This is a magic structure.  The 'string' field is magically filled in
612    with a pointer to the value specified by the user on the command line
613    assuming that the user has specified such a value.  */
614
615 static struct arm_cpu_select arm_select[] =
616 {
617   /* string       name            processors  */
618   { NULL,       "-mcpu=",       all_cores  },
619   { NULL,       "-march=",      all_architectures },
620   { NULL,       "-mtune=",      all_cores }
621 };
622
623 /* Defines representing the indexes into the above table.  */
624 #define ARM_OPT_SET_CPU 0
625 #define ARM_OPT_SET_ARCH 1
626 #define ARM_OPT_SET_TUNE 2
627
628 /* The name of the proprocessor macro to define for this architecture.  */
629
630 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
631
632 struct fpu_desc
633 {
634   const char * name;
635   enum fputype fpu;
636 };
637
638
639 /* Available values for for -mfpu=.  */
640
641 static const struct fpu_desc all_fpus[] =
642 {
643   {"fpa",       FPUTYPE_FPA},
644   {"fpe2",      FPUTYPE_FPA_EMU2},
645   {"fpe3",      FPUTYPE_FPA_EMU2},
646   {"maverick",  FPUTYPE_MAVERICK},
647   {"vfp",       FPUTYPE_VFP}
648 };
649
650
651 /* Floating point models used by the different hardware.
652    See fputype in arm.h.  */
653
654 static const enum fputype fp_model_for_fpu[] =
655 {
656   /* No FP hardware.  */
657   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
658   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
659   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
660   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
661   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
662   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
663 };
664
665
666 struct float_abi
667 {
668   const char * name;
669   enum float_abi_type abi_type;
670 };
671
672
673 /* Available values for -mfloat-abi=.  */
674
675 static const struct float_abi all_float_abis[] =
676 {
677   {"soft",      ARM_FLOAT_ABI_SOFT},
678   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
679   {"hard",      ARM_FLOAT_ABI_HARD}
680 };
681
682
683 struct abi_name
684 {
685   const char *name;
686   enum arm_abi_type abi_type;
687 };
688
689
690 /* Available values for -mabi=.  */
691
692 static const struct abi_name arm_all_abis[] =
693 {
694   {"apcs-gnu",    ARM_ABI_APCS},
695   {"atpcs",   ARM_ABI_ATPCS},
696   {"aapcs",   ARM_ABI_AAPCS},
697   {"iwmmxt",  ARM_ABI_IWMMXT},
698   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
699 };
700
701 /* Supported TLS relocations.  */
702
703 enum tls_reloc {
704   TLS_GD32,
705   TLS_LDM32,
706   TLS_LDO32,
707   TLS_IE32,
708   TLS_LE32
709 };
710
711 /* Return the number of bits set in VALUE.  */
712 static unsigned
713 bit_count (unsigned long value)
714 {
715   unsigned long count = 0;
716
717   while (value)
718     {
719       count++;
720       value &= value - 1;  /* Clear the least-significant set bit.  */
721     }
722
723   return count;
724 }
725
726 /* Set up library functions unique to ARM.  */
727
728 static void
729 arm_init_libfuncs (void)
730 {
731   /* There are no special library functions unless we are using the
732      ARM BPABI.  */
733   if (!TARGET_BPABI)
734     return;
735
736   /* The functions below are described in Section 4 of the "Run-Time
737      ABI for the ARM architecture", Version 1.0.  */
738
739   /* Double-precision floating-point arithmetic.  Table 2.  */
740   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
741   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
742   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
743   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
744   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
745
746   /* Double-precision comparisons.  Table 3.  */
747   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
748   set_optab_libfunc (ne_optab, DFmode, NULL);
749   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
750   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
751   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
752   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
753   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
754
755   /* Single-precision floating-point arithmetic.  Table 4.  */
756   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
757   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
758   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
759   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
760   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
761
762   /* Single-precision comparisons.  Table 5.  */
763   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
764   set_optab_libfunc (ne_optab, SFmode, NULL);
765   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
766   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
767   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
768   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
769   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
770
771   /* Floating-point to integer conversions.  Table 6.  */
772   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
773   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
774   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
775   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
776   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
777   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
778   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
779   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
780
781   /* Conversions between floating types.  Table 7.  */
782   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
783   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
784
785   /* Integer to floating-point conversions.  Table 8.  */
786   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
787   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
788   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
789   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
790   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
791   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
792   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
793   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
794
795   /* Long long.  Table 9.  */
796   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
797   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
798   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
799   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
800   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
801   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
802   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
803   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
804
805   /* Integer (32/32->32) division.  \S 4.3.1.  */
806   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
807   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
808
809   /* The divmod functions are designed so that they can be used for
810      plain division, even though they return both the quotient and the
811      remainder.  The quotient is returned in the usual location (i.e.,
812      r0 for SImode, {r0, r1} for DImode), just as would be expected
813      for an ordinary division routine.  Because the AAPCS calling
814      conventions specify that all of { r0, r1, r2, r3 } are
815      callee-saved registers, there is no need to tell the compiler
816      explicitly that those registers are clobbered by these
817      routines.  */
818   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
819   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
820
821   /* For SImode division the ABI provides div-without-mod routines,
822      which are faster.  */
823   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
824   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
825
826   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
827      divmod libcalls instead.  */
828   set_optab_libfunc (smod_optab, DImode, NULL);
829   set_optab_libfunc (umod_optab, DImode, NULL);
830   set_optab_libfunc (smod_optab, SImode, NULL);
831   set_optab_libfunc (umod_optab, SImode, NULL);
832 }
833
834 /* Implement TARGET_HANDLE_OPTION.  */
835
836 static bool
837 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
838 {
839   switch (code)
840     {
841     case OPT_march_:
842       arm_select[1].string = arg;
843       return true;
844
845     case OPT_mcpu_:
846       arm_select[0].string = arg;
847       return true;
848
849     case OPT_mhard_float:
850       target_float_abi_name = "hard";
851       return true;
852
853     case OPT_msoft_float:
854       target_float_abi_name = "soft";
855       return true;
856
857     case OPT_mtune_:
858       arm_select[2].string = arg;
859       return true;
860
861     default:
862       return true;
863     }
864 }
865
866 /* Fix up any incompatible options that the user has specified.
867    This has now turned into a maze.  */
868 void
869 arm_override_options (void)
870 {
871   unsigned i;
872   enum processor_type target_arch_cpu = arm_none;
873
874   /* Set up the flags based on the cpu/architecture selected by the user.  */
875   for (i = ARRAY_SIZE (arm_select); i--;)
876     {
877       struct arm_cpu_select * ptr = arm_select + i;
878
879       if (ptr->string != NULL && ptr->string[0] != '\0')
880         {
881           const struct processors * sel;
882
883           for (sel = ptr->processors; sel->name != NULL; sel++)
884             if (streq (ptr->string, sel->name))
885               {
886                 /* Set the architecture define.  */
887                 if (i != ARM_OPT_SET_TUNE)
888                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
889
890                 /* Determine the processor core for which we should
891                    tune code-generation.  */
892                 if (/* -mcpu= is a sensible default.  */
893                     i == ARM_OPT_SET_CPU
894                     /* -mtune= overrides -mcpu= and -march=.  */
895                     || i == ARM_OPT_SET_TUNE)
896                   arm_tune = (enum processor_type) (sel - ptr->processors);
897
898                 /* Remember the CPU associated with this architecture.
899                    If no other option is used to set the CPU type,
900                    we'll use this to guess the most suitable tuning
901                    options.  */
902                 if (i == ARM_OPT_SET_ARCH)
903                   target_arch_cpu = sel->core;
904
905                 if (i != ARM_OPT_SET_TUNE)
906                   {
907                     /* If we have been given an architecture and a processor
908                        make sure that they are compatible.  We only generate
909                        a warning though, and we prefer the CPU over the
910                        architecture.  */
911                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
912                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
913                                ptr->string);
914
915                     insn_flags = sel->flags;
916                   }
917
918                 break;
919               }
920
921           if (sel->name == NULL)
922             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
923         }
924     }
925
926   /* Guess the tuning options from the architecture if necessary.  */
927   if (arm_tune == arm_none)
928     arm_tune = target_arch_cpu;
929
930   /* If the user did not specify a processor, choose one for them.  */
931   if (insn_flags == 0)
932     {
933       const struct processors * sel;
934       unsigned int        sought;
935       enum processor_type cpu;
936
937       cpu = TARGET_CPU_DEFAULT;
938       if (cpu == arm_none)
939         {
940 #ifdef SUBTARGET_CPU_DEFAULT
941           /* Use the subtarget default CPU if none was specified by
942              configure.  */
943           cpu = SUBTARGET_CPU_DEFAULT;
944 #endif
945           /* Default to ARM6.  */
946           if (cpu == arm_none)
947             cpu = arm6;
948         }
949       sel = &all_cores[cpu];
950
951       insn_flags = sel->flags;
952
953       /* Now check to see if the user has specified some command line
954          switch that require certain abilities from the cpu.  */
955       sought = 0;
956
957       if (TARGET_INTERWORK || TARGET_THUMB)
958         {
959           sought |= (FL_THUMB | FL_MODE32);
960
961           /* There are no ARM processors that support both APCS-26 and
962              interworking.  Therefore we force FL_MODE26 to be removed
963              from insn_flags here (if it was set), so that the search
964              below will always be able to find a compatible processor.  */
965           insn_flags &= ~FL_MODE26;
966         }
967
968       if (sought != 0 && ((sought & insn_flags) != sought))
969         {
970           /* Try to locate a CPU type that supports all of the abilities
971              of the default CPU, plus the extra abilities requested by
972              the user.  */
973           for (sel = all_cores; sel->name != NULL; sel++)
974             if ((sel->flags & sought) == (sought | insn_flags))
975               break;
976
977           if (sel->name == NULL)
978             {
979               unsigned current_bit_count = 0;
980               const struct processors * best_fit = NULL;
981
982               /* Ideally we would like to issue an error message here
983                  saying that it was not possible to find a CPU compatible
984                  with the default CPU, but which also supports the command
985                  line options specified by the programmer, and so they
986                  ought to use the -mcpu=<name> command line option to
987                  override the default CPU type.
988
989                  If we cannot find a cpu that has both the
990                  characteristics of the default cpu and the given
991                  command line options we scan the array again looking
992                  for a best match.  */
993               for (sel = all_cores; sel->name != NULL; sel++)
994                 if ((sel->flags & sought) == sought)
995                   {
996                     unsigned count;
997
998                     count = bit_count (sel->flags & insn_flags);
999
1000                     if (count >= current_bit_count)
1001                       {
1002                         best_fit = sel;
1003                         current_bit_count = count;
1004                       }
1005                   }
1006
1007               gcc_assert (best_fit);
1008               sel = best_fit;
1009             }
1010
1011           insn_flags = sel->flags;
1012         }
1013       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1014       if (arm_tune == arm_none)
1015         arm_tune = (enum processor_type) (sel - all_cores);
1016     }
1017
1018   /* The processor for which we should tune should now have been
1019      chosen.  */
1020   gcc_assert (arm_tune != arm_none);
1021
1022   tune_flags = all_cores[(int)arm_tune].flags;
1023   if (optimize_size)
1024     targetm.rtx_costs = arm_size_rtx_costs;
1025   else
1026     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1027
1028   /* Make sure that the processor choice does not conflict with any of the
1029      other command line choices.  */
1030   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1031     {
1032       warning (0, "target CPU does not support interworking" );
1033       target_flags &= ~MASK_INTERWORK;
1034     }
1035
1036   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1037     {
1038       warning (0, "target CPU does not support THUMB instructions");
1039       target_flags &= ~MASK_THUMB;
1040     }
1041
1042   if (TARGET_APCS_FRAME && TARGET_THUMB)
1043     {
1044       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1045       target_flags &= ~MASK_APCS_FRAME;
1046     }
1047
1048   /* Callee super interworking implies thumb interworking.  Adding
1049      this to the flags here simplifies the logic elsewhere.  */
1050   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1051       target_flags |= MASK_INTERWORK;
1052
1053   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1054      from here where no function is being compiled currently.  */
1055   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1056     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1057
1058   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1059     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1060
1061   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1062     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1063
1064   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1065     {
1066       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1067       target_flags |= MASK_APCS_FRAME;
1068     }
1069
1070   if (TARGET_POKE_FUNCTION_NAME)
1071     target_flags |= MASK_APCS_FRAME;
1072
1073   if (TARGET_APCS_REENT && flag_pic)
1074     error ("-fpic and -mapcs-reent are incompatible");
1075
1076   if (TARGET_APCS_REENT)
1077     warning (0, "APCS reentrant code not supported.  Ignored");
1078
1079   /* If this target is normally configured to use APCS frames, warn if they
1080      are turned off and debugging is turned on.  */
1081   if (TARGET_ARM
1082       && write_symbols != NO_DEBUG
1083       && !TARGET_APCS_FRAME
1084       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1085     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1086
1087   /* If stack checking is disabled, we can use r10 as the PIC register,
1088      which keeps r9 available.  */
1089   if (flag_pic)
1090     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1091
1092   if (TARGET_APCS_FLOAT)
1093     warning (0, "passing floating point arguments in fp regs not yet supported");
1094
1095   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1096   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1097   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1098   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1099   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1100   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1101   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1102   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1103   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1104   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1105
1106   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1107   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1108   thumb_code = (TARGET_ARM == 0);
1109   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1110   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1111   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1112
1113   /* V5 code we generate is completely interworking capable, so we turn off
1114      TARGET_INTERWORK here to avoid many tests later on.  */
1115
1116   /* XXX However, we must pass the right pre-processor defines to CPP
1117      or GLD can get confused.  This is a hack.  */
1118   if (TARGET_INTERWORK)
1119     arm_cpp_interwork = 1;
1120
1121   if (arm_arch5)
1122     target_flags &= ~MASK_INTERWORK;
1123
1124   if (target_abi_name)
1125     {
1126       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1127         {
1128           if (streq (arm_all_abis[i].name, target_abi_name))
1129             {
1130               arm_abi = arm_all_abis[i].abi_type;
1131               break;
1132             }
1133         }
1134       if (i == ARRAY_SIZE (arm_all_abis))
1135         error ("invalid ABI option: -mabi=%s", target_abi_name);
1136     }
1137   else
1138     arm_abi = ARM_DEFAULT_ABI;
1139
1140   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1141     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1142
1143   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1144     error ("iwmmxt abi requires an iwmmxt capable cpu");
1145
1146   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1147   if (target_fpu_name == NULL && target_fpe_name != NULL)
1148     {
1149       if (streq (target_fpe_name, "2"))
1150         target_fpu_name = "fpe2";
1151       else if (streq (target_fpe_name, "3"))
1152         target_fpu_name = "fpe3";
1153       else
1154         error ("invalid floating point emulation option: -mfpe=%s",
1155                target_fpe_name);
1156     }
1157   if (target_fpu_name != NULL)
1158     {
1159       /* The user specified a FPU.  */
1160       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1161         {
1162           if (streq (all_fpus[i].name, target_fpu_name))
1163             {
1164               arm_fpu_arch = all_fpus[i].fpu;
1165               arm_fpu_tune = arm_fpu_arch;
1166               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1167               break;
1168             }
1169         }
1170       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1171         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1172     }
1173   else
1174     {
1175 #ifdef FPUTYPE_DEFAULT
1176       /* Use the default if it is specified for this platform.  */
1177       arm_fpu_arch = FPUTYPE_DEFAULT;
1178       arm_fpu_tune = FPUTYPE_DEFAULT;
1179 #else
1180       /* Pick one based on CPU type.  */
1181       /* ??? Some targets assume FPA is the default.
1182       if ((insn_flags & FL_VFP) != 0)
1183         arm_fpu_arch = FPUTYPE_VFP;
1184       else
1185       */
1186       if (arm_arch_cirrus)
1187         arm_fpu_arch = FPUTYPE_MAVERICK;
1188       else
1189         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1190 #endif
1191       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1192         arm_fpu_tune = FPUTYPE_FPA;
1193       else
1194         arm_fpu_tune = arm_fpu_arch;
1195       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1196       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1197     }
1198
1199   if (target_float_abi_name != NULL)
1200     {
1201       /* The user specified a FP ABI.  */
1202       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1203         {
1204           if (streq (all_float_abis[i].name, target_float_abi_name))
1205             {
1206               arm_float_abi = all_float_abis[i].abi_type;
1207               break;
1208             }
1209         }
1210       if (i == ARRAY_SIZE (all_float_abis))
1211         error ("invalid floating point abi: -mfloat-abi=%s",
1212                target_float_abi_name);
1213     }
1214   else
1215     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1216
1217   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1218     sorry ("-mfloat-abi=hard and VFP");
1219
1220   /* If soft-float is specified then don't use FPU.  */
1221   if (TARGET_SOFT_FLOAT)
1222     arm_fpu_arch = FPUTYPE_NONE;
1223
1224   /* For arm2/3 there is no need to do any scheduling if there is only
1225      a floating point emulator, or we are doing software floating-point.  */
1226   if ((TARGET_SOFT_FLOAT
1227        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1228        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1229       && (tune_flags & FL_MODE32) == 0)
1230     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1231
1232   if (target_thread_switch)
1233     {
1234       if (strcmp (target_thread_switch, "soft") == 0)
1235         target_thread_pointer = TP_SOFT;
1236       else if (strcmp (target_thread_switch, "auto") == 0)
1237         target_thread_pointer = TP_AUTO;
1238       else if (strcmp (target_thread_switch, "cp15") == 0)
1239         target_thread_pointer = TP_CP15;
1240       else
1241         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1242     }
1243
1244   /* Use the cp15 method if it is available.  */
1245   if (target_thread_pointer == TP_AUTO)
1246     {
1247       if (arm_arch6k && !TARGET_THUMB)
1248         target_thread_pointer = TP_CP15;
1249       else
1250         target_thread_pointer = TP_SOFT;
1251     }
1252
1253   if (TARGET_HARD_TP && TARGET_THUMB)
1254     error ("can not use -mtp=cp15 with -mthumb");
1255
1256   /* Override the default structure alignment for AAPCS ABI.  */
1257   if (TARGET_AAPCS_BASED)
1258     arm_structure_size_boundary = 8;
1259
1260   if (structure_size_string != NULL)
1261     {
1262       int size = strtol (structure_size_string, NULL, 0);
1263
1264       if (size == 8 || size == 32
1265           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1266         arm_structure_size_boundary = size;
1267       else
1268         warning (0, "structure size boundary can only be set to %s",
1269                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1270     }
1271
1272   if (arm_pic_register_string != NULL)
1273     {
1274       int pic_register = decode_reg_name (arm_pic_register_string);
1275
1276       if (!flag_pic)
1277         warning (0, "-mpic-register= is useless without -fpic");
1278
1279       /* Prevent the user from choosing an obviously stupid PIC register.  */
1280       else if (pic_register < 0 || call_used_regs[pic_register]
1281                || pic_register == HARD_FRAME_POINTER_REGNUM
1282                || pic_register == STACK_POINTER_REGNUM
1283                || pic_register >= PC_REGNUM)
1284         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1285       else
1286         arm_pic_register = pic_register;
1287     }
1288
1289   if (TARGET_THUMB && flag_schedule_insns)
1290     {
1291       /* Don't warn since it's on by default in -O2.  */
1292       flag_schedule_insns = 0;
1293     }
1294
1295   if (optimize_size)
1296     {
1297       arm_constant_limit = 1;
1298
1299       /* If optimizing for size, bump the number of instructions that we
1300          are prepared to conditionally execute (even on a StrongARM).  */
1301       max_insns_skipped = 6;
1302     }
1303   else
1304     {
1305       /* For processors with load scheduling, it never costs more than
1306          2 cycles to load a constant, and the load scheduler may well
1307          reduce that to 1.  */
1308       if (arm_ld_sched)
1309         arm_constant_limit = 1;
1310
1311       /* On XScale the longer latency of a load makes it more difficult
1312          to achieve a good schedule, so it's faster to synthesize
1313          constants that can be done in two insns.  */
1314       if (arm_tune_xscale)
1315         arm_constant_limit = 2;
1316
1317       /* StrongARM has early execution of branches, so a sequence
1318          that is worth skipping is shorter.  */
1319       if (arm_tune_strongarm)
1320         max_insns_skipped = 3;
1321     }
1322
1323   /* Register global variables with the garbage collector.  */
1324   arm_add_gc_roots ();
1325 }
1326
1327 static void
1328 arm_add_gc_roots (void)
1329 {
1330   gcc_obstack_init(&minipool_obstack);
1331   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1332 }
1333 \f
1334 /* A table of known ARM exception types.
1335    For use with the interrupt function attribute.  */
1336
1337 typedef struct
1338 {
1339   const char *const arg;
1340   const unsigned long return_value;
1341 }
1342 isr_attribute_arg;
1343
1344 static const isr_attribute_arg isr_attribute_args [] =
1345 {
1346   { "IRQ",   ARM_FT_ISR },
1347   { "irq",   ARM_FT_ISR },
1348   { "FIQ",   ARM_FT_FIQ },
1349   { "fiq",   ARM_FT_FIQ },
1350   { "ABORT", ARM_FT_ISR },
1351   { "abort", ARM_FT_ISR },
1352   { "ABORT", ARM_FT_ISR },
1353   { "abort", ARM_FT_ISR },
1354   { "UNDEF", ARM_FT_EXCEPTION },
1355   { "undef", ARM_FT_EXCEPTION },
1356   { "SWI",   ARM_FT_EXCEPTION },
1357   { "swi",   ARM_FT_EXCEPTION },
1358   { NULL,    ARM_FT_NORMAL }
1359 };
1360
1361 /* Returns the (interrupt) function type of the current
1362    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1363
1364 static unsigned long
1365 arm_isr_value (tree argument)
1366 {
1367   const isr_attribute_arg * ptr;
1368   const char *              arg;
1369
1370   /* No argument - default to IRQ.  */
1371   if (argument == NULL_TREE)
1372     return ARM_FT_ISR;
1373
1374   /* Get the value of the argument.  */
1375   if (TREE_VALUE (argument) == NULL_TREE
1376       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1377     return ARM_FT_UNKNOWN;
1378
1379   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1380
1381   /* Check it against the list of known arguments.  */
1382   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1383     if (streq (arg, ptr->arg))
1384       return ptr->return_value;
1385
1386   /* An unrecognized interrupt type.  */
1387   return ARM_FT_UNKNOWN;
1388 }
1389
1390 /* Computes the type of the current function.  */
1391
1392 static unsigned long
1393 arm_compute_func_type (void)
1394 {
1395   unsigned long type = ARM_FT_UNKNOWN;
1396   tree a;
1397   tree attr;
1398
1399   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1400
1401   /* Decide if the current function is volatile.  Such functions
1402      never return, and many memory cycles can be saved by not storing
1403      register values that will never be needed again.  This optimization
1404      was added to speed up context switching in a kernel application.  */
1405   if (optimize > 0
1406       && TREE_NOTHROW (current_function_decl)
1407       && TREE_THIS_VOLATILE (current_function_decl))
1408     type |= ARM_FT_VOLATILE;
1409
1410   if (cfun->static_chain_decl != NULL)
1411     type |= ARM_FT_NESTED;
1412
1413   attr = DECL_ATTRIBUTES (current_function_decl);
1414
1415   a = lookup_attribute ("naked", attr);
1416   if (a != NULL_TREE)
1417     type |= ARM_FT_NAKED;
1418
1419   a = lookup_attribute ("isr", attr);
1420   if (a == NULL_TREE)
1421     a = lookup_attribute ("interrupt", attr);
1422
1423   if (a == NULL_TREE)
1424     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1425   else
1426     type |= arm_isr_value (TREE_VALUE (a));
1427
1428   return type;
1429 }
1430
1431 /* Returns the type of the current function.  */
1432
1433 unsigned long
1434 arm_current_func_type (void)
1435 {
1436   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1437     cfun->machine->func_type = arm_compute_func_type ();
1438
1439   return cfun->machine->func_type;
1440 }
1441 \f
1442 /* Return 1 if it is possible to return using a single instruction.
1443    If SIBLING is non-null, this is a test for a return before a sibling
1444    call.  SIBLING is the call insn, so we can examine its register usage.  */
1445
1446 int
1447 use_return_insn (int iscond, rtx sibling)
1448 {
1449   int regno;
1450   unsigned int func_type;
1451   unsigned long saved_int_regs;
1452   unsigned HOST_WIDE_INT stack_adjust;
1453   arm_stack_offsets *offsets;
1454
1455   /* Never use a return instruction before reload has run.  */
1456   if (!reload_completed)
1457     return 0;
1458
1459   func_type = arm_current_func_type ();
1460
1461   /* Naked functions and volatile functions need special
1462      consideration.  */
1463   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1464     return 0;
1465
1466   /* So do interrupt functions that use the frame pointer.  */
1467   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1468     return 0;
1469
1470   offsets = arm_get_frame_offsets ();
1471   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1472
1473   /* As do variadic functions.  */
1474   if (current_function_pretend_args_size
1475       || cfun->machine->uses_anonymous_args
1476       /* Or if the function calls __builtin_eh_return () */
1477       || current_function_calls_eh_return
1478       /* Or if the function calls alloca */
1479       || current_function_calls_alloca
1480       /* Or if there is a stack adjustment.  However, if the stack pointer
1481          is saved on the stack, we can use a pre-incrementing stack load.  */
1482       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1483     return 0;
1484
1485   saved_int_regs = arm_compute_save_reg_mask ();
1486
1487   /* Unfortunately, the insn
1488
1489        ldmib sp, {..., sp, ...}
1490
1491      triggers a bug on most SA-110 based devices, such that the stack
1492      pointer won't be correctly restored if the instruction takes a
1493      page fault.  We work around this problem by popping r3 along with
1494      the other registers, since that is never slower than executing
1495      another instruction.
1496
1497      We test for !arm_arch5 here, because code for any architecture
1498      less than this could potentially be run on one of the buggy
1499      chips.  */
1500   if (stack_adjust == 4 && !arm_arch5)
1501     {
1502       /* Validate that r3 is a call-clobbered register (always true in
1503          the default abi) ...  */
1504       if (!call_used_regs[3])
1505         return 0;
1506
1507       /* ... that it isn't being used for a return value ... */
1508       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1509         return 0;
1510
1511       /* ... or for a tail-call argument ...  */
1512       if (sibling)
1513         {
1514           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1515
1516           if (find_regno_fusage (sibling, USE, 3))
1517             return 0;
1518         }
1519
1520       /* ... and that there are no call-saved registers in r0-r2
1521          (always true in the default ABI).  */
1522       if (saved_int_regs & 0x7)
1523         return 0;
1524     }
1525
1526   /* Can't be done if interworking with Thumb, and any registers have been
1527      stacked.  */
1528   if (TARGET_INTERWORK && saved_int_regs != 0)
1529     return 0;
1530
1531   /* On StrongARM, conditional returns are expensive if they aren't
1532      taken and multiple registers have been stacked.  */
1533   if (iscond && arm_tune_strongarm)
1534     {
1535       /* Conditional return when just the LR is stored is a simple
1536          conditional-load instruction, that's not expensive.  */
1537       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1538         return 0;
1539
1540       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1541         return 0;
1542     }
1543
1544   /* If there are saved registers but the LR isn't saved, then we need
1545      two instructions for the return.  */
1546   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1547     return 0;
1548
1549   /* Can't be done if any of the FPA regs are pushed,
1550      since this also requires an insn.  */
1551   if (TARGET_HARD_FLOAT && TARGET_FPA)
1552     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1553       if (regs_ever_live[regno] && !call_used_regs[regno])
1554         return 0;
1555
1556   /* Likewise VFP regs.  */
1557   if (TARGET_HARD_FLOAT && TARGET_VFP)
1558     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1559       if (regs_ever_live[regno] && !call_used_regs[regno])
1560         return 0;
1561
1562   if (TARGET_REALLY_IWMMXT)
1563     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1564       if (regs_ever_live[regno] && ! call_used_regs [regno])
1565         return 0;
1566
1567   return 1;
1568 }
1569
1570 /* Return TRUE if int I is a valid immediate ARM constant.  */
1571
1572 int
1573 const_ok_for_arm (HOST_WIDE_INT i)
1574 {
1575   int lowbit;
1576
1577   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1578      be all zero, or all one.  */
1579   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1580       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1581           != ((~(unsigned HOST_WIDE_INT) 0)
1582               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1583     return FALSE;
1584
1585   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1586
1587   /* Fast return for 0 and small values.  We must do this for zero, since
1588      the code below can't handle that one case.  */
1589   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1590     return TRUE;
1591
1592   /* Get the number of trailing zeros, rounded down to the nearest even
1593      number.  */
1594   lowbit = (ffs ((int) i) - 1) & ~1;
1595
1596   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1597     return TRUE;
1598   else if (lowbit <= 4
1599            && ((i & ~0xc000003f) == 0
1600                || (i & ~0xf000000f) == 0
1601                || (i & ~0xfc000003) == 0))
1602     return TRUE;
1603
1604   return FALSE;
1605 }
1606
1607 /* Return true if I is a valid constant for the operation CODE.  */
1608 static int
1609 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1610 {
1611   if (const_ok_for_arm (i))
1612     return 1;
1613
1614   switch (code)
1615     {
1616     case PLUS:
1617       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1618
1619     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1620     case XOR:
1621     case IOR:
1622       return 0;
1623
1624     case AND:
1625       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1626
1627     default:
1628       gcc_unreachable ();
1629     }
1630 }
1631
1632 /* Emit a sequence of insns to handle a large constant.
1633    CODE is the code of the operation required, it can be any of SET, PLUS,
1634    IOR, AND, XOR, MINUS;
1635    MODE is the mode in which the operation is being performed;
1636    VAL is the integer to operate on;
1637    SOURCE is the other operand (a register, or a null-pointer for SET);
1638    SUBTARGETS means it is safe to create scratch registers if that will
1639    either produce a simpler sequence, or we will want to cse the values.
1640    Return value is the number of insns emitted.  */
1641
1642 int
1643 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1644                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1645 {
1646   rtx cond;
1647
1648   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1649     cond = COND_EXEC_TEST (PATTERN (insn));
1650   else
1651     cond = NULL_RTX;
1652
1653   if (subtargets || code == SET
1654       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1655           && REGNO (target) != REGNO (source)))
1656     {
1657       /* After arm_reorg has been called, we can't fix up expensive
1658          constants by pushing them into memory so we must synthesize
1659          them in-line, regardless of the cost.  This is only likely to
1660          be more costly on chips that have load delay slots and we are
1661          compiling without running the scheduler (so no splitting
1662          occurred before the final instruction emission).
1663
1664          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1665       */
1666       if (!after_arm_reorg
1667           && !cond
1668           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1669                                 1, 0)
1670               > arm_constant_limit + (code != SET)))
1671         {
1672           if (code == SET)
1673             {
1674               /* Currently SET is the only monadic value for CODE, all
1675                  the rest are diadic.  */
1676               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1677               return 1;
1678             }
1679           else
1680             {
1681               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1682
1683               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1684               /* For MINUS, the value is subtracted from, since we never
1685                  have subtraction of a constant.  */
1686               if (code == MINUS)
1687                 emit_insn (gen_rtx_SET (VOIDmode, target,
1688                                         gen_rtx_MINUS (mode, temp, source)));
1689               else
1690                 emit_insn (gen_rtx_SET (VOIDmode, target,
1691                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1692               return 2;
1693             }
1694         }
1695     }
1696
1697   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1698                            1);
1699 }
1700
1701 static int
1702 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1703 {
1704   HOST_WIDE_INT temp1;
1705   int num_insns = 0;
1706   do
1707     {
1708       int end;
1709
1710       if (i <= 0)
1711         i += 32;
1712       if (remainder & (3 << (i - 2)))
1713         {
1714           end = i - 8;
1715           if (end < 0)
1716             end += 32;
1717           temp1 = remainder & ((0x0ff << end)
1718                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1719           remainder &= ~temp1;
1720           num_insns++;
1721           i -= 6;
1722         }
1723       i -= 2;
1724     } while (remainder);
1725   return num_insns;
1726 }
1727
1728 /* Emit an instruction with the indicated PATTERN.  If COND is
1729    non-NULL, conditionalize the execution of the instruction on COND
1730    being true.  */
1731
1732 static void
1733 emit_constant_insn (rtx cond, rtx pattern)
1734 {
1735   if (cond)
1736     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1737   emit_insn (pattern);
1738 }
1739
1740 /* As above, but extra parameter GENERATE which, if clear, suppresses
1741    RTL generation.  */
1742
1743 static int
1744 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1745                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1746                   int generate)
1747 {
1748   int can_invert = 0;
1749   int can_negate = 0;
1750   int can_negate_initial = 0;
1751   int can_shift = 0;
1752   int i;
1753   int num_bits_set = 0;
1754   int set_sign_bit_copies = 0;
1755   int clear_sign_bit_copies = 0;
1756   int clear_zero_bit_copies = 0;
1757   int set_zero_bit_copies = 0;
1758   int insns = 0;
1759   unsigned HOST_WIDE_INT temp1, temp2;
1760   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1761
1762   /* Find out which operations are safe for a given CODE.  Also do a quick
1763      check for degenerate cases; these can occur when DImode operations
1764      are split.  */
1765   switch (code)
1766     {
1767     case SET:
1768       can_invert = 1;
1769       can_shift = 1;
1770       can_negate = 1;
1771       break;
1772
1773     case PLUS:
1774       can_negate = 1;
1775       can_negate_initial = 1;
1776       break;
1777
1778     case IOR:
1779       if (remainder == 0xffffffff)
1780         {
1781           if (generate)
1782             emit_constant_insn (cond,
1783                                 gen_rtx_SET (VOIDmode, target,
1784                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1785           return 1;
1786         }
1787       if (remainder == 0)
1788         {
1789           if (reload_completed && rtx_equal_p (target, source))
1790             return 0;
1791           if (generate)
1792             emit_constant_insn (cond,
1793                                 gen_rtx_SET (VOIDmode, target, source));
1794           return 1;
1795         }
1796       break;
1797
1798     case AND:
1799       if (remainder == 0)
1800         {
1801           if (generate)
1802             emit_constant_insn (cond,
1803                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1804           return 1;
1805         }
1806       if (remainder == 0xffffffff)
1807         {
1808           if (reload_completed && rtx_equal_p (target, source))
1809             return 0;
1810           if (generate)
1811             emit_constant_insn (cond,
1812                                 gen_rtx_SET (VOIDmode, target, source));
1813           return 1;
1814         }
1815       can_invert = 1;
1816       break;
1817
1818     case XOR:
1819       if (remainder == 0)
1820         {
1821           if (reload_completed && rtx_equal_p (target, source))
1822             return 0;
1823           if (generate)
1824             emit_constant_insn (cond,
1825                                 gen_rtx_SET (VOIDmode, target, source));
1826           return 1;
1827         }
1828
1829       /* We don't know how to handle other cases yet.  */
1830       gcc_assert (remainder == 0xffffffff);
1831
1832       if (generate)
1833         emit_constant_insn (cond,
1834                             gen_rtx_SET (VOIDmode, target,
1835                                          gen_rtx_NOT (mode, source)));
1836       return 1;
1837
1838     case MINUS:
1839       /* We treat MINUS as (val - source), since (source - val) is always
1840          passed as (source + (-val)).  */
1841       if (remainder == 0)
1842         {
1843           if (generate)
1844             emit_constant_insn (cond,
1845                                 gen_rtx_SET (VOIDmode, target,
1846                                              gen_rtx_NEG (mode, source)));
1847           return 1;
1848         }
1849       if (const_ok_for_arm (val))
1850         {
1851           if (generate)
1852             emit_constant_insn (cond,
1853                                 gen_rtx_SET (VOIDmode, target,
1854                                              gen_rtx_MINUS (mode, GEN_INT (val),
1855                                                             source)));
1856           return 1;
1857         }
1858       can_negate = 1;
1859
1860       break;
1861
1862     default:
1863       gcc_unreachable ();
1864     }
1865
1866   /* If we can do it in one insn get out quickly.  */
1867   if (const_ok_for_arm (val)
1868       || (can_negate_initial && const_ok_for_arm (-val))
1869       || (can_invert && const_ok_for_arm (~val)))
1870     {
1871       if (generate)
1872         emit_constant_insn (cond,
1873                             gen_rtx_SET (VOIDmode, target,
1874                                          (source
1875                                           ? gen_rtx_fmt_ee (code, mode, source,
1876                                                             GEN_INT (val))
1877                                           : GEN_INT (val))));
1878       return 1;
1879     }
1880
1881   /* Calculate a few attributes that may be useful for specific
1882      optimizations.  */
1883   for (i = 31; i >= 0; i--)
1884     {
1885       if ((remainder & (1 << i)) == 0)
1886         clear_sign_bit_copies++;
1887       else
1888         break;
1889     }
1890
1891   for (i = 31; i >= 0; i--)
1892     {
1893       if ((remainder & (1 << i)) != 0)
1894         set_sign_bit_copies++;
1895       else
1896         break;
1897     }
1898
1899   for (i = 0; i <= 31; i++)
1900     {
1901       if ((remainder & (1 << i)) == 0)
1902         clear_zero_bit_copies++;
1903       else
1904         break;
1905     }
1906
1907   for (i = 0; i <= 31; i++)
1908     {
1909       if ((remainder & (1 << i)) != 0)
1910         set_zero_bit_copies++;
1911       else
1912         break;
1913     }
1914
1915   switch (code)
1916     {
1917     case SET:
1918       /* See if we can do this by sign_extending a constant that is known
1919          to be negative.  This is a good, way of doing it, since the shift
1920          may well merge into a subsequent insn.  */
1921       if (set_sign_bit_copies > 1)
1922         {
1923           if (const_ok_for_arm
1924               (temp1 = ARM_SIGN_EXTEND (remainder
1925                                         << (set_sign_bit_copies - 1))))
1926             {
1927               if (generate)
1928                 {
1929                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1930                   emit_constant_insn (cond,
1931                                       gen_rtx_SET (VOIDmode, new_src,
1932                                                    GEN_INT (temp1)));
1933                   emit_constant_insn (cond,
1934                                       gen_ashrsi3 (target, new_src,
1935                                                    GEN_INT (set_sign_bit_copies - 1)));
1936                 }
1937               return 2;
1938             }
1939           /* For an inverted constant, we will need to set the low bits,
1940              these will be shifted out of harm's way.  */
1941           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1942           if (const_ok_for_arm (~temp1))
1943             {
1944               if (generate)
1945                 {
1946                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1947                   emit_constant_insn (cond,
1948                                       gen_rtx_SET (VOIDmode, new_src,
1949                                                    GEN_INT (temp1)));
1950                   emit_constant_insn (cond,
1951                                       gen_ashrsi3 (target, new_src,
1952                                                    GEN_INT (set_sign_bit_copies - 1)));
1953                 }
1954               return 2;
1955             }
1956         }
1957
1958       /* See if we can calculate the value as the difference between two
1959          valid immediates.  */
1960       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1961         {
1962           int topshift = clear_sign_bit_copies & ~1;
1963
1964           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1965                                    & (0xff000000 >> topshift));
1966
1967           /* If temp1 is zero, then that means the 9 most significant
1968              bits of remainder were 1 and we've caused it to overflow.
1969              When topshift is 0 we don't need to do anything since we
1970              can borrow from 'bit 32'.  */
1971           if (temp1 == 0 && topshift != 0)
1972             temp1 = 0x80000000 >> (topshift - 1);
1973
1974           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1975
1976           if (const_ok_for_arm (temp2))
1977             {
1978               if (generate)
1979                 {
1980                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1981                   emit_constant_insn (cond,
1982                                       gen_rtx_SET (VOIDmode, new_src,
1983                                                    GEN_INT (temp1)));
1984                   emit_constant_insn (cond,
1985                                       gen_addsi3 (target, new_src,
1986                                                   GEN_INT (-temp2)));
1987                 }
1988
1989               return 2;
1990             }
1991         }
1992
1993       /* See if we can generate this by setting the bottom (or the top)
1994          16 bits, and then shifting these into the other half of the
1995          word.  We only look for the simplest cases, to do more would cost
1996          too much.  Be careful, however, not to generate this when the
1997          alternative would take fewer insns.  */
1998       if (val & 0xffff0000)
1999         {
2000           temp1 = remainder & 0xffff0000;
2001           temp2 = remainder & 0x0000ffff;
2002
2003           /* Overlaps outside this range are best done using other methods.  */
2004           for (i = 9; i < 24; i++)
2005             {
2006               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2007                   && !const_ok_for_arm (temp2))
2008                 {
2009                   rtx new_src = (subtargets
2010                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2011                                  : target);
2012                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2013                                             source, subtargets, generate);
2014                   source = new_src;
2015                   if (generate)
2016                     emit_constant_insn
2017                       (cond,
2018                        gen_rtx_SET
2019                        (VOIDmode, target,
2020                         gen_rtx_IOR (mode,
2021                                      gen_rtx_ASHIFT (mode, source,
2022                                                      GEN_INT (i)),
2023                                      source)));
2024                   return insns + 1;
2025                 }
2026             }
2027
2028           /* Don't duplicate cases already considered.  */
2029           for (i = 17; i < 24; i++)
2030             {
2031               if (((temp1 | (temp1 >> i)) == remainder)
2032                   && !const_ok_for_arm (temp1))
2033                 {
2034                   rtx new_src = (subtargets
2035                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2036                                  : target);
2037                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2038                                             source, subtargets, generate);
2039                   source = new_src;
2040                   if (generate)
2041                     emit_constant_insn
2042                       (cond,
2043                        gen_rtx_SET (VOIDmode, target,
2044                                     gen_rtx_IOR
2045                                     (mode,
2046                                      gen_rtx_LSHIFTRT (mode, source,
2047                                                        GEN_INT (i)),
2048                                      source)));
2049                   return insns + 1;
2050                 }
2051             }
2052         }
2053       break;
2054
2055     case IOR:
2056     case XOR:
2057       /* If we have IOR or XOR, and the constant can be loaded in a
2058          single instruction, and we can find a temporary to put it in,
2059          then this can be done in two instructions instead of 3-4.  */
2060       if (subtargets
2061           /* TARGET can't be NULL if SUBTARGETS is 0 */
2062           || (reload_completed && !reg_mentioned_p (target, source)))
2063         {
2064           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2065             {
2066               if (generate)
2067                 {
2068                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2069
2070                   emit_constant_insn (cond,
2071                                       gen_rtx_SET (VOIDmode, sub,
2072                                                    GEN_INT (val)));
2073                   emit_constant_insn (cond,
2074                                       gen_rtx_SET (VOIDmode, target,
2075                                                    gen_rtx_fmt_ee (code, mode,
2076                                                                    source, sub)));
2077                 }
2078               return 2;
2079             }
2080         }
2081
2082       if (code == XOR)
2083         break;
2084
2085       if (set_sign_bit_copies > 8
2086           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2087         {
2088           if (generate)
2089             {
2090               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2091               rtx shift = GEN_INT (set_sign_bit_copies);
2092
2093               emit_constant_insn
2094                 (cond,
2095                  gen_rtx_SET (VOIDmode, sub,
2096                               gen_rtx_NOT (mode,
2097                                            gen_rtx_ASHIFT (mode,
2098                                                            source,
2099                                                            shift))));
2100               emit_constant_insn
2101                 (cond,
2102                  gen_rtx_SET (VOIDmode, target,
2103                               gen_rtx_NOT (mode,
2104                                            gen_rtx_LSHIFTRT (mode, sub,
2105                                                              shift))));
2106             }
2107           return 2;
2108         }
2109
2110       if (set_zero_bit_copies > 8
2111           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2112         {
2113           if (generate)
2114             {
2115               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2116               rtx shift = GEN_INT (set_zero_bit_copies);
2117
2118               emit_constant_insn
2119                 (cond,
2120                  gen_rtx_SET (VOIDmode, sub,
2121                               gen_rtx_NOT (mode,
2122                                            gen_rtx_LSHIFTRT (mode,
2123                                                              source,
2124                                                              shift))));
2125               emit_constant_insn
2126                 (cond,
2127                  gen_rtx_SET (VOIDmode, target,
2128                               gen_rtx_NOT (mode,
2129                                            gen_rtx_ASHIFT (mode, sub,
2130                                                            shift))));
2131             }
2132           return 2;
2133         }
2134
2135       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2136         {
2137           if (generate)
2138             {
2139               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2140               emit_constant_insn (cond,
2141                                   gen_rtx_SET (VOIDmode, sub,
2142                                                gen_rtx_NOT (mode, source)));
2143               source = sub;
2144               if (subtargets)
2145                 sub = gen_reg_rtx (mode);
2146               emit_constant_insn (cond,
2147                                   gen_rtx_SET (VOIDmode, sub,
2148                                                gen_rtx_AND (mode, source,
2149                                                             GEN_INT (temp1))));
2150               emit_constant_insn (cond,
2151                                   gen_rtx_SET (VOIDmode, target,
2152                                                gen_rtx_NOT (mode, sub)));
2153             }
2154           return 3;
2155         }
2156       break;
2157
2158     case AND:
2159       /* See if two shifts will do 2 or more insn's worth of work.  */
2160       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2161         {
2162           HOST_WIDE_INT shift_mask = ((0xffffffff
2163                                        << (32 - clear_sign_bit_copies))
2164                                       & 0xffffffff);
2165
2166           if ((remainder | shift_mask) != 0xffffffff)
2167             {
2168               if (generate)
2169                 {
2170                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2171                   insns = arm_gen_constant (AND, mode, cond,
2172                                             remainder | shift_mask,
2173                                             new_src, source, subtargets, 1);
2174                   source = new_src;
2175                 }
2176               else
2177                 {
2178                   rtx targ = subtargets ? NULL_RTX : target;
2179                   insns = arm_gen_constant (AND, mode, cond,
2180                                             remainder | shift_mask,
2181                                             targ, source, subtargets, 0);
2182                 }
2183             }
2184
2185           if (generate)
2186             {
2187               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2188               rtx shift = GEN_INT (clear_sign_bit_copies);
2189
2190               emit_insn (gen_ashlsi3 (new_src, source, shift));
2191               emit_insn (gen_lshrsi3 (target, new_src, shift));
2192             }
2193
2194           return insns + 2;
2195         }
2196
2197       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2198         {
2199           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2200
2201           if ((remainder | shift_mask) != 0xffffffff)
2202             {
2203               if (generate)
2204                 {
2205                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2206
2207                   insns = arm_gen_constant (AND, mode, cond,
2208                                             remainder | shift_mask,
2209                                             new_src, source, subtargets, 1);
2210                   source = new_src;
2211                 }
2212               else
2213                 {
2214                   rtx targ = subtargets ? NULL_RTX : target;
2215
2216                   insns = arm_gen_constant (AND, mode, cond,
2217                                             remainder | shift_mask,
2218                                             targ, source, subtargets, 0);
2219                 }
2220             }
2221
2222           if (generate)
2223             {
2224               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2225               rtx shift = GEN_INT (clear_zero_bit_copies);
2226
2227               emit_insn (gen_lshrsi3 (new_src, source, shift));
2228               emit_insn (gen_ashlsi3 (target, new_src, shift));
2229             }
2230
2231           return insns + 2;
2232         }
2233
2234       break;
2235
2236     default:
2237       break;
2238     }
2239
2240   for (i = 0; i < 32; i++)
2241     if (remainder & (1 << i))
2242       num_bits_set++;
2243
2244   if (code == AND || (can_invert && num_bits_set > 16))
2245     remainder = (~remainder) & 0xffffffff;
2246   else if (code == PLUS && num_bits_set > 16)
2247     remainder = (-remainder) & 0xffffffff;
2248   else
2249     {
2250       can_invert = 0;
2251       can_negate = 0;
2252     }
2253
2254   /* Now try and find a way of doing the job in either two or three
2255      instructions.
2256      We start by looking for the largest block of zeros that are aligned on
2257      a 2-bit boundary, we then fill up the temps, wrapping around to the
2258      top of the word when we drop off the bottom.
2259      In the worst case this code should produce no more than four insns.  */
2260   {
2261     int best_start = 0;
2262     int best_consecutive_zeros = 0;
2263
2264     for (i = 0; i < 32; i += 2)
2265       {
2266         int consecutive_zeros = 0;
2267
2268         if (!(remainder & (3 << i)))
2269           {
2270             while ((i < 32) && !(remainder & (3 << i)))
2271               {
2272                 consecutive_zeros += 2;
2273                 i += 2;
2274               }
2275             if (consecutive_zeros > best_consecutive_zeros)
2276               {
2277                 best_consecutive_zeros = consecutive_zeros;
2278                 best_start = i - consecutive_zeros;
2279               }
2280             i -= 2;
2281           }
2282       }
2283
2284     /* So long as it won't require any more insns to do so, it's
2285        desirable to emit a small constant (in bits 0...9) in the last
2286        insn.  This way there is more chance that it can be combined with
2287        a later addressing insn to form a pre-indexed load or store
2288        operation.  Consider:
2289
2290                *((volatile int *)0xe0000100) = 1;
2291                *((volatile int *)0xe0000110) = 2;
2292
2293        We want this to wind up as:
2294
2295                 mov rA, #0xe0000000
2296                 mov rB, #1
2297                 str rB, [rA, #0x100]
2298                 mov rB, #2
2299                 str rB, [rA, #0x110]
2300
2301        rather than having to synthesize both large constants from scratch.
2302
2303        Therefore, we calculate how many insns would be required to emit
2304        the constant starting from `best_start', and also starting from
2305        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2306        yield a shorter sequence, we may as well use zero.  */
2307     if (best_start != 0
2308         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2309         && (count_insns_for_constant (remainder, 0) <=
2310             count_insns_for_constant (remainder, best_start)))
2311       best_start = 0;
2312
2313     /* Now start emitting the insns.  */
2314     i = best_start;
2315     do
2316       {
2317         int end;
2318
2319         if (i <= 0)
2320           i += 32;
2321         if (remainder & (3 << (i - 2)))
2322           {
2323             end = i - 8;
2324             if (end < 0)
2325               end += 32;
2326             temp1 = remainder & ((0x0ff << end)
2327                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2328             remainder &= ~temp1;
2329
2330             if (generate)
2331               {
2332                 rtx new_src, temp1_rtx;
2333
2334                 if (code == SET || code == MINUS)
2335                   {
2336                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2337                     if (can_invert && code != MINUS)
2338                       temp1 = ~temp1;
2339                   }
2340                 else
2341                   {
2342                     if (remainder && subtargets)
2343                       new_src = gen_reg_rtx (mode);
2344                     else
2345                       new_src = target;
2346                     if (can_invert)
2347                       temp1 = ~temp1;
2348                     else if (can_negate)
2349                       temp1 = -temp1;
2350                   }
2351
2352                 temp1 = trunc_int_for_mode (temp1, mode);
2353                 temp1_rtx = GEN_INT (temp1);
2354
2355                 if (code == SET)
2356                   ;
2357                 else if (code == MINUS)
2358                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2359                 else
2360                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2361
2362                 emit_constant_insn (cond,
2363                                     gen_rtx_SET (VOIDmode, new_src,
2364                                                  temp1_rtx));
2365                 source = new_src;
2366               }
2367
2368             if (code == SET)
2369               {
2370                 can_invert = 0;
2371                 code = PLUS;
2372               }
2373             else if (code == MINUS)
2374               code = PLUS;
2375
2376             insns++;
2377             i -= 6;
2378           }
2379         i -= 2;
2380       }
2381     while (remainder);
2382   }
2383
2384   return insns;
2385 }
2386
2387 /* Canonicalize a comparison so that we are more likely to recognize it.
2388    This can be done for a few constant compares, where we can make the
2389    immediate value easier to load.  */
2390
2391 enum rtx_code
2392 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2393                              rtx * op1)
2394 {
2395   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2396   unsigned HOST_WIDE_INT maxval;
2397   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2398
2399   switch (code)
2400     {
2401     case EQ:
2402     case NE:
2403       return code;
2404
2405     case GT:
2406     case LE:
2407       if (i != maxval
2408           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2409         {
2410           *op1 = GEN_INT (i + 1);
2411           return code == GT ? GE : LT;
2412         }
2413       break;
2414
2415     case GE:
2416     case LT:
2417       if (i != ~maxval
2418           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2419         {
2420           *op1 = GEN_INT (i - 1);
2421           return code == GE ? GT : LE;
2422         }
2423       break;
2424
2425     case GTU:
2426     case LEU:
2427       if (i != ~((unsigned HOST_WIDE_INT) 0)
2428           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2429         {
2430           *op1 = GEN_INT (i + 1);
2431           return code == GTU ? GEU : LTU;
2432         }
2433       break;
2434
2435     case GEU:
2436     case LTU:
2437       if (i != 0
2438           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2439         {
2440           *op1 = GEN_INT (i - 1);
2441           return code == GEU ? GTU : LEU;
2442         }
2443       break;
2444
2445     default:
2446       gcc_unreachable ();
2447     }
2448
2449   return code;
2450 }
2451
2452
2453 /* Define how to find the value returned by a function.  */
2454
2455 rtx
2456 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2457 {
2458   enum machine_mode mode;
2459   int unsignedp ATTRIBUTE_UNUSED;
2460   rtx r ATTRIBUTE_UNUSED;
2461
2462   mode = TYPE_MODE (type);
2463   /* Promote integer types.  */
2464   if (INTEGRAL_TYPE_P (type))
2465     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2466
2467   /* Promotes small structs returned in a register to full-word size
2468      for big-endian AAPCS.  */
2469   if (arm_return_in_msb (type))
2470     {
2471       HOST_WIDE_INT size = int_size_in_bytes (type);
2472       if (size % UNITS_PER_WORD != 0)
2473         {
2474           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2475           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2476         }
2477     }
2478
2479   return LIBCALL_VALUE(mode);
2480 }
2481
2482 /* Determine the amount of memory needed to store the possible return
2483    registers of an untyped call.  */
2484 int
2485 arm_apply_result_size (void)
2486 {
2487   int size = 16;
2488
2489   if (TARGET_ARM)
2490     {
2491       if (TARGET_HARD_FLOAT_ABI)
2492         {
2493           if (TARGET_FPA)
2494             size += 12;
2495           if (TARGET_MAVERICK)
2496             size += 8;
2497         }
2498       if (TARGET_IWMMXT_ABI)
2499         size += 8;
2500     }
2501
2502   return size;
2503 }
2504
2505 /* Decide whether a type should be returned in memory (true)
2506    or in a register (false).  This is called by the macro
2507    RETURN_IN_MEMORY.  */
2508 int
2509 arm_return_in_memory (tree type)
2510 {
2511   HOST_WIDE_INT size;
2512
2513   if (!AGGREGATE_TYPE_P (type) &&
2514       (TREE_CODE (type) != VECTOR_TYPE) &&
2515       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2516     /* All simple types are returned in registers.
2517        For AAPCS, complex types are treated the same as aggregates.  */
2518     return 0;
2519
2520   size = int_size_in_bytes (type);
2521
2522   if (arm_abi != ARM_ABI_APCS)
2523     {
2524       /* ATPCS and later return aggregate types in memory only if they are
2525          larger than a word (or are variable size).  */
2526       return (size < 0 || size > UNITS_PER_WORD);
2527     }
2528
2529   /* To maximize backwards compatibility with previous versions of gcc,
2530      return vectors up to 4 words in registers.  */
2531   if (TREE_CODE (type) == VECTOR_TYPE)
2532     return (size < 0 || size > (4 * UNITS_PER_WORD));
2533
2534   /* For the arm-wince targets we choose to be compatible with Microsoft's
2535      ARM and Thumb compilers, which always return aggregates in memory.  */
2536 #ifndef ARM_WINCE
2537   /* All structures/unions bigger than one word are returned in memory.
2538      Also catch the case where int_size_in_bytes returns -1.  In this case
2539      the aggregate is either huge or of variable size, and in either case
2540      we will want to return it via memory and not in a register.  */
2541   if (size < 0 || size > UNITS_PER_WORD)
2542     return 1;
2543
2544   if (TREE_CODE (type) == RECORD_TYPE)
2545     {
2546       tree field;
2547
2548       /* For a struct the APCS says that we only return in a register
2549          if the type is 'integer like' and every addressable element
2550          has an offset of zero.  For practical purposes this means
2551          that the structure can have at most one non bit-field element
2552          and that this element must be the first one in the structure.  */
2553
2554       /* Find the first field, ignoring non FIELD_DECL things which will
2555          have been created by C++.  */
2556       for (field = TYPE_FIELDS (type);
2557            field && TREE_CODE (field) != FIELD_DECL;
2558            field = TREE_CHAIN (field))
2559         continue;
2560
2561       if (field == NULL)
2562         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2563
2564       /* Check that the first field is valid for returning in a register.  */
2565
2566       /* ... Floats are not allowed */
2567       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2568         return 1;
2569
2570       /* ... Aggregates that are not themselves valid for returning in
2571          a register are not allowed.  */
2572       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2573         return 1;
2574
2575       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2576          since they are not addressable.  */
2577       for (field = TREE_CHAIN (field);
2578            field;
2579            field = TREE_CHAIN (field))
2580         {
2581           if (TREE_CODE (field) != FIELD_DECL)
2582             continue;
2583
2584           if (!DECL_BIT_FIELD_TYPE (field))
2585             return 1;
2586         }
2587
2588       return 0;
2589     }
2590
2591   if (TREE_CODE (type) == UNION_TYPE)
2592     {
2593       tree field;
2594
2595       /* Unions can be returned in registers if every element is
2596          integral, or can be returned in an integer register.  */
2597       for (field = TYPE_FIELDS (type);
2598            field;
2599            field = TREE_CHAIN (field))
2600         {
2601           if (TREE_CODE (field) != FIELD_DECL)
2602             continue;
2603
2604           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2605             return 1;
2606
2607           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2608             return 1;
2609         }
2610
2611       return 0;
2612     }
2613 #endif /* not ARM_WINCE */
2614
2615   /* Return all other types in memory.  */
2616   return 1;
2617 }
2618
2619 /* Indicate whether or not words of a double are in big-endian order.  */
2620
2621 int
2622 arm_float_words_big_endian (void)
2623 {
2624   if (TARGET_MAVERICK)
2625     return 0;
2626
2627   /* For FPA, float words are always big-endian.  For VFP, floats words
2628      follow the memory system mode.  */
2629
2630   if (TARGET_FPA)
2631     {
2632       return 1;
2633     }
2634
2635   if (TARGET_VFP)
2636     return (TARGET_BIG_END ? 1 : 0);
2637
2638   return 1;
2639 }
2640
2641 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2642    for a call to a function whose data type is FNTYPE.
2643    For a library call, FNTYPE is NULL.  */
2644 void
2645 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2646                           rtx libname  ATTRIBUTE_UNUSED,
2647                           tree fndecl ATTRIBUTE_UNUSED)
2648 {
2649   /* On the ARM, the offset starts at 0.  */
2650   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2651   pcum->iwmmxt_nregs = 0;
2652   pcum->can_split = true;
2653
2654   pcum->call_cookie = CALL_NORMAL;
2655
2656   if (TARGET_LONG_CALLS)
2657     pcum->call_cookie = CALL_LONG;
2658
2659   /* Check for long call/short call attributes.  The attributes
2660      override any command line option.  */
2661   if (fntype)
2662     {
2663       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2664         pcum->call_cookie = CALL_SHORT;
2665       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2666         pcum->call_cookie = CALL_LONG;
2667     }
2668
2669   /* Varargs vectors are treated the same as long long.
2670      named_count avoids having to change the way arm handles 'named' */
2671   pcum->named_count = 0;
2672   pcum->nargs = 0;
2673
2674   if (TARGET_REALLY_IWMMXT && fntype)
2675     {
2676       tree fn_arg;
2677
2678       for (fn_arg = TYPE_ARG_TYPES (fntype);
2679            fn_arg;
2680            fn_arg = TREE_CHAIN (fn_arg))
2681         pcum->named_count += 1;
2682
2683       if (! pcum->named_count)
2684         pcum->named_count = INT_MAX;
2685     }
2686 }
2687
2688
2689 /* Return true if mode/type need doubleword alignment.  */
2690 bool
2691 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2692 {
2693   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2694           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2695 }
2696
2697
2698 /* Determine where to put an argument to a function.
2699    Value is zero to push the argument on the stack,
2700    or a hard register in which to store the argument.
2701
2702    MODE is the argument's machine mode.
2703    TYPE is the data type of the argument (as a tree).
2704     This is null for libcalls where that information may
2705     not be available.
2706    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2707     the preceding args and about the function being called.
2708    NAMED is nonzero if this argument is a named parameter
2709     (otherwise it is an extra parameter matching an ellipsis).  */
2710
2711 rtx
2712 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2713                   tree type, int named)
2714 {
2715   int nregs;
2716
2717   /* Varargs vectors are treated the same as long long.
2718      named_count avoids having to change the way arm handles 'named' */
2719   if (TARGET_IWMMXT_ABI
2720       && arm_vector_mode_supported_p (mode)
2721       && pcum->named_count > pcum->nargs + 1)
2722     {
2723       if (pcum->iwmmxt_nregs <= 9)
2724         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2725       else
2726         {
2727           pcum->can_split = false;
2728           return NULL_RTX;
2729         }
2730     }
2731
2732   /* Put doubleword aligned quantities in even register pairs.  */
2733   if (pcum->nregs & 1
2734       && ARM_DOUBLEWORD_ALIGN
2735       && arm_needs_doubleword_align (mode, type))
2736     pcum->nregs++;
2737
2738   if (mode == VOIDmode)
2739     /* Compute operand 2 of the call insn.  */
2740     return GEN_INT (pcum->call_cookie);
2741
2742   /* Only allow splitting an arg between regs and memory if all preceding
2743      args were allocated to regs.  For args passed by reference we only count
2744      the reference pointer.  */
2745   if (pcum->can_split)
2746     nregs = 1;
2747   else
2748     nregs = ARM_NUM_REGS2 (mode, type);
2749
2750   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2751     return NULL_RTX;
2752
2753   return gen_rtx_REG (mode, pcum->nregs);
2754 }
2755
2756 static int
2757 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2758                        tree type, bool named ATTRIBUTE_UNUSED)
2759 {
2760   int nregs = pcum->nregs;
2761
2762   if (arm_vector_mode_supported_p (mode))
2763     return 0;
2764
2765   if (NUM_ARG_REGS > nregs
2766       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2767       && pcum->can_split)
2768     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2769
2770   return 0;
2771 }
2772
2773 /* Variable sized types are passed by reference.  This is a GCC
2774    extension to the ARM ABI.  */
2775
2776 static bool
2777 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2778                        enum machine_mode mode ATTRIBUTE_UNUSED,
2779                        tree type, bool named ATTRIBUTE_UNUSED)
2780 {
2781   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2782 }
2783 \f
2784 /* Encode the current state of the #pragma [no_]long_calls.  */
2785 typedef enum
2786 {
2787   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2788   LONG,         /* #pragma long_calls is in effect.  */
2789   SHORT         /* #pragma no_long_calls is in effect.  */
2790 } arm_pragma_enum;
2791
2792 static arm_pragma_enum arm_pragma_long_calls = OFF;
2793
2794 void
2795 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2796 {
2797   arm_pragma_long_calls = LONG;
2798 }
2799
2800 void
2801 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2802 {
2803   arm_pragma_long_calls = SHORT;
2804 }
2805
2806 void
2807 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2808 {
2809   arm_pragma_long_calls = OFF;
2810 }
2811 \f
2812 /* Table of machine attributes.  */
2813 const struct attribute_spec arm_attribute_table[] =
2814 {
2815   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2816   /* Function calls made to this symbol must be done indirectly, because
2817      it may lie outside of the 26 bit addressing range of a normal function
2818      call.  */
2819   { "long_call",    0, 0, false, true,  true,  NULL },
2820   /* Whereas these functions are always known to reside within the 26 bit
2821      addressing range.  */
2822   { "short_call",   0, 0, false, true,  true,  NULL },
2823   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2824   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2825   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2826   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2827 #ifdef ARM_PE
2828   /* ARM/PE has three new attributes:
2829      interfacearm - ?
2830      dllexport - for exporting a function/variable that will live in a dll
2831      dllimport - for importing a function/variable from a dll
2832
2833      Microsoft allows multiple declspecs in one __declspec, separating
2834      them with spaces.  We do NOT support this.  Instead, use __declspec
2835      multiple times.
2836   */
2837   { "dllimport",    0, 0, true,  false, false, NULL },
2838   { "dllexport",    0, 0, true,  false, false, NULL },
2839   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2840 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2841   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2842   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2843   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2844 #endif
2845   { NULL,           0, 0, false, false, false, NULL }
2846 };
2847
2848 /* Handle an attribute requiring a FUNCTION_DECL;
2849    arguments as in struct attribute_spec.handler.  */
2850 static tree
2851 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2852                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2853 {
2854   if (TREE_CODE (*node) != FUNCTION_DECL)
2855     {
2856       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2857                IDENTIFIER_POINTER (name));
2858       *no_add_attrs = true;
2859     }
2860
2861   return NULL_TREE;
2862 }
2863
2864 /* Handle an "interrupt" or "isr" attribute;
2865    arguments as in struct attribute_spec.handler.  */
2866 static tree
2867 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2868                           bool *no_add_attrs)
2869 {
2870   if (DECL_P (*node))
2871     {
2872       if (TREE_CODE (*node) != FUNCTION_DECL)
2873         {
2874           warning (OPT_Wattributes, "%qs attribute only applies to functions",
2875                    IDENTIFIER_POINTER (name));
2876           *no_add_attrs = true;
2877         }
2878       /* FIXME: the argument if any is checked for type attributes;
2879          should it be checked for decl ones?  */
2880     }
2881   else
2882     {
2883       if (TREE_CODE (*node) == FUNCTION_TYPE
2884           || TREE_CODE (*node) == METHOD_TYPE)
2885         {
2886           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2887             {
2888               warning (OPT_Wattributes, "%qs attribute ignored",
2889                        IDENTIFIER_POINTER (name));
2890               *no_add_attrs = true;
2891             }
2892         }
2893       else if (TREE_CODE (*node) == POINTER_TYPE
2894                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2895                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2896                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2897         {
2898           *node = build_variant_type_copy (*node);
2899           TREE_TYPE (*node) = build_type_attribute_variant
2900             (TREE_TYPE (*node),
2901              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2902           *no_add_attrs = true;
2903         }
2904       else
2905         {
2906           /* Possibly pass this attribute on from the type to a decl.  */
2907           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2908                        | (int) ATTR_FLAG_FUNCTION_NEXT
2909                        | (int) ATTR_FLAG_ARRAY_NEXT))
2910             {
2911               *no_add_attrs = true;
2912               return tree_cons (name, args, NULL_TREE);
2913             }
2914           else
2915             {
2916               warning (OPT_Wattributes, "%qs attribute ignored",
2917                        IDENTIFIER_POINTER (name));
2918             }
2919         }
2920     }
2921
2922   return NULL_TREE;
2923 }
2924
2925 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2926 /* Handle the "notshared" attribute.  This attribute is another way of
2927    requesting hidden visibility.  ARM's compiler supports
2928    "__declspec(notshared)"; we support the same thing via an
2929    attribute.  */
2930
2931 static tree
2932 arm_handle_notshared_attribute (tree *node,
2933                                 tree name ATTRIBUTE_UNUSED,
2934                                 tree args ATTRIBUTE_UNUSED,
2935                                 int flags ATTRIBUTE_UNUSED,
2936                                 bool *no_add_attrs)
2937 {
2938   tree decl = TYPE_NAME (*node);
2939
2940   if (decl)
2941     {
2942       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2943       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2944       *no_add_attrs = false;
2945     }
2946   return NULL_TREE;
2947 }
2948 #endif
2949
2950 /* Return 0 if the attributes for two types are incompatible, 1 if they
2951    are compatible, and 2 if they are nearly compatible (which causes a
2952    warning to be generated).  */
2953 static int
2954 arm_comp_type_attributes (tree type1, tree type2)
2955 {
2956   int l1, l2, s1, s2;
2957
2958   /* Check for mismatch of non-default calling convention.  */
2959   if (TREE_CODE (type1) != FUNCTION_TYPE)
2960     return 1;
2961
2962   /* Check for mismatched call attributes.  */
2963   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2964   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2965   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2966   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2967
2968   /* Only bother to check if an attribute is defined.  */
2969   if (l1 | l2 | s1 | s2)
2970     {
2971       /* If one type has an attribute, the other must have the same attribute.  */
2972       if ((l1 != l2) || (s1 != s2))
2973         return 0;
2974
2975       /* Disallow mixed attributes.  */
2976       if ((l1 & s2) || (l2 & s1))
2977         return 0;
2978     }
2979
2980   /* Check for mismatched ISR attribute.  */
2981   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2982   if (! l1)
2983     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2984   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2985   if (! l2)
2986     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2987   if (l1 != l2)
2988     return 0;
2989
2990   return 1;
2991 }
2992
2993 /*  Encode long_call or short_call attribute by prefixing
2994     symbol name in DECL with a special character FLAG.  */
2995 void
2996 arm_encode_call_attribute (tree decl, int flag)
2997 {
2998   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2999   int          len = strlen (str);
3000   char *       newstr;
3001
3002   /* Do not allow weak functions to be treated as short call.  */
3003   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
3004     return;
3005
3006   newstr = alloca (len + 2);
3007   newstr[0] = flag;
3008   strcpy (newstr + 1, str);
3009
3010   newstr = (char *) ggc_alloc_string (newstr, len + 1);
3011   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3012 }
3013
3014 /*  Assigns default attributes to newly defined type.  This is used to
3015     set short_call/long_call attributes for function types of
3016     functions defined inside corresponding #pragma scopes.  */
3017 static void
3018 arm_set_default_type_attributes (tree type)
3019 {
3020   /* Add __attribute__ ((long_call)) to all functions, when
3021      inside #pragma long_calls or __attribute__ ((short_call)),
3022      when inside #pragma no_long_calls.  */
3023   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3024     {
3025       tree type_attr_list, attr_name;
3026       type_attr_list = TYPE_ATTRIBUTES (type);
3027
3028       if (arm_pragma_long_calls == LONG)
3029         attr_name = get_identifier ("long_call");
3030       else if (arm_pragma_long_calls == SHORT)
3031         attr_name = get_identifier ("short_call");
3032       else
3033         return;
3034
3035       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3036       TYPE_ATTRIBUTES (type) = type_attr_list;
3037     }
3038 }
3039 \f
3040 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
3041    defined within the current compilation unit.  If this cannot be
3042    determined, then 0 is returned.  */
3043 static int
3044 current_file_function_operand (rtx sym_ref)
3045 {
3046   /* This is a bit of a fib.  A function will have a short call flag
3047      applied to its name if it has the short call attribute, or it has
3048      already been defined within the current compilation unit.  */
3049   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3050     return 1;
3051
3052   /* The current function is always defined within the current compilation
3053      unit.  If it s a weak definition however, then this may not be the real
3054      definition of the function, and so we have to say no.  */
3055   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3056       && !DECL_WEAK (current_function_decl))
3057     return 1;
3058
3059   /* We cannot make the determination - default to returning 0.  */
3060   return 0;
3061 }
3062
3063 /* Return nonzero if a 32 bit "long_call" should be generated for
3064    this call.  We generate a long_call if the function:
3065
3066         a.  has an __attribute__((long call))
3067      or b.  is within the scope of a #pragma long_calls
3068      or c.  the -mlong-calls command line switch has been specified
3069          .  and either:
3070                 1. -ffunction-sections is in effect
3071              or 2. the current function has __attribute__ ((section))
3072              or 3. the target function has __attribute__ ((section))
3073
3074    However we do not generate a long call if the function:
3075
3076         d.  has an __attribute__ ((short_call))
3077      or e.  is inside the scope of a #pragma no_long_calls
3078      or f.  is defined within the current compilation unit.
3079
3080    This function will be called by C fragments contained in the machine
3081    description file.  SYM_REF and CALL_COOKIE correspond to the matched
3082    rtl operands.  CALL_SYMBOL is used to distinguish between
3083    two different callers of the function.  It is set to 1 in the
3084    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3085    and "call_value" patterns.  This is because of the difference in the
3086    SYM_REFs passed by these patterns.  */
3087 int
3088 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3089 {
3090   if (!call_symbol)
3091     {
3092       if (GET_CODE (sym_ref) != MEM)
3093         return 0;
3094
3095       sym_ref = XEXP (sym_ref, 0);
3096     }
3097
3098   if (GET_CODE (sym_ref) != SYMBOL_REF)
3099     return 0;
3100
3101   if (call_cookie & CALL_SHORT)
3102     return 0;
3103
3104   if (TARGET_LONG_CALLS)
3105     {
3106       if (flag_function_sections
3107           || DECL_SECTION_NAME (current_function_decl))
3108         /* c.3 is handled by the definition of the
3109            ARM_DECLARE_FUNCTION_SIZE macro.  */
3110         return 1;
3111     }
3112
3113   if (current_file_function_operand (sym_ref))
3114     return 0;
3115
3116   return (call_cookie & CALL_LONG)
3117     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3118     || TARGET_LONG_CALLS;
3119 }
3120
3121 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3122 static bool
3123 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3124 {
3125   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3126
3127   if (cfun->machine->sibcall_blocked)
3128     return false;
3129
3130   /* Never tailcall something for which we have no decl, or if we
3131      are in Thumb mode.  */
3132   if (decl == NULL || TARGET_THUMB)
3133     return false;
3134
3135   /* Get the calling method.  */
3136   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3137     call_type = CALL_SHORT;
3138   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3139     call_type = CALL_LONG;
3140
3141   /* Cannot tail-call to long calls, since these are out of range of
3142      a branch instruction.  However, if not compiling PIC, we know
3143      we can reach the symbol if it is in this compilation unit.  */
3144   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3145     return false;
3146
3147   /* If we are interworking and the function is not declared static
3148      then we can't tail-call it unless we know that it exists in this
3149      compilation unit (since it might be a Thumb routine).  */
3150   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3151     return false;
3152
3153   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3154   if (IS_INTERRUPT (arm_current_func_type ()))
3155     return false;
3156
3157   /* Everything else is ok.  */
3158   return true;
3159 }
3160
3161 \f
3162 /* Addressing mode support functions.  */
3163
3164 /* Return nonzero if X is a legitimate immediate operand when compiling
3165    for PIC.  */
3166 int
3167 legitimate_pic_operand_p (rtx x)
3168 {
3169   if (CONSTANT_P (x)
3170       && flag_pic
3171       && (GET_CODE (x) == SYMBOL_REF
3172           || (GET_CODE (x) == CONST
3173               && GET_CODE (XEXP (x, 0)) == PLUS
3174               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
3175     return 0;
3176
3177   return 1;
3178 }
3179
3180 rtx
3181 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3182 {
3183   if (GET_CODE (orig) == SYMBOL_REF
3184       || GET_CODE (orig) == LABEL_REF)
3185     {
3186 #ifndef AOF_ASSEMBLER
3187       rtx pic_ref, address;
3188 #endif
3189       rtx insn;
3190       int subregs = 0;
3191
3192       if (reg == 0)
3193         {
3194           gcc_assert (!no_new_pseudos);
3195           reg = gen_reg_rtx (Pmode);
3196
3197           subregs = 1;
3198         }
3199
3200 #ifdef AOF_ASSEMBLER
3201       /* The AOF assembler can generate relocations for these directly, and
3202          understands that the PIC register has to be added into the offset.  */
3203       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3204 #else
3205       if (subregs)
3206         address = gen_reg_rtx (Pmode);
3207       else
3208         address = reg;
3209
3210       if (TARGET_ARM)
3211         emit_insn (gen_pic_load_addr_arm (address, orig));
3212       else
3213         emit_insn (gen_pic_load_addr_thumb (address, orig));
3214
3215       if ((GET_CODE (orig) == LABEL_REF
3216            || (GET_CODE (orig) == SYMBOL_REF &&
3217                SYMBOL_REF_LOCAL_P (orig)))
3218           && NEED_GOT_RELOC)
3219         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3220       else
3221         {
3222           pic_ref = gen_const_mem (Pmode,
3223                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3224                                                  address));
3225         }
3226
3227       insn = emit_move_insn (reg, pic_ref);
3228 #endif
3229       current_function_uses_pic_offset_table = 1;
3230       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3231          by loop.  */
3232       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3233                                             REG_NOTES (insn));
3234       return reg;
3235     }
3236   else if (GET_CODE (orig) == CONST)
3237     {
3238       rtx base, offset;
3239
3240       if (GET_CODE (XEXP (orig, 0)) == PLUS
3241           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3242         return orig;
3243
3244       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3245           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3246         return orig;
3247
3248       if (reg == 0)
3249         {
3250           gcc_assert (!no_new_pseudos);
3251           reg = gen_reg_rtx (Pmode);
3252         }
3253
3254       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3255
3256       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3257       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3258                                        base == reg ? 0 : reg);
3259
3260       if (GET_CODE (offset) == CONST_INT)
3261         {
3262           /* The base register doesn't really matter, we only want to
3263              test the index for the appropriate mode.  */
3264           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3265             {
3266               gcc_assert (!no_new_pseudos);
3267               offset = force_reg (Pmode, offset);
3268             }
3269
3270           if (GET_CODE (offset) == CONST_INT)
3271             return plus_constant (base, INTVAL (offset));
3272         }
3273
3274       if (GET_MODE_SIZE (mode) > 4
3275           && (GET_MODE_CLASS (mode) == MODE_INT
3276               || TARGET_SOFT_FLOAT))
3277         {
3278           emit_insn (gen_addsi3 (reg, base, offset));
3279           return reg;
3280         }
3281
3282       return gen_rtx_PLUS (Pmode, base, offset);
3283     }
3284
3285   return orig;
3286 }
3287
3288
3289 /* Find a spare low register to use during the prolog of a function.  */
3290
3291 static int
3292 thumb_find_work_register (unsigned long pushed_regs_mask)
3293 {
3294   int reg;
3295
3296   /* Check the argument registers first as these are call-used.  The
3297      register allocation order means that sometimes r3 might be used
3298      but earlier argument registers might not, so check them all.  */
3299   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3300     if (!regs_ever_live[reg])
3301       return reg;
3302
3303   /* Before going on to check the call-saved registers we can try a couple
3304      more ways of deducing that r3 is available.  The first is when we are
3305      pushing anonymous arguments onto the stack and we have less than 4
3306      registers worth of fixed arguments(*).  In this case r3 will be part of
3307      the variable argument list and so we can be sure that it will be
3308      pushed right at the start of the function.  Hence it will be available
3309      for the rest of the prologue.
3310      (*): ie current_function_pretend_args_size is greater than 0.  */
3311   if (cfun->machine->uses_anonymous_args
3312       && current_function_pretend_args_size > 0)
3313     return LAST_ARG_REGNUM;
3314
3315   /* The other case is when we have fixed arguments but less than 4 registers
3316      worth.  In this case r3 might be used in the body of the function, but
3317      it is not being used to convey an argument into the function.  In theory
3318      we could just check current_function_args_size to see how many bytes are
3319      being passed in argument registers, but it seems that it is unreliable.
3320      Sometimes it will have the value 0 when in fact arguments are being
3321      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3322      check the args_info.nregs field as well.  The problem with this field is
3323      that it makes no allowances for arguments that are passed to the
3324      function but which are not used.  Hence we could miss an opportunity
3325      when a function has an unused argument in r3.  But it is better to be
3326      safe than to be sorry.  */
3327   if (! cfun->machine->uses_anonymous_args
3328       && current_function_args_size >= 0
3329       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3330       && cfun->args_info.nregs < 4)
3331     return LAST_ARG_REGNUM;
3332
3333   /* Otherwise look for a call-saved register that is going to be pushed.  */
3334   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3335     if (pushed_regs_mask & (1 << reg))
3336       return reg;
3337
3338   /* Something went wrong - thumb_compute_save_reg_mask()
3339      should have arranged for a suitable register to be pushed.  */
3340   gcc_unreachable ();
3341 }
3342
3343 static GTY(()) int pic_labelno;
3344
3345 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3346    low register.  */
3347
3348 void
3349 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3350 {
3351 #ifndef AOF_ASSEMBLER
3352   rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3353   rtx global_offset_table;
3354
3355   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3356     return;
3357
3358   gcc_assert (flag_pic);
3359
3360   /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3361      in the code stream.  */
3362
3363   labelno = GEN_INT (pic_labelno++);
3364   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3365   l1 = gen_rtx_CONST (VOIDmode, l1);
3366
3367   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3368   /* On the ARM the PC register contains 'dot + 8' at the time of the
3369      addition, on the Thumb it is 'dot + 4'.  */
3370   pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3371   if (GOT_PCREL)
3372     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3373                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3374   else
3375     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3376
3377   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3378
3379   if (TARGET_ARM)
3380     {
3381       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3382       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx,
3383                                              pic_offset_table_rtx, labelno));
3384     }
3385   else
3386     {
3387       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3388         {
3389           /* We will have pushed the pic register, so we should always be
3390              able to find a work register.  */
3391           pic_tmp = gen_rtx_REG (SImode,
3392                                  thumb_find_work_register (saved_regs));
3393           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3394           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3395         }
3396       else
3397         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3398       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx,
3399                                             pic_offset_table_rtx, labelno));
3400     }
3401
3402   /* Need to emit this whether or not we obey regdecls,
3403      since setjmp/longjmp can cause life info to screw up.  */
3404   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3405 #endif /* AOF_ASSEMBLER */
3406 }
3407
3408
3409 /* Return nonzero if X is valid as an ARM state addressing register.  */
3410 static int
3411 arm_address_register_rtx_p (rtx x, int strict_p)
3412 {
3413   int regno;
3414
3415   if (GET_CODE (x) != REG)
3416     return 0;
3417
3418   regno = REGNO (x);
3419
3420   if (strict_p)
3421     return ARM_REGNO_OK_FOR_BASE_P (regno);
3422
3423   return (regno <= LAST_ARM_REGNUM
3424           || regno >= FIRST_PSEUDO_REGISTER
3425           || regno == FRAME_POINTER_REGNUM
3426           || regno == ARG_POINTER_REGNUM);
3427 }
3428
3429 /* Return TRUE if this rtx is the difference of a symbol and a label,
3430    and will reduce to a PC-relative relocation in the object file.
3431    Expressions like this can be left alone when generating PIC, rather
3432    than forced through the GOT.  */
3433 static int
3434 pcrel_constant_p (rtx x)
3435 {
3436   if (GET_CODE (x) == MINUS)
3437     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3438
3439   return FALSE;
3440 }
3441
3442 /* Return nonzero if X is a valid ARM state address operand.  */
3443 int
3444 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3445                           int strict_p)
3446 {
3447   bool use_ldrd;
3448   enum rtx_code code = GET_CODE (x);
3449
3450   if (arm_address_register_rtx_p (x, strict_p))
3451     return 1;
3452
3453   use_ldrd = (TARGET_LDRD
3454               && (mode == DImode
3455                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3456
3457   if (code == POST_INC || code == PRE_DEC
3458       || ((code == PRE_INC || code == POST_DEC)
3459           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3460     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3461
3462   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3463            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3464            && GET_CODE (XEXP (x, 1)) == PLUS
3465            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3466     {
3467       rtx addend = XEXP (XEXP (x, 1), 1);
3468
3469       /* Don't allow ldrd post increment by register because it's hard
3470          to fixup invalid register choices.  */
3471       if (use_ldrd
3472           && GET_CODE (x) == POST_MODIFY
3473           && GET_CODE (addend) == REG)
3474         return 0;
3475
3476       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3477               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3478     }
3479
3480   /* After reload constants split into minipools will have addresses
3481      from a LABEL_REF.  */
3482   else if (reload_completed
3483            && (code == LABEL_REF
3484                || (code == CONST
3485                    && GET_CODE (XEXP (x, 0)) == PLUS
3486                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3487                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3488     return 1;
3489
3490   else if (mode == TImode)
3491     return 0;
3492
3493   else if (code == PLUS)
3494     {
3495       rtx xop0 = XEXP (x, 0);
3496       rtx xop1 = XEXP (x, 1);
3497
3498       return ((arm_address_register_rtx_p (xop0, strict_p)
3499                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3500               || (arm_address_register_rtx_p (xop1, strict_p)
3501                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3502     }
3503
3504 #if 0
3505   /* Reload currently can't handle MINUS, so disable this for now */
3506   else if (GET_CODE (x) == MINUS)
3507     {
3508       rtx xop0 = XEXP (x, 0);
3509       rtx xop1 = XEXP (x, 1);
3510
3511       return (arm_address_register_rtx_p (xop0, strict_p)
3512               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3513     }
3514 #endif
3515
3516   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3517            && code == SYMBOL_REF
3518            && CONSTANT_POOL_ADDRESS_P (x)
3519            && ! (flag_pic
3520                  && symbol_mentioned_p (get_pool_constant (x))
3521                  && ! pcrel_constant_p (get_pool_constant (x))))
3522     return 1;
3523
3524   return 0;
3525 }
3526
3527 /* Return nonzero if INDEX is valid for an address index operand in
3528    ARM state.  */
3529 static int
3530 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3531                         int strict_p)
3532 {
3533   HOST_WIDE_INT range;
3534   enum rtx_code code = GET_CODE (index);
3535
3536   /* Standard coprocessor addressing modes.  */
3537   if (TARGET_HARD_FLOAT
3538       && (TARGET_FPA || TARGET_MAVERICK)
3539       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3540           || (TARGET_MAVERICK && mode == DImode)))
3541     return (code == CONST_INT && INTVAL (index) < 1024
3542             && INTVAL (index) > -1024
3543             && (INTVAL (index) & 3) == 0);
3544
3545   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3546     return (code == CONST_INT
3547             && INTVAL (index) < 1024
3548             && INTVAL (index) > -1024
3549             && (INTVAL (index) & 3) == 0);
3550
3551   if (arm_address_register_rtx_p (index, strict_p)
3552       && (GET_MODE_SIZE (mode) <= 4))
3553     return 1;
3554
3555   if (mode == DImode || mode == DFmode)
3556     {
3557       if (code == CONST_INT)
3558         {
3559           HOST_WIDE_INT val = INTVAL (index);
3560
3561           if (TARGET_LDRD)
3562             return val > -256 && val < 256;
3563           else
3564             return val > -4096 && val < 4092;
3565         }
3566
3567       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3568     }
3569
3570   if (GET_MODE_SIZE (mode) <= 4
3571       && ! (arm_arch4
3572             && (mode == HImode
3573                 || (mode == QImode && outer == SIGN_EXTEND))))
3574     {
3575       if (code == MULT)
3576         {
3577           rtx xiop0 = XEXP (index, 0);
3578           rtx xiop1 = XEXP (index, 1);
3579
3580           return ((arm_address_register_rtx_p (xiop0, strict_p)
3581                    && power_of_two_operand (xiop1, SImode))
3582                   || (arm_address_register_rtx_p (xiop1, strict_p)
3583                       && power_of_two_operand (xiop0, SImode)));
3584         }
3585       else if (code == LSHIFTRT || code == ASHIFTRT
3586                || code == ASHIFT || code == ROTATERT)
3587         {
3588           rtx op = XEXP (index, 1);
3589
3590           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3591                   && GET_CODE (op) == CONST_INT
3592                   && INTVAL (op) > 0
3593                   && INTVAL (op) <= 31);
3594         }
3595     }
3596
3597   /* For ARM v4 we may be doing a sign-extend operation during the
3598      load.  */
3599   if (arm_arch4)
3600     {
3601       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3602         range = 256;
3603       else
3604         range = 4096;
3605     }
3606   else
3607     range = (mode == HImode) ? 4095 : 4096;
3608
3609   return (code == CONST_INT
3610           && INTVAL (index) < range
3611           && INTVAL (index) > -range);
3612 }
3613
3614 /* Return nonzero if X is valid as a Thumb state base register.  */
3615 static int
3616 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3617 {
3618   int regno;
3619
3620   if (GET_CODE (x) != REG)
3621     return 0;
3622
3623   regno = REGNO (x);
3624
3625   if (strict_p)
3626     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3627
3628   return (regno <= LAST_LO_REGNUM
3629           || regno > LAST_VIRTUAL_REGISTER
3630           || regno == FRAME_POINTER_REGNUM
3631           || (GET_MODE_SIZE (mode) >= 4
3632               && (regno == STACK_POINTER_REGNUM
3633                   || regno >= FIRST_PSEUDO_REGISTER
3634                   || x == hard_frame_pointer_rtx
3635                   || x == arg_pointer_rtx)));
3636 }
3637
3638 /* Return nonzero if x is a legitimate index register.  This is the case
3639    for any base register that can access a QImode object.  */
3640 inline static int
3641 thumb_index_register_rtx_p (rtx x, int strict_p)
3642 {
3643   return thumb_base_register_rtx_p (x, QImode, strict_p);
3644 }
3645
3646 /* Return nonzero if x is a legitimate Thumb-state address.
3647
3648    The AP may be eliminated to either the SP or the FP, so we use the
3649    least common denominator, e.g. SImode, and offsets from 0 to 64.
3650
3651    ??? Verify whether the above is the right approach.
3652
3653    ??? Also, the FP may be eliminated to the SP, so perhaps that
3654    needs special handling also.
3655
3656    ??? Look at how the mips16 port solves this problem.  It probably uses
3657    better ways to solve some of these problems.
3658
3659    Although it is not incorrect, we don't accept QImode and HImode
3660    addresses based on the frame pointer or arg pointer until the
3661    reload pass starts.  This is so that eliminating such addresses
3662    into stack based ones won't produce impossible code.  */
3663 int
3664 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3665 {
3666   /* ??? Not clear if this is right.  Experiment.  */
3667   if (GET_MODE_SIZE (mode) < 4
3668       && !(reload_in_progress || reload_completed)
3669       && (reg_mentioned_p (frame_pointer_rtx, x)
3670           || reg_mentioned_p (arg_pointer_rtx, x)
3671           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3672           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3673           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3674           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3675     return 0;
3676
3677   /* Accept any base register.  SP only in SImode or larger.  */
3678   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3679     return 1;
3680
3681   /* This is PC relative data before arm_reorg runs.  */
3682   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3683            && GET_CODE (x) == SYMBOL_REF
3684            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3685     return 1;
3686
3687   /* This is PC relative data after arm_reorg runs.  */
3688   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3689            && (GET_CODE (x) == LABEL_REF
3690                || (GET_CODE (x) == CONST
3691                    && GET_CODE (XEXP (x, 0)) == PLUS
3692                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3693                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3694     return 1;
3695
3696   /* Post-inc indexing only supported for SImode and larger.  */
3697   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3698            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3699     return 1;
3700
3701   else if (GET_CODE (x) == PLUS)
3702     {
3703       /* REG+REG address can be any two index registers.  */
3704       /* We disallow FRAME+REG addressing since we know that FRAME
3705          will be replaced with STACK, and SP relative addressing only
3706          permits SP+OFFSET.  */
3707       if (GET_MODE_SIZE (mode) <= 4
3708           && XEXP (x, 0) != frame_pointer_rtx
3709           && XEXP (x, 1) != frame_pointer_rtx
3710           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3711           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3712         return 1;
3713
3714       /* REG+const has 5-7 bit offset for non-SP registers.  */
3715       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3716                 || XEXP (x, 0) == arg_pointer_rtx)
3717                && GET_CODE (XEXP (x, 1)) == CONST_INT
3718                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3719         return 1;
3720
3721       /* REG+const has 10 bit offset for SP, but only SImode and
3722          larger is supported.  */
3723       /* ??? Should probably check for DI/DFmode overflow here
3724          just like GO_IF_LEGITIMATE_OFFSET does.  */
3725       else if (GET_CODE (XEXP (x, 0)) == REG
3726                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3727                && GET_MODE_SIZE (mode) >= 4
3728                && GET_CODE (XEXP (x, 1)) == CONST_INT
3729                && INTVAL (XEXP (x, 1)) >= 0
3730                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3731                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3732         return 1;
3733
3734       else if (GET_CODE (XEXP (x, 0)) == REG
3735                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3736                && GET_MODE_SIZE (mode) >= 4
3737                && GET_CODE (XEXP (x, 1)) == CONST_INT
3738                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3739         return 1;
3740     }
3741
3742   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3743            && GET_MODE_SIZE (mode) == 4
3744            && GET_CODE (x) == SYMBOL_REF
3745            && CONSTANT_POOL_ADDRESS_P (x)
3746            && ! (flag_pic
3747                  && symbol_mentioned_p (get_pool_constant (x))
3748                  && ! pcrel_constant_p (get_pool_constant (x))))
3749     return 1;
3750
3751   return 0;
3752 }
3753
3754 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3755    instruction of mode MODE.  */
3756 int
3757 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3758 {
3759   switch (GET_MODE_SIZE (mode))
3760     {
3761     case 1:
3762       return val >= 0 && val < 32;
3763
3764     case 2:
3765       return val >= 0 && val < 64 && (val & 1) == 0;
3766
3767     default:
3768       return (val >= 0
3769               && (val + GET_MODE_SIZE (mode)) <= 128
3770               && (val & 3) == 0);
3771     }
3772 }
3773
3774 /* Build the SYMBOL_REF for __tls_get_addr.  */
3775
3776 static GTY(()) rtx tls_get_addr_libfunc;
3777
3778 static rtx
3779 get_tls_get_addr (void)
3780 {
3781   if (!tls_get_addr_libfunc)
3782     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
3783   return tls_get_addr_libfunc;
3784 }
3785
3786 static rtx
3787 arm_load_tp (rtx target)
3788 {
3789   if (!target)
3790     target = gen_reg_rtx (SImode);
3791
3792   if (TARGET_HARD_TP)
3793     {
3794       /* Can return in any reg.  */
3795       emit_insn (gen_load_tp_hard (target));
3796     }
3797   else
3798     {
3799       /* Always returned in r0.  Immediately copy the result into a pseudo,
3800          otherwise other uses of r0 (e.g. setting up function arguments) may
3801          clobber the value.  */
3802
3803       rtx tmp;
3804
3805       emit_insn (gen_load_tp_soft ());
3806
3807       tmp = gen_rtx_REG (SImode, 0);
3808       emit_move_insn (target, tmp);
3809     }
3810   return target;
3811 }
3812
3813 static rtx
3814 load_tls_operand (rtx x, rtx reg)
3815 {
3816   rtx tmp;
3817
3818   if (reg == NULL_RTX)
3819     reg = gen_reg_rtx (SImode);
3820
3821   tmp = gen_rtx_CONST (SImode, x);
3822
3823   emit_move_insn (reg, tmp);
3824
3825   return reg;
3826 }
3827
3828 static rtx
3829 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
3830 {
3831   rtx insns, label, labelno, sum;
3832
3833   start_sequence ();
3834
3835   labelno = GEN_INT (pic_labelno++);
3836   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3837   label = gen_rtx_CONST (VOIDmode, label);
3838
3839   sum = gen_rtx_UNSPEC (Pmode,
3840                         gen_rtvec (4, x, GEN_INT (reloc), label,
3841                                    GEN_INT (TARGET_ARM ? 8 : 4)),
3842                         UNSPEC_TLS);
3843   reg = load_tls_operand (sum, reg);
3844
3845   if (TARGET_ARM)
3846     emit_insn (gen_pic_add_dot_plus_eight (reg, reg, labelno));
3847   else
3848     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3849
3850   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
3851                                      Pmode, 1, reg, Pmode);
3852
3853   insns = get_insns ();
3854   end_sequence ();
3855
3856   return insns;
3857 }
3858
3859 rtx
3860 legitimize_tls_address (rtx x, rtx reg)
3861 {
3862   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
3863   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
3864
3865   switch (model)
3866     {
3867     case TLS_MODEL_GLOBAL_DYNAMIC:
3868       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
3869       dest = gen_reg_rtx (Pmode);
3870       emit_libcall_block (insns, dest, ret, x);
3871       return dest;
3872
3873     case TLS_MODEL_LOCAL_DYNAMIC:
3874       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
3875
3876       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3877          share the LDM result with other LD model accesses.  */
3878       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
3879                             UNSPEC_TLS);
3880       dest = gen_reg_rtx (Pmode);
3881       emit_libcall_block (insns, dest, ret, eqv);
3882
3883       /* Load the addend.  */
3884       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
3885                                UNSPEC_TLS);
3886       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
3887       return gen_rtx_PLUS (Pmode, dest, addend);
3888
3889     case TLS_MODEL_INITIAL_EXEC:
3890       labelno = GEN_INT (pic_labelno++);
3891       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3892       label = gen_rtx_CONST (VOIDmode, label);
3893       sum = gen_rtx_UNSPEC (Pmode,
3894                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
3895                                        GEN_INT (TARGET_ARM ? 8 : 4)),
3896                             UNSPEC_TLS);
3897       reg = load_tls_operand (sum, reg);
3898
3899       if (TARGET_ARM)
3900         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
3901       else
3902         {
3903           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3904           emit_move_insn (reg, gen_const_mem (SImode, reg));
3905         }
3906
3907       tp = arm_load_tp (NULL_RTX);
3908
3909       return gen_rtx_PLUS (Pmode, tp, reg);
3910
3911     case TLS_MODEL_LOCAL_EXEC:
3912       tp = arm_load_tp (NULL_RTX);
3913
3914       reg = gen_rtx_UNSPEC (Pmode,
3915                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
3916                             UNSPEC_TLS);
3917       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
3918
3919       return gen_rtx_PLUS (Pmode, tp, reg);
3920
3921     default:
3922       abort ();
3923     }
3924 }
3925
3926 /* Try machine-dependent ways of modifying an illegitimate address
3927    to be legitimate.  If we find one, return the new, valid address.  */
3928 rtx
3929 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3930 {
3931   if (arm_tls_symbol_p (x))
3932     return legitimize_tls_address (x, NULL_RTX);
3933
3934   if (GET_CODE (x) == PLUS)
3935     {
3936       rtx xop0 = XEXP (x, 0);
3937       rtx xop1 = XEXP (x, 1);
3938
3939       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3940         xop0 = force_reg (SImode, xop0);
3941
3942       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3943         xop1 = force_reg (SImode, xop1);
3944
3945       if (ARM_BASE_REGISTER_RTX_P (xop0)
3946           && GET_CODE (xop1) == CONST_INT)
3947         {
3948           HOST_WIDE_INT n, low_n;
3949           rtx base_reg, val;
3950           n = INTVAL (xop1);
3951
3952           /* VFP addressing modes actually allow greater offsets, but for
3953              now we just stick with the lowest common denominator.  */
3954           if (mode == DImode
3955               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3956             {
3957               low_n = n & 0x0f;
3958               n &= ~0x0f;
3959               if (low_n > 4)
3960                 {
3961                   n += 16;
3962                   low_n -= 16;
3963                 }
3964             }
3965           else
3966             {
3967               low_n = ((mode) == TImode ? 0
3968                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3969               n -= low_n;
3970             }
3971
3972           base_reg = gen_reg_rtx (SImode);
3973           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3974                                              GEN_INT (n)), NULL_RTX);
3975           emit_move_insn (base_reg, val);
3976           x = (low_n == 0 ? base_reg
3977                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3978         }
3979       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3980         x = gen_rtx_PLUS (SImode, xop0, xop1);
3981     }
3982
3983   /* XXX We don't allow MINUS any more -- see comment in
3984      arm_legitimate_address_p ().  */
3985   else if (GET_CODE (x) == MINUS)
3986     {
3987       rtx xop0 = XEXP (x, 0);
3988       rtx xop1 = XEXP (x, 1);
3989
3990       if (CONSTANT_P (xop0))
3991         xop0 = force_reg (SImode, xop0);
3992
3993       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3994         xop1 = force_reg (SImode, xop1);
3995
3996       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3997         x = gen_rtx_MINUS (SImode, xop0, xop1);
3998     }
3999
4000   /* Make sure to take full advantage of the pre-indexed addressing mode
4001      with absolute addresses which often allows for the base register to
4002      be factorized for multiple adjacent memory references, and it might
4003      even allows for the mini pool to be avoided entirely. */
4004   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4005     {
4006       unsigned int bits;
4007       HOST_WIDE_INT mask, base, index;
4008       rtx base_reg;
4009
4010       /* ldr and ldrb can use a 12 bit index, ldrsb and the rest can only
4011          use a 8 bit index. So let's use a 12 bit index for SImode only and
4012          hope that arm_gen_constant will enable ldrb to use more bits. */
4013       bits = (mode == SImode) ? 12 : 8;
4014       mask = (1 << bits) - 1;
4015       base = INTVAL (x) & ~mask;
4016       index = INTVAL (x) & mask;
4017       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4018         {
4019           /* It'll most probably be more efficient to generate the base
4020              with more bits set and use a negative index instead. */
4021           base |= mask;
4022           index -= mask;
4023         }
4024       base_reg = force_reg (SImode, GEN_INT (base));
4025       x = gen_rtx_PLUS (SImode, base_reg, GEN_INT (index));
4026     }
4027
4028   if (flag_pic)
4029     {
4030       /* We need to find and carefully transform any SYMBOL and LABEL
4031          references; so go back to the original address expression.  */
4032       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4033
4034       if (new_x != orig_x)
4035         x = new_x;
4036     }
4037
4038   return x;
4039 }
4040
4041
4042 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4043    to be legitimate.  If we find one, return the new, valid address.  */
4044 rtx
4045 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4046 {
4047   if (arm_tls_symbol_p (x))
4048     return legitimize_tls_address (x, NULL_RTX);
4049
4050   if (GET_CODE (x) == PLUS
4051       && GET_CODE (XEXP (x, 1)) == CONST_INT
4052       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4053           || INTVAL (XEXP (x, 1)) < 0))
4054     {
4055       rtx xop0 = XEXP (x, 0);
4056       rtx xop1 = XEXP (x, 1);
4057       HOST_WIDE_INT offset = INTVAL (xop1);
4058
4059       /* Try and fold the offset into a biasing of the base register and
4060          then offsetting that.  Don't do this when optimizing for space
4061          since it can cause too many CSEs.  */
4062       if (optimize_size && offset >= 0
4063           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4064         {
4065           HOST_WIDE_INT delta;
4066
4067           if (offset >= 256)
4068             delta = offset - (256 - GET_MODE_SIZE (mode));
4069           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4070             delta = 31 * GET_MODE_SIZE (mode);
4071           else
4072             delta = offset & (~31 * GET_MODE_SIZE (mode));
4073
4074           xop0 = force_operand (plus_constant (xop0, offset - delta),
4075                                 NULL_RTX);
4076           x = plus_constant (xop0, delta);
4077         }
4078       else if (offset < 0 && offset > -256)
4079         /* Small negative offsets are best done with a subtract before the
4080            dereference, forcing these into a register normally takes two
4081            instructions.  */
4082         x = force_operand (x, NULL_RTX);
4083       else
4084         {
4085           /* For the remaining cases, force the constant into a register.  */
4086           xop1 = force_reg (SImode, xop1);
4087           x = gen_rtx_PLUS (SImode, xop0, xop1);
4088         }
4089     }
4090   else if (GET_CODE (x) == PLUS
4091            && s_register_operand (XEXP (x, 1), SImode)
4092            && !s_register_operand (XEXP (x, 0), SImode))
4093     {
4094       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4095
4096       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4097     }
4098
4099   if (flag_pic)
4100     {
4101       /* We need to find and carefully transform any SYMBOL and LABEL
4102          references; so go back to the original address expression.  */
4103       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4104
4105       if (new_x != orig_x)
4106         x = new_x;
4107     }
4108
4109   return x;
4110 }
4111
4112 rtx
4113 thumb_legitimize_reload_address (rtx *x_p,
4114                                  enum machine_mode mode,
4115                                  int opnum, int type,
4116                                  int ind_levels ATTRIBUTE_UNUSED)
4117 {
4118   rtx x = *x_p;
4119
4120   if (GET_CODE (x) == PLUS
4121       && GET_MODE_SIZE (mode) < 4
4122       && REG_P (XEXP (x, 0))
4123       && XEXP (x, 0) == stack_pointer_rtx
4124       && GET_CODE (XEXP (x, 1)) == CONST_INT
4125       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4126     {
4127       rtx orig_x = x;
4128
4129       x = copy_rtx (x);
4130       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4131                    Pmode, VOIDmode, 0, 0, opnum, type);
4132       return x;
4133     }
4134
4135   /* If both registers are hi-regs, then it's better to reload the
4136      entire expression rather than each register individually.  That
4137      only requires one reload register rather than two.  */
4138   if (GET_CODE (x) == PLUS
4139       && REG_P (XEXP (x, 0))
4140       && REG_P (XEXP (x, 1))
4141       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4142       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4143     {
4144       rtx orig_x = x;
4145
4146       x = copy_rtx (x);
4147       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4148                    Pmode, VOIDmode, 0, 0, opnum, type);
4149       return x;
4150     }
4151
4152   return NULL;
4153 }
4154
4155 /* Test for various thread-local symbols.  */
4156
4157 /* Return TRUE if X is a thread-local symbol.  */
4158
4159 static bool
4160 arm_tls_symbol_p (rtx x)
4161 {
4162   if (! TARGET_HAVE_TLS)
4163     return false;
4164
4165   if (GET_CODE (x) != SYMBOL_REF)
4166     return false;
4167
4168   return SYMBOL_REF_TLS_MODEL (x) != 0;
4169 }
4170
4171 /* Helper for arm_tls_referenced_p.  */
4172
4173 static int
4174 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4175 {
4176   if (GET_CODE (*x) == SYMBOL_REF)
4177     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4178
4179   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4180      TLS offsets, not real symbol references.  */
4181   if (GET_CODE (*x) == UNSPEC
4182       && XINT (*x, 1) == UNSPEC_TLS)
4183     return -1;
4184
4185   return 0;
4186 }
4187
4188 /* Return TRUE if X contains any TLS symbol references.  */
4189
4190 bool
4191 arm_tls_referenced_p (rtx x)
4192 {
4193   if (! TARGET_HAVE_TLS)
4194     return false;
4195
4196   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4197 }
4198 \f
4199 #define REG_OR_SUBREG_REG(X)                                            \
4200   (GET_CODE (X) == REG                                                  \
4201    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4202
4203 #define REG_OR_SUBREG_RTX(X)                    \
4204    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4205
4206 #ifndef COSTS_N_INSNS
4207 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4208 #endif
4209 static inline int
4210 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4211 {
4212   enum machine_mode mode = GET_MODE (x);
4213
4214   switch (code)
4215     {
4216     case ASHIFT:
4217     case ASHIFTRT:
4218     case LSHIFTRT:
4219     case ROTATERT:
4220     case PLUS:
4221     case MINUS:
4222     case COMPARE:
4223     case NEG:
4224     case NOT:
4225       return COSTS_N_INSNS (1);
4226
4227     case MULT:
4228       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4229         {
4230           int cycles = 0;
4231           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4232
4233           while (i)
4234             {
4235               i >>= 2;
4236               cycles++;
4237             }
4238           return COSTS_N_INSNS (2) + cycles;
4239         }
4240       return COSTS_N_INSNS (1) + 16;
4241
4242     case SET:
4243       return (COSTS_N_INSNS (1)
4244               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4245                      + GET_CODE (SET_DEST (x)) == MEM));
4246
4247     case CONST_INT:
4248       if (outer == SET)
4249         {
4250           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4251             return 0;
4252           if (thumb_shiftable_const (INTVAL (x)))
4253             return COSTS_N_INSNS (2);
4254           return COSTS_N_INSNS (3);
4255         }
4256       else if ((outer == PLUS || outer == COMPARE)
4257                && INTVAL (x) < 256 && INTVAL (x) > -256)
4258         return 0;
4259       else if (outer == AND
4260                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4261         return COSTS_N_INSNS (1);
4262       else if (outer == ASHIFT || outer == ASHIFTRT
4263                || outer == LSHIFTRT)
4264         return 0;
4265       return COSTS_N_INSNS (2);
4266
4267     case CONST:
4268     case CONST_DOUBLE:
4269     case LABEL_REF:
4270     case SYMBOL_REF:
4271       return COSTS_N_INSNS (3);
4272
4273     case UDIV:
4274     case UMOD:
4275     case DIV:
4276     case MOD:
4277       return 100;
4278
4279     case TRUNCATE:
4280       return 99;
4281
4282     case AND:
4283     case XOR:
4284     case IOR:
4285       /* XXX guess.  */
4286       return 8;
4287
4288     case MEM:
4289       /* XXX another guess.  */
4290       /* Memory costs quite a lot for the first word, but subsequent words
4291          load at the equivalent of a single insn each.  */
4292       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4293               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4294                  ? 4 : 0));
4295
4296     case IF_THEN_ELSE:
4297       /* XXX a guess.  */
4298       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4299         return 14;
4300       return 2;
4301
4302     case ZERO_EXTEND:
4303       /* XXX still guessing.  */
4304       switch (GET_MODE (XEXP (x, 0)))
4305         {
4306         case QImode:
4307           return (1 + (mode == DImode ? 4 : 0)
4308                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4309
4310         case HImode:
4311           return (4 + (mode == DImode ? 4 : 0)
4312                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4313
4314         case SImode:
4315           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4316
4317         default:
4318           return 99;
4319         }
4320
4321     default:
4322       return 99;
4323     }
4324 }
4325
4326
4327 /* Worker routine for arm_rtx_costs.  */
4328 static inline int
4329 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4330 {
4331   enum machine_mode mode = GET_MODE (x);
4332   enum rtx_code subcode;
4333   int extra_cost;
4334
4335   switch (code)
4336     {
4337     case MEM:
4338       /* Memory costs quite a lot for the first word, but subsequent words
4339          load at the equivalent of a single insn each.  */
4340       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4341               + (GET_CODE (x) == SYMBOL_REF
4342                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4343
4344     case DIV:
4345     case MOD:
4346     case UDIV:
4347     case UMOD:
4348       return optimize_size ? COSTS_N_INSNS (2) : 100;
4349
4350     case ROTATE:
4351       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4352         return 4;
4353       /* Fall through */
4354     case ROTATERT:
4355       if (mode != SImode)
4356         return 8;
4357       /* Fall through */
4358     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4359       if (mode == DImode)
4360         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4361                 + ((GET_CODE (XEXP (x, 0)) == REG
4362                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4363                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4364                    ? 0 : 8));
4365       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4366                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4367                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4368                    ? 0 : 4)
4369               + ((GET_CODE (XEXP (x, 1)) == REG
4370                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4371                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4372                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4373                  ? 0 : 4));
4374
4375     case MINUS:
4376       if (mode == DImode)
4377         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4378                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4379                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4380                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4381                    ? 0 : 8));
4382
4383       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4384         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4385                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4386                           && arm_const_double_rtx (XEXP (x, 1))))
4387                      ? 0 : 8)
4388                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4389                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4390                         && arm_const_double_rtx (XEXP (x, 0))))
4391                    ? 0 : 8));
4392
4393       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4394             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4395             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4396           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4397                || subcode == ASHIFTRT || subcode == LSHIFTRT
4398                || subcode == ROTATE || subcode == ROTATERT
4399                || (subcode == MULT
4400                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4401                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4402                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4403               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4404               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4405                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4406               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4407         return 1;
4408       /* Fall through */
4409
4410     case PLUS:
4411       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4412         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4413                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4414                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4415                         && arm_const_double_rtx (XEXP (x, 1))))
4416                    ? 0 : 8));
4417
4418       /* Fall through */
4419     case AND: case XOR: case IOR:
4420       extra_cost = 0;
4421
4422       /* Normally the frame registers will be spilt into reg+const during
4423          reload, so it is a bad idea to combine them with other instructions,
4424          since then they might not be moved outside of loops.  As a compromise
4425          we allow integration with ops that have a constant as their second
4426          operand.  */
4427       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4428            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4429            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4430           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4431               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4432         extra_cost = 4;
4433
4434       if (mode == DImode)
4435         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4436                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4437                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4438                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4439                    ? 0 : 8));
4440
4441       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4442         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4443                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4444                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4445                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4446                    ? 0 : 4));
4447
4448       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4449         return (1 + extra_cost
4450                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4451                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4452                      || subcode == ROTATE || subcode == ROTATERT
4453                      || (subcode == MULT
4454                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4455                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4456                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4457                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4458                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4459                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4460                    ? 0 : 4));
4461
4462       return 8;
4463
4464     case MULT:
4465       /* This should have been handled by the CPU specific routines.  */
4466       gcc_unreachable ();
4467
4468     case TRUNCATE:
4469       if (arm_arch3m && mode == SImode
4470           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4471           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4472           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4473               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4474           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4475               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4476         return 8;
4477       return 99;
4478
4479     case NEG:
4480       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4481         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4482       /* Fall through */
4483     case NOT:
4484       if (mode == DImode)
4485         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4486
4487       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4488
4489     case IF_THEN_ELSE:
4490       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4491         return 14;
4492       return 2;
4493
4494     case COMPARE:
4495       return 1;
4496
4497     case ABS:
4498       return 4 + (mode == DImode ? 4 : 0);
4499
4500     case SIGN_EXTEND:
4501       if (GET_MODE (XEXP (x, 0)) == QImode)
4502         return (4 + (mode == DImode ? 4 : 0)
4503                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4504       /* Fall through */
4505     case ZERO_EXTEND:
4506       switch (GET_MODE (XEXP (x, 0)))
4507         {
4508         case QImode:
4509           return (1 + (mode == DImode ? 4 : 0)
4510                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4511
4512         case HImode:
4513           return (4 + (mode == DImode ? 4 : 0)
4514                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4515
4516         case SImode:
4517           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4518
4519         case V8QImode:
4520         case V4HImode:
4521         case V2SImode:
4522         case V4QImode:
4523         case V2HImode:
4524             return 1;
4525
4526         default:
4527           gcc_unreachable ();
4528         }
4529       gcc_unreachable ();
4530
4531     case CONST_INT:
4532       if (const_ok_for_arm (INTVAL (x)))
4533         return outer == SET ? 2 : -1;
4534       else if (outer == AND
4535                && const_ok_for_arm (~INTVAL (x)))
4536         return -1;
4537       else if ((outer == COMPARE
4538                 || outer == PLUS || outer == MINUS)
4539                && const_ok_for_arm (-INTVAL (x)))
4540         return -1;
4541       else
4542         return 5;
4543
4544     case CONST:
4545     case LABEL_REF:
4546     case SYMBOL_REF:
4547       return 6;
4548
4549     case CONST_DOUBLE:
4550       if (arm_const_double_rtx (x))
4551         return outer == SET ? 2 : -1;
4552       else if ((outer == COMPARE || outer == PLUS)
4553                && neg_const_double_rtx_ok_for_fpa (x))
4554         return -1;
4555       return 7;
4556
4557     default:
4558       return 99;
4559     }
4560 }
4561
4562 /* RTX costs when optimizing for size.  */
4563 static bool
4564 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4565 {
4566   enum machine_mode mode = GET_MODE (x);
4567
4568   if (TARGET_THUMB)
4569     {
4570       /* XXX TBD.  For now, use the standard costs.  */
4571       *total = thumb_rtx_costs (x, code, outer_code);
4572       return true;
4573     }
4574
4575   switch (code)
4576     {
4577     case MEM:
4578       /* A memory access costs 1 insn if the mode is small, or the address is
4579          a single register, otherwise it costs one insn per word.  */
4580       if (REG_P (XEXP (x, 0)))
4581         *total = COSTS_N_INSNS (1);
4582       else
4583         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4584       return true;
4585
4586     case DIV:
4587     case MOD:
4588     case UDIV:
4589     case UMOD:
4590       /* Needs a libcall, so it costs about this.  */
4591       *total = COSTS_N_INSNS (2);
4592       return false;
4593
4594     case ROTATE:
4595       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4596         {
4597           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4598           return true;
4599         }
4600       /* Fall through */
4601     case ROTATERT:
4602     case ASHIFT:
4603     case LSHIFTRT:
4604     case ASHIFTRT:
4605       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4606         {
4607           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4608           return true;
4609         }
4610       else if (mode == SImode)
4611         {
4612           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4613           /* Slightly disparage register shifts, but not by much.  */
4614           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4615             *total += 1 + rtx_cost (XEXP (x, 1), code);
4616           return true;
4617         }
4618
4619       /* Needs a libcall.  */
4620       *total = COSTS_N_INSNS (2);
4621       return false;
4622
4623     case MINUS:
4624       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4625         {
4626           *total = COSTS_N_INSNS (1);
4627           return false;
4628         }
4629
4630       if (mode == SImode)
4631         {
4632           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4633           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4634
4635           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4636               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4637               || subcode1 == ROTATE || subcode1 == ROTATERT
4638               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4639               || subcode1 == ASHIFTRT)
4640             {
4641               /* It's just the cost of the two operands.  */
4642               *total = 0;
4643               return false;
4644             }
4645
4646           *total = COSTS_N_INSNS (1);
4647           return false;
4648         }
4649
4650       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4651       return false;
4652
4653     case PLUS:
4654       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4655         {
4656           *total = COSTS_N_INSNS (1);
4657           return false;
4658         }
4659
4660       /* Fall through */
4661     case AND: case XOR: case IOR:
4662       if (mode == SImode)
4663         {
4664           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4665
4666           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4667               || subcode == LSHIFTRT || subcode == ASHIFTRT
4668               || (code == AND && subcode == NOT))
4669             {
4670               /* It's just the cost of the two operands.  */
4671               *total = 0;
4672               return false;
4673             }
4674         }
4675
4676       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4677       return false;
4678
4679     case MULT:
4680       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4681       return false;
4682
4683     case NEG:
4684       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4685         *total = COSTS_N_INSNS (1);
4686       /* Fall through */
4687     case NOT:
4688       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4689
4690       return false;
4691
4692     case IF_THEN_ELSE:
4693       *total = 0;
4694       return false;
4695
4696     case COMPARE:
4697       if (cc_register (XEXP (x, 0), VOIDmode))
4698         * total = 0;
4699       else
4700         *total = COSTS_N_INSNS (1);
4701       return false;
4702
4703     case ABS:
4704       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4705         *total = COSTS_N_INSNS (1);
4706       else
4707         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4708       return false;
4709
4710     case SIGN_EXTEND:
4711       *total = 0;
4712       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4713         {
4714           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4715             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4716         }
4717       if (mode == DImode)
4718         *total += COSTS_N_INSNS (1);
4719       return false;
4720
4721     case ZERO_EXTEND:
4722       *total = 0;
4723       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4724         {
4725           switch (GET_MODE (XEXP (x, 0)))
4726             {
4727             case QImode:
4728               *total += COSTS_N_INSNS (1);
4729               break;
4730
4731             case HImode:
4732               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4733
4734             case SImode:
4735               break;
4736
4737             default:
4738               *total += COSTS_N_INSNS (2);
4739             }
4740         }
4741
4742       if (mode == DImode)
4743         *total += COSTS_N_INSNS (1);
4744
4745       return false;
4746
4747     case CONST_INT:
4748       if (const_ok_for_arm (INTVAL (x)))
4749         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4750       else if (const_ok_for_arm (~INTVAL (x)))
4751         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4752       else if (const_ok_for_arm (-INTVAL (x)))
4753         {
4754           if (outer_code == COMPARE || outer_code == PLUS
4755               || outer_code == MINUS)
4756             *total = 0;
4757           else
4758             *total = COSTS_N_INSNS (1);
4759         }
4760       else
4761         *total = COSTS_N_INSNS (2);
4762       return true;
4763
4764     case CONST:
4765     case LABEL_REF:
4766     case SYMBOL_REF:
4767       *total = COSTS_N_INSNS (2);
4768       return true;
4769
4770     case CONST_DOUBLE:
4771       *total = COSTS_N_INSNS (4);
4772       return true;
4773
4774     default:
4775       if (mode != VOIDmode)
4776         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4777       else
4778         *total = COSTS_N_INSNS (4); /* How knows?  */
4779       return false;
4780     }
4781 }
4782
4783 /* RTX costs for cores with a slow MUL implementation.  */
4784
4785 static bool
4786 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4787 {
4788   enum machine_mode mode = GET_MODE (x);
4789
4790   if (TARGET_THUMB)
4791     {
4792       *total = thumb_rtx_costs (x, code, outer_code);
4793       return true;
4794     }
4795
4796   switch (code)
4797     {
4798     case MULT:
4799       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4800           || mode == DImode)
4801         {
4802           *total = 30;
4803           return true;
4804         }
4805
4806       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4807         {
4808           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4809                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4810           int cost, const_ok = const_ok_for_arm (i);
4811           int j, booth_unit_size;
4812
4813           /* Tune as appropriate.  */
4814           cost = const_ok ? 4 : 8;
4815           booth_unit_size = 2;
4816           for (j = 0; i && j < 32; j += booth_unit_size)
4817             {
4818               i >>= booth_unit_size;
4819               cost += 2;
4820             }
4821
4822           *total = cost;
4823           return true;
4824         }
4825
4826       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4827                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4828       return true;
4829
4830     default:
4831       *total = arm_rtx_costs_1 (x, code, outer_code);
4832       return true;
4833     }
4834 }
4835
4836
4837 /* RTX cost for cores with a fast multiply unit (M variants).  */
4838
4839 static bool
4840 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4841 {
4842   enum machine_mode mode = GET_MODE (x);
4843
4844   if (TARGET_THUMB)
4845     {
4846       *total = thumb_rtx_costs (x, code, outer_code);
4847       return true;
4848     }
4849
4850   switch (code)
4851     {
4852     case MULT:
4853       /* There is no point basing this on the tuning, since it is always the
4854          fast variant if it exists at all.  */
4855       if (mode == DImode
4856           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4857           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4858               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4859         {
4860           *total = 8;
4861           return true;
4862         }
4863
4864
4865       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4866           || mode == DImode)
4867         {
4868           *total = 30;
4869           return true;
4870         }
4871
4872       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4873         {
4874           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4875                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4876           int cost, const_ok = const_ok_for_arm (i);
4877           int j, booth_unit_size;
4878
4879           /* Tune as appropriate.  */
4880           cost = const_ok ? 4 : 8;
4881           booth_unit_size = 8;
4882           for (j = 0; i && j < 32; j += booth_unit_size)
4883             {
4884               i >>= booth_unit_size;
4885               cost += 2;
4886             }
4887
4888           *total = cost;
4889           return true;
4890         }
4891
4892       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4893                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4894       return true;
4895
4896     default:
4897       *total = arm_rtx_costs_1 (x, code, outer_code);
4898       return true;
4899     }
4900 }
4901
4902
4903 /* RTX cost for XScale CPUs.  */
4904
4905 static bool
4906 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4907 {
4908   enum machine_mode mode = GET_MODE (x);
4909
4910   if (TARGET_THUMB)
4911     {
4912       *total = thumb_rtx_costs (x, code, outer_code);
4913       return true;
4914     }
4915
4916   switch (code)
4917     {
4918     case MULT:
4919       /* There is no point basing this on the tuning, since it is always the
4920          fast variant if it exists at all.  */
4921       if (mode == DImode
4922           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4923           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4924               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4925         {
4926           *total = 8;
4927           return true;
4928         }
4929
4930
4931       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4932           || mode == DImode)
4933         {
4934           *total = 30;
4935           return true;
4936         }
4937
4938       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4939         {
4940           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4941                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4942           int cost, const_ok = const_ok_for_arm (i);
4943           unsigned HOST_WIDE_INT masked_const;
4944
4945           /* The cost will be related to two insns.
4946              First a load of the constant (MOV or LDR), then a multiply.  */
4947           cost = 2;
4948           if (! const_ok)
4949             cost += 1;      /* LDR is probably more expensive because
4950                                of longer result latency.  */
4951           masked_const = i & 0xffff8000;
4952           if (masked_const != 0 && masked_const != 0xffff8000)
4953             {
4954               masked_const = i & 0xf8000000;
4955               if (masked_const == 0 || masked_const == 0xf8000000)
4956                 cost += 1;
4957               else
4958                 cost += 2;
4959             }
4960           *total = cost;
4961           return true;
4962         }
4963
4964       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4965                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4966       return true;
4967
4968     case COMPARE:
4969       /* A COMPARE of a MULT is slow on XScale; the muls instruction
4970          will stall until the multiplication is complete.  */
4971       if (GET_CODE (XEXP (x, 0)) == MULT)
4972         *total = 4 + rtx_cost (XEXP (x, 0), code);
4973       else
4974         *total = arm_rtx_costs_1 (x, code, outer_code);
4975       return true;
4976
4977     default:
4978       *total = arm_rtx_costs_1 (x, code, outer_code);
4979       return true;
4980     }
4981 }
4982
4983
4984 /* RTX costs for 9e (and later) cores.  */
4985
4986 static bool
4987 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4988 {
4989   enum machine_mode mode = GET_MODE (x);
4990   int nonreg_cost;
4991   int cost;
4992
4993   if (TARGET_THUMB)
4994     {
4995       switch (code)
4996         {
4997         case MULT:
4998           *total = COSTS_N_INSNS (3);
4999           return true;
5000
5001         default:
5002           *total = thumb_rtx_costs (x, code, outer_code);
5003           return true;
5004         }
5005     }
5006
5007   switch (code)
5008     {
5009     case MULT:
5010       /* There is no point basing this on the tuning, since it is always the
5011          fast variant if it exists at all.  */
5012       if (mode == DImode
5013           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5014           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5015               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5016         {
5017           *total = 3;
5018           return true;
5019         }
5020
5021
5022       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5023         {
5024           *total = 30;
5025           return true;
5026         }
5027       if (mode == DImode)
5028         {
5029           cost = 7;
5030           nonreg_cost = 8;
5031         }
5032       else
5033         {
5034           cost = 2;
5035           nonreg_cost = 4;
5036         }
5037
5038
5039       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5040                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5041       return true;
5042
5043     default:
5044       *total = arm_rtx_costs_1 (x, code, outer_code);
5045       return true;
5046     }
5047 }
5048 /* All address computations that can be done are free, but rtx cost returns
5049    the same for practically all of them.  So we weight the different types
5050    of address here in the order (most pref first):
5051    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5052 static inline int
5053 arm_arm_address_cost (rtx x)
5054 {
5055   enum rtx_code c  = GET_CODE (x);
5056
5057   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5058     return 0;
5059   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5060     return 10;
5061
5062   if (c == PLUS || c == MINUS)
5063     {
5064       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5065         return 2;
5066
5067       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5068         return 3;
5069
5070       return 4;
5071     }
5072
5073   return 6;
5074 }
5075
5076 static inline int
5077 arm_thumb_address_cost (rtx x)
5078 {
5079   enum rtx_code c  = GET_CODE (x);
5080
5081   if (c == REG)
5082     return 1;
5083   if (c == PLUS
5084       && GET_CODE (XEXP (x, 0)) == REG
5085       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5086     return 1;
5087
5088   return 2;
5089 }
5090
5091 static int
5092 arm_address_cost (rtx x)
5093 {
5094   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5095 }
5096
5097 static int
5098 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5099 {
5100   rtx i_pat, d_pat;
5101
5102   /* Some true dependencies can have a higher cost depending
5103      on precisely how certain input operands are used.  */
5104   if (arm_tune_xscale
5105       && REG_NOTE_KIND (link) == 0
5106       && recog_memoized (insn) >= 0
5107       && recog_memoized (dep) >= 0)
5108     {
5109       int shift_opnum = get_attr_shift (insn);
5110       enum attr_type attr_type = get_attr_type (dep);
5111
5112       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5113          operand for INSN.  If we have a shifted input operand and the
5114          instruction we depend on is another ALU instruction, then we may
5115          have to account for an additional stall.  */
5116       if (shift_opnum != 0
5117           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5118         {
5119           rtx shifted_operand;
5120           int opno;
5121
5122           /* Get the shifted operand.  */
5123           extract_insn (insn);
5124           shifted_operand = recog_data.operand[shift_opnum];
5125
5126           /* Iterate over all the operands in DEP.  If we write an operand
5127              that overlaps with SHIFTED_OPERAND, then we have increase the
5128              cost of this dependency.  */
5129           extract_insn (dep);
5130           preprocess_constraints ();
5131           for (opno = 0; opno < recog_data.n_operands; opno++)
5132             {
5133               /* We can ignore strict inputs.  */
5134               if (recog_data.operand_type[opno] == OP_IN)
5135                 continue;
5136
5137               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5138                                            shifted_operand))
5139                 return 2;
5140             }
5141         }
5142     }
5143
5144   /* XXX This is not strictly true for the FPA.  */
5145   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5146       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5147     return 0;
5148
5149   /* Call insns don't incur a stall, even if they follow a load.  */
5150   if (REG_NOTE_KIND (link) == 0
5151       && GET_CODE (insn) == CALL_INSN)
5152     return 1;
5153
5154   if ((i_pat = single_set (insn)) != NULL
5155       && GET_CODE (SET_SRC (i_pat)) == MEM
5156       && (d_pat = single_set (dep)) != NULL
5157       && GET_CODE (SET_DEST (d_pat)) == MEM)
5158     {
5159       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5160       /* This is a load after a store, there is no conflict if the load reads
5161          from a cached area.  Assume that loads from the stack, and from the
5162          constant pool are cached, and that others will miss.  This is a
5163          hack.  */
5164
5165       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5166           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5167           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5168           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5169         return 1;
5170     }
5171
5172   return cost;
5173 }
5174
5175 static int fp_consts_inited = 0;
5176
5177 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5178 static const char * const strings_fp[8] =
5179 {
5180   "0",   "1",   "2",   "3",
5181   "4",   "5",   "0.5", "10"
5182 };
5183
5184 static REAL_VALUE_TYPE values_fp[8];
5185
5186 static void
5187 init_fp_table (void)
5188 {
5189   int i;
5190   REAL_VALUE_TYPE r;
5191
5192   if (TARGET_VFP)
5193     fp_consts_inited = 1;
5194   else
5195     fp_consts_inited = 8;
5196
5197   for (i = 0; i < fp_consts_inited; i++)
5198     {
5199       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5200       values_fp[i] = r;
5201     }
5202 }
5203
5204 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5205 int
5206 arm_const_double_rtx (rtx x)
5207 {
5208   REAL_VALUE_TYPE r;
5209   int i;
5210
5211   if (!fp_consts_inited)
5212     init_fp_table ();
5213
5214   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5215   if (REAL_VALUE_MINUS_ZERO (r))
5216     return 0;
5217
5218   for (i = 0; i < fp_consts_inited; i++)
5219     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5220       return 1;
5221
5222   return 0;
5223 }
5224
5225 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5226 int
5227 neg_const_double_rtx_ok_for_fpa (rtx x)
5228 {
5229   REAL_VALUE_TYPE r;
5230   int i;
5231
5232   if (!fp_consts_inited)
5233     init_fp_table ();
5234
5235   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5236   r = REAL_VALUE_NEGATE (r);
5237   if (REAL_VALUE_MINUS_ZERO (r))
5238     return 0;
5239
5240   for (i = 0; i < 8; i++)
5241     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5242       return 1;
5243
5244   return 0;
5245 }
5246 \f
5247 /* Predicates for `match_operand' and `match_operator'.  */
5248
5249 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
5250 int
5251 cirrus_memory_offset (rtx op)
5252 {
5253   /* Reject eliminable registers.  */
5254   if (! (reload_in_progress || reload_completed)
5255       && (   reg_mentioned_p (frame_pointer_rtx, op)
5256           || reg_mentioned_p (arg_pointer_rtx, op)
5257           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5258           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5259           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5260           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5261     return 0;
5262
5263   if (GET_CODE (op) == MEM)
5264     {
5265       rtx ind;
5266
5267       ind = XEXP (op, 0);
5268
5269       /* Match: (mem (reg)).  */
5270       if (GET_CODE (ind) == REG)
5271         return 1;
5272
5273       /* Match:
5274          (mem (plus (reg)
5275                     (const))).  */
5276       if (GET_CODE (ind) == PLUS
5277           && GET_CODE (XEXP (ind, 0)) == REG
5278           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5279           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5280         return 1;
5281     }
5282
5283   return 0;
5284 }
5285
5286 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5287    WB if true if writeback address modes are allowed.  */
5288
5289 int
5290 arm_coproc_mem_operand (rtx op, bool wb)
5291 {
5292   rtx ind;
5293
5294   /* Reject eliminable registers.  */
5295   if (! (reload_in_progress || reload_completed)
5296       && (   reg_mentioned_p (frame_pointer_rtx, op)
5297           || reg_mentioned_p (arg_pointer_rtx, op)
5298           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5299           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5300           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5301           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5302     return FALSE;
5303
5304   /* Constants are converted into offsets from labels.  */
5305   if (GET_CODE (op) != MEM)
5306     return FALSE;
5307
5308   ind = XEXP (op, 0);
5309
5310   if (reload_completed
5311       && (GET_CODE (ind) == LABEL_REF
5312           || (GET_CODE (ind) == CONST
5313               && GET_CODE (XEXP (ind, 0)) == PLUS
5314               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5315               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5316     return TRUE;
5317
5318   /* Match: (mem (reg)).  */
5319   if (GET_CODE (ind) == REG)
5320     return arm_address_register_rtx_p (ind, 0);
5321
5322   /* Autoincremment addressing modes.  */
5323   if (wb
5324       && (GET_CODE (ind) == PRE_INC
5325           || GET_CODE (ind) == POST_INC
5326           || GET_CODE (ind) == PRE_DEC
5327           || GET_CODE (ind) == POST_DEC))
5328     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5329
5330   if (wb
5331       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5332       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5333       && GET_CODE (XEXP (ind, 1)) == PLUS
5334       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5335     ind = XEXP (ind, 1);
5336
5337   /* Match:
5338      (plus (reg)
5339            (const)).  */
5340   if (GET_CODE (ind) == PLUS
5341       && GET_CODE (XEXP (ind, 0)) == REG
5342       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5343       && GET_CODE (XEXP (ind, 1)) == CONST_INT
5344       && INTVAL (XEXP (ind, 1)) > -1024
5345       && INTVAL (XEXP (ind, 1)) <  1024
5346       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5347     return TRUE;
5348
5349   return FALSE;
5350 }
5351
5352 /* Return true if X is a register that will be eliminated later on.  */
5353 int
5354 arm_eliminable_register (rtx x)
5355 {
5356   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5357                        || REGNO (x) == ARG_POINTER_REGNUM
5358                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5359                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5360 }
5361
5362 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5363    VFP registers.  Otherwise return NO_REGS.  */
5364
5365 enum reg_class
5366 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
5367 {
5368   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
5369     return NO_REGS;
5370
5371   return GENERAL_REGS;
5372 }
5373
5374 /* Values which must be returned in the most-significant end of the return
5375    register.  */
5376
5377 static bool
5378 arm_return_in_msb (tree valtype)
5379 {
5380   return (TARGET_AAPCS_BASED
5381           && BYTES_BIG_ENDIAN
5382           && (AGGREGATE_TYPE_P (valtype)
5383               || TREE_CODE (valtype) == COMPLEX_TYPE));
5384 }
5385
5386 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5387    Use by the Cirrus Maverick code which has to workaround
5388    a hardware bug triggered by such instructions.  */
5389 static bool
5390 arm_memory_load_p (rtx insn)
5391 {
5392   rtx body, lhs, rhs;;
5393
5394   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5395     return false;
5396
5397   body = PATTERN (insn);
5398
5399   if (GET_CODE (body) != SET)
5400     return false;
5401
5402   lhs = XEXP (body, 0);
5403   rhs = XEXP (body, 1);
5404
5405   lhs = REG_OR_SUBREG_RTX (lhs);
5406
5407   /* If the destination is not a general purpose
5408      register we do not have to worry.  */
5409   if (GET_CODE (lhs) != REG
5410       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5411     return false;
5412
5413   /* As well as loads from memory we also have to react
5414      to loads of invalid constants which will be turned
5415      into loads from the minipool.  */
5416   return (GET_CODE (rhs) == MEM
5417           || GET_CODE (rhs) == SYMBOL_REF
5418           || note_invalid_constants (insn, -1, false));
5419 }
5420
5421 /* Return TRUE if INSN is a Cirrus instruction.  */
5422 static bool
5423 arm_cirrus_insn_p (rtx insn)
5424 {
5425   enum attr_cirrus attr;
5426
5427   /* get_attr cannot accept USE or CLOBBER.  */
5428   if (!insn
5429       || GET_CODE (insn) != INSN
5430       || GET_CODE (PATTERN (insn)) == USE
5431       || GET_CODE (PATTERN (insn)) == CLOBBER)
5432     return 0;
5433
5434   attr = get_attr_cirrus (insn);
5435
5436   return attr != CIRRUS_NOT;
5437 }
5438
5439 /* Cirrus reorg for invalid instruction combinations.  */
5440 static void
5441 cirrus_reorg (rtx first)
5442 {
5443   enum attr_cirrus attr;
5444   rtx body = PATTERN (first);
5445   rtx t;
5446   int nops;
5447
5448   /* Any branch must be followed by 2 non Cirrus instructions.  */
5449   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5450     {
5451       nops = 0;
5452       t = next_nonnote_insn (first);
5453
5454       if (arm_cirrus_insn_p (t))
5455         ++ nops;
5456
5457       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5458         ++ nops;
5459
5460       while (nops --)
5461         emit_insn_after (gen_nop (), first);
5462
5463       return;
5464     }
5465
5466   /* (float (blah)) is in parallel with a clobber.  */
5467   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5468     body = XVECEXP (body, 0, 0);
5469
5470   if (GET_CODE (body) == SET)
5471     {
5472       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5473
5474       /* cfldrd, cfldr64, cfstrd, cfstr64 must
5475          be followed by a non Cirrus insn.  */
5476       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5477         {
5478           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5479             emit_insn_after (gen_nop (), first);
5480
5481           return;
5482         }
5483       else if (arm_memory_load_p (first))
5484         {
5485           unsigned int arm_regno;
5486
5487           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5488              ldr/cfmv64hr combination where the Rd field is the same
5489              in both instructions must be split with a non Cirrus
5490              insn.  Example:
5491
5492              ldr r0, blah
5493              nop
5494              cfmvsr mvf0, r0.  */
5495
5496           /* Get Arm register number for ldr insn.  */
5497           if (GET_CODE (lhs) == REG)
5498             arm_regno = REGNO (lhs);
5499           else
5500             {
5501               gcc_assert (GET_CODE (rhs) == REG);
5502               arm_regno = REGNO (rhs);
5503             }
5504
5505           /* Next insn.  */
5506           first = next_nonnote_insn (first);
5507
5508           if (! arm_cirrus_insn_p (first))
5509             return;
5510
5511           body = PATTERN (first);
5512
5513           /* (float (blah)) is in parallel with a clobber.  */
5514           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5515             body = XVECEXP (body, 0, 0);
5516
5517           if (GET_CODE (body) == FLOAT)
5518             body = XEXP (body, 0);
5519
5520           if (get_attr_cirrus (first) == CIRRUS_MOVE
5521               && GET_CODE (XEXP (body, 1)) == REG
5522               && arm_regno == REGNO (XEXP (body, 1)))
5523             emit_insn_after (gen_nop (), first);
5524
5525           return;
5526         }
5527     }
5528
5529   /* get_attr cannot accept USE or CLOBBER.  */
5530   if (!first
5531       || GET_CODE (first) != INSN
5532       || GET_CODE (PATTERN (first)) == USE
5533       || GET_CODE (PATTERN (first)) == CLOBBER)
5534     return;
5535
5536   attr = get_attr_cirrus (first);
5537
5538   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5539      must be followed by a non-coprocessor instruction.  */
5540   if (attr == CIRRUS_COMPARE)
5541     {
5542       nops = 0;
5543
5544       t = next_nonnote_insn (first);
5545
5546       if (arm_cirrus_insn_p (t))
5547         ++ nops;
5548
5549       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5550         ++ nops;
5551
5552       while (nops --)
5553         emit_insn_after (gen_nop (), first);
5554
5555       return;
5556     }
5557 }
5558
5559 /* Return TRUE if X references a SYMBOL_REF.  */
5560 int
5561 symbol_mentioned_p (rtx x)
5562 {
5563   const char * fmt;
5564   int i;
5565
5566   if (GET_CODE (x) == SYMBOL_REF)
5567     return 1;
5568
5569   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5570      are constant offsets, not symbols.  */
5571   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5572     return 0;
5573
5574   fmt = GET_RTX_FORMAT (GET_CODE (x));
5575
5576   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5577     {
5578       if (fmt[i] == 'E')
5579         {
5580           int j;
5581
5582           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5583             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5584               return 1;
5585         }
5586       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5587         return 1;
5588     }
5589
5590   return 0;
5591 }
5592
5593 /* Return TRUE if X references a LABEL_REF.  */
5594 int
5595 label_mentioned_p (rtx x)
5596 {
5597   const char * fmt;
5598   int i;
5599
5600   if (GET_CODE (x) == LABEL_REF)
5601     return 1;
5602
5603   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
5604      instruction, but they are constant offsets, not symbols.  */
5605   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5606     return 0;
5607
5608   fmt = GET_RTX_FORMAT (GET_CODE (x));
5609   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5610     {
5611       if (fmt[i] == 'E')
5612         {
5613           int j;
5614
5615           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5616             if (label_mentioned_p (XVECEXP (x, i, j)))
5617               return 1;
5618         }
5619       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5620         return 1;
5621     }
5622
5623   return 0;
5624 }
5625
5626 int
5627 tls_mentioned_p (rtx x)
5628 {
5629   switch (GET_CODE (x))
5630     {
5631     case CONST:
5632       return tls_mentioned_p (XEXP (x, 0));
5633
5634     case UNSPEC:
5635       if (XINT (x, 1) == UNSPEC_TLS)
5636         return 1;
5637
5638     default:
5639       return 0;
5640     }
5641 }
5642
5643 /* Must not copy a SET whose source operand is PC-relative.  */
5644
5645 static bool
5646 arm_cannot_copy_insn_p (rtx insn)
5647 {
5648   rtx pat = PATTERN (insn);
5649
5650   if (GET_CODE (pat) == PARALLEL
5651       && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
5652     {
5653       rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
5654
5655       if (GET_CODE (rhs) == UNSPEC
5656           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
5657         return TRUE;
5658
5659       if (GET_CODE (rhs) == MEM
5660           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
5661           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
5662         return TRUE;
5663     }
5664
5665   return FALSE;
5666 }
5667
5668 enum rtx_code
5669 minmax_code (rtx x)
5670 {
5671   enum rtx_code code = GET_CODE (x);
5672
5673   switch (code)
5674     {
5675     case SMAX:
5676       return GE;
5677     case SMIN:
5678       return LE;
5679     case UMIN:
5680       return LEU;
5681     case UMAX:
5682       return GEU;
5683     default:
5684       gcc_unreachable ();
5685     }
5686 }
5687
5688 /* Return 1 if memory locations are adjacent.  */
5689 int
5690 adjacent_mem_locations (rtx a, rtx b)
5691 {
5692   /* We don't guarantee to preserve the order of these memory refs.  */
5693   if (volatile_refs_p (a) || volatile_refs_p (b))
5694     return 0;
5695
5696   if ((GET_CODE (XEXP (a, 0)) == REG
5697        || (GET_CODE (XEXP (a, 0)) == PLUS
5698            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5699       && (GET_CODE (XEXP (b, 0)) == REG
5700           || (GET_CODE (XEXP (b, 0)) == PLUS
5701               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5702     {
5703       HOST_WIDE_INT val0 = 0, val1 = 0;
5704       rtx reg0, reg1;
5705       int val_diff;
5706
5707       if (GET_CODE (XEXP (a, 0)) == PLUS)
5708         {
5709           reg0 = XEXP (XEXP (a, 0), 0);
5710           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5711         }
5712       else
5713         reg0 = XEXP (a, 0);
5714
5715       if (GET_CODE (XEXP (b, 0)) == PLUS)
5716         {
5717           reg1 = XEXP (XEXP (b, 0), 0);
5718           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5719         }
5720       else
5721         reg1 = XEXP (b, 0);
5722
5723       /* Don't accept any offset that will require multiple
5724          instructions to handle, since this would cause the
5725          arith_adjacentmem pattern to output an overlong sequence.  */
5726       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5727         return 0;
5728
5729       /* Don't allow an eliminable register: register elimination can make
5730          the offset too large.  */
5731       if (arm_eliminable_register (reg0))
5732         return 0;
5733
5734       val_diff = val1 - val0;
5735
5736       if (arm_ld_sched)
5737         {
5738           /* If the target has load delay slots, then there's no benefit
5739              to using an ldm instruction unless the offset is zero and
5740              we are optimizing for size.  */
5741           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5742                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5743                   && (val_diff == 4 || val_diff == -4));
5744         }
5745
5746       return ((REGNO (reg0) == REGNO (reg1))
5747               && (val_diff == 4 || val_diff == -4));
5748     }
5749
5750   return 0;
5751 }
5752
5753 int
5754 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5755                         HOST_WIDE_INT *load_offset)
5756 {
5757   int unsorted_regs[4];
5758   HOST_WIDE_INT unsorted_offsets[4];
5759   int order[4];
5760   int base_reg = -1;
5761   int i;
5762
5763   /* Can only handle 2, 3, or 4 insns at present,
5764      though could be easily extended if required.  */
5765   gcc_assert (nops >= 2 && nops <= 4);
5766
5767   /* Loop over the operands and check that the memory references are
5768      suitable (i.e. immediate offsets from the same base register).  At
5769      the same time, extract the target register, and the memory
5770      offsets.  */
5771   for (i = 0; i < nops; i++)
5772     {
5773       rtx reg;
5774       rtx offset;
5775
5776       /* Convert a subreg of a mem into the mem itself.  */
5777       if (GET_CODE (operands[nops + i]) == SUBREG)
5778         operands[nops + i] = alter_subreg (operands + (nops + i));
5779
5780       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5781
5782       /* Don't reorder volatile memory references; it doesn't seem worth
5783          looking for the case where the order is ok anyway.  */
5784       if (MEM_VOLATILE_P (operands[nops + i]))
5785         return 0;
5786
5787       offset = const0_rtx;
5788
5789       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5790            || (GET_CODE (reg) == SUBREG
5791                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5792           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5793               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5794                    == REG)
5795                   || (GET_CODE (reg) == SUBREG
5796                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5797               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5798                   == CONST_INT)))
5799         {
5800           if (i == 0)
5801             {
5802               base_reg = REGNO (reg);
5803               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5804                                   ? REGNO (operands[i])
5805                                   : REGNO (SUBREG_REG (operands[i])));
5806               order[0] = 0;
5807             }
5808           else
5809             {
5810               if (base_reg != (int) REGNO (reg))
5811                 /* Not addressed from the same base register.  */
5812                 return 0;
5813
5814               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5815                                   ? REGNO (operands[i])
5816                                   : REGNO (SUBREG_REG (operands[i])));
5817               if (unsorted_regs[i] < unsorted_regs[order[0]])
5818                 order[0] = i;
5819             }
5820
5821           /* If it isn't an integer register, or if it overwrites the
5822              base register but isn't the last insn in the list, then
5823              we can't do this.  */
5824           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5825               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5826             return 0;
5827
5828           unsorted_offsets[i] = INTVAL (offset);
5829         }
5830       else
5831         /* Not a suitable memory address.  */
5832         return 0;
5833     }
5834
5835   /* All the useful information has now been extracted from the
5836      operands into unsorted_regs and unsorted_offsets; additionally,
5837      order[0] has been set to the lowest numbered register in the
5838      list.  Sort the registers into order, and check that the memory
5839      offsets are ascending and adjacent.  */
5840
5841   for (i = 1; i < nops; i++)
5842     {
5843       int j;
5844
5845       order[i] = order[i - 1];
5846       for (j = 0; j < nops; j++)
5847         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5848             && (order[i] == order[i - 1]
5849                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5850           order[i] = j;
5851
5852       /* Have we found a suitable register? if not, one must be used more
5853          than once.  */
5854       if (order[i] == order[i - 1])
5855         return 0;
5856
5857       /* Is the memory address adjacent and ascending? */
5858       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5859         return 0;
5860     }
5861
5862   if (base)
5863     {
5864       *base = base_reg;
5865
5866       for (i = 0; i < nops; i++)
5867         regs[i] = unsorted_regs[order[i]];
5868
5869       *load_offset = unsorted_offsets[order[0]];
5870     }
5871
5872   if (unsorted_offsets[order[0]] == 0)
5873     return 1; /* ldmia */
5874
5875   if (unsorted_offsets[order[0]] == 4)
5876     return 2; /* ldmib */
5877
5878   if (unsorted_offsets[order[nops - 1]] == 0)
5879     return 3; /* ldmda */
5880
5881   if (unsorted_offsets[order[nops - 1]] == -4)
5882     return 4; /* ldmdb */
5883
5884   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5885      if the offset isn't small enough.  The reason 2 ldrs are faster
5886      is because these ARMs are able to do more than one cache access
5887      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5888      whilst the ARM8 has a double bandwidth cache.  This means that
5889      these cores can do both an instruction fetch and a data fetch in
5890      a single cycle, so the trick of calculating the address into a
5891      scratch register (one of the result regs) and then doing a load
5892      multiple actually becomes slower (and no smaller in code size).
5893      That is the transformation
5894
5895         ldr     rd1, [rbase + offset]
5896         ldr     rd2, [rbase + offset + 4]
5897
5898      to
5899
5900         add     rd1, rbase, offset
5901         ldmia   rd1, {rd1, rd2}
5902
5903      produces worse code -- '3 cycles + any stalls on rd2' instead of
5904      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5905      access per cycle, the first sequence could never complete in less
5906      than 6 cycles, whereas the ldm sequence would only take 5 and
5907      would make better use of sequential accesses if not hitting the
5908      cache.
5909
5910      We cheat here and test 'arm_ld_sched' which we currently know to
5911      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5912      changes, then the test below needs to be reworked.  */
5913   if (nops == 2 && arm_ld_sched)
5914     return 0;
5915
5916   /* Can't do it without setting up the offset, only do this if it takes
5917      no more than one insn.  */
5918   return (const_ok_for_arm (unsorted_offsets[order[0]])
5919           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5920 }
5921
5922 const char *
5923 emit_ldm_seq (rtx *operands, int nops)
5924 {
5925   int regs[4];
5926   int base_reg;
5927   HOST_WIDE_INT offset;
5928   char buf[100];
5929   int i;
5930
5931   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5932     {
5933     case 1:
5934       strcpy (buf, "ldm%?ia\t");
5935       break;
5936
5937     case 2:
5938       strcpy (buf, "ldm%?ib\t");
5939       break;
5940
5941     case 3:
5942       strcpy (buf, "ldm%?da\t");
5943       break;
5944
5945     case 4:
5946       strcpy (buf, "ldm%?db\t");
5947       break;
5948
5949     case 5:
5950       if (offset >= 0)
5951         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5952                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5953                  (long) offset);
5954       else
5955         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5956                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5957                  (long) -offset);
5958       output_asm_insn (buf, operands);
5959       base_reg = regs[0];
5960       strcpy (buf, "ldm%?ia\t");
5961       break;
5962
5963     default:
5964       gcc_unreachable ();
5965     }
5966
5967   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5968            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5969
5970   for (i = 1; i < nops; i++)
5971     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5972              reg_names[regs[i]]);
5973
5974   strcat (buf, "}\t%@ phole ldm");
5975
5976   output_asm_insn (buf, operands);
5977   return "";
5978 }
5979
5980 int
5981 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5982                          HOST_WIDE_INT * load_offset)
5983 {
5984   int unsorted_regs[4];
5985   HOST_WIDE_INT unsorted_offsets[4];
5986   int order[4];
5987   int base_reg = -1;
5988   int i;
5989
5990   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5991      extended if required.  */
5992   gcc_assert (nops >= 2 && nops <= 4);
5993
5994   /* Loop over the operands and check that the memory references are
5995      suitable (i.e. immediate offsets from the same base register).  At
5996      the same time, extract the target register, and the memory
5997      offsets.  */
5998   for (i = 0; i < nops; i++)
5999     {
6000       rtx reg;
6001       rtx offset;
6002
6003       /* Convert a subreg of a mem into the mem itself.  */
6004       if (GET_CODE (operands[nops + i]) == SUBREG)
6005         operands[nops + i] = alter_subreg (operands + (nops + i));
6006
6007       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6008
6009       /* Don't reorder volatile memory references; it doesn't seem worth
6010          looking for the case where the order is ok anyway.  */
6011       if (MEM_VOLATILE_P (operands[nops + i]))
6012         return 0;
6013
6014       offset = const0_rtx;
6015
6016       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6017            || (GET_CODE (reg) == SUBREG
6018                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6019           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6020               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6021                    == REG)
6022                   || (GET_CODE (reg) == SUBREG
6023                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6024               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6025                   == CONST_INT)))
6026         {
6027           if (i == 0)
6028             {
6029               base_reg = REGNO (reg);
6030               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6031                                   ? REGNO (operands[i])
6032                                   : REGNO (SUBREG_REG (operands[i])));
6033               order[0] = 0;
6034             }
6035           else
6036             {
6037               if (base_reg != (int) REGNO (reg))
6038                 /* Not addressed from the same base register.  */
6039                 return 0;
6040
6041               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6042                                   ? REGNO (operands[i])
6043                                   : REGNO (SUBREG_REG (operands[i])));
6044               if (unsorted_regs[i] < unsorted_regs[order[0]])
6045                 order[0] = i;
6046             }
6047
6048           /* If it isn't an integer register, then we can't do this.  */
6049           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6050             return 0;
6051
6052           unsorted_offsets[i] = INTVAL (offset);
6053         }
6054       else
6055         /* Not a suitable memory address.  */
6056         return 0;
6057     }
6058
6059   /* All the useful information has now been extracted from the
6060      operands into unsorted_regs and unsorted_offsets; additionally,
6061      order[0] has been set to the lowest numbered register in the
6062      list.  Sort the registers into order, and check that the memory
6063      offsets are ascending and adjacent.  */
6064
6065   for (i = 1; i < nops; i++)
6066     {
6067       int j;
6068
6069       order[i] = order[i - 1];
6070       for (j = 0; j < nops; j++)
6071         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6072             && (order[i] == order[i - 1]
6073                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6074           order[i] = j;
6075
6076       /* Have we found a suitable register? if not, one must be used more
6077          than once.  */
6078       if (order[i] == order[i - 1])
6079         return 0;
6080
6081       /* Is the memory address adjacent and ascending? */
6082       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6083         return 0;
6084     }
6085
6086   if (base)
6087     {
6088       *base = base_reg;
6089
6090       for (i = 0; i < nops; i++)
6091         regs[i] = unsorted_regs[order[i]];
6092
6093       *load_offset = unsorted_offsets[order[0]];
6094     }
6095
6096   if (unsorted_offsets[order[0]] == 0)
6097     return 1; /* stmia */
6098
6099   if (unsorted_offsets[order[0]] == 4)
6100     return 2; /* stmib */
6101
6102   if (unsorted_offsets[order[nops - 1]] == 0)
6103     return 3; /* stmda */
6104
6105   if (unsorted_offsets[order[nops - 1]] == -4)
6106     return 4; /* stmdb */
6107
6108   return 0;
6109 }
6110
6111 const char *
6112 emit_stm_seq (rtx *operands, int nops)
6113 {
6114   int regs[4];
6115   int base_reg;
6116   HOST_WIDE_INT offset;
6117   char buf[100];
6118   int i;
6119
6120   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6121     {
6122     case 1:
6123       strcpy (buf, "stm%?ia\t");
6124       break;
6125
6126     case 2:
6127       strcpy (buf, "stm%?ib\t");
6128       break;
6129
6130     case 3:
6131       strcpy (buf, "stm%?da\t");
6132       break;
6133
6134     case 4:
6135       strcpy (buf, "stm%?db\t");
6136       break;
6137
6138     default:
6139       gcc_unreachable ();
6140     }
6141
6142   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6143            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6144
6145   for (i = 1; i < nops; i++)
6146     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6147              reg_names[regs[i]]);
6148
6149   strcat (buf, "}\t%@ phole stm");
6150
6151   output_asm_insn (buf, operands);
6152   return "";
6153 }
6154 \f
6155 /* Routines for use in generating RTL.  */
6156
6157 rtx
6158 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6159                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6160 {
6161   HOST_WIDE_INT offset = *offsetp;
6162   int i = 0, j;
6163   rtx result;
6164   int sign = up ? 1 : -1;
6165   rtx mem, addr;
6166
6167   /* XScale has load-store double instructions, but they have stricter
6168      alignment requirements than load-store multiple, so we cannot
6169      use them.
6170
6171      For XScale ldm requires 2 + NREGS cycles to complete and blocks
6172      the pipeline until completion.
6173
6174         NREGS           CYCLES
6175           1               3
6176           2               4
6177           3               5
6178           4               6
6179
6180      An ldr instruction takes 1-3 cycles, but does not block the
6181      pipeline.
6182
6183         NREGS           CYCLES
6184           1              1-3
6185           2              2-6
6186           3              3-9
6187           4              4-12
6188
6189      Best case ldr will always win.  However, the more ldr instructions
6190      we issue, the less likely we are to be able to schedule them well.
6191      Using ldr instructions also increases code size.
6192
6193      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6194      for counts of 3 or 4 regs.  */
6195   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6196     {
6197       rtx seq;
6198
6199       start_sequence ();
6200
6201       for (i = 0; i < count; i++)
6202         {
6203           addr = plus_constant (from, i * 4 * sign);
6204           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6205           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6206           offset += 4 * sign;
6207         }
6208
6209       if (write_back)
6210         {
6211           emit_move_insn (from, plus_constant (from, count * 4 * sign));
6212           *offsetp = offset;
6213         }
6214
6215       seq = get_insns ();
6216       end_sequence ();
6217
6218       return seq;
6219     }
6220
6221   result = gen_rtx_PARALLEL (VOIDmode,
6222                              rtvec_alloc (count + (write_back ? 1 : 0)));
6223   if (write_back)
6224     {
6225       XVECEXP (result, 0, 0)
6226         = gen_rtx_SET (GET_MODE (from), from,
6227                        plus_constant (from, count * 4 * sign));
6228       i = 1;
6229       count++;
6230     }
6231
6232   for (j = 0; i < count; i++, j++)
6233     {
6234       addr = plus_constant (from, j * 4 * sign);
6235       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6236       XVECEXP (result, 0, i)
6237         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6238       offset += 4 * sign;
6239     }
6240
6241   if (write_back)
6242     *offsetp = offset;
6243
6244   return result;
6245 }
6246
6247 rtx
6248 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6249                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6250 {
6251   HOST_WIDE_INT offset = *offsetp;
6252   int i = 0, j;
6253   rtx result;
6254   int sign = up ? 1 : -1;
6255   rtx mem, addr;
6256
6257   /* See arm_gen_load_multiple for discussion of
6258      the pros/cons of ldm/stm usage for XScale.  */
6259   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6260     {
6261       rtx seq;
6262
6263       start_sequence ();
6264
6265       for (i = 0; i < count; i++)
6266         {
6267           addr = plus_constant (to, i * 4 * sign);
6268           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6269           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6270           offset += 4 * sign;
6271         }
6272
6273       if (write_back)
6274         {
6275           emit_move_insn (to, plus_constant (to, count * 4 * sign));
6276           *offsetp = offset;
6277         }
6278
6279       seq = get_insns ();
6280       end_sequence ();
6281
6282       return seq;
6283     }
6284
6285   result = gen_rtx_PARALLEL (VOIDmode,
6286                              rtvec_alloc (count + (write_back ? 1 : 0)));
6287   if (write_back)
6288     {
6289       XVECEXP (result, 0, 0)
6290         = gen_rtx_SET (GET_MODE (to), to,
6291                        plus_constant (to, count * 4 * sign));
6292       i = 1;
6293       count++;
6294     }
6295
6296   for (j = 0; i < count; i++, j++)
6297     {
6298       addr = plus_constant (to, j * 4 * sign);
6299       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6300       XVECEXP (result, 0, i)
6301         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6302       offset += 4 * sign;
6303     }
6304
6305   if (write_back)
6306     *offsetp = offset;
6307
6308   return result;
6309 }
6310
6311 int
6312 arm_gen_movmemqi (rtx *operands)
6313 {
6314   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6315   HOST_WIDE_INT srcoffset, dstoffset;
6316   int i;
6317   rtx src, dst, srcbase, dstbase;
6318   rtx part_bytes_reg = NULL;
6319   rtx mem;
6320
6321   if (GET_CODE (operands[2]) != CONST_INT
6322       || GET_CODE (operands[3]) != CONST_INT
6323       || INTVAL (operands[2]) > 64
6324       || INTVAL (operands[3]) & 3)
6325     return 0;
6326
6327   dstbase = operands[0];
6328   srcbase = operands[1];
6329
6330   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6331   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6332
6333   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6334   out_words_to_go = INTVAL (operands[2]) / 4;
6335   last_bytes = INTVAL (operands[2]) & 3;
6336   dstoffset = srcoffset = 0;
6337
6338   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6339     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6340
6341   for (i = 0; in_words_to_go >= 2; i+=4)
6342     {
6343       if (in_words_to_go > 4)
6344         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6345                                           srcbase, &srcoffset));
6346       else
6347         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6348                                           FALSE, srcbase, &srcoffset));
6349
6350       if (out_words_to_go)
6351         {
6352           if (out_words_to_go > 4)
6353             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6354                                                dstbase, &dstoffset));
6355           else if (out_words_to_go != 1)
6356             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6357                                                dst, TRUE,
6358                                                (last_bytes == 0
6359                                                 ? FALSE : TRUE),
6360                                                dstbase, &dstoffset));
6361           else
6362             {
6363               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6364               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6365               if (last_bytes != 0)
6366                 {
6367                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6368                   dstoffset += 4;
6369                 }
6370             }
6371         }
6372
6373       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6374       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6375     }
6376
6377   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6378   if (out_words_to_go)
6379     {
6380       rtx sreg;
6381
6382       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6383       sreg = copy_to_reg (mem);
6384
6385       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6386       emit_move_insn (mem, sreg);
6387       in_words_to_go--;
6388
6389       gcc_assert (!in_words_to_go);     /* Sanity check */
6390     }
6391
6392   if (in_words_to_go)
6393     {
6394       gcc_assert (in_words_to_go > 0);
6395
6396       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6397       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6398     }
6399
6400   gcc_assert (!last_bytes || part_bytes_reg);
6401
6402   if (BYTES_BIG_ENDIAN && last_bytes)
6403     {
6404       rtx tmp = gen_reg_rtx (SImode);
6405
6406       /* The bytes we want are in the top end of the word.  */
6407       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6408                               GEN_INT (8 * (4 - last_bytes))));
6409       part_bytes_reg = tmp;
6410
6411       while (last_bytes)
6412         {
6413           mem = adjust_automodify_address (dstbase, QImode,
6414                                            plus_constant (dst, last_bytes - 1),
6415                                            dstoffset + last_bytes - 1);
6416           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6417
6418           if (--last_bytes)
6419             {
6420               tmp = gen_reg_rtx (SImode);
6421               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6422               part_bytes_reg = tmp;
6423             }
6424         }
6425
6426     }
6427   else
6428     {
6429       if (last_bytes > 1)
6430         {
6431           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6432           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6433           last_bytes -= 2;
6434           if (last_bytes)
6435             {
6436               rtx tmp = gen_reg_rtx (SImode);
6437               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6438               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6439               part_bytes_reg = tmp;
6440               dstoffset += 2;
6441             }
6442         }
6443
6444       if (last_bytes)
6445         {
6446           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6447           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6448         }
6449     }
6450
6451   return 1;
6452 }
6453
6454 /* Select a dominance comparison mode if possible for a test of the general
6455    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6456    COND_OR == DOM_CC_X_AND_Y => (X && Y)
6457    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6458    COND_OR == DOM_CC_X_OR_Y => (X || Y)
6459    In all cases OP will be either EQ or NE, but we don't need to know which
6460    here.  If we are unable to support a dominance comparison we return
6461    CC mode.  This will then fail to match for the RTL expressions that
6462    generate this call.  */
6463 enum machine_mode
6464 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6465 {
6466   enum rtx_code cond1, cond2;
6467   int swapped = 0;
6468
6469   /* Currently we will probably get the wrong result if the individual
6470      comparisons are not simple.  This also ensures that it is safe to
6471      reverse a comparison if necessary.  */
6472   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6473        != CCmode)
6474       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6475           != CCmode))
6476     return CCmode;
6477
6478   /* The if_then_else variant of this tests the second condition if the
6479      first passes, but is true if the first fails.  Reverse the first
6480      condition to get a true "inclusive-or" expression.  */
6481   if (cond_or == DOM_CC_NX_OR_Y)
6482     cond1 = reverse_condition (cond1);
6483
6484   /* If the comparisons are not equal, and one doesn't dominate the other,
6485      then we can't do this.  */
6486   if (cond1 != cond2
6487       && !comparison_dominates_p (cond1, cond2)
6488       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6489     return CCmode;
6490
6491   if (swapped)
6492     {
6493       enum rtx_code temp = cond1;
6494       cond1 = cond2;
6495       cond2 = temp;
6496     }
6497
6498   switch (cond1)
6499     {
6500     case EQ:
6501       if (cond_or == DOM_CC_X_AND_Y)
6502         return CC_DEQmode;
6503
6504       switch (cond2)
6505         {
6506         case EQ: return CC_DEQmode;
6507         case LE: return CC_DLEmode;
6508         case LEU: return CC_DLEUmode;
6509         case GE: return CC_DGEmode;
6510         case GEU: return CC_DGEUmode;
6511         default: gcc_unreachable ();
6512         }
6513
6514     case LT:
6515       if (cond_or == DOM_CC_X_AND_Y)
6516         return CC_DLTmode;
6517
6518       switch (cond2)
6519         {
6520         case  LT:
6521             return CC_DLTmode;
6522         case LE:
6523           return CC_DLEmode;
6524         case NE:
6525           return CC_DNEmode;
6526         default:
6527           gcc_unreachable ();
6528         }
6529
6530     case GT:
6531       if (cond_or == DOM_CC_X_AND_Y)
6532         return CC_DGTmode;
6533
6534       switch (cond2)
6535         {
6536         case GT:
6537           return CC_DGTmode;
6538         case GE:
6539           return CC_DGEmode;
6540         case NE:
6541           return CC_DNEmode;
6542         default:
6543           gcc_unreachable ();
6544         }
6545
6546     case LTU:
6547       if (cond_or == DOM_CC_X_AND_Y)
6548         return CC_DLTUmode;
6549
6550       switch (cond2)
6551         {
6552         case LTU:
6553           return CC_DLTUmode;
6554         case LEU:
6555           return CC_DLEUmode;
6556         case NE:
6557           return CC_DNEmode;
6558         default:
6559           gcc_unreachable ();
6560         }
6561
6562     case GTU:
6563       if (cond_or == DOM_CC_X_AND_Y)
6564         return CC_DGTUmode;
6565
6566       switch (cond2)
6567         {
6568         case GTU:
6569           return CC_DGTUmode;
6570         case GEU:
6571           return CC_DGEUmode;
6572         case NE:
6573           return CC_DNEmode;
6574         default:
6575           gcc_unreachable ();
6576         }
6577
6578     /* The remaining cases only occur when both comparisons are the
6579        same.  */
6580     case NE:
6581       gcc_assert (cond1 == cond2);
6582       return CC_DNEmode;
6583
6584     case LE:
6585       gcc_assert (cond1 == cond2);
6586       return CC_DLEmode;
6587
6588     case GE:
6589       gcc_assert (cond1 == cond2);
6590       return CC_DGEmode;
6591
6592     case LEU:
6593       gcc_assert (cond1 == cond2);
6594       return CC_DLEUmode;
6595
6596     case GEU:
6597       gcc_assert (cond1 == cond2);
6598       return CC_DGEUmode;
6599
6600     default:
6601       gcc_unreachable ();
6602     }
6603 }
6604
6605 enum machine_mode
6606 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6607 {
6608   /* All floating point compares return CCFP if it is an equality
6609      comparison, and CCFPE otherwise.  */
6610   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6611     {
6612       switch (op)
6613         {
6614         case EQ:
6615         case NE:
6616         case UNORDERED:
6617         case ORDERED:
6618         case UNLT:
6619         case UNLE:
6620         case UNGT:
6621         case UNGE:
6622         case UNEQ:
6623         case LTGT:
6624           return CCFPmode;
6625
6626         case LT:
6627         case LE:
6628         case GT:
6629         case GE:
6630           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6631             return CCFPmode;
6632           return CCFPEmode;
6633
6634         default:
6635           gcc_unreachable ();
6636         }
6637     }
6638
6639   /* A compare with a shifted operand.  Because of canonicalization, the
6640      comparison will have to be swapped when we emit the assembler.  */
6641   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6642       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6643           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6644           || GET_CODE (x) == ROTATERT))
6645     return CC_SWPmode;
6646
6647   /* This operation is performed swapped, but since we only rely on the Z
6648      flag we don't need an additional mode.  */
6649   if (GET_MODE (y) == SImode && REG_P (y)
6650       && GET_CODE (x) == NEG
6651       && (op == EQ || op == NE))
6652     return CC_Zmode;
6653
6654   /* This is a special case that is used by combine to allow a
6655      comparison of a shifted byte load to be split into a zero-extend
6656      followed by a comparison of the shifted integer (only valid for
6657      equalities and unsigned inequalities).  */
6658   if (GET_MODE (x) == SImode
6659       && GET_CODE (x) == ASHIFT
6660       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6661       && GET_CODE (XEXP (x, 0)) == SUBREG
6662       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6663       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6664       && (op == EQ || op == NE
6665           || op == GEU || op == GTU || op == LTU || op == LEU)
6666       && GET_CODE (y) == CONST_INT)
6667     return CC_Zmode;
6668
6669   /* A construct for a conditional compare, if the false arm contains
6670      0, then both conditions must be true, otherwise either condition
6671      must be true.  Not all conditions are possible, so CCmode is
6672      returned if it can't be done.  */
6673   if (GET_CODE (x) == IF_THEN_ELSE
6674       && (XEXP (x, 2) == const0_rtx
6675           || XEXP (x, 2) == const1_rtx)
6676       && COMPARISON_P (XEXP (x, 0))
6677       && COMPARISON_P (XEXP (x, 1)))
6678     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6679                                          INTVAL (XEXP (x, 2)));
6680
6681   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6682   if (GET_CODE (x) == AND
6683       && COMPARISON_P (XEXP (x, 0))
6684       && COMPARISON_P (XEXP (x, 1)))
6685     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6686                                          DOM_CC_X_AND_Y);
6687
6688   if (GET_CODE (x) == IOR
6689       && COMPARISON_P (XEXP (x, 0))
6690       && COMPARISON_P (XEXP (x, 1)))
6691     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6692                                          DOM_CC_X_OR_Y);
6693
6694   /* An operation (on Thumb) where we want to test for a single bit.
6695      This is done by shifting that bit up into the top bit of a
6696      scratch register; we can then branch on the sign bit.  */
6697   if (TARGET_THUMB
6698       && GET_MODE (x) == SImode
6699       && (op == EQ || op == NE)
6700       && (GET_CODE (x) == ZERO_EXTRACT))
6701     return CC_Nmode;
6702
6703   /* An operation that sets the condition codes as a side-effect, the
6704      V flag is not set correctly, so we can only use comparisons where
6705      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6706      instead.)  */
6707   if (GET_MODE (x) == SImode
6708       && y == const0_rtx
6709       && (op == EQ || op == NE || op == LT || op == GE)
6710       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6711           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6712           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6713           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6714           || GET_CODE (x) == LSHIFTRT
6715           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6716           || GET_CODE (x) == ROTATERT
6717           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6718     return CC_NOOVmode;
6719
6720   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6721     return CC_Zmode;
6722
6723   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6724       && GET_CODE (x) == PLUS
6725       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6726     return CC_Cmode;
6727
6728   return CCmode;
6729 }
6730
6731 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6732    return the rtx for register 0 in the proper mode.  FP means this is a
6733    floating point compare: I don't think that it is needed on the arm.  */
6734 rtx
6735 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6736 {
6737   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6738   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6739
6740   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6741                           gen_rtx_COMPARE (mode, x, y)));
6742
6743   return cc_reg;
6744 }
6745
6746 /* Generate a sequence of insns that will generate the correct return
6747    address mask depending on the physical architecture that the program
6748    is running on.  */
6749 rtx
6750 arm_gen_return_addr_mask (void)
6751 {
6752   rtx reg = gen_reg_rtx (Pmode);
6753
6754   emit_insn (gen_return_addr_mask (reg));
6755   return reg;
6756 }
6757
6758 void
6759 arm_reload_in_hi (rtx *operands)
6760 {
6761   rtx ref = operands[1];
6762   rtx base, scratch;
6763   HOST_WIDE_INT offset = 0;
6764
6765   if (GET_CODE (ref) == SUBREG)
6766     {
6767       offset = SUBREG_BYTE (ref);
6768       ref = SUBREG_REG (ref);
6769     }
6770
6771   if (GET_CODE (ref) == REG)
6772     {
6773       /* We have a pseudo which has been spilt onto the stack; there
6774          are two cases here: the first where there is a simple
6775          stack-slot replacement and a second where the stack-slot is
6776          out of range, or is used as a subreg.  */
6777       if (reg_equiv_mem[REGNO (ref)])
6778         {
6779           ref = reg_equiv_mem[REGNO (ref)];
6780           base = find_replacement (&XEXP (ref, 0));
6781         }
6782       else
6783         /* The slot is out of range, or was dressed up in a SUBREG.  */
6784         base = reg_equiv_address[REGNO (ref)];
6785     }
6786   else
6787     base = find_replacement (&XEXP (ref, 0));
6788
6789   /* Handle the case where the address is too complex to be offset by 1.  */
6790   if (GET_CODE (base) == MINUS
6791       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6792     {
6793       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6794
6795       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6796       base = base_plus;
6797     }
6798   else if (GET_CODE (base) == PLUS)
6799     {
6800       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6801       HOST_WIDE_INT hi, lo;
6802
6803       offset += INTVAL (XEXP (base, 1));
6804       base = XEXP (base, 0);
6805
6806       /* Rework the address into a legal sequence of insns.  */
6807       /* Valid range for lo is -4095 -> 4095 */
6808       lo = (offset >= 0
6809             ? (offset & 0xfff)
6810             : -((-offset) & 0xfff));
6811
6812       /* Corner case, if lo is the max offset then we would be out of range
6813          once we have added the additional 1 below, so bump the msb into the
6814          pre-loading insn(s).  */
6815       if (lo == 4095)
6816         lo &= 0x7ff;
6817
6818       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6819              ^ (HOST_WIDE_INT) 0x80000000)
6820             - (HOST_WIDE_INT) 0x80000000);
6821
6822       gcc_assert (hi + lo == offset);
6823
6824       if (hi != 0)
6825         {
6826           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6827
6828           /* Get the base address; addsi3 knows how to handle constants
6829              that require more than one insn.  */
6830           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6831           base = base_plus;
6832           offset = lo;
6833         }
6834     }
6835
6836   /* Operands[2] may overlap operands[0] (though it won't overlap
6837      operands[1]), that's why we asked for a DImode reg -- so we can
6838      use the bit that does not overlap.  */
6839   if (REGNO (operands[2]) == REGNO (operands[0]))
6840     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6841   else
6842     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6843
6844   emit_insn (gen_zero_extendqisi2 (scratch,
6845                                    gen_rtx_MEM (QImode,
6846                                                 plus_constant (base,
6847                                                                offset))));
6848   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6849                                    gen_rtx_MEM (QImode,
6850                                                 plus_constant (base,
6851                                                                offset + 1))));
6852   if (!BYTES_BIG_ENDIAN)
6853     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6854                         gen_rtx_IOR (SImode,
6855                                      gen_rtx_ASHIFT
6856                                      (SImode,
6857                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6858                                       GEN_INT (8)),
6859                                      scratch)));
6860   else
6861     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6862                             gen_rtx_IOR (SImode,
6863                                          gen_rtx_ASHIFT (SImode, scratch,
6864                                                          GEN_INT (8)),
6865                                          gen_rtx_SUBREG (SImode, operands[0],
6866                                                          0))));
6867 }
6868
6869 /* Handle storing a half-word to memory during reload by synthesizing as two
6870    byte stores.  Take care not to clobber the input values until after we
6871    have moved them somewhere safe.  This code assumes that if the DImode
6872    scratch in operands[2] overlaps either the input value or output address
6873    in some way, then that value must die in this insn (we absolutely need
6874    two scratch registers for some corner cases).  */
6875 void
6876 arm_reload_out_hi (rtx *operands)
6877 {
6878   rtx ref = operands[0];
6879   rtx outval = operands[1];
6880   rtx base, scratch;
6881   HOST_WIDE_INT offset = 0;
6882
6883   if (GET_CODE (ref) == SUBREG)
6884     {
6885       offset = SUBREG_BYTE (ref);
6886       ref = SUBREG_REG (ref);
6887     }
6888
6889   if (GET_CODE (ref) == REG)
6890     {
6891       /* We have a pseudo which has been spilt onto the stack; there
6892          are two cases here: the first where there is a simple
6893          stack-slot replacement and a second where the stack-slot is
6894          out of range, or is used as a subreg.  */
6895       if (reg_equiv_mem[REGNO (ref)])
6896         {
6897           ref = reg_equiv_mem[REGNO (ref)];
6898           base = find_replacement (&XEXP (ref, 0));
6899         }
6900       else
6901         /* The slot is out of range, or was dressed up in a SUBREG.  */
6902         base = reg_equiv_address[REGNO (ref)];
6903     }
6904   else
6905     base = find_replacement (&XEXP (ref, 0));
6906
6907   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6908
6909   /* Handle the case where the address is too complex to be offset by 1.  */
6910   if (GET_CODE (base) == MINUS
6911       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6912     {
6913       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6914
6915       /* Be careful not to destroy OUTVAL.  */
6916       if (reg_overlap_mentioned_p (base_plus, outval))
6917         {
6918           /* Updating base_plus might destroy outval, see if we can
6919              swap the scratch and base_plus.  */
6920           if (!reg_overlap_mentioned_p (scratch, outval))
6921             {
6922               rtx tmp = scratch;
6923               scratch = base_plus;
6924               base_plus = tmp;
6925             }
6926           else
6927             {
6928               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6929
6930               /* Be conservative and copy OUTVAL into the scratch now,
6931                  this should only be necessary if outval is a subreg
6932                  of something larger than a word.  */
6933               /* XXX Might this clobber base?  I can't see how it can,
6934                  since scratch is known to overlap with OUTVAL, and
6935                  must be wider than a word.  */
6936               emit_insn (gen_movhi (scratch_hi, outval));
6937               outval = scratch_hi;
6938             }
6939         }
6940
6941       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6942       base = base_plus;
6943     }
6944   else if (GET_CODE (base) == PLUS)
6945     {
6946       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6947       HOST_WIDE_INT hi, lo;
6948
6949       offset += INTVAL (XEXP (base, 1));
6950       base = XEXP (base, 0);
6951
6952       /* Rework the address into a legal sequence of insns.  */
6953       /* Valid range for lo is -4095 -> 4095 */
6954       lo = (offset >= 0
6955             ? (offset & 0xfff)
6956             : -((-offset) & 0xfff));
6957
6958       /* Corner case, if lo is the max offset then we would be out of range
6959          once we have added the additional 1 below, so bump the msb into the
6960          pre-loading insn(s).  */
6961       if (lo == 4095)
6962         lo &= 0x7ff;
6963
6964       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6965              ^ (HOST_WIDE_INT) 0x80000000)
6966             - (HOST_WIDE_INT) 0x80000000);
6967
6968       gcc_assert (hi + lo == offset);
6969
6970       if (hi != 0)
6971         {
6972           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6973
6974           /* Be careful not to destroy OUTVAL.  */
6975           if (reg_overlap_mentioned_p (base_plus, outval))
6976             {
6977               /* Updating base_plus might destroy outval, see if we
6978                  can swap the scratch and base_plus.  */
6979               if (!reg_overlap_mentioned_p (scratch, outval))
6980                 {
6981                   rtx tmp = scratch;
6982                   scratch = base_plus;
6983                   base_plus = tmp;
6984                 }
6985               else
6986                 {
6987                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6988
6989                   /* Be conservative and copy outval into scratch now,
6990                      this should only be necessary if outval is a
6991                      subreg of something larger than a word.  */
6992                   /* XXX Might this clobber base?  I can't see how it
6993                      can, since scratch is known to overlap with
6994                      outval.  */
6995                   emit_insn (gen_movhi (scratch_hi, outval));
6996                   outval = scratch_hi;
6997                 }
6998             }
6999
7000           /* Get the base address; addsi3 knows how to handle constants
7001              that require more than one insn.  */
7002           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7003           base = base_plus;
7004           offset = lo;
7005         }
7006     }
7007
7008   if (BYTES_BIG_ENDIAN)
7009     {
7010       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7011                                          plus_constant (base, offset + 1)),
7012                             gen_lowpart (QImode, outval)));
7013       emit_insn (gen_lshrsi3 (scratch,
7014                               gen_rtx_SUBREG (SImode, outval, 0),
7015                               GEN_INT (8)));
7016       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7017                             gen_lowpart (QImode, scratch)));
7018     }
7019   else
7020     {
7021       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7022                             gen_lowpart (QImode, outval)));
7023       emit_insn (gen_lshrsi3 (scratch,
7024                               gen_rtx_SUBREG (SImode, outval, 0),
7025                               GEN_INT (8)));
7026       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7027                                          plus_constant (base, offset + 1)),
7028                             gen_lowpart (QImode, scratch)));
7029     }
7030 }
7031
7032 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7033    (padded to the size of a word) should be passed in a register.  */
7034
7035 static bool
7036 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7037 {
7038   if (TARGET_AAPCS_BASED)
7039     return must_pass_in_stack_var_size (mode, type);
7040   else
7041     return must_pass_in_stack_var_size_or_pad (mode, type);
7042 }
7043
7044
7045 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7046    Return true if an argument passed on the stack should be padded upwards,
7047    i.e. if the least-significant byte has useful data.
7048    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
7049    aggregate types are placed in the lowest memory address.  */
7050
7051 bool
7052 arm_pad_arg_upward (enum machine_mode mode, tree type)
7053 {
7054   if (!TARGET_AAPCS_BASED)
7055     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7056
7057   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7058     return false;
7059
7060   return true;
7061 }
7062
7063
7064 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7065    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7066    byte of the register has useful data, and return the opposite if the
7067    most significant byte does.
7068    For AAPCS, small aggregates and small complex types are always padded
7069    upwards.  */
7070
7071 bool
7072 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7073                     tree type, int first ATTRIBUTE_UNUSED)
7074 {
7075   if (TARGET_AAPCS_BASED
7076       && BYTES_BIG_ENDIAN
7077       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7078       && int_size_in_bytes (type) <= 4)
7079     return true;
7080
7081   /* Otherwise, use default padding.  */
7082   return !BYTES_BIG_ENDIAN;
7083 }
7084
7085 \f
7086 /* Print a symbolic form of X to the debug file, F.  */
7087 static void
7088 arm_print_value (FILE *f, rtx x)
7089 {
7090   switch (GET_CODE (x))
7091     {
7092     case CONST_INT:
7093       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7094       return;
7095
7096     case CONST_DOUBLE:
7097       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7098       return;
7099
7100     case CONST_VECTOR:
7101       {
7102         int i;
7103
7104         fprintf (f, "<");
7105         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7106           {
7107             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7108             if (i < (CONST_VECTOR_NUNITS (x) - 1))
7109               fputc (',', f);
7110           }
7111         fprintf (f, ">");
7112       }
7113       return;
7114
7115     case CONST_STRING:
7116       fprintf (f, "\"%s\"", XSTR (x, 0));
7117       return;
7118
7119     case SYMBOL_REF:
7120       fprintf (f, "`%s'", XSTR (x, 0));
7121       return;
7122
7123     case LABEL_REF:
7124       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7125       return;
7126
7127     case CONST:
7128       arm_print_value (f, XEXP (x, 0));
7129       return;
7130
7131     case PLUS:
7132       arm_print_value (f, XEXP (x, 0));
7133       fprintf (f, "+");
7134       arm_print_value (f, XEXP (x, 1));
7135       return;
7136
7137     case PC:
7138       fprintf (f, "pc");
7139       return;
7140
7141     default:
7142       fprintf (f, "????");
7143       return;
7144     }
7145 }
7146 \f
7147 /* Routines for manipulation of the constant pool.  */
7148
7149 /* Arm instructions cannot load a large constant directly into a
7150    register; they have to come from a pc relative load.  The constant
7151    must therefore be placed in the addressable range of the pc
7152    relative load.  Depending on the precise pc relative load
7153    instruction the range is somewhere between 256 bytes and 4k.  This
7154    means that we often have to dump a constant inside a function, and
7155    generate code to branch around it.
7156
7157    It is important to minimize this, since the branches will slow
7158    things down and make the code larger.
7159
7160    Normally we can hide the table after an existing unconditional
7161    branch so that there is no interruption of the flow, but in the
7162    worst case the code looks like this:
7163
7164         ldr     rn, L1
7165         ...
7166         b       L2
7167         align
7168         L1:     .long value
7169         L2:
7170         ...
7171
7172         ldr     rn, L3
7173         ...
7174         b       L4
7175         align
7176         L3:     .long value
7177         L4:
7178         ...
7179
7180    We fix this by performing a scan after scheduling, which notices
7181    which instructions need to have their operands fetched from the
7182    constant table and builds the table.
7183
7184    The algorithm starts by building a table of all the constants that
7185    need fixing up and all the natural barriers in the function (places
7186    where a constant table can be dropped without breaking the flow).
7187    For each fixup we note how far the pc-relative replacement will be
7188    able to reach and the offset of the instruction into the function.
7189
7190    Having built the table we then group the fixes together to form
7191    tables that are as large as possible (subject to addressing
7192    constraints) and emit each table of constants after the last
7193    barrier that is within range of all the instructions in the group.
7194    If a group does not contain a barrier, then we forcibly create one
7195    by inserting a jump instruction into the flow.  Once the table has
7196    been inserted, the insns are then modified to reference the
7197    relevant entry in the pool.
7198
7199    Possible enhancements to the algorithm (not implemented) are:
7200
7201    1) For some processors and object formats, there may be benefit in
7202    aligning the pools to the start of cache lines; this alignment
7203    would need to be taken into account when calculating addressability
7204    of a pool.  */
7205
7206 /* These typedefs are located at the start of this file, so that
7207    they can be used in the prototypes there.  This comment is to
7208    remind readers of that fact so that the following structures
7209    can be understood more easily.
7210
7211      typedef struct minipool_node    Mnode;
7212      typedef struct minipool_fixup   Mfix;  */
7213
7214 struct minipool_node
7215 {
7216   /* Doubly linked chain of entries.  */
7217   Mnode * next;
7218   Mnode * prev;
7219   /* The maximum offset into the code that this entry can be placed.  While
7220      pushing fixes for forward references, all entries are sorted in order
7221      of increasing max_address.  */
7222   HOST_WIDE_INT max_address;
7223   /* Similarly for an entry inserted for a backwards ref.  */
7224   HOST_WIDE_INT min_address;
7225   /* The number of fixes referencing this entry.  This can become zero
7226      if we "unpush" an entry.  In this case we ignore the entry when we
7227      come to emit the code.  */
7228   int refcount;
7229   /* The offset from the start of the minipool.  */
7230   HOST_WIDE_INT offset;
7231   /* The value in table.  */
7232   rtx value;
7233   /* The mode of value.  */
7234   enum machine_mode mode;
7235   /* The size of the value.  With iWMMXt enabled
7236      sizes > 4 also imply an alignment of 8-bytes.  */
7237   int fix_size;
7238 };
7239
7240 struct minipool_fixup
7241 {
7242   Mfix *            next;
7243   rtx               insn;
7244   HOST_WIDE_INT     address;
7245   rtx *             loc;
7246   enum machine_mode mode;
7247   int               fix_size;
7248   rtx               value;
7249   Mnode *           minipool;
7250   HOST_WIDE_INT     forwards;
7251   HOST_WIDE_INT     backwards;
7252 };
7253
7254 /* Fixes less than a word need padding out to a word boundary.  */
7255 #define MINIPOOL_FIX_SIZE(mode) \
7256   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7257
7258 static Mnode *  minipool_vector_head;
7259 static Mnode *  minipool_vector_tail;
7260 static rtx      minipool_vector_label;
7261
7262 /* The linked list of all minipool fixes required for this function.  */
7263 Mfix *          minipool_fix_head;
7264 Mfix *          minipool_fix_tail;
7265 /* The fix entry for the current minipool, once it has been placed.  */
7266 Mfix *          minipool_barrier;
7267
7268 /* Determines if INSN is the start of a jump table.  Returns the end
7269    of the TABLE or NULL_RTX.  */
7270 static rtx
7271 is_jump_table (rtx insn)
7272 {
7273   rtx table;
7274
7275   if (GET_CODE (insn) == JUMP_INSN
7276       && JUMP_LABEL (insn) != NULL
7277       && ((table = next_real_insn (JUMP_LABEL (insn)))
7278           == next_real_insn (insn))
7279       && table != NULL
7280       && GET_CODE (table) == JUMP_INSN
7281       && (GET_CODE (PATTERN (table)) == ADDR_VEC
7282           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7283     return table;
7284
7285   return NULL_RTX;
7286 }
7287
7288 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7289 #define JUMP_TABLES_IN_TEXT_SECTION 0
7290 #endif
7291
7292 static HOST_WIDE_INT
7293 get_jump_table_size (rtx insn)
7294 {
7295   /* ADDR_VECs only take room if read-only data does into the text
7296      section.  */
7297   if (JUMP_TABLES_IN_TEXT_SECTION
7298 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
7299       || 1
7300 #endif
7301       )
7302     {
7303       rtx body = PATTERN (insn);
7304       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7305
7306       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
7307     }
7308
7309   return 0;
7310 }
7311
7312 /* Move a minipool fix MP from its current location to before MAX_MP.
7313    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7314    constraints may need updating.  */
7315 static Mnode *
7316 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7317                                HOST_WIDE_INT max_address)
7318 {
7319   /* The code below assumes these are different.  */
7320   gcc_assert (mp != max_mp);
7321
7322   if (max_mp == NULL)
7323     {
7324       if (max_address < mp->max_address)
7325         mp->max_address = max_address;
7326     }
7327   else
7328     {
7329       if (max_address > max_mp->max_address - mp->fix_size)
7330         mp->max_address = max_mp->max_address - mp->fix_size;
7331       else
7332         mp->max_address = max_address;
7333
7334       /* Unlink MP from its current position.  Since max_mp is non-null,
7335        mp->prev must be non-null.  */
7336       mp->prev->next = mp->next;
7337       if (mp->next != NULL)
7338         mp->next->prev = mp->prev;
7339       else
7340         minipool_vector_tail = mp->prev;
7341
7342       /* Re-insert it before MAX_MP.  */
7343       mp->next = max_mp;
7344       mp->prev = max_mp->prev;
7345       max_mp->prev = mp;
7346
7347       if (mp->prev != NULL)
7348         mp->prev->next = mp;
7349       else
7350         minipool_vector_head = mp;
7351     }
7352
7353   /* Save the new entry.  */
7354   max_mp = mp;
7355
7356   /* Scan over the preceding entries and adjust their addresses as
7357      required.  */
7358   while (mp->prev != NULL
7359          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7360     {
7361       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7362       mp = mp->prev;
7363     }
7364
7365   return max_mp;
7366 }
7367
7368 /* Add a constant to the minipool for a forward reference.  Returns the
7369    node added or NULL if the constant will not fit in this pool.  */
7370 static Mnode *
7371 add_minipool_forward_ref (Mfix *fix)
7372 {
7373   /* If set, max_mp is the first pool_entry that has a lower
7374      constraint than the one we are trying to add.  */
7375   Mnode *       max_mp = NULL;
7376   HOST_WIDE_INT max_address = fix->address + fix->forwards;
7377   Mnode *       mp;
7378
7379   /* If this fix's address is greater than the address of the first
7380      entry, then we can't put the fix in this pool.  We subtract the
7381      size of the current fix to ensure that if the table is fully
7382      packed we still have enough room to insert this value by shuffling
7383      the other fixes forwards.  */
7384   if (minipool_vector_head &&
7385       fix->address >= minipool_vector_head->max_address - fix->fix_size)
7386     return NULL;
7387
7388   /* Scan the pool to see if a constant with the same value has
7389      already been added.  While we are doing this, also note the
7390      location where we must insert the constant if it doesn't already
7391      exist.  */
7392   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7393     {
7394       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7395           && fix->mode == mp->mode
7396           && (GET_CODE (fix->value) != CODE_LABEL
7397               || (CODE_LABEL_NUMBER (fix->value)
7398                   == CODE_LABEL_NUMBER (mp->value)))
7399           && rtx_equal_p (fix->value, mp->value))
7400         {
7401           /* More than one fix references this entry.  */
7402           mp->refcount++;
7403           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7404         }
7405
7406       /* Note the insertion point if necessary.  */
7407       if (max_mp == NULL
7408           && mp->max_address > max_address)
7409         max_mp = mp;
7410
7411       /* If we are inserting an 8-bytes aligned quantity and
7412          we have not already found an insertion point, then
7413          make sure that all such 8-byte aligned quantities are
7414          placed at the start of the pool.  */
7415       if (ARM_DOUBLEWORD_ALIGN
7416           && max_mp == NULL
7417           && fix->fix_size == 8
7418           && mp->fix_size != 8)
7419         {
7420           max_mp = mp;
7421           max_address = mp->max_address;
7422         }
7423     }
7424
7425   /* The value is not currently in the minipool, so we need to create
7426      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7427      the end of the list since the placement is less constrained than
7428      any existing entry.  Otherwise, we insert the new fix before
7429      MAX_MP and, if necessary, adjust the constraints on the other
7430      entries.  */
7431   mp = xmalloc (sizeof (* mp));
7432   mp->fix_size = fix->fix_size;
7433   mp->mode = fix->mode;
7434   mp->value = fix->value;
7435   mp->refcount = 1;
7436   /* Not yet required for a backwards ref.  */
7437   mp->min_address = -65536;
7438
7439   if (max_mp == NULL)
7440     {
7441       mp->max_address = max_address;
7442       mp->next = NULL;
7443       mp->prev = minipool_vector_tail;
7444
7445       if (mp->prev == NULL)
7446         {
7447           minipool_vector_head = mp;
7448           minipool_vector_label = gen_label_rtx ();
7449         }
7450       else
7451         mp->prev->next = mp;
7452
7453       minipool_vector_tail = mp;
7454     }
7455   else
7456     {
7457       if (max_address > max_mp->max_address - mp->fix_size)
7458         mp->max_address = max_mp->max_address - mp->fix_size;
7459       else
7460         mp->max_address = max_address;
7461
7462       mp->next = max_mp;
7463       mp->prev = max_mp->prev;
7464       max_mp->prev = mp;
7465       if (mp->prev != NULL)
7466         mp->prev->next = mp;
7467       else
7468         minipool_vector_head = mp;
7469     }
7470
7471   /* Save the new entry.  */
7472   max_mp = mp;
7473
7474   /* Scan over the preceding entries and adjust their addresses as
7475      required.  */
7476   while (mp->prev != NULL
7477          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7478     {
7479       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7480       mp = mp->prev;
7481     }
7482
7483   return max_mp;
7484 }
7485
7486 static Mnode *
7487 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7488                                 HOST_WIDE_INT  min_address)
7489 {
7490   HOST_WIDE_INT offset;
7491
7492   /* The code below assumes these are different.  */
7493   gcc_assert (mp != min_mp);
7494
7495   if (min_mp == NULL)
7496     {
7497       if (min_address > mp->min_address)
7498         mp->min_address = min_address;
7499     }
7500   else
7501     {
7502       /* We will adjust this below if it is too loose.  */
7503       mp->min_address = min_address;
7504
7505       /* Unlink MP from its current position.  Since min_mp is non-null,
7506          mp->next must be non-null.  */
7507       mp->next->prev = mp->prev;
7508       if (mp->prev != NULL)
7509         mp->prev->next = mp->next;
7510       else
7511         minipool_vector_head = mp->next;
7512
7513       /* Reinsert it after MIN_MP.  */
7514       mp->prev = min_mp;
7515       mp->next = min_mp->next;
7516       min_mp->next = mp;
7517       if (mp->next != NULL)
7518         mp->next->prev = mp;
7519       else
7520         minipool_vector_tail = mp;
7521     }
7522
7523   min_mp = mp;
7524
7525   offset = 0;
7526   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7527     {
7528       mp->offset = offset;
7529       if (mp->refcount > 0)
7530         offset += mp->fix_size;
7531
7532       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7533         mp->next->min_address = mp->min_address + mp->fix_size;
7534     }
7535
7536   return min_mp;
7537 }
7538
7539 /* Add a constant to the minipool for a backward reference.  Returns the
7540    node added or NULL if the constant will not fit in this pool.
7541
7542    Note that the code for insertion for a backwards reference can be
7543    somewhat confusing because the calculated offsets for each fix do
7544    not take into account the size of the pool (which is still under
7545    construction.  */
7546 static Mnode *
7547 add_minipool_backward_ref (Mfix *fix)
7548 {
7549   /* If set, min_mp is the last pool_entry that has a lower constraint
7550      than the one we are trying to add.  */
7551   Mnode *min_mp = NULL;
7552   /* This can be negative, since it is only a constraint.  */
7553   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7554   Mnode *mp;
7555
7556   /* If we can't reach the current pool from this insn, or if we can't
7557      insert this entry at the end of the pool without pushing other
7558      fixes out of range, then we don't try.  This ensures that we
7559      can't fail later on.  */
7560   if (min_address >= minipool_barrier->address
7561       || (minipool_vector_tail->min_address + fix->fix_size
7562           >= minipool_barrier->address))
7563     return NULL;
7564
7565   /* Scan the pool to see if a constant with the same value has
7566      already been added.  While we are doing this, also note the
7567      location where we must insert the constant if it doesn't already
7568      exist.  */
7569   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7570     {
7571       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7572           && fix->mode == mp->mode
7573           && (GET_CODE (fix->value) != CODE_LABEL
7574               || (CODE_LABEL_NUMBER (fix->value)
7575                   == CODE_LABEL_NUMBER (mp->value)))
7576           && rtx_equal_p (fix->value, mp->value)
7577           /* Check that there is enough slack to move this entry to the
7578              end of the table (this is conservative).  */
7579           && (mp->max_address
7580               > (minipool_barrier->address
7581                  + minipool_vector_tail->offset
7582                  + minipool_vector_tail->fix_size)))
7583         {
7584           mp->refcount++;
7585           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7586         }
7587
7588       if (min_mp != NULL)
7589         mp->min_address += fix->fix_size;
7590       else
7591         {
7592           /* Note the insertion point if necessary.  */
7593           if (mp->min_address < min_address)
7594             {
7595               /* For now, we do not allow the insertion of 8-byte alignment
7596                  requiring nodes anywhere but at the start of the pool.  */
7597               if (ARM_DOUBLEWORD_ALIGN
7598                   && fix->fix_size == 8 && mp->fix_size != 8)
7599                 return NULL;
7600               else
7601                 min_mp = mp;
7602             }
7603           else if (mp->max_address
7604                    < minipool_barrier->address + mp->offset + fix->fix_size)
7605             {
7606               /* Inserting before this entry would push the fix beyond
7607                  its maximum address (which can happen if we have
7608                  re-located a forwards fix); force the new fix to come
7609                  after it.  */
7610               min_mp = mp;
7611               min_address = mp->min_address + fix->fix_size;
7612             }
7613           /* If we are inserting an 8-bytes aligned quantity and
7614              we have not already found an insertion point, then
7615              make sure that all such 8-byte aligned quantities are
7616              placed at the start of the pool.  */
7617           else if (ARM_DOUBLEWORD_ALIGN
7618                    && min_mp == NULL
7619                    && fix->fix_size == 8
7620                    && mp->fix_size < 8)
7621             {
7622               min_mp = mp;
7623               min_address = mp->min_address + fix->fix_size;
7624             }
7625         }
7626     }
7627
7628   /* We need to create a new entry.  */
7629   mp = xmalloc (sizeof (* mp));
7630   mp->fix_size = fix->fix_size;
7631   mp->mode = fix->mode;
7632   mp->value = fix->value;
7633   mp->refcount = 1;
7634   mp->max_address = minipool_barrier->address + 65536;
7635
7636   mp->min_address = min_address;
7637
7638   if (min_mp == NULL)
7639     {
7640       mp->prev = NULL;
7641       mp->next = minipool_vector_head;
7642
7643       if (mp->next == NULL)
7644         {
7645           minipool_vector_tail = mp;
7646           minipool_vector_label = gen_label_rtx ();
7647         }
7648       else
7649         mp->next->prev = mp;
7650
7651       minipool_vector_head = mp;
7652     }
7653   else
7654     {
7655       mp->next = min_mp->next;
7656       mp->prev = min_mp;
7657       min_mp->next = mp;
7658
7659       if (mp->next != NULL)
7660         mp->next->prev = mp;
7661       else
7662         minipool_vector_tail = mp;
7663     }
7664
7665   /* Save the new entry.  */
7666   min_mp = mp;
7667
7668   if (mp->prev)
7669     mp = mp->prev;
7670   else
7671     mp->offset = 0;
7672
7673   /* Scan over the following entries and adjust their offsets.  */
7674   while (mp->next != NULL)
7675     {
7676       if (mp->next->min_address < mp->min_address + mp->fix_size)
7677         mp->next->min_address = mp->min_address + mp->fix_size;
7678
7679       if (mp->refcount)
7680         mp->next->offset = mp->offset + mp->fix_size;
7681       else
7682         mp->next->offset = mp->offset;
7683
7684       mp = mp->next;
7685     }
7686
7687   return min_mp;
7688 }
7689
7690 static void
7691 assign_minipool_offsets (Mfix *barrier)
7692 {
7693   HOST_WIDE_INT offset = 0;
7694   Mnode *mp;
7695
7696   minipool_barrier = barrier;
7697
7698   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7699     {
7700       mp->offset = offset;
7701
7702       if (mp->refcount > 0)
7703         offset += mp->fix_size;
7704     }
7705 }
7706
7707 /* Output the literal table */
7708 static void
7709 dump_minipool (rtx scan)
7710 {
7711   Mnode * mp;
7712   Mnode * nmp;
7713   int align64 = 0;
7714
7715   if (ARM_DOUBLEWORD_ALIGN)
7716     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7717       if (mp->refcount > 0 && mp->fix_size == 8)
7718         {
7719           align64 = 1;
7720           break;
7721         }
7722
7723   if (dump_file)
7724     fprintf (dump_file,
7725              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7726              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7727
7728   scan = emit_label_after (gen_label_rtx (), scan);
7729   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7730   scan = emit_label_after (minipool_vector_label, scan);
7731
7732   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7733     {
7734       if (mp->refcount > 0)
7735         {
7736           if (dump_file)
7737             {
7738               fprintf (dump_file,
7739                        ";;  Offset %u, min %ld, max %ld ",
7740                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7741                        (unsigned long) mp->max_address);
7742               arm_print_value (dump_file, mp->value);
7743               fputc ('\n', dump_file);
7744             }
7745
7746           switch (mp->fix_size)
7747             {
7748 #ifdef HAVE_consttable_1
7749             case 1:
7750               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7751               break;
7752
7753 #endif
7754 #ifdef HAVE_consttable_2
7755             case 2:
7756               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7757               break;
7758
7759 #endif
7760 #ifdef HAVE_consttable_4
7761             case 4:
7762               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7763               break;
7764
7765 #endif
7766 #ifdef HAVE_consttable_8
7767             case 8:
7768               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7769               break;
7770
7771 #endif
7772             default:
7773               gcc_unreachable ();
7774             }
7775         }
7776
7777       nmp = mp->next;
7778       free (mp);
7779     }
7780
7781   minipool_vector_head = minipool_vector_tail = NULL;
7782   scan = emit_insn_after (gen_consttable_end (), scan);
7783   scan = emit_barrier_after (scan);
7784 }
7785
7786 /* Return the cost of forcibly inserting a barrier after INSN.  */
7787 static int
7788 arm_barrier_cost (rtx insn)
7789 {
7790   /* Basing the location of the pool on the loop depth is preferable,
7791      but at the moment, the basic block information seems to be
7792      corrupt by this stage of the compilation.  */
7793   int base_cost = 50;
7794   rtx next = next_nonnote_insn (insn);
7795
7796   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7797     base_cost -= 20;
7798
7799   switch (GET_CODE (insn))
7800     {
7801     case CODE_LABEL:
7802       /* It will always be better to place the table before the label, rather
7803          than after it.  */
7804       return 50;
7805
7806     case INSN:
7807     case CALL_INSN:
7808       return base_cost;
7809
7810     case JUMP_INSN:
7811       return base_cost - 10;
7812
7813     default:
7814       return base_cost + 10;
7815     }
7816 }
7817
7818 /* Find the best place in the insn stream in the range
7819    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7820    Create the barrier by inserting a jump and add a new fix entry for
7821    it.  */
7822 static Mfix *
7823 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7824 {
7825   HOST_WIDE_INT count = 0;
7826   rtx barrier;
7827   rtx from = fix->insn;
7828   rtx selected = from;
7829   int selected_cost;
7830   HOST_WIDE_INT selected_address;
7831   Mfix * new_fix;
7832   HOST_WIDE_INT max_count = max_address - fix->address;
7833   rtx label = gen_label_rtx ();
7834
7835   selected_cost = arm_barrier_cost (from);
7836   selected_address = fix->address;
7837
7838   while (from && count < max_count)
7839     {
7840       rtx tmp;
7841       int new_cost;
7842
7843       /* This code shouldn't have been called if there was a natural barrier
7844          within range.  */
7845       gcc_assert (GET_CODE (from) != BARRIER);
7846
7847       /* Count the length of this insn.  */
7848       count += get_attr_length (from);
7849
7850       /* If there is a jump table, add its length.  */
7851       tmp = is_jump_table (from);
7852       if (tmp != NULL)
7853         {
7854           count += get_jump_table_size (tmp);
7855
7856           /* Jump tables aren't in a basic block, so base the cost on
7857              the dispatch insn.  If we select this location, we will
7858              still put the pool after the table.  */
7859           new_cost = arm_barrier_cost (from);
7860
7861           if (count < max_count && new_cost <= selected_cost)
7862             {
7863               selected = tmp;
7864               selected_cost = new_cost;
7865               selected_address = fix->address + count;
7866             }
7867
7868           /* Continue after the dispatch table.  */
7869           from = NEXT_INSN (tmp);
7870           continue;
7871         }
7872
7873       new_cost = arm_barrier_cost (from);
7874
7875       if (count < max_count && new_cost <= selected_cost)
7876         {
7877           selected = from;
7878           selected_cost = new_cost;
7879           selected_address = fix->address + count;
7880         }
7881
7882       from = NEXT_INSN (from);
7883     }
7884
7885   /* Create a new JUMP_INSN that branches around a barrier.  */
7886   from = emit_jump_insn_after (gen_jump (label), selected);
7887   JUMP_LABEL (from) = label;
7888   barrier = emit_barrier_after (from);
7889   emit_label_after (label, barrier);
7890
7891   /* Create a minipool barrier entry for the new barrier.  */
7892   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7893   new_fix->insn = barrier;
7894   new_fix->address = selected_address;
7895   new_fix->next = fix->next;
7896   fix->next = new_fix;
7897
7898   return new_fix;
7899 }
7900
7901 /* Record that there is a natural barrier in the insn stream at
7902    ADDRESS.  */
7903 static void
7904 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7905 {
7906   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7907
7908   fix->insn = insn;
7909   fix->address = address;
7910
7911   fix->next = NULL;
7912   if (minipool_fix_head != NULL)
7913     minipool_fix_tail->next = fix;
7914   else
7915     minipool_fix_head = fix;
7916
7917   minipool_fix_tail = fix;
7918 }
7919
7920 /* Record INSN, which will need fixing up to load a value from the
7921    minipool.  ADDRESS is the offset of the insn since the start of the
7922    function; LOC is a pointer to the part of the insn which requires
7923    fixing; VALUE is the constant that must be loaded, which is of type
7924    MODE.  */
7925 static void
7926 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7927                    enum machine_mode mode, rtx value)
7928 {
7929   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7930
7931 #ifdef AOF_ASSEMBLER
7932   /* PIC symbol references need to be converted into offsets into the
7933      based area.  */
7934   /* XXX This shouldn't be done here.  */
7935   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7936     value = aof_pic_entry (value);
7937 #endif /* AOF_ASSEMBLER */
7938
7939   fix->insn = insn;
7940   fix->address = address;
7941   fix->loc = loc;
7942   fix->mode = mode;
7943   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7944   fix->value = value;
7945   fix->forwards = get_attr_pool_range (insn);
7946   fix->backwards = get_attr_neg_pool_range (insn);
7947   fix->minipool = NULL;
7948
7949   /* If an insn doesn't have a range defined for it, then it isn't
7950      expecting to be reworked by this code.  Better to stop now than
7951      to generate duff assembly code.  */
7952   gcc_assert (fix->forwards || fix->backwards);
7953
7954   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7955      So there might be an empty word before the start of the pool.
7956      Hence we reduce the forward range by 4 to allow for this
7957      possibility.  */
7958   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7959     fix->forwards -= 4;
7960
7961   if (dump_file)
7962     {
7963       fprintf (dump_file,
7964                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7965                GET_MODE_NAME (mode),
7966                INSN_UID (insn), (unsigned long) address,
7967                -1 * (long)fix->backwards, (long)fix->forwards);
7968       arm_print_value (dump_file, fix->value);
7969       fprintf (dump_file, "\n");
7970     }
7971
7972   /* Add it to the chain of fixes.  */
7973   fix->next = NULL;
7974
7975   if (minipool_fix_head != NULL)
7976     minipool_fix_tail->next = fix;
7977   else
7978     minipool_fix_head = fix;
7979
7980   minipool_fix_tail = fix;
7981 }
7982
7983 /* Return the cost of synthesizing a 64-bit constant VAL inline.
7984    Returns the number of insns needed, or 99 if we don't know how to
7985    do it.  */
7986 int
7987 arm_const_double_inline_cost (rtx val)
7988 {
7989   rtx lowpart, highpart;
7990   enum machine_mode mode;
7991
7992   mode = GET_MODE (val);
7993
7994   if (mode == VOIDmode)
7995     mode = DImode;
7996
7997   gcc_assert (GET_MODE_SIZE (mode) == 8);
7998
7999   lowpart = gen_lowpart (SImode, val);
8000   highpart = gen_highpart_mode (SImode, mode, val);
8001
8002   gcc_assert (GET_CODE (lowpart) == CONST_INT);
8003   gcc_assert (GET_CODE (highpart) == CONST_INT);
8004
8005   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8006                             NULL_RTX, NULL_RTX, 0, 0)
8007           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8008                               NULL_RTX, NULL_RTX, 0, 0));
8009 }
8010
8011 /* Return true if it is worthwhile to split a 64-bit constant into two
8012    32-bit operations.  This is the case if optimizing for size, or
8013    if we have load delay slots, or if one 32-bit part can be done with
8014    a single data operation.  */
8015 bool
8016 arm_const_double_by_parts (rtx val)
8017 {
8018   enum machine_mode mode = GET_MODE (val);
8019   rtx part;
8020
8021   if (optimize_size || arm_ld_sched)
8022     return true;
8023
8024   if (mode == VOIDmode)
8025     mode = DImode;
8026
8027   part = gen_highpart_mode (SImode, mode, val);
8028
8029   gcc_assert (GET_CODE (part) == CONST_INT);
8030
8031   if (const_ok_for_arm (INTVAL (part))
8032       || const_ok_for_arm (~INTVAL (part)))
8033     return true;
8034
8035   part = gen_lowpart (SImode, val);
8036
8037   gcc_assert (GET_CODE (part) == CONST_INT);
8038
8039   if (const_ok_for_arm (INTVAL (part))
8040       || const_ok_for_arm (~INTVAL (part)))
8041     return true;
8042
8043   return false;
8044 }
8045
8046 /* Scan INSN and note any of its operands that need fixing.
8047    If DO_PUSHES is false we do not actually push any of the fixups
8048    needed.  The function returns TRUE if any fixups were needed/pushed.
8049    This is used by arm_memory_load_p() which needs to know about loads
8050    of constants that will be converted into minipool loads.  */
8051 static bool
8052 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8053 {
8054   bool result = false;
8055   int opno;
8056
8057   extract_insn (insn);
8058
8059   if (!constrain_operands (1))
8060     fatal_insn_not_found (insn);
8061
8062   if (recog_data.n_alternatives == 0)
8063     return false;
8064
8065   /* Fill in recog_op_alt with information about the constraints of
8066      this insn.  */
8067   preprocess_constraints ();
8068
8069   for (opno = 0; opno < recog_data.n_operands; opno++)
8070     {
8071       /* Things we need to fix can only occur in inputs.  */
8072       if (recog_data.operand_type[opno] != OP_IN)
8073         continue;
8074
8075       /* If this alternative is a memory reference, then any mention
8076          of constants in this alternative is really to fool reload
8077          into allowing us to accept one there.  We need to fix them up
8078          now so that we output the right code.  */
8079       if (recog_op_alt[opno][which_alternative].memory_ok)
8080         {
8081           rtx op = recog_data.operand[opno];
8082
8083           if (CONSTANT_P (op))
8084             {
8085               if (do_pushes)
8086                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8087                                    recog_data.operand_mode[opno], op);
8088               result = true;
8089             }
8090           else if (GET_CODE (op) == MEM
8091                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8092                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8093             {
8094               if (do_pushes)
8095                 {
8096                   rtx cop = avoid_constant_pool_reference (op);
8097
8098                   /* Casting the address of something to a mode narrower
8099                      than a word can cause avoid_constant_pool_reference()
8100                      to return the pool reference itself.  That's no good to
8101                      us here.  Lets just hope that we can use the
8102                      constant pool value directly.  */
8103                   if (op == cop)
8104                     cop = get_pool_constant (XEXP (op, 0));
8105
8106                   push_minipool_fix (insn, address,
8107                                      recog_data.operand_loc[opno],
8108                                      recog_data.operand_mode[opno], cop);
8109                 }
8110
8111               result = true;
8112             }
8113         }
8114     }
8115
8116   return result;
8117 }
8118
8119 /* Gcc puts the pool in the wrong place for ARM, since we can only
8120    load addresses a limited distance around the pc.  We do some
8121    special munging to move the constant pool values to the correct
8122    point in the code.  */
8123 static void
8124 arm_reorg (void)
8125 {
8126   rtx insn;
8127   HOST_WIDE_INT address = 0;
8128   Mfix * fix;
8129
8130   minipool_fix_head = minipool_fix_tail = NULL;
8131
8132   /* The first insn must always be a note, or the code below won't
8133      scan it properly.  */
8134   insn = get_insns ();
8135   gcc_assert (GET_CODE (insn) == NOTE);
8136
8137   /* Scan all the insns and record the operands that will need fixing.  */
8138   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8139     {
8140       if (TARGET_CIRRUS_FIX_INVALID_INSNS
8141           && (arm_cirrus_insn_p (insn)
8142               || GET_CODE (insn) == JUMP_INSN
8143               || arm_memory_load_p (insn)))
8144         cirrus_reorg (insn);
8145
8146       if (GET_CODE (insn) == BARRIER)
8147         push_minipool_barrier (insn, address);
8148       else if (INSN_P (insn))
8149         {
8150           rtx table;
8151
8152           note_invalid_constants (insn, address, true);
8153           address += get_attr_length (insn);
8154
8155           /* If the insn is a vector jump, add the size of the table
8156              and skip the table.  */
8157           if ((table = is_jump_table (insn)) != NULL)
8158             {
8159               address += get_jump_table_size (table);
8160               insn = table;
8161             }
8162         }
8163     }
8164
8165   fix = minipool_fix_head;
8166
8167   /* Now scan the fixups and perform the required changes.  */
8168   while (fix)
8169     {
8170       Mfix * ftmp;
8171       Mfix * fdel;
8172       Mfix *  last_added_fix;
8173       Mfix * last_barrier = NULL;
8174       Mfix * this_fix;
8175
8176       /* Skip any further barriers before the next fix.  */
8177       while (fix && GET_CODE (fix->insn) == BARRIER)
8178         fix = fix->next;
8179
8180       /* No more fixes.  */
8181       if (fix == NULL)
8182         break;
8183
8184       last_added_fix = NULL;
8185
8186       for (ftmp = fix; ftmp; ftmp = ftmp->next)
8187         {
8188           if (GET_CODE (ftmp->insn) == BARRIER)
8189             {
8190               if (ftmp->address >= minipool_vector_head->max_address)
8191                 break;
8192
8193               last_barrier = ftmp;
8194             }
8195           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8196             break;
8197
8198           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
8199         }
8200
8201       /* If we found a barrier, drop back to that; any fixes that we
8202          could have reached but come after the barrier will now go in
8203          the next mini-pool.  */
8204       if (last_barrier != NULL)
8205         {
8206           /* Reduce the refcount for those fixes that won't go into this
8207              pool after all.  */
8208           for (fdel = last_barrier->next;
8209                fdel && fdel != ftmp;
8210                fdel = fdel->next)
8211             {
8212               fdel->minipool->refcount--;
8213               fdel->minipool = NULL;
8214             }
8215
8216           ftmp = last_barrier;
8217         }
8218       else
8219         {
8220           /* ftmp is first fix that we can't fit into this pool and
8221              there no natural barriers that we could use.  Insert a
8222              new barrier in the code somewhere between the previous
8223              fix and this one, and arrange to jump around it.  */
8224           HOST_WIDE_INT max_address;
8225
8226           /* The last item on the list of fixes must be a barrier, so
8227              we can never run off the end of the list of fixes without
8228              last_barrier being set.  */
8229           gcc_assert (ftmp);
8230
8231           max_address = minipool_vector_head->max_address;
8232           /* Check that there isn't another fix that is in range that
8233              we couldn't fit into this pool because the pool was
8234              already too large: we need to put the pool before such an
8235              instruction.  */
8236           if (ftmp->address < max_address)
8237             max_address = ftmp->address;
8238
8239           last_barrier = create_fix_barrier (last_added_fix, max_address);
8240         }
8241
8242       assign_minipool_offsets (last_barrier);
8243
8244       while (ftmp)
8245         {
8246           if (GET_CODE (ftmp->insn) != BARRIER
8247               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8248                   == NULL))
8249             break;
8250
8251           ftmp = ftmp->next;
8252         }
8253
8254       /* Scan over the fixes we have identified for this pool, fixing them
8255          up and adding the constants to the pool itself.  */
8256       for (this_fix = fix; this_fix && ftmp != this_fix;
8257            this_fix = this_fix->next)
8258         if (GET_CODE (this_fix->insn) != BARRIER)
8259           {
8260             rtx addr
8261               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8262                                                   minipool_vector_label),
8263                                this_fix->minipool->offset);
8264             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8265           }
8266
8267       dump_minipool (last_barrier->insn);
8268       fix = ftmp;
8269     }
8270
8271   /* From now on we must synthesize any constants that we can't handle
8272      directly.  This can happen if the RTL gets split during final
8273      instruction generation.  */
8274   after_arm_reorg = 1;
8275
8276   /* Free the minipool memory.  */
8277   obstack_free (&minipool_obstack, minipool_startobj);
8278 }
8279 \f
8280 /* Routines to output assembly language.  */
8281
8282 /* If the rtx is the correct value then return the string of the number.
8283    In this way we can ensure that valid double constants are generated even
8284    when cross compiling.  */
8285 const char *
8286 fp_immediate_constant (rtx x)
8287 {
8288   REAL_VALUE_TYPE r;
8289   int i;
8290
8291   if (!fp_consts_inited)
8292     init_fp_table ();
8293
8294   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8295   for (i = 0; i < 8; i++)
8296     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8297       return strings_fp[i];
8298
8299   gcc_unreachable ();
8300 }
8301
8302 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
8303 static const char *
8304 fp_const_from_val (REAL_VALUE_TYPE *r)
8305 {
8306   int i;
8307
8308   if (!fp_consts_inited)
8309     init_fp_table ();
8310
8311   for (i = 0; i < 8; i++)
8312     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8313       return strings_fp[i];
8314
8315   gcc_unreachable ();
8316 }
8317
8318 /* Output the operands of a LDM/STM instruction to STREAM.
8319    MASK is the ARM register set mask of which only bits 0-15 are important.
8320    REG is the base register, either the frame pointer or the stack pointer,
8321    INSTR is the possibly suffixed load or store instruction.  */
8322
8323 static void
8324 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8325                  unsigned long mask)
8326 {
8327   unsigned i;
8328   bool not_first = FALSE;
8329
8330   fputc ('\t', stream);
8331   asm_fprintf (stream, instr, reg);
8332   fputs (", {", stream);
8333
8334   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8335     if (mask & (1 << i))
8336       {
8337         if (not_first)
8338           fprintf (stream, ", ");
8339
8340         asm_fprintf (stream, "%r", i);
8341         not_first = TRUE;
8342       }
8343
8344   fprintf (stream, "}\n");
8345 }
8346
8347
8348 /* Output a FLDMX instruction to STREAM.
8349    BASE if the register containing the address.
8350    REG and COUNT specify the register range.
8351    Extra registers may be added to avoid hardware bugs.  */
8352
8353 static void
8354 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
8355 {
8356   int i;
8357
8358   /* Workaround ARM10 VFPr1 bug.  */
8359   if (count == 2 && !arm_arch6)
8360     {
8361       if (reg == 15)
8362         reg--;
8363       count++;
8364     }
8365
8366   fputc ('\t', stream);
8367   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
8368
8369   for (i = reg; i < reg + count; i++)
8370     {
8371       if (i > reg)
8372         fputs (", ", stream);
8373       asm_fprintf (stream, "d%d", i);
8374     }
8375   fputs ("}\n", stream);
8376
8377 }
8378
8379
8380 /* Output the assembly for a store multiple.  */
8381
8382 const char *
8383 vfp_output_fstmx (rtx * operands)
8384 {
8385   char pattern[100];
8386   int p;
8387   int base;
8388   int i;
8389
8390   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
8391   p = strlen (pattern);
8392
8393   gcc_assert (GET_CODE (operands[1]) == REG);
8394
8395   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8396   for (i = 1; i < XVECLEN (operands[2], 0); i++)
8397     {
8398       p += sprintf (&pattern[p], ", d%d", base + i);
8399     }
8400   strcpy (&pattern[p], "}");
8401
8402   output_asm_insn (pattern, operands);
8403   return "";
8404 }
8405
8406
8407 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
8408    number of bytes pushed.  */
8409
8410 static int
8411 vfp_emit_fstmx (int base_reg, int count)
8412 {
8413   rtx par;
8414   rtx dwarf;
8415   rtx tmp, reg;
8416   int i;
8417
8418   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8419      register pairs are stored by a store multiple insn.  We avoid this
8420      by pushing an extra pair.  */
8421   if (count == 2 && !arm_arch6)
8422     {
8423       if (base_reg == LAST_VFP_REGNUM - 3)
8424         base_reg -= 2;
8425       count++;
8426     }
8427
8428   /* ??? The frame layout is implementation defined.  We describe
8429      standard format 1 (equivalent to a FSTMD insn and unused pad word).
8430      We really need some way of representing the whole block so that the
8431      unwinder can figure it out at runtime.  */
8432   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8433   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8434
8435   reg = gen_rtx_REG (DFmode, base_reg);
8436   base_reg += 2;
8437
8438   XVECEXP (par, 0, 0)
8439     = gen_rtx_SET (VOIDmode,
8440                    gen_frame_mem (BLKmode,
8441                                   gen_rtx_PRE_DEC (BLKmode,
8442                                                    stack_pointer_rtx)),
8443                    gen_rtx_UNSPEC (BLKmode,
8444                                    gen_rtvec (1, reg),
8445                                    UNSPEC_PUSH_MULT));
8446
8447   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8448                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
8449                                    GEN_INT (-(count * 8 + 4))));
8450   RTX_FRAME_RELATED_P (tmp) = 1;
8451   XVECEXP (dwarf, 0, 0) = tmp;
8452
8453   tmp = gen_rtx_SET (VOIDmode,
8454                      gen_frame_mem (DFmode, stack_pointer_rtx),
8455                      reg);
8456   RTX_FRAME_RELATED_P (tmp) = 1;
8457   XVECEXP (dwarf, 0, 1) = tmp;
8458
8459   for (i = 1; i < count; i++)
8460     {
8461       reg = gen_rtx_REG (DFmode, base_reg);
8462       base_reg += 2;
8463       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8464
8465       tmp = gen_rtx_SET (VOIDmode,
8466                          gen_frame_mem (DFmode,
8467                                         gen_rtx_PLUS (SImode,
8468                                                       stack_pointer_rtx,
8469                                                       GEN_INT (i * 8))),
8470                          reg);
8471       RTX_FRAME_RELATED_P (tmp) = 1;
8472       XVECEXP (dwarf, 0, i + 1) = tmp;
8473     }
8474
8475   par = emit_insn (par);
8476   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8477                                        REG_NOTES (par));
8478   RTX_FRAME_RELATED_P (par) = 1;
8479
8480   return count * 8 + 4;
8481 }
8482
8483
8484 /* Output a 'call' insn.  */
8485 const char *
8486 output_call (rtx *operands)
8487 {
8488   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8489
8490   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8491   if (REGNO (operands[0]) == LR_REGNUM)
8492     {
8493       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8494       output_asm_insn ("mov%?\t%0, %|lr", operands);
8495     }
8496
8497   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8498
8499   if (TARGET_INTERWORK || arm_arch4t)
8500     output_asm_insn ("bx%?\t%0", operands);
8501   else
8502     output_asm_insn ("mov%?\t%|pc, %0", operands);
8503
8504   return "";
8505 }
8506
8507 /* Output a 'call' insn that is a reference in memory.  */
8508 const char *
8509 output_call_mem (rtx *operands)
8510 {
8511   if (TARGET_INTERWORK && !arm_arch5)
8512     {
8513       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8514       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8515       output_asm_insn ("bx%?\t%|ip", operands);
8516     }
8517   else if (regno_use_in (LR_REGNUM, operands[0]))
8518     {
8519       /* LR is used in the memory address.  We load the address in the
8520          first instruction.  It's safe to use IP as the target of the
8521          load since the call will kill it anyway.  */
8522       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8523       if (arm_arch5)
8524         output_asm_insn ("blx%?\t%|ip", operands);
8525       else
8526         {
8527           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8528           if (arm_arch4t)
8529             output_asm_insn ("bx%?\t%|ip", operands);
8530           else
8531             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8532         }
8533     }
8534   else
8535     {
8536       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8537       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8538     }
8539
8540   return "";
8541 }
8542
8543
8544 /* Output a move from arm registers to an fpa registers.
8545    OPERANDS[0] is an fpa register.
8546    OPERANDS[1] is the first registers of an arm register pair.  */
8547 const char *
8548 output_mov_long_double_fpa_from_arm (rtx *operands)
8549 {
8550   int arm_reg0 = REGNO (operands[1]);
8551   rtx ops[3];
8552
8553   gcc_assert (arm_reg0 != IP_REGNUM);
8554
8555   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8556   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8557   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8558
8559   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8560   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8561
8562   return "";
8563 }
8564
8565 /* Output a move from an fpa register to arm registers.
8566    OPERANDS[0] is the first registers of an arm register pair.
8567    OPERANDS[1] is an fpa register.  */
8568 const char *
8569 output_mov_long_double_arm_from_fpa (rtx *operands)
8570 {
8571   int arm_reg0 = REGNO (operands[0]);
8572   rtx ops[3];
8573
8574   gcc_assert (arm_reg0 != IP_REGNUM);
8575
8576   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8577   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8578   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8579
8580   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8581   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8582   return "";
8583 }
8584
8585 /* Output a move from arm registers to arm registers of a long double
8586    OPERANDS[0] is the destination.
8587    OPERANDS[1] is the source.  */
8588 const char *
8589 output_mov_long_double_arm_from_arm (rtx *operands)
8590 {
8591   /* We have to be careful here because the two might overlap.  */
8592   int dest_start = REGNO (operands[0]);
8593   int src_start = REGNO (operands[1]);
8594   rtx ops[2];
8595   int i;
8596
8597   if (dest_start < src_start)
8598     {
8599       for (i = 0; i < 3; i++)
8600         {
8601           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8602           ops[1] = gen_rtx_REG (SImode, src_start + i);
8603           output_asm_insn ("mov%?\t%0, %1", ops);
8604         }
8605     }
8606   else
8607     {
8608       for (i = 2; i >= 0; i--)
8609         {
8610           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8611           ops[1] = gen_rtx_REG (SImode, src_start + i);
8612           output_asm_insn ("mov%?\t%0, %1", ops);
8613         }
8614     }
8615
8616   return "";
8617 }
8618
8619
8620 /* Output a move from arm registers to an fpa registers.
8621    OPERANDS[0] is an fpa register.
8622    OPERANDS[1] is the first registers of an arm register pair.  */
8623 const char *
8624 output_mov_double_fpa_from_arm (rtx *operands)
8625 {
8626   int arm_reg0 = REGNO (operands[1]);
8627   rtx ops[2];
8628
8629   gcc_assert (arm_reg0 != IP_REGNUM);
8630
8631   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8632   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8633   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8634   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8635   return "";
8636 }
8637
8638 /* Output a move from an fpa register to arm registers.
8639    OPERANDS[0] is the first registers of an arm register pair.
8640    OPERANDS[1] is an fpa register.  */
8641 const char *
8642 output_mov_double_arm_from_fpa (rtx *operands)
8643 {
8644   int arm_reg0 = REGNO (operands[0]);
8645   rtx ops[2];
8646
8647   gcc_assert (arm_reg0 != IP_REGNUM);
8648
8649   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8650   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8651   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8652   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8653   return "";
8654 }
8655
8656 /* Output a move between double words.
8657    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8658    or MEM<-REG and all MEMs must be offsettable addresses.  */
8659 const char *
8660 output_move_double (rtx *operands)
8661 {
8662   enum rtx_code code0 = GET_CODE (operands[0]);
8663   enum rtx_code code1 = GET_CODE (operands[1]);
8664   rtx otherops[3];
8665
8666   if (code0 == REG)
8667     {
8668       int reg0 = REGNO (operands[0]);
8669
8670       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8671
8672       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
8673
8674       switch (GET_CODE (XEXP (operands[1], 0)))
8675         {
8676         case REG:
8677           output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8678           break;
8679
8680         case PRE_INC:
8681           gcc_assert (TARGET_LDRD);
8682           output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8683           break;
8684
8685         case PRE_DEC:
8686           output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8687           break;
8688
8689         case POST_INC:
8690           output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8691           break;
8692
8693         case POST_DEC:
8694           gcc_assert (TARGET_LDRD);
8695           output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8696           break;
8697
8698         case PRE_MODIFY:
8699         case POST_MODIFY:
8700           otherops[0] = operands[0];
8701           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8702           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8703
8704           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8705             {
8706               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8707                 {
8708                   /* Registers overlap so split out the increment.  */
8709                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
8710                   output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8711                 }
8712               else
8713                 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8714             }
8715           else
8716             {
8717               /* We only allow constant increments, so this is safe.  */
8718               output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8719             }
8720           break;
8721
8722         case LABEL_REF:
8723         case CONST:
8724           output_asm_insn ("adr%?\t%0, %1", operands);
8725           output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8726           break;
8727
8728         default:
8729           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8730                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8731             {
8732               otherops[0] = operands[0];
8733               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8734               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8735
8736               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8737                 {
8738                   if (GET_CODE (otherops[2]) == CONST_INT)
8739                     {
8740                       switch ((int) INTVAL (otherops[2]))
8741                         {
8742                         case -8:
8743                           output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8744                           return "";
8745                         case -4:
8746                           output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8747                           return "";
8748                         case 4:
8749                           output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8750                           return "";
8751                         }
8752                     }
8753                   if (TARGET_LDRD
8754                       && (GET_CODE (otherops[2]) == REG
8755                           || (GET_CODE (otherops[2]) == CONST_INT
8756                               && INTVAL (otherops[2]) > -256
8757                               && INTVAL (otherops[2]) < 256)))
8758                     {
8759                       if (reg_overlap_mentioned_p (otherops[0],
8760                                                    otherops[2]))
8761                         {
8762                           /* Swap base and index registers over to
8763                              avoid a conflict.  */
8764                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8765                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8766                         }
8767                       /* If both registers conflict, it will usually
8768                          have been fixed by a splitter.  */
8769                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8770                         {
8771                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
8772                           output_asm_insn ("ldr%?d\t%0, [%1]",
8773                                            otherops);
8774                         }
8775                       else
8776                         output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8777                       return "";
8778                     }
8779
8780                   if (GET_CODE (otherops[2]) == CONST_INT)
8781                     {
8782                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8783                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8784                       else
8785                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8786                     }
8787                   else
8788                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
8789                 }
8790               else
8791                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8792
8793               return "ldm%?ia\t%0, %M0";
8794             }
8795           else
8796             {
8797               otherops[1] = adjust_address (operands[1], SImode, 4);
8798               /* Take care of overlapping base/data reg.  */
8799               if (reg_mentioned_p (operands[0], operands[1]))
8800                 {
8801                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8802                   output_asm_insn ("ldr%?\t%0, %1", operands);
8803                 }
8804               else
8805                 {
8806                   output_asm_insn ("ldr%?\t%0, %1", operands);
8807                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8808                 }
8809             }
8810         }
8811     }
8812   else
8813     {
8814       /* Constraints should ensure this.  */
8815       gcc_assert (code0 == MEM && code1 == REG);
8816       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8817
8818       switch (GET_CODE (XEXP (operands[0], 0)))
8819         {
8820         case REG:
8821           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8822           break;
8823
8824         case PRE_INC:
8825           gcc_assert (TARGET_LDRD);
8826           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8827           break;
8828
8829         case PRE_DEC:
8830           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8831           break;
8832
8833         case POST_INC:
8834           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8835           break;
8836
8837         case POST_DEC:
8838           gcc_assert (TARGET_LDRD);
8839           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8840           break;
8841
8842         case PRE_MODIFY:
8843         case POST_MODIFY:
8844           otherops[0] = operands[1];
8845           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8846           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8847
8848           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8849             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8850           else
8851             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8852           break;
8853
8854         case PLUS:
8855           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8856           if (GET_CODE (otherops[2]) == CONST_INT)
8857             {
8858               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8859                 {
8860                 case -8:
8861                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8862                   return "";
8863
8864                 case -4:
8865                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8866                   return "";
8867
8868                 case 4:
8869                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8870                   return "";
8871                 }
8872             }
8873           if (TARGET_LDRD
8874               && (GET_CODE (otherops[2]) == REG
8875                   || (GET_CODE (otherops[2]) == CONST_INT
8876                       && INTVAL (otherops[2]) > -256
8877                       && INTVAL (otherops[2]) < 256)))
8878             {
8879               otherops[0] = operands[1];
8880               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8881               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8882               return "";
8883             }
8884           /* Fall through */
8885
8886         default:
8887           otherops[0] = adjust_address (operands[0], SImode, 4);
8888           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8889           output_asm_insn ("str%?\t%1, %0", operands);
8890           output_asm_insn ("str%?\t%1, %0", otherops);
8891         }
8892     }
8893
8894   return "";
8895 }
8896
8897 /* Output an ADD r, s, #n where n may be too big for one instruction.
8898    If adding zero to one register, output nothing.  */
8899 const char *
8900 output_add_immediate (rtx *operands)
8901 {
8902   HOST_WIDE_INT n = INTVAL (operands[2]);
8903
8904   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8905     {
8906       if (n < 0)
8907         output_multi_immediate (operands,
8908                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8909                                 -n);
8910       else
8911         output_multi_immediate (operands,
8912                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8913                                 n);
8914     }
8915
8916   return "";
8917 }
8918
8919 /* Output a multiple immediate operation.
8920    OPERANDS is the vector of operands referred to in the output patterns.
8921    INSTR1 is the output pattern to use for the first constant.
8922    INSTR2 is the output pattern to use for subsequent constants.
8923    IMMED_OP is the index of the constant slot in OPERANDS.
8924    N is the constant value.  */
8925 static const char *
8926 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8927                         int immed_op, HOST_WIDE_INT n)
8928 {
8929 #if HOST_BITS_PER_WIDE_INT > 32
8930   n &= 0xffffffff;
8931 #endif
8932
8933   if (n == 0)
8934     {
8935       /* Quick and easy output.  */
8936       operands[immed_op] = const0_rtx;
8937       output_asm_insn (instr1, operands);
8938     }
8939   else
8940     {
8941       int i;
8942       const char * instr = instr1;
8943
8944       /* Note that n is never zero here (which would give no output).  */
8945       for (i = 0; i < 32; i += 2)
8946         {
8947           if (n & (3 << i))
8948             {
8949               operands[immed_op] = GEN_INT (n & (255 << i));
8950               output_asm_insn (instr, operands);
8951               instr = instr2;
8952               i += 6;
8953             }
8954         }
8955     }
8956
8957   return "";
8958 }
8959
8960 /* Return the appropriate ARM instruction for the operation code.
8961    The returned result should not be overwritten.  OP is the rtx of the
8962    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8963    was shifted.  */
8964 const char *
8965 arithmetic_instr (rtx op, int shift_first_arg)
8966 {
8967   switch (GET_CODE (op))
8968     {
8969     case PLUS:
8970       return "add";
8971
8972     case MINUS:
8973       return shift_first_arg ? "rsb" : "sub";
8974
8975     case IOR:
8976       return "orr";
8977
8978     case XOR:
8979       return "eor";
8980
8981     case AND:
8982       return "and";
8983
8984     default:
8985       gcc_unreachable ();
8986     }
8987 }
8988
8989 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8990    for the operation code.  The returned result should not be overwritten.
8991    OP is the rtx code of the shift.
8992    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8993    shift.  */
8994 static const char *
8995 shift_op (rtx op, HOST_WIDE_INT *amountp)
8996 {
8997   const char * mnem;
8998   enum rtx_code code = GET_CODE (op);
8999
9000   switch (GET_CODE (XEXP (op, 1)))
9001     {
9002     case REG:
9003     case SUBREG:
9004       *amountp = -1;
9005       break;
9006
9007     case CONST_INT:
9008       *amountp = INTVAL (XEXP (op, 1));
9009       break;
9010
9011     default:
9012       gcc_unreachable ();
9013     }
9014
9015   switch (code)
9016     {
9017     case ASHIFT:
9018       mnem = "asl";
9019       break;
9020
9021     case ASHIFTRT:
9022       mnem = "asr";
9023       break;
9024
9025     case LSHIFTRT:
9026       mnem = "lsr";
9027       break;
9028
9029     case ROTATE:
9030       gcc_assert (*amountp != -1);
9031       *amountp = 32 - *amountp;
9032
9033       /* Fall through.  */
9034
9035     case ROTATERT:
9036       mnem = "ror";
9037       break;
9038
9039     case MULT:
9040       /* We never have to worry about the amount being other than a
9041          power of 2, since this case can never be reloaded from a reg.  */
9042       gcc_assert (*amountp != -1);
9043       *amountp = int_log2 (*amountp);
9044       return "asl";
9045
9046     default:
9047       gcc_unreachable ();
9048     }
9049
9050   if (*amountp != -1)
9051     {
9052       /* This is not 100% correct, but follows from the desire to merge
9053          multiplication by a power of 2 with the recognizer for a
9054          shift.  >=32 is not a valid shift for "asl", so we must try and
9055          output a shift that produces the correct arithmetical result.
9056          Using lsr #32 is identical except for the fact that the carry bit
9057          is not set correctly if we set the flags; but we never use the
9058          carry bit from such an operation, so we can ignore that.  */
9059       if (code == ROTATERT)
9060         /* Rotate is just modulo 32.  */
9061         *amountp &= 31;
9062       else if (*amountp != (*amountp & 31))
9063         {
9064           if (code == ASHIFT)
9065             mnem = "lsr";
9066           *amountp = 32;
9067         }
9068
9069       /* Shifts of 0 are no-ops.  */
9070       if (*amountp == 0)
9071         return NULL;
9072     }
9073
9074   return mnem;
9075 }
9076
9077 /* Obtain the shift from the POWER of two.  */
9078
9079 static HOST_WIDE_INT
9080 int_log2 (HOST_WIDE_INT power)
9081 {
9082   HOST_WIDE_INT shift = 0;
9083
9084   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9085     {
9086       gcc_assert (shift <= 31);
9087       shift++;
9088     }
9089
9090   return shift;
9091 }
9092
9093 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
9094    because /bin/as is horribly restrictive.  The judgement about
9095    whether or not each character is 'printable' (and can be output as
9096    is) or not (and must be printed with an octal escape) must be made
9097    with reference to the *host* character set -- the situation is
9098    similar to that discussed in the comments above pp_c_char in
9099    c-pretty-print.c.  */
9100
9101 #define MAX_ASCII_LEN 51
9102
9103 void
9104 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9105 {
9106   int i;
9107   int len_so_far = 0;
9108
9109   fputs ("\t.ascii\t\"", stream);
9110
9111   for (i = 0; i < len; i++)
9112     {
9113       int c = p[i];
9114
9115       if (len_so_far >= MAX_ASCII_LEN)
9116         {
9117           fputs ("\"\n\t.ascii\t\"", stream);
9118           len_so_far = 0;
9119         }
9120
9121       if (ISPRINT (c))
9122         {
9123           if (c == '\\' || c == '\"')
9124             {
9125               putc ('\\', stream);
9126               len_so_far++;
9127             }
9128           putc (c, stream);
9129           len_so_far++;
9130         }
9131       else
9132         {
9133           fprintf (stream, "\\%03o", c);
9134           len_so_far += 4;
9135         }
9136     }
9137
9138   fputs ("\"\n", stream);
9139 }
9140 \f
9141 /* Compute the register save mask for registers 0 through 12
9142    inclusive.  This code is used by arm_compute_save_reg_mask.  */
9143
9144 static unsigned long
9145 arm_compute_save_reg0_reg12_mask (void)
9146 {
9147   unsigned long func_type = arm_current_func_type ();
9148   unsigned long save_reg_mask = 0;
9149   unsigned int reg;
9150
9151   if (IS_INTERRUPT (func_type))
9152     {
9153       unsigned int max_reg;
9154       /* Interrupt functions must not corrupt any registers,
9155          even call clobbered ones.  If this is a leaf function
9156          we can just examine the registers used by the RTL, but
9157          otherwise we have to assume that whatever function is
9158          called might clobber anything, and so we have to save
9159          all the call-clobbered registers as well.  */
9160       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9161         /* FIQ handlers have registers r8 - r12 banked, so
9162            we only need to check r0 - r7, Normal ISRs only
9163            bank r14 and r15, so we must check up to r12.
9164            r13 is the stack pointer which is always preserved,
9165            so we do not need to consider it here.  */
9166         max_reg = 7;
9167       else
9168         max_reg = 12;
9169
9170       for (reg = 0; reg <= max_reg; reg++)
9171         if (regs_ever_live[reg]
9172             || (! current_function_is_leaf && call_used_regs [reg]))
9173           save_reg_mask |= (1 << reg);
9174
9175       /* Also save the pic base register if necessary.  */
9176       if (flag_pic
9177           && !TARGET_SINGLE_PIC_BASE
9178           && current_function_uses_pic_offset_table)
9179         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9180     }
9181   else
9182     {
9183       /* In the normal case we only need to save those registers
9184          which are call saved and which are used by this function.  */
9185       for (reg = 0; reg <= 10; reg++)
9186         if (regs_ever_live[reg] && ! call_used_regs [reg])
9187           save_reg_mask |= (1 << reg);
9188
9189       /* Handle the frame pointer as a special case.  */
9190       if (! TARGET_APCS_FRAME
9191           && ! frame_pointer_needed
9192           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9193           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9194         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9195
9196       /* If we aren't loading the PIC register,
9197          don't stack it even though it may be live.  */
9198       if (flag_pic
9199           && !TARGET_SINGLE_PIC_BASE
9200           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9201               || current_function_uses_pic_offset_table))
9202         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9203     }
9204
9205   /* Save registers so the exception handler can modify them.  */
9206   if (current_function_calls_eh_return)
9207     {
9208       unsigned int i;
9209
9210       for (i = 0; ; i++)
9211         {
9212           reg = EH_RETURN_DATA_REGNO (i);
9213           if (reg == INVALID_REGNUM)
9214             break;
9215           save_reg_mask |= 1 << reg;
9216         }
9217     }
9218
9219   return save_reg_mask;
9220 }
9221
9222 /* Compute a bit mask of which registers need to be
9223    saved on the stack for the current function.  */
9224
9225 static unsigned long
9226 arm_compute_save_reg_mask (void)
9227 {
9228   unsigned int save_reg_mask = 0;
9229   unsigned long func_type = arm_current_func_type ();
9230
9231   if (IS_NAKED (func_type))
9232     /* This should never really happen.  */
9233     return 0;
9234
9235   /* If we are creating a stack frame, then we must save the frame pointer,
9236      IP (which will hold the old stack pointer), LR and the PC.  */
9237   if (frame_pointer_needed)
9238     save_reg_mask |=
9239       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9240       | (1 << IP_REGNUM)
9241       | (1 << LR_REGNUM)
9242       | (1 << PC_REGNUM);
9243
9244   /* Volatile functions do not return, so there
9245      is no need to save any other registers.  */
9246   if (IS_VOLATILE (func_type))
9247     return save_reg_mask;
9248
9249   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9250
9251   /* Decide if we need to save the link register.
9252      Interrupt routines have their own banked link register,
9253      so they never need to save it.
9254      Otherwise if we do not use the link register we do not need to save
9255      it.  If we are pushing other registers onto the stack however, we
9256      can save an instruction in the epilogue by pushing the link register
9257      now and then popping it back into the PC.  This incurs extra memory
9258      accesses though, so we only do it when optimizing for size, and only
9259      if we know that we will not need a fancy return sequence.  */
9260   if (regs_ever_live [LR_REGNUM]
9261           || (save_reg_mask
9262               && optimize_size
9263               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9264               && !current_function_calls_eh_return))
9265     save_reg_mask |= 1 << LR_REGNUM;
9266
9267   if (cfun->machine->lr_save_eliminated)
9268     save_reg_mask &= ~ (1 << LR_REGNUM);
9269
9270   if (TARGET_REALLY_IWMMXT
9271       && ((bit_count (save_reg_mask)
9272            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9273     {
9274       unsigned int reg;
9275
9276       /* The total number of registers that are going to be pushed
9277          onto the stack is odd.  We need to ensure that the stack
9278          is 64-bit aligned before we start to save iWMMXt registers,
9279          and also before we start to create locals.  (A local variable
9280          might be a double or long long which we will load/store using
9281          an iWMMXt instruction).  Therefore we need to push another
9282          ARM register, so that the stack will be 64-bit aligned.  We
9283          try to avoid using the arg registers (r0 -r3) as they might be
9284          used to pass values in a tail call.  */
9285       for (reg = 4; reg <= 12; reg++)
9286         if ((save_reg_mask & (1 << reg)) == 0)
9287           break;
9288
9289       if (reg <= 12)
9290         save_reg_mask |= (1 << reg);
9291       else
9292         {
9293           cfun->machine->sibcall_blocked = 1;
9294           save_reg_mask |= (1 << 3);
9295         }
9296     }
9297
9298   return save_reg_mask;
9299 }
9300
9301
9302 /* Compute a bit mask of which registers need to be
9303    saved on the stack for the current function.  */
9304 static unsigned long
9305 thumb_compute_save_reg_mask (void)
9306 {
9307   unsigned long mask;
9308   unsigned reg;
9309
9310   mask = 0;
9311   for (reg = 0; reg < 12; reg ++)
9312     if (regs_ever_live[reg] && !call_used_regs[reg])
9313       mask |= 1 << reg;
9314
9315   if (flag_pic
9316       && !TARGET_SINGLE_PIC_BASE
9317       && current_function_uses_pic_offset_table)
9318     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9319
9320   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
9321   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9322     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9323
9324   /* LR will also be pushed if any lo regs are pushed.  */
9325   if (mask & 0xff || thumb_force_lr_save ())
9326     mask |= (1 << LR_REGNUM);
9327
9328   /* Make sure we have a low work register if we need one.
9329      We will need one if we are going to push a high register,
9330      but we are not currently intending to push a low register.  */
9331   if ((mask & 0xff) == 0
9332       && ((mask & 0x0f00) || TARGET_BACKTRACE))
9333     {
9334       /* Use thumb_find_work_register to choose which register
9335          we will use.  If the register is live then we will
9336          have to push it.  Use LAST_LO_REGNUM as our fallback
9337          choice for the register to select.  */
9338       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9339
9340       if (! call_used_regs[reg])
9341         mask |= 1 << reg;
9342     }
9343
9344   return mask;
9345 }
9346
9347
9348 /* Return the number of bytes required to save VFP registers.  */
9349 static int
9350 arm_get_vfp_saved_size (void)
9351 {
9352   unsigned int regno;
9353   int count;
9354   int saved;
9355
9356   saved = 0;
9357   /* Space for saved VFP registers.  */
9358   if (TARGET_HARD_FLOAT && TARGET_VFP)
9359     {
9360       count = 0;
9361       for (regno = FIRST_VFP_REGNUM;
9362            regno < LAST_VFP_REGNUM;
9363            regno += 2)
9364         {
9365           if ((!regs_ever_live[regno] || call_used_regs[regno])
9366               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9367             {
9368               if (count > 0)
9369                 {
9370                   /* Workaround ARM10 VFPr1 bug.  */
9371                   if (count == 2 && !arm_arch6)
9372                     count++;
9373                   saved += count * 8 + 4;
9374                 }
9375               count = 0;
9376             }
9377           else
9378             count++;
9379         }
9380       if (count > 0)
9381         {
9382           if (count == 2 && !arm_arch6)
9383             count++;
9384           saved += count * 8 + 4;
9385         }
9386     }
9387   return saved;
9388 }
9389
9390
9391 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9392    everything bar the final return instruction.  */
9393 const char *
9394 output_return_instruction (rtx operand, int really_return, int reverse)
9395 {
9396   char conditional[10];
9397   char instr[100];
9398   unsigned reg;
9399   unsigned long live_regs_mask;
9400   unsigned long func_type;
9401   arm_stack_offsets *offsets;
9402
9403   func_type = arm_current_func_type ();
9404
9405   if (IS_NAKED (func_type))
9406     return "";
9407
9408   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9409     {
9410       /* If this function was declared non-returning, and we have
9411          found a tail call, then we have to trust that the called
9412          function won't return.  */
9413       if (really_return)
9414         {
9415           rtx ops[2];
9416
9417           /* Otherwise, trap an attempted return by aborting.  */
9418           ops[0] = operand;
9419           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9420                                        : "abort");
9421           assemble_external_libcall (ops[1]);
9422           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9423         }
9424
9425       return "";
9426     }
9427
9428   gcc_assert (!current_function_calls_alloca || really_return);
9429
9430   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9431
9432   return_used_this_function = 1;
9433
9434   live_regs_mask = arm_compute_save_reg_mask ();
9435
9436   if (live_regs_mask)
9437     {
9438       const char * return_reg;
9439
9440       /* If we do not have any special requirements for function exit
9441          (e.g. interworking, or ISR) then we can load the return address
9442          directly into the PC.  Otherwise we must load it into LR.  */
9443       if (really_return
9444           && ! TARGET_INTERWORK)
9445         return_reg = reg_names[PC_REGNUM];
9446       else
9447         return_reg = reg_names[LR_REGNUM];
9448
9449       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9450         {
9451           /* There are three possible reasons for the IP register
9452              being saved.  1) a stack frame was created, in which case
9453              IP contains the old stack pointer, or 2) an ISR routine
9454              corrupted it, or 3) it was saved to align the stack on
9455              iWMMXt.  In case 1, restore IP into SP, otherwise just
9456              restore IP.  */
9457           if (frame_pointer_needed)
9458             {
9459               live_regs_mask &= ~ (1 << IP_REGNUM);
9460               live_regs_mask |=   (1 << SP_REGNUM);
9461             }
9462           else
9463             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9464         }
9465
9466       /* On some ARM architectures it is faster to use LDR rather than
9467          LDM to load a single register.  On other architectures, the
9468          cost is the same.  In 26 bit mode, or for exception handlers,
9469          we have to use LDM to load the PC so that the CPSR is also
9470          restored.  */
9471       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9472         if (live_regs_mask == (1U << reg))
9473           break;
9474
9475       if (reg <= LAST_ARM_REGNUM
9476           && (reg != LR_REGNUM
9477               || ! really_return
9478               || ! IS_INTERRUPT (func_type)))
9479         {
9480           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9481                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9482         }
9483       else
9484         {
9485           char *p;
9486           int first = 1;
9487
9488           /* Generate the load multiple instruction to restore the
9489              registers.  Note we can get here, even if
9490              frame_pointer_needed is true, but only if sp already
9491              points to the base of the saved core registers.  */
9492           if (live_regs_mask & (1 << SP_REGNUM))
9493             {
9494               unsigned HOST_WIDE_INT stack_adjust;
9495
9496               offsets = arm_get_frame_offsets ();
9497               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9498               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9499
9500               if (stack_adjust && arm_arch5)
9501                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9502               else
9503                 {
9504                   /* If we can't use ldmib (SA110 bug),
9505                      then try to pop r3 instead.  */
9506                   if (stack_adjust)
9507                     live_regs_mask |= 1 << 3;
9508                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9509                 }
9510             }
9511           else
9512             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9513
9514           p = instr + strlen (instr);
9515
9516           for (reg = 0; reg <= SP_REGNUM; reg++)
9517             if (live_regs_mask & (1 << reg))
9518               {
9519                 int l = strlen (reg_names[reg]);
9520
9521                 if (first)
9522                   first = 0;
9523                 else
9524                   {
9525                     memcpy (p, ", ", 2);
9526                     p += 2;
9527                   }
9528
9529                 memcpy (p, "%|", 2);
9530                 memcpy (p + 2, reg_names[reg], l);
9531                 p += l + 2;
9532               }
9533
9534           if (live_regs_mask & (1 << LR_REGNUM))
9535             {
9536               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9537               /* If returning from an interrupt, restore the CPSR.  */
9538               if (IS_INTERRUPT (func_type))
9539                 strcat (p, "^");
9540             }
9541           else
9542             strcpy (p, "}");
9543         }
9544
9545       output_asm_insn (instr, & operand);
9546
9547       /* See if we need to generate an extra instruction to
9548          perform the actual function return.  */
9549       if (really_return
9550           && func_type != ARM_FT_INTERWORKED
9551           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9552         {
9553           /* The return has already been handled
9554              by loading the LR into the PC.  */
9555           really_return = 0;
9556         }
9557     }
9558
9559   if (really_return)
9560     {
9561       switch ((int) ARM_FUNC_TYPE (func_type))
9562         {
9563         case ARM_FT_ISR:
9564         case ARM_FT_FIQ:
9565           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9566           break;
9567
9568         case ARM_FT_INTERWORKED:
9569           sprintf (instr, "bx%s\t%%|lr", conditional);
9570           break;
9571
9572         case ARM_FT_EXCEPTION:
9573           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9574           break;
9575
9576         default:
9577           /* Use bx if it's available.  */
9578           if (arm_arch5 || arm_arch4t)
9579             sprintf (instr, "bx%s\t%%|lr", conditional);
9580           else
9581             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9582           break;
9583         }
9584
9585       output_asm_insn (instr, & operand);
9586     }
9587
9588   return "";
9589 }
9590
9591 /* Write the function name into the code section, directly preceding
9592    the function prologue.
9593
9594    Code will be output similar to this:
9595      t0
9596          .ascii "arm_poke_function_name", 0
9597          .align
9598      t1
9599          .word 0xff000000 + (t1 - t0)
9600      arm_poke_function_name
9601          mov     ip, sp
9602          stmfd   sp!, {fp, ip, lr, pc}
9603          sub     fp, ip, #4
9604
9605    When performing a stack backtrace, code can inspect the value
9606    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9607    at location pc - 12 and the top 8 bits are set, then we know
9608    that there is a function name embedded immediately preceding this
9609    location and has length ((pc[-3]) & 0xff000000).
9610
9611    We assume that pc is declared as a pointer to an unsigned long.
9612
9613    It is of no benefit to output the function name if we are assembling
9614    a leaf function.  These function types will not contain a stack
9615    backtrace structure, therefore it is not possible to determine the
9616    function name.  */
9617 void
9618 arm_poke_function_name (FILE *stream, const char *name)
9619 {
9620   unsigned long alignlength;
9621   unsigned long length;
9622   rtx           x;
9623
9624   length      = strlen (name) + 1;
9625   alignlength = ROUND_UP_WORD (length);
9626
9627   ASM_OUTPUT_ASCII (stream, name, length);
9628   ASM_OUTPUT_ALIGN (stream, 2);
9629   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9630   assemble_aligned_integer (UNITS_PER_WORD, x);
9631 }
9632
9633 /* Place some comments into the assembler stream
9634    describing the current function.  */
9635 static void
9636 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9637 {
9638   unsigned long func_type;
9639
9640   if (!TARGET_ARM)
9641     {
9642       thumb_output_function_prologue (f, frame_size);
9643       return;
9644     }
9645
9646   /* Sanity check.  */
9647   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9648
9649   func_type = arm_current_func_type ();
9650
9651   switch ((int) ARM_FUNC_TYPE (func_type))
9652     {
9653     default:
9654     case ARM_FT_NORMAL:
9655       break;
9656     case ARM_FT_INTERWORKED:
9657       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9658       break;
9659     case ARM_FT_ISR:
9660       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9661       break;
9662     case ARM_FT_FIQ:
9663       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9664       break;
9665     case ARM_FT_EXCEPTION:
9666       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9667       break;
9668     }
9669
9670   if (IS_NAKED (func_type))
9671     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9672
9673   if (IS_VOLATILE (func_type))
9674     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9675
9676   if (IS_NESTED (func_type))
9677     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9678
9679   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9680                current_function_args_size,
9681                current_function_pretend_args_size, frame_size);
9682
9683   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9684                frame_pointer_needed,
9685                cfun->machine->uses_anonymous_args);
9686
9687   if (cfun->machine->lr_save_eliminated)
9688     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9689
9690   if (current_function_calls_eh_return)
9691     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9692
9693 #ifdef AOF_ASSEMBLER
9694   if (flag_pic)
9695     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9696 #endif
9697
9698   return_used_this_function = 0;
9699 }
9700
9701 const char *
9702 arm_output_epilogue (rtx sibling)
9703 {
9704   int reg;
9705   unsigned long saved_regs_mask;
9706   unsigned long func_type;
9707   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9708      frame that is $fp + 4 for a non-variadic function.  */
9709   int floats_offset = 0;
9710   rtx operands[3];
9711   FILE * f = asm_out_file;
9712   unsigned int lrm_count = 0;
9713   int really_return = (sibling == NULL);
9714   int start_reg;
9715   arm_stack_offsets *offsets;
9716
9717   /* If we have already generated the return instruction
9718      then it is futile to generate anything else.  */
9719   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9720     return "";
9721
9722   func_type = arm_current_func_type ();
9723
9724   if (IS_NAKED (func_type))
9725     /* Naked functions don't have epilogues.  */
9726     return "";
9727
9728   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9729     {
9730       rtx op;
9731
9732       /* A volatile function should never return.  Call abort.  */
9733       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9734       assemble_external_libcall (op);
9735       output_asm_insn ("bl\t%a0", &op);
9736
9737       return "";
9738     }
9739
9740   /* If we are throwing an exception, then we really must be doing a
9741      return, so we can't tail-call.  */
9742   gcc_assert (!current_function_calls_eh_return || really_return);
9743
9744   offsets = arm_get_frame_offsets ();
9745   saved_regs_mask = arm_compute_save_reg_mask ();
9746
9747   if (TARGET_IWMMXT)
9748     lrm_count = bit_count (saved_regs_mask);
9749
9750   floats_offset = offsets->saved_args;
9751   /* Compute how far away the floats will be.  */
9752   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9753     if (saved_regs_mask & (1 << reg))
9754       floats_offset += 4;
9755
9756   if (frame_pointer_needed)
9757     {
9758       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9759       int vfp_offset = offsets->frame;
9760
9761       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9762         {
9763           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9764             if (regs_ever_live[reg] && !call_used_regs[reg])
9765               {
9766                 floats_offset += 12;
9767                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9768                              reg, FP_REGNUM, floats_offset - vfp_offset);
9769               }
9770         }
9771       else
9772         {
9773           start_reg = LAST_FPA_REGNUM;
9774
9775           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9776             {
9777               if (regs_ever_live[reg] && !call_used_regs[reg])
9778                 {
9779                   floats_offset += 12;
9780
9781                   /* We can't unstack more than four registers at once.  */
9782                   if (start_reg - reg == 3)
9783                     {
9784                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9785                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9786                       start_reg = reg - 1;
9787                     }
9788                 }
9789               else
9790                 {
9791                   if (reg != start_reg)
9792                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9793                                  reg + 1, start_reg - reg,
9794                                  FP_REGNUM, floats_offset - vfp_offset);
9795                   start_reg = reg - 1;
9796                 }
9797             }
9798
9799           /* Just in case the last register checked also needs unstacking.  */
9800           if (reg != start_reg)
9801             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9802                          reg + 1, start_reg - reg,
9803                          FP_REGNUM, floats_offset - vfp_offset);
9804         }
9805
9806       if (TARGET_HARD_FLOAT && TARGET_VFP)
9807         {
9808           int saved_size;
9809
9810           /* The fldmx insn does not have base+offset addressing modes,
9811              so we use IP to hold the address.  */
9812           saved_size = arm_get_vfp_saved_size ();
9813
9814           if (saved_size > 0)
9815             {
9816               floats_offset += saved_size;
9817               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9818                            FP_REGNUM, floats_offset - vfp_offset);
9819             }
9820           start_reg = FIRST_VFP_REGNUM;
9821           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9822             {
9823               if ((!regs_ever_live[reg] || call_used_regs[reg])
9824                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9825                 {
9826                   if (start_reg != reg)
9827                     arm_output_fldmx (f, IP_REGNUM,
9828                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9829                                       (reg - start_reg) / 2);
9830                   start_reg = reg + 2;
9831                 }
9832             }
9833           if (start_reg != reg)
9834             arm_output_fldmx (f, IP_REGNUM,
9835                               (start_reg - FIRST_VFP_REGNUM) / 2,
9836                               (reg - start_reg) / 2);
9837         }
9838
9839       if (TARGET_IWMMXT)
9840         {
9841           /* The frame pointer is guaranteed to be non-double-word aligned.
9842              This is because it is set to (old_stack_pointer - 4) and the
9843              old_stack_pointer was double word aligned.  Thus the offset to
9844              the iWMMXt registers to be loaded must also be non-double-word
9845              sized, so that the resultant address *is* double-word aligned.
9846              We can ignore floats_offset since that was already included in
9847              the live_regs_mask.  */
9848           lrm_count += (lrm_count % 2 ? 2 : 1);
9849
9850           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9851             if (regs_ever_live[reg] && !call_used_regs[reg])
9852               {
9853                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9854                              reg, FP_REGNUM, lrm_count * 4);
9855                 lrm_count += 2;
9856               }
9857         }
9858
9859       /* saved_regs_mask should contain the IP, which at the time of stack
9860          frame generation actually contains the old stack pointer.  So a
9861          quick way to unwind the stack is just pop the IP register directly
9862          into the stack pointer.  */
9863       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9864       saved_regs_mask &= ~ (1 << IP_REGNUM);
9865       saved_regs_mask |=   (1 << SP_REGNUM);
9866
9867       /* There are two registers left in saved_regs_mask - LR and PC.  We
9868          only need to restore the LR register (the return address), but to
9869          save time we can load it directly into the PC, unless we need a
9870          special function exit sequence, or we are not really returning.  */
9871       if (really_return
9872           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9873           && !current_function_calls_eh_return)
9874         /* Delete the LR from the register mask, so that the LR on
9875            the stack is loaded into the PC in the register mask.  */
9876         saved_regs_mask &= ~ (1 << LR_REGNUM);
9877       else
9878         saved_regs_mask &= ~ (1 << PC_REGNUM);
9879
9880       /* We must use SP as the base register, because SP is one of the
9881          registers being restored.  If an interrupt or page fault
9882          happens in the ldm instruction, the SP might or might not
9883          have been restored.  That would be bad, as then SP will no
9884          longer indicate the safe area of stack, and we can get stack
9885          corruption.  Using SP as the base register means that it will
9886          be reset correctly to the original value, should an interrupt
9887          occur.  If the stack pointer already points at the right
9888          place, then omit the subtraction.  */
9889       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9890           || current_function_calls_alloca)
9891         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9892                      4 * bit_count (saved_regs_mask));
9893       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9894
9895       if (IS_INTERRUPT (func_type))
9896         /* Interrupt handlers will have pushed the
9897            IP onto the stack, so restore it now.  */
9898         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9899     }
9900   else
9901     {
9902       /* Restore stack pointer if necessary.  */
9903       if (offsets->outgoing_args != offsets->saved_regs)
9904         {
9905           operands[0] = operands[1] = stack_pointer_rtx;
9906           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9907           output_add_immediate (operands);
9908         }
9909
9910       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9911         {
9912           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9913             if (regs_ever_live[reg] && !call_used_regs[reg])
9914               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9915                            reg, SP_REGNUM);
9916         }
9917       else
9918         {
9919           start_reg = FIRST_FPA_REGNUM;
9920
9921           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9922             {
9923               if (regs_ever_live[reg] && !call_used_regs[reg])
9924                 {
9925                   if (reg - start_reg == 3)
9926                     {
9927                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9928                                    start_reg, SP_REGNUM);
9929                       start_reg = reg + 1;
9930                     }
9931                 }
9932               else
9933                 {
9934                   if (reg != start_reg)
9935                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9936                                  start_reg, reg - start_reg,
9937                                  SP_REGNUM);
9938
9939                   start_reg = reg + 1;
9940                 }
9941             }
9942
9943           /* Just in case the last register checked also needs unstacking.  */
9944           if (reg != start_reg)
9945             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9946                          start_reg, reg - start_reg, SP_REGNUM);
9947         }
9948
9949       if (TARGET_HARD_FLOAT && TARGET_VFP)
9950         {
9951           start_reg = FIRST_VFP_REGNUM;
9952           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9953             {
9954               if ((!regs_ever_live[reg] || call_used_regs[reg])
9955                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9956                 {
9957                   if (start_reg != reg)
9958                     arm_output_fldmx (f, SP_REGNUM,
9959                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9960                                       (reg - start_reg) / 2);
9961                   start_reg = reg + 2;
9962                 }
9963             }
9964           if (start_reg != reg)
9965             arm_output_fldmx (f, SP_REGNUM,
9966                               (start_reg - FIRST_VFP_REGNUM) / 2,
9967                               (reg - start_reg) / 2);
9968         }
9969       if (TARGET_IWMMXT)
9970         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9971           if (regs_ever_live[reg] && !call_used_regs[reg])
9972             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9973
9974       /* If we can, restore the LR into the PC.  */
9975       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9976           && really_return
9977           && current_function_pretend_args_size == 0
9978           && saved_regs_mask & (1 << LR_REGNUM)
9979           && !current_function_calls_eh_return)
9980         {
9981           saved_regs_mask &= ~ (1 << LR_REGNUM);
9982           saved_regs_mask |=   (1 << PC_REGNUM);
9983         }
9984
9985       /* Load the registers off the stack.  If we only have one register
9986          to load use the LDR instruction - it is faster.  */
9987       if (saved_regs_mask == (1 << LR_REGNUM))
9988         {
9989           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9990         }
9991       else if (saved_regs_mask)
9992         {
9993           if (saved_regs_mask & (1 << SP_REGNUM))
9994             /* Note - write back to the stack register is not enabled
9995                (i.e. "ldmfd sp!...").  We know that the stack pointer is
9996                in the list of registers and if we add writeback the
9997                instruction becomes UNPREDICTABLE.  */
9998             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9999           else
10000             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
10001         }
10002
10003       if (current_function_pretend_args_size)
10004         {
10005           /* Unwind the pre-pushed regs.  */
10006           operands[0] = operands[1] = stack_pointer_rtx;
10007           operands[2] = GEN_INT (current_function_pretend_args_size);
10008           output_add_immediate (operands);
10009         }
10010     }
10011
10012   /* We may have already restored PC directly from the stack.  */
10013   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10014     return "";
10015
10016   /* Stack adjustment for exception handler.  */
10017   if (current_function_calls_eh_return)
10018     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10019                  ARM_EH_STACKADJ_REGNUM);
10020
10021   /* Generate the return instruction.  */
10022   switch ((int) ARM_FUNC_TYPE (func_type))
10023     {
10024     case ARM_FT_ISR:
10025     case ARM_FT_FIQ:
10026       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10027       break;
10028
10029     case ARM_FT_EXCEPTION:
10030       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10031       break;
10032
10033     case ARM_FT_INTERWORKED:
10034       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10035       break;
10036
10037     default:
10038       if (arm_arch5 || arm_arch4t)
10039         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10040       else
10041         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10042       break;
10043     }
10044
10045   return "";
10046 }
10047
10048 static void
10049 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10050                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10051 {
10052   arm_stack_offsets *offsets;
10053
10054   if (TARGET_THUMB)
10055     {
10056       int regno;
10057
10058       /* Emit any call-via-reg trampolines that are needed for v4t support
10059          of call_reg and call_value_reg type insns.  */
10060       for (regno = 0; regno < LR_REGNUM; regno++)
10061         {
10062           rtx label = cfun->machine->call_via[regno];
10063
10064           if (label != NULL)
10065             {
10066               function_section (current_function_decl);
10067               targetm.asm_out.internal_label (asm_out_file, "L",
10068                                               CODE_LABEL_NUMBER (label));
10069               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10070             }
10071         }
10072
10073       /* ??? Probably not safe to set this here, since it assumes that a
10074          function will be emitted as assembly immediately after we generate
10075          RTL for it.  This does not happen for inline functions.  */
10076       return_used_this_function = 0;
10077     }
10078   else
10079     {
10080       /* We need to take into account any stack-frame rounding.  */
10081       offsets = arm_get_frame_offsets ();
10082
10083       gcc_assert (!use_return_insn (FALSE, NULL)
10084                   || !return_used_this_function
10085                   || offsets->saved_regs == offsets->outgoing_args
10086                   || frame_pointer_needed);
10087
10088       /* Reset the ARM-specific per-function variables.  */
10089       after_arm_reorg = 0;
10090     }
10091 }
10092
10093 /* Generate and emit an insn that we will recognize as a push_multi.
10094    Unfortunately, since this insn does not reflect very well the actual
10095    semantics of the operation, we need to annotate the insn for the benefit
10096    of DWARF2 frame unwind information.  */
10097 static rtx
10098 emit_multi_reg_push (unsigned long mask)
10099 {
10100   int num_regs = 0;
10101   int num_dwarf_regs;
10102   int i, j;
10103   rtx par;
10104   rtx dwarf;
10105   int dwarf_par_index;
10106   rtx tmp, reg;
10107
10108   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10109     if (mask & (1 << i))
10110       num_regs++;
10111
10112   gcc_assert (num_regs && num_regs <= 16);
10113
10114   /* We don't record the PC in the dwarf frame information.  */
10115   num_dwarf_regs = num_regs;
10116   if (mask & (1 << PC_REGNUM))
10117     num_dwarf_regs--;
10118
10119   /* For the body of the insn we are going to generate an UNSPEC in
10120      parallel with several USEs.  This allows the insn to be recognized
10121      by the push_multi pattern in the arm.md file.  The insn looks
10122      something like this:
10123
10124        (parallel [
10125            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10126                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10127            (use (reg:SI 11 fp))
10128            (use (reg:SI 12 ip))
10129            (use (reg:SI 14 lr))
10130            (use (reg:SI 15 pc))
10131         ])
10132
10133      For the frame note however, we try to be more explicit and actually
10134      show each register being stored into the stack frame, plus a (single)
10135      decrement of the stack pointer.  We do it this way in order to be
10136      friendly to the stack unwinding code, which only wants to see a single
10137      stack decrement per instruction.  The RTL we generate for the note looks
10138      something like this:
10139
10140       (sequence [
10141            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10142            (set (mem:SI (reg:SI sp)) (reg:SI r4))
10143            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10144            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10145            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10146         ])
10147
10148       This sequence is used both by the code to support stack unwinding for
10149       exceptions handlers and the code to generate dwarf2 frame debugging.  */
10150
10151   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10152   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10153   dwarf_par_index = 1;
10154
10155   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10156     {
10157       if (mask & (1 << i))
10158         {
10159           reg = gen_rtx_REG (SImode, i);
10160
10161           XVECEXP (par, 0, 0)
10162             = gen_rtx_SET (VOIDmode,
10163                            gen_frame_mem (BLKmode,
10164                                           gen_rtx_PRE_DEC (BLKmode,
10165                                                            stack_pointer_rtx)),
10166                            gen_rtx_UNSPEC (BLKmode,
10167                                            gen_rtvec (1, reg),
10168                                            UNSPEC_PUSH_MULT));
10169
10170           if (i != PC_REGNUM)
10171             {
10172               tmp = gen_rtx_SET (VOIDmode,
10173                                  gen_frame_mem (SImode, stack_pointer_rtx),
10174                                  reg);
10175               RTX_FRAME_RELATED_P (tmp) = 1;
10176               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10177               dwarf_par_index++;
10178             }
10179
10180           break;
10181         }
10182     }
10183
10184   for (j = 1, i++; j < num_regs; i++)
10185     {
10186       if (mask & (1 << i))
10187         {
10188           reg = gen_rtx_REG (SImode, i);
10189
10190           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10191
10192           if (i != PC_REGNUM)
10193             {
10194               tmp
10195                 = gen_rtx_SET (VOIDmode,
10196                                gen_frame_mem (SImode,
10197                                               plus_constant (stack_pointer_rtx,
10198                                                              4 * j)),
10199                                reg);
10200               RTX_FRAME_RELATED_P (tmp) = 1;
10201               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10202             }
10203
10204           j++;
10205         }
10206     }
10207
10208   par = emit_insn (par);
10209
10210   tmp = gen_rtx_SET (SImode,
10211                      stack_pointer_rtx,
10212                      gen_rtx_PLUS (SImode,
10213                                    stack_pointer_rtx,
10214                                    GEN_INT (-4 * num_regs)));
10215   RTX_FRAME_RELATED_P (tmp) = 1;
10216   XVECEXP (dwarf, 0, 0) = tmp;
10217
10218   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10219                                        REG_NOTES (par));
10220   return par;
10221 }
10222
10223 /* Calculate the size of the return value that is passed in registers.  */
10224 static int
10225 arm_size_return_regs (void)
10226 {
10227   enum machine_mode mode;
10228
10229   if (current_function_return_rtx != 0)
10230     mode = GET_MODE (current_function_return_rtx);
10231   else
10232     mode = DECL_MODE (DECL_RESULT (current_function_decl));
10233
10234   return GET_MODE_SIZE (mode);
10235 }
10236
10237 static rtx
10238 emit_sfm (int base_reg, int count)
10239 {
10240   rtx par;
10241   rtx dwarf;
10242   rtx tmp, reg;
10243   int i;
10244
10245   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10246   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10247
10248   reg = gen_rtx_REG (XFmode, base_reg++);
10249
10250   XVECEXP (par, 0, 0)
10251     = gen_rtx_SET (VOIDmode,
10252                    gen_frame_mem (BLKmode,
10253                                   gen_rtx_PRE_DEC (BLKmode,
10254                                                    stack_pointer_rtx)),
10255                    gen_rtx_UNSPEC (BLKmode,
10256                                    gen_rtvec (1, reg),
10257                                    UNSPEC_PUSH_MULT));
10258   tmp = gen_rtx_SET (VOIDmode,
10259                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10260   RTX_FRAME_RELATED_P (tmp) = 1;
10261   XVECEXP (dwarf, 0, 1) = tmp;
10262
10263   for (i = 1; i < count; i++)
10264     {
10265       reg = gen_rtx_REG (XFmode, base_reg++);
10266       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10267
10268       tmp = gen_rtx_SET (VOIDmode,
10269                          gen_frame_mem (XFmode,
10270                                         plus_constant (stack_pointer_rtx,
10271                                                        i * 12)),
10272                          reg);
10273       RTX_FRAME_RELATED_P (tmp) = 1;
10274       XVECEXP (dwarf, 0, i + 1) = tmp;
10275     }
10276
10277   tmp = gen_rtx_SET (VOIDmode,
10278                      stack_pointer_rtx,
10279                      gen_rtx_PLUS (SImode,
10280                                    stack_pointer_rtx,
10281                                    GEN_INT (-12 * count)));
10282   RTX_FRAME_RELATED_P (tmp) = 1;
10283   XVECEXP (dwarf, 0, 0) = tmp;
10284
10285   par = emit_insn (par);
10286   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10287                                        REG_NOTES (par));
10288   return par;
10289 }
10290
10291
10292 /* Return true if the current function needs to save/restore LR.  */
10293
10294 static bool
10295 thumb_force_lr_save (void)
10296 {
10297   return !cfun->machine->lr_save_eliminated
10298          && (!leaf_function_p ()
10299              || thumb_far_jump_used_p ()
10300              || regs_ever_live [LR_REGNUM]);
10301 }
10302
10303
10304 /* Compute the distance from register FROM to register TO.
10305    These can be the arg pointer (26), the soft frame pointer (25),
10306    the stack pointer (13) or the hard frame pointer (11).
10307    In thumb mode r7 is used as the soft frame pointer, if needed.
10308    Typical stack layout looks like this:
10309
10310        old stack pointer -> |    |
10311                              ----
10312                             |    | \
10313                             |    |   saved arguments for
10314                             |    |   vararg functions
10315                             |    | /
10316                               --
10317    hard FP & arg pointer -> |    | \
10318                             |    |   stack
10319                             |    |   frame
10320                             |    | /
10321                               --
10322                             |    | \
10323                             |    |   call saved
10324                             |    |   registers
10325       soft frame pointer -> |    | /
10326                               --
10327                             |    | \
10328                             |    |   local
10329                             |    |   variables
10330      locals base pointer -> |    | /
10331                               --
10332                             |    | \
10333                             |    |   outgoing
10334                             |    |   arguments
10335    current stack pointer -> |    | /
10336                               --
10337
10338   For a given function some or all of these stack components
10339   may not be needed, giving rise to the possibility of
10340   eliminating some of the registers.
10341
10342   The values returned by this function must reflect the behavior
10343   of arm_expand_prologue() and arm_compute_save_reg_mask().
10344
10345   The sign of the number returned reflects the direction of stack
10346   growth, so the values are positive for all eliminations except
10347   from the soft frame pointer to the hard frame pointer.
10348
10349   SFP may point just inside the local variables block to ensure correct
10350   alignment.  */
10351
10352
10353 /* Calculate stack offsets.  These are used to calculate register elimination
10354    offsets and in prologue/epilogue code.  */
10355
10356 static arm_stack_offsets *
10357 arm_get_frame_offsets (void)
10358 {
10359   struct arm_stack_offsets *offsets;
10360   unsigned long func_type;
10361   int leaf;
10362   int saved;
10363   HOST_WIDE_INT frame_size;
10364
10365   offsets = &cfun->machine->stack_offsets;
10366
10367   /* We need to know if we are a leaf function.  Unfortunately, it
10368      is possible to be called after start_sequence has been called,
10369      which causes get_insns to return the insns for the sequence,
10370      not the function, which will cause leaf_function_p to return
10371      the incorrect result.
10372
10373      to know about leaf functions once reload has completed, and the
10374      frame size cannot be changed after that time, so we can safely
10375      use the cached value.  */
10376
10377   if (reload_completed)
10378     return offsets;
10379
10380   /* Initially this is the size of the local variables.  It will translated
10381      into an offset once we have determined the size of preceding data.  */
10382   frame_size = ROUND_UP_WORD (get_frame_size ());
10383
10384   leaf = leaf_function_p ();
10385
10386   /* Space for variadic functions.  */
10387   offsets->saved_args = current_function_pretend_args_size;
10388
10389   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10390
10391   if (TARGET_ARM)
10392     {
10393       unsigned int regno;
10394
10395       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10396
10397       /* We know that SP will be doubleword aligned on entry, and we must
10398          preserve that condition at any subroutine call.  We also require the
10399          soft frame pointer to be doubleword aligned.  */
10400
10401       if (TARGET_REALLY_IWMMXT)
10402         {
10403           /* Check for the call-saved iWMMXt registers.  */
10404           for (regno = FIRST_IWMMXT_REGNUM;
10405                regno <= LAST_IWMMXT_REGNUM;
10406                regno++)
10407             if (regs_ever_live [regno] && ! call_used_regs [regno])
10408               saved += 8;
10409         }
10410
10411       func_type = arm_current_func_type ();
10412       if (! IS_VOLATILE (func_type))
10413         {
10414           /* Space for saved FPA registers.  */
10415           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10416           if (regs_ever_live[regno] && ! call_used_regs[regno])
10417             saved += 12;
10418
10419           /* Space for saved VFP registers.  */
10420           if (TARGET_HARD_FLOAT && TARGET_VFP)
10421             saved += arm_get_vfp_saved_size ();
10422         }
10423     }
10424   else /* TARGET_THUMB */
10425     {
10426       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10427       if (TARGET_BACKTRACE)
10428         saved += 16;
10429     }
10430
10431   /* Saved registers include the stack frame.  */
10432   offsets->saved_regs = offsets->saved_args + saved;
10433   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10434   /* A leaf function does not need any stack alignment if it has nothing
10435      on the stack.  */
10436   if (leaf && frame_size == 0)
10437     {
10438       offsets->outgoing_args = offsets->soft_frame;
10439       return offsets;
10440     }
10441
10442   /* Ensure SFP has the correct alignment.  */
10443   if (ARM_DOUBLEWORD_ALIGN
10444       && (offsets->soft_frame & 7))
10445     offsets->soft_frame += 4;
10446
10447   offsets->locals_base = offsets->soft_frame + frame_size;
10448   offsets->outgoing_args = (offsets->locals_base
10449                             + current_function_outgoing_args_size);
10450
10451   if (ARM_DOUBLEWORD_ALIGN)
10452     {
10453       /* Ensure SP remains doubleword aligned.  */
10454       if (offsets->outgoing_args & 7)
10455         offsets->outgoing_args += 4;
10456       gcc_assert (!(offsets->outgoing_args & 7));
10457     }
10458
10459   return offsets;
10460 }
10461
10462
10463 /* Calculate the relative offsets for the different stack pointers.  Positive
10464    offsets are in the direction of stack growth.  */
10465
10466 HOST_WIDE_INT
10467 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10468 {
10469   arm_stack_offsets *offsets;
10470
10471   offsets = arm_get_frame_offsets ();
10472
10473   /* OK, now we have enough information to compute the distances.
10474      There must be an entry in these switch tables for each pair
10475      of registers in ELIMINABLE_REGS, even if some of the entries
10476      seem to be redundant or useless.  */
10477   switch (from)
10478     {
10479     case ARG_POINTER_REGNUM:
10480       switch (to)
10481         {
10482         case THUMB_HARD_FRAME_POINTER_REGNUM:
10483           return 0;
10484
10485         case FRAME_POINTER_REGNUM:
10486           /* This is the reverse of the soft frame pointer
10487              to hard frame pointer elimination below.  */
10488           return offsets->soft_frame - offsets->saved_args;
10489
10490         case ARM_HARD_FRAME_POINTER_REGNUM:
10491           /* If there is no stack frame then the hard
10492              frame pointer and the arg pointer coincide.  */
10493           if (offsets->frame == offsets->saved_regs)
10494             return 0;
10495           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10496           return (frame_pointer_needed
10497                   && cfun->static_chain_decl != NULL
10498                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10499
10500         case STACK_POINTER_REGNUM:
10501           /* If nothing has been pushed on the stack at all
10502              then this will return -4.  This *is* correct!  */
10503           return offsets->outgoing_args - (offsets->saved_args + 4);
10504
10505         default:
10506           gcc_unreachable ();
10507         }
10508       gcc_unreachable ();
10509
10510     case FRAME_POINTER_REGNUM:
10511       switch (to)
10512         {
10513         case THUMB_HARD_FRAME_POINTER_REGNUM:
10514           return 0;
10515
10516         case ARM_HARD_FRAME_POINTER_REGNUM:
10517           /* The hard frame pointer points to the top entry in the
10518              stack frame.  The soft frame pointer to the bottom entry
10519              in the stack frame.  If there is no stack frame at all,
10520              then they are identical.  */
10521
10522           return offsets->frame - offsets->soft_frame;
10523
10524         case STACK_POINTER_REGNUM:
10525           return offsets->outgoing_args - offsets->soft_frame;
10526
10527         default:
10528           gcc_unreachable ();
10529         }
10530       gcc_unreachable ();
10531
10532     default:
10533       /* You cannot eliminate from the stack pointer.
10534          In theory you could eliminate from the hard frame
10535          pointer to the stack pointer, but this will never
10536          happen, since if a stack frame is not needed the
10537          hard frame pointer will never be used.  */
10538       gcc_unreachable ();
10539     }
10540 }
10541
10542
10543 /* Generate the prologue instructions for entry into an ARM function.  */
10544 void
10545 arm_expand_prologue (void)
10546 {
10547   int reg;
10548   rtx amount;
10549   rtx insn;
10550   rtx ip_rtx;
10551   unsigned long live_regs_mask;
10552   unsigned long func_type;
10553   int fp_offset = 0;
10554   int saved_pretend_args = 0;
10555   int saved_regs = 0;
10556   unsigned HOST_WIDE_INT args_to_push;
10557   arm_stack_offsets *offsets;
10558
10559   func_type = arm_current_func_type ();
10560
10561   /* Naked functions don't have prologues.  */
10562   if (IS_NAKED (func_type))
10563     return;
10564
10565   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10566   args_to_push = current_function_pretend_args_size;
10567
10568   /* Compute which register we will have to save onto the stack.  */
10569   live_regs_mask = arm_compute_save_reg_mask ();
10570
10571   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10572
10573   if (frame_pointer_needed)
10574     {
10575       if (IS_INTERRUPT (func_type))
10576         {
10577           /* Interrupt functions must not corrupt any registers.
10578              Creating a frame pointer however, corrupts the IP
10579              register, so we must push it first.  */
10580           insn = emit_multi_reg_push (1 << IP_REGNUM);
10581
10582           /* Do not set RTX_FRAME_RELATED_P on this insn.
10583              The dwarf stack unwinding code only wants to see one
10584              stack decrement per function, and this is not it.  If
10585              this instruction is labeled as being part of the frame
10586              creation sequence then dwarf2out_frame_debug_expr will
10587              die when it encounters the assignment of IP to FP
10588              later on, since the use of SP here establishes SP as
10589              the CFA register and not IP.
10590
10591              Anyway this instruction is not really part of the stack
10592              frame creation although it is part of the prologue.  */
10593         }
10594       else if (IS_NESTED (func_type))
10595         {
10596           /* The Static chain register is the same as the IP register
10597              used as a scratch register during stack frame creation.
10598              To get around this need to find somewhere to store IP
10599              whilst the frame is being created.  We try the following
10600              places in order:
10601
10602                1. The last argument register.
10603                2. A slot on the stack above the frame.  (This only
10604                   works if the function is not a varargs function).
10605                3. Register r3, after pushing the argument registers
10606                   onto the stack.
10607
10608              Note - we only need to tell the dwarf2 backend about the SP
10609              adjustment in the second variant; the static chain register
10610              doesn't need to be unwound, as it doesn't contain a value
10611              inherited from the caller.  */
10612
10613           if (regs_ever_live[3] == 0)
10614             {
10615               insn = gen_rtx_REG (SImode, 3);
10616               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10617               insn = emit_insn (insn);
10618             }
10619           else if (args_to_push == 0)
10620             {
10621               rtx dwarf;
10622               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10623               insn = gen_frame_mem (SImode, insn);
10624               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10625               insn = emit_insn (insn);
10626
10627               fp_offset = 4;
10628
10629               /* Just tell the dwarf backend that we adjusted SP.  */
10630               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10631                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10632                                                  GEN_INT (-fp_offset)));
10633               RTX_FRAME_RELATED_P (insn) = 1;
10634               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10635                                                     dwarf, REG_NOTES (insn));
10636             }
10637           else
10638             {
10639               /* Store the args on the stack.  */
10640               if (cfun->machine->uses_anonymous_args)
10641                 insn = emit_multi_reg_push
10642                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10643               else
10644                 insn = emit_insn
10645                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10646                                GEN_INT (- args_to_push)));
10647
10648               RTX_FRAME_RELATED_P (insn) = 1;
10649
10650               saved_pretend_args = 1;
10651               fp_offset = args_to_push;
10652               args_to_push = 0;
10653
10654               /* Now reuse r3 to preserve IP.  */
10655               insn = gen_rtx_REG (SImode, 3);
10656               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10657               (void) emit_insn (insn);
10658             }
10659         }
10660
10661       if (fp_offset)
10662         {
10663           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10664           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10665         }
10666       else
10667         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10668
10669       insn = emit_insn (insn);
10670       RTX_FRAME_RELATED_P (insn) = 1;
10671     }
10672
10673   if (args_to_push)
10674     {
10675       /* Push the argument registers, or reserve space for them.  */
10676       if (cfun->machine->uses_anonymous_args)
10677         insn = emit_multi_reg_push
10678           ((0xf0 >> (args_to_push / 4)) & 0xf);
10679       else
10680         insn = emit_insn
10681           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10682                        GEN_INT (- args_to_push)));
10683       RTX_FRAME_RELATED_P (insn) = 1;
10684     }
10685
10686   /* If this is an interrupt service routine, and the link register
10687      is going to be pushed, and we are not creating a stack frame,
10688      (which would involve an extra push of IP and a pop in the epilogue)
10689      subtracting four from LR now will mean that the function return
10690      can be done with a single instruction.  */
10691   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10692       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10693       && ! frame_pointer_needed)
10694     emit_insn (gen_rtx_SET (SImode,
10695                             gen_rtx_REG (SImode, LR_REGNUM),
10696                             gen_rtx_PLUS (SImode,
10697                                           gen_rtx_REG (SImode, LR_REGNUM),
10698                                           GEN_INT (-4))));
10699
10700   if (live_regs_mask)
10701     {
10702       insn = emit_multi_reg_push (live_regs_mask);
10703       saved_regs += bit_count (live_regs_mask) * 4;
10704       RTX_FRAME_RELATED_P (insn) = 1;
10705     }
10706
10707   if (TARGET_IWMMXT)
10708     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10709       if (regs_ever_live[reg] && ! call_used_regs [reg])
10710         {
10711           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10712           insn = gen_frame_mem (V2SImode, insn);
10713           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10714                                          gen_rtx_REG (V2SImode, reg)));
10715           RTX_FRAME_RELATED_P (insn) = 1;
10716           saved_regs += 8;
10717         }
10718
10719   if (! IS_VOLATILE (func_type))
10720     {
10721       int start_reg;
10722
10723       /* Save any floating point call-saved registers used by this
10724          function.  */
10725       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10726         {
10727           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10728             if (regs_ever_live[reg] && !call_used_regs[reg])
10729               {
10730                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10731                 insn = gen_frame_mem (XFmode, insn);
10732                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10733                                                gen_rtx_REG (XFmode, reg)));
10734                 RTX_FRAME_RELATED_P (insn) = 1;
10735                 saved_regs += 12;
10736               }
10737         }
10738       else
10739         {
10740           start_reg = LAST_FPA_REGNUM;
10741
10742           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10743             {
10744               if (regs_ever_live[reg] && !call_used_regs[reg])
10745                 {
10746                   if (start_reg - reg == 3)
10747                     {
10748                       insn = emit_sfm (reg, 4);
10749                       RTX_FRAME_RELATED_P (insn) = 1;
10750                       saved_regs += 48;
10751                       start_reg = reg - 1;
10752                     }
10753                 }
10754               else
10755                 {
10756                   if (start_reg != reg)
10757                     {
10758                       insn = emit_sfm (reg + 1, start_reg - reg);
10759                       RTX_FRAME_RELATED_P (insn) = 1;
10760                       saved_regs += (start_reg - reg) * 12;
10761                     }
10762                   start_reg = reg - 1;
10763                 }
10764             }
10765
10766           if (start_reg != reg)
10767             {
10768               insn = emit_sfm (reg + 1, start_reg - reg);
10769               saved_regs += (start_reg - reg) * 12;
10770               RTX_FRAME_RELATED_P (insn) = 1;
10771             }
10772         }
10773       if (TARGET_HARD_FLOAT && TARGET_VFP)
10774         {
10775           start_reg = FIRST_VFP_REGNUM;
10776
10777           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10778             {
10779               if ((!regs_ever_live[reg] || call_used_regs[reg])
10780                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10781                 {
10782                   if (start_reg != reg)
10783                     saved_regs += vfp_emit_fstmx (start_reg,
10784                                                   (reg - start_reg) / 2);
10785                   start_reg = reg + 2;
10786                 }
10787             }
10788           if (start_reg != reg)
10789             saved_regs += vfp_emit_fstmx (start_reg,
10790                                           (reg - start_reg) / 2);
10791         }
10792     }
10793
10794   if (frame_pointer_needed)
10795     {
10796       /* Create the new frame pointer.  */
10797       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10798       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10799       RTX_FRAME_RELATED_P (insn) = 1;
10800
10801       if (IS_NESTED (func_type))
10802         {
10803           /* Recover the static chain register.  */
10804           if (regs_ever_live [3] == 0
10805               || saved_pretend_args)
10806             insn = gen_rtx_REG (SImode, 3);
10807           else /* if (current_function_pretend_args_size == 0) */
10808             {
10809               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10810                                    GEN_INT (4));
10811               insn = gen_frame_mem (SImode, insn);
10812             }
10813
10814           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10815           /* Add a USE to stop propagate_one_insn() from barfing.  */
10816           emit_insn (gen_prologue_use (ip_rtx));
10817         }
10818     }
10819
10820   offsets = arm_get_frame_offsets ();
10821   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10822     {
10823       /* This add can produce multiple insns for a large constant, so we
10824          need to get tricky.  */
10825       rtx last = get_last_insn ();
10826
10827       amount = GEN_INT (offsets->saved_args + saved_regs
10828                         - offsets->outgoing_args);
10829
10830       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10831                                     amount));
10832       do
10833         {
10834           last = last ? NEXT_INSN (last) : get_insns ();
10835           RTX_FRAME_RELATED_P (last) = 1;
10836         }
10837       while (last != insn);
10838
10839       /* If the frame pointer is needed, emit a special barrier that
10840          will prevent the scheduler from moving stores to the frame
10841          before the stack adjustment.  */
10842       if (frame_pointer_needed)
10843         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10844                                          hard_frame_pointer_rtx));
10845     }
10846
10847
10848   if (flag_pic)
10849     arm_load_pic_register (0UL);
10850
10851   /* If we are profiling, make sure no instructions are scheduled before
10852      the call to mcount.  Similarly if the user has requested no
10853      scheduling in the prolog.  Similarly if we want non-call exceptions
10854      using the EABI unwinder, to prevent faulting instructions from being
10855      swapped with a stack adjustment.  */
10856   if (current_function_profile || !TARGET_SCHED_PROLOG
10857       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
10858     emit_insn (gen_blockage ());
10859
10860   /* If the link register is being kept alive, with the return address in it,
10861      then make sure that it does not get reused by the ce2 pass.  */
10862   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10863     {
10864       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10865       cfun->machine->lr_save_eliminated = 1;
10866     }
10867 }
10868 \f
10869 /* If CODE is 'd', then the X is a condition operand and the instruction
10870    should only be executed if the condition is true.
10871    if CODE is 'D', then the X is a condition operand and the instruction
10872    should only be executed if the condition is false: however, if the mode
10873    of the comparison is CCFPEmode, then always execute the instruction -- we
10874    do this because in these circumstances !GE does not necessarily imply LT;
10875    in these cases the instruction pattern will take care to make sure that
10876    an instruction containing %d will follow, thereby undoing the effects of
10877    doing this instruction unconditionally.
10878    If CODE is 'N' then X is a floating point operand that must be negated
10879    before output.
10880    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10881    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10882 void
10883 arm_print_operand (FILE *stream, rtx x, int code)
10884 {
10885   switch (code)
10886     {
10887     case '@':
10888       fputs (ASM_COMMENT_START, stream);
10889       return;
10890
10891     case '_':
10892       fputs (user_label_prefix, stream);
10893       return;
10894
10895     case '|':
10896       fputs (REGISTER_PREFIX, stream);
10897       return;
10898
10899     case '?':
10900       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10901         {
10902           if (TARGET_THUMB)
10903             {
10904               output_operand_lossage ("predicated Thumb instruction");
10905               break;
10906             }
10907           if (current_insn_predicate != NULL)
10908             {
10909               output_operand_lossage
10910                 ("predicated instruction in conditional sequence");
10911               break;
10912             }
10913
10914           fputs (arm_condition_codes[arm_current_cc], stream);
10915         }
10916       else if (current_insn_predicate)
10917         {
10918           enum arm_cond_code code;
10919
10920           if (TARGET_THUMB)
10921             {
10922               output_operand_lossage ("predicated Thumb instruction");
10923               break;
10924             }
10925
10926           code = get_arm_condition_code (current_insn_predicate);
10927           fputs (arm_condition_codes[code], stream);
10928         }
10929       return;
10930
10931     case 'N':
10932       {
10933         REAL_VALUE_TYPE r;
10934         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10935         r = REAL_VALUE_NEGATE (r);
10936         fprintf (stream, "%s", fp_const_from_val (&r));
10937       }
10938       return;
10939
10940     case 'B':
10941       if (GET_CODE (x) == CONST_INT)
10942         {
10943           HOST_WIDE_INT val;
10944           val = ARM_SIGN_EXTEND (~INTVAL (x));
10945           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10946         }
10947       else
10948         {
10949           putc ('~', stream);
10950           output_addr_const (stream, x);
10951         }
10952       return;
10953
10954     case 'i':
10955       fprintf (stream, "%s", arithmetic_instr (x, 1));
10956       return;
10957
10958     /* Truncate Cirrus shift counts.  */
10959     case 's':
10960       if (GET_CODE (x) == CONST_INT)
10961         {
10962           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10963           return;
10964         }
10965       arm_print_operand (stream, x, 0);
10966       return;
10967
10968     case 'I':
10969       fprintf (stream, "%s", arithmetic_instr (x, 0));
10970       return;
10971
10972     case 'S':
10973       {
10974         HOST_WIDE_INT val;
10975         const char * shift = shift_op (x, &val);
10976
10977         if (shift)
10978           {
10979             fprintf (stream, ", %s ", shift_op (x, &val));
10980             if (val == -1)
10981               arm_print_operand (stream, XEXP (x, 1), 0);
10982             else
10983               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10984           }
10985       }
10986       return;
10987
10988       /* An explanation of the 'Q', 'R' and 'H' register operands:
10989
10990          In a pair of registers containing a DI or DF value the 'Q'
10991          operand returns the register number of the register containing
10992          the least significant part of the value.  The 'R' operand returns
10993          the register number of the register containing the most
10994          significant part of the value.
10995
10996          The 'H' operand returns the higher of the two register numbers.
10997          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10998          same as the 'Q' operand, since the most significant part of the
10999          value is held in the lower number register.  The reverse is true
11000          on systems where WORDS_BIG_ENDIAN is false.
11001
11002          The purpose of these operands is to distinguish between cases
11003          where the endian-ness of the values is important (for example
11004          when they are added together), and cases where the endian-ness
11005          is irrelevant, but the order of register operations is important.
11006          For example when loading a value from memory into a register
11007          pair, the endian-ness does not matter.  Provided that the value
11008          from the lower memory address is put into the lower numbered
11009          register, and the value from the higher address is put into the
11010          higher numbered register, the load will work regardless of whether
11011          the value being loaded is big-wordian or little-wordian.  The
11012          order of the two register loads can matter however, if the address
11013          of the memory location is actually held in one of the registers
11014          being overwritten by the load.  */
11015     case 'Q':
11016       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11017         {
11018           output_operand_lossage ("invalid operand for code '%c'", code);
11019           return;
11020         }
11021
11022       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11023       return;
11024
11025     case 'R':
11026       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11027         {
11028           output_operand_lossage ("invalid operand for code '%c'", code);
11029           return;
11030         }
11031
11032       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11033       return;
11034
11035     case 'H':
11036       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11037         {
11038           output_operand_lossage ("invalid operand for code '%c'", code);
11039           return;
11040         }
11041
11042       asm_fprintf (stream, "%r", REGNO (x) + 1);
11043       return;
11044
11045     case 'm':
11046       asm_fprintf (stream, "%r",
11047                    GET_CODE (XEXP (x, 0)) == REG
11048                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11049       return;
11050
11051     case 'M':
11052       asm_fprintf (stream, "{%r-%r}",
11053                    REGNO (x),
11054                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11055       return;
11056
11057     case 'd':
11058       /* CONST_TRUE_RTX means always -- that's the default.  */
11059       if (x == const_true_rtx)
11060         return;
11061
11062       if (!COMPARISON_P (x))
11063         {
11064           output_operand_lossage ("invalid operand for code '%c'", code);
11065           return;
11066         }
11067
11068       fputs (arm_condition_codes[get_arm_condition_code (x)],
11069              stream);
11070       return;
11071
11072     case 'D':
11073       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
11074          want to do that.  */
11075       if (x == const_true_rtx)
11076         {
11077           output_operand_lossage ("instruction never exectued");
11078           return;
11079         }
11080       if (!COMPARISON_P (x))
11081         {
11082           output_operand_lossage ("invalid operand for code '%c'", code);
11083           return;
11084         }
11085
11086       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11087                                  (get_arm_condition_code (x))],
11088              stream);
11089       return;
11090
11091     /* Cirrus registers can be accessed in a variety of ways:
11092          single floating point (f)
11093          double floating point (d)
11094          32bit integer         (fx)
11095          64bit integer         (dx).  */
11096     case 'W':                   /* Cirrus register in F mode.  */
11097     case 'X':                   /* Cirrus register in D mode.  */
11098     case 'Y':                   /* Cirrus register in FX mode.  */
11099     case 'Z':                   /* Cirrus register in DX mode.  */
11100       gcc_assert (GET_CODE (x) == REG
11101                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11102
11103       fprintf (stream, "mv%s%s",
11104                code == 'W' ? "f"
11105                : code == 'X' ? "d"
11106                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11107
11108       return;
11109
11110     /* Print cirrus register in the mode specified by the register's mode.  */
11111     case 'V':
11112       {
11113         int mode = GET_MODE (x);
11114
11115         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11116           {
11117             output_operand_lossage ("invalid operand for code '%c'", code);
11118             return;
11119           }
11120
11121         fprintf (stream, "mv%s%s",
11122                  mode == DFmode ? "d"
11123                  : mode == SImode ? "fx"
11124                  : mode == DImode ? "dx"
11125                  : "f", reg_names[REGNO (x)] + 2);
11126
11127         return;
11128       }
11129
11130     case 'U':
11131       if (GET_CODE (x) != REG
11132           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11133           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11134         /* Bad value for wCG register number.  */
11135         {
11136           output_operand_lossage ("invalid operand for code '%c'", code);
11137           return;
11138         }
11139
11140       else
11141         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11142       return;
11143
11144       /* Print an iWMMXt control register name.  */
11145     case 'w':
11146       if (GET_CODE (x) != CONST_INT
11147           || INTVAL (x) < 0
11148           || INTVAL (x) >= 16)
11149         /* Bad value for wC register number.  */
11150         {
11151           output_operand_lossage ("invalid operand for code '%c'", code);
11152           return;
11153         }
11154
11155       else
11156         {
11157           static const char * wc_reg_names [16] =
11158             {
11159               "wCID",  "wCon",  "wCSSF", "wCASF",
11160               "wC4",   "wC5",   "wC6",   "wC7",
11161               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11162               "wC12",  "wC13",  "wC14",  "wC15"
11163             };
11164
11165           fprintf (stream, wc_reg_names [INTVAL (x)]);
11166         }
11167       return;
11168
11169       /* Print a VFP double precision register name.  */
11170     case 'P':
11171       {
11172         int mode = GET_MODE (x);
11173         int num;
11174
11175         if (mode != DImode && mode != DFmode)
11176           {
11177             output_operand_lossage ("invalid operand for code '%c'", code);
11178             return;
11179           }
11180
11181         if (GET_CODE (x) != REG
11182             || !IS_VFP_REGNUM (REGNO (x)))
11183           {
11184             output_operand_lossage ("invalid operand for code '%c'", code);
11185             return;
11186           }
11187
11188         num = REGNO(x) - FIRST_VFP_REGNUM;
11189         if (num & 1)
11190           {
11191             output_operand_lossage ("invalid operand for code '%c'", code);
11192             return;
11193           }
11194
11195         fprintf (stream, "d%d", num >> 1);
11196       }
11197       return;
11198
11199     default:
11200       if (x == 0)
11201         {
11202           output_operand_lossage ("missing operand");
11203           return;
11204         }
11205
11206       switch (GET_CODE (x))
11207         {
11208         case REG:
11209           asm_fprintf (stream, "%r", REGNO (x));
11210           break;
11211
11212         case MEM:
11213           output_memory_reference_mode = GET_MODE (x);
11214           output_address (XEXP (x, 0));
11215           break;
11216
11217         case CONST_DOUBLE:
11218           fprintf (stream, "#%s", fp_immediate_constant (x));
11219           break;
11220
11221         default:
11222           gcc_assert (GET_CODE (x) != NEG);
11223           fputc ('#', stream);
11224           output_addr_const (stream, x);
11225           break;
11226         }
11227     }
11228 }
11229 \f
11230 #ifndef AOF_ASSEMBLER
11231 /* Target hook for assembling integer objects.  The ARM version needs to
11232    handle word-sized values specially.  */
11233 static bool
11234 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11235 {
11236   if (size == UNITS_PER_WORD && aligned_p)
11237     {
11238       fputs ("\t.word\t", asm_out_file);
11239       output_addr_const (asm_out_file, x);
11240
11241       /* Mark symbols as position independent.  We only do this in the
11242          .text segment, not in the .data segment.  */
11243       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11244           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11245         {
11246           if (GET_CODE (x) == SYMBOL_REF
11247               && (CONSTANT_POOL_ADDRESS_P (x)
11248                   || SYMBOL_REF_LOCAL_P (x)))
11249             fputs ("(GOTOFF)", asm_out_file);
11250           else if (GET_CODE (x) == LABEL_REF)
11251             fputs ("(GOTOFF)", asm_out_file);
11252           else
11253             fputs ("(GOT)", asm_out_file);
11254         }
11255       fputc ('\n', asm_out_file);
11256       return true;
11257     }
11258
11259   if (arm_vector_mode_supported_p (GET_MODE (x)))
11260     {
11261       int i, units;
11262
11263       gcc_assert (GET_CODE (x) == CONST_VECTOR);
11264
11265       units = CONST_VECTOR_NUNITS (x);
11266
11267       switch (GET_MODE (x))
11268         {
11269         case V2SImode: size = 4; break;
11270         case V4HImode: size = 2; break;
11271         case V8QImode: size = 1; break;
11272         default:
11273           gcc_unreachable ();
11274         }
11275
11276       for (i = 0; i < units; i++)
11277         {
11278           rtx elt;
11279
11280           elt = CONST_VECTOR_ELT (x, i);
11281           assemble_integer
11282             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11283         }
11284
11285       return true;
11286     }
11287
11288   return default_assemble_integer (x, size, aligned_p);
11289 }
11290
11291
11292 /* Add a function to the list of static constructors.  */
11293
11294 static void
11295 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
11296 {
11297   if (!TARGET_AAPCS_BASED)
11298     {
11299       default_named_section_asm_out_constructor (symbol, priority);
11300       return;
11301     }
11302
11303   /* Put these in the .init_array section, using a special relocation.  */
11304   ctors_section ();
11305   assemble_align (POINTER_SIZE);
11306   fputs ("\t.word\t", asm_out_file);
11307   output_addr_const (asm_out_file, symbol);
11308   fputs ("(target1)\n", asm_out_file);
11309 }
11310 #endif
11311 \f
11312 /* A finite state machine takes care of noticing whether or not instructions
11313    can be conditionally executed, and thus decrease execution time and code
11314    size by deleting branch instructions.  The fsm is controlled by
11315    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
11316
11317 /* The state of the fsm controlling condition codes are:
11318    0: normal, do nothing special
11319    1: make ASM_OUTPUT_OPCODE not output this instruction
11320    2: make ASM_OUTPUT_OPCODE not output this instruction
11321    3: make instructions conditional
11322    4: make instructions conditional
11323
11324    State transitions (state->state by whom under condition):
11325    0 -> 1 final_prescan_insn if the `target' is a label
11326    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
11327    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
11328    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
11329    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
11330           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
11331    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
11332           (the target insn is arm_target_insn).
11333
11334    If the jump clobbers the conditions then we use states 2 and 4.
11335
11336    A similar thing can be done with conditional return insns.
11337
11338    XXX In case the `target' is an unconditional branch, this conditionalising
11339    of the instructions always reduces code size, but not always execution
11340    time.  But then, I want to reduce the code size to somewhere near what
11341    /bin/cc produces.  */
11342
11343 /* Returns the index of the ARM condition code string in
11344    `arm_condition_codes'.  COMPARISON should be an rtx like
11345    `(eq (...) (...))'.  */
11346 static enum arm_cond_code
11347 get_arm_condition_code (rtx comparison)
11348 {
11349   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
11350   int code;
11351   enum rtx_code comp_code = GET_CODE (comparison);
11352
11353   if (GET_MODE_CLASS (mode) != MODE_CC)
11354     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
11355                            XEXP (comparison, 1));
11356
11357   switch (mode)
11358     {
11359     case CC_DNEmode: code = ARM_NE; goto dominance;
11360     case CC_DEQmode: code = ARM_EQ; goto dominance;
11361     case CC_DGEmode: code = ARM_GE; goto dominance;
11362     case CC_DGTmode: code = ARM_GT; goto dominance;
11363     case CC_DLEmode: code = ARM_LE; goto dominance;
11364     case CC_DLTmode: code = ARM_LT; goto dominance;
11365     case CC_DGEUmode: code = ARM_CS; goto dominance;
11366     case CC_DGTUmode: code = ARM_HI; goto dominance;
11367     case CC_DLEUmode: code = ARM_LS; goto dominance;
11368     case CC_DLTUmode: code = ARM_CC;
11369
11370     dominance:
11371       gcc_assert (comp_code == EQ || comp_code == NE);
11372
11373       if (comp_code == EQ)
11374         return ARM_INVERSE_CONDITION_CODE (code);
11375       return code;
11376
11377     case CC_NOOVmode:
11378       switch (comp_code)
11379         {
11380         case NE: return ARM_NE;
11381         case EQ: return ARM_EQ;
11382         case GE: return ARM_PL;
11383         case LT: return ARM_MI;
11384         default: gcc_unreachable ();
11385         }
11386
11387     case CC_Zmode:
11388       switch (comp_code)
11389         {
11390         case NE: return ARM_NE;
11391         case EQ: return ARM_EQ;
11392         default: gcc_unreachable ();
11393         }
11394
11395     case CC_Nmode:
11396       switch (comp_code)
11397         {
11398         case NE: return ARM_MI;
11399         case EQ: return ARM_PL;
11400         default: gcc_unreachable ();
11401         }
11402
11403     case CCFPEmode:
11404     case CCFPmode:
11405       /* These encodings assume that AC=1 in the FPA system control
11406          byte.  This allows us to handle all cases except UNEQ and
11407          LTGT.  */
11408       switch (comp_code)
11409         {
11410         case GE: return ARM_GE;
11411         case GT: return ARM_GT;
11412         case LE: return ARM_LS;
11413         case LT: return ARM_MI;
11414         case NE: return ARM_NE;
11415         case EQ: return ARM_EQ;
11416         case ORDERED: return ARM_VC;
11417         case UNORDERED: return ARM_VS;
11418         case UNLT: return ARM_LT;
11419         case UNLE: return ARM_LE;
11420         case UNGT: return ARM_HI;
11421         case UNGE: return ARM_PL;
11422           /* UNEQ and LTGT do not have a representation.  */
11423         case UNEQ: /* Fall through.  */
11424         case LTGT: /* Fall through.  */
11425         default: gcc_unreachable ();
11426         }
11427
11428     case CC_SWPmode:
11429       switch (comp_code)
11430         {
11431         case NE: return ARM_NE;
11432         case EQ: return ARM_EQ;
11433         case GE: return ARM_LE;
11434         case GT: return ARM_LT;
11435         case LE: return ARM_GE;
11436         case LT: return ARM_GT;
11437         case GEU: return ARM_LS;
11438         case GTU: return ARM_CC;
11439         case LEU: return ARM_CS;
11440         case LTU: return ARM_HI;
11441         default: gcc_unreachable ();
11442         }
11443
11444     case CC_Cmode:
11445       switch (comp_code)
11446       {
11447       case LTU: return ARM_CS;
11448       case GEU: return ARM_CC;
11449       default: gcc_unreachable ();
11450       }
11451
11452     case CCmode:
11453       switch (comp_code)
11454         {
11455         case NE: return ARM_NE;
11456         case EQ: return ARM_EQ;
11457         case GE: return ARM_GE;
11458         case GT: return ARM_GT;
11459         case LE: return ARM_LE;
11460         case LT: return ARM_LT;
11461         case GEU: return ARM_CS;
11462         case GTU: return ARM_HI;
11463         case LEU: return ARM_LS;
11464         case LTU: return ARM_CC;
11465         default: gcc_unreachable ();
11466         }
11467
11468     default: gcc_unreachable ();
11469     }
11470 }
11471
11472 void
11473 arm_final_prescan_insn (rtx insn)
11474 {
11475   /* BODY will hold the body of INSN.  */
11476   rtx body = PATTERN (insn);
11477
11478   /* This will be 1 if trying to repeat the trick, and things need to be
11479      reversed if it appears to fail.  */
11480   int reverse = 0;
11481
11482   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11483      taken are clobbered, even if the rtl suggests otherwise.  It also
11484      means that we have to grub around within the jump expression to find
11485      out what the conditions are when the jump isn't taken.  */
11486   int jump_clobbers = 0;
11487
11488   /* If we start with a return insn, we only succeed if we find another one.  */
11489   int seeking_return = 0;
11490
11491   /* START_INSN will hold the insn from where we start looking.  This is the
11492      first insn after the following code_label if REVERSE is true.  */
11493   rtx start_insn = insn;
11494
11495   /* If in state 4, check if the target branch is reached, in order to
11496      change back to state 0.  */
11497   if (arm_ccfsm_state == 4)
11498     {
11499       if (insn == arm_target_insn)
11500         {
11501           arm_target_insn = NULL;
11502           arm_ccfsm_state = 0;
11503         }
11504       return;
11505     }
11506
11507   /* If in state 3, it is possible to repeat the trick, if this insn is an
11508      unconditional branch to a label, and immediately following this branch
11509      is the previous target label which is only used once, and the label this
11510      branch jumps to is not too far off.  */
11511   if (arm_ccfsm_state == 3)
11512     {
11513       if (simplejump_p (insn))
11514         {
11515           start_insn = next_nonnote_insn (start_insn);
11516           if (GET_CODE (start_insn) == BARRIER)
11517             {
11518               /* XXX Isn't this always a barrier?  */
11519               start_insn = next_nonnote_insn (start_insn);
11520             }
11521           if (GET_CODE (start_insn) == CODE_LABEL
11522               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11523               && LABEL_NUSES (start_insn) == 1)
11524             reverse = TRUE;
11525           else
11526             return;
11527         }
11528       else if (GET_CODE (body) == RETURN)
11529         {
11530           start_insn = next_nonnote_insn (start_insn);
11531           if (GET_CODE (start_insn) == BARRIER)
11532             start_insn = next_nonnote_insn (start_insn);
11533           if (GET_CODE (start_insn) == CODE_LABEL
11534               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11535               && LABEL_NUSES (start_insn) == 1)
11536             {
11537               reverse = TRUE;
11538               seeking_return = 1;
11539             }
11540           else
11541             return;
11542         }
11543       else
11544         return;
11545     }
11546
11547   gcc_assert (!arm_ccfsm_state || reverse);
11548   if (GET_CODE (insn) != JUMP_INSN)
11549     return;
11550
11551   /* This jump might be paralleled with a clobber of the condition codes
11552      the jump should always come first */
11553   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11554     body = XVECEXP (body, 0, 0);
11555
11556   if (reverse
11557       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11558           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11559     {
11560       int insns_skipped;
11561       int fail = FALSE, succeed = FALSE;
11562       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11563       int then_not_else = TRUE;
11564       rtx this_insn = start_insn, label = 0;
11565
11566       /* If the jump cannot be done with one instruction, we cannot
11567          conditionally execute the instruction in the inverse case.  */
11568       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11569         {
11570           jump_clobbers = 1;
11571           return;
11572         }
11573
11574       /* Register the insn jumped to.  */
11575       if (reverse)
11576         {
11577           if (!seeking_return)
11578             label = XEXP (SET_SRC (body), 0);
11579         }
11580       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11581         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11582       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11583         {
11584           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11585           then_not_else = FALSE;
11586         }
11587       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11588         seeking_return = 1;
11589       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11590         {
11591           seeking_return = 1;
11592           then_not_else = FALSE;
11593         }
11594       else
11595         gcc_unreachable ();
11596
11597       /* See how many insns this branch skips, and what kind of insns.  If all
11598          insns are okay, and the label or unconditional branch to the same
11599          label is not too far away, succeed.  */
11600       for (insns_skipped = 0;
11601            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11602         {
11603           rtx scanbody;
11604
11605           this_insn = next_nonnote_insn (this_insn);
11606           if (!this_insn)
11607             break;
11608
11609           switch (GET_CODE (this_insn))
11610             {
11611             case CODE_LABEL:
11612               /* Succeed if it is the target label, otherwise fail since
11613                  control falls in from somewhere else.  */
11614               if (this_insn == label)
11615                 {
11616                   if (jump_clobbers)
11617                     {
11618                       arm_ccfsm_state = 2;
11619                       this_insn = next_nonnote_insn (this_insn);
11620                     }
11621                   else
11622                     arm_ccfsm_state = 1;
11623                   succeed = TRUE;
11624                 }
11625               else
11626                 fail = TRUE;
11627               break;
11628
11629             case BARRIER:
11630               /* Succeed if the following insn is the target label.
11631                  Otherwise fail.
11632                  If return insns are used then the last insn in a function
11633                  will be a barrier.  */
11634               this_insn = next_nonnote_insn (this_insn);
11635               if (this_insn && this_insn == label)
11636                 {
11637                   if (jump_clobbers)
11638                     {
11639                       arm_ccfsm_state = 2;
11640                       this_insn = next_nonnote_insn (this_insn);
11641                     }
11642                   else
11643                     arm_ccfsm_state = 1;
11644                   succeed = TRUE;
11645                 }
11646               else
11647                 fail = TRUE;
11648               break;
11649
11650             case CALL_INSN:
11651               /* The AAPCS says that conditional calls should not be
11652                  used since they make interworking inefficient (the
11653                  linker can't transform BL<cond> into BLX).  That's
11654                  only a problem if the machine has BLX.  */
11655               if (arm_arch5)
11656                 {
11657                   fail = TRUE;
11658                   break;
11659                 }
11660
11661               /* Succeed if the following insn is the target label, or
11662                  if the following two insns are a barrier and the
11663                  target label.  */
11664               this_insn = next_nonnote_insn (this_insn);
11665               if (this_insn && GET_CODE (this_insn) == BARRIER)
11666                 this_insn = next_nonnote_insn (this_insn);
11667
11668               if (this_insn && this_insn == label
11669                   && insns_skipped < max_insns_skipped)
11670                 {
11671                   if (jump_clobbers)
11672                     {
11673                       arm_ccfsm_state = 2;
11674                       this_insn = next_nonnote_insn (this_insn);
11675                     }
11676                   else
11677                     arm_ccfsm_state = 1;
11678                   succeed = TRUE;
11679                 }
11680               else
11681                 fail = TRUE;
11682               break;
11683
11684             case JUMP_INSN:
11685               /* If this is an unconditional branch to the same label, succeed.
11686                  If it is to another label, do nothing.  If it is conditional,
11687                  fail.  */
11688               /* XXX Probably, the tests for SET and the PC are
11689                  unnecessary.  */
11690
11691               scanbody = PATTERN (this_insn);
11692               if (GET_CODE (scanbody) == SET
11693                   && GET_CODE (SET_DEST (scanbody)) == PC)
11694                 {
11695                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11696                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11697                     {
11698                       arm_ccfsm_state = 2;
11699                       succeed = TRUE;
11700                     }
11701                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11702                     fail = TRUE;
11703                 }
11704               /* Fail if a conditional return is undesirable (e.g. on a
11705                  StrongARM), but still allow this if optimizing for size.  */
11706               else if (GET_CODE (scanbody) == RETURN
11707                        && !use_return_insn (TRUE, NULL)
11708                        && !optimize_size)
11709                 fail = TRUE;
11710               else if (GET_CODE (scanbody) == RETURN
11711                        && seeking_return)
11712                 {
11713                   arm_ccfsm_state = 2;
11714                   succeed = TRUE;
11715                 }
11716               else if (GET_CODE (scanbody) == PARALLEL)
11717                 {
11718                   switch (get_attr_conds (this_insn))
11719                     {
11720                     case CONDS_NOCOND:
11721                       break;
11722                     default:
11723                       fail = TRUE;
11724                       break;
11725                     }
11726                 }
11727               else
11728                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11729
11730               break;
11731
11732             case INSN:
11733               /* Instructions using or affecting the condition codes make it
11734                  fail.  */
11735               scanbody = PATTERN (this_insn);
11736               if (!(GET_CODE (scanbody) == SET
11737                     || GET_CODE (scanbody) == PARALLEL)
11738                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11739                 fail = TRUE;
11740
11741               /* A conditional cirrus instruction must be followed by
11742                  a non Cirrus instruction.  However, since we
11743                  conditionalize instructions in this function and by
11744                  the time we get here we can't add instructions
11745                  (nops), because shorten_branches() has already been
11746                  called, we will disable conditionalizing Cirrus
11747                  instructions to be safe.  */
11748               if (GET_CODE (scanbody) != USE
11749                   && GET_CODE (scanbody) != CLOBBER
11750                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11751                 fail = TRUE;
11752               break;
11753
11754             default:
11755               break;
11756             }
11757         }
11758       if (succeed)
11759         {
11760           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11761             arm_target_label = CODE_LABEL_NUMBER (label);
11762           else
11763             {
11764               gcc_assert (seeking_return || arm_ccfsm_state == 2);
11765
11766               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11767                 {
11768                   this_insn = next_nonnote_insn (this_insn);
11769                   gcc_assert (!this_insn
11770                               || (GET_CODE (this_insn) != BARRIER
11771                                   && GET_CODE (this_insn) != CODE_LABEL));
11772                 }
11773               if (!this_insn)
11774                 {
11775                   /* Oh, dear! we ran off the end.. give up.  */
11776                   recog (PATTERN (insn), insn, NULL);
11777                   arm_ccfsm_state = 0;
11778                   arm_target_insn = NULL;
11779                   return;
11780                 }
11781               arm_target_insn = this_insn;
11782             }
11783           if (jump_clobbers)
11784             {
11785               gcc_assert (!reverse);
11786               arm_current_cc =
11787                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11788                                                             0), 0), 1));
11789               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11790                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11791               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11792                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11793             }
11794           else
11795             {
11796               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11797                  what it was.  */
11798               if (!reverse)
11799                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11800                                                                0));
11801             }
11802
11803           if (reverse || then_not_else)
11804             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11805         }
11806
11807       /* Restore recog_data (getting the attributes of other insns can
11808          destroy this array, but final.c assumes that it remains intact
11809          across this call; since the insn has been recognized already we
11810          call recog direct).  */
11811       recog (PATTERN (insn), insn, NULL);
11812     }
11813 }
11814
11815 /* Returns true if REGNO is a valid register
11816    for holding a quantity of type MODE.  */
11817 int
11818 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11819 {
11820   if (GET_MODE_CLASS (mode) == MODE_CC)
11821     return (regno == CC_REGNUM
11822             || (TARGET_HARD_FLOAT && TARGET_VFP
11823                 && regno == VFPCC_REGNUM));
11824
11825   if (TARGET_THUMB)
11826     /* For the Thumb we only allow values bigger than SImode in
11827        registers 0 - 6, so that there is always a second low
11828        register available to hold the upper part of the value.
11829        We probably we ought to ensure that the register is the
11830        start of an even numbered register pair.  */
11831     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11832
11833   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
11834       && IS_CIRRUS_REGNUM (regno))
11835     /* We have outlawed SI values in Cirrus registers because they
11836        reside in the lower 32 bits, but SF values reside in the
11837        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11838        even split the registers into pairs because Cirrus SI values
11839        get sign extended to 64bits-- aldyh.  */
11840     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11841
11842   if (TARGET_HARD_FLOAT && TARGET_VFP
11843       && IS_VFP_REGNUM (regno))
11844     {
11845       if (mode == SFmode || mode == SImode)
11846         return TRUE;
11847
11848       /* DFmode values are only valid in even register pairs.  */
11849       if (mode == DFmode)
11850         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11851       return FALSE;
11852     }
11853
11854   if (TARGET_REALLY_IWMMXT)
11855     {
11856       if (IS_IWMMXT_GR_REGNUM (regno))
11857         return mode == SImode;
11858
11859       if (IS_IWMMXT_REGNUM (regno))
11860         return VALID_IWMMXT_REG_MODE (mode);
11861     }
11862   
11863   /* We allow any value to be stored in the general registers.
11864      Restrict doubleword quantities to even register pairs so that we can
11865      use ldrd.  */
11866   if (regno <= LAST_ARM_REGNUM)
11867     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11868
11869   if (regno == FRAME_POINTER_REGNUM
11870       || regno == ARG_POINTER_REGNUM)
11871     /* We only allow integers in the fake hard registers.  */
11872     return GET_MODE_CLASS (mode) == MODE_INT;
11873
11874   /* The only registers left are the FPA registers
11875      which we only allow to hold FP values.  */
11876   return (TARGET_HARD_FLOAT && TARGET_FPA
11877           && GET_MODE_CLASS (mode) == MODE_FLOAT
11878           && regno >= FIRST_FPA_REGNUM
11879           && regno <= LAST_FPA_REGNUM);
11880 }
11881
11882 int
11883 arm_regno_class (int regno)
11884 {
11885   if (TARGET_THUMB)
11886     {
11887       if (regno == STACK_POINTER_REGNUM)
11888         return STACK_REG;
11889       if (regno == CC_REGNUM)
11890         return CC_REG;
11891       if (regno < 8)
11892         return LO_REGS;
11893       return HI_REGS;
11894     }
11895
11896   if (   regno <= LAST_ARM_REGNUM
11897       || regno == FRAME_POINTER_REGNUM
11898       || regno == ARG_POINTER_REGNUM)
11899     return GENERAL_REGS;
11900
11901   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11902     return NO_REGS;
11903
11904   if (IS_CIRRUS_REGNUM (regno))
11905     return CIRRUS_REGS;
11906
11907   if (IS_VFP_REGNUM (regno))
11908     return VFP_REGS;
11909
11910   if (IS_IWMMXT_REGNUM (regno))
11911     return IWMMXT_REGS;
11912
11913   if (IS_IWMMXT_GR_REGNUM (regno))
11914     return IWMMXT_GR_REGS;
11915
11916   return FPA_REGS;
11917 }
11918
11919 /* Handle a special case when computing the offset
11920    of an argument from the frame pointer.  */
11921 int
11922 arm_debugger_arg_offset (int value, rtx addr)
11923 {
11924   rtx insn;
11925
11926   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11927   if (value != 0)
11928     return 0;
11929
11930   /* We can only cope with the case where the address is held in a register.  */
11931   if (GET_CODE (addr) != REG)
11932     return 0;
11933
11934   /* If we are using the frame pointer to point at the argument, then
11935      an offset of 0 is correct.  */
11936   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11937     return 0;
11938
11939   /* If we are using the stack pointer to point at the
11940      argument, then an offset of 0 is correct.  */
11941   if ((TARGET_THUMB || !frame_pointer_needed)
11942       && REGNO (addr) == SP_REGNUM)
11943     return 0;
11944
11945   /* Oh dear.  The argument is pointed to by a register rather
11946      than being held in a register, or being stored at a known
11947      offset from the frame pointer.  Since GDB only understands
11948      those two kinds of argument we must translate the address
11949      held in the register into an offset from the frame pointer.
11950      We do this by searching through the insns for the function
11951      looking to see where this register gets its value.  If the
11952      register is initialized from the frame pointer plus an offset
11953      then we are in luck and we can continue, otherwise we give up.
11954
11955      This code is exercised by producing debugging information
11956      for a function with arguments like this:
11957
11958            double func (double a, double b, int c, double d) {return d;}
11959
11960      Without this code the stab for parameter 'd' will be set to
11961      an offset of 0 from the frame pointer, rather than 8.  */
11962
11963   /* The if() statement says:
11964
11965      If the insn is a normal instruction
11966      and if the insn is setting the value in a register
11967      and if the register being set is the register holding the address of the argument
11968      and if the address is computing by an addition
11969      that involves adding to a register
11970      which is the frame pointer
11971      a constant integer
11972
11973      then...  */
11974
11975   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11976     {
11977       if (   GET_CODE (insn) == INSN
11978           && GET_CODE (PATTERN (insn)) == SET
11979           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11980           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11981           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11982           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11983           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11984              )
11985         {
11986           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11987
11988           break;
11989         }
11990     }
11991
11992   if (value == 0)
11993     {
11994       debug_rtx (addr);
11995       warning (0, "unable to compute real location of stacked parameter");
11996       value = 8; /* XXX magic hack */
11997     }
11998
11999   return value;
12000 }
12001 \f
12002 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
12003   do                                                                    \
12004     {                                                                   \
12005       if ((MASK) & insn_flags)                                          \
12006         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
12007                                      BUILT_IN_MD, NULL, NULL_TREE);     \
12008     }                                                                   \
12009   while (0)
12010
12011 struct builtin_description
12012 {
12013   const unsigned int       mask;
12014   const enum insn_code     icode;
12015   const char * const       name;
12016   const enum arm_builtins  code;
12017   const enum rtx_code      comparison;
12018   const unsigned int       flag;
12019 };
12020
12021 static const struct builtin_description bdesc_2arg[] =
12022 {
12023 #define IWMMXT_BUILTIN(code, string, builtin) \
12024   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12025     ARM_BUILTIN_##builtin, 0, 0 },
12026
12027   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12028   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12029   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12030   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12031   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12032   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12033   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12034   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12035   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12036   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12037   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12038   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12039   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12040   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12041   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12042   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12043   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12044   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12045   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12046   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12047   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12048   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12049   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12050   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12051   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12052   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12053   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12054   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12055   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12056   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12057   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12058   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12059   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12060   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12061   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12062   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12063   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12064   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12065   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12066   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12067   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12068   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12069   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12070   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12071   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12072   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12073   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12074   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12075   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12076   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12077   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12078   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12079   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12080   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12081   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12082   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12083   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12084   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12085
12086 #define IWMMXT_BUILTIN2(code, builtin) \
12087   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12088
12089   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12090   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12091   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12092   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12093   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12094   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12095   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
12096   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
12097   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
12098   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
12099   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
12100   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
12101   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
12102   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
12103   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
12104   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
12105   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
12106   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
12107   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
12108   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
12109   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
12110   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
12111   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
12112   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
12113   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
12114   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
12115   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
12116   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
12117   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
12118   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
12119   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
12120   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
12121 };
12122
12123 static const struct builtin_description bdesc_1arg[] =
12124 {
12125   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12126   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12127   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12128   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12129   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12130   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12131   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12132   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12133   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12134   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12135   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12136   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12137   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12138   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12139   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12140   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12141   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12142   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12143 };
12144
12145 /* Set up all the iWMMXt builtins.  This is
12146    not called if TARGET_IWMMXT is zero.  */
12147
12148 static void
12149 arm_init_iwmmxt_builtins (void)
12150 {
12151   const struct builtin_description * d;
12152   size_t i;
12153   tree endlink = void_list_node;
12154
12155   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12156   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12157   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12158
12159   tree int_ftype_int
12160     = build_function_type (integer_type_node,
12161                            tree_cons (NULL_TREE, integer_type_node, endlink));
12162   tree v8qi_ftype_v8qi_v8qi_int
12163     = build_function_type (V8QI_type_node,
12164                            tree_cons (NULL_TREE, V8QI_type_node,
12165                                       tree_cons (NULL_TREE, V8QI_type_node,
12166                                                  tree_cons (NULL_TREE,
12167                                                             integer_type_node,
12168                                                             endlink))));
12169   tree v4hi_ftype_v4hi_int
12170     = build_function_type (V4HI_type_node,
12171                            tree_cons (NULL_TREE, V4HI_type_node,
12172                                       tree_cons (NULL_TREE, integer_type_node,
12173                                                  endlink)));
12174   tree v2si_ftype_v2si_int
12175     = build_function_type (V2SI_type_node,
12176                            tree_cons (NULL_TREE, V2SI_type_node,
12177                                       tree_cons (NULL_TREE, integer_type_node,
12178                                                  endlink)));
12179   tree v2si_ftype_di_di
12180     = build_function_type (V2SI_type_node,
12181                            tree_cons (NULL_TREE, long_long_integer_type_node,
12182                                       tree_cons (NULL_TREE, long_long_integer_type_node,
12183                                                  endlink)));
12184   tree di_ftype_di_int
12185     = build_function_type (long_long_integer_type_node,
12186                            tree_cons (NULL_TREE, long_long_integer_type_node,
12187                                       tree_cons (NULL_TREE, integer_type_node,
12188                                                  endlink)));
12189   tree di_ftype_di_int_int
12190     = build_function_type (long_long_integer_type_node,
12191                            tree_cons (NULL_TREE, long_long_integer_type_node,
12192                                       tree_cons (NULL_TREE, integer_type_node,
12193                                                  tree_cons (NULL_TREE,
12194                                                             integer_type_node,
12195                                                             endlink))));
12196   tree int_ftype_v8qi
12197     = build_function_type (integer_type_node,
12198                            tree_cons (NULL_TREE, V8QI_type_node,
12199                                       endlink));
12200   tree int_ftype_v4hi
12201     = build_function_type (integer_type_node,
12202                            tree_cons (NULL_TREE, V4HI_type_node,
12203                                       endlink));
12204   tree int_ftype_v2si
12205     = build_function_type (integer_type_node,
12206                            tree_cons (NULL_TREE, V2SI_type_node,
12207                                       endlink));
12208   tree int_ftype_v8qi_int
12209     = build_function_type (integer_type_node,
12210                            tree_cons (NULL_TREE, V8QI_type_node,
12211                                       tree_cons (NULL_TREE, integer_type_node,
12212                                                  endlink)));
12213   tree int_ftype_v4hi_int
12214     = build_function_type (integer_type_node,
12215                            tree_cons (NULL_TREE, V4HI_type_node,
12216                                       tree_cons (NULL_TREE, integer_type_node,
12217                                                  endlink)));
12218   tree int_ftype_v2si_int
12219     = build_function_type (integer_type_node,
12220                            tree_cons (NULL_TREE, V2SI_type_node,
12221                                       tree_cons (NULL_TREE, integer_type_node,
12222                                                  endlink)));
12223   tree v8qi_ftype_v8qi_int_int
12224     = build_function_type (V8QI_type_node,
12225                            tree_cons (NULL_TREE, V8QI_type_node,
12226                                       tree_cons (NULL_TREE, integer_type_node,
12227                                                  tree_cons (NULL_TREE,
12228                                                             integer_type_node,
12229                                                             endlink))));
12230   tree v4hi_ftype_v4hi_int_int
12231     = build_function_type (V4HI_type_node,
12232                            tree_cons (NULL_TREE, V4HI_type_node,
12233                                       tree_cons (NULL_TREE, integer_type_node,
12234                                                  tree_cons (NULL_TREE,
12235                                                             integer_type_node,
12236                                                             endlink))));
12237   tree v2si_ftype_v2si_int_int
12238     = build_function_type (V2SI_type_node,
12239                            tree_cons (NULL_TREE, V2SI_type_node,
12240                                       tree_cons (NULL_TREE, integer_type_node,
12241                                                  tree_cons (NULL_TREE,
12242                                                             integer_type_node,
12243                                                             endlink))));
12244   /* Miscellaneous.  */
12245   tree v8qi_ftype_v4hi_v4hi
12246     = build_function_type (V8QI_type_node,
12247                            tree_cons (NULL_TREE, V4HI_type_node,
12248                                       tree_cons (NULL_TREE, V4HI_type_node,
12249                                                  endlink)));
12250   tree v4hi_ftype_v2si_v2si
12251     = build_function_type (V4HI_type_node,
12252                            tree_cons (NULL_TREE, V2SI_type_node,
12253                                       tree_cons (NULL_TREE, V2SI_type_node,
12254                                                  endlink)));
12255   tree v2si_ftype_v4hi_v4hi
12256     = build_function_type (V2SI_type_node,
12257                            tree_cons (NULL_TREE, V4HI_type_node,
12258                                       tree_cons (NULL_TREE, V4HI_type_node,
12259                                                  endlink)));
12260   tree v2si_ftype_v8qi_v8qi
12261     = build_function_type (V2SI_type_node,
12262                            tree_cons (NULL_TREE, V8QI_type_node,
12263                                       tree_cons (NULL_TREE, V8QI_type_node,
12264                                                  endlink)));
12265   tree v4hi_ftype_v4hi_di
12266     = build_function_type (V4HI_type_node,
12267                            tree_cons (NULL_TREE, V4HI_type_node,
12268                                       tree_cons (NULL_TREE,
12269                                                  long_long_integer_type_node,
12270                                                  endlink)));
12271   tree v2si_ftype_v2si_di
12272     = build_function_type (V2SI_type_node,
12273                            tree_cons (NULL_TREE, V2SI_type_node,
12274                                       tree_cons (NULL_TREE,
12275                                                  long_long_integer_type_node,
12276                                                  endlink)));
12277   tree void_ftype_int_int
12278     = build_function_type (void_type_node,
12279                            tree_cons (NULL_TREE, integer_type_node,
12280                                       tree_cons (NULL_TREE, integer_type_node,
12281                                                  endlink)));
12282   tree di_ftype_void
12283     = build_function_type (long_long_unsigned_type_node, endlink);
12284   tree di_ftype_v8qi
12285     = build_function_type (long_long_integer_type_node,
12286                            tree_cons (NULL_TREE, V8QI_type_node,
12287                                       endlink));
12288   tree di_ftype_v4hi
12289     = build_function_type (long_long_integer_type_node,
12290                            tree_cons (NULL_TREE, V4HI_type_node,
12291                                       endlink));
12292   tree di_ftype_v2si
12293     = build_function_type (long_long_integer_type_node,
12294                            tree_cons (NULL_TREE, V2SI_type_node,
12295                                       endlink));
12296   tree v2si_ftype_v4hi
12297     = build_function_type (V2SI_type_node,
12298                            tree_cons (NULL_TREE, V4HI_type_node,
12299                                       endlink));
12300   tree v4hi_ftype_v8qi
12301     = build_function_type (V4HI_type_node,
12302                            tree_cons (NULL_TREE, V8QI_type_node,
12303                                       endlink));
12304
12305   tree di_ftype_di_v4hi_v4hi
12306     = build_function_type (long_long_unsigned_type_node,
12307                            tree_cons (NULL_TREE,
12308                                       long_long_unsigned_type_node,
12309                                       tree_cons (NULL_TREE, V4HI_type_node,
12310                                                  tree_cons (NULL_TREE,
12311                                                             V4HI_type_node,
12312                                                             endlink))));
12313
12314   tree di_ftype_v4hi_v4hi
12315     = build_function_type (long_long_unsigned_type_node,
12316                            tree_cons (NULL_TREE, V4HI_type_node,
12317                                       tree_cons (NULL_TREE, V4HI_type_node,
12318                                                  endlink)));
12319
12320   /* Normal vector binops.  */
12321   tree v8qi_ftype_v8qi_v8qi
12322     = build_function_type (V8QI_type_node,
12323                            tree_cons (NULL_TREE, V8QI_type_node,
12324                                       tree_cons (NULL_TREE, V8QI_type_node,
12325                                                  endlink)));
12326   tree v4hi_ftype_v4hi_v4hi
12327     = build_function_type (V4HI_type_node,
12328                            tree_cons (NULL_TREE, V4HI_type_node,
12329                                       tree_cons (NULL_TREE, V4HI_type_node,
12330                                                  endlink)));
12331   tree v2si_ftype_v2si_v2si
12332     = build_function_type (V2SI_type_node,
12333                            tree_cons (NULL_TREE, V2SI_type_node,
12334                                       tree_cons (NULL_TREE, V2SI_type_node,
12335                                                  endlink)));
12336   tree di_ftype_di_di
12337     = build_function_type (long_long_unsigned_type_node,
12338                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
12339                                       tree_cons (NULL_TREE,
12340                                                  long_long_unsigned_type_node,
12341                                                  endlink)));
12342
12343   /* Add all builtins that are more or less simple operations on two
12344      operands.  */
12345   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12346     {
12347       /* Use one of the operands; the target can have a different mode for
12348          mask-generating compares.  */
12349       enum machine_mode mode;
12350       tree type;
12351
12352       if (d->name == 0)
12353         continue;
12354
12355       mode = insn_data[d->icode].operand[1].mode;
12356
12357       switch (mode)
12358         {
12359         case V8QImode:
12360           type = v8qi_ftype_v8qi_v8qi;
12361           break;
12362         case V4HImode:
12363           type = v4hi_ftype_v4hi_v4hi;
12364           break;
12365         case V2SImode:
12366           type = v2si_ftype_v2si_v2si;
12367           break;
12368         case DImode:
12369           type = di_ftype_di_di;
12370           break;
12371
12372         default:
12373           gcc_unreachable ();
12374         }
12375
12376       def_mbuiltin (d->mask, d->name, type, d->code);
12377     }
12378
12379   /* Add the remaining MMX insns with somewhat more complicated types.  */
12380   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
12381   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
12382   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
12383
12384   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
12385   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
12386   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
12387   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
12388   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
12389   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
12390
12391   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
12392   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
12393   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
12394   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
12395   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
12396   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
12397
12398   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
12399   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
12400   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
12401   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
12402   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
12403   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
12404
12405   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
12406   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
12407   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
12408   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12409   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12410   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12411
12412   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12413
12414   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12415   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12416   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12417   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12418
12419   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12420   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12421   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12422   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12423   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12424   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12425   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12426   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12427   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12428
12429   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12430   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12431   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12432
12433   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12434   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12435   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12436
12437   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12438   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12439   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12440   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12441   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12442   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12443
12444   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12445   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12446   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12447   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12448   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12449   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12450   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12451   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12452   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12453   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12454   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12455   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12456
12457   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12458   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12459   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12460   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12461
12462   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12463   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12464   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12465   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12466   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12467   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12468   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12469 }
12470
12471 static void
12472 arm_init_tls_builtins (void)
12473 {
12474   tree ftype;
12475   tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
12476   tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
12477
12478   ftype = build_function_type (ptr_type_node, void_list_node);
12479   lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
12480                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
12481                                NULL, const_nothrow);
12482 }
12483
12484 static void
12485 arm_init_builtins (void)
12486 {
12487   arm_init_tls_builtins ();
12488
12489   if (TARGET_REALLY_IWMMXT)
12490     arm_init_iwmmxt_builtins ();
12491 }
12492
12493 /* Errors in the source file can cause expand_expr to return const0_rtx
12494    where we expect a vector.  To avoid crashing, use one of the vector
12495    clear instructions.  */
12496
12497 static rtx
12498 safe_vector_operand (rtx x, enum machine_mode mode)
12499 {
12500   if (x != const0_rtx)
12501     return x;
12502   x = gen_reg_rtx (mode);
12503
12504   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12505                                : gen_rtx_SUBREG (DImode, x, 0)));
12506   return x;
12507 }
12508
12509 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12510
12511 static rtx
12512 arm_expand_binop_builtin (enum insn_code icode,
12513                           tree arglist, rtx target)
12514 {
12515   rtx pat;
12516   tree arg0 = TREE_VALUE (arglist);
12517   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12518   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12519   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12520   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12521   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12522   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12523
12524   if (VECTOR_MODE_P (mode0))
12525     op0 = safe_vector_operand (op0, mode0);
12526   if (VECTOR_MODE_P (mode1))
12527     op1 = safe_vector_operand (op1, mode1);
12528
12529   if (! target
12530       || GET_MODE (target) != tmode
12531       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12532     target = gen_reg_rtx (tmode);
12533
12534   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12535
12536   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12537     op0 = copy_to_mode_reg (mode0, op0);
12538   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12539     op1 = copy_to_mode_reg (mode1, op1);
12540
12541   pat = GEN_FCN (icode) (target, op0, op1);
12542   if (! pat)
12543     return 0;
12544   emit_insn (pat);
12545   return target;
12546 }
12547
12548 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12549
12550 static rtx
12551 arm_expand_unop_builtin (enum insn_code icode,
12552                          tree arglist, rtx target, int do_load)
12553 {
12554   rtx pat;
12555   tree arg0 = TREE_VALUE (arglist);
12556   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12557   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12558   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12559
12560   if (! target
12561       || GET_MODE (target) != tmode
12562       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12563     target = gen_reg_rtx (tmode);
12564   if (do_load)
12565     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12566   else
12567     {
12568       if (VECTOR_MODE_P (mode0))
12569         op0 = safe_vector_operand (op0, mode0);
12570
12571       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12572         op0 = copy_to_mode_reg (mode0, op0);
12573     }
12574
12575   pat = GEN_FCN (icode) (target, op0);
12576   if (! pat)
12577     return 0;
12578   emit_insn (pat);
12579   return target;
12580 }
12581
12582 /* Expand an expression EXP that calls a built-in function,
12583    with result going to TARGET if that's convenient
12584    (and in mode MODE if that's convenient).
12585    SUBTARGET may be used as the target for computing one of EXP's operands.
12586    IGNORE is nonzero if the value is to be ignored.  */
12587
12588 static rtx
12589 arm_expand_builtin (tree exp,
12590                     rtx target,
12591                     rtx subtarget ATTRIBUTE_UNUSED,
12592                     enum machine_mode mode ATTRIBUTE_UNUSED,
12593                     int ignore ATTRIBUTE_UNUSED)
12594 {
12595   const struct builtin_description * d;
12596   enum insn_code    icode;
12597   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12598   tree              arglist = TREE_OPERAND (exp, 1);
12599   tree              arg0;
12600   tree              arg1;
12601   tree              arg2;
12602   rtx               op0;
12603   rtx               op1;
12604   rtx               op2;
12605   rtx               pat;
12606   int               fcode = DECL_FUNCTION_CODE (fndecl);
12607   size_t            i;
12608   enum machine_mode tmode;
12609   enum machine_mode mode0;
12610   enum machine_mode mode1;
12611   enum machine_mode mode2;
12612
12613   switch (fcode)
12614     {
12615     case ARM_BUILTIN_TEXTRMSB:
12616     case ARM_BUILTIN_TEXTRMUB:
12617     case ARM_BUILTIN_TEXTRMSH:
12618     case ARM_BUILTIN_TEXTRMUH:
12619     case ARM_BUILTIN_TEXTRMSW:
12620     case ARM_BUILTIN_TEXTRMUW:
12621       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12622                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12623                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12624                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12625                : CODE_FOR_iwmmxt_textrmw);
12626
12627       arg0 = TREE_VALUE (arglist);
12628       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12629       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12630       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12631       tmode = insn_data[icode].operand[0].mode;
12632       mode0 = insn_data[icode].operand[1].mode;
12633       mode1 = insn_data[icode].operand[2].mode;
12634
12635       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12636         op0 = copy_to_mode_reg (mode0, op0);
12637       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12638         {
12639           /* @@@ better error message */
12640           error ("selector must be an immediate");
12641           return gen_reg_rtx (tmode);
12642         }
12643       if (target == 0
12644           || GET_MODE (target) != tmode
12645           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12646         target = gen_reg_rtx (tmode);
12647       pat = GEN_FCN (icode) (target, op0, op1);
12648       if (! pat)
12649         return 0;
12650       emit_insn (pat);
12651       return target;
12652
12653     case ARM_BUILTIN_TINSRB:
12654     case ARM_BUILTIN_TINSRH:
12655     case ARM_BUILTIN_TINSRW:
12656       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12657                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12658                : CODE_FOR_iwmmxt_tinsrw);
12659       arg0 = TREE_VALUE (arglist);
12660       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12661       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12662       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12663       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12664       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12665       tmode = insn_data[icode].operand[0].mode;
12666       mode0 = insn_data[icode].operand[1].mode;
12667       mode1 = insn_data[icode].operand[2].mode;
12668       mode2 = insn_data[icode].operand[3].mode;
12669
12670       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12671         op0 = copy_to_mode_reg (mode0, op0);
12672       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12673         op1 = copy_to_mode_reg (mode1, op1);
12674       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12675         {
12676           /* @@@ better error message */
12677           error ("selector must be an immediate");
12678           return const0_rtx;
12679         }
12680       if (target == 0
12681           || GET_MODE (target) != tmode
12682           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12683         target = gen_reg_rtx (tmode);
12684       pat = GEN_FCN (icode) (target, op0, op1, op2);
12685       if (! pat)
12686         return 0;
12687       emit_insn (pat);
12688       return target;
12689
12690     case ARM_BUILTIN_SETWCX:
12691       arg0 = TREE_VALUE (arglist);
12692       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12693       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12694       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12695       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12696       return 0;
12697
12698     case ARM_BUILTIN_GETWCX:
12699       arg0 = TREE_VALUE (arglist);
12700       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12701       target = gen_reg_rtx (SImode);
12702       emit_insn (gen_iwmmxt_tmrc (target, op0));
12703       return target;
12704
12705     case ARM_BUILTIN_WSHUFH:
12706       icode = CODE_FOR_iwmmxt_wshufh;
12707       arg0 = TREE_VALUE (arglist);
12708       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12709       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12710       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12711       tmode = insn_data[icode].operand[0].mode;
12712       mode1 = insn_data[icode].operand[1].mode;
12713       mode2 = insn_data[icode].operand[2].mode;
12714
12715       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12716         op0 = copy_to_mode_reg (mode1, op0);
12717       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12718         {
12719           /* @@@ better error message */
12720           error ("mask must be an immediate");
12721           return const0_rtx;
12722         }
12723       if (target == 0
12724           || GET_MODE (target) != tmode
12725           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12726         target = gen_reg_rtx (tmode);
12727       pat = GEN_FCN (icode) (target, op0, op1);
12728       if (! pat)
12729         return 0;
12730       emit_insn (pat);
12731       return target;
12732
12733     case ARM_BUILTIN_WSADB:
12734       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12735     case ARM_BUILTIN_WSADH:
12736       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12737     case ARM_BUILTIN_WSADBZ:
12738       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12739     case ARM_BUILTIN_WSADHZ:
12740       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12741
12742       /* Several three-argument builtins.  */
12743     case ARM_BUILTIN_WMACS:
12744     case ARM_BUILTIN_WMACU:
12745     case ARM_BUILTIN_WALIGN:
12746     case ARM_BUILTIN_TMIA:
12747     case ARM_BUILTIN_TMIAPH:
12748     case ARM_BUILTIN_TMIATT:
12749     case ARM_BUILTIN_TMIATB:
12750     case ARM_BUILTIN_TMIABT:
12751     case ARM_BUILTIN_TMIABB:
12752       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12753                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12754                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12755                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12756                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12757                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12758                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12759                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12760                : CODE_FOR_iwmmxt_walign);
12761       arg0 = TREE_VALUE (arglist);
12762       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12763       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12764       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12765       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12766       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12767       tmode = insn_data[icode].operand[0].mode;
12768       mode0 = insn_data[icode].operand[1].mode;
12769       mode1 = insn_data[icode].operand[2].mode;
12770       mode2 = insn_data[icode].operand[3].mode;
12771
12772       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12773         op0 = copy_to_mode_reg (mode0, op0);
12774       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12775         op1 = copy_to_mode_reg (mode1, op1);
12776       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12777         op2 = copy_to_mode_reg (mode2, op2);
12778       if (target == 0
12779           || GET_MODE (target) != tmode
12780           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12781         target = gen_reg_rtx (tmode);
12782       pat = GEN_FCN (icode) (target, op0, op1, op2);
12783       if (! pat)
12784         return 0;
12785       emit_insn (pat);
12786       return target;
12787
12788     case ARM_BUILTIN_WZERO:
12789       target = gen_reg_rtx (DImode);
12790       emit_insn (gen_iwmmxt_clrdi (target));
12791       return target;
12792
12793     case ARM_BUILTIN_THREAD_POINTER:
12794       return arm_load_tp (target);
12795
12796     default:
12797       break;
12798     }
12799
12800   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12801     if (d->code == (const enum arm_builtins) fcode)
12802       return arm_expand_binop_builtin (d->icode, arglist, target);
12803
12804   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12805     if (d->code == (const enum arm_builtins) fcode)
12806       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12807
12808   /* @@@ Should really do something sensible here.  */
12809   return NULL_RTX;
12810 }
12811 \f
12812 /* Return the number (counting from 0) of
12813    the least significant set bit in MASK.  */
12814
12815 inline static int
12816 number_of_first_bit_set (unsigned mask)
12817 {
12818   int bit;
12819
12820   for (bit = 0;
12821        (mask & (1 << bit)) == 0;
12822        ++bit)
12823     continue;
12824
12825   return bit;
12826 }
12827
12828 /* Emit code to push or pop registers to or from the stack.  F is the
12829    assembly file.  MASK is the registers to push or pop.  PUSH is
12830    nonzero if we should push, and zero if we should pop.  For debugging
12831    output, if pushing, adjust CFA_OFFSET by the amount of space added
12832    to the stack.  REAL_REGS should have the same number of bits set as
12833    MASK, and will be used instead (in the same order) to describe which
12834    registers were saved - this is used to mark the save slots when we
12835    push high registers after moving them to low registers.  */
12836 static void
12837 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12838                unsigned long real_regs)
12839 {
12840   int regno;
12841   int lo_mask = mask & 0xFF;
12842   int pushed_words = 0;
12843
12844   gcc_assert (mask);
12845
12846   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12847     {
12848       /* Special case.  Do not generate a POP PC statement here, do it in
12849          thumb_exit() */
12850       thumb_exit (f, -1);
12851       return;
12852     }
12853
12854   if (ARM_EABI_UNWIND_TABLES && push)
12855     {
12856       fprintf (f, "\t.save\t{");
12857       for (regno = 0; regno < 15; regno++)
12858         {
12859           if (real_regs & (1 << regno))
12860             {
12861               if (real_regs & ((1 << regno) -1))
12862                 fprintf (f, ", ");
12863               asm_fprintf (f, "%r", regno);
12864             }
12865         }
12866       fprintf (f, "}\n");
12867     }
12868
12869   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12870
12871   /* Look at the low registers first.  */
12872   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12873     {
12874       if (lo_mask & 1)
12875         {
12876           asm_fprintf (f, "%r", regno);
12877
12878           if ((lo_mask & ~1) != 0)
12879             fprintf (f, ", ");
12880
12881           pushed_words++;
12882         }
12883     }
12884
12885   if (push && (mask & (1 << LR_REGNUM)))
12886     {
12887       /* Catch pushing the LR.  */
12888       if (mask & 0xFF)
12889         fprintf (f, ", ");
12890
12891       asm_fprintf (f, "%r", LR_REGNUM);
12892
12893       pushed_words++;
12894     }
12895   else if (!push && (mask & (1 << PC_REGNUM)))
12896     {
12897       /* Catch popping the PC.  */
12898       if (TARGET_INTERWORK || TARGET_BACKTRACE
12899           || current_function_calls_eh_return)
12900         {
12901           /* The PC is never poped directly, instead
12902              it is popped into r3 and then BX is used.  */
12903           fprintf (f, "}\n");
12904
12905           thumb_exit (f, -1);
12906
12907           return;
12908         }
12909       else
12910         {
12911           if (mask & 0xFF)
12912             fprintf (f, ", ");
12913
12914           asm_fprintf (f, "%r", PC_REGNUM);
12915         }
12916     }
12917
12918   fprintf (f, "}\n");
12919
12920   if (push && pushed_words && dwarf2out_do_frame ())
12921     {
12922       char *l = dwarf2out_cfi_label ();
12923       int pushed_mask = real_regs;
12924
12925       *cfa_offset += pushed_words * 4;
12926       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12927
12928       pushed_words = 0;
12929       pushed_mask = real_regs;
12930       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12931         {
12932           if (pushed_mask & 1)
12933             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12934         }
12935     }
12936 }
12937
12938 /* Generate code to return from a thumb function.
12939    If 'reg_containing_return_addr' is -1, then the return address is
12940    actually on the stack, at the stack pointer.  */
12941 static void
12942 thumb_exit (FILE *f, int reg_containing_return_addr)
12943 {
12944   unsigned regs_available_for_popping;
12945   unsigned regs_to_pop;
12946   int pops_needed;
12947   unsigned available;
12948   unsigned required;
12949   int mode;
12950   int size;
12951   int restore_a4 = FALSE;
12952
12953   /* Compute the registers we need to pop.  */
12954   regs_to_pop = 0;
12955   pops_needed = 0;
12956
12957   if (reg_containing_return_addr == -1)
12958     {
12959       regs_to_pop |= 1 << LR_REGNUM;
12960       ++pops_needed;
12961     }
12962
12963   if (TARGET_BACKTRACE)
12964     {
12965       /* Restore the (ARM) frame pointer and stack pointer.  */
12966       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12967       pops_needed += 2;
12968     }
12969
12970   /* If there is nothing to pop then just emit the BX instruction and
12971      return.  */
12972   if (pops_needed == 0)
12973     {
12974       if (current_function_calls_eh_return)
12975         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12976
12977       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12978       return;
12979     }
12980   /* Otherwise if we are not supporting interworking and we have not created
12981      a backtrace structure and the function was not entered in ARM mode then
12982      just pop the return address straight into the PC.  */
12983   else if (!TARGET_INTERWORK
12984            && !TARGET_BACKTRACE
12985            && !is_called_in_ARM_mode (current_function_decl)
12986            && !current_function_calls_eh_return)
12987     {
12988       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12989       return;
12990     }
12991
12992   /* Find out how many of the (return) argument registers we can corrupt.  */
12993   regs_available_for_popping = 0;
12994
12995   /* If returning via __builtin_eh_return, the bottom three registers
12996      all contain information needed for the return.  */
12997   if (current_function_calls_eh_return)
12998     size = 12;
12999   else
13000     {
13001       /* If we can deduce the registers used from the function's
13002          return value.  This is more reliable that examining
13003          regs_ever_live[] because that will be set if the register is
13004          ever used in the function, not just if the register is used
13005          to hold a return value.  */
13006
13007       if (current_function_return_rtx != 0)
13008         mode = GET_MODE (current_function_return_rtx);
13009       else
13010         mode = DECL_MODE (DECL_RESULT (current_function_decl));
13011
13012       size = GET_MODE_SIZE (mode);
13013
13014       if (size == 0)
13015         {
13016           /* In a void function we can use any argument register.
13017              In a function that returns a structure on the stack
13018              we can use the second and third argument registers.  */
13019           if (mode == VOIDmode)
13020             regs_available_for_popping =
13021               (1 << ARG_REGISTER (1))
13022               | (1 << ARG_REGISTER (2))
13023               | (1 << ARG_REGISTER (3));
13024           else
13025             regs_available_for_popping =
13026               (1 << ARG_REGISTER (2))
13027               | (1 << ARG_REGISTER (3));
13028         }
13029       else if (size <= 4)
13030         regs_available_for_popping =
13031           (1 << ARG_REGISTER (2))
13032           | (1 << ARG_REGISTER (3));
13033       else if (size <= 8)
13034         regs_available_for_popping =
13035           (1 << ARG_REGISTER (3));
13036     }
13037
13038   /* Match registers to be popped with registers into which we pop them.  */
13039   for (available = regs_available_for_popping,
13040        required  = regs_to_pop;
13041        required != 0 && available != 0;
13042        available &= ~(available & - available),
13043        required  &= ~(required  & - required))
13044     -- pops_needed;
13045
13046   /* If we have any popping registers left over, remove them.  */
13047   if (available > 0)
13048     regs_available_for_popping &= ~available;
13049
13050   /* Otherwise if we need another popping register we can use
13051      the fourth argument register.  */
13052   else if (pops_needed)
13053     {
13054       /* If we have not found any free argument registers and
13055          reg a4 contains the return address, we must move it.  */
13056       if (regs_available_for_popping == 0
13057           && reg_containing_return_addr == LAST_ARG_REGNUM)
13058         {
13059           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13060           reg_containing_return_addr = LR_REGNUM;
13061         }
13062       else if (size > 12)
13063         {
13064           /* Register a4 is being used to hold part of the return value,
13065              but we have dire need of a free, low register.  */
13066           restore_a4 = TRUE;
13067
13068           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13069         }
13070
13071       if (reg_containing_return_addr != LAST_ARG_REGNUM)
13072         {
13073           /* The fourth argument register is available.  */
13074           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13075
13076           --pops_needed;
13077         }
13078     }
13079
13080   /* Pop as many registers as we can.  */
13081   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13082                  regs_available_for_popping);
13083
13084   /* Process the registers we popped.  */
13085   if (reg_containing_return_addr == -1)
13086     {
13087       /* The return address was popped into the lowest numbered register.  */
13088       regs_to_pop &= ~(1 << LR_REGNUM);
13089
13090       reg_containing_return_addr =
13091         number_of_first_bit_set (regs_available_for_popping);
13092
13093       /* Remove this register for the mask of available registers, so that
13094          the return address will not be corrupted by further pops.  */
13095       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13096     }
13097
13098   /* If we popped other registers then handle them here.  */
13099   if (regs_available_for_popping)
13100     {
13101       int frame_pointer;
13102
13103       /* Work out which register currently contains the frame pointer.  */
13104       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13105
13106       /* Move it into the correct place.  */
13107       asm_fprintf (f, "\tmov\t%r, %r\n",
13108                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13109
13110       /* (Temporarily) remove it from the mask of popped registers.  */
13111       regs_available_for_popping &= ~(1 << frame_pointer);
13112       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13113
13114       if (regs_available_for_popping)
13115         {
13116           int stack_pointer;
13117
13118           /* We popped the stack pointer as well,
13119              find the register that contains it.  */
13120           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13121
13122           /* Move it into the stack register.  */
13123           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13124
13125           /* At this point we have popped all necessary registers, so
13126              do not worry about restoring regs_available_for_popping
13127              to its correct value:
13128
13129              assert (pops_needed == 0)
13130              assert (regs_available_for_popping == (1 << frame_pointer))
13131              assert (regs_to_pop == (1 << STACK_POINTER))  */
13132         }
13133       else
13134         {
13135           /* Since we have just move the popped value into the frame
13136              pointer, the popping register is available for reuse, and
13137              we know that we still have the stack pointer left to pop.  */
13138           regs_available_for_popping |= (1 << frame_pointer);
13139         }
13140     }
13141
13142   /* If we still have registers left on the stack, but we no longer have
13143      any registers into which we can pop them, then we must move the return
13144      address into the link register and make available the register that
13145      contained it.  */
13146   if (regs_available_for_popping == 0 && pops_needed > 0)
13147     {
13148       regs_available_for_popping |= 1 << reg_containing_return_addr;
13149
13150       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13151                    reg_containing_return_addr);
13152
13153       reg_containing_return_addr = LR_REGNUM;
13154     }
13155
13156   /* If we have registers left on the stack then pop some more.
13157      We know that at most we will want to pop FP and SP.  */
13158   if (pops_needed > 0)
13159     {
13160       int  popped_into;
13161       int  move_to;
13162
13163       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13164                      regs_available_for_popping);
13165
13166       /* We have popped either FP or SP.
13167          Move whichever one it is into the correct register.  */
13168       popped_into = number_of_first_bit_set (regs_available_for_popping);
13169       move_to     = number_of_first_bit_set (regs_to_pop);
13170
13171       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
13172
13173       regs_to_pop &= ~(1 << move_to);
13174
13175       --pops_needed;
13176     }
13177
13178   /* If we still have not popped everything then we must have only
13179      had one register available to us and we are now popping the SP.  */
13180   if (pops_needed > 0)
13181     {
13182       int  popped_into;
13183
13184       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13185                      regs_available_for_popping);
13186
13187       popped_into = number_of_first_bit_set (regs_available_for_popping);
13188
13189       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
13190       /*
13191         assert (regs_to_pop == (1 << STACK_POINTER))
13192         assert (pops_needed == 1)
13193       */
13194     }
13195
13196   /* If necessary restore the a4 register.  */
13197   if (restore_a4)
13198     {
13199       if (reg_containing_return_addr != LR_REGNUM)
13200         {
13201           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13202           reg_containing_return_addr = LR_REGNUM;
13203         }
13204
13205       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13206     }
13207
13208   if (current_function_calls_eh_return)
13209     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13210
13211   /* Return to caller.  */
13212   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13213 }
13214
13215 \f
13216 void
13217 thumb_final_prescan_insn (rtx insn)
13218 {
13219   if (flag_print_asm_name)
13220     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
13221                  INSN_ADDRESSES (INSN_UID (insn)));
13222 }
13223
13224 int
13225 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
13226 {
13227   unsigned HOST_WIDE_INT mask = 0xff;
13228   int i;
13229
13230   if (val == 0) /* XXX */
13231     return 0;
13232
13233   for (i = 0; i < 25; i++)
13234     if ((val & (mask << i)) == val)
13235       return 1;
13236
13237   return 0;
13238 }
13239
13240 /* Returns nonzero if the current function contains,
13241    or might contain a far jump.  */
13242 static int
13243 thumb_far_jump_used_p (void)
13244 {
13245   rtx insn;
13246
13247   /* This test is only important for leaf functions.  */
13248   /* assert (!leaf_function_p ()); */
13249
13250   /* If we have already decided that far jumps may be used,
13251      do not bother checking again, and always return true even if
13252      it turns out that they are not being used.  Once we have made
13253      the decision that far jumps are present (and that hence the link
13254      register will be pushed onto the stack) we cannot go back on it.  */
13255   if (cfun->machine->far_jump_used)
13256     return 1;
13257
13258   /* If this function is not being called from the prologue/epilogue
13259      generation code then it must be being called from the
13260      INITIAL_ELIMINATION_OFFSET macro.  */
13261   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
13262     {
13263       /* In this case we know that we are being asked about the elimination
13264          of the arg pointer register.  If that register is not being used,
13265          then there are no arguments on the stack, and we do not have to
13266          worry that a far jump might force the prologue to push the link
13267          register, changing the stack offsets.  In this case we can just
13268          return false, since the presence of far jumps in the function will
13269          not affect stack offsets.
13270
13271          If the arg pointer is live (or if it was live, but has now been
13272          eliminated and so set to dead) then we do have to test to see if
13273          the function might contain a far jump.  This test can lead to some
13274          false negatives, since before reload is completed, then length of
13275          branch instructions is not known, so gcc defaults to returning their
13276          longest length, which in turn sets the far jump attribute to true.
13277
13278          A false negative will not result in bad code being generated, but it
13279          will result in a needless push and pop of the link register.  We
13280          hope that this does not occur too often.
13281
13282          If we need doubleword stack alignment this could affect the other
13283          elimination offsets so we can't risk getting it wrong.  */
13284       if (regs_ever_live [ARG_POINTER_REGNUM])
13285         cfun->machine->arg_pointer_live = 1;
13286       else if (!cfun->machine->arg_pointer_live)
13287         return 0;
13288     }
13289
13290   /* Check to see if the function contains a branch
13291      insn with the far jump attribute set.  */
13292   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13293     {
13294       if (GET_CODE (insn) == JUMP_INSN
13295           /* Ignore tablejump patterns.  */
13296           && GET_CODE (PATTERN (insn)) != ADDR_VEC
13297           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
13298           && get_attr_far_jump (insn) == FAR_JUMP_YES
13299           )
13300         {
13301           /* Record the fact that we have decided that
13302              the function does use far jumps.  */
13303           cfun->machine->far_jump_used = 1;
13304           return 1;
13305         }
13306     }
13307
13308   return 0;
13309 }
13310
13311 /* Return nonzero if FUNC must be entered in ARM mode.  */
13312 int
13313 is_called_in_ARM_mode (tree func)
13314 {
13315   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
13316
13317   /* Ignore the problem about functions whose address is taken.  */
13318   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
13319     return TRUE;
13320
13321 #ifdef ARM_PE
13322   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
13323 #else
13324   return FALSE;
13325 #endif
13326 }
13327
13328 /* The bits which aren't usefully expanded as rtl.  */
13329 const char *
13330 thumb_unexpanded_epilogue (void)
13331 {
13332   int regno;
13333   unsigned long live_regs_mask = 0;
13334   int high_regs_pushed = 0;
13335   int had_to_push_lr;
13336   int size;
13337
13338   if (return_used_this_function)
13339     return "";
13340
13341   if (IS_NAKED (arm_current_func_type ()))
13342     return "";
13343
13344   live_regs_mask = thumb_compute_save_reg_mask ();
13345   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13346
13347   /* If we can deduce the registers used from the function's return value.
13348      This is more reliable that examining regs_ever_live[] because that
13349      will be set if the register is ever used in the function, not just if
13350      the register is used to hold a return value.  */
13351   size = arm_size_return_regs ();
13352
13353   /* The prolog may have pushed some high registers to use as
13354      work registers.  e.g. the testsuite file:
13355      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
13356      compiles to produce:
13357         push    {r4, r5, r6, r7, lr}
13358         mov     r7, r9
13359         mov     r6, r8
13360         push    {r6, r7}
13361      as part of the prolog.  We have to undo that pushing here.  */
13362
13363   if (high_regs_pushed)
13364     {
13365       unsigned long mask = live_regs_mask & 0xff;
13366       int next_hi_reg;
13367
13368       /* The available low registers depend on the size of the value we are
13369          returning.  */
13370       if (size <= 12)
13371         mask |=  1 << 3;
13372       if (size <= 8)
13373         mask |= 1 << 2;
13374
13375       if (mask == 0)
13376         /* Oh dear!  We have no low registers into which we can pop
13377            high registers!  */
13378         internal_error
13379           ("no low registers available for popping high registers");
13380
13381       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
13382         if (live_regs_mask & (1 << next_hi_reg))
13383           break;
13384
13385       while (high_regs_pushed)
13386         {
13387           /* Find lo register(s) into which the high register(s) can
13388              be popped.  */
13389           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13390             {
13391               if (mask & (1 << regno))
13392                 high_regs_pushed--;
13393               if (high_regs_pushed == 0)
13394                 break;
13395             }
13396
13397           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
13398
13399           /* Pop the values into the low register(s).  */
13400           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13401
13402           /* Move the value(s) into the high registers.  */
13403           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13404             {
13405               if (mask & (1 << regno))
13406                 {
13407                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13408                                regno);
13409
13410                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13411                     if (live_regs_mask & (1 << next_hi_reg))
13412                       break;
13413                 }
13414             }
13415         }
13416       live_regs_mask &= ~0x0f00;
13417     }
13418
13419   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
13420   live_regs_mask &= 0xff;
13421
13422   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13423     {
13424       /* Pop the return address into the PC.  */
13425       if (had_to_push_lr)
13426         live_regs_mask |= 1 << PC_REGNUM;
13427
13428       /* Either no argument registers were pushed or a backtrace
13429          structure was created which includes an adjusted stack
13430          pointer, so just pop everything.  */
13431       if (live_regs_mask)
13432         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13433                        live_regs_mask);
13434
13435       /* We have either just popped the return address into the
13436          PC or it is was kept in LR for the entire function.  */
13437       if (!had_to_push_lr)
13438         thumb_exit (asm_out_file, LR_REGNUM);
13439     }
13440   else
13441     {
13442       /* Pop everything but the return address.  */
13443       if (live_regs_mask)
13444         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13445                        live_regs_mask);
13446
13447       if (had_to_push_lr)
13448         {
13449           if (size > 12)
13450             {
13451               /* We have no free low regs, so save one.  */
13452               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13453                            LAST_ARG_REGNUM);
13454             }
13455
13456           /* Get the return address into a temporary register.  */
13457           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13458                          1 << LAST_ARG_REGNUM);
13459
13460           if (size > 12)
13461             {
13462               /* Move the return address to lr.  */
13463               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13464                            LAST_ARG_REGNUM);
13465               /* Restore the low register.  */
13466               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13467                            IP_REGNUM);
13468               regno = LR_REGNUM;
13469             }
13470           else
13471             regno = LAST_ARG_REGNUM;
13472         }
13473       else
13474         regno = LR_REGNUM;
13475
13476       /* Remove the argument registers that were pushed onto the stack.  */
13477       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13478                    SP_REGNUM, SP_REGNUM,
13479                    current_function_pretend_args_size);
13480
13481       thumb_exit (asm_out_file, regno);
13482     }
13483
13484   return "";
13485 }
13486
13487 /* Functions to save and restore machine-specific function data.  */
13488 static struct machine_function *
13489 arm_init_machine_status (void)
13490 {
13491   struct machine_function *machine;
13492   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13493
13494 #if ARM_FT_UNKNOWN != 0
13495   machine->func_type = ARM_FT_UNKNOWN;
13496 #endif
13497   return machine;
13498 }
13499
13500 /* Return an RTX indicating where the return address to the
13501    calling function can be found.  */
13502 rtx
13503 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13504 {
13505   if (count != 0)
13506     return NULL_RTX;
13507
13508   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13509 }
13510
13511 /* Do anything needed before RTL is emitted for each function.  */
13512 void
13513 arm_init_expanders (void)
13514 {
13515   /* Arrange to initialize and mark the machine per-function status.  */
13516   init_machine_status = arm_init_machine_status;
13517
13518   /* This is to stop the combine pass optimizing away the alignment
13519      adjustment of va_arg.  */
13520   /* ??? It is claimed that this should not be necessary.  */
13521   if (cfun)
13522     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13523 }
13524
13525
13526 /* Like arm_compute_initial_elimination offset.  Simpler because there
13527    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
13528    to point at the base of the local variables after static stack
13529    space for a function has been allocated.  */
13530
13531 HOST_WIDE_INT
13532 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13533 {
13534   arm_stack_offsets *offsets;
13535
13536   offsets = arm_get_frame_offsets ();
13537
13538   switch (from)
13539     {
13540     case ARG_POINTER_REGNUM:
13541       switch (to)
13542         {
13543         case STACK_POINTER_REGNUM:
13544           return offsets->outgoing_args - offsets->saved_args;
13545
13546         case FRAME_POINTER_REGNUM:
13547           return offsets->soft_frame - offsets->saved_args;
13548
13549         case ARM_HARD_FRAME_POINTER_REGNUM:
13550           return offsets->saved_regs - offsets->saved_args;
13551
13552         case THUMB_HARD_FRAME_POINTER_REGNUM:
13553           return offsets->locals_base - offsets->saved_args;
13554
13555         default:
13556           gcc_unreachable ();
13557         }
13558       break;
13559
13560     case FRAME_POINTER_REGNUM:
13561       switch (to)
13562         {
13563         case STACK_POINTER_REGNUM:
13564           return offsets->outgoing_args - offsets->soft_frame;
13565
13566         case ARM_HARD_FRAME_POINTER_REGNUM:
13567           return offsets->saved_regs - offsets->soft_frame;
13568
13569         case THUMB_HARD_FRAME_POINTER_REGNUM:
13570           return offsets->locals_base - offsets->soft_frame;
13571
13572         default:
13573           gcc_unreachable ();
13574         }
13575       break;
13576
13577     default:
13578       gcc_unreachable ();
13579     }
13580 }
13581
13582
13583 /* Generate the rest of a function's prologue.  */
13584 void
13585 thumb_expand_prologue (void)
13586 {
13587   rtx insn, dwarf;
13588
13589   HOST_WIDE_INT amount;
13590   arm_stack_offsets *offsets;
13591   unsigned long func_type;
13592   int regno;
13593   unsigned long live_regs_mask;
13594
13595   func_type = arm_current_func_type ();
13596
13597   /* Naked functions don't have prologues.  */
13598   if (IS_NAKED (func_type))
13599     return;
13600
13601   if (IS_INTERRUPT (func_type))
13602     {
13603       error ("interrupt Service Routines cannot be coded in Thumb mode");
13604       return;
13605     }
13606
13607   live_regs_mask = thumb_compute_save_reg_mask ();
13608   /* Load the pic register before setting the frame pointer,
13609      so we can use r7 as a temporary work register.  */
13610   if (flag_pic)
13611     arm_load_pic_register (live_regs_mask);
13612
13613   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13614     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13615                     stack_pointer_rtx);
13616
13617   offsets = arm_get_frame_offsets ();
13618   amount = offsets->outgoing_args - offsets->saved_regs;
13619   if (amount)
13620     {
13621       if (amount < 512)
13622         {
13623           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13624                                         GEN_INT (- amount)));
13625           RTX_FRAME_RELATED_P (insn) = 1;
13626         }
13627       else
13628         {
13629           rtx reg;
13630
13631           /* The stack decrement is too big for an immediate value in a single
13632              insn.  In theory we could issue multiple subtracts, but after
13633              three of them it becomes more space efficient to place the full
13634              value in the constant pool and load into a register.  (Also the
13635              ARM debugger really likes to see only one stack decrement per
13636              function).  So instead we look for a scratch register into which
13637              we can load the decrement, and then we subtract this from the
13638              stack pointer.  Unfortunately on the thumb the only available
13639              scratch registers are the argument registers, and we cannot use
13640              these as they may hold arguments to the function.  Instead we
13641              attempt to locate a call preserved register which is used by this
13642              function.  If we can find one, then we know that it will have
13643              been pushed at the start of the prologue and so we can corrupt
13644              it now.  */
13645           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13646             if (live_regs_mask & (1 << regno)
13647                 && !(frame_pointer_needed
13648                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13649               break;
13650
13651           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13652             {
13653               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13654
13655               /* Choose an arbitrary, non-argument low register.  */
13656               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13657
13658               /* Save it by copying it into a high, scratch register.  */
13659               emit_insn (gen_movsi (spare, reg));
13660               /* Add a USE to stop propagate_one_insn() from barfing.  */
13661               emit_insn (gen_prologue_use (spare));
13662
13663               /* Decrement the stack.  */
13664               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13665               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13666                                             stack_pointer_rtx, reg));
13667               RTX_FRAME_RELATED_P (insn) = 1;
13668               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13669                                    plus_constant (stack_pointer_rtx,
13670                                                   -amount));
13671               RTX_FRAME_RELATED_P (dwarf) = 1;
13672               REG_NOTES (insn)
13673                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13674                                      REG_NOTES (insn));
13675
13676               /* Restore the low register's original value.  */
13677               emit_insn (gen_movsi (reg, spare));
13678
13679               /* Emit a USE of the restored scratch register, so that flow
13680                  analysis will not consider the restore redundant.  The
13681                  register won't be used again in this function and isn't
13682                  restored by the epilogue.  */
13683               emit_insn (gen_prologue_use (reg));
13684             }
13685           else
13686             {
13687               reg = gen_rtx_REG (SImode, regno);
13688
13689               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13690
13691               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13692                                             stack_pointer_rtx, reg));
13693               RTX_FRAME_RELATED_P (insn) = 1;
13694               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13695                                    plus_constant (stack_pointer_rtx,
13696                                                   -amount));
13697               RTX_FRAME_RELATED_P (dwarf) = 1;
13698               REG_NOTES (insn)
13699                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13700                                      REG_NOTES (insn));
13701             }
13702         }
13703     }
13704
13705   if (frame_pointer_needed)
13706     {
13707       amount = offsets->outgoing_args - offsets->locals_base;
13708
13709       if (amount < 1024)
13710         insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13711                                       stack_pointer_rtx, GEN_INT (amount)));
13712       else
13713         {
13714           emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13715           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13716                                         hard_frame_pointer_rtx,
13717                                         stack_pointer_rtx));
13718           dwarf = gen_rtx_SET (SImode, hard_frame_pointer_rtx,
13719                                plus_constant (stack_pointer_rtx, amount));
13720           RTX_FRAME_RELATED_P (dwarf) = 1;
13721           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13722                                                 REG_NOTES (insn));
13723         }
13724
13725       RTX_FRAME_RELATED_P (insn) = 1;
13726     }
13727
13728   /* If we are profiling, make sure no instructions are scheduled before
13729      the call to mcount.  Similarly if the user has requested no
13730      scheduling in the prolog.  Similarly if we want non-call exceptions
13731      using the EABI unwinder, to prevent faulting instructions from being
13732      swapped with a stack adjustment.  */
13733   if (current_function_profile || !TARGET_SCHED_PROLOG
13734       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13735     emit_insn (gen_blockage ());
13736
13737   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13738   if (live_regs_mask & 0xff)
13739     cfun->machine->lr_save_eliminated = 0;
13740
13741   /* If the link register is being kept alive, with the return address in it,
13742      then make sure that it does not get reused by the ce2 pass.  */
13743   if (cfun->machine->lr_save_eliminated)
13744     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13745 }
13746
13747
13748 void
13749 thumb_expand_epilogue (void)
13750 {
13751   HOST_WIDE_INT amount;
13752   arm_stack_offsets *offsets;
13753   int regno;
13754
13755   /* Naked functions don't have prologues.  */
13756   if (IS_NAKED (arm_current_func_type ()))
13757     return;
13758
13759   offsets = arm_get_frame_offsets ();
13760   amount = offsets->outgoing_args - offsets->saved_regs;
13761
13762   if (frame_pointer_needed)
13763     {
13764       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13765       amount = offsets->locals_base - offsets->saved_regs;
13766     }
13767
13768   if (amount)
13769     {
13770       if (amount < 512)
13771         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13772                                GEN_INT (amount)));
13773       else
13774         {
13775           /* r3 is always free in the epilogue.  */
13776           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13777
13778           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13779           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13780         }
13781     }
13782
13783   /* Emit a USE (stack_pointer_rtx), so that
13784      the stack adjustment will not be deleted.  */
13785   emit_insn (gen_prologue_use (stack_pointer_rtx));
13786
13787   if (current_function_profile || !TARGET_SCHED_PROLOG)
13788     emit_insn (gen_blockage ());
13789
13790   /* Emit a clobber for each insn that will be restored in the epilogue,
13791      so that flow2 will get register lifetimes correct.  */
13792   for (regno = 0; regno < 13; regno++)
13793     if (regs_ever_live[regno] && !call_used_regs[regno])
13794       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13795
13796   if (! regs_ever_live[LR_REGNUM])
13797     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13798 }
13799
13800 static void
13801 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13802 {
13803   unsigned long live_regs_mask = 0;
13804   unsigned long l_mask;
13805   unsigned high_regs_pushed = 0;
13806   int cfa_offset = 0;
13807   int regno;
13808
13809   if (IS_NAKED (arm_current_func_type ()))
13810     return;
13811
13812   if (is_called_in_ARM_mode (current_function_decl))
13813     {
13814       const char * name;
13815
13816       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13817       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13818                   == SYMBOL_REF);
13819       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13820
13821       /* Generate code sequence to switch us into Thumb mode.  */
13822       /* The .code 32 directive has already been emitted by
13823          ASM_DECLARE_FUNCTION_NAME.  */
13824       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13825       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13826
13827       /* Generate a label, so that the debugger will notice the
13828          change in instruction sets.  This label is also used by
13829          the assembler to bypass the ARM code when this function
13830          is called from a Thumb encoded function elsewhere in the
13831          same file.  Hence the definition of STUB_NAME here must
13832          agree with the definition in gas/config/tc-arm.c.  */
13833
13834 #define STUB_NAME ".real_start_of"
13835
13836       fprintf (f, "\t.code\t16\n");
13837 #ifdef ARM_PE
13838       if (arm_dllexport_name_p (name))
13839         name = arm_strip_name_encoding (name);
13840 #endif
13841       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13842       fprintf (f, "\t.thumb_func\n");
13843       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13844     }
13845
13846   if (current_function_pretend_args_size)
13847     {
13848       /* Output unwind directive for the stack adjustment.  */
13849       if (ARM_EABI_UNWIND_TABLES)
13850         fprintf (f, "\t.pad #%d\n",
13851                  current_function_pretend_args_size);
13852
13853       if (cfun->machine->uses_anonymous_args)
13854         {
13855           int num_pushes;
13856
13857           fprintf (f, "\tpush\t{");
13858
13859           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13860
13861           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13862                regno <= LAST_ARG_REGNUM;
13863                regno++)
13864             asm_fprintf (f, "%r%s", regno,
13865                          regno == LAST_ARG_REGNUM ? "" : ", ");
13866
13867           fprintf (f, "}\n");
13868         }
13869       else
13870         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13871                      SP_REGNUM, SP_REGNUM,
13872                      current_function_pretend_args_size);
13873
13874       /* We don't need to record the stores for unwinding (would it
13875          help the debugger any if we did?), but record the change in
13876          the stack pointer.  */
13877       if (dwarf2out_do_frame ())
13878         {
13879           char *l = dwarf2out_cfi_label ();
13880
13881           cfa_offset = cfa_offset + current_function_pretend_args_size;
13882           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13883         }
13884     }
13885
13886   /* Get the registers we are going to push.  */
13887   live_regs_mask = thumb_compute_save_reg_mask ();
13888   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13889   l_mask = live_regs_mask & 0x40ff;
13890   /* Then count how many other high registers will need to be pushed.  */
13891   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13892
13893   if (TARGET_BACKTRACE)
13894     {
13895       unsigned offset;
13896       unsigned work_register;
13897
13898       /* We have been asked to create a stack backtrace structure.
13899          The code looks like this:
13900
13901          0   .align 2
13902          0   func:
13903          0     sub   SP, #16         Reserve space for 4 registers.
13904          2     push  {R7}            Push low registers.
13905          4     add   R7, SP, #20     Get the stack pointer before the push.
13906          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13907          8     mov   R7, PC          Get hold of the start of this code plus 12.
13908         10     str   R7, [SP, #16]   Store it.
13909         12     mov   R7, FP          Get hold of the current frame pointer.
13910         14     str   R7, [SP, #4]    Store it.
13911         16     mov   R7, LR          Get hold of the current return address.
13912         18     str   R7, [SP, #12]   Store it.
13913         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13914         22     mov   FP, R7          Put this value into the frame pointer.  */
13915
13916       work_register = thumb_find_work_register (live_regs_mask);
13917
13918       if (ARM_EABI_UNWIND_TABLES)
13919         asm_fprintf (f, "\t.pad #16\n");
13920
13921       asm_fprintf
13922         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13923          SP_REGNUM, SP_REGNUM);
13924
13925       if (dwarf2out_do_frame ())
13926         {
13927           char *l = dwarf2out_cfi_label ();
13928
13929           cfa_offset = cfa_offset + 16;
13930           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13931         }
13932
13933       if (l_mask)
13934         {
13935           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13936           offset = bit_count (l_mask) * UNITS_PER_WORD;
13937         }
13938       else
13939         offset = 0;
13940
13941       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13942                    offset + 16 + current_function_pretend_args_size);
13943
13944       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13945                    offset + 4);
13946
13947       /* Make sure that the instruction fetching the PC is in the right place
13948          to calculate "start of backtrace creation code + 12".  */
13949       if (l_mask)
13950         {
13951           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13952           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13953                        offset + 12);
13954           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13955                        ARM_HARD_FRAME_POINTER_REGNUM);
13956           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13957                        offset);
13958         }
13959       else
13960         {
13961           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13962                        ARM_HARD_FRAME_POINTER_REGNUM);
13963           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13964                        offset);
13965           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13966           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13967                        offset + 12);
13968         }
13969
13970       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13971       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13972                    offset + 8);
13973       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13974                    offset + 12);
13975       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13976                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13977     }
13978   /* Optimization:  If we are not pushing any low registers but we are going
13979      to push some high registers then delay our first push.  This will just
13980      be a push of LR and we can combine it with the push of the first high
13981      register.  */
13982   else if ((l_mask & 0xff) != 0
13983            || (high_regs_pushed == 0 && l_mask))
13984     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13985
13986   if (high_regs_pushed)
13987     {
13988       unsigned pushable_regs;
13989       unsigned next_hi_reg;
13990
13991       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13992         if (live_regs_mask & (1 << next_hi_reg))
13993           break;
13994
13995       pushable_regs = l_mask & 0xff;
13996
13997       if (pushable_regs == 0)
13998         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13999
14000       while (high_regs_pushed > 0)
14001         {
14002           unsigned long real_regs_mask = 0;
14003
14004           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14005             {
14006               if (pushable_regs & (1 << regno))
14007                 {
14008                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14009
14010                   high_regs_pushed --;
14011                   real_regs_mask |= (1 << next_hi_reg);
14012
14013                   if (high_regs_pushed)
14014                     {
14015                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14016                            next_hi_reg --)
14017                         if (live_regs_mask & (1 << next_hi_reg))
14018                           break;
14019                     }
14020                   else
14021                     {
14022                       pushable_regs &= ~((1 << regno) - 1);
14023                       break;
14024                     }
14025                 }
14026             }
14027
14028           /* If we had to find a work register and we have not yet
14029              saved the LR then add it to the list of regs to push.  */
14030           if (l_mask == (1 << LR_REGNUM))
14031             {
14032               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14033                              1, &cfa_offset,
14034                              real_regs_mask | (1 << LR_REGNUM));
14035               l_mask = 0;
14036             }
14037           else
14038             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14039         }
14040     }
14041 }
14042
14043 /* Handle the case of a double word load into a low register from
14044    a computed memory address.  The computed address may involve a
14045    register which is overwritten by the load.  */
14046 const char *
14047 thumb_load_double_from_address (rtx *operands)
14048 {
14049   rtx addr;
14050   rtx base;
14051   rtx offset;
14052   rtx arg1;
14053   rtx arg2;
14054
14055   gcc_assert (GET_CODE (operands[0]) == REG);
14056   gcc_assert (GET_CODE (operands[1]) == MEM);
14057
14058   /* Get the memory address.  */
14059   addr = XEXP (operands[1], 0);
14060
14061   /* Work out how the memory address is computed.  */
14062   switch (GET_CODE (addr))
14063     {
14064     case REG:
14065       operands[2] = adjust_address (operands[1], SImode, 4);
14066
14067       if (REGNO (operands[0]) == REGNO (addr))
14068         {
14069           output_asm_insn ("ldr\t%H0, %2", operands);
14070           output_asm_insn ("ldr\t%0, %1", operands);
14071         }
14072       else
14073         {
14074           output_asm_insn ("ldr\t%0, %1", operands);
14075           output_asm_insn ("ldr\t%H0, %2", operands);
14076         }
14077       break;
14078
14079     case CONST:
14080       /* Compute <address> + 4 for the high order load.  */
14081       operands[2] = adjust_address (operands[1], SImode, 4);
14082
14083       output_asm_insn ("ldr\t%0, %1", operands);
14084       output_asm_insn ("ldr\t%H0, %2", operands);
14085       break;
14086
14087     case PLUS:
14088       arg1   = XEXP (addr, 0);
14089       arg2   = XEXP (addr, 1);
14090
14091       if (CONSTANT_P (arg1))
14092         base = arg2, offset = arg1;
14093       else
14094         base = arg1, offset = arg2;
14095
14096       gcc_assert (GET_CODE (base) == REG);
14097
14098       /* Catch the case of <address> = <reg> + <reg> */
14099       if (GET_CODE (offset) == REG)
14100         {
14101           int reg_offset = REGNO (offset);
14102           int reg_base   = REGNO (base);
14103           int reg_dest   = REGNO (operands[0]);
14104
14105           /* Add the base and offset registers together into the
14106              higher destination register.  */
14107           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14108                        reg_dest + 1, reg_base, reg_offset);
14109
14110           /* Load the lower destination register from the address in
14111              the higher destination register.  */
14112           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14113                        reg_dest, reg_dest + 1);
14114
14115           /* Load the higher destination register from its own address
14116              plus 4.  */
14117           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14118                        reg_dest + 1, reg_dest + 1);
14119         }
14120       else
14121         {
14122           /* Compute <address> + 4 for the high order load.  */
14123           operands[2] = adjust_address (operands[1], SImode, 4);
14124
14125           /* If the computed address is held in the low order register
14126              then load the high order register first, otherwise always
14127              load the low order register first.  */
14128           if (REGNO (operands[0]) == REGNO (base))
14129             {
14130               output_asm_insn ("ldr\t%H0, %2", operands);
14131               output_asm_insn ("ldr\t%0, %1", operands);
14132             }
14133           else
14134             {
14135               output_asm_insn ("ldr\t%0, %1", operands);
14136               output_asm_insn ("ldr\t%H0, %2", operands);
14137             }
14138         }
14139       break;
14140
14141     case LABEL_REF:
14142       /* With no registers to worry about we can just load the value
14143          directly.  */
14144       operands[2] = adjust_address (operands[1], SImode, 4);
14145
14146       output_asm_insn ("ldr\t%H0, %2", operands);
14147       output_asm_insn ("ldr\t%0, %1", operands);
14148       break;
14149
14150     default:
14151       gcc_unreachable ();
14152     }
14153
14154   return "";
14155 }
14156
14157 const char *
14158 thumb_output_move_mem_multiple (int n, rtx *operands)
14159 {
14160   rtx tmp;
14161
14162   switch (n)
14163     {
14164     case 2:
14165       if (REGNO (operands[4]) > REGNO (operands[5]))
14166         {
14167           tmp = operands[4];
14168           operands[4] = operands[5];
14169           operands[5] = tmp;
14170         }
14171       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14172       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14173       break;
14174
14175     case 3:
14176       if (REGNO (operands[4]) > REGNO (operands[5]))
14177         {
14178           tmp = operands[4];
14179           operands[4] = operands[5];
14180           operands[5] = tmp;
14181         }
14182       if (REGNO (operands[5]) > REGNO (operands[6]))
14183         {
14184           tmp = operands[5];
14185           operands[5] = operands[6];
14186           operands[6] = tmp;
14187         }
14188       if (REGNO (operands[4]) > REGNO (operands[5]))
14189         {
14190           tmp = operands[4];
14191           operands[4] = operands[5];
14192           operands[5] = tmp;
14193         }
14194
14195       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
14196       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
14197       break;
14198
14199     default:
14200       gcc_unreachable ();
14201     }
14202
14203   return "";
14204 }
14205
14206 /* Output a call-via instruction for thumb state.  */
14207 const char *
14208 thumb_call_via_reg (rtx reg)
14209 {
14210   int regno = REGNO (reg);
14211   rtx *labelp;
14212
14213   gcc_assert (regno < LR_REGNUM);
14214
14215   /* If we are in the normal text section we can use a single instance
14216      per compilation unit.  If we are doing function sections, then we need
14217      an entry per section, since we can't rely on reachability.  */
14218   if (in_text_section ())
14219     {
14220       thumb_call_reg_needed = 1;
14221
14222       if (thumb_call_via_label[regno] == NULL)
14223         thumb_call_via_label[regno] = gen_label_rtx ();
14224       labelp = thumb_call_via_label + regno;
14225     }
14226   else
14227     {
14228       if (cfun->machine->call_via[regno] == NULL)
14229         cfun->machine->call_via[regno] = gen_label_rtx ();
14230       labelp = cfun->machine->call_via + regno;
14231     }
14232
14233   output_asm_insn ("bl\t%a0", labelp);
14234   return "";
14235 }
14236
14237 /* Routines for generating rtl.  */
14238 void
14239 thumb_expand_movmemqi (rtx *operands)
14240 {
14241   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
14242   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
14243   HOST_WIDE_INT len = INTVAL (operands[2]);
14244   HOST_WIDE_INT offset = 0;
14245
14246   while (len >= 12)
14247     {
14248       emit_insn (gen_movmem12b (out, in, out, in));
14249       len -= 12;
14250     }
14251
14252   if (len >= 8)
14253     {
14254       emit_insn (gen_movmem8b (out, in, out, in));
14255       len -= 8;
14256     }
14257
14258   if (len >= 4)
14259     {
14260       rtx reg = gen_reg_rtx (SImode);
14261       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
14262       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
14263       len -= 4;
14264       offset += 4;
14265     }
14266
14267   if (len >= 2)
14268     {
14269       rtx reg = gen_reg_rtx (HImode);
14270       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
14271                                               plus_constant (in, offset))));
14272       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
14273                             reg));
14274       len -= 2;
14275       offset += 2;
14276     }
14277
14278   if (len)
14279     {
14280       rtx reg = gen_reg_rtx (QImode);
14281       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
14282                                               plus_constant (in, offset))));
14283       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
14284                             reg));
14285     }
14286 }
14287
14288 void
14289 thumb_reload_out_hi (rtx *operands)
14290 {
14291   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
14292 }
14293
14294 /* Handle reading a half-word from memory during reload.  */
14295 void
14296 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
14297 {
14298   gcc_unreachable ();
14299 }
14300
14301 /* Return the length of a function name prefix
14302     that starts with the character 'c'.  */
14303 static int
14304 arm_get_strip_length (int c)
14305 {
14306   switch (c)
14307     {
14308     ARM_NAME_ENCODING_LENGTHS
14309       default: return 0;
14310     }
14311 }
14312
14313 /* Return a pointer to a function's name with any
14314    and all prefix encodings stripped from it.  */
14315 const char *
14316 arm_strip_name_encoding (const char *name)
14317 {
14318   int skip;
14319
14320   while ((skip = arm_get_strip_length (* name)))
14321     name += skip;
14322
14323   return name;
14324 }
14325
14326 /* If there is a '*' anywhere in the name's prefix, then
14327    emit the stripped name verbatim, otherwise prepend an
14328    underscore if leading underscores are being used.  */
14329 void
14330 arm_asm_output_labelref (FILE *stream, const char *name)
14331 {
14332   int skip;
14333   int verbatim = 0;
14334
14335   while ((skip = arm_get_strip_length (* name)))
14336     {
14337       verbatim |= (*name == '*');
14338       name += skip;
14339     }
14340
14341   if (verbatim)
14342     fputs (name, stream);
14343   else
14344     asm_fprintf (stream, "%U%s", name);
14345 }
14346
14347 static void
14348 arm_file_end (void)
14349 {
14350   int regno;
14351
14352   if (! thumb_call_reg_needed)
14353     return;
14354
14355   text_section ();
14356   asm_fprintf (asm_out_file, "\t.code 16\n");
14357   ASM_OUTPUT_ALIGN (asm_out_file, 1);
14358
14359   for (regno = 0; regno < LR_REGNUM; regno++)
14360     {
14361       rtx label = thumb_call_via_label[regno];
14362
14363       if (label != 0)
14364         {
14365           targetm.asm_out.internal_label (asm_out_file, "L",
14366                                           CODE_LABEL_NUMBER (label));
14367           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14368         }
14369     }
14370 }
14371
14372 rtx aof_pic_label;
14373
14374 #ifdef AOF_ASSEMBLER
14375 /* Special functions only needed when producing AOF syntax assembler.  */
14376
14377 struct pic_chain
14378 {
14379   struct pic_chain * next;
14380   const char * symname;
14381 };
14382
14383 static struct pic_chain * aof_pic_chain = NULL;
14384
14385 rtx
14386 aof_pic_entry (rtx x)
14387 {
14388   struct pic_chain ** chainp;
14389   int offset;
14390
14391   if (aof_pic_label == NULL_RTX)
14392     {
14393       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
14394     }
14395
14396   for (offset = 0, chainp = &aof_pic_chain; *chainp;
14397        offset += 4, chainp = &(*chainp)->next)
14398     if ((*chainp)->symname == XSTR (x, 0))
14399       return plus_constant (aof_pic_label, offset);
14400
14401   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
14402   (*chainp)->next = NULL;
14403   (*chainp)->symname = XSTR (x, 0);
14404   return plus_constant (aof_pic_label, offset);
14405 }
14406
14407 void
14408 aof_dump_pic_table (FILE *f)
14409 {
14410   struct pic_chain * chain;
14411
14412   if (aof_pic_chain == NULL)
14413     return;
14414
14415   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
14416                PIC_OFFSET_TABLE_REGNUM,
14417                PIC_OFFSET_TABLE_REGNUM);
14418   fputs ("|x$adcons|\n", f);
14419
14420   for (chain = aof_pic_chain; chain; chain = chain->next)
14421     {
14422       fputs ("\tDCD\t", f);
14423       assemble_name (f, chain->symname);
14424       fputs ("\n", f);
14425     }
14426 }
14427
14428 int arm_text_section_count = 1;
14429
14430 char *
14431 aof_text_section (void )
14432 {
14433   static char buf[100];
14434   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
14435            arm_text_section_count++);
14436   if (flag_pic)
14437     strcat (buf, ", PIC, REENTRANT");
14438   return buf;
14439 }
14440
14441 static int arm_data_section_count = 1;
14442
14443 char *
14444 aof_data_section (void)
14445 {
14446   static char buf[100];
14447   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
14448   return buf;
14449 }
14450
14451 /* The AOF assembler is religiously strict about declarations of
14452    imported and exported symbols, so that it is impossible to declare
14453    a function as imported near the beginning of the file, and then to
14454    export it later on.  It is, however, possible to delay the decision
14455    until all the functions in the file have been compiled.  To get
14456    around this, we maintain a list of the imports and exports, and
14457    delete from it any that are subsequently defined.  At the end of
14458    compilation we spit the remainder of the list out before the END
14459    directive.  */
14460
14461 struct import
14462 {
14463   struct import * next;
14464   const char * name;
14465 };
14466
14467 static struct import * imports_list = NULL;
14468
14469 void
14470 aof_add_import (const char *name)
14471 {
14472   struct import * new;
14473
14474   for (new = imports_list; new; new = new->next)
14475     if (new->name == name)
14476       return;
14477
14478   new = (struct import *) xmalloc (sizeof (struct import));
14479   new->next = imports_list;
14480   imports_list = new;
14481   new->name = name;
14482 }
14483
14484 void
14485 aof_delete_import (const char *name)
14486 {
14487   struct import ** old;
14488
14489   for (old = &imports_list; *old; old = & (*old)->next)
14490     {
14491       if ((*old)->name == name)
14492         {
14493           *old = (*old)->next;
14494           return;
14495         }
14496     }
14497 }
14498
14499 int arm_main_function = 0;
14500
14501 static void
14502 aof_dump_imports (FILE *f)
14503 {
14504   /* The AOF assembler needs this to cause the startup code to be extracted
14505      from the library.  Brining in __main causes the whole thing to work
14506      automagically.  */
14507   if (arm_main_function)
14508     {
14509       text_section ();
14510       fputs ("\tIMPORT __main\n", f);
14511       fputs ("\tDCD __main\n", f);
14512     }
14513
14514   /* Now dump the remaining imports.  */
14515   while (imports_list)
14516     {
14517       fprintf (f, "\tIMPORT\t");
14518       assemble_name (f, imports_list->name);
14519       fputc ('\n', f);
14520       imports_list = imports_list->next;
14521     }
14522 }
14523
14524 static void
14525 aof_globalize_label (FILE *stream, const char *name)
14526 {
14527   default_globalize_label (stream, name);
14528   if (! strcmp (name, "main"))
14529     arm_main_function = 1;
14530 }
14531
14532 static void
14533 aof_file_start (void)
14534 {
14535   fputs ("__r0\tRN\t0\n", asm_out_file);
14536   fputs ("__a1\tRN\t0\n", asm_out_file);
14537   fputs ("__a2\tRN\t1\n", asm_out_file);
14538   fputs ("__a3\tRN\t2\n", asm_out_file);
14539   fputs ("__a4\tRN\t3\n", asm_out_file);
14540   fputs ("__v1\tRN\t4\n", asm_out_file);
14541   fputs ("__v2\tRN\t5\n", asm_out_file);
14542   fputs ("__v3\tRN\t6\n", asm_out_file);
14543   fputs ("__v4\tRN\t7\n", asm_out_file);
14544   fputs ("__v5\tRN\t8\n", asm_out_file);
14545   fputs ("__v6\tRN\t9\n", asm_out_file);
14546   fputs ("__sl\tRN\t10\n", asm_out_file);
14547   fputs ("__fp\tRN\t11\n", asm_out_file);
14548   fputs ("__ip\tRN\t12\n", asm_out_file);
14549   fputs ("__sp\tRN\t13\n", asm_out_file);
14550   fputs ("__lr\tRN\t14\n", asm_out_file);
14551   fputs ("__pc\tRN\t15\n", asm_out_file);
14552   fputs ("__f0\tFN\t0\n", asm_out_file);
14553   fputs ("__f1\tFN\t1\n", asm_out_file);
14554   fputs ("__f2\tFN\t2\n", asm_out_file);
14555   fputs ("__f3\tFN\t3\n", asm_out_file);
14556   fputs ("__f4\tFN\t4\n", asm_out_file);
14557   fputs ("__f5\tFN\t5\n", asm_out_file);
14558   fputs ("__f6\tFN\t6\n", asm_out_file);
14559   fputs ("__f7\tFN\t7\n", asm_out_file);
14560   text_section ();
14561 }
14562
14563 static void
14564 aof_file_end (void)
14565 {
14566   if (flag_pic)
14567     aof_dump_pic_table (asm_out_file);
14568   arm_file_end ();
14569   aof_dump_imports (asm_out_file);
14570   fputs ("\tEND\n", asm_out_file);
14571 }
14572 #endif /* AOF_ASSEMBLER */
14573
14574 #ifndef ARM_PE
14575 /* Symbols in the text segment can be accessed without indirecting via the
14576    constant pool; it may take an extra binary operation, but this is still
14577    faster than indirecting via memory.  Don't do this when not optimizing,
14578    since we won't be calculating al of the offsets necessary to do this
14579    simplification.  */
14580
14581 static void
14582 arm_encode_section_info (tree decl, rtx rtl, int first)
14583 {
14584   /* This doesn't work with AOF syntax, since the string table may be in
14585      a different AREA.  */
14586 #ifndef AOF_ASSEMBLER
14587   if (optimize > 0 && TREE_CONSTANT (decl))
14588     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14589 #endif
14590
14591   /* If we are referencing a function that is weak then encode a long call
14592      flag in the function name, otherwise if the function is static or
14593      or known to be defined in this file then encode a short call flag.  */
14594   if (first && DECL_P (decl))
14595     {
14596       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14597         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14598       else if (! TREE_PUBLIC (decl))
14599         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14600     }
14601
14602   default_encode_section_info (decl, rtl, first);
14603 }
14604 #endif /* !ARM_PE */
14605
14606 static void
14607 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14608 {
14609   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14610       && !strcmp (prefix, "L"))
14611     {
14612       arm_ccfsm_state = 0;
14613       arm_target_insn = NULL;
14614     }
14615   default_internal_label (stream, prefix, labelno);
14616 }
14617
14618 /* Output code to add DELTA to the first argument, and then jump
14619    to FUNCTION.  Used for C++ multiple inheritance.  */
14620 static void
14621 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14622                      HOST_WIDE_INT delta,
14623                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14624                      tree function)
14625 {
14626   static int thunk_label = 0;
14627   char label[256];
14628   int mi_delta = delta;
14629   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14630   int shift = 0;
14631   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14632                     ? 1 : 0);
14633   if (mi_delta < 0)
14634     mi_delta = - mi_delta;
14635   if (TARGET_THUMB)
14636     {
14637       int labelno = thunk_label++;
14638       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14639       fputs ("\tldr\tr12, ", file);
14640       assemble_name (file, label);
14641       fputc ('\n', file);
14642     }
14643   while (mi_delta != 0)
14644     {
14645       if ((mi_delta & (3 << shift)) == 0)
14646         shift += 2;
14647       else
14648         {
14649           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14650                        mi_op, this_regno, this_regno,
14651                        mi_delta & (0xff << shift));
14652           mi_delta &= ~(0xff << shift);
14653           shift += 8;
14654         }
14655     }
14656   if (TARGET_THUMB)
14657     {
14658       fprintf (file, "\tbx\tr12\n");
14659       ASM_OUTPUT_ALIGN (file, 2);
14660       assemble_name (file, label);
14661       fputs (":\n", file);
14662       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14663     }
14664   else
14665     {
14666       fputs ("\tb\t", file);
14667       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14668       if (NEED_PLT_RELOC)
14669         fputs ("(PLT)", file);
14670       fputc ('\n', file);
14671     }
14672 }
14673
14674 int
14675 arm_emit_vector_const (FILE *file, rtx x)
14676 {
14677   int i;
14678   const char * pattern;
14679
14680   gcc_assert (GET_CODE (x) == CONST_VECTOR);
14681
14682   switch (GET_MODE (x))
14683     {
14684     case V2SImode: pattern = "%08x"; break;
14685     case V4HImode: pattern = "%04x"; break;
14686     case V8QImode: pattern = "%02x"; break;
14687     default:       gcc_unreachable ();
14688     }
14689
14690   fprintf (file, "0x");
14691   for (i = CONST_VECTOR_NUNITS (x); i--;)
14692     {
14693       rtx element;
14694
14695       element = CONST_VECTOR_ELT (x, i);
14696       fprintf (file, pattern, INTVAL (element));
14697     }
14698
14699   return 1;
14700 }
14701
14702 const char *
14703 arm_output_load_gr (rtx *operands)
14704 {
14705   rtx reg;
14706   rtx offset;
14707   rtx wcgr;
14708   rtx sum;
14709
14710   if (GET_CODE (operands [1]) != MEM
14711       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14712       || GET_CODE (reg = XEXP (sum, 0)) != REG
14713       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14714       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14715     return "wldrw%?\t%0, %1";
14716
14717   /* Fix up an out-of-range load of a GR register.  */
14718   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14719   wcgr = operands[0];
14720   operands[0] = reg;
14721   output_asm_insn ("ldr%?\t%0, %1", operands);
14722
14723   operands[0] = wcgr;
14724   operands[1] = reg;
14725   output_asm_insn ("tmcr%?\t%0, %1", operands);
14726   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14727
14728   return "";
14729 }
14730
14731 static rtx
14732 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14733                       int incoming ATTRIBUTE_UNUSED)
14734 {
14735 #if 0
14736   /* FIXME: The ARM backend has special code to handle structure
14737          returns, and will reserve its own hidden first argument.  So
14738          if this macro is enabled a *second* hidden argument will be
14739          reserved, which will break binary compatibility with old
14740          toolchains and also thunk handling.  One day this should be
14741          fixed.  */
14742   return 0;
14743 #else
14744   /* Register in which address to store a structure value
14745      is passed to a function.  */
14746   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14747 #endif
14748 }
14749
14750 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14751
14752    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14753    named arg and all anonymous args onto the stack.
14754    XXX I know the prologue shouldn't be pushing registers, but it is faster
14755    that way.  */
14756
14757 static void
14758 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14759                             enum machine_mode mode ATTRIBUTE_UNUSED,
14760                             tree type ATTRIBUTE_UNUSED,
14761                             int *pretend_size,
14762                             int second_time ATTRIBUTE_UNUSED)
14763 {
14764   cfun->machine->uses_anonymous_args = 1;
14765   if (cum->nregs < NUM_ARG_REGS)
14766     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14767 }
14768
14769 /* Return nonzero if the CONSUMER instruction (a store) does not need
14770    PRODUCER's value to calculate the address.  */
14771
14772 int
14773 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14774 {
14775   rtx value = PATTERN (producer);
14776   rtx addr = PATTERN (consumer);
14777
14778   if (GET_CODE (value) == COND_EXEC)
14779     value = COND_EXEC_CODE (value);
14780   if (GET_CODE (value) == PARALLEL)
14781     value = XVECEXP (value, 0, 0);
14782   value = XEXP (value, 0);
14783   if (GET_CODE (addr) == COND_EXEC)
14784     addr = COND_EXEC_CODE (addr);
14785   if (GET_CODE (addr) == PARALLEL)
14786     addr = XVECEXP (addr, 0, 0);
14787   addr = XEXP (addr, 0);
14788
14789   return !reg_overlap_mentioned_p (value, addr);
14790 }
14791
14792 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14793    have an early register shift value or amount dependency on the
14794    result of PRODUCER.  */
14795
14796 int
14797 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14798 {
14799   rtx value = PATTERN (producer);
14800   rtx op = PATTERN (consumer);
14801   rtx early_op;
14802
14803   if (GET_CODE (value) == COND_EXEC)
14804     value = COND_EXEC_CODE (value);
14805   if (GET_CODE (value) == PARALLEL)
14806     value = XVECEXP (value, 0, 0);
14807   value = XEXP (value, 0);
14808   if (GET_CODE (op) == COND_EXEC)
14809     op = COND_EXEC_CODE (op);
14810   if (GET_CODE (op) == PARALLEL)
14811     op = XVECEXP (op, 0, 0);
14812   op = XEXP (op, 1);
14813
14814   early_op = XEXP (op, 0);
14815   /* This is either an actual independent shift, or a shift applied to
14816      the first operand of another operation.  We want the whole shift
14817      operation.  */
14818   if (GET_CODE (early_op) == REG)
14819     early_op = op;
14820
14821   return !reg_overlap_mentioned_p (value, early_op);
14822 }
14823
14824 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14825    have an early register shift value dependency on the result of
14826    PRODUCER.  */
14827
14828 int
14829 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14830 {
14831   rtx value = PATTERN (producer);
14832   rtx op = PATTERN (consumer);
14833   rtx early_op;
14834
14835   if (GET_CODE (value) == COND_EXEC)
14836     value = COND_EXEC_CODE (value);
14837   if (GET_CODE (value) == PARALLEL)
14838     value = XVECEXP (value, 0, 0);
14839   value = XEXP (value, 0);
14840   if (GET_CODE (op) == COND_EXEC)
14841     op = COND_EXEC_CODE (op);
14842   if (GET_CODE (op) == PARALLEL)
14843     op = XVECEXP (op, 0, 0);
14844   op = XEXP (op, 1);
14845
14846   early_op = XEXP (op, 0);
14847
14848   /* This is either an actual independent shift, or a shift applied to
14849      the first operand of another operation.  We want the value being
14850      shifted, in either case.  */
14851   if (GET_CODE (early_op) != REG)
14852     early_op = XEXP (early_op, 0);
14853
14854   return !reg_overlap_mentioned_p (value, early_op);
14855 }
14856
14857 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14858    have an early register mult dependency on the result of
14859    PRODUCER.  */
14860
14861 int
14862 arm_no_early_mul_dep (rtx producer, rtx consumer)
14863 {
14864   rtx value = PATTERN (producer);
14865   rtx op = PATTERN (consumer);
14866
14867   if (GET_CODE (value) == COND_EXEC)
14868     value = COND_EXEC_CODE (value);
14869   if (GET_CODE (value) == PARALLEL)
14870     value = XVECEXP (value, 0, 0);
14871   value = XEXP (value, 0);
14872   if (GET_CODE (op) == COND_EXEC)
14873     op = COND_EXEC_CODE (op);
14874   if (GET_CODE (op) == PARALLEL)
14875     op = XVECEXP (op, 0, 0);
14876   op = XEXP (op, 1);
14877
14878   return (GET_CODE (op) == PLUS
14879           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14880 }
14881
14882
14883 /* We can't rely on the caller doing the proper promotion when
14884    using APCS or ATPCS.  */
14885
14886 static bool
14887 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14888 {
14889     return !TARGET_AAPCS_BASED;
14890 }
14891
14892
14893 /* AAPCS based ABIs use short enums by default.  */
14894
14895 static bool
14896 arm_default_short_enums (void)
14897 {
14898   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
14899 }
14900
14901
14902 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14903
14904 static bool
14905 arm_align_anon_bitfield (void)
14906 {
14907   return TARGET_AAPCS_BASED;
14908 }
14909
14910
14911 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14912
14913 static tree
14914 arm_cxx_guard_type (void)
14915 {
14916   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14917 }
14918
14919
14920 /* The EABI says test the least significant bit of a guard variable.  */
14921
14922 static bool
14923 arm_cxx_guard_mask_bit (void)
14924 {
14925   return TARGET_AAPCS_BASED;
14926 }
14927
14928
14929 /* The EABI specifies that all array cookies are 8 bytes long.  */
14930
14931 static tree
14932 arm_get_cookie_size (tree type)
14933 {
14934   tree size;
14935
14936   if (!TARGET_AAPCS_BASED)
14937     return default_cxx_get_cookie_size (type);
14938
14939   size = build_int_cst (sizetype, 8);
14940   return size;
14941 }
14942
14943
14944 /* The EABI says that array cookies should also contain the element size.  */
14945
14946 static bool
14947 arm_cookie_has_size (void)
14948 {
14949   return TARGET_AAPCS_BASED;
14950 }
14951
14952
14953 /* The EABI says constructors and destructors should return a pointer to
14954    the object constructed/destroyed.  */
14955
14956 static bool
14957 arm_cxx_cdtor_returns_this (void)
14958 {
14959   return TARGET_AAPCS_BASED;
14960 }
14961
14962 /* The EABI says that an inline function may never be the key
14963    method.  */
14964
14965 static bool
14966 arm_cxx_key_method_may_be_inline (void)
14967 {
14968   return !TARGET_AAPCS_BASED;
14969 }
14970
14971 static void
14972 arm_cxx_determine_class_data_visibility (tree decl)
14973 {
14974   if (!TARGET_AAPCS_BASED)
14975     return;
14976
14977   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
14978      is exported.  However, on systems without dynamic vague linkage,
14979      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
14980   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
14981     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
14982   else
14983     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
14984   DECL_VISIBILITY_SPECIFIED (decl) = 1;
14985 }
14986
14987 static bool
14988 arm_cxx_class_data_always_comdat (void)
14989 {
14990   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
14991      vague linkage if the class has no key function.  */
14992   return !TARGET_AAPCS_BASED;
14993 }
14994
14995
14996 /* The EABI says __aeabi_atexit should be used to register static
14997    destructors.  */
14998
14999 static bool
15000 arm_cxx_use_aeabi_atexit (void)
15001 {
15002   return TARGET_AAPCS_BASED;
15003 }
15004
15005
15006 void
15007 arm_set_return_address (rtx source, rtx scratch)
15008 {
15009   arm_stack_offsets *offsets;
15010   HOST_WIDE_INT delta;
15011   rtx addr;
15012   unsigned long saved_regs;
15013
15014   saved_regs = arm_compute_save_reg_mask ();
15015
15016   if ((saved_regs & (1 << LR_REGNUM)) == 0)
15017     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15018   else
15019     {
15020       if (frame_pointer_needed)
15021         addr = plus_constant(hard_frame_pointer_rtx, -4);
15022       else
15023         {
15024           /* LR will be the first saved register.  */
15025           offsets = arm_get_frame_offsets ();
15026           delta = offsets->outgoing_args - (offsets->frame + 4);
15027
15028
15029           if (delta >= 4096)
15030             {
15031               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15032                                      GEN_INT (delta & ~4095)));
15033               addr = scratch;
15034               delta &= 4095;
15035             }
15036           else
15037             addr = stack_pointer_rtx;
15038
15039           addr = plus_constant (addr, delta);
15040         }
15041       emit_move_insn (gen_frame_mem (Pmode, addr), source);
15042     }
15043 }
15044
15045
15046 void
15047 thumb_set_return_address (rtx source, rtx scratch)
15048 {
15049   arm_stack_offsets *offsets;
15050   HOST_WIDE_INT delta;
15051   int reg;
15052   rtx addr;
15053   unsigned long mask;
15054
15055   emit_insn (gen_rtx_USE (VOIDmode, source));
15056
15057   mask = thumb_compute_save_reg_mask ();
15058   if (mask & (1 << LR_REGNUM))
15059     {
15060       offsets = arm_get_frame_offsets ();
15061
15062       /* Find the saved regs.  */
15063       if (frame_pointer_needed)
15064         {
15065           delta = offsets->soft_frame - offsets->saved_args;
15066           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
15067         }
15068       else
15069         {
15070           delta = offsets->outgoing_args - offsets->saved_args;
15071           reg = SP_REGNUM;
15072         }
15073       /* Allow for the stack frame.  */
15074       if (TARGET_BACKTRACE)
15075         delta -= 16;
15076       /* The link register is always the first saved register.  */
15077       delta -= 4;
15078
15079       /* Construct the address.  */
15080       addr = gen_rtx_REG (SImode, reg);
15081       if ((reg != SP_REGNUM && delta >= 128)
15082           || delta >= 1024)
15083         {
15084           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
15085           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
15086           addr = scratch;
15087         }
15088       else
15089         addr = plus_constant (addr, delta);
15090
15091       emit_move_insn (gen_frame_mem (Pmode, addr), source);
15092     }
15093   else
15094     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15095 }
15096
15097 /* Implements target hook vector_mode_supported_p.  */
15098 bool
15099 arm_vector_mode_supported_p (enum machine_mode mode)
15100 {
15101   if ((mode == V2SImode)
15102       || (mode == V4HImode)
15103       || (mode == V8QImode))
15104     return true;
15105
15106   return false;
15107 }
15108
15109 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
15110    ARM insns and therefore guarantee that the shift count is modulo 256.
15111    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
15112    guarantee no particular behavior for out-of-range counts.  */
15113
15114 static unsigned HOST_WIDE_INT
15115 arm_shift_truncation_mask (enum machine_mode mode)
15116 {
15117   return mode == SImode ? 255 : 0;
15118 }
15119
15120
15121 /* Map internal gcc register numbers to DWARF2 register numbers.  */
15122
15123 unsigned int
15124 arm_dbx_register_number (unsigned int regno)
15125 {
15126   if (regno < 16)
15127     return regno;
15128
15129   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
15130      compatibility.  The EABI defines them as registers 96-103.  */
15131   if (IS_FPA_REGNUM (regno))
15132     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
15133
15134   if (IS_VFP_REGNUM (regno))
15135     return 64 + regno - FIRST_VFP_REGNUM;
15136
15137   if (IS_IWMMXT_GR_REGNUM (regno))
15138     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
15139
15140   if (IS_IWMMXT_REGNUM (regno))
15141     return 112 + regno - FIRST_IWMMXT_REGNUM;
15142
15143   gcc_unreachable ();
15144 }
15145
15146
15147 #ifdef TARGET_UNWIND_INFO
15148 /* Emit unwind directives for a store-multiple instruction.  This should
15149    only ever be generated by the function prologue code, so we expect it
15150    to have a particular form.  */
15151
15152 static void
15153 arm_unwind_emit_stm (FILE * asm_out_file, rtx p)
15154 {
15155   int i;
15156   HOST_WIDE_INT offset;
15157   HOST_WIDE_INT nregs;
15158   int reg_size;
15159   unsigned reg;
15160   unsigned lastreg;
15161   rtx e;
15162
15163   /* First insn will adjust the stack pointer.  */
15164   e = XVECEXP (p, 0, 0);
15165   if (GET_CODE (e) != SET
15166       || GET_CODE (XEXP (e, 0)) != REG
15167       || REGNO (XEXP (e, 0)) != SP_REGNUM
15168       || GET_CODE (XEXP (e, 1)) != PLUS)
15169     abort ();
15170
15171   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
15172   nregs = XVECLEN (p, 0) - 1;
15173
15174   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
15175   if (reg < 16)
15176     {
15177       /* The function prologue may also push pc, but not annotate it as it is
15178          never restored.  We turn this into a stack pointer adjustment.  */
15179       if (nregs * 4 == offset - 4)
15180         {
15181           fprintf (asm_out_file, "\t.pad #4\n");
15182           offset -= 4;
15183         }
15184       reg_size = 4;
15185     }
15186   else if (IS_VFP_REGNUM (reg))
15187     {
15188       /* FPA register saves use an additional word.  */
15189       offset -= 4;
15190       reg_size = 8;
15191     }
15192   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
15193     {
15194       /* FPA registers are done differently.  */
15195       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
15196       return;
15197     }
15198   else
15199     /* Unknown register type.  */
15200     abort ();
15201
15202   /* If the stack increment doesn't match the size of the saved registers,
15203      something has gone horribly wrong.  */
15204   if (offset != nregs * reg_size)
15205     abort ();
15206
15207   fprintf (asm_out_file, "\t.save {");
15208
15209   offset = 0;
15210   lastreg = 0;
15211   /* The remaining insns will describe the stores.  */
15212   for (i = 1; i <= nregs; i++)
15213     {
15214       /* Expect (set (mem <addr>) (reg)).
15215          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
15216       e = XVECEXP (p, 0, i);
15217       if (GET_CODE (e) != SET
15218           || GET_CODE (XEXP (e, 0)) != MEM
15219           || GET_CODE (XEXP (e, 1)) != REG)
15220         abort ();
15221
15222       reg = REGNO (XEXP (e, 1));
15223       if (reg < lastreg)
15224         abort ();
15225
15226       if (i != 1)
15227         fprintf (asm_out_file, ", ");
15228       /* We can't use %r for vfp because we need to use the
15229          double precision register names.  */
15230       if (IS_VFP_REGNUM (reg))
15231         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
15232       else
15233         asm_fprintf (asm_out_file, "%r", reg);
15234
15235 #ifdef ENABLE_CHECKING
15236       /* Check that the addresses are consecutive.  */
15237       e = XEXP (XEXP (e, 0), 0);
15238       if (GET_CODE (e) == PLUS)
15239         {
15240           offset += reg_size;
15241           if (GET_CODE (XEXP (e, 0)) != REG
15242               || REGNO (XEXP (e, 0)) != SP_REGNUM
15243               || GET_CODE (XEXP (e, 1)) != CONST_INT
15244               || offset != INTVAL (XEXP (e, 1)))
15245             abort ();
15246         }
15247       else if (i != 1
15248                || GET_CODE (e) != REG
15249                || REGNO (e) != SP_REGNUM)
15250         abort ();
15251 #endif
15252     }
15253   fprintf (asm_out_file, "}\n");
15254 }
15255
15256 /*  Emit unwind directives for a SET.  */
15257
15258 static void
15259 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
15260 {
15261   rtx e0;
15262   rtx e1;
15263
15264   e0 = XEXP (p, 0);
15265   e1 = XEXP (p, 1);
15266   switch (GET_CODE (e0))
15267     {
15268     case MEM:
15269       /* Pushing a single register.  */
15270       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
15271           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
15272           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
15273         abort ();
15274
15275       asm_fprintf (asm_out_file, "\t.save ");
15276       if (IS_VFP_REGNUM (REGNO (e1)))
15277         asm_fprintf(asm_out_file, "{d%d}\n",
15278                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
15279       else
15280         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
15281       break;
15282
15283     case REG:
15284       if (REGNO (e0) == SP_REGNUM)
15285         {
15286           /* A stack increment.  */
15287           if (GET_CODE (e1) != PLUS
15288               || GET_CODE (XEXP (e1, 0)) != REG
15289               || REGNO (XEXP (e1, 0)) != SP_REGNUM
15290               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15291             abort ();
15292
15293           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
15294                        -INTVAL (XEXP (e1, 1)));
15295         }
15296       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
15297         {
15298           HOST_WIDE_INT offset;
15299           unsigned reg;
15300
15301           if (GET_CODE (e1) == PLUS)
15302             {
15303               if (GET_CODE (XEXP (e1, 0)) != REG
15304                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15305                 abort ();
15306               reg = REGNO (XEXP (e1, 0));
15307               offset = INTVAL (XEXP (e1, 1));
15308               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
15309                            HARD_FRAME_POINTER_REGNUM, reg,
15310                            INTVAL (XEXP (e1, 1)));
15311             }
15312           else if (GET_CODE (e1) == REG)
15313             {
15314               reg = REGNO (e1);
15315               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
15316                            HARD_FRAME_POINTER_REGNUM, reg);
15317             }
15318           else
15319             abort ();
15320         }
15321       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
15322         {
15323           /* Move from sp to reg.  */
15324           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
15325         }
15326       else
15327         abort ();
15328       break;
15329
15330     default:
15331       abort ();
15332     }
15333 }
15334
15335
15336 /* Emit unwind directives for the given insn.  */
15337
15338 static void
15339 arm_unwind_emit (FILE * asm_out_file, rtx insn)
15340 {
15341   rtx pat;
15342
15343   if (!ARM_EABI_UNWIND_TABLES)
15344     return;
15345
15346   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
15347     return;
15348
15349   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
15350   if (pat)
15351     pat = XEXP (pat, 0);
15352   else
15353     pat = PATTERN (insn);
15354
15355   switch (GET_CODE (pat))
15356     {
15357     case SET:
15358       arm_unwind_emit_set (asm_out_file, pat);
15359       break;
15360
15361     case SEQUENCE:
15362       /* Store multiple.  */
15363       arm_unwind_emit_stm (asm_out_file, pat);
15364       break;
15365
15366     default:
15367       abort();
15368     }
15369 }
15370
15371
15372 /* Output a reference from a function exception table to the type_info
15373    object X.  The EABI specifies that the symbol should be relocated by
15374    an R_ARM_TARGET2 relocation.  */
15375
15376 static bool
15377 arm_output_ttype (rtx x)
15378 {
15379   fputs ("\t.word\t", asm_out_file);
15380   output_addr_const (asm_out_file, x);
15381   /* Use special relocations for symbol references.  */
15382   if (GET_CODE (x) != CONST_INT)
15383     fputs ("(TARGET2)", asm_out_file);
15384   fputc ('\n', asm_out_file);
15385
15386   return TRUE;
15387 }
15388 #endif /* TARGET_UNWIND_INFO */
15389
15390
15391 /* Output unwind directives for the start/end of a function.  */
15392
15393 void
15394 arm_output_fn_unwind (FILE * f, bool prologue)
15395 {
15396   if (!ARM_EABI_UNWIND_TABLES)
15397     return;
15398
15399   if (prologue)
15400     fputs ("\t.fnstart\n", f);
15401   else
15402     fputs ("\t.fnend\n", f);
15403 }
15404
15405 static bool
15406 arm_emit_tls_decoration (FILE *fp, rtx x)
15407 {
15408   enum tls_reloc reloc;
15409   rtx val;
15410
15411   val = XVECEXP (x, 0, 0);
15412   reloc = INTVAL (XVECEXP (x, 0, 1));
15413
15414   output_addr_const (fp, val);
15415
15416   switch (reloc)
15417     {
15418     case TLS_GD32:
15419       fputs ("(tlsgd)", fp);
15420       break;
15421     case TLS_LDM32:
15422       fputs ("(tlsldm)", fp);
15423       break;
15424     case TLS_LDO32:
15425       fputs ("(tlsldo)", fp);
15426       break;
15427     case TLS_IE32:
15428       fputs ("(gottpoff)", fp);
15429       break;
15430     case TLS_LE32:
15431       fputs ("(tpoff)", fp);
15432       break;
15433     default:
15434       gcc_unreachable ();
15435     }
15436
15437   switch (reloc)
15438     {
15439     case TLS_GD32:
15440     case TLS_LDM32:
15441     case TLS_IE32:
15442       fputs (" + (. - ", fp);
15443       output_addr_const (fp, XVECEXP (x, 0, 2));
15444       fputs (" - ", fp);
15445       output_addr_const (fp, XVECEXP (x, 0, 3));
15446       fputc (')', fp);
15447       break;
15448     default:
15449       break;
15450     }
15451
15452   return TRUE;
15453 }
15454
15455 bool
15456 arm_output_addr_const_extra (FILE *fp, rtx x)
15457 {
15458   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
15459     return arm_emit_tls_decoration (fp, x);
15460   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
15461     {
15462       char label[256];
15463       int labelno = INTVAL (XVECEXP (x, 0, 0));
15464
15465       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
15466       assemble_name_raw (fp, label);
15467
15468       return TRUE;
15469     }
15470   else if (GET_CODE (x) == CONST_VECTOR)
15471     return arm_emit_vector_const (fp, x);
15472
15473   return FALSE;
15474 }
15475
15476 #include "gt-arm.h"