OSDN Git Service

4e9ae1b6e3f0abc527a7983121f2ddf19dc57bf5
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006  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 rtx emit_set_insn (rtx, rtx);
146 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
147                                   tree, bool);
148
149 #ifdef OBJECT_FORMAT_ELF
150 static void arm_elf_asm_constructor (rtx, int);
151 #endif
152 #ifndef ARM_PE
153 static void arm_encode_section_info (tree, rtx, int);
154 #endif
155
156 static void arm_file_end (void);
157
158 #ifdef AOF_ASSEMBLER
159 static void aof_globalize_label (FILE *, const char *);
160 static void aof_dump_imports (FILE *);
161 static void aof_dump_pic_table (FILE *);
162 static void aof_file_start (void);
163 static void aof_file_end (void);
164 static void aof_asm_init_sections (void);
165 #endif
166 static rtx arm_struct_value_rtx (tree, int);
167 static void arm_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
168                                         tree, int *, int);
169 static bool arm_pass_by_reference (CUMULATIVE_ARGS *,
170                                    enum machine_mode, tree, bool);
171 static bool arm_promote_prototypes (tree);
172 static bool arm_default_short_enums (void);
173 static bool arm_align_anon_bitfield (void);
174 static bool arm_return_in_msb (tree);
175 static bool arm_must_pass_in_stack (enum machine_mode, tree);
176 #ifdef TARGET_UNWIND_INFO
177 static void arm_unwind_emit (FILE *, rtx);
178 static bool arm_output_ttype (rtx);
179 #endif
180
181 static tree arm_cxx_guard_type (void);
182 static bool arm_cxx_guard_mask_bit (void);
183 static tree arm_get_cookie_size (tree);
184 static bool arm_cookie_has_size (void);
185 static bool arm_cxx_cdtor_returns_this (void);
186 static bool arm_cxx_key_method_may_be_inline (void);
187 static void arm_cxx_determine_class_data_visibility (tree);
188 static bool arm_cxx_class_data_always_comdat (void);
189 static bool arm_cxx_use_aeabi_atexit (void);
190 static void arm_init_libfuncs (void);
191 static bool arm_handle_option (size_t, const char *, int);
192 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
193 static bool arm_cannot_copy_insn_p (rtx);
194 static bool arm_tls_symbol_p (rtx x);
195
196 \f
197 /* Initialize the GCC target structure.  */
198 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
199 #undef  TARGET_MERGE_DECL_ATTRIBUTES
200 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
201 #endif
202
203 #undef  TARGET_ATTRIBUTE_TABLE
204 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
205
206 #undef TARGET_ASM_FILE_END
207 #define TARGET_ASM_FILE_END arm_file_end
208
209 #ifdef AOF_ASSEMBLER
210 #undef  TARGET_ASM_BYTE_OP
211 #define TARGET_ASM_BYTE_OP "\tDCB\t"
212 #undef  TARGET_ASM_ALIGNED_HI_OP
213 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
214 #undef  TARGET_ASM_ALIGNED_SI_OP
215 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
216 #undef TARGET_ASM_GLOBALIZE_LABEL
217 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
218 #undef TARGET_ASM_FILE_START
219 #define TARGET_ASM_FILE_START aof_file_start
220 #undef TARGET_ASM_FILE_END
221 #define TARGET_ASM_FILE_END aof_file_end
222 #else
223 #undef  TARGET_ASM_ALIGNED_SI_OP
224 #define TARGET_ASM_ALIGNED_SI_OP NULL
225 #undef  TARGET_ASM_INTEGER
226 #define TARGET_ASM_INTEGER arm_assemble_integer
227 #endif
228
229 #undef  TARGET_ASM_FUNCTION_PROLOGUE
230 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
231
232 #undef  TARGET_ASM_FUNCTION_EPILOGUE
233 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
234
235 #undef  TARGET_DEFAULT_TARGET_FLAGS
236 #define TARGET_DEFAULT_TARGET_FLAGS (TARGET_DEFAULT | MASK_SCHED_PROLOG)
237 #undef  TARGET_HANDLE_OPTION
238 #define TARGET_HANDLE_OPTION arm_handle_option
239
240 #undef  TARGET_COMP_TYPE_ATTRIBUTES
241 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
242
243 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
244 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
245
246 #undef  TARGET_SCHED_ADJUST_COST
247 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
248
249 #undef TARGET_ENCODE_SECTION_INFO
250 #ifdef ARM_PE
251 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
252 #else
253 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
254 #endif
255
256 #undef  TARGET_STRIP_NAME_ENCODING
257 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
258
259 #undef  TARGET_ASM_INTERNAL_LABEL
260 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
261
262 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
263 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
264
265 #undef  TARGET_ASM_OUTPUT_MI_THUNK
266 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
267 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
268 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
269
270 /* This will be overridden in arm_override_options.  */
271 #undef  TARGET_RTX_COSTS
272 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
273 #undef  TARGET_ADDRESS_COST
274 #define TARGET_ADDRESS_COST arm_address_cost
275
276 #undef TARGET_SHIFT_TRUNCATION_MASK
277 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
278 #undef TARGET_VECTOR_MODE_SUPPORTED_P
279 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
280
281 #undef  TARGET_MACHINE_DEPENDENT_REORG
282 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
283
284 #undef  TARGET_INIT_BUILTINS
285 #define TARGET_INIT_BUILTINS  arm_init_builtins
286 #undef  TARGET_EXPAND_BUILTIN
287 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
288
289 #undef TARGET_INIT_LIBFUNCS
290 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
291
292 #undef TARGET_PROMOTE_FUNCTION_ARGS
293 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
294 #undef TARGET_PROMOTE_FUNCTION_RETURN
295 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
296 #undef TARGET_PROMOTE_PROTOTYPES
297 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
298 #undef TARGET_PASS_BY_REFERENCE
299 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
300 #undef TARGET_ARG_PARTIAL_BYTES
301 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
302
303 #undef TARGET_STRUCT_VALUE_RTX
304 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
305
306 #undef  TARGET_SETUP_INCOMING_VARARGS
307 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
308
309 #undef TARGET_DEFAULT_SHORT_ENUMS
310 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
311
312 #undef TARGET_ALIGN_ANON_BITFIELD
313 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
314
315 #undef TARGET_CXX_GUARD_TYPE
316 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
317
318 #undef TARGET_CXX_GUARD_MASK_BIT
319 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
320
321 #undef TARGET_CXX_GET_COOKIE_SIZE
322 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
323
324 #undef TARGET_CXX_COOKIE_HAS_SIZE
325 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
326
327 #undef TARGET_CXX_CDTOR_RETURNS_THIS
328 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
329
330 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
331 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
332
333 #undef TARGET_CXX_USE_AEABI_ATEXIT
334 #define TARGET_CXX_USE_AEABI_ATEXIT arm_cxx_use_aeabi_atexit
335
336 #undef TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY
337 #define TARGET_CXX_DETERMINE_CLASS_DATA_VISIBILITY \
338   arm_cxx_determine_class_data_visibility
339
340 #undef TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT
341 #define TARGET_CXX_CLASS_DATA_ALWAYS_COMDAT arm_cxx_class_data_always_comdat
342
343 #undef TARGET_RETURN_IN_MSB
344 #define TARGET_RETURN_IN_MSB arm_return_in_msb
345
346 #undef TARGET_MUST_PASS_IN_STACK
347 #define TARGET_MUST_PASS_IN_STACK arm_must_pass_in_stack
348
349 #ifdef TARGET_UNWIND_INFO
350 #undef TARGET_UNWIND_EMIT
351 #define TARGET_UNWIND_EMIT arm_unwind_emit
352
353 /* EABI unwinding tables use a different format for the typeinfo tables.  */
354 #undef TARGET_ASM_TTYPE
355 #define TARGET_ASM_TTYPE arm_output_ttype
356
357 #undef TARGET_ARM_EABI_UNWINDER
358 #define TARGET_ARM_EABI_UNWINDER true
359 #endif /* TARGET_UNWIND_INFO */
360
361 #undef  TARGET_CANNOT_COPY_INSN_P
362 #define TARGET_CANNOT_COPY_INSN_P arm_cannot_copy_insn_p
363
364 #ifdef HAVE_AS_TLS
365 #undef TARGET_HAVE_TLS
366 #define TARGET_HAVE_TLS true
367 #endif
368
369 #undef TARGET_CANNOT_FORCE_CONST_MEM
370 #define TARGET_CANNOT_FORCE_CONST_MEM arm_tls_referenced_p
371
372 struct gcc_target targetm = TARGET_INITIALIZER;
373 \f
374 /* Obstack for minipool constant handling.  */
375 static struct obstack minipool_obstack;
376 static char *         minipool_startobj;
377
378 /* The maximum number of insns skipped which
379    will be conditionalised if possible.  */
380 static int max_insns_skipped = 5;
381
382 extern FILE * asm_out_file;
383
384 /* True if we are currently building a constant table.  */
385 int making_const_table;
386
387 /* Define the information needed to generate branch insns.  This is
388    stored from the compare operation.  */
389 rtx arm_compare_op0, arm_compare_op1;
390
391 /* The processor for which instructions should be scheduled.  */
392 enum processor_type arm_tune = arm_none;
393
394 /* Which floating point model to use.  */
395 enum arm_fp_model arm_fp_model;
396
397 /* Which floating point hardware is available.  */
398 enum fputype arm_fpu_arch;
399
400 /* Which floating point hardware to schedule for.  */
401 enum fputype arm_fpu_tune;
402
403 /* Whether to use floating point hardware.  */
404 enum float_abi_type arm_float_abi;
405
406 /* Which ABI to use.  */
407 enum arm_abi_type arm_abi;
408
409 /* Which thread pointer model to use.  */
410 enum arm_tp_type target_thread_pointer = TP_AUTO;
411
412 /* Used to parse -mstructure_size_boundary command line option.  */
413 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
414
415 /* Used for Thumb call_via trampolines.  */
416 rtx thumb_call_via_label[14];
417 static int thumb_call_reg_needed;
418
419 /* Bit values used to identify processor capabilities.  */
420 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
421 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
422 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
423 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
424 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
425 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
426 #define FL_THUMB      (1 << 6)        /* Thumb aware */
427 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
428 #define FL_STRONG     (1 << 8)        /* StrongARM */
429 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
430 #define FL_XSCALE     (1 << 10)       /* XScale */
431 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
432 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
433                                          media instructions.  */
434 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
435 #define FL_WBUF       (1 << 14)       /* Schedule for write buffer ops.
436                                          Note: ARM6 & 7 derivatives only.  */
437 #define FL_ARCH6K     (1 << 15)       /* Architecture rel 6 K extensions.  */
438
439 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
440
441 #define FL_FOR_ARCH2    0
442 #define FL_FOR_ARCH3    FL_MODE32
443 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
444 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
445 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
446 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
447 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
448 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
449 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
450 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
451 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
452 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
453 #define FL_FOR_ARCH6K   (FL_FOR_ARCH6 | FL_ARCH6K)
454 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
455 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6K
456
457 /* The bits in this mask specify which
458    instructions we are allowed to generate.  */
459 static unsigned long insn_flags = 0;
460
461 /* The bits in this mask specify which instruction scheduling options should
462    be used.  */
463 static unsigned long tune_flags = 0;
464
465 /* The following are used in the arm.md file as equivalents to bits
466    in the above two flag variables.  */
467
468 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
469 int arm_arch3m = 0;
470
471 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
472 int arm_arch4 = 0;
473
474 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
475 int arm_arch4t = 0;
476
477 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
478 int arm_arch5 = 0;
479
480 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
481 int arm_arch5e = 0;
482
483 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
484 int arm_arch6 = 0;
485
486 /* Nonzero if this chip supports the ARM 6K extensions.  */
487 int arm_arch6k = 0;
488
489 /* Nonzero if this chip can benefit from load scheduling.  */
490 int arm_ld_sched = 0;
491
492 /* Nonzero if this chip is a StrongARM.  */
493 int arm_tune_strongarm = 0;
494
495 /* Nonzero if this chip is a Cirrus variant.  */
496 int arm_arch_cirrus = 0;
497
498 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
499 int arm_arch_iwmmxt = 0;
500
501 /* Nonzero if this chip is an XScale.  */
502 int arm_arch_xscale = 0;
503
504 /* Nonzero if tuning for XScale  */
505 int arm_tune_xscale = 0;
506
507 /* Nonzero if we want to tune for stores that access the write-buffer.
508    This typically means an ARM6 or ARM7 with MMU or MPU.  */
509 int arm_tune_wbuf = 0;
510
511 /* Nonzero if generating Thumb instructions.  */
512 int thumb_code = 0;
513
514 /* Nonzero if we should define __THUMB_INTERWORK__ in the
515    preprocessor.
516    XXX This is a bit of a hack, it's intended to help work around
517    problems in GLD which doesn't understand that armv5t code is
518    interworking clean.  */
519 int arm_cpp_interwork = 0;
520
521 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
522    must report the mode of the memory reference from PRINT_OPERAND to
523    PRINT_OPERAND_ADDRESS.  */
524 enum machine_mode output_memory_reference_mode;
525
526 /* The register number to be used for the PIC offset register.  */
527 unsigned arm_pic_register = INVALID_REGNUM;
528
529 /* Set to 1 when a return insn is output, this means that the epilogue
530    is not needed.  */
531 int return_used_this_function;
532
533 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
534    the next function.  */
535 static int after_arm_reorg = 0;
536
537 /* The maximum number of insns to be used when loading a constant.  */
538 static int arm_constant_limit = 3;
539
540 /* For an explanation of these variables, see final_prescan_insn below.  */
541 int arm_ccfsm_state;
542 enum arm_cond_code arm_current_cc;
543 rtx arm_target_insn;
544 int arm_target_label;
545
546 /* The condition codes of the ARM, and the inverse function.  */
547 static const char * const arm_condition_codes[] =
548 {
549   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
550   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
551 };
552
553 #define streq(string1, string2) (strcmp (string1, string2) == 0)
554 \f
555 /* Initialization code.  */
556
557 struct processors
558 {
559   const char *const name;
560   enum processor_type core;
561   const char *arch;
562   const unsigned long flags;
563   bool (* rtx_costs) (rtx, int, int, int *);
564 };
565
566 /* Not all of these give usefully different compilation alternatives,
567    but there is no simple way of generalizing them.  */
568 static const struct processors all_cores[] =
569 {
570   /* ARM Cores */
571 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
572   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
573 #include "arm-cores.def"
574 #undef ARM_CORE
575   {NULL, arm_none, NULL, 0, NULL}
576 };
577
578 static const struct processors all_architectures[] =
579 {
580   /* ARM Architectures */
581   /* We don't specify rtx_costs here as it will be figured out
582      from the core.  */
583
584   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
585   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
586   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
587   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
588   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
589   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
590      implementations that support it, so we will leave it out for now.  */
591   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
592   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
593   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
594   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
595   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
596   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
597   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
598   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
599   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
600   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
601   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
602   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
603   {NULL, arm_none, NULL, 0 , NULL}
604 };
605
606 struct arm_cpu_select
607 {
608   const char *              string;
609   const char *              name;
610   const struct processors * processors;
611 };
612
613 /* This is a magic structure.  The 'string' field is magically filled in
614    with a pointer to the value specified by the user on the command line
615    assuming that the user has specified such a value.  */
616
617 static struct arm_cpu_select arm_select[] =
618 {
619   /* string       name            processors  */
620   { NULL,       "-mcpu=",       all_cores  },
621   { NULL,       "-march=",      all_architectures },
622   { NULL,       "-mtune=",      all_cores }
623 };
624
625 /* Defines representing the indexes into the above table.  */
626 #define ARM_OPT_SET_CPU 0
627 #define ARM_OPT_SET_ARCH 1
628 #define ARM_OPT_SET_TUNE 2
629
630 /* The name of the proprocessor macro to define for this architecture.  */
631
632 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
633
634 struct fpu_desc
635 {
636   const char * name;
637   enum fputype fpu;
638 };
639
640
641 /* Available values for -mfpu=.  */
642
643 static const struct fpu_desc all_fpus[] =
644 {
645   {"fpa",       FPUTYPE_FPA},
646   {"fpe2",      FPUTYPE_FPA_EMU2},
647   {"fpe3",      FPUTYPE_FPA_EMU2},
648   {"maverick",  FPUTYPE_MAVERICK},
649   {"vfp",       FPUTYPE_VFP}
650 };
651
652
653 /* Floating point models used by the different hardware.
654    See fputype in arm.h.  */
655
656 static const enum fputype fp_model_for_fpu[] =
657 {
658   /* No FP hardware.  */
659   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
660   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
661   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
662   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
663   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
664   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
665 };
666
667
668 struct float_abi
669 {
670   const char * name;
671   enum float_abi_type abi_type;
672 };
673
674
675 /* Available values for -mfloat-abi=.  */
676
677 static const struct float_abi all_float_abis[] =
678 {
679   {"soft",      ARM_FLOAT_ABI_SOFT},
680   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
681   {"hard",      ARM_FLOAT_ABI_HARD}
682 };
683
684
685 struct abi_name
686 {
687   const char *name;
688   enum arm_abi_type abi_type;
689 };
690
691
692 /* Available values for -mabi=.  */
693
694 static const struct abi_name arm_all_abis[] =
695 {
696   {"apcs-gnu",    ARM_ABI_APCS},
697   {"atpcs",   ARM_ABI_ATPCS},
698   {"aapcs",   ARM_ABI_AAPCS},
699   {"iwmmxt",  ARM_ABI_IWMMXT},
700   {"aapcs-linux",   ARM_ABI_AAPCS_LINUX}
701 };
702
703 /* Supported TLS relocations.  */
704
705 enum tls_reloc {
706   TLS_GD32,
707   TLS_LDM32,
708   TLS_LDO32,
709   TLS_IE32,
710   TLS_LE32
711 };
712
713 /* Emit an insn that's a simple single-set.  Both the operands must be known
714    to be valid.  */
715 inline static rtx
716 emit_set_insn (rtx x, rtx y)
717 {
718   return emit_insn (gen_rtx_SET (VOIDmode, x, y));
719 }
720
721 /* Return the number of bits set in VALUE.  */
722 static unsigned
723 bit_count (unsigned long value)
724 {
725   unsigned long count = 0;
726
727   while (value)
728     {
729       count++;
730       value &= value - 1;  /* Clear the least-significant set bit.  */
731     }
732
733   return count;
734 }
735
736 /* Set up library functions unique to ARM.  */
737
738 static void
739 arm_init_libfuncs (void)
740 {
741   /* There are no special library functions unless we are using the
742      ARM BPABI.  */
743   if (!TARGET_BPABI)
744     return;
745
746   /* The functions below are described in Section 4 of the "Run-Time
747      ABI for the ARM architecture", Version 1.0.  */
748
749   /* Double-precision floating-point arithmetic.  Table 2.  */
750   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
751   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
752   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
753   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
754   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
755
756   /* Double-precision comparisons.  Table 3.  */
757   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
758   set_optab_libfunc (ne_optab, DFmode, NULL);
759   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
760   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
761   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
762   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
763   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
764
765   /* Single-precision floating-point arithmetic.  Table 4.  */
766   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
767   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
768   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
769   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
770   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
771
772   /* Single-precision comparisons.  Table 5.  */
773   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
774   set_optab_libfunc (ne_optab, SFmode, NULL);
775   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
776   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
777   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
778   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
779   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
780
781   /* Floating-point to integer conversions.  Table 6.  */
782   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
783   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
784   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
785   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
786   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
787   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
788   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
789   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
790
791   /* Conversions between floating types.  Table 7.  */
792   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
793   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
794
795   /* Integer to floating-point conversions.  Table 8.  */
796   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
797   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
798   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
799   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
800   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
801   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
802   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
803   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
804
805   /* Long long.  Table 9.  */
806   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
807   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
808   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
809   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
810   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
811   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
812   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
813   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
814
815   /* Integer (32/32->32) division.  \S 4.3.1.  */
816   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
817   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
818
819   /* The divmod functions are designed so that they can be used for
820      plain division, even though they return both the quotient and the
821      remainder.  The quotient is returned in the usual location (i.e.,
822      r0 for SImode, {r0, r1} for DImode), just as would be expected
823      for an ordinary division routine.  Because the AAPCS calling
824      conventions specify that all of { r0, r1, r2, r3 } are
825      callee-saved registers, there is no need to tell the compiler
826      explicitly that those registers are clobbered by these
827      routines.  */
828   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
829   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
830
831   /* For SImode division the ABI provides div-without-mod routines,
832      which are faster.  */
833   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idiv");
834   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidiv");
835
836   /* We don't have mod libcalls.  Fortunately gcc knows how to use the
837      divmod libcalls instead.  */
838   set_optab_libfunc (smod_optab, DImode, NULL);
839   set_optab_libfunc (umod_optab, DImode, NULL);
840   set_optab_libfunc (smod_optab, SImode, NULL);
841   set_optab_libfunc (umod_optab, SImode, NULL);
842 }
843
844 /* Implement TARGET_HANDLE_OPTION.  */
845
846 static bool
847 arm_handle_option (size_t code, const char *arg, int value ATTRIBUTE_UNUSED)
848 {
849   switch (code)
850     {
851     case OPT_march_:
852       arm_select[1].string = arg;
853       return true;
854
855     case OPT_mcpu_:
856       arm_select[0].string = arg;
857       return true;
858
859     case OPT_mhard_float:
860       target_float_abi_name = "hard";
861       return true;
862
863     case OPT_msoft_float:
864       target_float_abi_name = "soft";
865       return true;
866
867     case OPT_mtune_:
868       arm_select[2].string = arg;
869       return true;
870
871     default:
872       return true;
873     }
874 }
875
876 /* Fix up any incompatible options that the user has specified.
877    This has now turned into a maze.  */
878 void
879 arm_override_options (void)
880 {
881   unsigned i;
882   enum processor_type target_arch_cpu = arm_none;
883
884   /* Set up the flags based on the cpu/architecture selected by the user.  */
885   for (i = ARRAY_SIZE (arm_select); i--;)
886     {
887       struct arm_cpu_select * ptr = arm_select + i;
888
889       if (ptr->string != NULL && ptr->string[0] != '\0')
890         {
891           const struct processors * sel;
892
893           for (sel = ptr->processors; sel->name != NULL; sel++)
894             if (streq (ptr->string, sel->name))
895               {
896                 /* Set the architecture define.  */
897                 if (i != ARM_OPT_SET_TUNE)
898                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
899
900                 /* Determine the processor core for which we should
901                    tune code-generation.  */
902                 if (/* -mcpu= is a sensible default.  */
903                     i == ARM_OPT_SET_CPU
904                     /* -mtune= overrides -mcpu= and -march=.  */
905                     || i == ARM_OPT_SET_TUNE)
906                   arm_tune = (enum processor_type) (sel - ptr->processors);
907
908                 /* Remember the CPU associated with this architecture.
909                    If no other option is used to set the CPU type,
910                    we'll use this to guess the most suitable tuning
911                    options.  */
912                 if (i == ARM_OPT_SET_ARCH)
913                   target_arch_cpu = sel->core;
914
915                 if (i != ARM_OPT_SET_TUNE)
916                   {
917                     /* If we have been given an architecture and a processor
918                        make sure that they are compatible.  We only generate
919                        a warning though, and we prefer the CPU over the
920                        architecture.  */
921                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
922                       warning (0, "switch -mcpu=%s conflicts with -march= switch",
923                                ptr->string);
924
925                     insn_flags = sel->flags;
926                   }
927
928                 break;
929               }
930
931           if (sel->name == NULL)
932             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
933         }
934     }
935
936   /* Guess the tuning options from the architecture if necessary.  */
937   if (arm_tune == arm_none)
938     arm_tune = target_arch_cpu;
939
940   /* If the user did not specify a processor, choose one for them.  */
941   if (insn_flags == 0)
942     {
943       const struct processors * sel;
944       unsigned int        sought;
945       enum processor_type cpu;
946
947       cpu = TARGET_CPU_DEFAULT;
948       if (cpu == arm_none)
949         {
950 #ifdef SUBTARGET_CPU_DEFAULT
951           /* Use the subtarget default CPU if none was specified by
952              configure.  */
953           cpu = SUBTARGET_CPU_DEFAULT;
954 #endif
955           /* Default to ARM6.  */
956           if (cpu == arm_none)
957             cpu = arm6;
958         }
959       sel = &all_cores[cpu];
960
961       insn_flags = sel->flags;
962
963       /* Now check to see if the user has specified some command line
964          switch that require certain abilities from the cpu.  */
965       sought = 0;
966
967       if (TARGET_INTERWORK || TARGET_THUMB)
968         {
969           sought |= (FL_THUMB | FL_MODE32);
970
971           /* There are no ARM processors that support both APCS-26 and
972              interworking.  Therefore we force FL_MODE26 to be removed
973              from insn_flags here (if it was set), so that the search
974              below will always be able to find a compatible processor.  */
975           insn_flags &= ~FL_MODE26;
976         }
977
978       if (sought != 0 && ((sought & insn_flags) != sought))
979         {
980           /* Try to locate a CPU type that supports all of the abilities
981              of the default CPU, plus the extra abilities requested by
982              the user.  */
983           for (sel = all_cores; sel->name != NULL; sel++)
984             if ((sel->flags & sought) == (sought | insn_flags))
985               break;
986
987           if (sel->name == NULL)
988             {
989               unsigned current_bit_count = 0;
990               const struct processors * best_fit = NULL;
991
992               /* Ideally we would like to issue an error message here
993                  saying that it was not possible to find a CPU compatible
994                  with the default CPU, but which also supports the command
995                  line options specified by the programmer, and so they
996                  ought to use the -mcpu=<name> command line option to
997                  override the default CPU type.
998
999                  If we cannot find a cpu that has both the
1000                  characteristics of the default cpu and the given
1001                  command line options we scan the array again looking
1002                  for a best match.  */
1003               for (sel = all_cores; sel->name != NULL; sel++)
1004                 if ((sel->flags & sought) == sought)
1005                   {
1006                     unsigned count;
1007
1008                     count = bit_count (sel->flags & insn_flags);
1009
1010                     if (count >= current_bit_count)
1011                       {
1012                         best_fit = sel;
1013                         current_bit_count = count;
1014                       }
1015                   }
1016
1017               gcc_assert (best_fit);
1018               sel = best_fit;
1019             }
1020
1021           insn_flags = sel->flags;
1022         }
1023       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
1024       if (arm_tune == arm_none)
1025         arm_tune = (enum processor_type) (sel - all_cores);
1026     }
1027
1028   /* The processor for which we should tune should now have been
1029      chosen.  */
1030   gcc_assert (arm_tune != arm_none);
1031
1032   tune_flags = all_cores[(int)arm_tune].flags;
1033   if (optimize_size)
1034     targetm.rtx_costs = arm_size_rtx_costs;
1035   else
1036     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
1037
1038   /* Make sure that the processor choice does not conflict with any of the
1039      other command line choices.  */
1040   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
1041     {
1042       warning (0, "target CPU does not support interworking" );
1043       target_flags &= ~MASK_INTERWORK;
1044     }
1045
1046   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
1047     {
1048       warning (0, "target CPU does not support THUMB instructions");
1049       target_flags &= ~MASK_THUMB;
1050     }
1051
1052   if (TARGET_APCS_FRAME && TARGET_THUMB)
1053     {
1054       /* warning (0, "ignoring -mapcs-frame because -mthumb was used"); */
1055       target_flags &= ~MASK_APCS_FRAME;
1056     }
1057
1058   /* Callee super interworking implies thumb interworking.  Adding
1059      this to the flags here simplifies the logic elsewhere.  */
1060   if (TARGET_THUMB && TARGET_CALLEE_INTERWORKING)
1061       target_flags |= MASK_INTERWORK;
1062
1063   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
1064      from here where no function is being compiled currently.  */
1065   if ((TARGET_TPCS_FRAME || TARGET_TPCS_LEAF_FRAME) && TARGET_ARM)
1066     warning (0, "enabling backtrace support is only meaningful when compiling for the Thumb");
1067
1068   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
1069     warning (0, "enabling callee interworking support is only meaningful when compiling for the Thumb");
1070
1071   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
1072     warning (0, "enabling caller interworking support is only meaningful when compiling for the Thumb");
1073
1074   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
1075     {
1076       warning (0, "-mapcs-stack-check incompatible with -mno-apcs-frame");
1077       target_flags |= MASK_APCS_FRAME;
1078     }
1079
1080   if (TARGET_POKE_FUNCTION_NAME)
1081     target_flags |= MASK_APCS_FRAME;
1082
1083   if (TARGET_APCS_REENT && flag_pic)
1084     error ("-fpic and -mapcs-reent are incompatible");
1085
1086   if (TARGET_APCS_REENT)
1087     warning (0, "APCS reentrant code not supported.  Ignored");
1088
1089   /* If this target is normally configured to use APCS frames, warn if they
1090      are turned off and debugging is turned on.  */
1091   if (TARGET_ARM
1092       && write_symbols != NO_DEBUG
1093       && !TARGET_APCS_FRAME
1094       && (TARGET_DEFAULT & MASK_APCS_FRAME))
1095     warning (0, "-g with -mno-apcs-frame may not give sensible debugging");
1096
1097   /* If stack checking is disabled, we can use r10 as the PIC register,
1098      which keeps r9 available.  */
1099   if (flag_pic && TARGET_SINGLE_PIC_BASE)
1100     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
1101
1102   if (TARGET_APCS_FLOAT)
1103     warning (0, "passing floating point arguments in fp regs not yet supported");
1104
1105   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
1106   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
1107   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
1108   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
1109   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
1110   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
1111   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
1112   arm_arch6k = (insn_flags & FL_ARCH6K) != 0;
1113   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
1114   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
1115
1116   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
1117   arm_tune_strongarm = (tune_flags & FL_STRONG) != 0;
1118   thumb_code = (TARGET_ARM == 0);
1119   arm_tune_wbuf = (tune_flags & FL_WBUF) != 0;
1120   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
1121   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
1122
1123   /* V5 code we generate is completely interworking capable, so we turn off
1124      TARGET_INTERWORK here to avoid many tests later on.  */
1125
1126   /* XXX However, we must pass the right pre-processor defines to CPP
1127      or GLD can get confused.  This is a hack.  */
1128   if (TARGET_INTERWORK)
1129     arm_cpp_interwork = 1;
1130
1131   if (arm_arch5)
1132     target_flags &= ~MASK_INTERWORK;
1133
1134   if (target_abi_name)
1135     {
1136       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1137         {
1138           if (streq (arm_all_abis[i].name, target_abi_name))
1139             {
1140               arm_abi = arm_all_abis[i].abi_type;
1141               break;
1142             }
1143         }
1144       if (i == ARRAY_SIZE (arm_all_abis))
1145         error ("invalid ABI option: -mabi=%s", target_abi_name);
1146     }
1147   else
1148     arm_abi = ARM_DEFAULT_ABI;
1149
1150   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1151     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1152
1153   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1154     error ("iwmmxt abi requires an iwmmxt capable cpu");
1155
1156   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1157   if (target_fpu_name == NULL && target_fpe_name != NULL)
1158     {
1159       if (streq (target_fpe_name, "2"))
1160         target_fpu_name = "fpe2";
1161       else if (streq (target_fpe_name, "3"))
1162         target_fpu_name = "fpe3";
1163       else
1164         error ("invalid floating point emulation option: -mfpe=%s",
1165                target_fpe_name);
1166     }
1167   if (target_fpu_name != NULL)
1168     {
1169       /* The user specified a FPU.  */
1170       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1171         {
1172           if (streq (all_fpus[i].name, target_fpu_name))
1173             {
1174               arm_fpu_arch = all_fpus[i].fpu;
1175               arm_fpu_tune = arm_fpu_arch;
1176               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1177               break;
1178             }
1179         }
1180       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1181         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1182     }
1183   else
1184     {
1185 #ifdef FPUTYPE_DEFAULT
1186       /* Use the default if it is specified for this platform.  */
1187       arm_fpu_arch = FPUTYPE_DEFAULT;
1188       arm_fpu_tune = FPUTYPE_DEFAULT;
1189 #else
1190       /* Pick one based on CPU type.  */
1191       /* ??? Some targets assume FPA is the default.
1192       if ((insn_flags & FL_VFP) != 0)
1193         arm_fpu_arch = FPUTYPE_VFP;
1194       else
1195       */
1196       if (arm_arch_cirrus)
1197         arm_fpu_arch = FPUTYPE_MAVERICK;
1198       else
1199         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1200 #endif
1201       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1202         arm_fpu_tune = FPUTYPE_FPA;
1203       else
1204         arm_fpu_tune = arm_fpu_arch;
1205       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1206       gcc_assert (arm_fp_model != ARM_FP_MODEL_UNKNOWN);
1207     }
1208
1209   if (target_float_abi_name != NULL)
1210     {
1211       /* The user specified a FP ABI.  */
1212       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1213         {
1214           if (streq (all_float_abis[i].name, target_float_abi_name))
1215             {
1216               arm_float_abi = all_float_abis[i].abi_type;
1217               break;
1218             }
1219         }
1220       if (i == ARRAY_SIZE (all_float_abis))
1221         error ("invalid floating point abi: -mfloat-abi=%s",
1222                target_float_abi_name);
1223     }
1224   else
1225     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1226
1227   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1228     sorry ("-mfloat-abi=hard and VFP");
1229
1230   /* If soft-float is specified then don't use FPU.  */
1231   if (TARGET_SOFT_FLOAT)
1232     arm_fpu_arch = FPUTYPE_NONE;
1233
1234   /* For arm2/3 there is no need to do any scheduling if there is only
1235      a floating point emulator, or we are doing software floating-point.  */
1236   if ((TARGET_SOFT_FLOAT
1237        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1238        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1239       && (tune_flags & FL_MODE32) == 0)
1240     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1241
1242   if (target_thread_switch)
1243     {
1244       if (strcmp (target_thread_switch, "soft") == 0)
1245         target_thread_pointer = TP_SOFT;
1246       else if (strcmp (target_thread_switch, "auto") == 0)
1247         target_thread_pointer = TP_AUTO;
1248       else if (strcmp (target_thread_switch, "cp15") == 0)
1249         target_thread_pointer = TP_CP15;
1250       else
1251         error ("invalid thread pointer option: -mtp=%s", target_thread_switch);
1252     }
1253
1254   /* Use the cp15 method if it is available.  */
1255   if (target_thread_pointer == TP_AUTO)
1256     {
1257       if (arm_arch6k && !TARGET_THUMB)
1258         target_thread_pointer = TP_CP15;
1259       else
1260         target_thread_pointer = TP_SOFT;
1261     }
1262
1263   if (TARGET_HARD_TP && TARGET_THUMB)
1264     error ("can not use -mtp=cp15 with -mthumb");
1265
1266   /* Override the default structure alignment for AAPCS ABI.  */
1267   if (TARGET_AAPCS_BASED)
1268     arm_structure_size_boundary = 8;
1269
1270   if (structure_size_string != NULL)
1271     {
1272       int size = strtol (structure_size_string, NULL, 0);
1273
1274       if (size == 8 || size == 32
1275           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1276         arm_structure_size_boundary = size;
1277       else
1278         warning (0, "structure size boundary can only be set to %s",
1279                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1280     }
1281
1282   if (arm_pic_register_string != NULL)
1283     {
1284       int pic_register = decode_reg_name (arm_pic_register_string);
1285
1286       if (!flag_pic)
1287         warning (0, "-mpic-register= is useless without -fpic");
1288
1289       /* Prevent the user from choosing an obviously stupid PIC register.  */
1290       else if (pic_register < 0 || call_used_regs[pic_register]
1291                || pic_register == HARD_FRAME_POINTER_REGNUM
1292                || pic_register == STACK_POINTER_REGNUM
1293                || pic_register >= PC_REGNUM)
1294         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1295       else
1296         arm_pic_register = pic_register;
1297     }
1298
1299   if (TARGET_THUMB && flag_schedule_insns)
1300     {
1301       /* Don't warn since it's on by default in -O2.  */
1302       flag_schedule_insns = 0;
1303     }
1304
1305   if (optimize_size)
1306     {
1307       arm_constant_limit = 1;
1308
1309       /* If optimizing for size, bump the number of instructions that we
1310          are prepared to conditionally execute (even on a StrongARM).  */
1311       max_insns_skipped = 6;
1312     }
1313   else
1314     {
1315       /* For processors with load scheduling, it never costs more than
1316          2 cycles to load a constant, and the load scheduler may well
1317          reduce that to 1.  */
1318       if (arm_ld_sched)
1319         arm_constant_limit = 1;
1320
1321       /* On XScale the longer latency of a load makes it more difficult
1322          to achieve a good schedule, so it's faster to synthesize
1323          constants that can be done in two insns.  */
1324       if (arm_tune_xscale)
1325         arm_constant_limit = 2;
1326
1327       /* StrongARM has early execution of branches, so a sequence
1328          that is worth skipping is shorter.  */
1329       if (arm_tune_strongarm)
1330         max_insns_skipped = 3;
1331     }
1332
1333   /* Register global variables with the garbage collector.  */
1334   arm_add_gc_roots ();
1335 }
1336
1337 static void
1338 arm_add_gc_roots (void)
1339 {
1340   gcc_obstack_init(&minipool_obstack);
1341   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1342 }
1343 \f
1344 /* A table of known ARM exception types.
1345    For use with the interrupt function attribute.  */
1346
1347 typedef struct
1348 {
1349   const char *const arg;
1350   const unsigned long return_value;
1351 }
1352 isr_attribute_arg;
1353
1354 static const isr_attribute_arg isr_attribute_args [] =
1355 {
1356   { "IRQ",   ARM_FT_ISR },
1357   { "irq",   ARM_FT_ISR },
1358   { "FIQ",   ARM_FT_FIQ },
1359   { "fiq",   ARM_FT_FIQ },
1360   { "ABORT", ARM_FT_ISR },
1361   { "abort", ARM_FT_ISR },
1362   { "ABORT", ARM_FT_ISR },
1363   { "abort", ARM_FT_ISR },
1364   { "UNDEF", ARM_FT_EXCEPTION },
1365   { "undef", ARM_FT_EXCEPTION },
1366   { "SWI",   ARM_FT_EXCEPTION },
1367   { "swi",   ARM_FT_EXCEPTION },
1368   { NULL,    ARM_FT_NORMAL }
1369 };
1370
1371 /* Returns the (interrupt) function type of the current
1372    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1373
1374 static unsigned long
1375 arm_isr_value (tree argument)
1376 {
1377   const isr_attribute_arg * ptr;
1378   const char *              arg;
1379
1380   /* No argument - default to IRQ.  */
1381   if (argument == NULL_TREE)
1382     return ARM_FT_ISR;
1383
1384   /* Get the value of the argument.  */
1385   if (TREE_VALUE (argument) == NULL_TREE
1386       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1387     return ARM_FT_UNKNOWN;
1388
1389   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1390
1391   /* Check it against the list of known arguments.  */
1392   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1393     if (streq (arg, ptr->arg))
1394       return ptr->return_value;
1395
1396   /* An unrecognized interrupt type.  */
1397   return ARM_FT_UNKNOWN;
1398 }
1399
1400 /* Computes the type of the current function.  */
1401
1402 static unsigned long
1403 arm_compute_func_type (void)
1404 {
1405   unsigned long type = ARM_FT_UNKNOWN;
1406   tree a;
1407   tree attr;
1408
1409   gcc_assert (TREE_CODE (current_function_decl) == FUNCTION_DECL);
1410
1411   /* Decide if the current function is volatile.  Such functions
1412      never return, and many memory cycles can be saved by not storing
1413      register values that will never be needed again.  This optimization
1414      was added to speed up context switching in a kernel application.  */
1415   if (optimize > 0
1416       && (TREE_NOTHROW (current_function_decl)
1417           || !(flag_unwind_tables
1418                || (flag_exceptions && !USING_SJLJ_EXCEPTIONS)))
1419       && TREE_THIS_VOLATILE (current_function_decl))
1420     type |= ARM_FT_VOLATILE;
1421
1422   if (cfun->static_chain_decl != NULL)
1423     type |= ARM_FT_NESTED;
1424
1425   attr = DECL_ATTRIBUTES (current_function_decl);
1426
1427   a = lookup_attribute ("naked", attr);
1428   if (a != NULL_TREE)
1429     type |= ARM_FT_NAKED;
1430
1431   a = lookup_attribute ("isr", attr);
1432   if (a == NULL_TREE)
1433     a = lookup_attribute ("interrupt", attr);
1434
1435   if (a == NULL_TREE)
1436     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1437   else
1438     type |= arm_isr_value (TREE_VALUE (a));
1439
1440   return type;
1441 }
1442
1443 /* Returns the type of the current function.  */
1444
1445 unsigned long
1446 arm_current_func_type (void)
1447 {
1448   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1449     cfun->machine->func_type = arm_compute_func_type ();
1450
1451   return cfun->machine->func_type;
1452 }
1453 \f
1454 /* Return 1 if it is possible to return using a single instruction.
1455    If SIBLING is non-null, this is a test for a return before a sibling
1456    call.  SIBLING is the call insn, so we can examine its register usage.  */
1457
1458 int
1459 use_return_insn (int iscond, rtx sibling)
1460 {
1461   int regno;
1462   unsigned int func_type;
1463   unsigned long saved_int_regs;
1464   unsigned HOST_WIDE_INT stack_adjust;
1465   arm_stack_offsets *offsets;
1466
1467   /* Never use a return instruction before reload has run.  */
1468   if (!reload_completed)
1469     return 0;
1470
1471   func_type = arm_current_func_type ();
1472
1473   /* Naked functions and volatile functions need special
1474      consideration.  */
1475   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1476     return 0;
1477
1478   /* So do interrupt functions that use the frame pointer.  */
1479   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1480     return 0;
1481
1482   offsets = arm_get_frame_offsets ();
1483   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1484
1485   /* As do variadic functions.  */
1486   if (current_function_pretend_args_size
1487       || cfun->machine->uses_anonymous_args
1488       /* Or if the function calls __builtin_eh_return () */
1489       || current_function_calls_eh_return
1490       /* Or if the function calls alloca */
1491       || current_function_calls_alloca
1492       /* Or if there is a stack adjustment.  However, if the stack pointer
1493          is saved on the stack, we can use a pre-incrementing stack load.  */
1494       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1495     return 0;
1496
1497   saved_int_regs = arm_compute_save_reg_mask ();
1498
1499   /* Unfortunately, the insn
1500
1501        ldmib sp, {..., sp, ...}
1502
1503      triggers a bug on most SA-110 based devices, such that the stack
1504      pointer won't be correctly restored if the instruction takes a
1505      page fault.  We work around this problem by popping r3 along with
1506      the other registers, since that is never slower than executing
1507      another instruction.
1508
1509      We test for !arm_arch5 here, because code for any architecture
1510      less than this could potentially be run on one of the buggy
1511      chips.  */
1512   if (stack_adjust == 4 && !arm_arch5)
1513     {
1514       /* Validate that r3 is a call-clobbered register (always true in
1515          the default abi) ...  */
1516       if (!call_used_regs[3])
1517         return 0;
1518
1519       /* ... that it isn't being used for a return value ... */
1520       if (arm_size_return_regs () >= (4 * UNITS_PER_WORD))
1521         return 0;
1522
1523       /* ... or for a tail-call argument ...  */
1524       if (sibling)
1525         {
1526           gcc_assert (GET_CODE (sibling) == CALL_INSN);
1527
1528           if (find_regno_fusage (sibling, USE, 3))
1529             return 0;
1530         }
1531
1532       /* ... and that there are no call-saved registers in r0-r2
1533          (always true in the default ABI).  */
1534       if (saved_int_regs & 0x7)
1535         return 0;
1536     }
1537
1538   /* Can't be done if interworking with Thumb, and any registers have been
1539      stacked.  */
1540   if (TARGET_INTERWORK && saved_int_regs != 0)
1541     return 0;
1542
1543   /* On StrongARM, conditional returns are expensive if they aren't
1544      taken and multiple registers have been stacked.  */
1545   if (iscond && arm_tune_strongarm)
1546     {
1547       /* Conditional return when just the LR is stored is a simple
1548          conditional-load instruction, that's not expensive.  */
1549       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1550         return 0;
1551
1552       if (flag_pic 
1553           && arm_pic_register != INVALID_REGNUM
1554           && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1555         return 0;
1556     }
1557
1558   /* If there are saved registers but the LR isn't saved, then we need
1559      two instructions for the return.  */
1560   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1561     return 0;
1562
1563   /* Can't be done if any of the FPA regs are pushed,
1564      since this also requires an insn.  */
1565   if (TARGET_HARD_FLOAT && TARGET_FPA)
1566     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1567       if (regs_ever_live[regno] && !call_used_regs[regno])
1568         return 0;
1569
1570   /* Likewise VFP regs.  */
1571   if (TARGET_HARD_FLOAT && TARGET_VFP)
1572     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1573       if (regs_ever_live[regno] && !call_used_regs[regno])
1574         return 0;
1575
1576   if (TARGET_REALLY_IWMMXT)
1577     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1578       if (regs_ever_live[regno] && ! call_used_regs [regno])
1579         return 0;
1580
1581   return 1;
1582 }
1583
1584 /* Return TRUE if int I is a valid immediate ARM constant.  */
1585
1586 int
1587 const_ok_for_arm (HOST_WIDE_INT i)
1588 {
1589   int lowbit;
1590
1591   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1592      be all zero, or all one.  */
1593   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1594       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1595           != ((~(unsigned HOST_WIDE_INT) 0)
1596               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1597     return FALSE;
1598
1599   i &= (unsigned HOST_WIDE_INT) 0xffffffff;
1600
1601   /* Fast return for 0 and small values.  We must do this for zero, since
1602      the code below can't handle that one case.  */
1603   if ((i & ~(unsigned HOST_WIDE_INT) 0xff) == 0)
1604     return TRUE;
1605
1606   /* Get the number of trailing zeros, rounded down to the nearest even
1607      number.  */
1608   lowbit = (ffs ((int) i) - 1) & ~1;
1609
1610   if ((i & ~(((unsigned HOST_WIDE_INT) 0xff) << lowbit)) == 0)
1611     return TRUE;
1612   else if (lowbit <= 4
1613            && ((i & ~0xc000003f) == 0
1614                || (i & ~0xf000000f) == 0
1615                || (i & ~0xfc000003) == 0))
1616     return TRUE;
1617
1618   return FALSE;
1619 }
1620
1621 /* Return true if I is a valid constant for the operation CODE.  */
1622 static int
1623 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1624 {
1625   if (const_ok_for_arm (i))
1626     return 1;
1627
1628   switch (code)
1629     {
1630     case PLUS:
1631       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1632
1633     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1634     case XOR:
1635     case IOR:
1636       return 0;
1637
1638     case AND:
1639       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1640
1641     default:
1642       gcc_unreachable ();
1643     }
1644 }
1645
1646 /* Emit a sequence of insns to handle a large constant.
1647    CODE is the code of the operation required, it can be any of SET, PLUS,
1648    IOR, AND, XOR, MINUS;
1649    MODE is the mode in which the operation is being performed;
1650    VAL is the integer to operate on;
1651    SOURCE is the other operand (a register, or a null-pointer for SET);
1652    SUBTARGETS means it is safe to create scratch registers if that will
1653    either produce a simpler sequence, or we will want to cse the values.
1654    Return value is the number of insns emitted.  */
1655
1656 int
1657 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1658                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1659 {
1660   rtx cond;
1661
1662   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1663     cond = COND_EXEC_TEST (PATTERN (insn));
1664   else
1665     cond = NULL_RTX;
1666
1667   if (subtargets || code == SET
1668       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1669           && REGNO (target) != REGNO (source)))
1670     {
1671       /* After arm_reorg has been called, we can't fix up expensive
1672          constants by pushing them into memory so we must synthesize
1673          them in-line, regardless of the cost.  This is only likely to
1674          be more costly on chips that have load delay slots and we are
1675          compiling without running the scheduler (so no splitting
1676          occurred before the final instruction emission).
1677
1678          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1679       */
1680       if (!after_arm_reorg
1681           && !cond
1682           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1683                                 1, 0)
1684               > arm_constant_limit + (code != SET)))
1685         {
1686           if (code == SET)
1687             {
1688               /* Currently SET is the only monadic value for CODE, all
1689                  the rest are diadic.  */
1690               emit_set_insn (target, GEN_INT (val));
1691               return 1;
1692             }
1693           else
1694             {
1695               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1696
1697               emit_set_insn (temp, GEN_INT (val));
1698               /* For MINUS, the value is subtracted from, since we never
1699                  have subtraction of a constant.  */
1700               if (code == MINUS)
1701                 emit_set_insn (target, gen_rtx_MINUS (mode, temp, source));
1702               else
1703                 emit_set_insn (target,
1704                                gen_rtx_fmt_ee (code, mode, source, temp));
1705               return 2;
1706             }
1707         }
1708     }
1709
1710   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1711                            1);
1712 }
1713
1714 static int
1715 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1716 {
1717   HOST_WIDE_INT temp1;
1718   int num_insns = 0;
1719   do
1720     {
1721       int end;
1722
1723       if (i <= 0)
1724         i += 32;
1725       if (remainder & (3 << (i - 2)))
1726         {
1727           end = i - 8;
1728           if (end < 0)
1729             end += 32;
1730           temp1 = remainder & ((0x0ff << end)
1731                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1732           remainder &= ~temp1;
1733           num_insns++;
1734           i -= 6;
1735         }
1736       i -= 2;
1737     } while (remainder);
1738   return num_insns;
1739 }
1740
1741 /* Emit an instruction with the indicated PATTERN.  If COND is
1742    non-NULL, conditionalize the execution of the instruction on COND
1743    being true.  */
1744
1745 static void
1746 emit_constant_insn (rtx cond, rtx pattern)
1747 {
1748   if (cond)
1749     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1750   emit_insn (pattern);
1751 }
1752
1753 /* As above, but extra parameter GENERATE which, if clear, suppresses
1754    RTL generation.  */
1755
1756 static int
1757 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1758                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1759                   int generate)
1760 {
1761   int can_invert = 0;
1762   int can_negate = 0;
1763   int can_negate_initial = 0;
1764   int can_shift = 0;
1765   int i;
1766   int num_bits_set = 0;
1767   int set_sign_bit_copies = 0;
1768   int clear_sign_bit_copies = 0;
1769   int clear_zero_bit_copies = 0;
1770   int set_zero_bit_copies = 0;
1771   int insns = 0;
1772   unsigned HOST_WIDE_INT temp1, temp2;
1773   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1774
1775   /* Find out which operations are safe for a given CODE.  Also do a quick
1776      check for degenerate cases; these can occur when DImode operations
1777      are split.  */
1778   switch (code)
1779     {
1780     case SET:
1781       can_invert = 1;
1782       can_shift = 1;
1783       can_negate = 1;
1784       break;
1785
1786     case PLUS:
1787       can_negate = 1;
1788       can_negate_initial = 1;
1789       break;
1790
1791     case IOR:
1792       if (remainder == 0xffffffff)
1793         {
1794           if (generate)
1795             emit_constant_insn (cond,
1796                                 gen_rtx_SET (VOIDmode, target,
1797                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1798           return 1;
1799         }
1800       if (remainder == 0)
1801         {
1802           if (reload_completed && rtx_equal_p (target, source))
1803             return 0;
1804           if (generate)
1805             emit_constant_insn (cond,
1806                                 gen_rtx_SET (VOIDmode, target, source));
1807           return 1;
1808         }
1809       break;
1810
1811     case AND:
1812       if (remainder == 0)
1813         {
1814           if (generate)
1815             emit_constant_insn (cond,
1816                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1817           return 1;
1818         }
1819       if (remainder == 0xffffffff)
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       can_invert = 1;
1829       break;
1830
1831     case XOR:
1832       if (remainder == 0)
1833         {
1834           if (reload_completed && rtx_equal_p (target, source))
1835             return 0;
1836           if (generate)
1837             emit_constant_insn (cond,
1838                                 gen_rtx_SET (VOIDmode, target, source));
1839           return 1;
1840         }
1841
1842       /* We don't know how to handle other cases yet.  */
1843       gcc_assert (remainder == 0xffffffff);
1844
1845       if (generate)
1846         emit_constant_insn (cond,
1847                             gen_rtx_SET (VOIDmode, target,
1848                                          gen_rtx_NOT (mode, source)));
1849       return 1;
1850
1851     case MINUS:
1852       /* We treat MINUS as (val - source), since (source - val) is always
1853          passed as (source + (-val)).  */
1854       if (remainder == 0)
1855         {
1856           if (generate)
1857             emit_constant_insn (cond,
1858                                 gen_rtx_SET (VOIDmode, target,
1859                                              gen_rtx_NEG (mode, source)));
1860           return 1;
1861         }
1862       if (const_ok_for_arm (val))
1863         {
1864           if (generate)
1865             emit_constant_insn (cond,
1866                                 gen_rtx_SET (VOIDmode, target,
1867                                              gen_rtx_MINUS (mode, GEN_INT (val),
1868                                                             source)));
1869           return 1;
1870         }
1871       can_negate = 1;
1872
1873       break;
1874
1875     default:
1876       gcc_unreachable ();
1877     }
1878
1879   /* If we can do it in one insn get out quickly.  */
1880   if (const_ok_for_arm (val)
1881       || (can_negate_initial && const_ok_for_arm (-val))
1882       || (can_invert && const_ok_for_arm (~val)))
1883     {
1884       if (generate)
1885         emit_constant_insn (cond,
1886                             gen_rtx_SET (VOIDmode, target,
1887                                          (source
1888                                           ? gen_rtx_fmt_ee (code, mode, source,
1889                                                             GEN_INT (val))
1890                                           : GEN_INT (val))));
1891       return 1;
1892     }
1893
1894   /* Calculate a few attributes that may be useful for specific
1895      optimizations.  */
1896   for (i = 31; i >= 0; i--)
1897     {
1898       if ((remainder & (1 << i)) == 0)
1899         clear_sign_bit_copies++;
1900       else
1901         break;
1902     }
1903
1904   for (i = 31; i >= 0; i--)
1905     {
1906       if ((remainder & (1 << i)) != 0)
1907         set_sign_bit_copies++;
1908       else
1909         break;
1910     }
1911
1912   for (i = 0; i <= 31; i++)
1913     {
1914       if ((remainder & (1 << i)) == 0)
1915         clear_zero_bit_copies++;
1916       else
1917         break;
1918     }
1919
1920   for (i = 0; i <= 31; i++)
1921     {
1922       if ((remainder & (1 << i)) != 0)
1923         set_zero_bit_copies++;
1924       else
1925         break;
1926     }
1927
1928   switch (code)
1929     {
1930     case SET:
1931       /* See if we can do this by sign_extending a constant that is known
1932          to be negative.  This is a good, way of doing it, since the shift
1933          may well merge into a subsequent insn.  */
1934       if (set_sign_bit_copies > 1)
1935         {
1936           if (const_ok_for_arm
1937               (temp1 = ARM_SIGN_EXTEND (remainder
1938                                         << (set_sign_bit_copies - 1))))
1939             {
1940               if (generate)
1941                 {
1942                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1943                   emit_constant_insn (cond,
1944                                       gen_rtx_SET (VOIDmode, new_src,
1945                                                    GEN_INT (temp1)));
1946                   emit_constant_insn (cond,
1947                                       gen_ashrsi3 (target, new_src,
1948                                                    GEN_INT (set_sign_bit_copies - 1)));
1949                 }
1950               return 2;
1951             }
1952           /* For an inverted constant, we will need to set the low bits,
1953              these will be shifted out of harm's way.  */
1954           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1955           if (const_ok_for_arm (~temp1))
1956             {
1957               if (generate)
1958                 {
1959                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1960                   emit_constant_insn (cond,
1961                                       gen_rtx_SET (VOIDmode, new_src,
1962                                                    GEN_INT (temp1)));
1963                   emit_constant_insn (cond,
1964                                       gen_ashrsi3 (target, new_src,
1965                                                    GEN_INT (set_sign_bit_copies - 1)));
1966                 }
1967               return 2;
1968             }
1969         }
1970
1971       /* See if we can calculate the value as the difference between two
1972          valid immediates.  */
1973       if (clear_sign_bit_copies + clear_zero_bit_copies <= 16)
1974         {
1975           int topshift = clear_sign_bit_copies & ~1;
1976
1977           temp1 = ARM_SIGN_EXTEND ((remainder + (0x00800000 >> topshift))
1978                                    & (0xff000000 >> topshift));
1979
1980           /* If temp1 is zero, then that means the 9 most significant
1981              bits of remainder were 1 and we've caused it to overflow.
1982              When topshift is 0 we don't need to do anything since we
1983              can borrow from 'bit 32'.  */
1984           if (temp1 == 0 && topshift != 0)
1985             temp1 = 0x80000000 >> (topshift - 1);
1986
1987           temp2 = ARM_SIGN_EXTEND (temp1 - remainder);
1988
1989           if (const_ok_for_arm (temp2))
1990             {
1991               if (generate)
1992                 {
1993                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1994                   emit_constant_insn (cond,
1995                                       gen_rtx_SET (VOIDmode, new_src,
1996                                                    GEN_INT (temp1)));
1997                   emit_constant_insn (cond,
1998                                       gen_addsi3 (target, new_src,
1999                                                   GEN_INT (-temp2)));
2000                 }
2001
2002               return 2;
2003             }
2004         }
2005
2006       /* See if we can generate this by setting the bottom (or the top)
2007          16 bits, and then shifting these into the other half of the
2008          word.  We only look for the simplest cases, to do more would cost
2009          too much.  Be careful, however, not to generate this when the
2010          alternative would take fewer insns.  */
2011       if (val & 0xffff0000)
2012         {
2013           temp1 = remainder & 0xffff0000;
2014           temp2 = remainder & 0x0000ffff;
2015
2016           /* Overlaps outside this range are best done using other methods.  */
2017           for (i = 9; i < 24; i++)
2018             {
2019               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
2020                   && !const_ok_for_arm (temp2))
2021                 {
2022                   rtx new_src = (subtargets
2023                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2024                                  : target);
2025                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
2026                                             source, subtargets, generate);
2027                   source = new_src;
2028                   if (generate)
2029                     emit_constant_insn
2030                       (cond,
2031                        gen_rtx_SET
2032                        (VOIDmode, target,
2033                         gen_rtx_IOR (mode,
2034                                      gen_rtx_ASHIFT (mode, source,
2035                                                      GEN_INT (i)),
2036                                      source)));
2037                   return insns + 1;
2038                 }
2039             }
2040
2041           /* Don't duplicate cases already considered.  */
2042           for (i = 17; i < 24; i++)
2043             {
2044               if (((temp1 | (temp1 >> i)) == remainder)
2045                   && !const_ok_for_arm (temp1))
2046                 {
2047                   rtx new_src = (subtargets
2048                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
2049                                  : target);
2050                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
2051                                             source, subtargets, generate);
2052                   source = new_src;
2053                   if (generate)
2054                     emit_constant_insn
2055                       (cond,
2056                        gen_rtx_SET (VOIDmode, target,
2057                                     gen_rtx_IOR
2058                                     (mode,
2059                                      gen_rtx_LSHIFTRT (mode, source,
2060                                                        GEN_INT (i)),
2061                                      source)));
2062                   return insns + 1;
2063                 }
2064             }
2065         }
2066       break;
2067
2068     case IOR:
2069     case XOR:
2070       /* If we have IOR or XOR, and the constant can be loaded in a
2071          single instruction, and we can find a temporary to put it in,
2072          then this can be done in two instructions instead of 3-4.  */
2073       if (subtargets
2074           /* TARGET can't be NULL if SUBTARGETS is 0 */
2075           || (reload_completed && !reg_mentioned_p (target, source)))
2076         {
2077           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
2078             {
2079               if (generate)
2080                 {
2081                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2082
2083                   emit_constant_insn (cond,
2084                                       gen_rtx_SET (VOIDmode, sub,
2085                                                    GEN_INT (val)));
2086                   emit_constant_insn (cond,
2087                                       gen_rtx_SET (VOIDmode, target,
2088                                                    gen_rtx_fmt_ee (code, mode,
2089                                                                    source, sub)));
2090                 }
2091               return 2;
2092             }
2093         }
2094
2095       if (code == XOR)
2096         break;
2097
2098       if (set_sign_bit_copies > 8
2099           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
2100         {
2101           if (generate)
2102             {
2103               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2104               rtx shift = GEN_INT (set_sign_bit_copies);
2105
2106               emit_constant_insn
2107                 (cond,
2108                  gen_rtx_SET (VOIDmode, sub,
2109                               gen_rtx_NOT (mode,
2110                                            gen_rtx_ASHIFT (mode,
2111                                                            source,
2112                                                            shift))));
2113               emit_constant_insn
2114                 (cond,
2115                  gen_rtx_SET (VOIDmode, target,
2116                               gen_rtx_NOT (mode,
2117                                            gen_rtx_LSHIFTRT (mode, sub,
2118                                                              shift))));
2119             }
2120           return 2;
2121         }
2122
2123       if (set_zero_bit_copies > 8
2124           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
2125         {
2126           if (generate)
2127             {
2128               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2129               rtx shift = GEN_INT (set_zero_bit_copies);
2130
2131               emit_constant_insn
2132                 (cond,
2133                  gen_rtx_SET (VOIDmode, sub,
2134                               gen_rtx_NOT (mode,
2135                                            gen_rtx_LSHIFTRT (mode,
2136                                                              source,
2137                                                              shift))));
2138               emit_constant_insn
2139                 (cond,
2140                  gen_rtx_SET (VOIDmode, target,
2141                               gen_rtx_NOT (mode,
2142                                            gen_rtx_ASHIFT (mode, sub,
2143                                                            shift))));
2144             }
2145           return 2;
2146         }
2147
2148       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
2149         {
2150           if (generate)
2151             {
2152               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
2153               emit_constant_insn (cond,
2154                                   gen_rtx_SET (VOIDmode, sub,
2155                                                gen_rtx_NOT (mode, source)));
2156               source = sub;
2157               if (subtargets)
2158                 sub = gen_reg_rtx (mode);
2159               emit_constant_insn (cond,
2160                                   gen_rtx_SET (VOIDmode, sub,
2161                                                gen_rtx_AND (mode, source,
2162                                                             GEN_INT (temp1))));
2163               emit_constant_insn (cond,
2164                                   gen_rtx_SET (VOIDmode, target,
2165                                                gen_rtx_NOT (mode, sub)));
2166             }
2167           return 3;
2168         }
2169       break;
2170
2171     case AND:
2172       /* See if two shifts will do 2 or more insn's worth of work.  */
2173       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
2174         {
2175           HOST_WIDE_INT shift_mask = ((0xffffffff
2176                                        << (32 - clear_sign_bit_copies))
2177                                       & 0xffffffff);
2178
2179           if ((remainder | shift_mask) != 0xffffffff)
2180             {
2181               if (generate)
2182                 {
2183                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2184                   insns = arm_gen_constant (AND, mode, cond,
2185                                             remainder | shift_mask,
2186                                             new_src, source, subtargets, 1);
2187                   source = new_src;
2188                 }
2189               else
2190                 {
2191                   rtx targ = subtargets ? NULL_RTX : target;
2192                   insns = arm_gen_constant (AND, mode, cond,
2193                                             remainder | shift_mask,
2194                                             targ, source, subtargets, 0);
2195                 }
2196             }
2197
2198           if (generate)
2199             {
2200               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2201               rtx shift = GEN_INT (clear_sign_bit_copies);
2202
2203               emit_insn (gen_ashlsi3 (new_src, source, shift));
2204               emit_insn (gen_lshrsi3 (target, new_src, shift));
2205             }
2206
2207           return insns + 2;
2208         }
2209
2210       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2211         {
2212           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2213
2214           if ((remainder | shift_mask) != 0xffffffff)
2215             {
2216               if (generate)
2217                 {
2218                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2219
2220                   insns = arm_gen_constant (AND, mode, cond,
2221                                             remainder | shift_mask,
2222                                             new_src, source, subtargets, 1);
2223                   source = new_src;
2224                 }
2225               else
2226                 {
2227                   rtx targ = subtargets ? NULL_RTX : target;
2228
2229                   insns = arm_gen_constant (AND, mode, cond,
2230                                             remainder | shift_mask,
2231                                             targ, source, subtargets, 0);
2232                 }
2233             }
2234
2235           if (generate)
2236             {
2237               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2238               rtx shift = GEN_INT (clear_zero_bit_copies);
2239
2240               emit_insn (gen_lshrsi3 (new_src, source, shift));
2241               emit_insn (gen_ashlsi3 (target, new_src, shift));
2242             }
2243
2244           return insns + 2;
2245         }
2246
2247       break;
2248
2249     default:
2250       break;
2251     }
2252
2253   for (i = 0; i < 32; i++)
2254     if (remainder & (1 << i))
2255       num_bits_set++;
2256
2257   if (code == AND || (can_invert && num_bits_set > 16))
2258     remainder = (~remainder) & 0xffffffff;
2259   else if (code == PLUS && num_bits_set > 16)
2260     remainder = (-remainder) & 0xffffffff;
2261   else
2262     {
2263       can_invert = 0;
2264       can_negate = 0;
2265     }
2266
2267   /* Now try and find a way of doing the job in either two or three
2268      instructions.
2269      We start by looking for the largest block of zeros that are aligned on
2270      a 2-bit boundary, we then fill up the temps, wrapping around to the
2271      top of the word when we drop off the bottom.
2272      In the worst case this code should produce no more than four insns.  */
2273   {
2274     int best_start = 0;
2275     int best_consecutive_zeros = 0;
2276
2277     for (i = 0; i < 32; i += 2)
2278       {
2279         int consecutive_zeros = 0;
2280
2281         if (!(remainder & (3 << i)))
2282           {
2283             while ((i < 32) && !(remainder & (3 << i)))
2284               {
2285                 consecutive_zeros += 2;
2286                 i += 2;
2287               }
2288             if (consecutive_zeros > best_consecutive_zeros)
2289               {
2290                 best_consecutive_zeros = consecutive_zeros;
2291                 best_start = i - consecutive_zeros;
2292               }
2293             i -= 2;
2294           }
2295       }
2296
2297     /* So long as it won't require any more insns to do so, it's
2298        desirable to emit a small constant (in bits 0...9) in the last
2299        insn.  This way there is more chance that it can be combined with
2300        a later addressing insn to form a pre-indexed load or store
2301        operation.  Consider:
2302
2303                *((volatile int *)0xe0000100) = 1;
2304                *((volatile int *)0xe0000110) = 2;
2305
2306        We want this to wind up as:
2307
2308                 mov rA, #0xe0000000
2309                 mov rB, #1
2310                 str rB, [rA, #0x100]
2311                 mov rB, #2
2312                 str rB, [rA, #0x110]
2313
2314        rather than having to synthesize both large constants from scratch.
2315
2316        Therefore, we calculate how many insns would be required to emit
2317        the constant starting from `best_start', and also starting from
2318        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2319        yield a shorter sequence, we may as well use zero.  */
2320     if (best_start != 0
2321         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2322         && (count_insns_for_constant (remainder, 0) <=
2323             count_insns_for_constant (remainder, best_start)))
2324       best_start = 0;
2325
2326     /* Now start emitting the insns.  */
2327     i = best_start;
2328     do
2329       {
2330         int end;
2331
2332         if (i <= 0)
2333           i += 32;
2334         if (remainder & (3 << (i - 2)))
2335           {
2336             end = i - 8;
2337             if (end < 0)
2338               end += 32;
2339             temp1 = remainder & ((0x0ff << end)
2340                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2341             remainder &= ~temp1;
2342
2343             if (generate)
2344               {
2345                 rtx new_src, temp1_rtx;
2346
2347                 if (code == SET || code == MINUS)
2348                   {
2349                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2350                     if (can_invert && code != MINUS)
2351                       temp1 = ~temp1;
2352                   }
2353                 else
2354                   {
2355                     if (remainder && subtargets)
2356                       new_src = gen_reg_rtx (mode);
2357                     else
2358                       new_src = target;
2359                     if (can_invert)
2360                       temp1 = ~temp1;
2361                     else if (can_negate)
2362                       temp1 = -temp1;
2363                   }
2364
2365                 temp1 = trunc_int_for_mode (temp1, mode);
2366                 temp1_rtx = GEN_INT (temp1);
2367
2368                 if (code == SET)
2369                   ;
2370                 else if (code == MINUS)
2371                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2372                 else
2373                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2374
2375                 emit_constant_insn (cond,
2376                                     gen_rtx_SET (VOIDmode, new_src,
2377                                                  temp1_rtx));
2378                 source = new_src;
2379               }
2380
2381             if (code == SET)
2382               {
2383                 can_invert = 0;
2384                 code = PLUS;
2385               }
2386             else if (code == MINUS)
2387               code = PLUS;
2388
2389             insns++;
2390             i -= 6;
2391           }
2392         i -= 2;
2393       }
2394     while (remainder);
2395   }
2396
2397   return insns;
2398 }
2399
2400 /* Canonicalize a comparison so that we are more likely to recognize it.
2401    This can be done for a few constant compares, where we can make the
2402    immediate value easier to load.  */
2403
2404 enum rtx_code
2405 arm_canonicalize_comparison (enum rtx_code code, enum machine_mode mode,
2406                              rtx * op1)
2407 {
2408   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2409   unsigned HOST_WIDE_INT maxval;
2410   maxval = (((unsigned HOST_WIDE_INT) 1) << (GET_MODE_BITSIZE(mode) - 1)) - 1;
2411
2412   switch (code)
2413     {
2414     case EQ:
2415     case NE:
2416       return code;
2417
2418     case GT:
2419     case LE:
2420       if (i != maxval
2421           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2422         {
2423           *op1 = GEN_INT (i + 1);
2424           return code == GT ? GE : LT;
2425         }
2426       break;
2427
2428     case GE:
2429     case LT:
2430       if (i != ~maxval
2431           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2432         {
2433           *op1 = GEN_INT (i - 1);
2434           return code == GE ? GT : LE;
2435         }
2436       break;
2437
2438     case GTU:
2439     case LEU:
2440       if (i != ~((unsigned HOST_WIDE_INT) 0)
2441           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2442         {
2443           *op1 = GEN_INT (i + 1);
2444           return code == GTU ? GEU : LTU;
2445         }
2446       break;
2447
2448     case GEU:
2449     case LTU:
2450       if (i != 0
2451           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2452         {
2453           *op1 = GEN_INT (i - 1);
2454           return code == GEU ? GTU : LEU;
2455         }
2456       break;
2457
2458     default:
2459       gcc_unreachable ();
2460     }
2461
2462   return code;
2463 }
2464
2465
2466 /* Define how to find the value returned by a function.  */
2467
2468 rtx
2469 arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2470 {
2471   enum machine_mode mode;
2472   int unsignedp ATTRIBUTE_UNUSED;
2473   rtx r ATTRIBUTE_UNUSED;
2474
2475   mode = TYPE_MODE (type);
2476   /* Promote integer types.  */
2477   if (INTEGRAL_TYPE_P (type))
2478     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2479
2480   /* Promotes small structs returned in a register to full-word size
2481      for big-endian AAPCS.  */
2482   if (arm_return_in_msb (type))
2483     {
2484       HOST_WIDE_INT size = int_size_in_bytes (type);
2485       if (size % UNITS_PER_WORD != 0)
2486         {
2487           size += UNITS_PER_WORD - size % UNITS_PER_WORD;
2488           mode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2489         }
2490     }
2491
2492   return LIBCALL_VALUE(mode);
2493 }
2494
2495 /* Determine the amount of memory needed to store the possible return
2496    registers of an untyped call.  */
2497 int
2498 arm_apply_result_size (void)
2499 {
2500   int size = 16;
2501
2502   if (TARGET_ARM)
2503     {
2504       if (TARGET_HARD_FLOAT_ABI)
2505         {
2506           if (TARGET_FPA)
2507             size += 12;
2508           if (TARGET_MAVERICK)
2509             size += 8;
2510         }
2511       if (TARGET_IWMMXT_ABI)
2512         size += 8;
2513     }
2514
2515   return size;
2516 }
2517
2518 /* Decide whether a type should be returned in memory (true)
2519    or in a register (false).  This is called by the macro
2520    RETURN_IN_MEMORY.  */
2521 int
2522 arm_return_in_memory (tree type)
2523 {
2524   HOST_WIDE_INT size;
2525
2526   if (!AGGREGATE_TYPE_P (type) &&
2527       (TREE_CODE (type) != VECTOR_TYPE) &&
2528       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2529     /* All simple types are returned in registers.
2530        For AAPCS, complex types are treated the same as aggregates.  */
2531     return 0;
2532
2533   size = int_size_in_bytes (type);
2534
2535   if (arm_abi != ARM_ABI_APCS)
2536     {
2537       /* ATPCS and later return aggregate types in memory only if they are
2538          larger than a word (or are variable size).  */
2539       return (size < 0 || size > UNITS_PER_WORD);
2540     }
2541
2542   /* To maximize backwards compatibility with previous versions of gcc,
2543      return vectors up to 4 words in registers.  */
2544   if (TREE_CODE (type) == VECTOR_TYPE)
2545     return (size < 0 || size > (4 * UNITS_PER_WORD));
2546
2547   /* For the arm-wince targets we choose to be compatible with Microsoft's
2548      ARM and Thumb compilers, which always return aggregates in memory.  */
2549 #ifndef ARM_WINCE
2550   /* All structures/unions bigger than one word are returned in memory.
2551      Also catch the case where int_size_in_bytes returns -1.  In this case
2552      the aggregate is either huge or of variable size, and in either case
2553      we will want to return it via memory and not in a register.  */
2554   if (size < 0 || size > UNITS_PER_WORD)
2555     return 1;
2556
2557   if (TREE_CODE (type) == RECORD_TYPE)
2558     {
2559       tree field;
2560
2561       /* For a struct the APCS says that we only return in a register
2562          if the type is 'integer like' and every addressable element
2563          has an offset of zero.  For practical purposes this means
2564          that the structure can have at most one non bit-field element
2565          and that this element must be the first one in the structure.  */
2566
2567       /* Find the first field, ignoring non FIELD_DECL things which will
2568          have been created by C++.  */
2569       for (field = TYPE_FIELDS (type);
2570            field && TREE_CODE (field) != FIELD_DECL;
2571            field = TREE_CHAIN (field))
2572         continue;
2573
2574       if (field == NULL)
2575         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2576
2577       /* Check that the first field is valid for returning in a register.  */
2578
2579       /* ... Floats are not allowed */
2580       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2581         return 1;
2582
2583       /* ... Aggregates that are not themselves valid for returning in
2584          a register are not allowed.  */
2585       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2586         return 1;
2587
2588       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2589          since they are not addressable.  */
2590       for (field = TREE_CHAIN (field);
2591            field;
2592            field = TREE_CHAIN (field))
2593         {
2594           if (TREE_CODE (field) != FIELD_DECL)
2595             continue;
2596
2597           if (!DECL_BIT_FIELD_TYPE (field))
2598             return 1;
2599         }
2600
2601       return 0;
2602     }
2603
2604   if (TREE_CODE (type) == UNION_TYPE)
2605     {
2606       tree field;
2607
2608       /* Unions can be returned in registers if every element is
2609          integral, or can be returned in an integer register.  */
2610       for (field = TYPE_FIELDS (type);
2611            field;
2612            field = TREE_CHAIN (field))
2613         {
2614           if (TREE_CODE (field) != FIELD_DECL)
2615             continue;
2616
2617           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2618             return 1;
2619
2620           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2621             return 1;
2622         }
2623
2624       return 0;
2625     }
2626 #endif /* not ARM_WINCE */
2627
2628   /* Return all other types in memory.  */
2629   return 1;
2630 }
2631
2632 /* Indicate whether or not words of a double are in big-endian order.  */
2633
2634 int
2635 arm_float_words_big_endian (void)
2636 {
2637   if (TARGET_MAVERICK)
2638     return 0;
2639
2640   /* For FPA, float words are always big-endian.  For VFP, floats words
2641      follow the memory system mode.  */
2642
2643   if (TARGET_FPA)
2644     {
2645       return 1;
2646     }
2647
2648   if (TARGET_VFP)
2649     return (TARGET_BIG_END ? 1 : 0);
2650
2651   return 1;
2652 }
2653
2654 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2655    for a call to a function whose data type is FNTYPE.
2656    For a library call, FNTYPE is NULL.  */
2657 void
2658 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2659                           rtx libname  ATTRIBUTE_UNUSED,
2660                           tree fndecl ATTRIBUTE_UNUSED)
2661 {
2662   /* On the ARM, the offset starts at 0.  */
2663   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2664   pcum->iwmmxt_nregs = 0;
2665   pcum->can_split = true;
2666
2667   pcum->call_cookie = CALL_NORMAL;
2668
2669   if (TARGET_LONG_CALLS)
2670     pcum->call_cookie = CALL_LONG;
2671
2672   /* Check for long call/short call attributes.  The attributes
2673      override any command line option.  */
2674   if (fntype)
2675     {
2676       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2677         pcum->call_cookie = CALL_SHORT;
2678       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2679         pcum->call_cookie = CALL_LONG;
2680     }
2681
2682   /* Varargs vectors are treated the same as long long.
2683      named_count avoids having to change the way arm handles 'named' */
2684   pcum->named_count = 0;
2685   pcum->nargs = 0;
2686
2687   if (TARGET_REALLY_IWMMXT && fntype)
2688     {
2689       tree fn_arg;
2690
2691       for (fn_arg = TYPE_ARG_TYPES (fntype);
2692            fn_arg;
2693            fn_arg = TREE_CHAIN (fn_arg))
2694         pcum->named_count += 1;
2695
2696       if (! pcum->named_count)
2697         pcum->named_count = INT_MAX;
2698     }
2699 }
2700
2701
2702 /* Return true if mode/type need doubleword alignment.  */
2703 bool
2704 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2705 {
2706   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2707           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2708 }
2709
2710
2711 /* Determine where to put an argument to a function.
2712    Value is zero to push the argument on the stack,
2713    or a hard register in which to store the argument.
2714
2715    MODE is the argument's machine mode.
2716    TYPE is the data type of the argument (as a tree).
2717     This is null for libcalls where that information may
2718     not be available.
2719    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2720     the preceding args and about the function being called.
2721    NAMED is nonzero if this argument is a named parameter
2722     (otherwise it is an extra parameter matching an ellipsis).  */
2723
2724 rtx
2725 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2726                   tree type, int named)
2727 {
2728   int nregs;
2729
2730   /* Varargs vectors are treated the same as long long.
2731      named_count avoids having to change the way arm handles 'named' */
2732   if (TARGET_IWMMXT_ABI
2733       && arm_vector_mode_supported_p (mode)
2734       && pcum->named_count > pcum->nargs + 1)
2735     {
2736       if (pcum->iwmmxt_nregs <= 9)
2737         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2738       else
2739         {
2740           pcum->can_split = false;
2741           return NULL_RTX;
2742         }
2743     }
2744
2745   /* Put doubleword aligned quantities in even register pairs.  */
2746   if (pcum->nregs & 1
2747       && ARM_DOUBLEWORD_ALIGN
2748       && arm_needs_doubleword_align (mode, type))
2749     pcum->nregs++;
2750
2751   if (mode == VOIDmode)
2752     /* Compute operand 2 of the call insn.  */
2753     return GEN_INT (pcum->call_cookie);
2754
2755   /* Only allow splitting an arg between regs and memory if all preceding
2756      args were allocated to regs.  For args passed by reference we only count
2757      the reference pointer.  */
2758   if (pcum->can_split)
2759     nregs = 1;
2760   else
2761     nregs = ARM_NUM_REGS2 (mode, type);
2762
2763   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2764     return NULL_RTX;
2765
2766   return gen_rtx_REG (mode, pcum->nregs);
2767 }
2768
2769 static int
2770 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2771                        tree type, bool named ATTRIBUTE_UNUSED)
2772 {
2773   int nregs = pcum->nregs;
2774
2775   if (arm_vector_mode_supported_p (mode))
2776     return 0;
2777
2778   if (NUM_ARG_REGS > nregs
2779       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2780       && pcum->can_split)
2781     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2782
2783   return 0;
2784 }
2785
2786 /* Variable sized types are passed by reference.  This is a GCC
2787    extension to the ARM ABI.  */
2788
2789 static bool
2790 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2791                        enum machine_mode mode ATTRIBUTE_UNUSED,
2792                        tree type, bool named ATTRIBUTE_UNUSED)
2793 {
2794   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2795 }
2796 \f
2797 /* Encode the current state of the #pragma [no_]long_calls.  */
2798 typedef enum
2799 {
2800   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2801   LONG,         /* #pragma long_calls is in effect.  */
2802   SHORT         /* #pragma no_long_calls is in effect.  */
2803 } arm_pragma_enum;
2804
2805 static arm_pragma_enum arm_pragma_long_calls = OFF;
2806
2807 void
2808 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2809 {
2810   arm_pragma_long_calls = LONG;
2811 }
2812
2813 void
2814 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2815 {
2816   arm_pragma_long_calls = SHORT;
2817 }
2818
2819 void
2820 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2821 {
2822   arm_pragma_long_calls = OFF;
2823 }
2824 \f
2825 /* Table of machine attributes.  */
2826 const struct attribute_spec arm_attribute_table[] =
2827 {
2828   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2829   /* Function calls made to this symbol must be done indirectly, because
2830      it may lie outside of the 26 bit addressing range of a normal function
2831      call.  */
2832   { "long_call",    0, 0, false, true,  true,  NULL },
2833   /* Whereas these functions are always known to reside within the 26 bit
2834      addressing range.  */
2835   { "short_call",   0, 0, false, true,  true,  NULL },
2836   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2837   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2838   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2839   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2840 #ifdef ARM_PE
2841   /* ARM/PE has three new attributes:
2842      interfacearm - ?
2843      dllexport - for exporting a function/variable that will live in a dll
2844      dllimport - for importing a function/variable from a dll
2845
2846      Microsoft allows multiple declspecs in one __declspec, separating
2847      them with spaces.  We do NOT support this.  Instead, use __declspec
2848      multiple times.
2849   */
2850   { "dllimport",    0, 0, true,  false, false, NULL },
2851   { "dllexport",    0, 0, true,  false, false, NULL },
2852   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2853 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2854   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2855   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2856   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2857 #endif
2858   { NULL,           0, 0, false, false, false, NULL }
2859 };
2860
2861 /* Handle an attribute requiring a FUNCTION_DECL;
2862    arguments as in struct attribute_spec.handler.  */
2863 static tree
2864 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2865                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2866 {
2867   if (TREE_CODE (*node) != FUNCTION_DECL)
2868     {
2869       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2870                IDENTIFIER_POINTER (name));
2871       *no_add_attrs = true;
2872     }
2873
2874   return NULL_TREE;
2875 }
2876
2877 /* Handle an "interrupt" or "isr" attribute;
2878    arguments as in struct attribute_spec.handler.  */
2879 static tree
2880 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2881                           bool *no_add_attrs)
2882 {
2883   if (DECL_P (*node))
2884     {
2885       if (TREE_CODE (*node) != FUNCTION_DECL)
2886         {
2887           warning (OPT_Wattributes, "%qs attribute only applies to functions",
2888                    IDENTIFIER_POINTER (name));
2889           *no_add_attrs = true;
2890         }
2891       /* FIXME: the argument if any is checked for type attributes;
2892          should it be checked for decl ones?  */
2893     }
2894   else
2895     {
2896       if (TREE_CODE (*node) == FUNCTION_TYPE
2897           || TREE_CODE (*node) == METHOD_TYPE)
2898         {
2899           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2900             {
2901               warning (OPT_Wattributes, "%qs attribute ignored",
2902                        IDENTIFIER_POINTER (name));
2903               *no_add_attrs = true;
2904             }
2905         }
2906       else if (TREE_CODE (*node) == POINTER_TYPE
2907                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2908                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2909                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2910         {
2911           *node = build_variant_type_copy (*node);
2912           TREE_TYPE (*node) = build_type_attribute_variant
2913             (TREE_TYPE (*node),
2914              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2915           *no_add_attrs = true;
2916         }
2917       else
2918         {
2919           /* Possibly pass this attribute on from the type to a decl.  */
2920           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2921                        | (int) ATTR_FLAG_FUNCTION_NEXT
2922                        | (int) ATTR_FLAG_ARRAY_NEXT))
2923             {
2924               *no_add_attrs = true;
2925               return tree_cons (name, args, NULL_TREE);
2926             }
2927           else
2928             {
2929               warning (OPT_Wattributes, "%qs attribute ignored",
2930                        IDENTIFIER_POINTER (name));
2931             }
2932         }
2933     }
2934
2935   return NULL_TREE;
2936 }
2937
2938 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2939 /* Handle the "notshared" attribute.  This attribute is another way of
2940    requesting hidden visibility.  ARM's compiler supports
2941    "__declspec(notshared)"; we support the same thing via an
2942    attribute.  */
2943
2944 static tree
2945 arm_handle_notshared_attribute (tree *node,
2946                                 tree name ATTRIBUTE_UNUSED,
2947                                 tree args ATTRIBUTE_UNUSED,
2948                                 int flags ATTRIBUTE_UNUSED,
2949                                 bool *no_add_attrs)
2950 {
2951   tree decl = TYPE_NAME (*node);
2952
2953   if (decl)
2954     {
2955       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2956       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2957       *no_add_attrs = false;
2958     }
2959   return NULL_TREE;
2960 }
2961 #endif
2962
2963 /* Return 0 if the attributes for two types are incompatible, 1 if they
2964    are compatible, and 2 if they are nearly compatible (which causes a
2965    warning to be generated).  */
2966 static int
2967 arm_comp_type_attributes (tree type1, tree type2)
2968 {
2969   int l1, l2, s1, s2;
2970
2971   /* Check for mismatch of non-default calling convention.  */
2972   if (TREE_CODE (type1) != FUNCTION_TYPE)
2973     return 1;
2974
2975   /* Check for mismatched call attributes.  */
2976   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2977   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2978   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2979   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2980
2981   /* Only bother to check if an attribute is defined.  */
2982   if (l1 | l2 | s1 | s2)
2983     {
2984       /* If one type has an attribute, the other must have the same attribute.  */
2985       if ((l1 != l2) || (s1 != s2))
2986         return 0;
2987
2988       /* Disallow mixed attributes.  */
2989       if ((l1 & s2) || (l2 & s1))
2990         return 0;
2991     }
2992
2993   /* Check for mismatched ISR attribute.  */
2994   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2995   if (! l1)
2996     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2997   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2998   if (! l2)
2999     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
3000   if (l1 != l2)
3001     return 0;
3002
3003   return 1;
3004 }
3005
3006 /*  Encode long_call or short_call attribute by prefixing
3007     symbol name in DECL with a special character FLAG.  */
3008 void
3009 arm_encode_call_attribute (tree decl, int flag)
3010 {
3011   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
3012   int          len = strlen (str);
3013   char *       newstr;
3014
3015   /* Do not allow weak functions to be treated as short call.  */
3016   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
3017     return;
3018
3019   newstr = alloca (len + 2);
3020   newstr[0] = flag;
3021   strcpy (newstr + 1, str);
3022
3023   newstr = (char *) ggc_alloc_string (newstr, len + 1);
3024   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
3025 }
3026
3027 /*  Assigns default attributes to newly defined type.  This is used to
3028     set short_call/long_call attributes for function types of
3029     functions defined inside corresponding #pragma scopes.  */
3030 static void
3031 arm_set_default_type_attributes (tree type)
3032 {
3033   /* Add __attribute__ ((long_call)) to all functions, when
3034      inside #pragma long_calls or __attribute__ ((short_call)),
3035      when inside #pragma no_long_calls.  */
3036   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
3037     {
3038       tree type_attr_list, attr_name;
3039       type_attr_list = TYPE_ATTRIBUTES (type);
3040
3041       if (arm_pragma_long_calls == LONG)
3042         attr_name = get_identifier ("long_call");
3043       else if (arm_pragma_long_calls == SHORT)
3044         attr_name = get_identifier ("short_call");
3045       else
3046         return;
3047
3048       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
3049       TYPE_ATTRIBUTES (type) = type_attr_list;
3050     }
3051 }
3052 \f
3053 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
3054    defined within the current compilation unit.  If this cannot be
3055    determined, then 0 is returned.  */
3056 static int
3057 current_file_function_operand (rtx sym_ref)
3058 {
3059   /* This is a bit of a fib.  A function will have a short call flag
3060      applied to its name if it has the short call attribute, or it has
3061      already been defined within the current compilation unit.  */
3062   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
3063     return 1;
3064
3065   /* The current function is always defined within the current compilation
3066      unit.  If it s a weak definition however, then this may not be the real
3067      definition of the function, and so we have to say no.  */
3068   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
3069       && !DECL_WEAK (current_function_decl))
3070     return 1;
3071
3072   /* We cannot make the determination - default to returning 0.  */
3073   return 0;
3074 }
3075
3076 /* Return nonzero if a 32 bit "long_call" should be generated for
3077    this call.  We generate a long_call if the function:
3078
3079         a.  has an __attribute__((long call))
3080      or b.  is within the scope of a #pragma long_calls
3081      or c.  the -mlong-calls command line switch has been specified
3082          .  and either:
3083                 1. -ffunction-sections is in effect
3084              or 2. the current function has __attribute__ ((section))
3085              or 3. the target function has __attribute__ ((section))
3086
3087    However we do not generate a long call if the function:
3088
3089         d.  has an __attribute__ ((short_call))
3090      or e.  is inside the scope of a #pragma no_long_calls
3091      or f.  is defined within the current compilation unit.
3092
3093    This function will be called by C fragments contained in the machine
3094    description file.  SYM_REF and CALL_COOKIE correspond to the matched
3095    rtl operands.  CALL_SYMBOL is used to distinguish between
3096    two different callers of the function.  It is set to 1 in the
3097    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
3098    and "call_value" patterns.  This is because of the difference in the
3099    SYM_REFs passed by these patterns.  */
3100 int
3101 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
3102 {
3103   if (!call_symbol)
3104     {
3105       if (GET_CODE (sym_ref) != MEM)
3106         return 0;
3107
3108       sym_ref = XEXP (sym_ref, 0);
3109     }
3110
3111   if (GET_CODE (sym_ref) != SYMBOL_REF)
3112     return 0;
3113
3114   if (call_cookie & CALL_SHORT)
3115     return 0;
3116
3117   if (TARGET_LONG_CALLS)
3118     {
3119       if (flag_function_sections
3120           || DECL_SECTION_NAME (current_function_decl))
3121         /* c.3 is handled by the definition of the
3122            ARM_DECLARE_FUNCTION_SIZE macro.  */
3123         return 1;
3124     }
3125
3126   if (current_file_function_operand (sym_ref))
3127     return 0;
3128
3129   return (call_cookie & CALL_LONG)
3130     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
3131     || TARGET_LONG_CALLS;
3132 }
3133
3134 /* Return nonzero if it is ok to make a tail-call to DECL.  */
3135 static bool
3136 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
3137 {
3138   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
3139
3140   if (cfun->machine->sibcall_blocked)
3141     return false;
3142
3143   /* Never tailcall something for which we have no decl, or if we
3144      are in Thumb mode.  */
3145   if (decl == NULL || TARGET_THUMB)
3146     return false;
3147
3148   /* Get the calling method.  */
3149   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3150     call_type = CALL_SHORT;
3151   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
3152     call_type = CALL_LONG;
3153
3154   /* Cannot tail-call to long calls, since these are out of range of
3155      a branch instruction.  However, if not compiling PIC, we know
3156      we can reach the symbol if it is in this compilation unit.  */
3157   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
3158     return false;
3159
3160   /* If we are interworking and the function is not declared static
3161      then we can't tail-call it unless we know that it exists in this
3162      compilation unit (since it might be a Thumb routine).  */
3163   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
3164     return false;
3165
3166   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
3167   if (IS_INTERRUPT (arm_current_func_type ()))
3168     return false;
3169
3170   /* Everything else is ok.  */
3171   return true;
3172 }
3173
3174 \f
3175 /* Addressing mode support functions.  */
3176
3177 /* Return nonzero if X is a legitimate immediate operand when compiling
3178    for PIC.  We know that X satisfies CONSTANT_P and flag_pic is true.  */
3179 int
3180 legitimate_pic_operand_p (rtx x)
3181 {
3182   if (GET_CODE (x) == SYMBOL_REF
3183       || (GET_CODE (x) == CONST
3184           && GET_CODE (XEXP (x, 0)) == PLUS
3185           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF))
3186     return 0;
3187
3188   return 1;
3189 }
3190
3191 rtx
3192 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
3193 {
3194   if (GET_CODE (orig) == SYMBOL_REF
3195       || GET_CODE (orig) == LABEL_REF)
3196     {
3197 #ifndef AOF_ASSEMBLER
3198       rtx pic_ref, address;
3199 #endif
3200       rtx insn;
3201       int subregs = 0;
3202
3203       /* If this function doesn't have a pic register, create one now.
3204          A lot of the logic here is made obscure by the fact that this
3205          routine gets called as part of the rtx cost estimation
3206          process.  We don't want those calls to affect any assumptions
3207          about the real function; and further, we can't call
3208          entry_of_function() until we start the real expansion
3209          process.  */
3210       if (!current_function_uses_pic_offset_table)
3211         {
3212           gcc_assert (!no_new_pseudos);
3213           if (arm_pic_register != INVALID_REGNUM)
3214             {
3215               cfun->machine->pic_reg = gen_rtx_REG (Pmode, arm_pic_register);
3216
3217               /* Play games to avoid marking the function as needing pic
3218                  if we are being called as part of the cost-estimation
3219                  process.  */
3220               if (!ir_type())
3221                 current_function_uses_pic_offset_table = 1;
3222             }
3223           else
3224             {
3225               rtx seq;
3226
3227               cfun->machine->pic_reg = gen_reg_rtx (Pmode);
3228
3229               /* Play games to avoid marking the function as needing pic
3230                  if we are being called as part of the cost-estimation
3231                  process.  */
3232               if (!ir_type())
3233                 {
3234                   current_function_uses_pic_offset_table = 1;
3235                   start_sequence ();
3236
3237                   arm_load_pic_register (0UL);
3238
3239                   seq = get_insns ();
3240                   end_sequence ();
3241                   emit_insn_after (seq, entry_of_function ());
3242                 }
3243             }
3244         }
3245
3246       if (reg == 0)
3247         {
3248           gcc_assert (!no_new_pseudos);
3249           reg = gen_reg_rtx (Pmode);
3250
3251           subregs = 1;
3252         }
3253
3254 #ifdef AOF_ASSEMBLER
3255       /* The AOF assembler can generate relocations for these directly, and
3256          understands that the PIC register has to be added into the offset.  */
3257       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
3258 #else
3259       if (subregs)
3260         address = gen_reg_rtx (Pmode);
3261       else
3262         address = reg;
3263
3264       if (TARGET_ARM)
3265         emit_insn (gen_pic_load_addr_arm (address, orig));
3266       else
3267         emit_insn (gen_pic_load_addr_thumb (address, orig));
3268
3269       if ((GET_CODE (orig) == LABEL_REF
3270            || (GET_CODE (orig) == SYMBOL_REF &&
3271                SYMBOL_REF_LOCAL_P (orig)))
3272           && NEED_GOT_RELOC)
3273         pic_ref = gen_rtx_PLUS (Pmode, cfun->machine->pic_reg, address);
3274       else
3275         {
3276           pic_ref = gen_const_mem (Pmode,
3277                                    gen_rtx_PLUS (Pmode, cfun->machine->pic_reg,
3278                                                  address));
3279         }
3280
3281       insn = emit_move_insn (reg, pic_ref);
3282 #endif
3283       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3284          by loop.  */
3285       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3286                                             REG_NOTES (insn));
3287       return reg;
3288     }
3289   else if (GET_CODE (orig) == CONST)
3290     {
3291       rtx base, offset;
3292
3293       if (GET_CODE (XEXP (orig, 0)) == PLUS
3294           && XEXP (XEXP (orig, 0), 0) == cfun->machine->pic_reg)
3295         return orig;
3296
3297       if (GET_CODE (XEXP (orig, 0)) == UNSPEC
3298           && XINT (XEXP (orig, 0), 1) == UNSPEC_TLS)
3299         return orig;
3300
3301       if (reg == 0)
3302         {
3303           gcc_assert (!no_new_pseudos);
3304           reg = gen_reg_rtx (Pmode);
3305         }
3306
3307       gcc_assert (GET_CODE (XEXP (orig, 0)) == PLUS);
3308
3309       base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3310       offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3311                                        base == reg ? 0 : reg);
3312
3313       if (GET_CODE (offset) == CONST_INT)
3314         {
3315           /* The base register doesn't really matter, we only want to
3316              test the index for the appropriate mode.  */
3317           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3318             {
3319               gcc_assert (!no_new_pseudos);
3320               offset = force_reg (Pmode, offset);
3321             }
3322
3323           if (GET_CODE (offset) == CONST_INT)
3324             return plus_constant (base, INTVAL (offset));
3325         }
3326
3327       if (GET_MODE_SIZE (mode) > 4
3328           && (GET_MODE_CLASS (mode) == MODE_INT
3329               || TARGET_SOFT_FLOAT))
3330         {
3331           emit_insn (gen_addsi3 (reg, base, offset));
3332           return reg;
3333         }
3334
3335       return gen_rtx_PLUS (Pmode, base, offset);
3336     }
3337
3338   return orig;
3339 }
3340
3341
3342 /* Find a spare low register to use during the prolog of a function.  */
3343
3344 static int
3345 thumb_find_work_register (unsigned long pushed_regs_mask)
3346 {
3347   int reg;
3348
3349   /* Check the argument registers first as these are call-used.  The
3350      register allocation order means that sometimes r3 might be used
3351      but earlier argument registers might not, so check them all.  */
3352   for (reg = LAST_ARG_REGNUM; reg >= 0; reg --)
3353     if (!regs_ever_live[reg])
3354       return reg;
3355
3356   /* Before going on to check the call-saved registers we can try a couple
3357      more ways of deducing that r3 is available.  The first is when we are
3358      pushing anonymous arguments onto the stack and we have less than 4
3359      registers worth of fixed arguments(*).  In this case r3 will be part of
3360      the variable argument list and so we can be sure that it will be
3361      pushed right at the start of the function.  Hence it will be available
3362      for the rest of the prologue.
3363      (*): ie current_function_pretend_args_size is greater than 0.  */
3364   if (cfun->machine->uses_anonymous_args
3365       && current_function_pretend_args_size > 0)
3366     return LAST_ARG_REGNUM;
3367
3368   /* The other case is when we have fixed arguments but less than 4 registers
3369      worth.  In this case r3 might be used in the body of the function, but
3370      it is not being used to convey an argument into the function.  In theory
3371      we could just check current_function_args_size to see how many bytes are
3372      being passed in argument registers, but it seems that it is unreliable.
3373      Sometimes it will have the value 0 when in fact arguments are being
3374      passed.  (See testcase execute/20021111-1.c for an example).  So we also
3375      check the args_info.nregs field as well.  The problem with this field is
3376      that it makes no allowances for arguments that are passed to the
3377      function but which are not used.  Hence we could miss an opportunity
3378      when a function has an unused argument in r3.  But it is better to be
3379      safe than to be sorry.  */
3380   if (! cfun->machine->uses_anonymous_args
3381       && current_function_args_size >= 0
3382       && current_function_args_size <= (LAST_ARG_REGNUM * UNITS_PER_WORD)
3383       && cfun->args_info.nregs < 4)
3384     return LAST_ARG_REGNUM;
3385
3386   /* Otherwise look for a call-saved register that is going to be pushed.  */
3387   for (reg = LAST_LO_REGNUM; reg > LAST_ARG_REGNUM; reg --)
3388     if (pushed_regs_mask & (1 << reg))
3389       return reg;
3390
3391   /* Something went wrong - thumb_compute_save_reg_mask()
3392      should have arranged for a suitable register to be pushed.  */
3393   gcc_unreachable ();
3394 }
3395
3396 static GTY(()) int pic_labelno;
3397
3398 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3399    low register.  */
3400
3401 void
3402 arm_load_pic_register (unsigned long saved_regs ATTRIBUTE_UNUSED)
3403 {
3404 #ifndef AOF_ASSEMBLER
3405   rtx l1, labelno, pic_tmp, pic_tmp2, pic_rtx;
3406   rtx global_offset_table;
3407
3408   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3409     return;
3410
3411   gcc_assert (flag_pic);
3412
3413   /* We use an UNSPEC rather than a LABEL_REF because this label never appears
3414      in the code stream.  */
3415
3416   labelno = GEN_INT (pic_labelno++);
3417   l1 = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3418   l1 = gen_rtx_CONST (VOIDmode, l1);
3419
3420   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3421   /* On the ARM the PC register contains 'dot + 8' at the time of the
3422      addition, on the Thumb it is 'dot + 4'.  */
3423   pic_tmp = plus_constant (l1, TARGET_ARM ? 8 : 4);
3424   if (GOT_PCREL)
3425     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3426                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3427   else
3428     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3429
3430   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3431
3432   if (TARGET_ARM)
3433     {
3434       emit_insn (gen_pic_load_addr_arm (cfun->machine->pic_reg, pic_rtx));
3435       emit_insn (gen_pic_add_dot_plus_eight (cfun->machine->pic_reg,
3436                                              cfun->machine->pic_reg, labelno));
3437     }
3438   else
3439     {
3440       if (arm_pic_register != INVALID_REGNUM
3441           && REGNO (cfun->machine->pic_reg) > LAST_LO_REGNUM)
3442         {
3443           /* We will have pushed the pic register, so we should always be
3444              able to find a work register.  */
3445           pic_tmp = gen_rtx_REG (SImode,
3446                                  thumb_find_work_register (saved_regs));
3447           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3448           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3449         }
3450       else
3451         emit_insn (gen_pic_load_addr_thumb (cfun->machine->pic_reg, pic_rtx));
3452       emit_insn (gen_pic_add_dot_plus_four (cfun->machine->pic_reg,
3453                                             cfun->machine->pic_reg, labelno));
3454     }
3455
3456   /* Need to emit this whether or not we obey regdecls,
3457      since setjmp/longjmp can cause life info to screw up.  */
3458   emit_insn (gen_rtx_USE (VOIDmode, cfun->machine->pic_reg));
3459 #endif /* AOF_ASSEMBLER */
3460 }
3461
3462
3463 /* Return nonzero if X is valid as an ARM state addressing register.  */
3464 static int
3465 arm_address_register_rtx_p (rtx x, int strict_p)
3466 {
3467   int regno;
3468
3469   if (GET_CODE (x) != REG)
3470     return 0;
3471
3472   regno = REGNO (x);
3473
3474   if (strict_p)
3475     return ARM_REGNO_OK_FOR_BASE_P (regno);
3476
3477   return (regno <= LAST_ARM_REGNUM
3478           || regno >= FIRST_PSEUDO_REGISTER
3479           || regno == FRAME_POINTER_REGNUM
3480           || regno == ARG_POINTER_REGNUM);
3481 }
3482
3483 /* Return TRUE if this rtx is the difference of a symbol and a label,
3484    and will reduce to a PC-relative relocation in the object file.
3485    Expressions like this can be left alone when generating PIC, rather
3486    than forced through the GOT.  */
3487 static int
3488 pcrel_constant_p (rtx x)
3489 {
3490   if (GET_CODE (x) == MINUS)
3491     return symbol_mentioned_p (XEXP (x, 0)) && label_mentioned_p (XEXP (x, 1));
3492
3493   return FALSE;
3494 }
3495
3496 /* Return nonzero if X is a valid ARM state address operand.  */
3497 int
3498 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3499                           int strict_p)
3500 {
3501   bool use_ldrd;
3502   enum rtx_code code = GET_CODE (x);
3503
3504   if (arm_address_register_rtx_p (x, strict_p))
3505     return 1;
3506
3507   use_ldrd = (TARGET_LDRD
3508               && (mode == DImode
3509                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3510
3511   if (code == POST_INC || code == PRE_DEC
3512       || ((code == PRE_INC || code == POST_DEC)
3513           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3514     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3515
3516   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3517            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3518            && GET_CODE (XEXP (x, 1)) == PLUS
3519            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3520     {
3521       rtx addend = XEXP (XEXP (x, 1), 1);
3522
3523       /* Don't allow ldrd post increment by register because it's hard
3524          to fixup invalid register choices.  */
3525       if (use_ldrd
3526           && GET_CODE (x) == POST_MODIFY
3527           && GET_CODE (addend) == REG)
3528         return 0;
3529
3530       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3531               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3532     }
3533
3534   /* After reload constants split into minipools will have addresses
3535      from a LABEL_REF.  */
3536   else if (reload_completed
3537            && (code == LABEL_REF
3538                || (code == CONST
3539                    && GET_CODE (XEXP (x, 0)) == PLUS
3540                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3541                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3542     return 1;
3543
3544   else if (mode == TImode)
3545     return 0;
3546
3547   else if (code == PLUS)
3548     {
3549       rtx xop0 = XEXP (x, 0);
3550       rtx xop1 = XEXP (x, 1);
3551
3552       return ((arm_address_register_rtx_p (xop0, strict_p)
3553                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3554               || (arm_address_register_rtx_p (xop1, strict_p)
3555                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3556     }
3557
3558 #if 0
3559   /* Reload currently can't handle MINUS, so disable this for now */
3560   else if (GET_CODE (x) == MINUS)
3561     {
3562       rtx xop0 = XEXP (x, 0);
3563       rtx xop1 = XEXP (x, 1);
3564
3565       return (arm_address_register_rtx_p (xop0, strict_p)
3566               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3567     }
3568 #endif
3569
3570   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3571            && code == SYMBOL_REF
3572            && CONSTANT_POOL_ADDRESS_P (x)
3573            && ! (flag_pic
3574                  && symbol_mentioned_p (get_pool_constant (x))
3575                  && ! pcrel_constant_p (get_pool_constant (x))))
3576     return 1;
3577
3578   return 0;
3579 }
3580
3581 /* Return nonzero if INDEX is valid for an address index operand in
3582    ARM state.  */
3583 static int
3584 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3585                         int strict_p)
3586 {
3587   HOST_WIDE_INT range;
3588   enum rtx_code code = GET_CODE (index);
3589
3590   /* Standard coprocessor addressing modes.  */
3591   if (TARGET_HARD_FLOAT
3592       && (TARGET_FPA || TARGET_MAVERICK)
3593       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3594           || (TARGET_MAVERICK && mode == DImode)))
3595     return (code == CONST_INT && INTVAL (index) < 1024
3596             && INTVAL (index) > -1024
3597             && (INTVAL (index) & 3) == 0);
3598
3599   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3600     return (code == CONST_INT
3601             && INTVAL (index) < 1024
3602             && INTVAL (index) > -1024
3603             && (INTVAL (index) & 3) == 0);
3604
3605   if (arm_address_register_rtx_p (index, strict_p)
3606       && (GET_MODE_SIZE (mode) <= 4))
3607     return 1;
3608
3609   if (mode == DImode || mode == DFmode)
3610     {
3611       if (code == CONST_INT)
3612         {
3613           HOST_WIDE_INT val = INTVAL (index);
3614
3615           if (TARGET_LDRD)
3616             return val > -256 && val < 256;
3617           else
3618             return val > -4096 && val < 4092;
3619         }
3620
3621       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3622     }
3623
3624   if (GET_MODE_SIZE (mode) <= 4
3625       && ! (arm_arch4
3626             && (mode == HImode
3627                 || (mode == QImode && outer == SIGN_EXTEND))))
3628     {
3629       if (code == MULT)
3630         {
3631           rtx xiop0 = XEXP (index, 0);
3632           rtx xiop1 = XEXP (index, 1);
3633
3634           return ((arm_address_register_rtx_p (xiop0, strict_p)
3635                    && power_of_two_operand (xiop1, SImode))
3636                   || (arm_address_register_rtx_p (xiop1, strict_p)
3637                       && power_of_two_operand (xiop0, SImode)));
3638         }
3639       else if (code == LSHIFTRT || code == ASHIFTRT
3640                || code == ASHIFT || code == ROTATERT)
3641         {
3642           rtx op = XEXP (index, 1);
3643
3644           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3645                   && GET_CODE (op) == CONST_INT
3646                   && INTVAL (op) > 0
3647                   && INTVAL (op) <= 31);
3648         }
3649     }
3650
3651   /* For ARM v4 we may be doing a sign-extend operation during the
3652      load.  */
3653   if (arm_arch4)
3654     {
3655       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3656         range = 256;
3657       else
3658         range = 4096;
3659     }
3660   else
3661     range = (mode == HImode) ? 4095 : 4096;
3662
3663   return (code == CONST_INT
3664           && INTVAL (index) < range
3665           && INTVAL (index) > -range);
3666 }
3667
3668 /* Return nonzero if X is valid as a Thumb state base register.  */
3669 static int
3670 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3671 {
3672   int regno;
3673
3674   if (GET_CODE (x) != REG)
3675     return 0;
3676
3677   regno = REGNO (x);
3678
3679   if (strict_p)
3680     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3681
3682   return (regno <= LAST_LO_REGNUM
3683           || regno > LAST_VIRTUAL_REGISTER
3684           || regno == FRAME_POINTER_REGNUM
3685           || (GET_MODE_SIZE (mode) >= 4
3686               && (regno == STACK_POINTER_REGNUM
3687                   || regno >= FIRST_PSEUDO_REGISTER
3688                   || x == hard_frame_pointer_rtx
3689                   || x == arg_pointer_rtx)));
3690 }
3691
3692 /* Return nonzero if x is a legitimate index register.  This is the case
3693    for any base register that can access a QImode object.  */
3694 inline static int
3695 thumb_index_register_rtx_p (rtx x, int strict_p)
3696 {
3697   return thumb_base_register_rtx_p (x, QImode, strict_p);
3698 }
3699
3700 /* Return nonzero if x is a legitimate Thumb-state address.
3701
3702    The AP may be eliminated to either the SP or the FP, so we use the
3703    least common denominator, e.g. SImode, and offsets from 0 to 64.
3704
3705    ??? Verify whether the above is the right approach.
3706
3707    ??? Also, the FP may be eliminated to the SP, so perhaps that
3708    needs special handling also.
3709
3710    ??? Look at how the mips16 port solves this problem.  It probably uses
3711    better ways to solve some of these problems.
3712
3713    Although it is not incorrect, we don't accept QImode and HImode
3714    addresses based on the frame pointer or arg pointer until the
3715    reload pass starts.  This is so that eliminating such addresses
3716    into stack based ones won't produce impossible code.  */
3717 int
3718 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3719 {
3720   /* ??? Not clear if this is right.  Experiment.  */
3721   if (GET_MODE_SIZE (mode) < 4
3722       && !(reload_in_progress || reload_completed)
3723       && (reg_mentioned_p (frame_pointer_rtx, x)
3724           || reg_mentioned_p (arg_pointer_rtx, x)
3725           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3726           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3727           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3728           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3729     return 0;
3730
3731   /* Accept any base register.  SP only in SImode or larger.  */
3732   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3733     return 1;
3734
3735   /* This is PC relative data before arm_reorg runs.  */
3736   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3737            && GET_CODE (x) == SYMBOL_REF
3738            && CONSTANT_POOL_ADDRESS_P (x) && !flag_pic)
3739     return 1;
3740
3741   /* This is PC relative data after arm_reorg runs.  */
3742   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3743            && (GET_CODE (x) == LABEL_REF
3744                || (GET_CODE (x) == CONST
3745                    && GET_CODE (XEXP (x, 0)) == PLUS
3746                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3747                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3748     return 1;
3749
3750   /* Post-inc indexing only supported for SImode and larger.  */
3751   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3752            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3753     return 1;
3754
3755   else if (GET_CODE (x) == PLUS)
3756     {
3757       /* REG+REG address can be any two index registers.  */
3758       /* We disallow FRAME+REG addressing since we know that FRAME
3759          will be replaced with STACK, and SP relative addressing only
3760          permits SP+OFFSET.  */
3761       if (GET_MODE_SIZE (mode) <= 4
3762           && XEXP (x, 0) != frame_pointer_rtx
3763           && XEXP (x, 1) != frame_pointer_rtx
3764           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3765           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3766         return 1;
3767
3768       /* REG+const has 5-7 bit offset for non-SP registers.  */
3769       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3770                 || XEXP (x, 0) == arg_pointer_rtx)
3771                && GET_CODE (XEXP (x, 1)) == CONST_INT
3772                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3773         return 1;
3774
3775       /* REG+const has 10 bit offset for SP, but only SImode and
3776          larger is supported.  */
3777       /* ??? Should probably check for DI/DFmode overflow here
3778          just like GO_IF_LEGITIMATE_OFFSET does.  */
3779       else if (GET_CODE (XEXP (x, 0)) == REG
3780                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3781                && GET_MODE_SIZE (mode) >= 4
3782                && GET_CODE (XEXP (x, 1)) == CONST_INT
3783                && INTVAL (XEXP (x, 1)) >= 0
3784                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3785                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3786         return 1;
3787
3788       else if (GET_CODE (XEXP (x, 0)) == REG
3789                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3790                && GET_MODE_SIZE (mode) >= 4
3791                && GET_CODE (XEXP (x, 1)) == CONST_INT
3792                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3793         return 1;
3794     }
3795
3796   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3797            && GET_MODE_SIZE (mode) == 4
3798            && GET_CODE (x) == SYMBOL_REF
3799            && CONSTANT_POOL_ADDRESS_P (x)
3800            && ! (flag_pic
3801                  && symbol_mentioned_p (get_pool_constant (x))
3802                  && ! pcrel_constant_p (get_pool_constant (x))))
3803     return 1;
3804
3805   return 0;
3806 }
3807
3808 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3809    instruction of mode MODE.  */
3810 int
3811 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3812 {
3813   switch (GET_MODE_SIZE (mode))
3814     {
3815     case 1:
3816       return val >= 0 && val < 32;
3817
3818     case 2:
3819       return val >= 0 && val < 64 && (val & 1) == 0;
3820
3821     default:
3822       return (val >= 0
3823               && (val + GET_MODE_SIZE (mode)) <= 128
3824               && (val & 3) == 0);
3825     }
3826 }
3827
3828 /* Build the SYMBOL_REF for __tls_get_addr.  */
3829
3830 static GTY(()) rtx tls_get_addr_libfunc;
3831
3832 static rtx
3833 get_tls_get_addr (void)
3834 {
3835   if (!tls_get_addr_libfunc)
3836     tls_get_addr_libfunc = init_one_libfunc ("__tls_get_addr");
3837   return tls_get_addr_libfunc;
3838 }
3839
3840 static rtx
3841 arm_load_tp (rtx target)
3842 {
3843   if (!target)
3844     target = gen_reg_rtx (SImode);
3845
3846   if (TARGET_HARD_TP)
3847     {
3848       /* Can return in any reg.  */
3849       emit_insn (gen_load_tp_hard (target));
3850     }
3851   else
3852     {
3853       /* Always returned in r0.  Immediately copy the result into a pseudo,
3854          otherwise other uses of r0 (e.g. setting up function arguments) may
3855          clobber the value.  */
3856
3857       rtx tmp;
3858
3859       emit_insn (gen_load_tp_soft ());
3860
3861       tmp = gen_rtx_REG (SImode, 0);
3862       emit_move_insn (target, tmp);
3863     }
3864   return target;
3865 }
3866
3867 static rtx
3868 load_tls_operand (rtx x, rtx reg)
3869 {
3870   rtx tmp;
3871
3872   if (reg == NULL_RTX)
3873     reg = gen_reg_rtx (SImode);
3874
3875   tmp = gen_rtx_CONST (SImode, x);
3876
3877   emit_move_insn (reg, tmp);
3878
3879   return reg;
3880 }
3881
3882 static rtx
3883 arm_call_tls_get_addr (rtx x, rtx reg, rtx *valuep, int reloc)
3884 {
3885   rtx insns, label, labelno, sum;
3886
3887   start_sequence ();
3888
3889   labelno = GEN_INT (pic_labelno++);
3890   label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3891   label = gen_rtx_CONST (VOIDmode, label);
3892
3893   sum = gen_rtx_UNSPEC (Pmode,
3894                         gen_rtvec (4, x, GEN_INT (reloc), 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_pic_add_dot_plus_eight (reg, reg, labelno));
3901   else
3902     emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3903
3904   *valuep = emit_library_call_value (get_tls_get_addr (), NULL_RTX, LCT_PURE, /* LCT_CONST?  */
3905                                      Pmode, 1, reg, Pmode);
3906
3907   insns = get_insns ();
3908   end_sequence ();
3909
3910   return insns;
3911 }
3912
3913 rtx
3914 legitimize_tls_address (rtx x, rtx reg)
3915 {
3916   rtx dest, tp, label, labelno, sum, insns, ret, eqv, addend;
3917   unsigned int model = SYMBOL_REF_TLS_MODEL (x);
3918
3919   switch (model)
3920     {
3921     case TLS_MODEL_GLOBAL_DYNAMIC:
3922       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_GD32);
3923       dest = gen_reg_rtx (Pmode);
3924       emit_libcall_block (insns, dest, ret, x);
3925       return dest;
3926
3927     case TLS_MODEL_LOCAL_DYNAMIC:
3928       insns = arm_call_tls_get_addr (x, reg, &ret, TLS_LDM32);
3929
3930       /* Attach a unique REG_EQUIV, to allow the RTL optimizers to
3931          share the LDM result with other LD model accesses.  */
3932       eqv = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const1_rtx),
3933                             UNSPEC_TLS);
3934       dest = gen_reg_rtx (Pmode);
3935       emit_libcall_block (insns, dest, ret, eqv);
3936
3937       /* Load the addend.  */
3938       addend = gen_rtx_UNSPEC (Pmode, gen_rtvec (2, x, GEN_INT (TLS_LDO32)),
3939                                UNSPEC_TLS);
3940       addend = force_reg (SImode, gen_rtx_CONST (SImode, addend));
3941       return gen_rtx_PLUS (Pmode, dest, addend);
3942
3943     case TLS_MODEL_INITIAL_EXEC:
3944       labelno = GEN_INT (pic_labelno++);
3945       label = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, labelno), UNSPEC_PIC_LABEL);
3946       label = gen_rtx_CONST (VOIDmode, label);
3947       sum = gen_rtx_UNSPEC (Pmode,
3948                             gen_rtvec (4, x, GEN_INT (TLS_IE32), label,
3949                                        GEN_INT (TARGET_ARM ? 8 : 4)),
3950                             UNSPEC_TLS);
3951       reg = load_tls_operand (sum, reg);
3952
3953       if (TARGET_ARM)
3954         emit_insn (gen_tls_load_dot_plus_eight (reg, reg, labelno));
3955       else
3956         {
3957           emit_insn (gen_pic_add_dot_plus_four (reg, reg, labelno));
3958           emit_move_insn (reg, gen_const_mem (SImode, reg));
3959         }
3960
3961       tp = arm_load_tp (NULL_RTX);
3962
3963       return gen_rtx_PLUS (Pmode, tp, reg);
3964
3965     case TLS_MODEL_LOCAL_EXEC:
3966       tp = arm_load_tp (NULL_RTX);
3967
3968       reg = gen_rtx_UNSPEC (Pmode,
3969                             gen_rtvec (2, x, GEN_INT (TLS_LE32)),
3970                             UNSPEC_TLS);
3971       reg = force_reg (SImode, gen_rtx_CONST (SImode, reg));
3972
3973       return gen_rtx_PLUS (Pmode, tp, reg);
3974
3975     default:
3976       abort ();
3977     }
3978 }
3979
3980 /* Try machine-dependent ways of modifying an illegitimate address
3981    to be legitimate.  If we find one, return the new, valid address.  */
3982 rtx
3983 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3984 {
3985   if (arm_tls_symbol_p (x))
3986     return legitimize_tls_address (x, NULL_RTX);
3987
3988   if (GET_CODE (x) == PLUS)
3989     {
3990       rtx xop0 = XEXP (x, 0);
3991       rtx xop1 = XEXP (x, 1);
3992
3993       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3994         xop0 = force_reg (SImode, xop0);
3995
3996       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3997         xop1 = force_reg (SImode, xop1);
3998
3999       if (ARM_BASE_REGISTER_RTX_P (xop0)
4000           && GET_CODE (xop1) == CONST_INT)
4001         {
4002           HOST_WIDE_INT n, low_n;
4003           rtx base_reg, val;
4004           n = INTVAL (xop1);
4005
4006           /* VFP addressing modes actually allow greater offsets, but for
4007              now we just stick with the lowest common denominator.  */
4008           if (mode == DImode
4009               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
4010             {
4011               low_n = n & 0x0f;
4012               n &= ~0x0f;
4013               if (low_n > 4)
4014                 {
4015                   n += 16;
4016                   low_n -= 16;
4017                 }
4018             }
4019           else
4020             {
4021               low_n = ((mode) == TImode ? 0
4022                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
4023               n -= low_n;
4024             }
4025
4026           base_reg = gen_reg_rtx (SImode);
4027           val = force_operand (plus_constant (xop0, n), NULL_RTX);
4028           emit_move_insn (base_reg, val);
4029           x = plus_constant (base_reg, low_n);
4030         }
4031       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4032         x = gen_rtx_PLUS (SImode, xop0, xop1);
4033     }
4034
4035   /* XXX We don't allow MINUS any more -- see comment in
4036      arm_legitimate_address_p ().  */
4037   else if (GET_CODE (x) == MINUS)
4038     {
4039       rtx xop0 = XEXP (x, 0);
4040       rtx xop1 = XEXP (x, 1);
4041
4042       if (CONSTANT_P (xop0))
4043         xop0 = force_reg (SImode, xop0);
4044
4045       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
4046         xop1 = force_reg (SImode, xop1);
4047
4048       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
4049         x = gen_rtx_MINUS (SImode, xop0, xop1);
4050     }
4051
4052   /* Make sure to take full advantage of the pre-indexed addressing mode
4053      with absolute addresses which often allows for the base register to
4054      be factorized for multiple adjacent memory references, and it might
4055      even allows for the mini pool to be avoided entirely. */
4056   else if (GET_CODE (x) == CONST_INT && optimize > 0)
4057     {
4058       unsigned int bits;
4059       HOST_WIDE_INT mask, base, index;
4060       rtx base_reg;
4061
4062       /* ldr and ldrb can use a 12 bit index, ldrsb and the rest can only
4063          use a 8 bit index. So let's use a 12 bit index for SImode only and
4064          hope that arm_gen_constant will enable ldrb to use more bits. */
4065       bits = (mode == SImode) ? 12 : 8;
4066       mask = (1 << bits) - 1;
4067       base = INTVAL (x) & ~mask;
4068       index = INTVAL (x) & mask;
4069       if (bit_count (base & 0xffffffff) > (32 - bits)/2)
4070         {
4071           /* It'll most probably be more efficient to generate the base
4072              with more bits set and use a negative index instead. */
4073           base |= mask;
4074           index -= mask;
4075         }
4076       base_reg = force_reg (SImode, GEN_INT (base));
4077       x = plus_constant (base_reg, index);
4078     }
4079
4080   if (flag_pic)
4081     {
4082       /* We need to find and carefully transform any SYMBOL and LABEL
4083          references; so go back to the original address expression.  */
4084       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4085
4086       if (new_x != orig_x)
4087         x = new_x;
4088     }
4089
4090   return x;
4091 }
4092
4093
4094 /* Try machine-dependent ways of modifying an illegitimate Thumb address
4095    to be legitimate.  If we find one, return the new, valid address.  */
4096 rtx
4097 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
4098 {
4099   if (arm_tls_symbol_p (x))
4100     return legitimize_tls_address (x, NULL_RTX);
4101
4102   if (GET_CODE (x) == PLUS
4103       && GET_CODE (XEXP (x, 1)) == CONST_INT
4104       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
4105           || INTVAL (XEXP (x, 1)) < 0))
4106     {
4107       rtx xop0 = XEXP (x, 0);
4108       rtx xop1 = XEXP (x, 1);
4109       HOST_WIDE_INT offset = INTVAL (xop1);
4110
4111       /* Try and fold the offset into a biasing of the base register and
4112          then offsetting that.  Don't do this when optimizing for space
4113          since it can cause too many CSEs.  */
4114       if (optimize_size && offset >= 0
4115           && offset < 256 + 31 * GET_MODE_SIZE (mode))
4116         {
4117           HOST_WIDE_INT delta;
4118
4119           if (offset >= 256)
4120             delta = offset - (256 - GET_MODE_SIZE (mode));
4121           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
4122             delta = 31 * GET_MODE_SIZE (mode);
4123           else
4124             delta = offset & (~31 * GET_MODE_SIZE (mode));
4125
4126           xop0 = force_operand (plus_constant (xop0, offset - delta),
4127                                 NULL_RTX);
4128           x = plus_constant (xop0, delta);
4129         }
4130       else if (offset < 0 && offset > -256)
4131         /* Small negative offsets are best done with a subtract before the
4132            dereference, forcing these into a register normally takes two
4133            instructions.  */
4134         x = force_operand (x, NULL_RTX);
4135       else
4136         {
4137           /* For the remaining cases, force the constant into a register.  */
4138           xop1 = force_reg (SImode, xop1);
4139           x = gen_rtx_PLUS (SImode, xop0, xop1);
4140         }
4141     }
4142   else if (GET_CODE (x) == PLUS
4143            && s_register_operand (XEXP (x, 1), SImode)
4144            && !s_register_operand (XEXP (x, 0), SImode))
4145     {
4146       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
4147
4148       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
4149     }
4150
4151   if (flag_pic)
4152     {
4153       /* We need to find and carefully transform any SYMBOL and LABEL
4154          references; so go back to the original address expression.  */
4155       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
4156
4157       if (new_x != orig_x)
4158         x = new_x;
4159     }
4160
4161   return x;
4162 }
4163
4164 rtx
4165 thumb_legitimize_reload_address (rtx *x_p,
4166                                  enum machine_mode mode,
4167                                  int opnum, int type,
4168                                  int ind_levels ATTRIBUTE_UNUSED)
4169 {
4170   rtx x = *x_p;
4171
4172   if (GET_CODE (x) == PLUS
4173       && GET_MODE_SIZE (mode) < 4
4174       && REG_P (XEXP (x, 0))
4175       && XEXP (x, 0) == stack_pointer_rtx
4176       && GET_CODE (XEXP (x, 1)) == CONST_INT
4177       && !thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
4178     {
4179       rtx orig_x = x;
4180
4181       x = copy_rtx (x);
4182       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4183                    Pmode, VOIDmode, 0, 0, opnum, type);
4184       return x;
4185     }
4186
4187   /* If both registers are hi-regs, then it's better to reload the
4188      entire expression rather than each register individually.  That
4189      only requires one reload register rather than two.  */
4190   if (GET_CODE (x) == PLUS
4191       && REG_P (XEXP (x, 0))
4192       && REG_P (XEXP (x, 1))
4193       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 0), mode)
4194       && !REG_MODE_OK_FOR_REG_BASE_P (XEXP (x, 1), mode))
4195     {
4196       rtx orig_x = x;
4197
4198       x = copy_rtx (x);
4199       push_reload (orig_x, NULL_RTX, x_p, NULL, MODE_BASE_REG_CLASS (mode),
4200                    Pmode, VOIDmode, 0, 0, opnum, type);
4201       return x;
4202     }
4203
4204   return NULL;
4205 }
4206
4207 /* Test for various thread-local symbols.  */
4208
4209 /* Return TRUE if X is a thread-local symbol.  */
4210
4211 static bool
4212 arm_tls_symbol_p (rtx x)
4213 {
4214   if (! TARGET_HAVE_TLS)
4215     return false;
4216
4217   if (GET_CODE (x) != SYMBOL_REF)
4218     return false;
4219
4220   return SYMBOL_REF_TLS_MODEL (x) != 0;
4221 }
4222
4223 /* Helper for arm_tls_referenced_p.  */
4224
4225 static int
4226 arm_tls_operand_p_1 (rtx *x, void *data ATTRIBUTE_UNUSED)
4227 {
4228   if (GET_CODE (*x) == SYMBOL_REF)
4229     return SYMBOL_REF_TLS_MODEL (*x) != 0;
4230
4231   /* Don't recurse into UNSPEC_TLS looking for TLS symbols; these are
4232      TLS offsets, not real symbol references.  */
4233   if (GET_CODE (*x) == UNSPEC
4234       && XINT (*x, 1) == UNSPEC_TLS)
4235     return -1;
4236
4237   return 0;
4238 }
4239
4240 /* Return TRUE if X contains any TLS symbol references.  */
4241
4242 bool
4243 arm_tls_referenced_p (rtx x)
4244 {
4245   if (! TARGET_HAVE_TLS)
4246     return false;
4247
4248   return for_each_rtx (&x, arm_tls_operand_p_1, NULL);
4249 }
4250 \f
4251 #define REG_OR_SUBREG_REG(X)                                            \
4252   (GET_CODE (X) == REG                                                  \
4253    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
4254
4255 #define REG_OR_SUBREG_RTX(X)                    \
4256    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
4257
4258 #ifndef COSTS_N_INSNS
4259 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
4260 #endif
4261 static inline int
4262 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
4263 {
4264   enum machine_mode mode = GET_MODE (x);
4265
4266   switch (code)
4267     {
4268     case ASHIFT:
4269     case ASHIFTRT:
4270     case LSHIFTRT:
4271     case ROTATERT:
4272     case PLUS:
4273     case MINUS:
4274     case COMPARE:
4275     case NEG:
4276     case NOT:
4277       return COSTS_N_INSNS (1);
4278
4279     case MULT:
4280       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4281         {
4282           int cycles = 0;
4283           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
4284
4285           while (i)
4286             {
4287               i >>= 2;
4288               cycles++;
4289             }
4290           return COSTS_N_INSNS (2) + cycles;
4291         }
4292       return COSTS_N_INSNS (1) + 16;
4293
4294     case SET:
4295       return (COSTS_N_INSNS (1)
4296               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
4297                      + GET_CODE (SET_DEST (x)) == MEM));
4298
4299     case CONST_INT:
4300       if (outer == SET)
4301         {
4302           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
4303             return 0;
4304           if (thumb_shiftable_const (INTVAL (x)))
4305             return COSTS_N_INSNS (2);
4306           return COSTS_N_INSNS (3);
4307         }
4308       else if ((outer == PLUS || outer == COMPARE)
4309                && INTVAL (x) < 256 && INTVAL (x) > -256)
4310         return 0;
4311       else if (outer == AND
4312                && INTVAL (x) < 256 && INTVAL (x) >= -256)
4313         return COSTS_N_INSNS (1);
4314       else if (outer == ASHIFT || outer == ASHIFTRT
4315                || outer == LSHIFTRT)
4316         return 0;
4317       return COSTS_N_INSNS (2);
4318
4319     case CONST:
4320     case CONST_DOUBLE:
4321     case LABEL_REF:
4322     case SYMBOL_REF:
4323       return COSTS_N_INSNS (3);
4324
4325     case UDIV:
4326     case UMOD:
4327     case DIV:
4328     case MOD:
4329       return 100;
4330
4331     case TRUNCATE:
4332       return 99;
4333
4334     case AND:
4335     case XOR:
4336     case IOR:
4337       /* XXX guess.  */
4338       return 8;
4339
4340     case MEM:
4341       /* XXX another guess.  */
4342       /* Memory costs quite a lot for the first word, but subsequent words
4343          load at the equivalent of a single insn each.  */
4344       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4345               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
4346                  ? 4 : 0));
4347
4348     case IF_THEN_ELSE:
4349       /* XXX a guess.  */
4350       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4351         return 14;
4352       return 2;
4353
4354     case ZERO_EXTEND:
4355       /* XXX still guessing.  */
4356       switch (GET_MODE (XEXP (x, 0)))
4357         {
4358         case QImode:
4359           return (1 + (mode == DImode ? 4 : 0)
4360                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4361
4362         case HImode:
4363           return (4 + (mode == DImode ? 4 : 0)
4364                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4365
4366         case SImode:
4367           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4368
4369         default:
4370           return 99;
4371         }
4372
4373     default:
4374       return 99;
4375     }
4376 }
4377
4378
4379 /* Worker routine for arm_rtx_costs.  */
4380 static inline int
4381 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
4382 {
4383   enum machine_mode mode = GET_MODE (x);
4384   enum rtx_code subcode;
4385   int extra_cost;
4386
4387   switch (code)
4388     {
4389     case MEM:
4390       /* Memory costs quite a lot for the first word, but subsequent words
4391          load at the equivalent of a single insn each.  */
4392       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
4393               + (GET_CODE (x) == SYMBOL_REF
4394                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
4395
4396     case DIV:
4397     case MOD:
4398     case UDIV:
4399     case UMOD:
4400       return optimize_size ? COSTS_N_INSNS (2) : 100;
4401
4402     case ROTATE:
4403       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4404         return 4;
4405       /* Fall through */
4406     case ROTATERT:
4407       if (mode != SImode)
4408         return 8;
4409       /* Fall through */
4410     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
4411       if (mode == DImode)
4412         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
4413                 + ((GET_CODE (XEXP (x, 0)) == REG
4414                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4415                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4416                    ? 0 : 8));
4417       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
4418                     || (GET_CODE (XEXP (x, 0)) == SUBREG
4419                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
4420                    ? 0 : 4)
4421               + ((GET_CODE (XEXP (x, 1)) == REG
4422                   || (GET_CODE (XEXP (x, 1)) == SUBREG
4423                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
4424                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
4425                  ? 0 : 4));
4426
4427     case MINUS:
4428       if (mode == DImode)
4429         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
4430                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4431                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
4432                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
4433                    ? 0 : 8));
4434
4435       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4436         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4437                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4438                           && arm_const_double_rtx (XEXP (x, 1))))
4439                      ? 0 : 8)
4440                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
4441                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
4442                         && arm_const_double_rtx (XEXP (x, 0))))
4443                    ? 0 : 8));
4444
4445       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
4446             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
4447             && REG_OR_SUBREG_REG (XEXP (x, 1))))
4448           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
4449                || subcode == ASHIFTRT || subcode == LSHIFTRT
4450                || subcode == ROTATE || subcode == ROTATERT
4451                || (subcode == MULT
4452                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4453                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
4454                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
4455               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
4456               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
4457                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
4458               && REG_OR_SUBREG_REG (XEXP (x, 0))))
4459         return 1;
4460       /* Fall through */
4461
4462     case PLUS:
4463       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4464         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4465                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4466                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
4467                         && arm_const_double_rtx (XEXP (x, 1))))
4468                    ? 0 : 8));
4469
4470       /* Fall through */
4471     case AND: case XOR: case IOR:
4472       extra_cost = 0;
4473
4474       /* Normally the frame registers will be spilt into reg+const during
4475          reload, so it is a bad idea to combine them with other instructions,
4476          since then they might not be moved outside of loops.  As a compromise
4477          we allow integration with ops that have a constant as their second
4478          operand.  */
4479       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
4480            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
4481            && GET_CODE (XEXP (x, 1)) != CONST_INT)
4482           || (REG_OR_SUBREG_REG (XEXP (x, 0))
4483               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
4484         extra_cost = 4;
4485
4486       if (mode == DImode)
4487         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
4488                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4489                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4490                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4491                    ? 0 : 8));
4492
4493       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
4494         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
4495                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
4496                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
4497                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
4498                    ? 0 : 4));
4499
4500       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
4501         return (1 + extra_cost
4502                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
4503                      || subcode == LSHIFTRT || subcode == ASHIFTRT
4504                      || subcode == ROTATE || subcode == ROTATERT
4505                      || (subcode == MULT
4506                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4507                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
4508                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
4509                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
4510                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
4511                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
4512                    ? 0 : 4));
4513
4514       return 8;
4515
4516     case MULT:
4517       /* This should have been handled by the CPU specific routines.  */
4518       gcc_unreachable ();
4519
4520     case TRUNCATE:
4521       if (arm_arch3m && mode == SImode
4522           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
4523           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
4524           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
4525               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
4526           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
4527               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
4528         return 8;
4529       return 99;
4530
4531     case NEG:
4532       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4533         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
4534       /* Fall through */
4535     case NOT:
4536       if (mode == DImode)
4537         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4538
4539       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
4540
4541     case IF_THEN_ELSE:
4542       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
4543         return 14;
4544       return 2;
4545
4546     case COMPARE:
4547       return 1;
4548
4549     case ABS:
4550       return 4 + (mode == DImode ? 4 : 0);
4551
4552     case SIGN_EXTEND:
4553       if (GET_MODE (XEXP (x, 0)) == QImode)
4554         return (4 + (mode == DImode ? 4 : 0)
4555                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4556       /* Fall through */
4557     case ZERO_EXTEND:
4558       switch (GET_MODE (XEXP (x, 0)))
4559         {
4560         case QImode:
4561           return (1 + (mode == DImode ? 4 : 0)
4562                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4563
4564         case HImode:
4565           return (4 + (mode == DImode ? 4 : 0)
4566                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4567
4568         case SImode:
4569           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
4570
4571         case V8QImode:
4572         case V4HImode:
4573         case V2SImode:
4574         case V4QImode:
4575         case V2HImode:
4576             return 1;
4577
4578         default:
4579           gcc_unreachable ();
4580         }
4581       gcc_unreachable ();
4582
4583     case CONST_INT:
4584       if (const_ok_for_arm (INTVAL (x)))
4585         return outer == SET ? 2 : -1;
4586       else if (outer == AND
4587                && const_ok_for_arm (~INTVAL (x)))
4588         return -1;
4589       else if ((outer == COMPARE
4590                 || outer == PLUS || outer == MINUS)
4591                && const_ok_for_arm (-INTVAL (x)))
4592         return -1;
4593       else
4594         return 5;
4595
4596     case CONST:
4597     case LABEL_REF:
4598     case SYMBOL_REF:
4599       return 6;
4600
4601     case CONST_DOUBLE:
4602       if (arm_const_double_rtx (x))
4603         return outer == SET ? 2 : -1;
4604       else if ((outer == COMPARE || outer == PLUS)
4605                && neg_const_double_rtx_ok_for_fpa (x))
4606         return -1;
4607       return 7;
4608
4609     default:
4610       return 99;
4611     }
4612 }
4613
4614 /* RTX costs when optimizing for size.  */
4615 static bool
4616 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4617 {
4618   enum machine_mode mode = GET_MODE (x);
4619
4620   if (TARGET_THUMB)
4621     {
4622       /* XXX TBD.  For now, use the standard costs.  */
4623       *total = thumb_rtx_costs (x, code, outer_code);
4624       return true;
4625     }
4626
4627   switch (code)
4628     {
4629     case MEM:
4630       /* A memory access costs 1 insn if the mode is small, or the address is
4631          a single register, otherwise it costs one insn per word.  */
4632       if (REG_P (XEXP (x, 0)))
4633         *total = COSTS_N_INSNS (1);
4634       else
4635         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4636       return true;
4637
4638     case DIV:
4639     case MOD:
4640     case UDIV:
4641     case UMOD:
4642       /* Needs a libcall, so it costs about this.  */
4643       *total = COSTS_N_INSNS (2);
4644       return false;
4645
4646     case ROTATE:
4647       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4648         {
4649           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4650           return true;
4651         }
4652       /* Fall through */
4653     case ROTATERT:
4654     case ASHIFT:
4655     case LSHIFTRT:
4656     case ASHIFTRT:
4657       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4658         {
4659           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4660           return true;
4661         }
4662       else if (mode == SImode)
4663         {
4664           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4665           /* Slightly disparage register shifts, but not by much.  */
4666           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4667             *total += 1 + rtx_cost (XEXP (x, 1), code);
4668           return true;
4669         }
4670
4671       /* Needs a libcall.  */
4672       *total = COSTS_N_INSNS (2);
4673       return false;
4674
4675     case MINUS:
4676       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4677         {
4678           *total = COSTS_N_INSNS (1);
4679           return false;
4680         }
4681
4682       if (mode == SImode)
4683         {
4684           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4685           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4686
4687           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4688               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4689               || subcode1 == ROTATE || subcode1 == ROTATERT
4690               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4691               || subcode1 == ASHIFTRT)
4692             {
4693               /* It's just the cost of the two operands.  */
4694               *total = 0;
4695               return false;
4696             }
4697
4698           *total = COSTS_N_INSNS (1);
4699           return false;
4700         }
4701
4702       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4703       return false;
4704
4705     case PLUS:
4706       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4707         {
4708           *total = COSTS_N_INSNS (1);
4709           return false;
4710         }
4711
4712       /* Fall through */
4713     case AND: case XOR: case IOR:
4714       if (mode == SImode)
4715         {
4716           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4717
4718           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4719               || subcode == LSHIFTRT || subcode == ASHIFTRT
4720               || (code == AND && subcode == NOT))
4721             {
4722               /* It's just the cost of the two operands.  */
4723               *total = 0;
4724               return false;
4725             }
4726         }
4727
4728       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4729       return false;
4730
4731     case MULT:
4732       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4733       return false;
4734
4735     case NEG:
4736       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4737         *total = COSTS_N_INSNS (1);
4738       /* Fall through */
4739     case NOT:
4740       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4741
4742       return false;
4743
4744     case IF_THEN_ELSE:
4745       *total = 0;
4746       return false;
4747
4748     case COMPARE:
4749       if (cc_register (XEXP (x, 0), VOIDmode))
4750         * total = 0;
4751       else
4752         *total = COSTS_N_INSNS (1);
4753       return false;
4754
4755     case ABS:
4756       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4757         *total = COSTS_N_INSNS (1);
4758       else
4759         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4760       return false;
4761
4762     case SIGN_EXTEND:
4763       *total = 0;
4764       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4765         {
4766           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4767             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4768         }
4769       if (mode == DImode)
4770         *total += COSTS_N_INSNS (1);
4771       return false;
4772
4773     case ZERO_EXTEND:
4774       *total = 0;
4775       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4776         {
4777           switch (GET_MODE (XEXP (x, 0)))
4778             {
4779             case QImode:
4780               *total += COSTS_N_INSNS (1);
4781               break;
4782
4783             case HImode:
4784               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4785
4786             case SImode:
4787               break;
4788
4789             default:
4790               *total += COSTS_N_INSNS (2);
4791             }
4792         }
4793
4794       if (mode == DImode)
4795         *total += COSTS_N_INSNS (1);
4796
4797       return false;
4798
4799     case CONST_INT:
4800       if (const_ok_for_arm (INTVAL (x)))
4801         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4802       else if (const_ok_for_arm (~INTVAL (x)))
4803         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4804       else if (const_ok_for_arm (-INTVAL (x)))
4805         {
4806           if (outer_code == COMPARE || outer_code == PLUS
4807               || outer_code == MINUS)
4808             *total = 0;
4809           else
4810             *total = COSTS_N_INSNS (1);
4811         }
4812       else
4813         *total = COSTS_N_INSNS (2);
4814       return true;
4815
4816     case CONST:
4817     case LABEL_REF:
4818     case SYMBOL_REF:
4819       *total = COSTS_N_INSNS (2);
4820       return true;
4821
4822     case CONST_DOUBLE:
4823       *total = COSTS_N_INSNS (4);
4824       return true;
4825
4826     default:
4827       if (mode != VOIDmode)
4828         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4829       else
4830         *total = COSTS_N_INSNS (4); /* How knows?  */
4831       return false;
4832     }
4833 }
4834
4835 /* RTX costs for cores with a slow MUL implementation.  */
4836
4837 static bool
4838 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4839 {
4840   enum machine_mode mode = GET_MODE (x);
4841
4842   if (TARGET_THUMB)
4843     {
4844       *total = thumb_rtx_costs (x, code, outer_code);
4845       return true;
4846     }
4847
4848   switch (code)
4849     {
4850     case MULT:
4851       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4852           || mode == DImode)
4853         {
4854           *total = 30;
4855           return true;
4856         }
4857
4858       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4859         {
4860           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4861                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4862           int cost, const_ok = const_ok_for_arm (i);
4863           int j, booth_unit_size;
4864
4865           /* Tune as appropriate.  */
4866           cost = const_ok ? 4 : 8;
4867           booth_unit_size = 2;
4868           for (j = 0; i && j < 32; j += booth_unit_size)
4869             {
4870               i >>= booth_unit_size;
4871               cost += 2;
4872             }
4873
4874           *total = cost;
4875           return true;
4876         }
4877
4878       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4879                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4880       return true;
4881
4882     default:
4883       *total = arm_rtx_costs_1 (x, code, outer_code);
4884       return true;
4885     }
4886 }
4887
4888
4889 /* RTX cost for cores with a fast multiply unit (M variants).  */
4890
4891 static bool
4892 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4893 {
4894   enum machine_mode mode = GET_MODE (x);
4895
4896   if (TARGET_THUMB)
4897     {
4898       *total = thumb_rtx_costs (x, code, outer_code);
4899       return true;
4900     }
4901
4902   switch (code)
4903     {
4904     case MULT:
4905       /* There is no point basing this on the tuning, since it is always the
4906          fast variant if it exists at all.  */
4907       if (mode == DImode
4908           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4909           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4910               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4911         {
4912           *total = 8;
4913           return true;
4914         }
4915
4916
4917       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4918           || mode == DImode)
4919         {
4920           *total = 30;
4921           return true;
4922         }
4923
4924       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4925         {
4926           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4927                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4928           int cost, const_ok = const_ok_for_arm (i);
4929           int j, booth_unit_size;
4930
4931           /* Tune as appropriate.  */
4932           cost = const_ok ? 4 : 8;
4933           booth_unit_size = 8;
4934           for (j = 0; i && j < 32; j += booth_unit_size)
4935             {
4936               i >>= booth_unit_size;
4937               cost += 2;
4938             }
4939
4940           *total = cost;
4941           return true;
4942         }
4943
4944       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4945                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4946       return true;
4947
4948     default:
4949       *total = arm_rtx_costs_1 (x, code, outer_code);
4950       return true;
4951     }
4952 }
4953
4954
4955 /* RTX cost for XScale CPUs.  */
4956
4957 static bool
4958 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4959 {
4960   enum machine_mode mode = GET_MODE (x);
4961
4962   if (TARGET_THUMB)
4963     {
4964       *total = thumb_rtx_costs (x, code, outer_code);
4965       return true;
4966     }
4967
4968   switch (code)
4969     {
4970     case MULT:
4971       /* There is no point basing this on the tuning, since it is always the
4972          fast variant if it exists at all.  */
4973       if (mode == DImode
4974           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4975           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4976               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4977         {
4978           *total = 8;
4979           return true;
4980         }
4981
4982
4983       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4984           || mode == DImode)
4985         {
4986           *total = 30;
4987           return true;
4988         }
4989
4990       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4991         {
4992           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4993                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4994           int cost, const_ok = const_ok_for_arm (i);
4995           unsigned HOST_WIDE_INT masked_const;
4996
4997           /* The cost will be related to two insns.
4998              First a load of the constant (MOV or LDR), then a multiply.  */
4999           cost = 2;
5000           if (! const_ok)
5001             cost += 1;      /* LDR is probably more expensive because
5002                                of longer result latency.  */
5003           masked_const = i & 0xffff8000;
5004           if (masked_const != 0 && masked_const != 0xffff8000)
5005             {
5006               masked_const = i & 0xf8000000;
5007               if (masked_const == 0 || masked_const == 0xf8000000)
5008                 cost += 1;
5009               else
5010                 cost += 2;
5011             }
5012           *total = cost;
5013           return true;
5014         }
5015
5016       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
5017                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
5018       return true;
5019
5020     case COMPARE:
5021       /* A COMPARE of a MULT is slow on XScale; the muls instruction
5022          will stall until the multiplication is complete.  */
5023       if (GET_CODE (XEXP (x, 0)) == MULT)
5024         *total = 4 + rtx_cost (XEXP (x, 0), code);
5025       else
5026         *total = arm_rtx_costs_1 (x, code, outer_code);
5027       return true;
5028
5029     default:
5030       *total = arm_rtx_costs_1 (x, code, outer_code);
5031       return true;
5032     }
5033 }
5034
5035
5036 /* RTX costs for 9e (and later) cores.  */
5037
5038 static bool
5039 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
5040 {
5041   enum machine_mode mode = GET_MODE (x);
5042   int nonreg_cost;
5043   int cost;
5044
5045   if (TARGET_THUMB)
5046     {
5047       switch (code)
5048         {
5049         case MULT:
5050           *total = COSTS_N_INSNS (3);
5051           return true;
5052
5053         default:
5054           *total = thumb_rtx_costs (x, code, outer_code);
5055           return true;
5056         }
5057     }
5058
5059   switch (code)
5060     {
5061     case MULT:
5062       /* There is no point basing this on the tuning, since it is always the
5063          fast variant if it exists at all.  */
5064       if (mode == DImode
5065           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
5066           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5067               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
5068         {
5069           *total = 3;
5070           return true;
5071         }
5072
5073
5074       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5075         {
5076           *total = 30;
5077           return true;
5078         }
5079       if (mode == DImode)
5080         {
5081           cost = 7;
5082           nonreg_cost = 8;
5083         }
5084       else
5085         {
5086           cost = 2;
5087           nonreg_cost = 4;
5088         }
5089
5090
5091       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
5092                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
5093       return true;
5094
5095     default:
5096       *total = arm_rtx_costs_1 (x, code, outer_code);
5097       return true;
5098     }
5099 }
5100 /* All address computations that can be done are free, but rtx cost returns
5101    the same for practically all of them.  So we weight the different types
5102    of address here in the order (most pref first):
5103    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
5104 static inline int
5105 arm_arm_address_cost (rtx x)
5106 {
5107   enum rtx_code c  = GET_CODE (x);
5108
5109   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
5110     return 0;
5111   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
5112     return 10;
5113
5114   if (c == PLUS || c == MINUS)
5115     {
5116       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5117         return 2;
5118
5119       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
5120         return 3;
5121
5122       return 4;
5123     }
5124
5125   return 6;
5126 }
5127
5128 static inline int
5129 arm_thumb_address_cost (rtx x)
5130 {
5131   enum rtx_code c  = GET_CODE (x);
5132
5133   if (c == REG)
5134     return 1;
5135   if (c == PLUS
5136       && GET_CODE (XEXP (x, 0)) == REG
5137       && GET_CODE (XEXP (x, 1)) == CONST_INT)
5138     return 1;
5139
5140   return 2;
5141 }
5142
5143 static int
5144 arm_address_cost (rtx x)
5145 {
5146   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
5147 }
5148
5149 static int
5150 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
5151 {
5152   rtx i_pat, d_pat;
5153
5154   /* Some true dependencies can have a higher cost depending
5155      on precisely how certain input operands are used.  */
5156   if (arm_tune_xscale
5157       && REG_NOTE_KIND (link) == 0
5158       && recog_memoized (insn) >= 0
5159       && recog_memoized (dep) >= 0)
5160     {
5161       int shift_opnum = get_attr_shift (insn);
5162       enum attr_type attr_type = get_attr_type (dep);
5163
5164       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
5165          operand for INSN.  If we have a shifted input operand and the
5166          instruction we depend on is another ALU instruction, then we may
5167          have to account for an additional stall.  */
5168       if (shift_opnum != 0
5169           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
5170         {
5171           rtx shifted_operand;
5172           int opno;
5173
5174           /* Get the shifted operand.  */
5175           extract_insn (insn);
5176           shifted_operand = recog_data.operand[shift_opnum];
5177
5178           /* Iterate over all the operands in DEP.  If we write an operand
5179              that overlaps with SHIFTED_OPERAND, then we have increase the
5180              cost of this dependency.  */
5181           extract_insn (dep);
5182           preprocess_constraints ();
5183           for (opno = 0; opno < recog_data.n_operands; opno++)
5184             {
5185               /* We can ignore strict inputs.  */
5186               if (recog_data.operand_type[opno] == OP_IN)
5187                 continue;
5188
5189               if (reg_overlap_mentioned_p (recog_data.operand[opno],
5190                                            shifted_operand))
5191                 return 2;
5192             }
5193         }
5194     }
5195
5196   /* XXX This is not strictly true for the FPA.  */
5197   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
5198       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
5199     return 0;
5200
5201   /* Call insns don't incur a stall, even if they follow a load.  */
5202   if (REG_NOTE_KIND (link) == 0
5203       && GET_CODE (insn) == CALL_INSN)
5204     return 1;
5205
5206   if ((i_pat = single_set (insn)) != NULL
5207       && GET_CODE (SET_SRC (i_pat)) == MEM
5208       && (d_pat = single_set (dep)) != NULL
5209       && GET_CODE (SET_DEST (d_pat)) == MEM)
5210     {
5211       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
5212       /* This is a load after a store, there is no conflict if the load reads
5213          from a cached area.  Assume that loads from the stack, and from the
5214          constant pool are cached, and that others will miss.  This is a
5215          hack.  */
5216
5217       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
5218           || reg_mentioned_p (stack_pointer_rtx, src_mem)
5219           || reg_mentioned_p (frame_pointer_rtx, src_mem)
5220           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
5221         return 1;
5222     }
5223
5224   return cost;
5225 }
5226
5227 static int fp_consts_inited = 0;
5228
5229 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
5230 static const char * const strings_fp[8] =
5231 {
5232   "0",   "1",   "2",   "3",
5233   "4",   "5",   "0.5", "10"
5234 };
5235
5236 static REAL_VALUE_TYPE values_fp[8];
5237
5238 static void
5239 init_fp_table (void)
5240 {
5241   int i;
5242   REAL_VALUE_TYPE r;
5243
5244   if (TARGET_VFP)
5245     fp_consts_inited = 1;
5246   else
5247     fp_consts_inited = 8;
5248
5249   for (i = 0; i < fp_consts_inited; i++)
5250     {
5251       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
5252       values_fp[i] = r;
5253     }
5254 }
5255
5256 /* Return TRUE if rtx X is a valid immediate FP constant.  */
5257 int
5258 arm_const_double_rtx (rtx x)
5259 {
5260   REAL_VALUE_TYPE r;
5261   int i;
5262
5263   if (!fp_consts_inited)
5264     init_fp_table ();
5265
5266   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5267   if (REAL_VALUE_MINUS_ZERO (r))
5268     return 0;
5269
5270   for (i = 0; i < fp_consts_inited; i++)
5271     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5272       return 1;
5273
5274   return 0;
5275 }
5276
5277 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
5278 int
5279 neg_const_double_rtx_ok_for_fpa (rtx x)
5280 {
5281   REAL_VALUE_TYPE r;
5282   int i;
5283
5284   if (!fp_consts_inited)
5285     init_fp_table ();
5286
5287   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5288   r = REAL_VALUE_NEGATE (r);
5289   if (REAL_VALUE_MINUS_ZERO (r))
5290     return 0;
5291
5292   for (i = 0; i < 8; i++)
5293     if (REAL_VALUES_EQUAL (r, values_fp[i]))
5294       return 1;
5295
5296   return 0;
5297 }
5298 \f
5299 /* Predicates for `match_operand' and `match_operator'.  */
5300
5301 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
5302 int
5303 cirrus_memory_offset (rtx op)
5304 {
5305   /* Reject eliminable registers.  */
5306   if (! (reload_in_progress || reload_completed)
5307       && (   reg_mentioned_p (frame_pointer_rtx, op)
5308           || reg_mentioned_p (arg_pointer_rtx, op)
5309           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5310           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5311           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5312           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5313     return 0;
5314
5315   if (GET_CODE (op) == MEM)
5316     {
5317       rtx ind;
5318
5319       ind = XEXP (op, 0);
5320
5321       /* Match: (mem (reg)).  */
5322       if (GET_CODE (ind) == REG)
5323         return 1;
5324
5325       /* Match:
5326          (mem (plus (reg)
5327                     (const))).  */
5328       if (GET_CODE (ind) == PLUS
5329           && GET_CODE (XEXP (ind, 0)) == REG
5330           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5331           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
5332         return 1;
5333     }
5334
5335   return 0;
5336 }
5337
5338 /* Return TRUE if OP is a valid coprocessor memory address pattern.
5339    WB if true if writeback address modes are allowed.  */
5340
5341 int
5342 arm_coproc_mem_operand (rtx op, bool wb)
5343 {
5344   rtx ind;
5345
5346   /* Reject eliminable registers.  */
5347   if (! (reload_in_progress || reload_completed)
5348       && (   reg_mentioned_p (frame_pointer_rtx, op)
5349           || reg_mentioned_p (arg_pointer_rtx, op)
5350           || reg_mentioned_p (virtual_incoming_args_rtx, op)
5351           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
5352           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
5353           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
5354     return FALSE;
5355
5356   /* Constants are converted into offsets from labels.  */
5357   if (GET_CODE (op) != MEM)
5358     return FALSE;
5359
5360   ind = XEXP (op, 0);
5361
5362   if (reload_completed
5363       && (GET_CODE (ind) == LABEL_REF
5364           || (GET_CODE (ind) == CONST
5365               && GET_CODE (XEXP (ind, 0)) == PLUS
5366               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
5367               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
5368     return TRUE;
5369
5370   /* Match: (mem (reg)).  */
5371   if (GET_CODE (ind) == REG)
5372     return arm_address_register_rtx_p (ind, 0);
5373
5374   /* Autoincremment addressing modes.  */
5375   if (wb
5376       && (GET_CODE (ind) == PRE_INC
5377           || GET_CODE (ind) == POST_INC
5378           || GET_CODE (ind) == PRE_DEC
5379           || GET_CODE (ind) == POST_DEC))
5380     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
5381
5382   if (wb
5383       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
5384       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
5385       && GET_CODE (XEXP (ind, 1)) == PLUS
5386       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
5387     ind = XEXP (ind, 1);
5388
5389   /* Match:
5390      (plus (reg)
5391            (const)).  */
5392   if (GET_CODE (ind) == PLUS
5393       && GET_CODE (XEXP (ind, 0)) == REG
5394       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
5395       && GET_CODE (XEXP (ind, 1)) == CONST_INT
5396       && INTVAL (XEXP (ind, 1)) > -1024
5397       && INTVAL (XEXP (ind, 1)) <  1024
5398       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
5399     return TRUE;
5400
5401   return FALSE;
5402 }
5403
5404 /* Return true if X is a register that will be eliminated later on.  */
5405 int
5406 arm_eliminable_register (rtx x)
5407 {
5408   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
5409                        || REGNO (x) == ARG_POINTER_REGNUM
5410                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
5411                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
5412 }
5413
5414 /* Return GENERAL_REGS if a scratch register required to reload x to/from
5415    VFP registers.  Otherwise return NO_REGS.  */
5416
5417 enum reg_class
5418 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
5419 {
5420   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
5421     return NO_REGS;
5422
5423   return GENERAL_REGS;
5424 }
5425
5426 /* Values which must be returned in the most-significant end of the return
5427    register.  */
5428
5429 static bool
5430 arm_return_in_msb (tree valtype)
5431 {
5432   return (TARGET_AAPCS_BASED
5433           && BYTES_BIG_ENDIAN
5434           && (AGGREGATE_TYPE_P (valtype)
5435               || TREE_CODE (valtype) == COMPLEX_TYPE));
5436 }
5437
5438 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
5439    Use by the Cirrus Maverick code which has to workaround
5440    a hardware bug triggered by such instructions.  */
5441 static bool
5442 arm_memory_load_p (rtx insn)
5443 {
5444   rtx body, lhs, rhs;;
5445
5446   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
5447     return false;
5448
5449   body = PATTERN (insn);
5450
5451   if (GET_CODE (body) != SET)
5452     return false;
5453
5454   lhs = XEXP (body, 0);
5455   rhs = XEXP (body, 1);
5456
5457   lhs = REG_OR_SUBREG_RTX (lhs);
5458
5459   /* If the destination is not a general purpose
5460      register we do not have to worry.  */
5461   if (GET_CODE (lhs) != REG
5462       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
5463     return false;
5464
5465   /* As well as loads from memory we also have to react
5466      to loads of invalid constants which will be turned
5467      into loads from the minipool.  */
5468   return (GET_CODE (rhs) == MEM
5469           || GET_CODE (rhs) == SYMBOL_REF
5470           || note_invalid_constants (insn, -1, false));
5471 }
5472
5473 /* Return TRUE if INSN is a Cirrus instruction.  */
5474 static bool
5475 arm_cirrus_insn_p (rtx insn)
5476 {
5477   enum attr_cirrus attr;
5478
5479   /* get_attr cannot accept USE or CLOBBER.  */
5480   if (!insn
5481       || GET_CODE (insn) != INSN
5482       || GET_CODE (PATTERN (insn)) == USE
5483       || GET_CODE (PATTERN (insn)) == CLOBBER)
5484     return 0;
5485
5486   attr = get_attr_cirrus (insn);
5487
5488   return attr != CIRRUS_NOT;
5489 }
5490
5491 /* Cirrus reorg for invalid instruction combinations.  */
5492 static void
5493 cirrus_reorg (rtx first)
5494 {
5495   enum attr_cirrus attr;
5496   rtx body = PATTERN (first);
5497   rtx t;
5498   int nops;
5499
5500   /* Any branch must be followed by 2 non Cirrus instructions.  */
5501   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
5502     {
5503       nops = 0;
5504       t = next_nonnote_insn (first);
5505
5506       if (arm_cirrus_insn_p (t))
5507         ++ nops;
5508
5509       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5510         ++ nops;
5511
5512       while (nops --)
5513         emit_insn_after (gen_nop (), first);
5514
5515       return;
5516     }
5517
5518   /* (float (blah)) is in parallel with a clobber.  */
5519   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
5520     body = XVECEXP (body, 0, 0);
5521
5522   if (GET_CODE (body) == SET)
5523     {
5524       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
5525
5526       /* cfldrd, cfldr64, cfstrd, cfstr64 must
5527          be followed by a non Cirrus insn.  */
5528       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
5529         {
5530           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
5531             emit_insn_after (gen_nop (), first);
5532
5533           return;
5534         }
5535       else if (arm_memory_load_p (first))
5536         {
5537           unsigned int arm_regno;
5538
5539           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
5540              ldr/cfmv64hr combination where the Rd field is the same
5541              in both instructions must be split with a non Cirrus
5542              insn.  Example:
5543
5544              ldr r0, blah
5545              nop
5546              cfmvsr mvf0, r0.  */
5547
5548           /* Get Arm register number for ldr insn.  */
5549           if (GET_CODE (lhs) == REG)
5550             arm_regno = REGNO (lhs);
5551           else
5552             {
5553               gcc_assert (GET_CODE (rhs) == REG);
5554               arm_regno = REGNO (rhs);
5555             }
5556
5557           /* Next insn.  */
5558           first = next_nonnote_insn (first);
5559
5560           if (! arm_cirrus_insn_p (first))
5561             return;
5562
5563           body = PATTERN (first);
5564
5565           /* (float (blah)) is in parallel with a clobber.  */
5566           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
5567             body = XVECEXP (body, 0, 0);
5568
5569           if (GET_CODE (body) == FLOAT)
5570             body = XEXP (body, 0);
5571
5572           if (get_attr_cirrus (first) == CIRRUS_MOVE
5573               && GET_CODE (XEXP (body, 1)) == REG
5574               && arm_regno == REGNO (XEXP (body, 1)))
5575             emit_insn_after (gen_nop (), first);
5576
5577           return;
5578         }
5579     }
5580
5581   /* get_attr cannot accept USE or CLOBBER.  */
5582   if (!first
5583       || GET_CODE (first) != INSN
5584       || GET_CODE (PATTERN (first)) == USE
5585       || GET_CODE (PATTERN (first)) == CLOBBER)
5586     return;
5587
5588   attr = get_attr_cirrus (first);
5589
5590   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
5591      must be followed by a non-coprocessor instruction.  */
5592   if (attr == CIRRUS_COMPARE)
5593     {
5594       nops = 0;
5595
5596       t = next_nonnote_insn (first);
5597
5598       if (arm_cirrus_insn_p (t))
5599         ++ nops;
5600
5601       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5602         ++ nops;
5603
5604       while (nops --)
5605         emit_insn_after (gen_nop (), first);
5606
5607       return;
5608     }
5609 }
5610
5611 /* Return TRUE if X references a SYMBOL_REF.  */
5612 int
5613 symbol_mentioned_p (rtx x)
5614 {
5615   const char * fmt;
5616   int i;
5617
5618   if (GET_CODE (x) == SYMBOL_REF)
5619     return 1;
5620
5621   /* UNSPEC_TLS entries for a symbol include the SYMBOL_REF, but they
5622      are constant offsets, not symbols.  */
5623   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5624     return 0;
5625
5626   fmt = GET_RTX_FORMAT (GET_CODE (x));
5627
5628   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5629     {
5630       if (fmt[i] == 'E')
5631         {
5632           int j;
5633
5634           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5635             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5636               return 1;
5637         }
5638       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5639         return 1;
5640     }
5641
5642   return 0;
5643 }
5644
5645 /* Return TRUE if X references a LABEL_REF.  */
5646 int
5647 label_mentioned_p (rtx x)
5648 {
5649   const char * fmt;
5650   int i;
5651
5652   if (GET_CODE (x) == LABEL_REF)
5653     return 1;
5654
5655   /* UNSPEC_TLS entries for a symbol include a LABEL_REF for the referencing
5656      instruction, but they are constant offsets, not symbols.  */
5657   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
5658     return 0;
5659
5660   fmt = GET_RTX_FORMAT (GET_CODE (x));
5661   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5662     {
5663       if (fmt[i] == 'E')
5664         {
5665           int j;
5666
5667           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5668             if (label_mentioned_p (XVECEXP (x, i, j)))
5669               return 1;
5670         }
5671       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5672         return 1;
5673     }
5674
5675   return 0;
5676 }
5677
5678 int
5679 tls_mentioned_p (rtx x)
5680 {
5681   switch (GET_CODE (x))
5682     {
5683     case CONST:
5684       return tls_mentioned_p (XEXP (x, 0));
5685
5686     case UNSPEC:
5687       if (XINT (x, 1) == UNSPEC_TLS)
5688         return 1;
5689
5690     default:
5691       return 0;
5692     }
5693 }
5694
5695 /* Must not copy a SET whose source operand is PC-relative.  */
5696
5697 static bool
5698 arm_cannot_copy_insn_p (rtx insn)
5699 {
5700   rtx pat = PATTERN (insn);
5701
5702   if (GET_CODE (pat) == PARALLEL
5703       && GET_CODE (XVECEXP (pat, 0, 0)) == SET)
5704     {
5705       rtx rhs = SET_SRC (XVECEXP (pat, 0, 0));
5706
5707       if (GET_CODE (rhs) == UNSPEC
5708           && XINT (rhs, 1) == UNSPEC_PIC_BASE)
5709         return TRUE;
5710
5711       if (GET_CODE (rhs) == MEM
5712           && GET_CODE (XEXP (rhs, 0)) == UNSPEC
5713           && XINT (XEXP (rhs, 0), 1) == UNSPEC_PIC_BASE)
5714         return TRUE;
5715     }
5716
5717   return FALSE;
5718 }
5719
5720 enum rtx_code
5721 minmax_code (rtx x)
5722 {
5723   enum rtx_code code = GET_CODE (x);
5724
5725   switch (code)
5726     {
5727     case SMAX:
5728       return GE;
5729     case SMIN:
5730       return LE;
5731     case UMIN:
5732       return LEU;
5733     case UMAX:
5734       return GEU;
5735     default:
5736       gcc_unreachable ();
5737     }
5738 }
5739
5740 /* Return 1 if memory locations are adjacent.  */
5741 int
5742 adjacent_mem_locations (rtx a, rtx b)
5743 {
5744   /* We don't guarantee to preserve the order of these memory refs.  */
5745   if (volatile_refs_p (a) || volatile_refs_p (b))
5746     return 0;
5747
5748   if ((GET_CODE (XEXP (a, 0)) == REG
5749        || (GET_CODE (XEXP (a, 0)) == PLUS
5750            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5751       && (GET_CODE (XEXP (b, 0)) == REG
5752           || (GET_CODE (XEXP (b, 0)) == PLUS
5753               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5754     {
5755       HOST_WIDE_INT val0 = 0, val1 = 0;
5756       rtx reg0, reg1;
5757       int val_diff;
5758
5759       if (GET_CODE (XEXP (a, 0)) == PLUS)
5760         {
5761           reg0 = XEXP (XEXP (a, 0), 0);
5762           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5763         }
5764       else
5765         reg0 = XEXP (a, 0);
5766
5767       if (GET_CODE (XEXP (b, 0)) == PLUS)
5768         {
5769           reg1 = XEXP (XEXP (b, 0), 0);
5770           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5771         }
5772       else
5773         reg1 = XEXP (b, 0);
5774
5775       /* Don't accept any offset that will require multiple
5776          instructions to handle, since this would cause the
5777          arith_adjacentmem pattern to output an overlong sequence.  */
5778       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5779         return 0;
5780
5781       /* Don't allow an eliminable register: register elimination can make
5782          the offset too large.  */
5783       if (arm_eliminable_register (reg0))
5784         return 0;
5785
5786       val_diff = val1 - val0;
5787
5788       if (arm_ld_sched)
5789         {
5790           /* If the target has load delay slots, then there's no benefit
5791              to using an ldm instruction unless the offset is zero and
5792              we are optimizing for size.  */
5793           return (optimize_size && (REGNO (reg0) == REGNO (reg1))
5794                   && (val0 == 0 || val1 == 0 || val0 == 4 || val1 == 4)
5795                   && (val_diff == 4 || val_diff == -4));
5796         }
5797
5798       return ((REGNO (reg0) == REGNO (reg1))
5799               && (val_diff == 4 || val_diff == -4));
5800     }
5801
5802   return 0;
5803 }
5804
5805 int
5806 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5807                         HOST_WIDE_INT *load_offset)
5808 {
5809   int unsorted_regs[4];
5810   HOST_WIDE_INT unsorted_offsets[4];
5811   int order[4];
5812   int base_reg = -1;
5813   int i;
5814
5815   /* Can only handle 2, 3, or 4 insns at present,
5816      though could be easily extended if required.  */
5817   gcc_assert (nops >= 2 && nops <= 4);
5818
5819   /* Loop over the operands and check that the memory references are
5820      suitable (i.e. immediate offsets from the same base register).  At
5821      the same time, extract the target register, and the memory
5822      offsets.  */
5823   for (i = 0; i < nops; i++)
5824     {
5825       rtx reg;
5826       rtx offset;
5827
5828       /* Convert a subreg of a mem into the mem itself.  */
5829       if (GET_CODE (operands[nops + i]) == SUBREG)
5830         operands[nops + i] = alter_subreg (operands + (nops + i));
5831
5832       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
5833
5834       /* Don't reorder volatile memory references; it doesn't seem worth
5835          looking for the case where the order is ok anyway.  */
5836       if (MEM_VOLATILE_P (operands[nops + i]))
5837         return 0;
5838
5839       offset = const0_rtx;
5840
5841       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5842            || (GET_CODE (reg) == SUBREG
5843                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5844           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5845               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5846                    == REG)
5847                   || (GET_CODE (reg) == SUBREG
5848                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5849               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5850                   == CONST_INT)))
5851         {
5852           if (i == 0)
5853             {
5854               base_reg = REGNO (reg);
5855               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5856                                   ? REGNO (operands[i])
5857                                   : REGNO (SUBREG_REG (operands[i])));
5858               order[0] = 0;
5859             }
5860           else
5861             {
5862               if (base_reg != (int) REGNO (reg))
5863                 /* Not addressed from the same base register.  */
5864                 return 0;
5865
5866               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5867                                   ? REGNO (operands[i])
5868                                   : REGNO (SUBREG_REG (operands[i])));
5869               if (unsorted_regs[i] < unsorted_regs[order[0]])
5870                 order[0] = i;
5871             }
5872
5873           /* If it isn't an integer register, or if it overwrites the
5874              base register but isn't the last insn in the list, then
5875              we can't do this.  */
5876           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5877               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5878             return 0;
5879
5880           unsorted_offsets[i] = INTVAL (offset);
5881         }
5882       else
5883         /* Not a suitable memory address.  */
5884         return 0;
5885     }
5886
5887   /* All the useful information has now been extracted from the
5888      operands into unsorted_regs and unsorted_offsets; additionally,
5889      order[0] has been set to the lowest numbered register in the
5890      list.  Sort the registers into order, and check that the memory
5891      offsets are ascending and adjacent.  */
5892
5893   for (i = 1; i < nops; i++)
5894     {
5895       int j;
5896
5897       order[i] = order[i - 1];
5898       for (j = 0; j < nops; j++)
5899         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5900             && (order[i] == order[i - 1]
5901                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5902           order[i] = j;
5903
5904       /* Have we found a suitable register? if not, one must be used more
5905          than once.  */
5906       if (order[i] == order[i - 1])
5907         return 0;
5908
5909       /* Is the memory address adjacent and ascending? */
5910       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5911         return 0;
5912     }
5913
5914   if (base)
5915     {
5916       *base = base_reg;
5917
5918       for (i = 0; i < nops; i++)
5919         regs[i] = unsorted_regs[order[i]];
5920
5921       *load_offset = unsorted_offsets[order[0]];
5922     }
5923
5924   if (unsorted_offsets[order[0]] == 0)
5925     return 1; /* ldmia */
5926
5927   if (unsorted_offsets[order[0]] == 4)
5928     return 2; /* ldmib */
5929
5930   if (unsorted_offsets[order[nops - 1]] == 0)
5931     return 3; /* ldmda */
5932
5933   if (unsorted_offsets[order[nops - 1]] == -4)
5934     return 4; /* ldmdb */
5935
5936   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5937      if the offset isn't small enough.  The reason 2 ldrs are faster
5938      is because these ARMs are able to do more than one cache access
5939      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5940      whilst the ARM8 has a double bandwidth cache.  This means that
5941      these cores can do both an instruction fetch and a data fetch in
5942      a single cycle, so the trick of calculating the address into a
5943      scratch register (one of the result regs) and then doing a load
5944      multiple actually becomes slower (and no smaller in code size).
5945      That is the transformation
5946
5947         ldr     rd1, [rbase + offset]
5948         ldr     rd2, [rbase + offset + 4]
5949
5950      to
5951
5952         add     rd1, rbase, offset
5953         ldmia   rd1, {rd1, rd2}
5954
5955      produces worse code -- '3 cycles + any stalls on rd2' instead of
5956      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5957      access per cycle, the first sequence could never complete in less
5958      than 6 cycles, whereas the ldm sequence would only take 5 and
5959      would make better use of sequential accesses if not hitting the
5960      cache.
5961
5962      We cheat here and test 'arm_ld_sched' which we currently know to
5963      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5964      changes, then the test below needs to be reworked.  */
5965   if (nops == 2 && arm_ld_sched)
5966     return 0;
5967
5968   /* Can't do it without setting up the offset, only do this if it takes
5969      no more than one insn.  */
5970   return (const_ok_for_arm (unsorted_offsets[order[0]])
5971           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5972 }
5973
5974 const char *
5975 emit_ldm_seq (rtx *operands, int nops)
5976 {
5977   int regs[4];
5978   int base_reg;
5979   HOST_WIDE_INT offset;
5980   char buf[100];
5981   int i;
5982
5983   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5984     {
5985     case 1:
5986       strcpy (buf, "ldm%?ia\t");
5987       break;
5988
5989     case 2:
5990       strcpy (buf, "ldm%?ib\t");
5991       break;
5992
5993     case 3:
5994       strcpy (buf, "ldm%?da\t");
5995       break;
5996
5997     case 4:
5998       strcpy (buf, "ldm%?db\t");
5999       break;
6000
6001     case 5:
6002       if (offset >= 0)
6003         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6004                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6005                  (long) offset);
6006       else
6007         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
6008                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
6009                  (long) -offset);
6010       output_asm_insn (buf, operands);
6011       base_reg = regs[0];
6012       strcpy (buf, "ldm%?ia\t");
6013       break;
6014
6015     default:
6016       gcc_unreachable ();
6017     }
6018
6019   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6020            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6021
6022   for (i = 1; i < nops; i++)
6023     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6024              reg_names[regs[i]]);
6025
6026   strcat (buf, "}\t%@ phole ldm");
6027
6028   output_asm_insn (buf, operands);
6029   return "";
6030 }
6031
6032 int
6033 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
6034                          HOST_WIDE_INT * load_offset)
6035 {
6036   int unsorted_regs[4];
6037   HOST_WIDE_INT unsorted_offsets[4];
6038   int order[4];
6039   int base_reg = -1;
6040   int i;
6041
6042   /* Can only handle 2, 3, or 4 insns at present, though could be easily
6043      extended if required.  */
6044   gcc_assert (nops >= 2 && nops <= 4);
6045
6046   /* Loop over the operands and check that the memory references are
6047      suitable (i.e. immediate offsets from the same base register).  At
6048      the same time, extract the target register, and the memory
6049      offsets.  */
6050   for (i = 0; i < nops; i++)
6051     {
6052       rtx reg;
6053       rtx offset;
6054
6055       /* Convert a subreg of a mem into the mem itself.  */
6056       if (GET_CODE (operands[nops + i]) == SUBREG)
6057         operands[nops + i] = alter_subreg (operands + (nops + i));
6058
6059       gcc_assert (GET_CODE (operands[nops + i]) == MEM);
6060
6061       /* Don't reorder volatile memory references; it doesn't seem worth
6062          looking for the case where the order is ok anyway.  */
6063       if (MEM_VOLATILE_P (operands[nops + i]))
6064         return 0;
6065
6066       offset = const0_rtx;
6067
6068       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
6069            || (GET_CODE (reg) == SUBREG
6070                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6071           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
6072               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
6073                    == REG)
6074                   || (GET_CODE (reg) == SUBREG
6075                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
6076               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
6077                   == CONST_INT)))
6078         {
6079           if (i == 0)
6080             {
6081               base_reg = REGNO (reg);
6082               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
6083                                   ? REGNO (operands[i])
6084                                   : REGNO (SUBREG_REG (operands[i])));
6085               order[0] = 0;
6086             }
6087           else
6088             {
6089               if (base_reg != (int) REGNO (reg))
6090                 /* Not addressed from the same base register.  */
6091                 return 0;
6092
6093               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
6094                                   ? REGNO (operands[i])
6095                                   : REGNO (SUBREG_REG (operands[i])));
6096               if (unsorted_regs[i] < unsorted_regs[order[0]])
6097                 order[0] = i;
6098             }
6099
6100           /* If it isn't an integer register, then we can't do this.  */
6101           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
6102             return 0;
6103
6104           unsorted_offsets[i] = INTVAL (offset);
6105         }
6106       else
6107         /* Not a suitable memory address.  */
6108         return 0;
6109     }
6110
6111   /* All the useful information has now been extracted from the
6112      operands into unsorted_regs and unsorted_offsets; additionally,
6113      order[0] has been set to the lowest numbered register in the
6114      list.  Sort the registers into order, and check that the memory
6115      offsets are ascending and adjacent.  */
6116
6117   for (i = 1; i < nops; i++)
6118     {
6119       int j;
6120
6121       order[i] = order[i - 1];
6122       for (j = 0; j < nops; j++)
6123         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
6124             && (order[i] == order[i - 1]
6125                 || unsorted_regs[j] < unsorted_regs[order[i]]))
6126           order[i] = j;
6127
6128       /* Have we found a suitable register? if not, one must be used more
6129          than once.  */
6130       if (order[i] == order[i - 1])
6131         return 0;
6132
6133       /* Is the memory address adjacent and ascending? */
6134       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
6135         return 0;
6136     }
6137
6138   if (base)
6139     {
6140       *base = base_reg;
6141
6142       for (i = 0; i < nops; i++)
6143         regs[i] = unsorted_regs[order[i]];
6144
6145       *load_offset = unsorted_offsets[order[0]];
6146     }
6147
6148   if (unsorted_offsets[order[0]] == 0)
6149     return 1; /* stmia */
6150
6151   if (unsorted_offsets[order[0]] == 4)
6152     return 2; /* stmib */
6153
6154   if (unsorted_offsets[order[nops - 1]] == 0)
6155     return 3; /* stmda */
6156
6157   if (unsorted_offsets[order[nops - 1]] == -4)
6158     return 4; /* stmdb */
6159
6160   return 0;
6161 }
6162
6163 const char *
6164 emit_stm_seq (rtx *operands, int nops)
6165 {
6166   int regs[4];
6167   int base_reg;
6168   HOST_WIDE_INT offset;
6169   char buf[100];
6170   int i;
6171
6172   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
6173     {
6174     case 1:
6175       strcpy (buf, "stm%?ia\t");
6176       break;
6177
6178     case 2:
6179       strcpy (buf, "stm%?ib\t");
6180       break;
6181
6182     case 3:
6183       strcpy (buf, "stm%?da\t");
6184       break;
6185
6186     case 4:
6187       strcpy (buf, "stm%?db\t");
6188       break;
6189
6190     default:
6191       gcc_unreachable ();
6192     }
6193
6194   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
6195            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
6196
6197   for (i = 1; i < nops; i++)
6198     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
6199              reg_names[regs[i]]);
6200
6201   strcat (buf, "}\t%@ phole stm");
6202
6203   output_asm_insn (buf, operands);
6204   return "";
6205 }
6206 \f
6207 /* Routines for use in generating RTL.  */
6208
6209 rtx
6210 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
6211                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6212 {
6213   HOST_WIDE_INT offset = *offsetp;
6214   int i = 0, j;
6215   rtx result;
6216   int sign = up ? 1 : -1;
6217   rtx mem, addr;
6218
6219   /* XScale has load-store double instructions, but they have stricter
6220      alignment requirements than load-store multiple, so we cannot
6221      use them.
6222
6223      For XScale ldm requires 2 + NREGS cycles to complete and blocks
6224      the pipeline until completion.
6225
6226         NREGS           CYCLES
6227           1               3
6228           2               4
6229           3               5
6230           4               6
6231
6232      An ldr instruction takes 1-3 cycles, but does not block the
6233      pipeline.
6234
6235         NREGS           CYCLES
6236           1              1-3
6237           2              2-6
6238           3              3-9
6239           4              4-12
6240
6241      Best case ldr will always win.  However, the more ldr instructions
6242      we issue, the less likely we are to be able to schedule them well.
6243      Using ldr instructions also increases code size.
6244
6245      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
6246      for counts of 3 or 4 regs.  */
6247   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6248     {
6249       rtx seq;
6250
6251       start_sequence ();
6252
6253       for (i = 0; i < count; i++)
6254         {
6255           addr = plus_constant (from, i * 4 * sign);
6256           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6257           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
6258           offset += 4 * sign;
6259         }
6260
6261       if (write_back)
6262         {
6263           emit_move_insn (from, plus_constant (from, count * 4 * sign));
6264           *offsetp = offset;
6265         }
6266
6267       seq = get_insns ();
6268       end_sequence ();
6269
6270       return seq;
6271     }
6272
6273   result = gen_rtx_PARALLEL (VOIDmode,
6274                              rtvec_alloc (count + (write_back ? 1 : 0)));
6275   if (write_back)
6276     {
6277       XVECEXP (result, 0, 0)
6278         = gen_rtx_SET (VOIDmode, from, plus_constant (from, count * 4 * sign));
6279       i = 1;
6280       count++;
6281     }
6282
6283   for (j = 0; i < count; i++, j++)
6284     {
6285       addr = plus_constant (from, j * 4 * sign);
6286       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6287       XVECEXP (result, 0, i)
6288         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
6289       offset += 4 * sign;
6290     }
6291
6292   if (write_back)
6293     *offsetp = offset;
6294
6295   return result;
6296 }
6297
6298 rtx
6299 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
6300                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
6301 {
6302   HOST_WIDE_INT offset = *offsetp;
6303   int i = 0, j;
6304   rtx result;
6305   int sign = up ? 1 : -1;
6306   rtx mem, addr;
6307
6308   /* See arm_gen_load_multiple for discussion of
6309      the pros/cons of ldm/stm usage for XScale.  */
6310   if (arm_tune_xscale && count <= 2 && ! optimize_size)
6311     {
6312       rtx seq;
6313
6314       start_sequence ();
6315
6316       for (i = 0; i < count; i++)
6317         {
6318           addr = plus_constant (to, i * 4 * sign);
6319           mem = adjust_automodify_address (basemem, SImode, addr, offset);
6320           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
6321           offset += 4 * sign;
6322         }
6323
6324       if (write_back)
6325         {
6326           emit_move_insn (to, plus_constant (to, count * 4 * sign));
6327           *offsetp = offset;
6328         }
6329
6330       seq = get_insns ();
6331       end_sequence ();
6332
6333       return seq;
6334     }
6335
6336   result = gen_rtx_PARALLEL (VOIDmode,
6337                              rtvec_alloc (count + (write_back ? 1 : 0)));
6338   if (write_back)
6339     {
6340       XVECEXP (result, 0, 0)
6341         = gen_rtx_SET (VOIDmode, to,
6342                        plus_constant (to, count * 4 * sign));
6343       i = 1;
6344       count++;
6345     }
6346
6347   for (j = 0; i < count; i++, j++)
6348     {
6349       addr = plus_constant (to, j * 4 * sign);
6350       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
6351       XVECEXP (result, 0, i)
6352         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
6353       offset += 4 * sign;
6354     }
6355
6356   if (write_back)
6357     *offsetp = offset;
6358
6359   return result;
6360 }
6361
6362 int
6363 arm_gen_movmemqi (rtx *operands)
6364 {
6365   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
6366   HOST_WIDE_INT srcoffset, dstoffset;
6367   int i;
6368   rtx src, dst, srcbase, dstbase;
6369   rtx part_bytes_reg = NULL;
6370   rtx mem;
6371
6372   if (GET_CODE (operands[2]) != CONST_INT
6373       || GET_CODE (operands[3]) != CONST_INT
6374       || INTVAL (operands[2]) > 64
6375       || INTVAL (operands[3]) & 3)
6376     return 0;
6377
6378   dstbase = operands[0];
6379   srcbase = operands[1];
6380
6381   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
6382   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
6383
6384   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
6385   out_words_to_go = INTVAL (operands[2]) / 4;
6386   last_bytes = INTVAL (operands[2]) & 3;
6387   dstoffset = srcoffset = 0;
6388
6389   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
6390     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
6391
6392   for (i = 0; in_words_to_go >= 2; i+=4)
6393     {
6394       if (in_words_to_go > 4)
6395         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
6396                                           srcbase, &srcoffset));
6397       else
6398         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
6399                                           FALSE, srcbase, &srcoffset));
6400
6401       if (out_words_to_go)
6402         {
6403           if (out_words_to_go > 4)
6404             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
6405                                                dstbase, &dstoffset));
6406           else if (out_words_to_go != 1)
6407             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
6408                                                dst, TRUE,
6409                                                (last_bytes == 0
6410                                                 ? FALSE : TRUE),
6411                                                dstbase, &dstoffset));
6412           else
6413             {
6414               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6415               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
6416               if (last_bytes != 0)
6417                 {
6418                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
6419                   dstoffset += 4;
6420                 }
6421             }
6422         }
6423
6424       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
6425       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
6426     }
6427
6428   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
6429   if (out_words_to_go)
6430     {
6431       rtx sreg;
6432
6433       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6434       sreg = copy_to_reg (mem);
6435
6436       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
6437       emit_move_insn (mem, sreg);
6438       in_words_to_go--;
6439
6440       gcc_assert (!in_words_to_go);     /* Sanity check */
6441     }
6442
6443   if (in_words_to_go)
6444     {
6445       gcc_assert (in_words_to_go > 0);
6446
6447       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
6448       part_bytes_reg = copy_to_mode_reg (SImode, mem);
6449     }
6450
6451   gcc_assert (!last_bytes || part_bytes_reg);
6452
6453   if (BYTES_BIG_ENDIAN && last_bytes)
6454     {
6455       rtx tmp = gen_reg_rtx (SImode);
6456
6457       /* The bytes we want are in the top end of the word.  */
6458       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
6459                               GEN_INT (8 * (4 - last_bytes))));
6460       part_bytes_reg = tmp;
6461
6462       while (last_bytes)
6463         {
6464           mem = adjust_automodify_address (dstbase, QImode,
6465                                            plus_constant (dst, last_bytes - 1),
6466                                            dstoffset + last_bytes - 1);
6467           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6468
6469           if (--last_bytes)
6470             {
6471               tmp = gen_reg_rtx (SImode);
6472               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
6473               part_bytes_reg = tmp;
6474             }
6475         }
6476
6477     }
6478   else
6479     {
6480       if (last_bytes > 1)
6481         {
6482           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
6483           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
6484           last_bytes -= 2;
6485           if (last_bytes)
6486             {
6487               rtx tmp = gen_reg_rtx (SImode);
6488               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
6489               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
6490               part_bytes_reg = tmp;
6491               dstoffset += 2;
6492             }
6493         }
6494
6495       if (last_bytes)
6496         {
6497           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
6498           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
6499         }
6500     }
6501
6502   return 1;
6503 }
6504
6505 /* Select a dominance comparison mode if possible for a test of the general
6506    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
6507    COND_OR == DOM_CC_X_AND_Y => (X && Y)
6508    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
6509    COND_OR == DOM_CC_X_OR_Y => (X || Y)
6510    In all cases OP will be either EQ or NE, but we don't need to know which
6511    here.  If we are unable to support a dominance comparison we return
6512    CC mode.  This will then fail to match for the RTL expressions that
6513    generate this call.  */
6514 enum machine_mode
6515 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
6516 {
6517   enum rtx_code cond1, cond2;
6518   int swapped = 0;
6519
6520   /* Currently we will probably get the wrong result if the individual
6521      comparisons are not simple.  This also ensures that it is safe to
6522      reverse a comparison if necessary.  */
6523   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
6524        != CCmode)
6525       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
6526           != CCmode))
6527     return CCmode;
6528
6529   /* The if_then_else variant of this tests the second condition if the
6530      first passes, but is true if the first fails.  Reverse the first
6531      condition to get a true "inclusive-or" expression.  */
6532   if (cond_or == DOM_CC_NX_OR_Y)
6533     cond1 = reverse_condition (cond1);
6534
6535   /* If the comparisons are not equal, and one doesn't dominate the other,
6536      then we can't do this.  */
6537   if (cond1 != cond2
6538       && !comparison_dominates_p (cond1, cond2)
6539       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
6540     return CCmode;
6541
6542   if (swapped)
6543     {
6544       enum rtx_code temp = cond1;
6545       cond1 = cond2;
6546       cond2 = temp;
6547     }
6548
6549   switch (cond1)
6550     {
6551     case EQ:
6552       if (cond_or == DOM_CC_X_AND_Y)
6553         return CC_DEQmode;
6554
6555       switch (cond2)
6556         {
6557         case EQ: return CC_DEQmode;
6558         case LE: return CC_DLEmode;
6559         case LEU: return CC_DLEUmode;
6560         case GE: return CC_DGEmode;
6561         case GEU: return CC_DGEUmode;
6562         default: gcc_unreachable ();
6563         }
6564
6565     case LT:
6566       if (cond_or == DOM_CC_X_AND_Y)
6567         return CC_DLTmode;
6568
6569       switch (cond2)
6570         {
6571         case  LT:
6572             return CC_DLTmode;
6573         case LE:
6574           return CC_DLEmode;
6575         case NE:
6576           return CC_DNEmode;
6577         default:
6578           gcc_unreachable ();
6579         }
6580
6581     case GT:
6582       if (cond_or == DOM_CC_X_AND_Y)
6583         return CC_DGTmode;
6584
6585       switch (cond2)
6586         {
6587         case GT:
6588           return CC_DGTmode;
6589         case GE:
6590           return CC_DGEmode;
6591         case NE:
6592           return CC_DNEmode;
6593         default:
6594           gcc_unreachable ();
6595         }
6596
6597     case LTU:
6598       if (cond_or == DOM_CC_X_AND_Y)
6599         return CC_DLTUmode;
6600
6601       switch (cond2)
6602         {
6603         case LTU:
6604           return CC_DLTUmode;
6605         case LEU:
6606           return CC_DLEUmode;
6607         case NE:
6608           return CC_DNEmode;
6609         default:
6610           gcc_unreachable ();
6611         }
6612
6613     case GTU:
6614       if (cond_or == DOM_CC_X_AND_Y)
6615         return CC_DGTUmode;
6616
6617       switch (cond2)
6618         {
6619         case GTU:
6620           return CC_DGTUmode;
6621         case GEU:
6622           return CC_DGEUmode;
6623         case NE:
6624           return CC_DNEmode;
6625         default:
6626           gcc_unreachable ();
6627         }
6628
6629     /* The remaining cases only occur when both comparisons are the
6630        same.  */
6631     case NE:
6632       gcc_assert (cond1 == cond2);
6633       return CC_DNEmode;
6634
6635     case LE:
6636       gcc_assert (cond1 == cond2);
6637       return CC_DLEmode;
6638
6639     case GE:
6640       gcc_assert (cond1 == cond2);
6641       return CC_DGEmode;
6642
6643     case LEU:
6644       gcc_assert (cond1 == cond2);
6645       return CC_DLEUmode;
6646
6647     case GEU:
6648       gcc_assert (cond1 == cond2);
6649       return CC_DGEUmode;
6650
6651     default:
6652       gcc_unreachable ();
6653     }
6654 }
6655
6656 enum machine_mode
6657 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
6658 {
6659   /* All floating point compares return CCFP if it is an equality
6660      comparison, and CCFPE otherwise.  */
6661   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6662     {
6663       switch (op)
6664         {
6665         case EQ:
6666         case NE:
6667         case UNORDERED:
6668         case ORDERED:
6669         case UNLT:
6670         case UNLE:
6671         case UNGT:
6672         case UNGE:
6673         case UNEQ:
6674         case LTGT:
6675           return CCFPmode;
6676
6677         case LT:
6678         case LE:
6679         case GT:
6680         case GE:
6681           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6682             return CCFPmode;
6683           return CCFPEmode;
6684
6685         default:
6686           gcc_unreachable ();
6687         }
6688     }
6689
6690   /* A compare with a shifted operand.  Because of canonicalization, the
6691      comparison will have to be swapped when we emit the assembler.  */
6692   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6693       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6694           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6695           || GET_CODE (x) == ROTATERT))
6696     return CC_SWPmode;
6697
6698   /* This operation is performed swapped, but since we only rely on the Z
6699      flag we don't need an additional mode.  */
6700   if (GET_MODE (y) == SImode && REG_P (y)
6701       && GET_CODE (x) == NEG
6702       && (op == EQ || op == NE))
6703     return CC_Zmode;
6704
6705   /* This is a special case that is used by combine to allow a
6706      comparison of a shifted byte load to be split into a zero-extend
6707      followed by a comparison of the shifted integer (only valid for
6708      equalities and unsigned inequalities).  */
6709   if (GET_MODE (x) == SImode
6710       && GET_CODE (x) == ASHIFT
6711       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6712       && GET_CODE (XEXP (x, 0)) == SUBREG
6713       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6714       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6715       && (op == EQ || op == NE
6716           || op == GEU || op == GTU || op == LTU || op == LEU)
6717       && GET_CODE (y) == CONST_INT)
6718     return CC_Zmode;
6719
6720   /* A construct for a conditional compare, if the false arm contains
6721      0, then both conditions must be true, otherwise either condition
6722      must be true.  Not all conditions are possible, so CCmode is
6723      returned if it can't be done.  */
6724   if (GET_CODE (x) == IF_THEN_ELSE
6725       && (XEXP (x, 2) == const0_rtx
6726           || XEXP (x, 2) == const1_rtx)
6727       && COMPARISON_P (XEXP (x, 0))
6728       && COMPARISON_P (XEXP (x, 1)))
6729     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6730                                          INTVAL (XEXP (x, 2)));
6731
6732   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6733   if (GET_CODE (x) == AND
6734       && COMPARISON_P (XEXP (x, 0))
6735       && COMPARISON_P (XEXP (x, 1)))
6736     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6737                                          DOM_CC_X_AND_Y);
6738
6739   if (GET_CODE (x) == IOR
6740       && COMPARISON_P (XEXP (x, 0))
6741       && COMPARISON_P (XEXP (x, 1)))
6742     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6743                                          DOM_CC_X_OR_Y);
6744
6745   /* An operation (on Thumb) where we want to test for a single bit.
6746      This is done by shifting that bit up into the top bit of a
6747      scratch register; we can then branch on the sign bit.  */
6748   if (TARGET_THUMB
6749       && GET_MODE (x) == SImode
6750       && (op == EQ || op == NE)
6751       && GET_CODE (x) == ZERO_EXTRACT
6752       && XEXP (x, 1) == const1_rtx)
6753     return CC_Nmode;
6754
6755   /* An operation that sets the condition codes as a side-effect, the
6756      V flag is not set correctly, so we can only use comparisons where
6757      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6758      instead.)  */
6759   if (GET_MODE (x) == SImode
6760       && y == const0_rtx
6761       && (op == EQ || op == NE || op == LT || op == GE)
6762       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6763           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6764           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6765           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6766           || GET_CODE (x) == LSHIFTRT
6767           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6768           || GET_CODE (x) == ROTATERT
6769           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6770     return CC_NOOVmode;
6771
6772   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6773     return CC_Zmode;
6774
6775   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6776       && GET_CODE (x) == PLUS
6777       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6778     return CC_Cmode;
6779
6780   return CCmode;
6781 }
6782
6783 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6784    return the rtx for register 0 in the proper mode.  FP means this is a
6785    floating point compare: I don't think that it is needed on the arm.  */
6786 rtx
6787 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6788 {
6789   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6790   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6791
6792   emit_set_insn (cc_reg, gen_rtx_COMPARE (mode, x, y));
6793
6794   return cc_reg;
6795 }
6796
6797 /* Generate a sequence of insns that will generate the correct return
6798    address mask depending on the physical architecture that the program
6799    is running on.  */
6800 rtx
6801 arm_gen_return_addr_mask (void)
6802 {
6803   rtx reg = gen_reg_rtx (Pmode);
6804
6805   emit_insn (gen_return_addr_mask (reg));
6806   return reg;
6807 }
6808
6809 void
6810 arm_reload_in_hi (rtx *operands)
6811 {
6812   rtx ref = operands[1];
6813   rtx base, scratch;
6814   HOST_WIDE_INT offset = 0;
6815
6816   if (GET_CODE (ref) == SUBREG)
6817     {
6818       offset = SUBREG_BYTE (ref);
6819       ref = SUBREG_REG (ref);
6820     }
6821
6822   if (GET_CODE (ref) == REG)
6823     {
6824       /* We have a pseudo which has been spilt onto the stack; there
6825          are two cases here: the first where there is a simple
6826          stack-slot replacement and a second where the stack-slot is
6827          out of range, or is used as a subreg.  */
6828       if (reg_equiv_mem[REGNO (ref)])
6829         {
6830           ref = reg_equiv_mem[REGNO (ref)];
6831           base = find_replacement (&XEXP (ref, 0));
6832         }
6833       else
6834         /* The slot is out of range, or was dressed up in a SUBREG.  */
6835         base = reg_equiv_address[REGNO (ref)];
6836     }
6837   else
6838     base = find_replacement (&XEXP (ref, 0));
6839
6840   /* Handle the case where the address is too complex to be offset by 1.  */
6841   if (GET_CODE (base) == MINUS
6842       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6843     {
6844       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6845
6846       emit_set_insn (base_plus, base);
6847       base = base_plus;
6848     }
6849   else if (GET_CODE (base) == PLUS)
6850     {
6851       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6852       HOST_WIDE_INT hi, lo;
6853
6854       offset += INTVAL (XEXP (base, 1));
6855       base = XEXP (base, 0);
6856
6857       /* Rework the address into a legal sequence of insns.  */
6858       /* Valid range for lo is -4095 -> 4095 */
6859       lo = (offset >= 0
6860             ? (offset & 0xfff)
6861             : -((-offset) & 0xfff));
6862
6863       /* Corner case, if lo is the max offset then we would be out of range
6864          once we have added the additional 1 below, so bump the msb into the
6865          pre-loading insn(s).  */
6866       if (lo == 4095)
6867         lo &= 0x7ff;
6868
6869       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6870              ^ (HOST_WIDE_INT) 0x80000000)
6871             - (HOST_WIDE_INT) 0x80000000);
6872
6873       gcc_assert (hi + lo == offset);
6874
6875       if (hi != 0)
6876         {
6877           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6878
6879           /* Get the base address; addsi3 knows how to handle constants
6880              that require more than one insn.  */
6881           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6882           base = base_plus;
6883           offset = lo;
6884         }
6885     }
6886
6887   /* Operands[2] may overlap operands[0] (though it won't overlap
6888      operands[1]), that's why we asked for a DImode reg -- so we can
6889      use the bit that does not overlap.  */
6890   if (REGNO (operands[2]) == REGNO (operands[0]))
6891     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6892   else
6893     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6894
6895   emit_insn (gen_zero_extendqisi2 (scratch,
6896                                    gen_rtx_MEM (QImode,
6897                                                 plus_constant (base,
6898                                                                offset))));
6899   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6900                                    gen_rtx_MEM (QImode,
6901                                                 plus_constant (base,
6902                                                                offset + 1))));
6903   if (!BYTES_BIG_ENDIAN)
6904     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6905                    gen_rtx_IOR (SImode,
6906                                 gen_rtx_ASHIFT
6907                                 (SImode,
6908                                  gen_rtx_SUBREG (SImode, operands[0], 0),
6909                                  GEN_INT (8)),
6910                                 scratch));
6911   else
6912     emit_set_insn (gen_rtx_SUBREG (SImode, operands[0], 0),
6913                    gen_rtx_IOR (SImode,
6914                                 gen_rtx_ASHIFT (SImode, scratch,
6915                                                 GEN_INT (8)),
6916                                 gen_rtx_SUBREG (SImode, operands[0], 0)));
6917 }
6918
6919 /* Handle storing a half-word to memory during reload by synthesizing as two
6920    byte stores.  Take care not to clobber the input values until after we
6921    have moved them somewhere safe.  This code assumes that if the DImode
6922    scratch in operands[2] overlaps either the input value or output address
6923    in some way, then that value must die in this insn (we absolutely need
6924    two scratch registers for some corner cases).  */
6925 void
6926 arm_reload_out_hi (rtx *operands)
6927 {
6928   rtx ref = operands[0];
6929   rtx outval = operands[1];
6930   rtx base, scratch;
6931   HOST_WIDE_INT offset = 0;
6932
6933   if (GET_CODE (ref) == SUBREG)
6934     {
6935       offset = SUBREG_BYTE (ref);
6936       ref = SUBREG_REG (ref);
6937     }
6938
6939   if (GET_CODE (ref) == REG)
6940     {
6941       /* We have a pseudo which has been spilt onto the stack; there
6942          are two cases here: the first where there is a simple
6943          stack-slot replacement and a second where the stack-slot is
6944          out of range, or is used as a subreg.  */
6945       if (reg_equiv_mem[REGNO (ref)])
6946         {
6947           ref = reg_equiv_mem[REGNO (ref)];
6948           base = find_replacement (&XEXP (ref, 0));
6949         }
6950       else
6951         /* The slot is out of range, or was dressed up in a SUBREG.  */
6952         base = reg_equiv_address[REGNO (ref)];
6953     }
6954   else
6955     base = find_replacement (&XEXP (ref, 0));
6956
6957   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6958
6959   /* Handle the case where the address is too complex to be offset by 1.  */
6960   if (GET_CODE (base) == MINUS
6961       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6962     {
6963       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6964
6965       /* Be careful not to destroy OUTVAL.  */
6966       if (reg_overlap_mentioned_p (base_plus, outval))
6967         {
6968           /* Updating base_plus might destroy outval, see if we can
6969              swap the scratch and base_plus.  */
6970           if (!reg_overlap_mentioned_p (scratch, outval))
6971             {
6972               rtx tmp = scratch;
6973               scratch = base_plus;
6974               base_plus = tmp;
6975             }
6976           else
6977             {
6978               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6979
6980               /* Be conservative and copy OUTVAL into the scratch now,
6981                  this should only be necessary if outval is a subreg
6982                  of something larger than a word.  */
6983               /* XXX Might this clobber base?  I can't see how it can,
6984                  since scratch is known to overlap with OUTVAL, and
6985                  must be wider than a word.  */
6986               emit_insn (gen_movhi (scratch_hi, outval));
6987               outval = scratch_hi;
6988             }
6989         }
6990
6991       emit_set_insn (base_plus, base);
6992       base = base_plus;
6993     }
6994   else if (GET_CODE (base) == PLUS)
6995     {
6996       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6997       HOST_WIDE_INT hi, lo;
6998
6999       offset += INTVAL (XEXP (base, 1));
7000       base = XEXP (base, 0);
7001
7002       /* Rework the address into a legal sequence of insns.  */
7003       /* Valid range for lo is -4095 -> 4095 */
7004       lo = (offset >= 0
7005             ? (offset & 0xfff)
7006             : -((-offset) & 0xfff));
7007
7008       /* Corner case, if lo is the max offset then we would be out of range
7009          once we have added the additional 1 below, so bump the msb into the
7010          pre-loading insn(s).  */
7011       if (lo == 4095)
7012         lo &= 0x7ff;
7013
7014       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
7015              ^ (HOST_WIDE_INT) 0x80000000)
7016             - (HOST_WIDE_INT) 0x80000000);
7017
7018       gcc_assert (hi + lo == offset);
7019
7020       if (hi != 0)
7021         {
7022           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
7023
7024           /* Be careful not to destroy OUTVAL.  */
7025           if (reg_overlap_mentioned_p (base_plus, outval))
7026             {
7027               /* Updating base_plus might destroy outval, see if we
7028                  can swap the scratch and base_plus.  */
7029               if (!reg_overlap_mentioned_p (scratch, outval))
7030                 {
7031                   rtx tmp = scratch;
7032                   scratch = base_plus;
7033                   base_plus = tmp;
7034                 }
7035               else
7036                 {
7037                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
7038
7039                   /* Be conservative and copy outval into scratch now,
7040                      this should only be necessary if outval is a
7041                      subreg of something larger than a word.  */
7042                   /* XXX Might this clobber base?  I can't see how it
7043                      can, since scratch is known to overlap with
7044                      outval.  */
7045                   emit_insn (gen_movhi (scratch_hi, outval));
7046                   outval = scratch_hi;
7047                 }
7048             }
7049
7050           /* Get the base address; addsi3 knows how to handle constants
7051              that require more than one insn.  */
7052           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
7053           base = base_plus;
7054           offset = lo;
7055         }
7056     }
7057
7058   if (BYTES_BIG_ENDIAN)
7059     {
7060       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7061                                          plus_constant (base, offset + 1)),
7062                             gen_lowpart (QImode, outval)));
7063       emit_insn (gen_lshrsi3 (scratch,
7064                               gen_rtx_SUBREG (SImode, outval, 0),
7065                               GEN_INT (8)));
7066       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7067                             gen_lowpart (QImode, scratch)));
7068     }
7069   else
7070     {
7071       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
7072                             gen_lowpart (QImode, outval)));
7073       emit_insn (gen_lshrsi3 (scratch,
7074                               gen_rtx_SUBREG (SImode, outval, 0),
7075                               GEN_INT (8)));
7076       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
7077                                          plus_constant (base, offset + 1)),
7078                             gen_lowpart (QImode, scratch)));
7079     }
7080 }
7081
7082 /* Return true if a type must be passed in memory. For AAPCS, small aggregates
7083    (padded to the size of a word) should be passed in a register.  */
7084
7085 static bool
7086 arm_must_pass_in_stack (enum machine_mode mode, tree type)
7087 {
7088   if (TARGET_AAPCS_BASED)
7089     return must_pass_in_stack_var_size (mode, type);
7090   else
7091     return must_pass_in_stack_var_size_or_pad (mode, type);
7092 }
7093
7094
7095 /* For use by FUNCTION_ARG_PADDING (MODE, TYPE).
7096    Return true if an argument passed on the stack should be padded upwards,
7097    i.e. if the least-significant byte has useful data.
7098    For legacy APCS ABIs we use the default.  For AAPCS based ABIs small
7099    aggregate types are placed in the lowest memory address.  */
7100
7101 bool
7102 arm_pad_arg_upward (enum machine_mode mode, tree type)
7103 {
7104   if (!TARGET_AAPCS_BASED)
7105     return DEFAULT_FUNCTION_ARG_PADDING(mode, type) == upward;
7106
7107   if (type && BYTES_BIG_ENDIAN && INTEGRAL_TYPE_P (type))
7108     return false;
7109
7110   return true;
7111 }
7112
7113
7114 /* Similarly, for use by BLOCK_REG_PADDING (MODE, TYPE, FIRST).
7115    For non-AAPCS, return !BYTES_BIG_ENDIAN if the least significant
7116    byte of the register has useful data, and return the opposite if the
7117    most significant byte does.
7118    For AAPCS, small aggregates and small complex types are always padded
7119    upwards.  */
7120
7121 bool
7122 arm_pad_reg_upward (enum machine_mode mode ATTRIBUTE_UNUSED,
7123                     tree type, int first ATTRIBUTE_UNUSED)
7124 {
7125   if (TARGET_AAPCS_BASED
7126       && BYTES_BIG_ENDIAN
7127       && (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
7128       && int_size_in_bytes (type) <= 4)
7129     return true;
7130
7131   /* Otherwise, use default padding.  */
7132   return !BYTES_BIG_ENDIAN;
7133 }
7134
7135 \f
7136 /* Print a symbolic form of X to the debug file, F.  */
7137 static void
7138 arm_print_value (FILE *f, rtx x)
7139 {
7140   switch (GET_CODE (x))
7141     {
7142     case CONST_INT:
7143       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
7144       return;
7145
7146     case CONST_DOUBLE:
7147       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
7148       return;
7149
7150     case CONST_VECTOR:
7151       {
7152         int i;
7153
7154         fprintf (f, "<");
7155         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
7156           {
7157             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
7158             if (i < (CONST_VECTOR_NUNITS (x) - 1))
7159               fputc (',', f);
7160           }
7161         fprintf (f, ">");
7162       }
7163       return;
7164
7165     case CONST_STRING:
7166       fprintf (f, "\"%s\"", XSTR (x, 0));
7167       return;
7168
7169     case SYMBOL_REF:
7170       fprintf (f, "`%s'", XSTR (x, 0));
7171       return;
7172
7173     case LABEL_REF:
7174       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
7175       return;
7176
7177     case CONST:
7178       arm_print_value (f, XEXP (x, 0));
7179       return;
7180
7181     case PLUS:
7182       arm_print_value (f, XEXP (x, 0));
7183       fprintf (f, "+");
7184       arm_print_value (f, XEXP (x, 1));
7185       return;
7186
7187     case PC:
7188       fprintf (f, "pc");
7189       return;
7190
7191     default:
7192       fprintf (f, "????");
7193       return;
7194     }
7195 }
7196 \f
7197 /* Routines for manipulation of the constant pool.  */
7198
7199 /* Arm instructions cannot load a large constant directly into a
7200    register; they have to come from a pc relative load.  The constant
7201    must therefore be placed in the addressable range of the pc
7202    relative load.  Depending on the precise pc relative load
7203    instruction the range is somewhere between 256 bytes and 4k.  This
7204    means that we often have to dump a constant inside a function, and
7205    generate code to branch around it.
7206
7207    It is important to minimize this, since the branches will slow
7208    things down and make the code larger.
7209
7210    Normally we can hide the table after an existing unconditional
7211    branch so that there is no interruption of the flow, but in the
7212    worst case the code looks like this:
7213
7214         ldr     rn, L1
7215         ...
7216         b       L2
7217         align
7218         L1:     .long value
7219         L2:
7220         ...
7221
7222         ldr     rn, L3
7223         ...
7224         b       L4
7225         align
7226         L3:     .long value
7227         L4:
7228         ...
7229
7230    We fix this by performing a scan after scheduling, which notices
7231    which instructions need to have their operands fetched from the
7232    constant table and builds the table.
7233
7234    The algorithm starts by building a table of all the constants that
7235    need fixing up and all the natural barriers in the function (places
7236    where a constant table can be dropped without breaking the flow).
7237    For each fixup we note how far the pc-relative replacement will be
7238    able to reach and the offset of the instruction into the function.
7239
7240    Having built the table we then group the fixes together to form
7241    tables that are as large as possible (subject to addressing
7242    constraints) and emit each table of constants after the last
7243    barrier that is within range of all the instructions in the group.
7244    If a group does not contain a barrier, then we forcibly create one
7245    by inserting a jump instruction into the flow.  Once the table has
7246    been inserted, the insns are then modified to reference the
7247    relevant entry in the pool.
7248
7249    Possible enhancements to the algorithm (not implemented) are:
7250
7251    1) For some processors and object formats, there may be benefit in
7252    aligning the pools to the start of cache lines; this alignment
7253    would need to be taken into account when calculating addressability
7254    of a pool.  */
7255
7256 /* These typedefs are located at the start of this file, so that
7257    they can be used in the prototypes there.  This comment is to
7258    remind readers of that fact so that the following structures
7259    can be understood more easily.
7260
7261      typedef struct minipool_node    Mnode;
7262      typedef struct minipool_fixup   Mfix;  */
7263
7264 struct minipool_node
7265 {
7266   /* Doubly linked chain of entries.  */
7267   Mnode * next;
7268   Mnode * prev;
7269   /* The maximum offset into the code that this entry can be placed.  While
7270      pushing fixes for forward references, all entries are sorted in order
7271      of increasing max_address.  */
7272   HOST_WIDE_INT max_address;
7273   /* Similarly for an entry inserted for a backwards ref.  */
7274   HOST_WIDE_INT min_address;
7275   /* The number of fixes referencing this entry.  This can become zero
7276      if we "unpush" an entry.  In this case we ignore the entry when we
7277      come to emit the code.  */
7278   int refcount;
7279   /* The offset from the start of the minipool.  */
7280   HOST_WIDE_INT offset;
7281   /* The value in table.  */
7282   rtx value;
7283   /* The mode of value.  */
7284   enum machine_mode mode;
7285   /* The size of the value.  With iWMMXt enabled
7286      sizes > 4 also imply an alignment of 8-bytes.  */
7287   int fix_size;
7288 };
7289
7290 struct minipool_fixup
7291 {
7292   Mfix *            next;
7293   rtx               insn;
7294   HOST_WIDE_INT     address;
7295   rtx *             loc;
7296   enum machine_mode mode;
7297   int               fix_size;
7298   rtx               value;
7299   Mnode *           minipool;
7300   HOST_WIDE_INT     forwards;
7301   HOST_WIDE_INT     backwards;
7302 };
7303
7304 /* Fixes less than a word need padding out to a word boundary.  */
7305 #define MINIPOOL_FIX_SIZE(mode) \
7306   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
7307
7308 static Mnode *  minipool_vector_head;
7309 static Mnode *  minipool_vector_tail;
7310 static rtx      minipool_vector_label;
7311
7312 /* The linked list of all minipool fixes required for this function.  */
7313 Mfix *          minipool_fix_head;
7314 Mfix *          minipool_fix_tail;
7315 /* The fix entry for the current minipool, once it has been placed.  */
7316 Mfix *          minipool_barrier;
7317
7318 /* Determines if INSN is the start of a jump table.  Returns the end
7319    of the TABLE or NULL_RTX.  */
7320 static rtx
7321 is_jump_table (rtx insn)
7322 {
7323   rtx table;
7324
7325   if (GET_CODE (insn) == JUMP_INSN
7326       && JUMP_LABEL (insn) != NULL
7327       && ((table = next_real_insn (JUMP_LABEL (insn)))
7328           == next_real_insn (insn))
7329       && table != NULL
7330       && GET_CODE (table) == JUMP_INSN
7331       && (GET_CODE (PATTERN (table)) == ADDR_VEC
7332           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
7333     return table;
7334
7335   return NULL_RTX;
7336 }
7337
7338 #ifndef JUMP_TABLES_IN_TEXT_SECTION
7339 #define JUMP_TABLES_IN_TEXT_SECTION 0
7340 #endif
7341
7342 static HOST_WIDE_INT
7343 get_jump_table_size (rtx insn)
7344 {
7345   /* ADDR_VECs only take room if read-only data does into the text
7346      section.  */
7347   if (JUMP_TABLES_IN_TEXT_SECTION || readonly_data_section == text_section)
7348     {
7349       rtx body = PATTERN (insn);
7350       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
7351
7352       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
7353     }
7354
7355   return 0;
7356 }
7357
7358 /* Move a minipool fix MP from its current location to before MAX_MP.
7359    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
7360    constraints may need updating.  */
7361 static Mnode *
7362 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
7363                                HOST_WIDE_INT max_address)
7364 {
7365   /* The code below assumes these are different.  */
7366   gcc_assert (mp != max_mp);
7367
7368   if (max_mp == NULL)
7369     {
7370       if (max_address < mp->max_address)
7371         mp->max_address = max_address;
7372     }
7373   else
7374     {
7375       if (max_address > max_mp->max_address - mp->fix_size)
7376         mp->max_address = max_mp->max_address - mp->fix_size;
7377       else
7378         mp->max_address = max_address;
7379
7380       /* Unlink MP from its current position.  Since max_mp is non-null,
7381        mp->prev must be non-null.  */
7382       mp->prev->next = mp->next;
7383       if (mp->next != NULL)
7384         mp->next->prev = mp->prev;
7385       else
7386         minipool_vector_tail = mp->prev;
7387
7388       /* Re-insert it before MAX_MP.  */
7389       mp->next = max_mp;
7390       mp->prev = max_mp->prev;
7391       max_mp->prev = mp;
7392
7393       if (mp->prev != NULL)
7394         mp->prev->next = mp;
7395       else
7396         minipool_vector_head = mp;
7397     }
7398
7399   /* Save the new entry.  */
7400   max_mp = mp;
7401
7402   /* Scan over the preceding entries and adjust their addresses as
7403      required.  */
7404   while (mp->prev != NULL
7405          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7406     {
7407       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7408       mp = mp->prev;
7409     }
7410
7411   return max_mp;
7412 }
7413
7414 /* Add a constant to the minipool for a forward reference.  Returns the
7415    node added or NULL if the constant will not fit in this pool.  */
7416 static Mnode *
7417 add_minipool_forward_ref (Mfix *fix)
7418 {
7419   /* If set, max_mp is the first pool_entry that has a lower
7420      constraint than the one we are trying to add.  */
7421   Mnode *       max_mp = NULL;
7422   HOST_WIDE_INT max_address = fix->address + fix->forwards;
7423   Mnode *       mp;
7424
7425   /* If this fix's address is greater than the address of the first
7426      entry, then we can't put the fix in this pool.  We subtract the
7427      size of the current fix to ensure that if the table is fully
7428      packed we still have enough room to insert this value by shuffling
7429      the other fixes forwards.  */
7430   if (minipool_vector_head &&
7431       fix->address >= minipool_vector_head->max_address - fix->fix_size)
7432     return NULL;
7433
7434   /* Scan the pool to see if a constant with the same value has
7435      already been added.  While we are doing this, also note the
7436      location where we must insert the constant if it doesn't already
7437      exist.  */
7438   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7439     {
7440       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7441           && fix->mode == mp->mode
7442           && (GET_CODE (fix->value) != CODE_LABEL
7443               || (CODE_LABEL_NUMBER (fix->value)
7444                   == CODE_LABEL_NUMBER (mp->value)))
7445           && rtx_equal_p (fix->value, mp->value))
7446         {
7447           /* More than one fix references this entry.  */
7448           mp->refcount++;
7449           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
7450         }
7451
7452       /* Note the insertion point if necessary.  */
7453       if (max_mp == NULL
7454           && mp->max_address > max_address)
7455         max_mp = mp;
7456
7457       /* If we are inserting an 8-bytes aligned quantity and
7458          we have not already found an insertion point, then
7459          make sure that all such 8-byte aligned quantities are
7460          placed at the start of the pool.  */
7461       if (ARM_DOUBLEWORD_ALIGN
7462           && max_mp == NULL
7463           && fix->fix_size == 8
7464           && mp->fix_size != 8)
7465         {
7466           max_mp = mp;
7467           max_address = mp->max_address;
7468         }
7469     }
7470
7471   /* The value is not currently in the minipool, so we need to create
7472      a new entry for it.  If MAX_MP is NULL, the entry will be put on
7473      the end of the list since the placement is less constrained than
7474      any existing entry.  Otherwise, we insert the new fix before
7475      MAX_MP and, if necessary, adjust the constraints on the other
7476      entries.  */
7477   mp = XNEW (Mnode);
7478   mp->fix_size = fix->fix_size;
7479   mp->mode = fix->mode;
7480   mp->value = fix->value;
7481   mp->refcount = 1;
7482   /* Not yet required for a backwards ref.  */
7483   mp->min_address = -65536;
7484
7485   if (max_mp == NULL)
7486     {
7487       mp->max_address = max_address;
7488       mp->next = NULL;
7489       mp->prev = minipool_vector_tail;
7490
7491       if (mp->prev == NULL)
7492         {
7493           minipool_vector_head = mp;
7494           minipool_vector_label = gen_label_rtx ();
7495         }
7496       else
7497         mp->prev->next = mp;
7498
7499       minipool_vector_tail = mp;
7500     }
7501   else
7502     {
7503       if (max_address > max_mp->max_address - mp->fix_size)
7504         mp->max_address = max_mp->max_address - mp->fix_size;
7505       else
7506         mp->max_address = max_address;
7507
7508       mp->next = max_mp;
7509       mp->prev = max_mp->prev;
7510       max_mp->prev = mp;
7511       if (mp->prev != NULL)
7512         mp->prev->next = mp;
7513       else
7514         minipool_vector_head = mp;
7515     }
7516
7517   /* Save the new entry.  */
7518   max_mp = mp;
7519
7520   /* Scan over the preceding entries and adjust their addresses as
7521      required.  */
7522   while (mp->prev != NULL
7523          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
7524     {
7525       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
7526       mp = mp->prev;
7527     }
7528
7529   return max_mp;
7530 }
7531
7532 static Mnode *
7533 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
7534                                 HOST_WIDE_INT  min_address)
7535 {
7536   HOST_WIDE_INT offset;
7537
7538   /* The code below assumes these are different.  */
7539   gcc_assert (mp != min_mp);
7540
7541   if (min_mp == NULL)
7542     {
7543       if (min_address > mp->min_address)
7544         mp->min_address = min_address;
7545     }
7546   else
7547     {
7548       /* We will adjust this below if it is too loose.  */
7549       mp->min_address = min_address;
7550
7551       /* Unlink MP from its current position.  Since min_mp is non-null,
7552          mp->next must be non-null.  */
7553       mp->next->prev = mp->prev;
7554       if (mp->prev != NULL)
7555         mp->prev->next = mp->next;
7556       else
7557         minipool_vector_head = mp->next;
7558
7559       /* Reinsert it after MIN_MP.  */
7560       mp->prev = min_mp;
7561       mp->next = min_mp->next;
7562       min_mp->next = mp;
7563       if (mp->next != NULL)
7564         mp->next->prev = mp;
7565       else
7566         minipool_vector_tail = mp;
7567     }
7568
7569   min_mp = mp;
7570
7571   offset = 0;
7572   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7573     {
7574       mp->offset = offset;
7575       if (mp->refcount > 0)
7576         offset += mp->fix_size;
7577
7578       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
7579         mp->next->min_address = mp->min_address + mp->fix_size;
7580     }
7581
7582   return min_mp;
7583 }
7584
7585 /* Add a constant to the minipool for a backward reference.  Returns the
7586    node added or NULL if the constant will not fit in this pool.
7587
7588    Note that the code for insertion for a backwards reference can be
7589    somewhat confusing because the calculated offsets for each fix do
7590    not take into account the size of the pool (which is still under
7591    construction.  */
7592 static Mnode *
7593 add_minipool_backward_ref (Mfix *fix)
7594 {
7595   /* If set, min_mp is the last pool_entry that has a lower constraint
7596      than the one we are trying to add.  */
7597   Mnode *min_mp = NULL;
7598   /* This can be negative, since it is only a constraint.  */
7599   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
7600   Mnode *mp;
7601
7602   /* If we can't reach the current pool from this insn, or if we can't
7603      insert this entry at the end of the pool without pushing other
7604      fixes out of range, then we don't try.  This ensures that we
7605      can't fail later on.  */
7606   if (min_address >= minipool_barrier->address
7607       || (minipool_vector_tail->min_address + fix->fix_size
7608           >= minipool_barrier->address))
7609     return NULL;
7610
7611   /* Scan the pool to see if a constant with the same value has
7612      already been added.  While we are doing this, also note the
7613      location where we must insert the constant if it doesn't already
7614      exist.  */
7615   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
7616     {
7617       if (GET_CODE (fix->value) == GET_CODE (mp->value)
7618           && fix->mode == mp->mode
7619           && (GET_CODE (fix->value) != CODE_LABEL
7620               || (CODE_LABEL_NUMBER (fix->value)
7621                   == CODE_LABEL_NUMBER (mp->value)))
7622           && rtx_equal_p (fix->value, mp->value)
7623           /* Check that there is enough slack to move this entry to the
7624              end of the table (this is conservative).  */
7625           && (mp->max_address
7626               > (minipool_barrier->address
7627                  + minipool_vector_tail->offset
7628                  + minipool_vector_tail->fix_size)))
7629         {
7630           mp->refcount++;
7631           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
7632         }
7633
7634       if (min_mp != NULL)
7635         mp->min_address += fix->fix_size;
7636       else
7637         {
7638           /* Note the insertion point if necessary.  */
7639           if (mp->min_address < min_address)
7640             {
7641               /* For now, we do not allow the insertion of 8-byte alignment
7642                  requiring nodes anywhere but at the start of the pool.  */
7643               if (ARM_DOUBLEWORD_ALIGN
7644                   && fix->fix_size == 8 && mp->fix_size != 8)
7645                 return NULL;
7646               else
7647                 min_mp = mp;
7648             }
7649           else if (mp->max_address
7650                    < minipool_barrier->address + mp->offset + fix->fix_size)
7651             {
7652               /* Inserting before this entry would push the fix beyond
7653                  its maximum address (which can happen if we have
7654                  re-located a forwards fix); force the new fix to come
7655                  after it.  */
7656               min_mp = mp;
7657               min_address = mp->min_address + fix->fix_size;
7658             }
7659           /* If we are inserting an 8-bytes aligned quantity and
7660              we have not already found an insertion point, then
7661              make sure that all such 8-byte aligned quantities are
7662              placed at the start of the pool.  */
7663           else if (ARM_DOUBLEWORD_ALIGN
7664                    && min_mp == NULL
7665                    && fix->fix_size == 8
7666                    && mp->fix_size < 8)
7667             {
7668               min_mp = mp;
7669               min_address = mp->min_address + fix->fix_size;
7670             }
7671         }
7672     }
7673
7674   /* We need to create a new entry.  */
7675   mp = XNEW (Mnode);
7676   mp->fix_size = fix->fix_size;
7677   mp->mode = fix->mode;
7678   mp->value = fix->value;
7679   mp->refcount = 1;
7680   mp->max_address = minipool_barrier->address + 65536;
7681
7682   mp->min_address = min_address;
7683
7684   if (min_mp == NULL)
7685     {
7686       mp->prev = NULL;
7687       mp->next = minipool_vector_head;
7688
7689       if (mp->next == NULL)
7690         {
7691           minipool_vector_tail = mp;
7692           minipool_vector_label = gen_label_rtx ();
7693         }
7694       else
7695         mp->next->prev = mp;
7696
7697       minipool_vector_head = mp;
7698     }
7699   else
7700     {
7701       mp->next = min_mp->next;
7702       mp->prev = min_mp;
7703       min_mp->next = mp;
7704
7705       if (mp->next != NULL)
7706         mp->next->prev = mp;
7707       else
7708         minipool_vector_tail = mp;
7709     }
7710
7711   /* Save the new entry.  */
7712   min_mp = mp;
7713
7714   if (mp->prev)
7715     mp = mp->prev;
7716   else
7717     mp->offset = 0;
7718
7719   /* Scan over the following entries and adjust their offsets.  */
7720   while (mp->next != NULL)
7721     {
7722       if (mp->next->min_address < mp->min_address + mp->fix_size)
7723         mp->next->min_address = mp->min_address + mp->fix_size;
7724
7725       if (mp->refcount)
7726         mp->next->offset = mp->offset + mp->fix_size;
7727       else
7728         mp->next->offset = mp->offset;
7729
7730       mp = mp->next;
7731     }
7732
7733   return min_mp;
7734 }
7735
7736 static void
7737 assign_minipool_offsets (Mfix *barrier)
7738 {
7739   HOST_WIDE_INT offset = 0;
7740   Mnode *mp;
7741
7742   minipool_barrier = barrier;
7743
7744   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7745     {
7746       mp->offset = offset;
7747
7748       if (mp->refcount > 0)
7749         offset += mp->fix_size;
7750     }
7751 }
7752
7753 /* Output the literal table */
7754 static void
7755 dump_minipool (rtx scan)
7756 {
7757   Mnode * mp;
7758   Mnode * nmp;
7759   int align64 = 0;
7760
7761   if (ARM_DOUBLEWORD_ALIGN)
7762     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7763       if (mp->refcount > 0 && mp->fix_size == 8)
7764         {
7765           align64 = 1;
7766           break;
7767         }
7768
7769   if (dump_file)
7770     fprintf (dump_file,
7771              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7772              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7773
7774   scan = emit_label_after (gen_label_rtx (), scan);
7775   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7776   scan = emit_label_after (minipool_vector_label, scan);
7777
7778   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7779     {
7780       if (mp->refcount > 0)
7781         {
7782           if (dump_file)
7783             {
7784               fprintf (dump_file,
7785                        ";;  Offset %u, min %ld, max %ld ",
7786                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7787                        (unsigned long) mp->max_address);
7788               arm_print_value (dump_file, mp->value);
7789               fputc ('\n', dump_file);
7790             }
7791
7792           switch (mp->fix_size)
7793             {
7794 #ifdef HAVE_consttable_1
7795             case 1:
7796               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7797               break;
7798
7799 #endif
7800 #ifdef HAVE_consttable_2
7801             case 2:
7802               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7803               break;
7804
7805 #endif
7806 #ifdef HAVE_consttable_4
7807             case 4:
7808               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7809               break;
7810
7811 #endif
7812 #ifdef HAVE_consttable_8
7813             case 8:
7814               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7815               break;
7816
7817 #endif
7818             default:
7819               gcc_unreachable ();
7820             }
7821         }
7822
7823       nmp = mp->next;
7824       free (mp);
7825     }
7826
7827   minipool_vector_head = minipool_vector_tail = NULL;
7828   scan = emit_insn_after (gen_consttable_end (), scan);
7829   scan = emit_barrier_after (scan);
7830 }
7831
7832 /* Return the cost of forcibly inserting a barrier after INSN.  */
7833 static int
7834 arm_barrier_cost (rtx insn)
7835 {
7836   /* Basing the location of the pool on the loop depth is preferable,
7837      but at the moment, the basic block information seems to be
7838      corrupt by this stage of the compilation.  */
7839   int base_cost = 50;
7840   rtx next = next_nonnote_insn (insn);
7841
7842   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7843     base_cost -= 20;
7844
7845   switch (GET_CODE (insn))
7846     {
7847     case CODE_LABEL:
7848       /* It will always be better to place the table before the label, rather
7849          than after it.  */
7850       return 50;
7851
7852     case INSN:
7853     case CALL_INSN:
7854       return base_cost;
7855
7856     case JUMP_INSN:
7857       return base_cost - 10;
7858
7859     default:
7860       return base_cost + 10;
7861     }
7862 }
7863
7864 /* Find the best place in the insn stream in the range
7865    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7866    Create the barrier by inserting a jump and add a new fix entry for
7867    it.  */
7868 static Mfix *
7869 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7870 {
7871   HOST_WIDE_INT count = 0;
7872   rtx barrier;
7873   rtx from = fix->insn;
7874   rtx selected = from;
7875   int selected_cost;
7876   HOST_WIDE_INT selected_address;
7877   Mfix * new_fix;
7878   HOST_WIDE_INT max_count = max_address - fix->address;
7879   rtx label = gen_label_rtx ();
7880
7881   selected_cost = arm_barrier_cost (from);
7882   selected_address = fix->address;
7883
7884   while (from && count < max_count)
7885     {
7886       rtx tmp;
7887       int new_cost;
7888
7889       /* This code shouldn't have been called if there was a natural barrier
7890          within range.  */
7891       gcc_assert (GET_CODE (from) != BARRIER);
7892
7893       /* Count the length of this insn.  */
7894       count += get_attr_length (from);
7895
7896       /* If there is a jump table, add its length.  */
7897       tmp = is_jump_table (from);
7898       if (tmp != NULL)
7899         {
7900           count += get_jump_table_size (tmp);
7901
7902           /* Jump tables aren't in a basic block, so base the cost on
7903              the dispatch insn.  If we select this location, we will
7904              still put the pool after the table.  */
7905           new_cost = arm_barrier_cost (from);
7906
7907           if (count < max_count && new_cost <= selected_cost)
7908             {
7909               selected = tmp;
7910               selected_cost = new_cost;
7911               selected_address = fix->address + count;
7912             }
7913
7914           /* Continue after the dispatch table.  */
7915           from = NEXT_INSN (tmp);
7916           continue;
7917         }
7918
7919       new_cost = arm_barrier_cost (from);
7920
7921       if (count < max_count && new_cost <= selected_cost)
7922         {
7923           selected = from;
7924           selected_cost = new_cost;
7925           selected_address = fix->address + count;
7926         }
7927
7928       from = NEXT_INSN (from);
7929     }
7930
7931   /* Create a new JUMP_INSN that branches around a barrier.  */
7932   from = emit_jump_insn_after (gen_jump (label), selected);
7933   JUMP_LABEL (from) = label;
7934   barrier = emit_barrier_after (from);
7935   emit_label_after (label, barrier);
7936
7937   /* Create a minipool barrier entry for the new barrier.  */
7938   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7939   new_fix->insn = barrier;
7940   new_fix->address = selected_address;
7941   new_fix->next = fix->next;
7942   fix->next = new_fix;
7943
7944   return new_fix;
7945 }
7946
7947 /* Record that there is a natural barrier in the insn stream at
7948    ADDRESS.  */
7949 static void
7950 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7951 {
7952   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7953
7954   fix->insn = insn;
7955   fix->address = address;
7956
7957   fix->next = NULL;
7958   if (minipool_fix_head != NULL)
7959     minipool_fix_tail->next = fix;
7960   else
7961     minipool_fix_head = fix;
7962
7963   minipool_fix_tail = fix;
7964 }
7965
7966 /* Record INSN, which will need fixing up to load a value from the
7967    minipool.  ADDRESS is the offset of the insn since the start of the
7968    function; LOC is a pointer to the part of the insn which requires
7969    fixing; VALUE is the constant that must be loaded, which is of type
7970    MODE.  */
7971 static void
7972 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7973                    enum machine_mode mode, rtx value)
7974 {
7975   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7976
7977 #ifdef AOF_ASSEMBLER
7978   /* PIC symbol references need to be converted into offsets into the
7979      based area.  */
7980   /* XXX This shouldn't be done here.  */
7981   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7982     value = aof_pic_entry (value);
7983 #endif /* AOF_ASSEMBLER */
7984
7985   fix->insn = insn;
7986   fix->address = address;
7987   fix->loc = loc;
7988   fix->mode = mode;
7989   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7990   fix->value = value;
7991   fix->forwards = get_attr_pool_range (insn);
7992   fix->backwards = get_attr_neg_pool_range (insn);
7993   fix->minipool = NULL;
7994
7995   /* If an insn doesn't have a range defined for it, then it isn't
7996      expecting to be reworked by this code.  Better to stop now than
7997      to generate duff assembly code.  */
7998   gcc_assert (fix->forwards || fix->backwards);
7999
8000   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
8001      So there might be an empty word before the start of the pool.
8002      Hence we reduce the forward range by 4 to allow for this
8003      possibility.  */
8004   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
8005     fix->forwards -= 4;
8006
8007   if (dump_file)
8008     {
8009       fprintf (dump_file,
8010                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
8011                GET_MODE_NAME (mode),
8012                INSN_UID (insn), (unsigned long) address,
8013                -1 * (long)fix->backwards, (long)fix->forwards);
8014       arm_print_value (dump_file, fix->value);
8015       fprintf (dump_file, "\n");
8016     }
8017
8018   /* Add it to the chain of fixes.  */
8019   fix->next = NULL;
8020
8021   if (minipool_fix_head != NULL)
8022     minipool_fix_tail->next = fix;
8023   else
8024     minipool_fix_head = fix;
8025
8026   minipool_fix_tail = fix;
8027 }
8028
8029 /* Return the cost of synthesizing a 64-bit constant VAL inline.
8030    Returns the number of insns needed, or 99 if we don't know how to
8031    do it.  */
8032 int
8033 arm_const_double_inline_cost (rtx val)
8034 {
8035   rtx lowpart, highpart;
8036   enum machine_mode mode;
8037
8038   mode = GET_MODE (val);
8039
8040   if (mode == VOIDmode)
8041     mode = DImode;
8042
8043   gcc_assert (GET_MODE_SIZE (mode) == 8);
8044
8045   lowpart = gen_lowpart (SImode, val);
8046   highpart = gen_highpart_mode (SImode, mode, val);
8047
8048   gcc_assert (GET_CODE (lowpart) == CONST_INT);
8049   gcc_assert (GET_CODE (highpart) == CONST_INT);
8050
8051   return (arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (lowpart),
8052                             NULL_RTX, NULL_RTX, 0, 0)
8053           + arm_gen_constant (SET, SImode, NULL_RTX, INTVAL (highpart),
8054                               NULL_RTX, NULL_RTX, 0, 0));
8055 }
8056
8057 /* Return true if it is worthwhile to split a 64-bit constant into two
8058    32-bit operations.  This is the case if optimizing for size, or
8059    if we have load delay slots, or if one 32-bit part can be done with
8060    a single data operation.  */
8061 bool
8062 arm_const_double_by_parts (rtx val)
8063 {
8064   enum machine_mode mode = GET_MODE (val);
8065   rtx part;
8066
8067   if (optimize_size || arm_ld_sched)
8068     return true;
8069
8070   if (mode == VOIDmode)
8071     mode = DImode;
8072
8073   part = gen_highpart_mode (SImode, mode, val);
8074
8075   gcc_assert (GET_CODE (part) == CONST_INT);
8076
8077   if (const_ok_for_arm (INTVAL (part))
8078       || const_ok_for_arm (~INTVAL (part)))
8079     return true;
8080
8081   part = gen_lowpart (SImode, val);
8082
8083   gcc_assert (GET_CODE (part) == CONST_INT);
8084
8085   if (const_ok_for_arm (INTVAL (part))
8086       || const_ok_for_arm (~INTVAL (part)))
8087     return true;
8088
8089   return false;
8090 }
8091
8092 /* Scan INSN and note any of its operands that need fixing.
8093    If DO_PUSHES is false we do not actually push any of the fixups
8094    needed.  The function returns TRUE if any fixups were needed/pushed.
8095    This is used by arm_memory_load_p() which needs to know about loads
8096    of constants that will be converted into minipool loads.  */
8097 static bool
8098 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
8099 {
8100   bool result = false;
8101   int opno;
8102
8103   extract_insn (insn);
8104
8105   if (!constrain_operands (1))
8106     fatal_insn_not_found (insn);
8107
8108   if (recog_data.n_alternatives == 0)
8109     return false;
8110
8111   /* Fill in recog_op_alt with information about the constraints of
8112      this insn.  */
8113   preprocess_constraints ();
8114
8115   for (opno = 0; opno < recog_data.n_operands; opno++)
8116     {
8117       /* Things we need to fix can only occur in inputs.  */
8118       if (recog_data.operand_type[opno] != OP_IN)
8119         continue;
8120
8121       /* If this alternative is a memory reference, then any mention
8122          of constants in this alternative is really to fool reload
8123          into allowing us to accept one there.  We need to fix them up
8124          now so that we output the right code.  */
8125       if (recog_op_alt[opno][which_alternative].memory_ok)
8126         {
8127           rtx op = recog_data.operand[opno];
8128
8129           if (CONSTANT_P (op))
8130             {
8131               if (do_pushes)
8132                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
8133                                    recog_data.operand_mode[opno], op);
8134               result = true;
8135             }
8136           else if (GET_CODE (op) == MEM
8137                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
8138                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
8139             {
8140               if (do_pushes)
8141                 {
8142                   rtx cop = avoid_constant_pool_reference (op);
8143
8144                   /* Casting the address of something to a mode narrower
8145                      than a word can cause avoid_constant_pool_reference()
8146                      to return the pool reference itself.  That's no good to
8147                      us here.  Lets just hope that we can use the
8148                      constant pool value directly.  */
8149                   if (op == cop)
8150                     cop = get_pool_constant (XEXP (op, 0));
8151
8152                   push_minipool_fix (insn, address,
8153                                      recog_data.operand_loc[opno],
8154                                      recog_data.operand_mode[opno], cop);
8155                 }
8156
8157               result = true;
8158             }
8159         }
8160     }
8161
8162   return result;
8163 }
8164
8165 /* Gcc puts the pool in the wrong place for ARM, since we can only
8166    load addresses a limited distance around the pc.  We do some
8167    special munging to move the constant pool values to the correct
8168    point in the code.  */
8169 static void
8170 arm_reorg (void)
8171 {
8172   rtx insn;
8173   HOST_WIDE_INT address = 0;
8174   Mfix * fix;
8175
8176   minipool_fix_head = minipool_fix_tail = NULL;
8177
8178   /* The first insn must always be a note, or the code below won't
8179      scan it properly.  */
8180   insn = get_insns ();
8181   gcc_assert (GET_CODE (insn) == NOTE);
8182
8183   /* Scan all the insns and record the operands that will need fixing.  */
8184   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
8185     {
8186       if (TARGET_CIRRUS_FIX_INVALID_INSNS
8187           && (arm_cirrus_insn_p (insn)
8188               || GET_CODE (insn) == JUMP_INSN
8189               || arm_memory_load_p (insn)))
8190         cirrus_reorg (insn);
8191
8192       if (GET_CODE (insn) == BARRIER)
8193         push_minipool_barrier (insn, address);
8194       else if (INSN_P (insn))
8195         {
8196           rtx table;
8197
8198           note_invalid_constants (insn, address, true);
8199           address += get_attr_length (insn);
8200
8201           /* If the insn is a vector jump, add the size of the table
8202              and skip the table.  */
8203           if ((table = is_jump_table (insn)) != NULL)
8204             {
8205               address += get_jump_table_size (table);
8206               insn = table;
8207             }
8208         }
8209     }
8210
8211   fix = minipool_fix_head;
8212
8213   /* Now scan the fixups and perform the required changes.  */
8214   while (fix)
8215     {
8216       Mfix * ftmp;
8217       Mfix * fdel;
8218       Mfix *  last_added_fix;
8219       Mfix * last_barrier = NULL;
8220       Mfix * this_fix;
8221
8222       /* Skip any further barriers before the next fix.  */
8223       while (fix && GET_CODE (fix->insn) == BARRIER)
8224         fix = fix->next;
8225
8226       /* No more fixes.  */
8227       if (fix == NULL)
8228         break;
8229
8230       last_added_fix = NULL;
8231
8232       for (ftmp = fix; ftmp; ftmp = ftmp->next)
8233         {
8234           if (GET_CODE (ftmp->insn) == BARRIER)
8235             {
8236               if (ftmp->address >= minipool_vector_head->max_address)
8237                 break;
8238
8239               last_barrier = ftmp;
8240             }
8241           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
8242             break;
8243
8244           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
8245         }
8246
8247       /* If we found a barrier, drop back to that; any fixes that we
8248          could have reached but come after the barrier will now go in
8249          the next mini-pool.  */
8250       if (last_barrier != NULL)
8251         {
8252           /* Reduce the refcount for those fixes that won't go into this
8253              pool after all.  */
8254           for (fdel = last_barrier->next;
8255                fdel && fdel != ftmp;
8256                fdel = fdel->next)
8257             {
8258               fdel->minipool->refcount--;
8259               fdel->minipool = NULL;
8260             }
8261
8262           ftmp = last_barrier;
8263         }
8264       else
8265         {
8266           /* ftmp is first fix that we can't fit into this pool and
8267              there no natural barriers that we could use.  Insert a
8268              new barrier in the code somewhere between the previous
8269              fix and this one, and arrange to jump around it.  */
8270           HOST_WIDE_INT max_address;
8271
8272           /* The last item on the list of fixes must be a barrier, so
8273              we can never run off the end of the list of fixes without
8274              last_barrier being set.  */
8275           gcc_assert (ftmp);
8276
8277           max_address = minipool_vector_head->max_address;
8278           /* Check that there isn't another fix that is in range that
8279              we couldn't fit into this pool because the pool was
8280              already too large: we need to put the pool before such an
8281              instruction.  */
8282           if (ftmp->address < max_address)
8283             max_address = ftmp->address;
8284
8285           last_barrier = create_fix_barrier (last_added_fix, max_address);
8286         }
8287
8288       assign_minipool_offsets (last_barrier);
8289
8290       while (ftmp)
8291         {
8292           if (GET_CODE (ftmp->insn) != BARRIER
8293               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
8294                   == NULL))
8295             break;
8296
8297           ftmp = ftmp->next;
8298         }
8299
8300       /* Scan over the fixes we have identified for this pool, fixing them
8301          up and adding the constants to the pool itself.  */
8302       for (this_fix = fix; this_fix && ftmp != this_fix;
8303            this_fix = this_fix->next)
8304         if (GET_CODE (this_fix->insn) != BARRIER)
8305           {
8306             rtx addr
8307               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
8308                                                   minipool_vector_label),
8309                                this_fix->minipool->offset);
8310             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
8311           }
8312
8313       dump_minipool (last_barrier->insn);
8314       fix = ftmp;
8315     }
8316
8317   /* From now on we must synthesize any constants that we can't handle
8318      directly.  This can happen if the RTL gets split during final
8319      instruction generation.  */
8320   after_arm_reorg = 1;
8321
8322   /* Free the minipool memory.  */
8323   obstack_free (&minipool_obstack, minipool_startobj);
8324 }
8325 \f
8326 /* Routines to output assembly language.  */
8327
8328 /* If the rtx is the correct value then return the string of the number.
8329    In this way we can ensure that valid double constants are generated even
8330    when cross compiling.  */
8331 const char *
8332 fp_immediate_constant (rtx x)
8333 {
8334   REAL_VALUE_TYPE r;
8335   int i;
8336
8337   if (!fp_consts_inited)
8338     init_fp_table ();
8339
8340   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8341   for (i = 0; i < 8; i++)
8342     if (REAL_VALUES_EQUAL (r, values_fp[i]))
8343       return strings_fp[i];
8344
8345   gcc_unreachable ();
8346 }
8347
8348 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
8349 static const char *
8350 fp_const_from_val (REAL_VALUE_TYPE *r)
8351 {
8352   int i;
8353
8354   if (!fp_consts_inited)
8355     init_fp_table ();
8356
8357   for (i = 0; i < 8; i++)
8358     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
8359       return strings_fp[i];
8360
8361   gcc_unreachable ();
8362 }
8363
8364 /* Output the operands of a LDM/STM instruction to STREAM.
8365    MASK is the ARM register set mask of which only bits 0-15 are important.
8366    REG is the base register, either the frame pointer or the stack pointer,
8367    INSTR is the possibly suffixed load or store instruction.  */
8368
8369 static void
8370 print_multi_reg (FILE *stream, const char *instr, unsigned reg,
8371                  unsigned long mask)
8372 {
8373   unsigned i;
8374   bool not_first = FALSE;
8375
8376   fputc ('\t', stream);
8377   asm_fprintf (stream, instr, reg);
8378   fputs (", {", stream);
8379
8380   for (i = 0; i <= LAST_ARM_REGNUM; i++)
8381     if (mask & (1 << i))
8382       {
8383         if (not_first)
8384           fprintf (stream, ", ");
8385
8386         asm_fprintf (stream, "%r", i);
8387         not_first = TRUE;
8388       }
8389
8390   fprintf (stream, "}\n");
8391 }
8392
8393
8394 /* Output a FLDMX instruction to STREAM.
8395    BASE if the register containing the address.
8396    REG and COUNT specify the register range.
8397    Extra registers may be added to avoid hardware bugs.  */
8398
8399 static void
8400 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
8401 {
8402   int i;
8403
8404   /* Workaround ARM10 VFPr1 bug.  */
8405   if (count == 2 && !arm_arch6)
8406     {
8407       if (reg == 15)
8408         reg--;
8409       count++;
8410     }
8411
8412   fputc ('\t', stream);
8413   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
8414
8415   for (i = reg; i < reg + count; i++)
8416     {
8417       if (i > reg)
8418         fputs (", ", stream);
8419       asm_fprintf (stream, "d%d", i);
8420     }
8421   fputs ("}\n", stream);
8422
8423 }
8424
8425
8426 /* Output the assembly for a store multiple.  */
8427
8428 const char *
8429 vfp_output_fstmx (rtx * operands)
8430 {
8431   char pattern[100];
8432   int p;
8433   int base;
8434   int i;
8435
8436   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
8437   p = strlen (pattern);
8438
8439   gcc_assert (GET_CODE (operands[1]) == REG);
8440
8441   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
8442   for (i = 1; i < XVECLEN (operands[2], 0); i++)
8443     {
8444       p += sprintf (&pattern[p], ", d%d", base + i);
8445     }
8446   strcpy (&pattern[p], "}");
8447
8448   output_asm_insn (pattern, operands);
8449   return "";
8450 }
8451
8452
8453 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
8454    number of bytes pushed.  */
8455
8456 static int
8457 vfp_emit_fstmx (int base_reg, int count)
8458 {
8459   rtx par;
8460   rtx dwarf;
8461   rtx tmp, reg;
8462   int i;
8463
8464   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
8465      register pairs are stored by a store multiple insn.  We avoid this
8466      by pushing an extra pair.  */
8467   if (count == 2 && !arm_arch6)
8468     {
8469       if (base_reg == LAST_VFP_REGNUM - 3)
8470         base_reg -= 2;
8471       count++;
8472     }
8473
8474   /* ??? The frame layout is implementation defined.  We describe
8475      standard format 1 (equivalent to a FSTMD insn and unused pad word).
8476      We really need some way of representing the whole block so that the
8477      unwinder can figure it out at runtime.  */
8478   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
8479   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
8480
8481   reg = gen_rtx_REG (DFmode, base_reg);
8482   base_reg += 2;
8483
8484   XVECEXP (par, 0, 0)
8485     = gen_rtx_SET (VOIDmode,
8486                    gen_frame_mem (BLKmode,
8487                                   gen_rtx_PRE_DEC (BLKmode,
8488                                                    stack_pointer_rtx)),
8489                    gen_rtx_UNSPEC (BLKmode,
8490                                    gen_rtvec (1, reg),
8491                                    UNSPEC_PUSH_MULT));
8492
8493   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8494                      plus_constant (stack_pointer_rtx, -(count * 8 + 4)));
8495   RTX_FRAME_RELATED_P (tmp) = 1;
8496   XVECEXP (dwarf, 0, 0) = tmp;
8497
8498   tmp = gen_rtx_SET (VOIDmode,
8499                      gen_frame_mem (DFmode, stack_pointer_rtx),
8500                      reg);
8501   RTX_FRAME_RELATED_P (tmp) = 1;
8502   XVECEXP (dwarf, 0, 1) = tmp;
8503
8504   for (i = 1; i < count; i++)
8505     {
8506       reg = gen_rtx_REG (DFmode, base_reg);
8507       base_reg += 2;
8508       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
8509
8510       tmp = gen_rtx_SET (VOIDmode,
8511                          gen_frame_mem (DFmode,
8512                                         plus_constant (stack_pointer_rtx,
8513                                                        i * 8)),
8514                          reg);
8515       RTX_FRAME_RELATED_P (tmp) = 1;
8516       XVECEXP (dwarf, 0, i + 1) = tmp;
8517     }
8518
8519   par = emit_insn (par);
8520   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
8521                                        REG_NOTES (par));
8522   RTX_FRAME_RELATED_P (par) = 1;
8523
8524   return count * 8 + 4;
8525 }
8526
8527
8528 /* Output a 'call' insn.  */
8529 const char *
8530 output_call (rtx *operands)
8531 {
8532   gcc_assert (!arm_arch5); /* Patterns should call blx <reg> directly.  */
8533
8534   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
8535   if (REGNO (operands[0]) == LR_REGNUM)
8536     {
8537       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
8538       output_asm_insn ("mov%?\t%0, %|lr", operands);
8539     }
8540
8541   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8542
8543   if (TARGET_INTERWORK || arm_arch4t)
8544     output_asm_insn ("bx%?\t%0", operands);
8545   else
8546     output_asm_insn ("mov%?\t%|pc, %0", operands);
8547
8548   return "";
8549 }
8550
8551 /* Output a 'call' insn that is a reference in memory.  */
8552 const char *
8553 output_call_mem (rtx *operands)
8554 {
8555   if (TARGET_INTERWORK && !arm_arch5)
8556     {
8557       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8558       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8559       output_asm_insn ("bx%?\t%|ip", operands);
8560     }
8561   else if (regno_use_in (LR_REGNUM, operands[0]))
8562     {
8563       /* LR is used in the memory address.  We load the address in the
8564          first instruction.  It's safe to use IP as the target of the
8565          load since the call will kill it anyway.  */
8566       output_asm_insn ("ldr%?\t%|ip, %0", operands);
8567       if (arm_arch5)
8568         output_asm_insn ("blx%?\t%|ip", operands);
8569       else
8570         {
8571           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8572           if (arm_arch4t)
8573             output_asm_insn ("bx%?\t%|ip", operands);
8574           else
8575             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
8576         }
8577     }
8578   else
8579     {
8580       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
8581       output_asm_insn ("ldr%?\t%|pc, %0", operands);
8582     }
8583
8584   return "";
8585 }
8586
8587
8588 /* Output a move from arm registers to an fpa registers.
8589    OPERANDS[0] is an fpa register.
8590    OPERANDS[1] is the first registers of an arm register pair.  */
8591 const char *
8592 output_mov_long_double_fpa_from_arm (rtx *operands)
8593 {
8594   int arm_reg0 = REGNO (operands[1]);
8595   rtx ops[3];
8596
8597   gcc_assert (arm_reg0 != IP_REGNUM);
8598
8599   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8600   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8601   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8602
8603   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
8604   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
8605
8606   return "";
8607 }
8608
8609 /* Output a move from an fpa register to arm registers.
8610    OPERANDS[0] is the first registers of an arm register pair.
8611    OPERANDS[1] is an fpa register.  */
8612 const char *
8613 output_mov_long_double_arm_from_fpa (rtx *operands)
8614 {
8615   int arm_reg0 = REGNO (operands[0]);
8616   rtx ops[3];
8617
8618   gcc_assert (arm_reg0 != IP_REGNUM);
8619
8620   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8621   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8622   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
8623
8624   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
8625   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
8626   return "";
8627 }
8628
8629 /* Output a move from arm registers to arm registers of a long double
8630    OPERANDS[0] is the destination.
8631    OPERANDS[1] is the source.  */
8632 const char *
8633 output_mov_long_double_arm_from_arm (rtx *operands)
8634 {
8635   /* We have to be careful here because the two might overlap.  */
8636   int dest_start = REGNO (operands[0]);
8637   int src_start = REGNO (operands[1]);
8638   rtx ops[2];
8639   int i;
8640
8641   if (dest_start < src_start)
8642     {
8643       for (i = 0; i < 3; i++)
8644         {
8645           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8646           ops[1] = gen_rtx_REG (SImode, src_start + i);
8647           output_asm_insn ("mov%?\t%0, %1", ops);
8648         }
8649     }
8650   else
8651     {
8652       for (i = 2; i >= 0; i--)
8653         {
8654           ops[0] = gen_rtx_REG (SImode, dest_start + i);
8655           ops[1] = gen_rtx_REG (SImode, src_start + i);
8656           output_asm_insn ("mov%?\t%0, %1", ops);
8657         }
8658     }
8659
8660   return "";
8661 }
8662
8663
8664 /* Output a move from arm registers to an fpa registers.
8665    OPERANDS[0] is an fpa register.
8666    OPERANDS[1] is the first registers of an arm register pair.  */
8667 const char *
8668 output_mov_double_fpa_from_arm (rtx *operands)
8669 {
8670   int arm_reg0 = REGNO (operands[1]);
8671   rtx ops[2];
8672
8673   gcc_assert (arm_reg0 != IP_REGNUM);
8674
8675   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8676   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8677   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
8678   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
8679   return "";
8680 }
8681
8682 /* Output a move from an fpa register to arm registers.
8683    OPERANDS[0] is the first registers of an arm register pair.
8684    OPERANDS[1] is an fpa register.  */
8685 const char *
8686 output_mov_double_arm_from_fpa (rtx *operands)
8687 {
8688   int arm_reg0 = REGNO (operands[0]);
8689   rtx ops[2];
8690
8691   gcc_assert (arm_reg0 != IP_REGNUM);
8692
8693   ops[0] = gen_rtx_REG (SImode, arm_reg0);
8694   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
8695   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
8696   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
8697   return "";
8698 }
8699
8700 /* Output a move between double words.
8701    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
8702    or MEM<-REG and all MEMs must be offsettable addresses.  */
8703 const char *
8704 output_move_double (rtx *operands)
8705 {
8706   enum rtx_code code0 = GET_CODE (operands[0]);
8707   enum rtx_code code1 = GET_CODE (operands[1]);
8708   rtx otherops[3];
8709
8710   if (code0 == REG)
8711     {
8712       int reg0 = REGNO (operands[0]);
8713
8714       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8715
8716       gcc_assert (code1 == MEM);  /* Constraints should ensure this.  */
8717
8718       switch (GET_CODE (XEXP (operands[1], 0)))
8719         {
8720         case REG:
8721           output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8722           break;
8723
8724         case PRE_INC:
8725           gcc_assert (TARGET_LDRD);
8726           output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8727           break;
8728
8729         case PRE_DEC:
8730           output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8731           break;
8732
8733         case POST_INC:
8734           output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8735           break;
8736
8737         case POST_DEC:
8738           gcc_assert (TARGET_LDRD);
8739           output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8740           break;
8741
8742         case PRE_MODIFY:
8743         case POST_MODIFY:
8744           otherops[0] = operands[0];
8745           otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8746           otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8747
8748           if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8749             {
8750               if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8751                 {
8752                   /* Registers overlap so split out the increment.  */
8753                   output_asm_insn ("add%?\t%1, %1, %2", otherops);
8754                   output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8755                 }
8756               else
8757                 output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8758             }
8759           else
8760             {
8761               /* We only allow constant increments, so this is safe.  */
8762               output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8763             }
8764           break;
8765
8766         case LABEL_REF:
8767         case CONST:
8768           output_asm_insn ("adr%?\t%0, %1", operands);
8769           output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8770           break;
8771
8772         default:
8773           if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8774                                GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8775             {
8776               otherops[0] = operands[0];
8777               otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8778               otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8779
8780               if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8781                 {
8782                   if (GET_CODE (otherops[2]) == CONST_INT)
8783                     {
8784                       switch ((int) INTVAL (otherops[2]))
8785                         {
8786                         case -8:
8787                           output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8788                           return "";
8789                         case -4:
8790                           output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8791                           return "";
8792                         case 4:
8793                           output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8794                           return "";
8795                         }
8796                     }
8797                   if (TARGET_LDRD
8798                       && (GET_CODE (otherops[2]) == REG
8799                           || (GET_CODE (otherops[2]) == CONST_INT
8800                               && INTVAL (otherops[2]) > -256
8801                               && INTVAL (otherops[2]) < 256)))
8802                     {
8803                       if (reg_overlap_mentioned_p (otherops[0],
8804                                                    otherops[2]))
8805                         {
8806                           /* Swap base and index registers over to
8807                              avoid a conflict.  */
8808                           otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8809                           otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8810                         }
8811                       /* If both registers conflict, it will usually
8812                          have been fixed by a splitter.  */
8813                       if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8814                         {
8815                           output_asm_insn ("add%?\t%1, %1, %2", otherops);
8816                           output_asm_insn ("ldr%?d\t%0, [%1]",
8817                                            otherops);
8818                         }
8819                       else
8820                         output_asm_insn ("ldr%?d\t%0, [%1, %2]", otherops);
8821                       return "";
8822                     }
8823
8824                   if (GET_CODE (otherops[2]) == CONST_INT)
8825                     {
8826                       if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8827                         output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8828                       else
8829                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8830                     }
8831                   else
8832                     output_asm_insn ("add%?\t%0, %1, %2", otherops);
8833                 }
8834               else
8835                 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8836
8837               return "ldm%?ia\t%0, %M0";
8838             }
8839           else
8840             {
8841               otherops[1] = adjust_address (operands[1], SImode, 4);
8842               /* Take care of overlapping base/data reg.  */
8843               if (reg_mentioned_p (operands[0], operands[1]))
8844                 {
8845                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8846                   output_asm_insn ("ldr%?\t%0, %1", operands);
8847                 }
8848               else
8849                 {
8850                   output_asm_insn ("ldr%?\t%0, %1", operands);
8851                   output_asm_insn ("ldr%?\t%0, %1", otherops);
8852                 }
8853             }
8854         }
8855     }
8856   else
8857     {
8858       /* Constraints should ensure this.  */
8859       gcc_assert (code0 == MEM && code1 == REG);
8860       gcc_assert (REGNO (operands[1]) != IP_REGNUM);
8861
8862       switch (GET_CODE (XEXP (operands[0], 0)))
8863         {
8864         case REG:
8865           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8866           break;
8867
8868         case PRE_INC:
8869           gcc_assert (TARGET_LDRD);
8870           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8871           break;
8872
8873         case PRE_DEC:
8874           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8875           break;
8876
8877         case POST_INC:
8878           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8879           break;
8880
8881         case POST_DEC:
8882           gcc_assert (TARGET_LDRD);
8883           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8884           break;
8885
8886         case PRE_MODIFY:
8887         case POST_MODIFY:
8888           otherops[0] = operands[1];
8889           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8890           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8891
8892           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8893             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8894           else
8895             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8896           break;
8897
8898         case PLUS:
8899           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8900           if (GET_CODE (otherops[2]) == CONST_INT)
8901             {
8902               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8903                 {
8904                 case -8:
8905                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8906                   return "";
8907
8908                 case -4:
8909                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8910                   return "";
8911
8912                 case 4:
8913                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8914                   return "";
8915                 }
8916             }
8917           if (TARGET_LDRD
8918               && (GET_CODE (otherops[2]) == REG
8919                   || (GET_CODE (otherops[2]) == CONST_INT
8920                       && INTVAL (otherops[2]) > -256
8921                       && INTVAL (otherops[2]) < 256)))
8922             {
8923               otherops[0] = operands[1];
8924               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8925               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8926               return "";
8927             }
8928           /* Fall through */
8929
8930         default:
8931           otherops[0] = adjust_address (operands[0], SImode, 4);
8932           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8933           output_asm_insn ("str%?\t%1, %0", operands);
8934           output_asm_insn ("str%?\t%1, %0", otherops);
8935         }
8936     }
8937
8938   return "";
8939 }
8940
8941 /* Output an ADD r, s, #n where n may be too big for one instruction.
8942    If adding zero to one register, output nothing.  */
8943 const char *
8944 output_add_immediate (rtx *operands)
8945 {
8946   HOST_WIDE_INT n = INTVAL (operands[2]);
8947
8948   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8949     {
8950       if (n < 0)
8951         output_multi_immediate (operands,
8952                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8953                                 -n);
8954       else
8955         output_multi_immediate (operands,
8956                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8957                                 n);
8958     }
8959
8960   return "";
8961 }
8962
8963 /* Output a multiple immediate operation.
8964    OPERANDS is the vector of operands referred to in the output patterns.
8965    INSTR1 is the output pattern to use for the first constant.
8966    INSTR2 is the output pattern to use for subsequent constants.
8967    IMMED_OP is the index of the constant slot in OPERANDS.
8968    N is the constant value.  */
8969 static const char *
8970 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8971                         int immed_op, HOST_WIDE_INT n)
8972 {
8973 #if HOST_BITS_PER_WIDE_INT > 32
8974   n &= 0xffffffff;
8975 #endif
8976
8977   if (n == 0)
8978     {
8979       /* Quick and easy output.  */
8980       operands[immed_op] = const0_rtx;
8981       output_asm_insn (instr1, operands);
8982     }
8983   else
8984     {
8985       int i;
8986       const char * instr = instr1;
8987
8988       /* Note that n is never zero here (which would give no output).  */
8989       for (i = 0; i < 32; i += 2)
8990         {
8991           if (n & (3 << i))
8992             {
8993               operands[immed_op] = GEN_INT (n & (255 << i));
8994               output_asm_insn (instr, operands);
8995               instr = instr2;
8996               i += 6;
8997             }
8998         }
8999     }
9000
9001   return "";
9002 }
9003
9004 /* Return the appropriate ARM instruction for the operation code.
9005    The returned result should not be overwritten.  OP is the rtx of the
9006    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
9007    was shifted.  */
9008 const char *
9009 arithmetic_instr (rtx op, int shift_first_arg)
9010 {
9011   switch (GET_CODE (op))
9012     {
9013     case PLUS:
9014       return "add";
9015
9016     case MINUS:
9017       return shift_first_arg ? "rsb" : "sub";
9018
9019     case IOR:
9020       return "orr";
9021
9022     case XOR:
9023       return "eor";
9024
9025     case AND:
9026       return "and";
9027
9028     default:
9029       gcc_unreachable ();
9030     }
9031 }
9032
9033 /* Ensure valid constant shifts and return the appropriate shift mnemonic
9034    for the operation code.  The returned result should not be overwritten.
9035    OP is the rtx code of the shift.
9036    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
9037    shift.  */
9038 static const char *
9039 shift_op (rtx op, HOST_WIDE_INT *amountp)
9040 {
9041   const char * mnem;
9042   enum rtx_code code = GET_CODE (op);
9043
9044   switch (GET_CODE (XEXP (op, 1)))
9045     {
9046     case REG:
9047     case SUBREG:
9048       *amountp = -1;
9049       break;
9050
9051     case CONST_INT:
9052       *amountp = INTVAL (XEXP (op, 1));
9053       break;
9054
9055     default:
9056       gcc_unreachable ();
9057     }
9058
9059   switch (code)
9060     {
9061     case ASHIFT:
9062       mnem = "asl";
9063       break;
9064
9065     case ASHIFTRT:
9066       mnem = "asr";
9067       break;
9068
9069     case LSHIFTRT:
9070       mnem = "lsr";
9071       break;
9072
9073     case ROTATE:
9074       gcc_assert (*amountp != -1);
9075       *amountp = 32 - *amountp;
9076
9077       /* Fall through.  */
9078
9079     case ROTATERT:
9080       mnem = "ror";
9081       break;
9082
9083     case MULT:
9084       /* We never have to worry about the amount being other than a
9085          power of 2, since this case can never be reloaded from a reg.  */
9086       gcc_assert (*amountp != -1);
9087       *amountp = int_log2 (*amountp);
9088       return "asl";
9089
9090     default:
9091       gcc_unreachable ();
9092     }
9093
9094   if (*amountp != -1)
9095     {
9096       /* This is not 100% correct, but follows from the desire to merge
9097          multiplication by a power of 2 with the recognizer for a
9098          shift.  >=32 is not a valid shift for "asl", so we must try and
9099          output a shift that produces the correct arithmetical result.
9100          Using lsr #32 is identical except for the fact that the carry bit
9101          is not set correctly if we set the flags; but we never use the
9102          carry bit from such an operation, so we can ignore that.  */
9103       if (code == ROTATERT)
9104         /* Rotate is just modulo 32.  */
9105         *amountp &= 31;
9106       else if (*amountp != (*amountp & 31))
9107         {
9108           if (code == ASHIFT)
9109             mnem = "lsr";
9110           *amountp = 32;
9111         }
9112
9113       /* Shifts of 0 are no-ops.  */
9114       if (*amountp == 0)
9115         return NULL;
9116     }
9117
9118   return mnem;
9119 }
9120
9121 /* Obtain the shift from the POWER of two.  */
9122
9123 static HOST_WIDE_INT
9124 int_log2 (HOST_WIDE_INT power)
9125 {
9126   HOST_WIDE_INT shift = 0;
9127
9128   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
9129     {
9130       gcc_assert (shift <= 31);
9131       shift++;
9132     }
9133
9134   return shift;
9135 }
9136
9137 /* Output a .ascii pseudo-op, keeping track of lengths.  This is
9138    because /bin/as is horribly restrictive.  The judgement about
9139    whether or not each character is 'printable' (and can be output as
9140    is) or not (and must be printed with an octal escape) must be made
9141    with reference to the *host* character set -- the situation is
9142    similar to that discussed in the comments above pp_c_char in
9143    c-pretty-print.c.  */
9144
9145 #define MAX_ASCII_LEN 51
9146
9147 void
9148 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
9149 {
9150   int i;
9151   int len_so_far = 0;
9152
9153   fputs ("\t.ascii\t\"", stream);
9154
9155   for (i = 0; i < len; i++)
9156     {
9157       int c = p[i];
9158
9159       if (len_so_far >= MAX_ASCII_LEN)
9160         {
9161           fputs ("\"\n\t.ascii\t\"", stream);
9162           len_so_far = 0;
9163         }
9164
9165       if (ISPRINT (c))
9166         {
9167           if (c == '\\' || c == '\"')
9168             {
9169               putc ('\\', stream);
9170               len_so_far++;
9171             }
9172           putc (c, stream);
9173           len_so_far++;
9174         }
9175       else
9176         {
9177           fprintf (stream, "\\%03o", c);
9178           len_so_far += 4;
9179         }
9180     }
9181
9182   fputs ("\"\n", stream);
9183 }
9184 \f
9185 /* Compute the register save mask for registers 0 through 12
9186    inclusive.  This code is used by arm_compute_save_reg_mask.  */
9187
9188 static unsigned long
9189 arm_compute_save_reg0_reg12_mask (void)
9190 {
9191   unsigned long func_type = arm_current_func_type ();
9192   unsigned long save_reg_mask = 0;
9193   unsigned int reg;
9194
9195   if (IS_INTERRUPT (func_type))
9196     {
9197       unsigned int max_reg;
9198       /* Interrupt functions must not corrupt any registers,
9199          even call clobbered ones.  If this is a leaf function
9200          we can just examine the registers used by the RTL, but
9201          otherwise we have to assume that whatever function is
9202          called might clobber anything, and so we have to save
9203          all the call-clobbered registers as well.  */
9204       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
9205         /* FIQ handlers have registers r8 - r12 banked, so
9206            we only need to check r0 - r7, Normal ISRs only
9207            bank r14 and r15, so we must check up to r12.
9208            r13 is the stack pointer which is always preserved,
9209            so we do not need to consider it here.  */
9210         max_reg = 7;
9211       else
9212         max_reg = 12;
9213
9214       for (reg = 0; reg <= max_reg; reg++)
9215         if (regs_ever_live[reg]
9216             || (! current_function_is_leaf && call_used_regs [reg]))
9217           save_reg_mask |= (1 << reg);
9218
9219       /* Also save the pic base register if necessary.  */
9220       if (flag_pic
9221           && !TARGET_SINGLE_PIC_BASE
9222           && arm_pic_register != INVALID_REGNUM
9223           && current_function_uses_pic_offset_table)
9224         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9225     }
9226   else
9227     {
9228       /* In the normal case we only need to save those registers
9229          which are call saved and which are used by this function.  */
9230       for (reg = 0; reg <= 10; reg++)
9231         if (regs_ever_live[reg] && ! call_used_regs [reg])
9232           save_reg_mask |= (1 << reg);
9233
9234       /* Handle the frame pointer as a special case.  */
9235       if (! TARGET_APCS_FRAME
9236           && ! frame_pointer_needed
9237           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
9238           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
9239         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
9240
9241       /* If we aren't loading the PIC register,
9242          don't stack it even though it may be live.  */
9243       if (flag_pic
9244           && !TARGET_SINGLE_PIC_BASE
9245           && arm_pic_register != INVALID_REGNUM
9246           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
9247               || current_function_uses_pic_offset_table))
9248         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9249     }
9250
9251   /* Save registers so the exception handler can modify them.  */
9252   if (current_function_calls_eh_return)
9253     {
9254       unsigned int i;
9255
9256       for (i = 0; ; i++)
9257         {
9258           reg = EH_RETURN_DATA_REGNO (i);
9259           if (reg == INVALID_REGNUM)
9260             break;
9261           save_reg_mask |= 1 << reg;
9262         }
9263     }
9264
9265   return save_reg_mask;
9266 }
9267
9268 /* Compute a bit mask of which registers need to be
9269    saved on the stack for the current function.  */
9270
9271 static unsigned long
9272 arm_compute_save_reg_mask (void)
9273 {
9274   unsigned int save_reg_mask = 0;
9275   unsigned long func_type = arm_current_func_type ();
9276
9277   if (IS_NAKED (func_type))
9278     /* This should never really happen.  */
9279     return 0;
9280
9281   /* If we are creating a stack frame, then we must save the frame pointer,
9282      IP (which will hold the old stack pointer), LR and the PC.  */
9283   if (frame_pointer_needed)
9284     save_reg_mask |=
9285       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
9286       | (1 << IP_REGNUM)
9287       | (1 << LR_REGNUM)
9288       | (1 << PC_REGNUM);
9289
9290   /* Volatile functions do not return, so there
9291      is no need to save any other registers.  */
9292   if (IS_VOLATILE (func_type))
9293     return save_reg_mask;
9294
9295   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
9296
9297   /* Decide if we need to save the link register.
9298      Interrupt routines have their own banked link register,
9299      so they never need to save it.
9300      Otherwise if we do not use the link register we do not need to save
9301      it.  If we are pushing other registers onto the stack however, we
9302      can save an instruction in the epilogue by pushing the link register
9303      now and then popping it back into the PC.  This incurs extra memory
9304      accesses though, so we only do it when optimizing for size, and only
9305      if we know that we will not need a fancy return sequence.  */
9306   if (regs_ever_live [LR_REGNUM]
9307           || (save_reg_mask
9308               && optimize_size
9309               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9310               && !current_function_calls_eh_return))
9311     save_reg_mask |= 1 << LR_REGNUM;
9312
9313   if (cfun->machine->lr_save_eliminated)
9314     save_reg_mask &= ~ (1 << LR_REGNUM);
9315
9316   if (TARGET_REALLY_IWMMXT
9317       && ((bit_count (save_reg_mask)
9318            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
9319     {
9320       unsigned int reg;
9321
9322       /* The total number of registers that are going to be pushed
9323          onto the stack is odd.  We need to ensure that the stack
9324          is 64-bit aligned before we start to save iWMMXt registers,
9325          and also before we start to create locals.  (A local variable
9326          might be a double or long long which we will load/store using
9327          an iWMMXt instruction).  Therefore we need to push another
9328          ARM register, so that the stack will be 64-bit aligned.  We
9329          try to avoid using the arg registers (r0 -r3) as they might be
9330          used to pass values in a tail call.  */
9331       for (reg = 4; reg <= 12; reg++)
9332         if ((save_reg_mask & (1 << reg)) == 0)
9333           break;
9334
9335       if (reg <= 12)
9336         save_reg_mask |= (1 << reg);
9337       else
9338         {
9339           cfun->machine->sibcall_blocked = 1;
9340           save_reg_mask |= (1 << 3);
9341         }
9342     }
9343
9344   return save_reg_mask;
9345 }
9346
9347
9348 /* Compute a bit mask of which registers need to be
9349    saved on the stack for the current function.  */
9350 static unsigned long
9351 thumb_compute_save_reg_mask (void)
9352 {
9353   unsigned long mask;
9354   unsigned reg;
9355
9356   mask = 0;
9357   for (reg = 0; reg < 12; reg ++)
9358     if (regs_ever_live[reg] && !call_used_regs[reg])
9359       mask |= 1 << reg;
9360
9361   if (flag_pic
9362       && !TARGET_SINGLE_PIC_BASE
9363       && arm_pic_register != INVALID_REGNUM
9364       && current_function_uses_pic_offset_table)
9365     mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
9366
9367   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
9368   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
9369     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
9370
9371   /* LR will also be pushed if any lo regs are pushed.  */
9372   if (mask & 0xff || thumb_force_lr_save ())
9373     mask |= (1 << LR_REGNUM);
9374
9375   /* Make sure we have a low work register if we need one.
9376      We will need one if we are going to push a high register,
9377      but we are not currently intending to push a low register.  */
9378   if ((mask & 0xff) == 0
9379       && ((mask & 0x0f00) || TARGET_BACKTRACE))
9380     {
9381       /* Use thumb_find_work_register to choose which register
9382          we will use.  If the register is live then we will
9383          have to push it.  Use LAST_LO_REGNUM as our fallback
9384          choice for the register to select.  */
9385       reg = thumb_find_work_register (1 << LAST_LO_REGNUM);
9386
9387       if (! call_used_regs[reg])
9388         mask |= 1 << reg;
9389     }
9390
9391   return mask;
9392 }
9393
9394
9395 /* Return the number of bytes required to save VFP registers.  */
9396 static int
9397 arm_get_vfp_saved_size (void)
9398 {
9399   unsigned int regno;
9400   int count;
9401   int saved;
9402
9403   saved = 0;
9404   /* Space for saved VFP registers.  */
9405   if (TARGET_HARD_FLOAT && TARGET_VFP)
9406     {
9407       count = 0;
9408       for (regno = FIRST_VFP_REGNUM;
9409            regno < LAST_VFP_REGNUM;
9410            regno += 2)
9411         {
9412           if ((!regs_ever_live[regno] || call_used_regs[regno])
9413               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
9414             {
9415               if (count > 0)
9416                 {
9417                   /* Workaround ARM10 VFPr1 bug.  */
9418                   if (count == 2 && !arm_arch6)
9419                     count++;
9420                   saved += count * 8 + 4;
9421                 }
9422               count = 0;
9423             }
9424           else
9425             count++;
9426         }
9427       if (count > 0)
9428         {
9429           if (count == 2 && !arm_arch6)
9430             count++;
9431           saved += count * 8 + 4;
9432         }
9433     }
9434   return saved;
9435 }
9436
9437
9438 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
9439    everything bar the final return instruction.  */
9440 const char *
9441 output_return_instruction (rtx operand, int really_return, int reverse)
9442 {
9443   char conditional[10];
9444   char instr[100];
9445   unsigned reg;
9446   unsigned long live_regs_mask;
9447   unsigned long func_type;
9448   arm_stack_offsets *offsets;
9449
9450   func_type = arm_current_func_type ();
9451
9452   if (IS_NAKED (func_type))
9453     return "";
9454
9455   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9456     {
9457       /* If this function was declared non-returning, and we have
9458          found a tail call, then we have to trust that the called
9459          function won't return.  */
9460       if (really_return)
9461         {
9462           rtx ops[2];
9463
9464           /* Otherwise, trap an attempted return by aborting.  */
9465           ops[0] = operand;
9466           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
9467                                        : "abort");
9468           assemble_external_libcall (ops[1]);
9469           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
9470         }
9471
9472       return "";
9473     }
9474
9475   gcc_assert (!current_function_calls_alloca || really_return);
9476
9477   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
9478
9479   return_used_this_function = 1;
9480
9481   live_regs_mask = arm_compute_save_reg_mask ();
9482
9483   if (live_regs_mask)
9484     {
9485       const char * return_reg;
9486
9487       /* If we do not have any special requirements for function exit
9488          (e.g. interworking, or ISR) then we can load the return address
9489          directly into the PC.  Otherwise we must load it into LR.  */
9490       if (really_return
9491           && ! TARGET_INTERWORK)
9492         return_reg = reg_names[PC_REGNUM];
9493       else
9494         return_reg = reg_names[LR_REGNUM];
9495
9496       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9497         {
9498           /* There are three possible reasons for the IP register
9499              being saved.  1) a stack frame was created, in which case
9500              IP contains the old stack pointer, or 2) an ISR routine
9501              corrupted it, or 3) it was saved to align the stack on
9502              iWMMXt.  In case 1, restore IP into SP, otherwise just
9503              restore IP.  */
9504           if (frame_pointer_needed)
9505             {
9506               live_regs_mask &= ~ (1 << IP_REGNUM);
9507               live_regs_mask |=   (1 << SP_REGNUM);
9508             }
9509           else
9510             gcc_assert (IS_INTERRUPT (func_type) || TARGET_REALLY_IWMMXT);
9511         }
9512
9513       /* On some ARM architectures it is faster to use LDR rather than
9514          LDM to load a single register.  On other architectures, the
9515          cost is the same.  In 26 bit mode, or for exception handlers,
9516          we have to use LDM to load the PC so that the CPSR is also
9517          restored.  */
9518       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9519         if (live_regs_mask == (1U << reg))
9520           break;
9521
9522       if (reg <= LAST_ARM_REGNUM
9523           && (reg != LR_REGNUM
9524               || ! really_return
9525               || ! IS_INTERRUPT (func_type)))
9526         {
9527           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9528                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9529         }
9530       else
9531         {
9532           char *p;
9533           int first = 1;
9534
9535           /* Generate the load multiple instruction to restore the
9536              registers.  Note we can get here, even if
9537              frame_pointer_needed is true, but only if sp already
9538              points to the base of the saved core registers.  */
9539           if (live_regs_mask & (1 << SP_REGNUM))
9540             {
9541               unsigned HOST_WIDE_INT stack_adjust;
9542
9543               offsets = arm_get_frame_offsets ();
9544               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9545               gcc_assert (stack_adjust == 0 || stack_adjust == 4);
9546
9547               if (stack_adjust && arm_arch5)
9548                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9549               else
9550                 {
9551                   /* If we can't use ldmib (SA110 bug),
9552                      then try to pop r3 instead.  */
9553                   if (stack_adjust)
9554                     live_regs_mask |= 1 << 3;
9555                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9556                 }
9557             }
9558           else
9559             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9560
9561           p = instr + strlen (instr);
9562
9563           for (reg = 0; reg <= SP_REGNUM; reg++)
9564             if (live_regs_mask & (1 << reg))
9565               {
9566                 int l = strlen (reg_names[reg]);
9567
9568                 if (first)
9569                   first = 0;
9570                 else
9571                   {
9572                     memcpy (p, ", ", 2);
9573                     p += 2;
9574                   }
9575
9576                 memcpy (p, "%|", 2);
9577                 memcpy (p + 2, reg_names[reg], l);
9578                 p += l + 2;
9579               }
9580
9581           if (live_regs_mask & (1 << LR_REGNUM))
9582             {
9583               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9584               /* If returning from an interrupt, restore the CPSR.  */
9585               if (IS_INTERRUPT (func_type))
9586                 strcat (p, "^");
9587             }
9588           else
9589             strcpy (p, "}");
9590         }
9591
9592       output_asm_insn (instr, & operand);
9593
9594       /* See if we need to generate an extra instruction to
9595          perform the actual function return.  */
9596       if (really_return
9597           && func_type != ARM_FT_INTERWORKED
9598           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9599         {
9600           /* The return has already been handled
9601              by loading the LR into the PC.  */
9602           really_return = 0;
9603         }
9604     }
9605
9606   if (really_return)
9607     {
9608       switch ((int) ARM_FUNC_TYPE (func_type))
9609         {
9610         case ARM_FT_ISR:
9611         case ARM_FT_FIQ:
9612           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9613           break;
9614
9615         case ARM_FT_INTERWORKED:
9616           sprintf (instr, "bx%s\t%%|lr", conditional);
9617           break;
9618
9619         case ARM_FT_EXCEPTION:
9620           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9621           break;
9622
9623         default:
9624           /* Use bx if it's available.  */
9625           if (arm_arch5 || arm_arch4t)
9626             sprintf (instr, "bx%s\t%%|lr", conditional);
9627           else
9628             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9629           break;
9630         }
9631
9632       output_asm_insn (instr, & operand);
9633     }
9634
9635   return "";
9636 }
9637
9638 /* Write the function name into the code section, directly preceding
9639    the function prologue.
9640
9641    Code will be output similar to this:
9642      t0
9643          .ascii "arm_poke_function_name", 0
9644          .align
9645      t1
9646          .word 0xff000000 + (t1 - t0)
9647      arm_poke_function_name
9648          mov     ip, sp
9649          stmfd   sp!, {fp, ip, lr, pc}
9650          sub     fp, ip, #4
9651
9652    When performing a stack backtrace, code can inspect the value
9653    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9654    at location pc - 12 and the top 8 bits are set, then we know
9655    that there is a function name embedded immediately preceding this
9656    location and has length ((pc[-3]) & 0xff000000).
9657
9658    We assume that pc is declared as a pointer to an unsigned long.
9659
9660    It is of no benefit to output the function name if we are assembling
9661    a leaf function.  These function types will not contain a stack
9662    backtrace structure, therefore it is not possible to determine the
9663    function name.  */
9664 void
9665 arm_poke_function_name (FILE *stream, const char *name)
9666 {
9667   unsigned long alignlength;
9668   unsigned long length;
9669   rtx           x;
9670
9671   length      = strlen (name) + 1;
9672   alignlength = ROUND_UP_WORD (length);
9673
9674   ASM_OUTPUT_ASCII (stream, name, length);
9675   ASM_OUTPUT_ALIGN (stream, 2);
9676   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9677   assemble_aligned_integer (UNITS_PER_WORD, x);
9678 }
9679
9680 /* Place some comments into the assembler stream
9681    describing the current function.  */
9682 static void
9683 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9684 {
9685   unsigned long func_type;
9686
9687   if (!TARGET_ARM)
9688     {
9689       thumb_output_function_prologue (f, frame_size);
9690       return;
9691     }
9692
9693   /* Sanity check.  */
9694   gcc_assert (!arm_ccfsm_state && !arm_target_insn);
9695
9696   func_type = arm_current_func_type ();
9697
9698   switch ((int) ARM_FUNC_TYPE (func_type))
9699     {
9700     default:
9701     case ARM_FT_NORMAL:
9702       break;
9703     case ARM_FT_INTERWORKED:
9704       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9705       break;
9706     case ARM_FT_ISR:
9707       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9708       break;
9709     case ARM_FT_FIQ:
9710       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9711       break;
9712     case ARM_FT_EXCEPTION:
9713       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9714       break;
9715     }
9716
9717   if (IS_NAKED (func_type))
9718     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9719
9720   if (IS_VOLATILE (func_type))
9721     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9722
9723   if (IS_NESTED (func_type))
9724     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9725
9726   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9727                current_function_args_size,
9728                current_function_pretend_args_size, frame_size);
9729
9730   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9731                frame_pointer_needed,
9732                cfun->machine->uses_anonymous_args);
9733
9734   if (cfun->machine->lr_save_eliminated)
9735     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9736
9737   if (current_function_calls_eh_return)
9738     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9739
9740 #ifdef AOF_ASSEMBLER
9741   if (flag_pic)
9742     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9743 #endif
9744
9745   return_used_this_function = 0;
9746 }
9747
9748 const char *
9749 arm_output_epilogue (rtx sibling)
9750 {
9751   int reg;
9752   unsigned long saved_regs_mask;
9753   unsigned long func_type;
9754   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9755      frame that is $fp + 4 for a non-variadic function.  */
9756   int floats_offset = 0;
9757   rtx operands[3];
9758   FILE * f = asm_out_file;
9759   unsigned int lrm_count = 0;
9760   int really_return = (sibling == NULL);
9761   int start_reg;
9762   arm_stack_offsets *offsets;
9763
9764   /* If we have already generated the return instruction
9765      then it is futile to generate anything else.  */
9766   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9767     return "";
9768
9769   func_type = arm_current_func_type ();
9770
9771   if (IS_NAKED (func_type))
9772     /* Naked functions don't have epilogues.  */
9773     return "";
9774
9775   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9776     {
9777       rtx op;
9778
9779       /* A volatile function should never return.  Call abort.  */
9780       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9781       assemble_external_libcall (op);
9782       output_asm_insn ("bl\t%a0", &op);
9783
9784       return "";
9785     }
9786
9787   /* If we are throwing an exception, then we really must be doing a
9788      return, so we can't tail-call.  */
9789   gcc_assert (!current_function_calls_eh_return || really_return);
9790
9791   offsets = arm_get_frame_offsets ();
9792   saved_regs_mask = arm_compute_save_reg_mask ();
9793
9794   if (TARGET_IWMMXT)
9795     lrm_count = bit_count (saved_regs_mask);
9796
9797   floats_offset = offsets->saved_args;
9798   /* Compute how far away the floats will be.  */
9799   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9800     if (saved_regs_mask & (1 << reg))
9801       floats_offset += 4;
9802
9803   if (frame_pointer_needed)
9804     {
9805       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9806       int vfp_offset = offsets->frame;
9807
9808       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9809         {
9810           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9811             if (regs_ever_live[reg] && !call_used_regs[reg])
9812               {
9813                 floats_offset += 12;
9814                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9815                              reg, FP_REGNUM, floats_offset - vfp_offset);
9816               }
9817         }
9818       else
9819         {
9820           start_reg = LAST_FPA_REGNUM;
9821
9822           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9823             {
9824               if (regs_ever_live[reg] && !call_used_regs[reg])
9825                 {
9826                   floats_offset += 12;
9827
9828                   /* We can't unstack more than four registers at once.  */
9829                   if (start_reg - reg == 3)
9830                     {
9831                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9832                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9833                       start_reg = reg - 1;
9834                     }
9835                 }
9836               else
9837                 {
9838                   if (reg != start_reg)
9839                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9840                                  reg + 1, start_reg - reg,
9841                                  FP_REGNUM, floats_offset - vfp_offset);
9842                   start_reg = reg - 1;
9843                 }
9844             }
9845
9846           /* Just in case the last register checked also needs unstacking.  */
9847           if (reg != start_reg)
9848             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9849                          reg + 1, start_reg - reg,
9850                          FP_REGNUM, floats_offset - vfp_offset);
9851         }
9852
9853       if (TARGET_HARD_FLOAT && TARGET_VFP)
9854         {
9855           int saved_size;
9856
9857           /* The fldmx insn does not have base+offset addressing modes,
9858              so we use IP to hold the address.  */
9859           saved_size = arm_get_vfp_saved_size ();
9860
9861           if (saved_size > 0)
9862             {
9863               floats_offset += saved_size;
9864               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9865                            FP_REGNUM, floats_offset - vfp_offset);
9866             }
9867           start_reg = FIRST_VFP_REGNUM;
9868           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9869             {
9870               if ((!regs_ever_live[reg] || call_used_regs[reg])
9871                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9872                 {
9873                   if (start_reg != reg)
9874                     arm_output_fldmx (f, IP_REGNUM,
9875                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9876                                       (reg - start_reg) / 2);
9877                   start_reg = reg + 2;
9878                 }
9879             }
9880           if (start_reg != reg)
9881             arm_output_fldmx (f, IP_REGNUM,
9882                               (start_reg - FIRST_VFP_REGNUM) / 2,
9883                               (reg - start_reg) / 2);
9884         }
9885
9886       if (TARGET_IWMMXT)
9887         {
9888           /* The frame pointer is guaranteed to be non-double-word aligned.
9889              This is because it is set to (old_stack_pointer - 4) and the
9890              old_stack_pointer was double word aligned.  Thus the offset to
9891              the iWMMXt registers to be loaded must also be non-double-word
9892              sized, so that the resultant address *is* double-word aligned.
9893              We can ignore floats_offset since that was already included in
9894              the live_regs_mask.  */
9895           lrm_count += (lrm_count % 2 ? 2 : 1);
9896
9897           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9898             if (regs_ever_live[reg] && !call_used_regs[reg])
9899               {
9900                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9901                              reg, FP_REGNUM, lrm_count * 4);
9902                 lrm_count += 2;
9903               }
9904         }
9905
9906       /* saved_regs_mask should contain the IP, which at the time of stack
9907          frame generation actually contains the old stack pointer.  So a
9908          quick way to unwind the stack is just pop the IP register directly
9909          into the stack pointer.  */
9910       gcc_assert (saved_regs_mask & (1 << IP_REGNUM));
9911       saved_regs_mask &= ~ (1 << IP_REGNUM);
9912       saved_regs_mask |=   (1 << SP_REGNUM);
9913
9914       /* There are two registers left in saved_regs_mask - LR and PC.  We
9915          only need to restore the LR register (the return address), but to
9916          save time we can load it directly into the PC, unless we need a
9917          special function exit sequence, or we are not really returning.  */
9918       if (really_return
9919           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9920           && !current_function_calls_eh_return)
9921         /* Delete the LR from the register mask, so that the LR on
9922            the stack is loaded into the PC in the register mask.  */
9923         saved_regs_mask &= ~ (1 << LR_REGNUM);
9924       else
9925         saved_regs_mask &= ~ (1 << PC_REGNUM);
9926
9927       /* We must use SP as the base register, because SP is one of the
9928          registers being restored.  If an interrupt or page fault
9929          happens in the ldm instruction, the SP might or might not
9930          have been restored.  That would be bad, as then SP will no
9931          longer indicate the safe area of stack, and we can get stack
9932          corruption.  Using SP as the base register means that it will
9933          be reset correctly to the original value, should an interrupt
9934          occur.  If the stack pointer already points at the right
9935          place, then omit the subtraction.  */
9936       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9937           || current_function_calls_alloca)
9938         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9939                      4 * bit_count (saved_regs_mask));
9940       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9941
9942       if (IS_INTERRUPT (func_type))
9943         /* Interrupt handlers will have pushed the
9944            IP onto the stack, so restore it now.  */
9945         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9946     }
9947   else
9948     {
9949       /* Restore stack pointer if necessary.  */
9950       if (offsets->outgoing_args != offsets->saved_regs)
9951         {
9952           operands[0] = operands[1] = stack_pointer_rtx;
9953           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9954           output_add_immediate (operands);
9955         }
9956
9957       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9958         {
9959           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9960             if (regs_ever_live[reg] && !call_used_regs[reg])
9961               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9962                            reg, SP_REGNUM);
9963         }
9964       else
9965         {
9966           start_reg = FIRST_FPA_REGNUM;
9967
9968           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9969             {
9970               if (regs_ever_live[reg] && !call_used_regs[reg])
9971                 {
9972                   if (reg - start_reg == 3)
9973                     {
9974                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9975                                    start_reg, SP_REGNUM);
9976                       start_reg = reg + 1;
9977                     }
9978                 }
9979               else
9980                 {
9981                   if (reg != start_reg)
9982                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9983                                  start_reg, reg - start_reg,
9984                                  SP_REGNUM);
9985
9986                   start_reg = reg + 1;
9987                 }
9988             }
9989
9990           /* Just in case the last register checked also needs unstacking.  */
9991           if (reg != start_reg)
9992             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9993                          start_reg, reg - start_reg, SP_REGNUM);
9994         }
9995
9996       if (TARGET_HARD_FLOAT && TARGET_VFP)
9997         {
9998           start_reg = FIRST_VFP_REGNUM;
9999           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10000             {
10001               if ((!regs_ever_live[reg] || call_used_regs[reg])
10002                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10003                 {
10004                   if (start_reg != reg)
10005                     arm_output_fldmx (f, SP_REGNUM,
10006                                       (start_reg - FIRST_VFP_REGNUM) / 2,
10007                                       (reg - start_reg) / 2);
10008                   start_reg = reg + 2;
10009                 }
10010             }
10011           if (start_reg != reg)
10012             arm_output_fldmx (f, SP_REGNUM,
10013                               (start_reg - FIRST_VFP_REGNUM) / 2,
10014                               (reg - start_reg) / 2);
10015         }
10016       if (TARGET_IWMMXT)
10017         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
10018           if (regs_ever_live[reg] && !call_used_regs[reg])
10019             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
10020
10021       /* If we can, restore the LR into the PC.  */
10022       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
10023           && really_return
10024           && current_function_pretend_args_size == 0
10025           && saved_regs_mask & (1 << LR_REGNUM)
10026           && !current_function_calls_eh_return)
10027         {
10028           saved_regs_mask &= ~ (1 << LR_REGNUM);
10029           saved_regs_mask |=   (1 << PC_REGNUM);
10030         }
10031
10032       /* Load the registers off the stack.  If we only have one register
10033          to load use the LDR instruction - it is faster.  */
10034       if (saved_regs_mask == (1 << LR_REGNUM))
10035         {
10036           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
10037         }
10038       else if (saved_regs_mask)
10039         {
10040           if (saved_regs_mask & (1 << SP_REGNUM))
10041             /* Note - write back to the stack register is not enabled
10042                (i.e. "ldmfd sp!...").  We know that the stack pointer is
10043                in the list of registers and if we add writeback the
10044                instruction becomes UNPREDICTABLE.  */
10045             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
10046           else
10047             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
10048         }
10049
10050       if (current_function_pretend_args_size)
10051         {
10052           /* Unwind the pre-pushed regs.  */
10053           operands[0] = operands[1] = stack_pointer_rtx;
10054           operands[2] = GEN_INT (current_function_pretend_args_size);
10055           output_add_immediate (operands);
10056         }
10057     }
10058
10059   /* We may have already restored PC directly from the stack.  */
10060   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
10061     return "";
10062
10063   /* Stack adjustment for exception handler.  */
10064   if (current_function_calls_eh_return)
10065     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
10066                  ARM_EH_STACKADJ_REGNUM);
10067
10068   /* Generate the return instruction.  */
10069   switch ((int) ARM_FUNC_TYPE (func_type))
10070     {
10071     case ARM_FT_ISR:
10072     case ARM_FT_FIQ:
10073       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
10074       break;
10075
10076     case ARM_FT_EXCEPTION:
10077       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10078       break;
10079
10080     case ARM_FT_INTERWORKED:
10081       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10082       break;
10083
10084     default:
10085       if (arm_arch5 || arm_arch4t)
10086         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
10087       else
10088         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
10089       break;
10090     }
10091
10092   return "";
10093 }
10094
10095 static void
10096 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10097                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
10098 {
10099   arm_stack_offsets *offsets;
10100
10101   if (TARGET_THUMB)
10102     {
10103       int regno;
10104
10105       /* Emit any call-via-reg trampolines that are needed for v4t support
10106          of call_reg and call_value_reg type insns.  */
10107       for (regno = 0; regno < LR_REGNUM; regno++)
10108         {
10109           rtx label = cfun->machine->call_via[regno];
10110
10111           if (label != NULL)
10112             {
10113               switch_to_section (function_section (current_function_decl));
10114               targetm.asm_out.internal_label (asm_out_file, "L",
10115                                               CODE_LABEL_NUMBER (label));
10116               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
10117             }
10118         }
10119
10120       /* ??? Probably not safe to set this here, since it assumes that a
10121          function will be emitted as assembly immediately after we generate
10122          RTL for it.  This does not happen for inline functions.  */
10123       return_used_this_function = 0;
10124     }
10125   else
10126     {
10127       /* We need to take into account any stack-frame rounding.  */
10128       offsets = arm_get_frame_offsets ();
10129
10130       gcc_assert (!use_return_insn (FALSE, NULL)
10131                   || !return_used_this_function
10132                   || offsets->saved_regs == offsets->outgoing_args
10133                   || frame_pointer_needed);
10134
10135       /* Reset the ARM-specific per-function variables.  */
10136       after_arm_reorg = 0;
10137     }
10138 }
10139
10140 /* Generate and emit an insn that we will recognize as a push_multi.
10141    Unfortunately, since this insn does not reflect very well the actual
10142    semantics of the operation, we need to annotate the insn for the benefit
10143    of DWARF2 frame unwind information.  */
10144 static rtx
10145 emit_multi_reg_push (unsigned long mask)
10146 {
10147   int num_regs = 0;
10148   int num_dwarf_regs;
10149   int i, j;
10150   rtx par;
10151   rtx dwarf;
10152   int dwarf_par_index;
10153   rtx tmp, reg;
10154
10155   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10156     if (mask & (1 << i))
10157       num_regs++;
10158
10159   gcc_assert (num_regs && num_regs <= 16);
10160
10161   /* We don't record the PC in the dwarf frame information.  */
10162   num_dwarf_regs = num_regs;
10163   if (mask & (1 << PC_REGNUM))
10164     num_dwarf_regs--;
10165
10166   /* For the body of the insn we are going to generate an UNSPEC in
10167      parallel with several USEs.  This allows the insn to be recognized
10168      by the push_multi pattern in the arm.md file.  The insn looks
10169      something like this:
10170
10171        (parallel [
10172            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
10173                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
10174            (use (reg:SI 11 fp))
10175            (use (reg:SI 12 ip))
10176            (use (reg:SI 14 lr))
10177            (use (reg:SI 15 pc))
10178         ])
10179
10180      For the frame note however, we try to be more explicit and actually
10181      show each register being stored into the stack frame, plus a (single)
10182      decrement of the stack pointer.  We do it this way in order to be
10183      friendly to the stack unwinding code, which only wants to see a single
10184      stack decrement per instruction.  The RTL we generate for the note looks
10185      something like this:
10186
10187       (sequence [
10188            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
10189            (set (mem:SI (reg:SI sp)) (reg:SI r4))
10190            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
10191            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
10192            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
10193         ])
10194
10195       This sequence is used both by the code to support stack unwinding for
10196       exceptions handlers and the code to generate dwarf2 frame debugging.  */
10197
10198   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
10199   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
10200   dwarf_par_index = 1;
10201
10202   for (i = 0; i <= LAST_ARM_REGNUM; i++)
10203     {
10204       if (mask & (1 << i))
10205         {
10206           reg = gen_rtx_REG (SImode, i);
10207
10208           XVECEXP (par, 0, 0)
10209             = gen_rtx_SET (VOIDmode,
10210                            gen_frame_mem (BLKmode,
10211                                           gen_rtx_PRE_DEC (BLKmode,
10212                                                            stack_pointer_rtx)),
10213                            gen_rtx_UNSPEC (BLKmode,
10214                                            gen_rtvec (1, reg),
10215                                            UNSPEC_PUSH_MULT));
10216
10217           if (i != PC_REGNUM)
10218             {
10219               tmp = gen_rtx_SET (VOIDmode,
10220                                  gen_frame_mem (SImode, stack_pointer_rtx),
10221                                  reg);
10222               RTX_FRAME_RELATED_P (tmp) = 1;
10223               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
10224               dwarf_par_index++;
10225             }
10226
10227           break;
10228         }
10229     }
10230
10231   for (j = 1, i++; j < num_regs; i++)
10232     {
10233       if (mask & (1 << i))
10234         {
10235           reg = gen_rtx_REG (SImode, i);
10236
10237           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
10238
10239           if (i != PC_REGNUM)
10240             {
10241               tmp
10242                 = gen_rtx_SET (VOIDmode,
10243                                gen_frame_mem (SImode,
10244                                               plus_constant (stack_pointer_rtx,
10245                                                              4 * j)),
10246                                reg);
10247               RTX_FRAME_RELATED_P (tmp) = 1;
10248               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
10249             }
10250
10251           j++;
10252         }
10253     }
10254
10255   par = emit_insn (par);
10256
10257   tmp = gen_rtx_SET (VOIDmode,
10258                      stack_pointer_rtx,
10259                      plus_constant (stack_pointer_rtx, -4 * num_regs));
10260   RTX_FRAME_RELATED_P (tmp) = 1;
10261   XVECEXP (dwarf, 0, 0) = tmp;
10262
10263   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10264                                        REG_NOTES (par));
10265   return par;
10266 }
10267
10268 /* Calculate the size of the return value that is passed in registers.  */
10269 static int
10270 arm_size_return_regs (void)
10271 {
10272   enum machine_mode mode;
10273
10274   if (current_function_return_rtx != 0)
10275     mode = GET_MODE (current_function_return_rtx);
10276   else
10277     mode = DECL_MODE (DECL_RESULT (current_function_decl));
10278
10279   return GET_MODE_SIZE (mode);
10280 }
10281
10282 static rtx
10283 emit_sfm (int base_reg, int count)
10284 {
10285   rtx par;
10286   rtx dwarf;
10287   rtx tmp, reg;
10288   int i;
10289
10290   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
10291   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
10292
10293   reg = gen_rtx_REG (XFmode, base_reg++);
10294
10295   XVECEXP (par, 0, 0)
10296     = gen_rtx_SET (VOIDmode,
10297                    gen_frame_mem (BLKmode,
10298                                   gen_rtx_PRE_DEC (BLKmode,
10299                                                    stack_pointer_rtx)),
10300                    gen_rtx_UNSPEC (BLKmode,
10301                                    gen_rtvec (1, reg),
10302                                    UNSPEC_PUSH_MULT));
10303   tmp = gen_rtx_SET (VOIDmode,
10304                      gen_frame_mem (XFmode, stack_pointer_rtx), reg);
10305   RTX_FRAME_RELATED_P (tmp) = 1;
10306   XVECEXP (dwarf, 0, 1) = tmp;
10307
10308   for (i = 1; i < count; i++)
10309     {
10310       reg = gen_rtx_REG (XFmode, base_reg++);
10311       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
10312
10313       tmp = gen_rtx_SET (VOIDmode,
10314                          gen_frame_mem (XFmode,
10315                                         plus_constant (stack_pointer_rtx,
10316                                                        i * 12)),
10317                          reg);
10318       RTX_FRAME_RELATED_P (tmp) = 1;
10319       XVECEXP (dwarf, 0, i + 1) = tmp;
10320     }
10321
10322   tmp = gen_rtx_SET (VOIDmode,
10323                      stack_pointer_rtx,
10324                      plus_constant (stack_pointer_rtx, -12 * count));
10325
10326   RTX_FRAME_RELATED_P (tmp) = 1;
10327   XVECEXP (dwarf, 0, 0) = tmp;
10328
10329   par = emit_insn (par);
10330   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
10331                                        REG_NOTES (par));
10332   return par;
10333 }
10334
10335
10336 /* Return true if the current function needs to save/restore LR.  */
10337
10338 static bool
10339 thumb_force_lr_save (void)
10340 {
10341   return !cfun->machine->lr_save_eliminated
10342          && (!leaf_function_p ()
10343              || thumb_far_jump_used_p ()
10344              || regs_ever_live [LR_REGNUM]);
10345 }
10346
10347
10348 /* Compute the distance from register FROM to register TO.
10349    These can be the arg pointer (26), the soft frame pointer (25),
10350    the stack pointer (13) or the hard frame pointer (11).
10351    In thumb mode r7 is used as the soft frame pointer, if needed.
10352    Typical stack layout looks like this:
10353
10354        old stack pointer -> |    |
10355                              ----
10356                             |    | \
10357                             |    |   saved arguments for
10358                             |    |   vararg functions
10359                             |    | /
10360                               --
10361    hard FP & arg pointer -> |    | \
10362                             |    |   stack
10363                             |    |   frame
10364                             |    | /
10365                               --
10366                             |    | \
10367                             |    |   call saved
10368                             |    |   registers
10369       soft frame pointer -> |    | /
10370                               --
10371                             |    | \
10372                             |    |   local
10373                             |    |   variables
10374      locals base pointer -> |    | /
10375                               --
10376                             |    | \
10377                             |    |   outgoing
10378                             |    |   arguments
10379    current stack pointer -> |    | /
10380                               --
10381
10382   For a given function some or all of these stack components
10383   may not be needed, giving rise to the possibility of
10384   eliminating some of the registers.
10385
10386   The values returned by this function must reflect the behavior
10387   of arm_expand_prologue() and arm_compute_save_reg_mask().
10388
10389   The sign of the number returned reflects the direction of stack
10390   growth, so the values are positive for all eliminations except
10391   from the soft frame pointer to the hard frame pointer.
10392
10393   SFP may point just inside the local variables block to ensure correct
10394   alignment.  */
10395
10396
10397 /* Calculate stack offsets.  These are used to calculate register elimination
10398    offsets and in prologue/epilogue code.  */
10399
10400 static arm_stack_offsets *
10401 arm_get_frame_offsets (void)
10402 {
10403   struct arm_stack_offsets *offsets;
10404   unsigned long func_type;
10405   int leaf;
10406   int saved;
10407   HOST_WIDE_INT frame_size;
10408
10409   offsets = &cfun->machine->stack_offsets;
10410
10411   /* We need to know if we are a leaf function.  Unfortunately, it
10412      is possible to be called after start_sequence has been called,
10413      which causes get_insns to return the insns for the sequence,
10414      not the function, which will cause leaf_function_p to return
10415      the incorrect result.
10416
10417      to know about leaf functions once reload has completed, and the
10418      frame size cannot be changed after that time, so we can safely
10419      use the cached value.  */
10420
10421   if (reload_completed)
10422     return offsets;
10423
10424   /* Initially this is the size of the local variables.  It will translated
10425      into an offset once we have determined the size of preceding data.  */
10426   frame_size = ROUND_UP_WORD (get_frame_size ());
10427
10428   leaf = leaf_function_p ();
10429
10430   /* Space for variadic functions.  */
10431   offsets->saved_args = current_function_pretend_args_size;
10432
10433   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
10434
10435   if (TARGET_ARM)
10436     {
10437       unsigned int regno;
10438
10439       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
10440
10441       /* We know that SP will be doubleword aligned on entry, and we must
10442          preserve that condition at any subroutine call.  We also require the
10443          soft frame pointer to be doubleword aligned.  */
10444
10445       if (TARGET_REALLY_IWMMXT)
10446         {
10447           /* Check for the call-saved iWMMXt registers.  */
10448           for (regno = FIRST_IWMMXT_REGNUM;
10449                regno <= LAST_IWMMXT_REGNUM;
10450                regno++)
10451             if (regs_ever_live [regno] && ! call_used_regs [regno])
10452               saved += 8;
10453         }
10454
10455       func_type = arm_current_func_type ();
10456       if (! IS_VOLATILE (func_type))
10457         {
10458           /* Space for saved FPA registers.  */
10459           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
10460           if (regs_ever_live[regno] && ! call_used_regs[regno])
10461             saved += 12;
10462
10463           /* Space for saved VFP registers.  */
10464           if (TARGET_HARD_FLOAT && TARGET_VFP)
10465             saved += arm_get_vfp_saved_size ();
10466         }
10467     }
10468   else /* TARGET_THUMB */
10469     {
10470       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
10471       if (TARGET_BACKTRACE)
10472         saved += 16;
10473     }
10474
10475   /* Saved registers include the stack frame.  */
10476   offsets->saved_regs = offsets->saved_args + saved;
10477   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
10478   /* A leaf function does not need any stack alignment if it has nothing
10479      on the stack.  */
10480   if (leaf && frame_size == 0)
10481     {
10482       offsets->outgoing_args = offsets->soft_frame;
10483       return offsets;
10484     }
10485
10486   /* Ensure SFP has the correct alignment.  */
10487   if (ARM_DOUBLEWORD_ALIGN
10488       && (offsets->soft_frame & 7))
10489     offsets->soft_frame += 4;
10490
10491   offsets->locals_base = offsets->soft_frame + frame_size;
10492   offsets->outgoing_args = (offsets->locals_base
10493                             + current_function_outgoing_args_size);
10494
10495   if (ARM_DOUBLEWORD_ALIGN)
10496     {
10497       /* Ensure SP remains doubleword aligned.  */
10498       if (offsets->outgoing_args & 7)
10499         offsets->outgoing_args += 4;
10500       gcc_assert (!(offsets->outgoing_args & 7));
10501     }
10502
10503   return offsets;
10504 }
10505
10506
10507 /* Calculate the relative offsets for the different stack pointers.  Positive
10508    offsets are in the direction of stack growth.  */
10509
10510 HOST_WIDE_INT
10511 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10512 {
10513   arm_stack_offsets *offsets;
10514
10515   offsets = arm_get_frame_offsets ();
10516
10517   /* OK, now we have enough information to compute the distances.
10518      There must be an entry in these switch tables for each pair
10519      of registers in ELIMINABLE_REGS, even if some of the entries
10520      seem to be redundant or useless.  */
10521   switch (from)
10522     {
10523     case ARG_POINTER_REGNUM:
10524       switch (to)
10525         {
10526         case THUMB_HARD_FRAME_POINTER_REGNUM:
10527           return 0;
10528
10529         case FRAME_POINTER_REGNUM:
10530           /* This is the reverse of the soft frame pointer
10531              to hard frame pointer elimination below.  */
10532           return offsets->soft_frame - offsets->saved_args;
10533
10534         case ARM_HARD_FRAME_POINTER_REGNUM:
10535           /* If there is no stack frame then the hard
10536              frame pointer and the arg pointer coincide.  */
10537           if (offsets->frame == offsets->saved_regs)
10538             return 0;
10539           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10540           return (frame_pointer_needed
10541                   && cfun->static_chain_decl != NULL
10542                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10543
10544         case STACK_POINTER_REGNUM:
10545           /* If nothing has been pushed on the stack at all
10546              then this will return -4.  This *is* correct!  */
10547           return offsets->outgoing_args - (offsets->saved_args + 4);
10548
10549         default:
10550           gcc_unreachable ();
10551         }
10552       gcc_unreachable ();
10553
10554     case FRAME_POINTER_REGNUM:
10555       switch (to)
10556         {
10557         case THUMB_HARD_FRAME_POINTER_REGNUM:
10558           return 0;
10559
10560         case ARM_HARD_FRAME_POINTER_REGNUM:
10561           /* The hard frame pointer points to the top entry in the
10562              stack frame.  The soft frame pointer to the bottom entry
10563              in the stack frame.  If there is no stack frame at all,
10564              then they are identical.  */
10565
10566           return offsets->frame - offsets->soft_frame;
10567
10568         case STACK_POINTER_REGNUM:
10569           return offsets->outgoing_args - offsets->soft_frame;
10570
10571         default:
10572           gcc_unreachable ();
10573         }
10574       gcc_unreachable ();
10575
10576     default:
10577       /* You cannot eliminate from the stack pointer.
10578          In theory you could eliminate from the hard frame
10579          pointer to the stack pointer, but this will never
10580          happen, since if a stack frame is not needed the
10581          hard frame pointer will never be used.  */
10582       gcc_unreachable ();
10583     }
10584 }
10585
10586
10587 /* Generate the prologue instructions for entry into an ARM function.  */
10588 void
10589 arm_expand_prologue (void)
10590 {
10591   int reg;
10592   rtx amount;
10593   rtx insn;
10594   rtx ip_rtx;
10595   unsigned long live_regs_mask;
10596   unsigned long func_type;
10597   int fp_offset = 0;
10598   int saved_pretend_args = 0;
10599   int saved_regs = 0;
10600   unsigned HOST_WIDE_INT args_to_push;
10601   arm_stack_offsets *offsets;
10602
10603   func_type = arm_current_func_type ();
10604
10605   /* Naked functions don't have prologues.  */
10606   if (IS_NAKED (func_type))
10607     return;
10608
10609   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10610   args_to_push = current_function_pretend_args_size;
10611
10612   /* Compute which register we will have to save onto the stack.  */
10613   live_regs_mask = arm_compute_save_reg_mask ();
10614
10615   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10616
10617   if (frame_pointer_needed)
10618     {
10619       if (IS_INTERRUPT (func_type))
10620         {
10621           /* Interrupt functions must not corrupt any registers.
10622              Creating a frame pointer however, corrupts the IP
10623              register, so we must push it first.  */
10624           insn = emit_multi_reg_push (1 << IP_REGNUM);
10625
10626           /* Do not set RTX_FRAME_RELATED_P on this insn.
10627              The dwarf stack unwinding code only wants to see one
10628              stack decrement per function, and this is not it.  If
10629              this instruction is labeled as being part of the frame
10630              creation sequence then dwarf2out_frame_debug_expr will
10631              die when it encounters the assignment of IP to FP
10632              later on, since the use of SP here establishes SP as
10633              the CFA register and not IP.
10634
10635              Anyway this instruction is not really part of the stack
10636              frame creation although it is part of the prologue.  */
10637         }
10638       else if (IS_NESTED (func_type))
10639         {
10640           /* The Static chain register is the same as the IP register
10641              used as a scratch register during stack frame creation.
10642              To get around this need to find somewhere to store IP
10643              whilst the frame is being created.  We try the following
10644              places in order:
10645
10646                1. The last argument register.
10647                2. A slot on the stack above the frame.  (This only
10648                   works if the function is not a varargs function).
10649                3. Register r3, after pushing the argument registers
10650                   onto the stack.
10651
10652              Note - we only need to tell the dwarf2 backend about the SP
10653              adjustment in the second variant; the static chain register
10654              doesn't need to be unwound, as it doesn't contain a value
10655              inherited from the caller.  */
10656
10657           if (regs_ever_live[3] == 0)
10658             insn = emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10659           else if (args_to_push == 0)
10660             {
10661               rtx dwarf;
10662
10663               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10664               insn = emit_set_insn (gen_frame_mem (SImode, insn), ip_rtx);
10665               fp_offset = 4;
10666
10667               /* Just tell the dwarf backend that we adjusted SP.  */
10668               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10669                                    plus_constant (stack_pointer_rtx,
10670                                                   -fp_offset));
10671               RTX_FRAME_RELATED_P (insn) = 1;
10672               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10673                                                     dwarf, REG_NOTES (insn));
10674             }
10675           else
10676             {
10677               /* Store the args on the stack.  */
10678               if (cfun->machine->uses_anonymous_args)
10679                 insn = emit_multi_reg_push
10680                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10681               else
10682                 insn = emit_insn
10683                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10684                                GEN_INT (- args_to_push)));
10685
10686               RTX_FRAME_RELATED_P (insn) = 1;
10687
10688               saved_pretend_args = 1;
10689               fp_offset = args_to_push;
10690               args_to_push = 0;
10691
10692               /* Now reuse r3 to preserve IP.  */
10693               emit_set_insn (gen_rtx_REG (SImode, 3), ip_rtx);
10694             }
10695         }
10696
10697       insn = emit_set_insn (ip_rtx,
10698                             plus_constant (stack_pointer_rtx, fp_offset));
10699       RTX_FRAME_RELATED_P (insn) = 1;
10700     }
10701
10702   if (args_to_push)
10703     {
10704       /* Push the argument registers, or reserve space for them.  */
10705       if (cfun->machine->uses_anonymous_args)
10706         insn = emit_multi_reg_push
10707           ((0xf0 >> (args_to_push / 4)) & 0xf);
10708       else
10709         insn = emit_insn
10710           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10711                        GEN_INT (- args_to_push)));
10712       RTX_FRAME_RELATED_P (insn) = 1;
10713     }
10714
10715   /* If this is an interrupt service routine, and the link register
10716      is going to be pushed, and we are not creating a stack frame,
10717      (which would involve an extra push of IP and a pop in the epilogue)
10718      subtracting four from LR now will mean that the function return
10719      can be done with a single instruction.  */
10720   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10721       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10722       && ! frame_pointer_needed)
10723     {
10724       rtx lr = gen_rtx_REG (SImode, LR_REGNUM);
10725       
10726       emit_set_insn (lr, plus_constant (lr, -4));
10727     }
10728
10729   if (live_regs_mask)
10730     {
10731       insn = emit_multi_reg_push (live_regs_mask);
10732       saved_regs += bit_count (live_regs_mask) * 4;
10733       RTX_FRAME_RELATED_P (insn) = 1;
10734     }
10735
10736   if (TARGET_IWMMXT)
10737     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10738       if (regs_ever_live[reg] && ! call_used_regs [reg])
10739         {
10740           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10741           insn = gen_frame_mem (V2SImode, insn);
10742           insn = emit_set_insn (insn, gen_rtx_REG (V2SImode, reg));
10743           RTX_FRAME_RELATED_P (insn) = 1;
10744           saved_regs += 8;
10745         }
10746
10747   if (! IS_VOLATILE (func_type))
10748     {
10749       int start_reg;
10750
10751       /* Save any floating point call-saved registers used by this
10752          function.  */
10753       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10754         {
10755           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10756             if (regs_ever_live[reg] && !call_used_regs[reg])
10757               {
10758                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10759                 insn = gen_frame_mem (XFmode, insn);
10760                 insn = emit_set_insn (insn, gen_rtx_REG (XFmode, reg));
10761                 RTX_FRAME_RELATED_P (insn) = 1;
10762                 saved_regs += 12;
10763               }
10764         }
10765       else
10766         {
10767           start_reg = LAST_FPA_REGNUM;
10768
10769           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10770             {
10771               if (regs_ever_live[reg] && !call_used_regs[reg])
10772                 {
10773                   if (start_reg - reg == 3)
10774                     {
10775                       insn = emit_sfm (reg, 4);
10776                       RTX_FRAME_RELATED_P (insn) = 1;
10777                       saved_regs += 48;
10778                       start_reg = reg - 1;
10779                     }
10780                 }
10781               else
10782                 {
10783                   if (start_reg != reg)
10784                     {
10785                       insn = emit_sfm (reg + 1, start_reg - reg);
10786                       RTX_FRAME_RELATED_P (insn) = 1;
10787                       saved_regs += (start_reg - reg) * 12;
10788                     }
10789                   start_reg = reg - 1;
10790                 }
10791             }
10792
10793           if (start_reg != reg)
10794             {
10795               insn = emit_sfm (reg + 1, start_reg - reg);
10796               saved_regs += (start_reg - reg) * 12;
10797               RTX_FRAME_RELATED_P (insn) = 1;
10798             }
10799         }
10800       if (TARGET_HARD_FLOAT && TARGET_VFP)
10801         {
10802           start_reg = FIRST_VFP_REGNUM;
10803
10804           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10805             {
10806               if ((!regs_ever_live[reg] || call_used_regs[reg])
10807                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10808                 {
10809                   if (start_reg != reg)
10810                     saved_regs += vfp_emit_fstmx (start_reg,
10811                                                   (reg - start_reg) / 2);
10812                   start_reg = reg + 2;
10813                 }
10814             }
10815           if (start_reg != reg)
10816             saved_regs += vfp_emit_fstmx (start_reg,
10817                                           (reg - start_reg) / 2);
10818         }
10819     }
10820
10821   if (frame_pointer_needed)
10822     {
10823       /* Create the new frame pointer.  */
10824       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10825       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10826       RTX_FRAME_RELATED_P (insn) = 1;
10827
10828       if (IS_NESTED (func_type))
10829         {
10830           /* Recover the static chain register.  */
10831           if (regs_ever_live [3] == 0
10832               || saved_pretend_args)
10833             insn = gen_rtx_REG (SImode, 3);
10834           else /* if (current_function_pretend_args_size == 0) */
10835             {
10836               insn = plus_constant (hard_frame_pointer_rtx, 4);
10837               insn = gen_frame_mem (SImode, insn);
10838             }
10839
10840           emit_set_insn (ip_rtx, insn);
10841           /* Add a USE to stop propagate_one_insn() from barfing.  */
10842           emit_insn (gen_prologue_use (ip_rtx));
10843         }
10844     }
10845
10846   offsets = arm_get_frame_offsets ();
10847   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10848     {
10849       /* This add can produce multiple insns for a large constant, so we
10850          need to get tricky.  */
10851       rtx last = get_last_insn ();
10852
10853       amount = GEN_INT (offsets->saved_args + saved_regs
10854                         - offsets->outgoing_args);
10855
10856       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10857                                     amount));
10858       do
10859         {
10860           last = last ? NEXT_INSN (last) : get_insns ();
10861           RTX_FRAME_RELATED_P (last) = 1;
10862         }
10863       while (last != insn);
10864
10865       /* If the frame pointer is needed, emit a special barrier that
10866          will prevent the scheduler from moving stores to the frame
10867          before the stack adjustment.  */
10868       if (frame_pointer_needed)
10869         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10870                                          hard_frame_pointer_rtx));
10871     }
10872
10873
10874   if (flag_pic && arm_pic_register != INVALID_REGNUM)
10875     arm_load_pic_register (0UL);
10876
10877   /* If we are profiling, make sure no instructions are scheduled before
10878      the call to mcount.  Similarly if the user has requested no
10879      scheduling in the prolog.  Similarly if we want non-call exceptions
10880      using the EABI unwinder, to prevent faulting instructions from being
10881      swapped with a stack adjustment.  */
10882   if (current_function_profile || !TARGET_SCHED_PROLOG
10883       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
10884     emit_insn (gen_blockage ());
10885
10886   /* If the link register is being kept alive, with the return address in it,
10887      then make sure that it does not get reused by the ce2 pass.  */
10888   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10889     {
10890       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10891       cfun->machine->lr_save_eliminated = 1;
10892     }
10893 }
10894 \f
10895 /* If CODE is 'd', then the X is a condition operand and the instruction
10896    should only be executed if the condition is true.
10897    if CODE is 'D', then the X is a condition operand and the instruction
10898    should only be executed if the condition is false: however, if the mode
10899    of the comparison is CCFPEmode, then always execute the instruction -- we
10900    do this because in these circumstances !GE does not necessarily imply LT;
10901    in these cases the instruction pattern will take care to make sure that
10902    an instruction containing %d will follow, thereby undoing the effects of
10903    doing this instruction unconditionally.
10904    If CODE is 'N' then X is a floating point operand that must be negated
10905    before output.
10906    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10907    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10908 void
10909 arm_print_operand (FILE *stream, rtx x, int code)
10910 {
10911   switch (code)
10912     {
10913     case '@':
10914       fputs (ASM_COMMENT_START, stream);
10915       return;
10916
10917     case '_':
10918       fputs (user_label_prefix, stream);
10919       return;
10920
10921     case '|':
10922       fputs (REGISTER_PREFIX, stream);
10923       return;
10924
10925     case '?':
10926       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10927         {
10928           if (TARGET_THUMB)
10929             {
10930               output_operand_lossage ("predicated Thumb instruction");
10931               break;
10932             }
10933           if (current_insn_predicate != NULL)
10934             {
10935               output_operand_lossage
10936                 ("predicated instruction in conditional sequence");
10937               break;
10938             }
10939
10940           fputs (arm_condition_codes[arm_current_cc], stream);
10941         }
10942       else if (current_insn_predicate)
10943         {
10944           enum arm_cond_code code;
10945
10946           if (TARGET_THUMB)
10947             {
10948               output_operand_lossage ("predicated Thumb instruction");
10949               break;
10950             }
10951
10952           code = get_arm_condition_code (current_insn_predicate);
10953           fputs (arm_condition_codes[code], stream);
10954         }
10955       return;
10956
10957     case 'N':
10958       {
10959         REAL_VALUE_TYPE r;
10960         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10961         r = REAL_VALUE_NEGATE (r);
10962         fprintf (stream, "%s", fp_const_from_val (&r));
10963       }
10964       return;
10965
10966     case 'B':
10967       if (GET_CODE (x) == CONST_INT)
10968         {
10969           HOST_WIDE_INT val;
10970           val = ARM_SIGN_EXTEND (~INTVAL (x));
10971           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10972         }
10973       else
10974         {
10975           putc ('~', stream);
10976           output_addr_const (stream, x);
10977         }
10978       return;
10979
10980     case 'i':
10981       fprintf (stream, "%s", arithmetic_instr (x, 1));
10982       return;
10983
10984     /* Truncate Cirrus shift counts.  */
10985     case 's':
10986       if (GET_CODE (x) == CONST_INT)
10987         {
10988           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10989           return;
10990         }
10991       arm_print_operand (stream, x, 0);
10992       return;
10993
10994     case 'I':
10995       fprintf (stream, "%s", arithmetic_instr (x, 0));
10996       return;
10997
10998     case 'S':
10999       {
11000         HOST_WIDE_INT val;
11001         const char * shift = shift_op (x, &val);
11002
11003         if (shift)
11004           {
11005             fprintf (stream, ", %s ", shift_op (x, &val));
11006             if (val == -1)
11007               arm_print_operand (stream, XEXP (x, 1), 0);
11008             else
11009               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
11010           }
11011       }
11012       return;
11013
11014       /* An explanation of the 'Q', 'R' and 'H' register operands:
11015
11016          In a pair of registers containing a DI or DF value the 'Q'
11017          operand returns the register number of the register containing
11018          the least significant part of the value.  The 'R' operand returns
11019          the register number of the register containing the most
11020          significant part of the value.
11021
11022          The 'H' operand returns the higher of the two register numbers.
11023          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
11024          same as the 'Q' operand, since the most significant part of the
11025          value is held in the lower number register.  The reverse is true
11026          on systems where WORDS_BIG_ENDIAN is false.
11027
11028          The purpose of these operands is to distinguish between cases
11029          where the endian-ness of the values is important (for example
11030          when they are added together), and cases where the endian-ness
11031          is irrelevant, but the order of register operations is important.
11032          For example when loading a value from memory into a register
11033          pair, the endian-ness does not matter.  Provided that the value
11034          from the lower memory address is put into the lower numbered
11035          register, and the value from the higher address is put into the
11036          higher numbered register, the load will work regardless of whether
11037          the value being loaded is big-wordian or little-wordian.  The
11038          order of the two register loads can matter however, if the address
11039          of the memory location is actually held in one of the registers
11040          being overwritten by the load.  */
11041     case 'Q':
11042       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11043         {
11044           output_operand_lossage ("invalid operand for code '%c'", code);
11045           return;
11046         }
11047
11048       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
11049       return;
11050
11051     case 'R':
11052       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11053         {
11054           output_operand_lossage ("invalid operand for code '%c'", code);
11055           return;
11056         }
11057
11058       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
11059       return;
11060
11061     case 'H':
11062       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
11063         {
11064           output_operand_lossage ("invalid operand for code '%c'", code);
11065           return;
11066         }
11067
11068       asm_fprintf (stream, "%r", REGNO (x) + 1);
11069       return;
11070
11071     case 'm':
11072       asm_fprintf (stream, "%r",
11073                    GET_CODE (XEXP (x, 0)) == REG
11074                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
11075       return;
11076
11077     case 'M':
11078       asm_fprintf (stream, "{%r-%r}",
11079                    REGNO (x),
11080                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
11081       return;
11082
11083     case 'd':
11084       /* CONST_TRUE_RTX means always -- that's the default.  */
11085       if (x == const_true_rtx)
11086         return;
11087
11088       if (!COMPARISON_P (x))
11089         {
11090           output_operand_lossage ("invalid operand for code '%c'", code);
11091           return;
11092         }
11093
11094       fputs (arm_condition_codes[get_arm_condition_code (x)],
11095              stream);
11096       return;
11097
11098     case 'D':
11099       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
11100          want to do that.  */
11101       if (x == const_true_rtx)
11102         {
11103           output_operand_lossage ("instruction never exectued");
11104           return;
11105         }
11106       if (!COMPARISON_P (x))
11107         {
11108           output_operand_lossage ("invalid operand for code '%c'", code);
11109           return;
11110         }
11111
11112       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
11113                                  (get_arm_condition_code (x))],
11114              stream);
11115       return;
11116
11117     /* Cirrus registers can be accessed in a variety of ways:
11118          single floating point (f)
11119          double floating point (d)
11120          32bit integer         (fx)
11121          64bit integer         (dx).  */
11122     case 'W':                   /* Cirrus register in F mode.  */
11123     case 'X':                   /* Cirrus register in D mode.  */
11124     case 'Y':                   /* Cirrus register in FX mode.  */
11125     case 'Z':                   /* Cirrus register in DX mode.  */
11126       gcc_assert (GET_CODE (x) == REG
11127                   && REGNO_REG_CLASS (REGNO (x)) == CIRRUS_REGS);
11128
11129       fprintf (stream, "mv%s%s",
11130                code == 'W' ? "f"
11131                : code == 'X' ? "d"
11132                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
11133
11134       return;
11135
11136     /* Print cirrus register in the mode specified by the register's mode.  */
11137     case 'V':
11138       {
11139         int mode = GET_MODE (x);
11140
11141         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
11142           {
11143             output_operand_lossage ("invalid operand for code '%c'", code);
11144             return;
11145           }
11146
11147         fprintf (stream, "mv%s%s",
11148                  mode == DFmode ? "d"
11149                  : mode == SImode ? "fx"
11150                  : mode == DImode ? "dx"
11151                  : "f", reg_names[REGNO (x)] + 2);
11152
11153         return;
11154       }
11155
11156     case 'U':
11157       if (GET_CODE (x) != REG
11158           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
11159           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
11160         /* Bad value for wCG register number.  */
11161         {
11162           output_operand_lossage ("invalid operand for code '%c'", code);
11163           return;
11164         }
11165
11166       else
11167         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
11168       return;
11169
11170       /* Print an iWMMXt control register name.  */
11171     case 'w':
11172       if (GET_CODE (x) != CONST_INT
11173           || INTVAL (x) < 0
11174           || INTVAL (x) >= 16)
11175         /* Bad value for wC register number.  */
11176         {
11177           output_operand_lossage ("invalid operand for code '%c'", code);
11178           return;
11179         }
11180
11181       else
11182         {
11183           static const char * wc_reg_names [16] =
11184             {
11185               "wCID",  "wCon",  "wCSSF", "wCASF",
11186               "wC4",   "wC5",   "wC6",   "wC7",
11187               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
11188               "wC12",  "wC13",  "wC14",  "wC15"
11189             };
11190
11191           fprintf (stream, wc_reg_names [INTVAL (x)]);
11192         }
11193       return;
11194
11195       /* Print a VFP double precision register name.  */
11196     case 'P':
11197       {
11198         int mode = GET_MODE (x);
11199         int num;
11200
11201         if (mode != DImode && mode != DFmode)
11202           {
11203             output_operand_lossage ("invalid operand for code '%c'", code);
11204             return;
11205           }
11206
11207         if (GET_CODE (x) != REG
11208             || !IS_VFP_REGNUM (REGNO (x)))
11209           {
11210             output_operand_lossage ("invalid operand for code '%c'", code);
11211             return;
11212           }
11213
11214         num = REGNO(x) - FIRST_VFP_REGNUM;
11215         if (num & 1)
11216           {
11217             output_operand_lossage ("invalid operand for code '%c'", code);
11218             return;
11219           }
11220
11221         fprintf (stream, "d%d", num >> 1);
11222       }
11223       return;
11224
11225     default:
11226       if (x == 0)
11227         {
11228           output_operand_lossage ("missing operand");
11229           return;
11230         }
11231
11232       switch (GET_CODE (x))
11233         {
11234         case REG:
11235           asm_fprintf (stream, "%r", REGNO (x));
11236           break;
11237
11238         case MEM:
11239           output_memory_reference_mode = GET_MODE (x);
11240           output_address (XEXP (x, 0));
11241           break;
11242
11243         case CONST_DOUBLE:
11244           fprintf (stream, "#%s", fp_immediate_constant (x));
11245           break;
11246
11247         default:
11248           gcc_assert (GET_CODE (x) != NEG);
11249           fputc ('#', stream);
11250           output_addr_const (stream, x);
11251           break;
11252         }
11253     }
11254 }
11255 \f
11256 #ifndef AOF_ASSEMBLER
11257 /* Target hook for assembling integer objects.  The ARM version needs to
11258    handle word-sized values specially.  */
11259 static bool
11260 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
11261 {
11262   if (size == UNITS_PER_WORD && aligned_p)
11263     {
11264       fputs ("\t.word\t", asm_out_file);
11265       output_addr_const (asm_out_file, x);
11266
11267       /* Mark symbols as position independent.  We only do this in the
11268          .text segment, not in the .data segment.  */
11269       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
11270           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
11271         {
11272           if (GET_CODE (x) == SYMBOL_REF
11273               && (CONSTANT_POOL_ADDRESS_P (x)
11274                   || SYMBOL_REF_LOCAL_P (x)))
11275             fputs ("(GOTOFF)", asm_out_file);
11276           else if (GET_CODE (x) == LABEL_REF)
11277             fputs ("(GOTOFF)", asm_out_file);
11278           else
11279             fputs ("(GOT)", asm_out_file);
11280         }
11281       fputc ('\n', asm_out_file);
11282       return true;
11283     }
11284
11285   if (arm_vector_mode_supported_p (GET_MODE (x)))
11286     {
11287       int i, units;
11288
11289       gcc_assert (GET_CODE (x) == CONST_VECTOR);
11290
11291       units = CONST_VECTOR_NUNITS (x);
11292
11293       switch (GET_MODE (x))
11294         {
11295         case V2SImode: size = 4; break;
11296         case V4HImode: size = 2; break;
11297         case V8QImode: size = 1; break;
11298         default:
11299           gcc_unreachable ();
11300         }
11301
11302       for (i = 0; i < units; i++)
11303         {
11304           rtx elt;
11305
11306           elt = CONST_VECTOR_ELT (x, i);
11307           assemble_integer
11308             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
11309         }
11310
11311       return true;
11312     }
11313
11314   return default_assemble_integer (x, size, aligned_p);
11315 }
11316
11317
11318 /* Add a function to the list of static constructors.  */
11319
11320 static void
11321 arm_elf_asm_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
11322 {
11323   if (!TARGET_AAPCS_BASED)
11324     {
11325       default_named_section_asm_out_constructor (symbol, priority);
11326       return;
11327     }
11328
11329   /* Put these in the .init_array section, using a special relocation.  */
11330   switch_to_section (ctors_section);
11331   assemble_align (POINTER_SIZE);
11332   fputs ("\t.word\t", asm_out_file);
11333   output_addr_const (asm_out_file, symbol);
11334   fputs ("(target1)\n", asm_out_file);
11335 }
11336 #endif
11337 \f
11338 /* A finite state machine takes care of noticing whether or not instructions
11339    can be conditionally executed, and thus decrease execution time and code
11340    size by deleting branch instructions.  The fsm is controlled by
11341    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
11342
11343 /* The state of the fsm controlling condition codes are:
11344    0: normal, do nothing special
11345    1: make ASM_OUTPUT_OPCODE not output this instruction
11346    2: make ASM_OUTPUT_OPCODE not output this instruction
11347    3: make instructions conditional
11348    4: make instructions conditional
11349
11350    State transitions (state->state by whom under condition):
11351    0 -> 1 final_prescan_insn if the `target' is a label
11352    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
11353    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
11354    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
11355    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
11356           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
11357    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
11358           (the target insn is arm_target_insn).
11359
11360    If the jump clobbers the conditions then we use states 2 and 4.
11361
11362    A similar thing can be done with conditional return insns.
11363
11364    XXX In case the `target' is an unconditional branch, this conditionalising
11365    of the instructions always reduces code size, but not always execution
11366    time.  But then, I want to reduce the code size to somewhere near what
11367    /bin/cc produces.  */
11368
11369 /* Returns the index of the ARM condition code string in
11370    `arm_condition_codes'.  COMPARISON should be an rtx like
11371    `(eq (...) (...))'.  */
11372 static enum arm_cond_code
11373 get_arm_condition_code (rtx comparison)
11374 {
11375   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
11376   int code;
11377   enum rtx_code comp_code = GET_CODE (comparison);
11378
11379   if (GET_MODE_CLASS (mode) != MODE_CC)
11380     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
11381                            XEXP (comparison, 1));
11382
11383   switch (mode)
11384     {
11385     case CC_DNEmode: code = ARM_NE; goto dominance;
11386     case CC_DEQmode: code = ARM_EQ; goto dominance;
11387     case CC_DGEmode: code = ARM_GE; goto dominance;
11388     case CC_DGTmode: code = ARM_GT; goto dominance;
11389     case CC_DLEmode: code = ARM_LE; goto dominance;
11390     case CC_DLTmode: code = ARM_LT; goto dominance;
11391     case CC_DGEUmode: code = ARM_CS; goto dominance;
11392     case CC_DGTUmode: code = ARM_HI; goto dominance;
11393     case CC_DLEUmode: code = ARM_LS; goto dominance;
11394     case CC_DLTUmode: code = ARM_CC;
11395
11396     dominance:
11397       gcc_assert (comp_code == EQ || comp_code == NE);
11398
11399       if (comp_code == EQ)
11400         return ARM_INVERSE_CONDITION_CODE (code);
11401       return code;
11402
11403     case CC_NOOVmode:
11404       switch (comp_code)
11405         {
11406         case NE: return ARM_NE;
11407         case EQ: return ARM_EQ;
11408         case GE: return ARM_PL;
11409         case LT: return ARM_MI;
11410         default: gcc_unreachable ();
11411         }
11412
11413     case CC_Zmode:
11414       switch (comp_code)
11415         {
11416         case NE: return ARM_NE;
11417         case EQ: return ARM_EQ;
11418         default: gcc_unreachable ();
11419         }
11420
11421     case CC_Nmode:
11422       switch (comp_code)
11423         {
11424         case NE: return ARM_MI;
11425         case EQ: return ARM_PL;
11426         default: gcc_unreachable ();
11427         }
11428
11429     case CCFPEmode:
11430     case CCFPmode:
11431       /* These encodings assume that AC=1 in the FPA system control
11432          byte.  This allows us to handle all cases except UNEQ and
11433          LTGT.  */
11434       switch (comp_code)
11435         {
11436         case GE: return ARM_GE;
11437         case GT: return ARM_GT;
11438         case LE: return ARM_LS;
11439         case LT: return ARM_MI;
11440         case NE: return ARM_NE;
11441         case EQ: return ARM_EQ;
11442         case ORDERED: return ARM_VC;
11443         case UNORDERED: return ARM_VS;
11444         case UNLT: return ARM_LT;
11445         case UNLE: return ARM_LE;
11446         case UNGT: return ARM_HI;
11447         case UNGE: return ARM_PL;
11448           /* UNEQ and LTGT do not have a representation.  */
11449         case UNEQ: /* Fall through.  */
11450         case LTGT: /* Fall through.  */
11451         default: gcc_unreachable ();
11452         }
11453
11454     case CC_SWPmode:
11455       switch (comp_code)
11456         {
11457         case NE: return ARM_NE;
11458         case EQ: return ARM_EQ;
11459         case GE: return ARM_LE;
11460         case GT: return ARM_LT;
11461         case LE: return ARM_GE;
11462         case LT: return ARM_GT;
11463         case GEU: return ARM_LS;
11464         case GTU: return ARM_CC;
11465         case LEU: return ARM_CS;
11466         case LTU: return ARM_HI;
11467         default: gcc_unreachable ();
11468         }
11469
11470     case CC_Cmode:
11471       switch (comp_code)
11472       {
11473       case LTU: return ARM_CS;
11474       case GEU: return ARM_CC;
11475       default: gcc_unreachable ();
11476       }
11477
11478     case CCmode:
11479       switch (comp_code)
11480         {
11481         case NE: return ARM_NE;
11482         case EQ: return ARM_EQ;
11483         case GE: return ARM_GE;
11484         case GT: return ARM_GT;
11485         case LE: return ARM_LE;
11486         case LT: return ARM_LT;
11487         case GEU: return ARM_CS;
11488         case GTU: return ARM_HI;
11489         case LEU: return ARM_LS;
11490         case LTU: return ARM_CC;
11491         default: gcc_unreachable ();
11492         }
11493
11494     default: gcc_unreachable ();
11495     }
11496 }
11497
11498 void
11499 arm_final_prescan_insn (rtx insn)
11500 {
11501   /* BODY will hold the body of INSN.  */
11502   rtx body = PATTERN (insn);
11503
11504   /* This will be 1 if trying to repeat the trick, and things need to be
11505      reversed if it appears to fail.  */
11506   int reverse = 0;
11507
11508   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11509      taken are clobbered, even if the rtl suggests otherwise.  It also
11510      means that we have to grub around within the jump expression to find
11511      out what the conditions are when the jump isn't taken.  */
11512   int jump_clobbers = 0;
11513
11514   /* If we start with a return insn, we only succeed if we find another one.  */
11515   int seeking_return = 0;
11516
11517   /* START_INSN will hold the insn from where we start looking.  This is the
11518      first insn after the following code_label if REVERSE is true.  */
11519   rtx start_insn = insn;
11520
11521   /* If in state 4, check if the target branch is reached, in order to
11522      change back to state 0.  */
11523   if (arm_ccfsm_state == 4)
11524     {
11525       if (insn == arm_target_insn)
11526         {
11527           arm_target_insn = NULL;
11528           arm_ccfsm_state = 0;
11529         }
11530       return;
11531     }
11532
11533   /* If in state 3, it is possible to repeat the trick, if this insn is an
11534      unconditional branch to a label, and immediately following this branch
11535      is the previous target label which is only used once, and the label this
11536      branch jumps to is not too far off.  */
11537   if (arm_ccfsm_state == 3)
11538     {
11539       if (simplejump_p (insn))
11540         {
11541           start_insn = next_nonnote_insn (start_insn);
11542           if (GET_CODE (start_insn) == BARRIER)
11543             {
11544               /* XXX Isn't this always a barrier?  */
11545               start_insn = next_nonnote_insn (start_insn);
11546             }
11547           if (GET_CODE (start_insn) == CODE_LABEL
11548               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11549               && LABEL_NUSES (start_insn) == 1)
11550             reverse = TRUE;
11551           else
11552             return;
11553         }
11554       else if (GET_CODE (body) == RETURN)
11555         {
11556           start_insn = next_nonnote_insn (start_insn);
11557           if (GET_CODE (start_insn) == BARRIER)
11558             start_insn = next_nonnote_insn (start_insn);
11559           if (GET_CODE (start_insn) == CODE_LABEL
11560               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11561               && LABEL_NUSES (start_insn) == 1)
11562             {
11563               reverse = TRUE;
11564               seeking_return = 1;
11565             }
11566           else
11567             return;
11568         }
11569       else
11570         return;
11571     }
11572
11573   gcc_assert (!arm_ccfsm_state || reverse);
11574   if (GET_CODE (insn) != JUMP_INSN)
11575     return;
11576
11577   /* This jump might be paralleled with a clobber of the condition codes
11578      the jump should always come first */
11579   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11580     body = XVECEXP (body, 0, 0);
11581
11582   if (reverse
11583       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11584           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11585     {
11586       int insns_skipped;
11587       int fail = FALSE, succeed = FALSE;
11588       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11589       int then_not_else = TRUE;
11590       rtx this_insn = start_insn, label = 0;
11591
11592       /* If the jump cannot be done with one instruction, we cannot
11593          conditionally execute the instruction in the inverse case.  */
11594       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11595         {
11596           jump_clobbers = 1;
11597           return;
11598         }
11599
11600       /* Register the insn jumped to.  */
11601       if (reverse)
11602         {
11603           if (!seeking_return)
11604             label = XEXP (SET_SRC (body), 0);
11605         }
11606       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11607         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11608       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11609         {
11610           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11611           then_not_else = FALSE;
11612         }
11613       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11614         seeking_return = 1;
11615       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11616         {
11617           seeking_return = 1;
11618           then_not_else = FALSE;
11619         }
11620       else
11621         gcc_unreachable ();
11622
11623       /* See how many insns this branch skips, and what kind of insns.  If all
11624          insns are okay, and the label or unconditional branch to the same
11625          label is not too far away, succeed.  */
11626       for (insns_skipped = 0;
11627            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11628         {
11629           rtx scanbody;
11630
11631           this_insn = next_nonnote_insn (this_insn);
11632           if (!this_insn)
11633             break;
11634
11635           switch (GET_CODE (this_insn))
11636             {
11637             case CODE_LABEL:
11638               /* Succeed if it is the target label, otherwise fail since
11639                  control falls in from somewhere else.  */
11640               if (this_insn == label)
11641                 {
11642                   if (jump_clobbers)
11643                     {
11644                       arm_ccfsm_state = 2;
11645                       this_insn = next_nonnote_insn (this_insn);
11646                     }
11647                   else
11648                     arm_ccfsm_state = 1;
11649                   succeed = TRUE;
11650                 }
11651               else
11652                 fail = TRUE;
11653               break;
11654
11655             case BARRIER:
11656               /* Succeed if the following insn is the target label.
11657                  Otherwise fail.
11658                  If return insns are used then the last insn in a function
11659                  will be a barrier.  */
11660               this_insn = next_nonnote_insn (this_insn);
11661               if (this_insn && this_insn == label)
11662                 {
11663                   if (jump_clobbers)
11664                     {
11665                       arm_ccfsm_state = 2;
11666                       this_insn = next_nonnote_insn (this_insn);
11667                     }
11668                   else
11669                     arm_ccfsm_state = 1;
11670                   succeed = TRUE;
11671                 }
11672               else
11673                 fail = TRUE;
11674               break;
11675
11676             case CALL_INSN:
11677               /* The AAPCS says that conditional calls should not be
11678                  used since they make interworking inefficient (the
11679                  linker can't transform BL<cond> into BLX).  That's
11680                  only a problem if the machine has BLX.  */
11681               if (arm_arch5)
11682                 {
11683                   fail = TRUE;
11684                   break;
11685                 }
11686
11687               /* Succeed if the following insn is the target label, or
11688                  if the following two insns are a barrier and the
11689                  target label.  */
11690               this_insn = next_nonnote_insn (this_insn);
11691               if (this_insn && GET_CODE (this_insn) == BARRIER)
11692                 this_insn = next_nonnote_insn (this_insn);
11693
11694               if (this_insn && this_insn == label
11695                   && insns_skipped < max_insns_skipped)
11696                 {
11697                   if (jump_clobbers)
11698                     {
11699                       arm_ccfsm_state = 2;
11700                       this_insn = next_nonnote_insn (this_insn);
11701                     }
11702                   else
11703                     arm_ccfsm_state = 1;
11704                   succeed = TRUE;
11705                 }
11706               else
11707                 fail = TRUE;
11708               break;
11709
11710             case JUMP_INSN:
11711               /* If this is an unconditional branch to the same label, succeed.
11712                  If it is to another label, do nothing.  If it is conditional,
11713                  fail.  */
11714               /* XXX Probably, the tests for SET and the PC are
11715                  unnecessary.  */
11716
11717               scanbody = PATTERN (this_insn);
11718               if (GET_CODE (scanbody) == SET
11719                   && GET_CODE (SET_DEST (scanbody)) == PC)
11720                 {
11721                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11722                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11723                     {
11724                       arm_ccfsm_state = 2;
11725                       succeed = TRUE;
11726                     }
11727                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11728                     fail = TRUE;
11729                 }
11730               /* Fail if a conditional return is undesirable (e.g. on a
11731                  StrongARM), but still allow this if optimizing for size.  */
11732               else if (GET_CODE (scanbody) == RETURN
11733                        && !use_return_insn (TRUE, NULL)
11734                        && !optimize_size)
11735                 fail = TRUE;
11736               else if (GET_CODE (scanbody) == RETURN
11737                        && seeking_return)
11738                 {
11739                   arm_ccfsm_state = 2;
11740                   succeed = TRUE;
11741                 }
11742               else if (GET_CODE (scanbody) == PARALLEL)
11743                 {
11744                   switch (get_attr_conds (this_insn))
11745                     {
11746                     case CONDS_NOCOND:
11747                       break;
11748                     default:
11749                       fail = TRUE;
11750                       break;
11751                     }
11752                 }
11753               else
11754                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11755
11756               break;
11757
11758             case INSN:
11759               /* Instructions using or affecting the condition codes make it
11760                  fail.  */
11761               scanbody = PATTERN (this_insn);
11762               if (!(GET_CODE (scanbody) == SET
11763                     || GET_CODE (scanbody) == PARALLEL)
11764                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11765                 fail = TRUE;
11766
11767               /* A conditional cirrus instruction must be followed by
11768                  a non Cirrus instruction.  However, since we
11769                  conditionalize instructions in this function and by
11770                  the time we get here we can't add instructions
11771                  (nops), because shorten_branches() has already been
11772                  called, we will disable conditionalizing Cirrus
11773                  instructions to be safe.  */
11774               if (GET_CODE (scanbody) != USE
11775                   && GET_CODE (scanbody) != CLOBBER
11776                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11777                 fail = TRUE;
11778               break;
11779
11780             default:
11781               break;
11782             }
11783         }
11784       if (succeed)
11785         {
11786           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11787             arm_target_label = CODE_LABEL_NUMBER (label);
11788           else
11789             {
11790               gcc_assert (seeking_return || arm_ccfsm_state == 2);
11791
11792               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11793                 {
11794                   this_insn = next_nonnote_insn (this_insn);
11795                   gcc_assert (!this_insn
11796                               || (GET_CODE (this_insn) != BARRIER
11797                                   && GET_CODE (this_insn) != CODE_LABEL));
11798                 }
11799               if (!this_insn)
11800                 {
11801                   /* Oh, dear! we ran off the end.. give up.  */
11802                   recog (PATTERN (insn), insn, NULL);
11803                   arm_ccfsm_state = 0;
11804                   arm_target_insn = NULL;
11805                   return;
11806                 }
11807               arm_target_insn = this_insn;
11808             }
11809           if (jump_clobbers)
11810             {
11811               gcc_assert (!reverse);
11812               arm_current_cc =
11813                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11814                                                             0), 0), 1));
11815               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11816                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11817               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11818                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11819             }
11820           else
11821             {
11822               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11823                  what it was.  */
11824               if (!reverse)
11825                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11826                                                                0));
11827             }
11828
11829           if (reverse || then_not_else)
11830             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11831         }
11832
11833       /* Restore recog_data (getting the attributes of other insns can
11834          destroy this array, but final.c assumes that it remains intact
11835          across this call; since the insn has been recognized already we
11836          call recog direct).  */
11837       recog (PATTERN (insn), insn, NULL);
11838     }
11839 }
11840
11841 /* Returns true if REGNO is a valid register
11842    for holding a quantity of type MODE.  */
11843 int
11844 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11845 {
11846   if (GET_MODE_CLASS (mode) == MODE_CC)
11847     return (regno == CC_REGNUM
11848             || (TARGET_HARD_FLOAT && TARGET_VFP
11849                 && regno == VFPCC_REGNUM));
11850
11851   if (TARGET_THUMB)
11852     /* For the Thumb we only allow values bigger than SImode in
11853        registers 0 - 6, so that there is always a second low
11854        register available to hold the upper part of the value.
11855        We probably we ought to ensure that the register is the
11856        start of an even numbered register pair.  */
11857     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11858
11859   if (TARGET_HARD_FLOAT && TARGET_MAVERICK
11860       && IS_CIRRUS_REGNUM (regno))
11861     /* We have outlawed SI values in Cirrus registers because they
11862        reside in the lower 32 bits, but SF values reside in the
11863        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11864        even split the registers into pairs because Cirrus SI values
11865        get sign extended to 64bits-- aldyh.  */
11866     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11867
11868   if (TARGET_HARD_FLOAT && TARGET_VFP
11869       && IS_VFP_REGNUM (regno))
11870     {
11871       if (mode == SFmode || mode == SImode)
11872         return TRUE;
11873
11874       /* DFmode values are only valid in even register pairs.  */
11875       if (mode == DFmode)
11876         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11877       return FALSE;
11878     }
11879
11880   if (TARGET_REALLY_IWMMXT)
11881     {
11882       if (IS_IWMMXT_GR_REGNUM (regno))
11883         return mode == SImode;
11884
11885       if (IS_IWMMXT_REGNUM (regno))
11886         return VALID_IWMMXT_REG_MODE (mode);
11887     }
11888   
11889   /* We allow any value to be stored in the general registers.
11890      Restrict doubleword quantities to even register pairs so that we can
11891      use ldrd.  */
11892   if (regno <= LAST_ARM_REGNUM)
11893     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11894
11895   if (regno == FRAME_POINTER_REGNUM
11896       || regno == ARG_POINTER_REGNUM)
11897     /* We only allow integers in the fake hard registers.  */
11898     return GET_MODE_CLASS (mode) == MODE_INT;
11899
11900   /* The only registers left are the FPA registers
11901      which we only allow to hold FP values.  */
11902   return (TARGET_HARD_FLOAT && TARGET_FPA
11903           && GET_MODE_CLASS (mode) == MODE_FLOAT
11904           && regno >= FIRST_FPA_REGNUM
11905           && regno <= LAST_FPA_REGNUM);
11906 }
11907
11908 int
11909 arm_regno_class (int regno)
11910 {
11911   if (TARGET_THUMB)
11912     {
11913       if (regno == STACK_POINTER_REGNUM)
11914         return STACK_REG;
11915       if (regno == CC_REGNUM)
11916         return CC_REG;
11917       if (regno < 8)
11918         return LO_REGS;
11919       return HI_REGS;
11920     }
11921
11922   if (   regno <= LAST_ARM_REGNUM
11923       || regno == FRAME_POINTER_REGNUM
11924       || regno == ARG_POINTER_REGNUM)
11925     return GENERAL_REGS;
11926
11927   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11928     return NO_REGS;
11929
11930   if (IS_CIRRUS_REGNUM (regno))
11931     return CIRRUS_REGS;
11932
11933   if (IS_VFP_REGNUM (regno))
11934     return VFP_REGS;
11935
11936   if (IS_IWMMXT_REGNUM (regno))
11937     return IWMMXT_REGS;
11938
11939   if (IS_IWMMXT_GR_REGNUM (regno))
11940     return IWMMXT_GR_REGS;
11941
11942   return FPA_REGS;
11943 }
11944
11945 /* Handle a special case when computing the offset
11946    of an argument from the frame pointer.  */
11947 int
11948 arm_debugger_arg_offset (int value, rtx addr)
11949 {
11950   rtx insn;
11951
11952   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11953   if (value != 0)
11954     return 0;
11955
11956   /* We can only cope with the case where the address is held in a register.  */
11957   if (GET_CODE (addr) != REG)
11958     return 0;
11959
11960   /* If we are using the frame pointer to point at the argument, then
11961      an offset of 0 is correct.  */
11962   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11963     return 0;
11964
11965   /* If we are using the stack pointer to point at the
11966      argument, then an offset of 0 is correct.  */
11967   if ((TARGET_THUMB || !frame_pointer_needed)
11968       && REGNO (addr) == SP_REGNUM)
11969     return 0;
11970
11971   /* Oh dear.  The argument is pointed to by a register rather
11972      than being held in a register, or being stored at a known
11973      offset from the frame pointer.  Since GDB only understands
11974      those two kinds of argument we must translate the address
11975      held in the register into an offset from the frame pointer.
11976      We do this by searching through the insns for the function
11977      looking to see where this register gets its value.  If the
11978      register is initialized from the frame pointer plus an offset
11979      then we are in luck and we can continue, otherwise we give up.
11980
11981      This code is exercised by producing debugging information
11982      for a function with arguments like this:
11983
11984            double func (double a, double b, int c, double d) {return d;}
11985
11986      Without this code the stab for parameter 'd' will be set to
11987      an offset of 0 from the frame pointer, rather than 8.  */
11988
11989   /* The if() statement says:
11990
11991      If the insn is a normal instruction
11992      and if the insn is setting the value in a register
11993      and if the register being set is the register holding the address of the argument
11994      and if the address is computing by an addition
11995      that involves adding to a register
11996      which is the frame pointer
11997      a constant integer
11998
11999      then...  */
12000
12001   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12002     {
12003       if (   GET_CODE (insn) == INSN
12004           && GET_CODE (PATTERN (insn)) == SET
12005           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
12006           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
12007           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
12008           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
12009           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
12010              )
12011         {
12012           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
12013
12014           break;
12015         }
12016     }
12017
12018   if (value == 0)
12019     {
12020       debug_rtx (addr);
12021       warning (0, "unable to compute real location of stacked parameter");
12022       value = 8; /* XXX magic hack */
12023     }
12024
12025   return value;
12026 }
12027 \f
12028 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
12029   do                                                                    \
12030     {                                                                   \
12031       if ((MASK) & insn_flags)                                          \
12032         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
12033                                      BUILT_IN_MD, NULL, NULL_TREE);     \
12034     }                                                                   \
12035   while (0)
12036
12037 struct builtin_description
12038 {
12039   const unsigned int       mask;
12040   const enum insn_code     icode;
12041   const char * const       name;
12042   const enum arm_builtins  code;
12043   const enum rtx_code      comparison;
12044   const unsigned int       flag;
12045 };
12046
12047 static const struct builtin_description bdesc_2arg[] =
12048 {
12049 #define IWMMXT_BUILTIN(code, string, builtin) \
12050   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
12051     ARM_BUILTIN_##builtin, 0, 0 },
12052
12053   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
12054   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
12055   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
12056   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
12057   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
12058   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
12059   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
12060   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
12061   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
12062   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
12063   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
12064   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
12065   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
12066   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
12067   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
12068   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
12069   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
12070   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
12071   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
12072   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
12073   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
12074   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
12075   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
12076   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
12077   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
12078   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
12079   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
12080   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
12081   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
12082   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
12083   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
12084   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
12085   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
12086   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
12087   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
12088   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
12089   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
12090   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
12091   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
12092   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
12093   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
12094   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
12095   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
12096   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
12097   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
12098   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
12099   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
12100   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
12101   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
12102   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
12103   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
12104   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
12105   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
12106   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
12107   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
12108   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
12109   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
12110   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
12111
12112 #define IWMMXT_BUILTIN2(code, builtin) \
12113   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
12114
12115   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
12116   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
12117   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
12118   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
12119   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
12120   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
12121   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
12122   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
12123   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
12124   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
12125   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
12126   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
12127   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
12128   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
12129   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
12130   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
12131   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
12132   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
12133   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
12134   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
12135   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
12136   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
12137   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
12138   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
12139   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
12140   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
12141   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
12142   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
12143   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
12144   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
12145   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
12146   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
12147 };
12148
12149 static const struct builtin_description bdesc_1arg[] =
12150 {
12151   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
12152   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
12153   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
12154   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
12155   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
12156   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
12157   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
12158   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
12159   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
12160   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
12161   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
12162   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
12163   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
12164   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
12165   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
12166   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
12167   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
12168   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
12169 };
12170
12171 /* Set up all the iWMMXt builtins.  This is
12172    not called if TARGET_IWMMXT is zero.  */
12173
12174 static void
12175 arm_init_iwmmxt_builtins (void)
12176 {
12177   const struct builtin_description * d;
12178   size_t i;
12179   tree endlink = void_list_node;
12180
12181   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
12182   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
12183   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
12184
12185   tree int_ftype_int
12186     = build_function_type (integer_type_node,
12187                            tree_cons (NULL_TREE, integer_type_node, endlink));
12188   tree v8qi_ftype_v8qi_v8qi_int
12189     = build_function_type (V8QI_type_node,
12190                            tree_cons (NULL_TREE, V8QI_type_node,
12191                                       tree_cons (NULL_TREE, V8QI_type_node,
12192                                                  tree_cons (NULL_TREE,
12193                                                             integer_type_node,
12194                                                             endlink))));
12195   tree v4hi_ftype_v4hi_int
12196     = build_function_type (V4HI_type_node,
12197                            tree_cons (NULL_TREE, V4HI_type_node,
12198                                       tree_cons (NULL_TREE, integer_type_node,
12199                                                  endlink)));
12200   tree v2si_ftype_v2si_int
12201     = build_function_type (V2SI_type_node,
12202                            tree_cons (NULL_TREE, V2SI_type_node,
12203                                       tree_cons (NULL_TREE, integer_type_node,
12204                                                  endlink)));
12205   tree v2si_ftype_di_di
12206     = build_function_type (V2SI_type_node,
12207                            tree_cons (NULL_TREE, long_long_integer_type_node,
12208                                       tree_cons (NULL_TREE, long_long_integer_type_node,
12209                                                  endlink)));
12210   tree di_ftype_di_int
12211     = build_function_type (long_long_integer_type_node,
12212                            tree_cons (NULL_TREE, long_long_integer_type_node,
12213                                       tree_cons (NULL_TREE, integer_type_node,
12214                                                  endlink)));
12215   tree di_ftype_di_int_int
12216     = build_function_type (long_long_integer_type_node,
12217                            tree_cons (NULL_TREE, long_long_integer_type_node,
12218                                       tree_cons (NULL_TREE, integer_type_node,
12219                                                  tree_cons (NULL_TREE,
12220                                                             integer_type_node,
12221                                                             endlink))));
12222   tree int_ftype_v8qi
12223     = build_function_type (integer_type_node,
12224                            tree_cons (NULL_TREE, V8QI_type_node,
12225                                       endlink));
12226   tree int_ftype_v4hi
12227     = build_function_type (integer_type_node,
12228                            tree_cons (NULL_TREE, V4HI_type_node,
12229                                       endlink));
12230   tree int_ftype_v2si
12231     = build_function_type (integer_type_node,
12232                            tree_cons (NULL_TREE, V2SI_type_node,
12233                                       endlink));
12234   tree int_ftype_v8qi_int
12235     = build_function_type (integer_type_node,
12236                            tree_cons (NULL_TREE, V8QI_type_node,
12237                                       tree_cons (NULL_TREE, integer_type_node,
12238                                                  endlink)));
12239   tree int_ftype_v4hi_int
12240     = build_function_type (integer_type_node,
12241                            tree_cons (NULL_TREE, V4HI_type_node,
12242                                       tree_cons (NULL_TREE, integer_type_node,
12243                                                  endlink)));
12244   tree int_ftype_v2si_int
12245     = build_function_type (integer_type_node,
12246                            tree_cons (NULL_TREE, V2SI_type_node,
12247                                       tree_cons (NULL_TREE, integer_type_node,
12248                                                  endlink)));
12249   tree v8qi_ftype_v8qi_int_int
12250     = build_function_type (V8QI_type_node,
12251                            tree_cons (NULL_TREE, V8QI_type_node,
12252                                       tree_cons (NULL_TREE, integer_type_node,
12253                                                  tree_cons (NULL_TREE,
12254                                                             integer_type_node,
12255                                                             endlink))));
12256   tree v4hi_ftype_v4hi_int_int
12257     = build_function_type (V4HI_type_node,
12258                            tree_cons (NULL_TREE, V4HI_type_node,
12259                                       tree_cons (NULL_TREE, integer_type_node,
12260                                                  tree_cons (NULL_TREE,
12261                                                             integer_type_node,
12262                                                             endlink))));
12263   tree v2si_ftype_v2si_int_int
12264     = build_function_type (V2SI_type_node,
12265                            tree_cons (NULL_TREE, V2SI_type_node,
12266                                       tree_cons (NULL_TREE, integer_type_node,
12267                                                  tree_cons (NULL_TREE,
12268                                                             integer_type_node,
12269                                                             endlink))));
12270   /* Miscellaneous.  */
12271   tree v8qi_ftype_v4hi_v4hi
12272     = build_function_type (V8QI_type_node,
12273                            tree_cons (NULL_TREE, V4HI_type_node,
12274                                       tree_cons (NULL_TREE, V4HI_type_node,
12275                                                  endlink)));
12276   tree v4hi_ftype_v2si_v2si
12277     = build_function_type (V4HI_type_node,
12278                            tree_cons (NULL_TREE, V2SI_type_node,
12279                                       tree_cons (NULL_TREE, V2SI_type_node,
12280                                                  endlink)));
12281   tree v2si_ftype_v4hi_v4hi
12282     = build_function_type (V2SI_type_node,
12283                            tree_cons (NULL_TREE, V4HI_type_node,
12284                                       tree_cons (NULL_TREE, V4HI_type_node,
12285                                                  endlink)));
12286   tree v2si_ftype_v8qi_v8qi
12287     = build_function_type (V2SI_type_node,
12288                            tree_cons (NULL_TREE, V8QI_type_node,
12289                                       tree_cons (NULL_TREE, V8QI_type_node,
12290                                                  endlink)));
12291   tree v4hi_ftype_v4hi_di
12292     = build_function_type (V4HI_type_node,
12293                            tree_cons (NULL_TREE, V4HI_type_node,
12294                                       tree_cons (NULL_TREE,
12295                                                  long_long_integer_type_node,
12296                                                  endlink)));
12297   tree v2si_ftype_v2si_di
12298     = build_function_type (V2SI_type_node,
12299                            tree_cons (NULL_TREE, V2SI_type_node,
12300                                       tree_cons (NULL_TREE,
12301                                                  long_long_integer_type_node,
12302                                                  endlink)));
12303   tree void_ftype_int_int
12304     = build_function_type (void_type_node,
12305                            tree_cons (NULL_TREE, integer_type_node,
12306                                       tree_cons (NULL_TREE, integer_type_node,
12307                                                  endlink)));
12308   tree di_ftype_void
12309     = build_function_type (long_long_unsigned_type_node, endlink);
12310   tree di_ftype_v8qi
12311     = build_function_type (long_long_integer_type_node,
12312                            tree_cons (NULL_TREE, V8QI_type_node,
12313                                       endlink));
12314   tree di_ftype_v4hi
12315     = build_function_type (long_long_integer_type_node,
12316                            tree_cons (NULL_TREE, V4HI_type_node,
12317                                       endlink));
12318   tree di_ftype_v2si
12319     = build_function_type (long_long_integer_type_node,
12320                            tree_cons (NULL_TREE, V2SI_type_node,
12321                                       endlink));
12322   tree v2si_ftype_v4hi
12323     = build_function_type (V2SI_type_node,
12324                            tree_cons (NULL_TREE, V4HI_type_node,
12325                                       endlink));
12326   tree v4hi_ftype_v8qi
12327     = build_function_type (V4HI_type_node,
12328                            tree_cons (NULL_TREE, V8QI_type_node,
12329                                       endlink));
12330
12331   tree di_ftype_di_v4hi_v4hi
12332     = build_function_type (long_long_unsigned_type_node,
12333                            tree_cons (NULL_TREE,
12334                                       long_long_unsigned_type_node,
12335                                       tree_cons (NULL_TREE, V4HI_type_node,
12336                                                  tree_cons (NULL_TREE,
12337                                                             V4HI_type_node,
12338                                                             endlink))));
12339
12340   tree di_ftype_v4hi_v4hi
12341     = build_function_type (long_long_unsigned_type_node,
12342                            tree_cons (NULL_TREE, V4HI_type_node,
12343                                       tree_cons (NULL_TREE, V4HI_type_node,
12344                                                  endlink)));
12345
12346   /* Normal vector binops.  */
12347   tree v8qi_ftype_v8qi_v8qi
12348     = build_function_type (V8QI_type_node,
12349                            tree_cons (NULL_TREE, V8QI_type_node,
12350                                       tree_cons (NULL_TREE, V8QI_type_node,
12351                                                  endlink)));
12352   tree v4hi_ftype_v4hi_v4hi
12353     = build_function_type (V4HI_type_node,
12354                            tree_cons (NULL_TREE, V4HI_type_node,
12355                                       tree_cons (NULL_TREE, V4HI_type_node,
12356                                                  endlink)));
12357   tree v2si_ftype_v2si_v2si
12358     = build_function_type (V2SI_type_node,
12359                            tree_cons (NULL_TREE, V2SI_type_node,
12360                                       tree_cons (NULL_TREE, V2SI_type_node,
12361                                                  endlink)));
12362   tree di_ftype_di_di
12363     = build_function_type (long_long_unsigned_type_node,
12364                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
12365                                       tree_cons (NULL_TREE,
12366                                                  long_long_unsigned_type_node,
12367                                                  endlink)));
12368
12369   /* Add all builtins that are more or less simple operations on two
12370      operands.  */
12371   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12372     {
12373       /* Use one of the operands; the target can have a different mode for
12374          mask-generating compares.  */
12375       enum machine_mode mode;
12376       tree type;
12377
12378       if (d->name == 0)
12379         continue;
12380
12381       mode = insn_data[d->icode].operand[1].mode;
12382
12383       switch (mode)
12384         {
12385         case V8QImode:
12386           type = v8qi_ftype_v8qi_v8qi;
12387           break;
12388         case V4HImode:
12389           type = v4hi_ftype_v4hi_v4hi;
12390           break;
12391         case V2SImode:
12392           type = v2si_ftype_v2si_v2si;
12393           break;
12394         case DImode:
12395           type = di_ftype_di_di;
12396           break;
12397
12398         default:
12399           gcc_unreachable ();
12400         }
12401
12402       def_mbuiltin (d->mask, d->name, type, d->code);
12403     }
12404
12405   /* Add the remaining MMX insns with somewhat more complicated types.  */
12406   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
12407   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
12408   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
12409
12410   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
12411   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
12412   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
12413   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
12414   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
12415   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
12416
12417   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
12418   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
12419   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
12420   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
12421   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
12422   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
12423
12424   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
12425   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
12426   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
12427   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
12428   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
12429   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
12430
12431   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
12432   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
12433   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
12434   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
12435   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
12436   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
12437
12438   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
12439
12440   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
12441   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
12442   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
12443   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
12444
12445   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
12446   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
12447   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
12448   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
12449   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
12450   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
12451   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
12452   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
12453   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
12454
12455   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
12456   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
12457   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
12458
12459   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
12460   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
12461   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
12462
12463   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
12464   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
12465   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
12466   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
12467   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
12468   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
12469
12470   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
12471   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
12472   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
12473   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
12474   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
12475   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
12476   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
12477   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
12478   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
12479   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
12480   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
12481   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
12482
12483   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
12484   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
12485   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
12486   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
12487
12488   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
12489   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
12490   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
12491   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
12492   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
12493   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
12494   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12495 }
12496
12497 static void
12498 arm_init_tls_builtins (void)
12499 {
12500   tree ftype;
12501   tree nothrow = tree_cons (get_identifier ("nothrow"), NULL, NULL);
12502   tree const_nothrow = tree_cons (get_identifier ("const"), NULL, nothrow);
12503
12504   ftype = build_function_type (ptr_type_node, void_list_node);
12505   lang_hooks.builtin_function ("__builtin_thread_pointer", ftype,
12506                                ARM_BUILTIN_THREAD_POINTER, BUILT_IN_MD,
12507                                NULL, const_nothrow);
12508 }
12509
12510 static void
12511 arm_init_builtins (void)
12512 {
12513   arm_init_tls_builtins ();
12514
12515   if (TARGET_REALLY_IWMMXT)
12516     arm_init_iwmmxt_builtins ();
12517 }
12518
12519 /* Errors in the source file can cause expand_expr to return const0_rtx
12520    where we expect a vector.  To avoid crashing, use one of the vector
12521    clear instructions.  */
12522
12523 static rtx
12524 safe_vector_operand (rtx x, enum machine_mode mode)
12525 {
12526   if (x != const0_rtx)
12527     return x;
12528   x = gen_reg_rtx (mode);
12529
12530   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12531                                : gen_rtx_SUBREG (DImode, x, 0)));
12532   return x;
12533 }
12534
12535 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12536
12537 static rtx
12538 arm_expand_binop_builtin (enum insn_code icode,
12539                           tree arglist, rtx target)
12540 {
12541   rtx pat;
12542   tree arg0 = TREE_VALUE (arglist);
12543   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12544   rtx op0 = expand_normal (arg0);
12545   rtx op1 = expand_normal (arg1);
12546   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12547   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12548   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12549
12550   if (VECTOR_MODE_P (mode0))
12551     op0 = safe_vector_operand (op0, mode0);
12552   if (VECTOR_MODE_P (mode1))
12553     op1 = safe_vector_operand (op1, mode1);
12554
12555   if (! target
12556       || GET_MODE (target) != tmode
12557       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12558     target = gen_reg_rtx (tmode);
12559
12560   gcc_assert (GET_MODE (op0) == mode0 && GET_MODE (op1) == mode1);
12561
12562   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12563     op0 = copy_to_mode_reg (mode0, op0);
12564   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12565     op1 = copy_to_mode_reg (mode1, op1);
12566
12567   pat = GEN_FCN (icode) (target, op0, op1);
12568   if (! pat)
12569     return 0;
12570   emit_insn (pat);
12571   return target;
12572 }
12573
12574 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12575
12576 static rtx
12577 arm_expand_unop_builtin (enum insn_code icode,
12578                          tree arglist, rtx target, int do_load)
12579 {
12580   rtx pat;
12581   tree arg0 = TREE_VALUE (arglist);
12582   rtx op0 = expand_normal (arg0);
12583   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12584   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12585
12586   if (! target
12587       || GET_MODE (target) != tmode
12588       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12589     target = gen_reg_rtx (tmode);
12590   if (do_load)
12591     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12592   else
12593     {
12594       if (VECTOR_MODE_P (mode0))
12595         op0 = safe_vector_operand (op0, mode0);
12596
12597       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12598         op0 = copy_to_mode_reg (mode0, op0);
12599     }
12600
12601   pat = GEN_FCN (icode) (target, op0);
12602   if (! pat)
12603     return 0;
12604   emit_insn (pat);
12605   return target;
12606 }
12607
12608 /* Expand an expression EXP that calls a built-in function,
12609    with result going to TARGET if that's convenient
12610    (and in mode MODE if that's convenient).
12611    SUBTARGET may be used as the target for computing one of EXP's operands.
12612    IGNORE is nonzero if the value is to be ignored.  */
12613
12614 static rtx
12615 arm_expand_builtin (tree exp,
12616                     rtx target,
12617                     rtx subtarget ATTRIBUTE_UNUSED,
12618                     enum machine_mode mode ATTRIBUTE_UNUSED,
12619                     int ignore ATTRIBUTE_UNUSED)
12620 {
12621   const struct builtin_description * d;
12622   enum insn_code    icode;
12623   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12624   tree              arglist = TREE_OPERAND (exp, 1);
12625   tree              arg0;
12626   tree              arg1;
12627   tree              arg2;
12628   rtx               op0;
12629   rtx               op1;
12630   rtx               op2;
12631   rtx               pat;
12632   int               fcode = DECL_FUNCTION_CODE (fndecl);
12633   size_t            i;
12634   enum machine_mode tmode;
12635   enum machine_mode mode0;
12636   enum machine_mode mode1;
12637   enum machine_mode mode2;
12638
12639   switch (fcode)
12640     {
12641     case ARM_BUILTIN_TEXTRMSB:
12642     case ARM_BUILTIN_TEXTRMUB:
12643     case ARM_BUILTIN_TEXTRMSH:
12644     case ARM_BUILTIN_TEXTRMUH:
12645     case ARM_BUILTIN_TEXTRMSW:
12646     case ARM_BUILTIN_TEXTRMUW:
12647       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12648                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12649                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12650                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12651                : CODE_FOR_iwmmxt_textrmw);
12652
12653       arg0 = TREE_VALUE (arglist);
12654       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12655       op0 = expand_normal (arg0);
12656       op1 = expand_normal (arg1);
12657       tmode = insn_data[icode].operand[0].mode;
12658       mode0 = insn_data[icode].operand[1].mode;
12659       mode1 = insn_data[icode].operand[2].mode;
12660
12661       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12662         op0 = copy_to_mode_reg (mode0, op0);
12663       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12664         {
12665           /* @@@ better error message */
12666           error ("selector must be an immediate");
12667           return gen_reg_rtx (tmode);
12668         }
12669       if (target == 0
12670           || GET_MODE (target) != tmode
12671           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12672         target = gen_reg_rtx (tmode);
12673       pat = GEN_FCN (icode) (target, op0, op1);
12674       if (! pat)
12675         return 0;
12676       emit_insn (pat);
12677       return target;
12678
12679     case ARM_BUILTIN_TINSRB:
12680     case ARM_BUILTIN_TINSRH:
12681     case ARM_BUILTIN_TINSRW:
12682       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12683                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12684                : CODE_FOR_iwmmxt_tinsrw);
12685       arg0 = TREE_VALUE (arglist);
12686       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12687       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12688       op0 = expand_normal (arg0);
12689       op1 = expand_normal (arg1);
12690       op2 = expand_normal (arg2);
12691       tmode = insn_data[icode].operand[0].mode;
12692       mode0 = insn_data[icode].operand[1].mode;
12693       mode1 = insn_data[icode].operand[2].mode;
12694       mode2 = insn_data[icode].operand[3].mode;
12695
12696       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12697         op0 = copy_to_mode_reg (mode0, op0);
12698       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12699         op1 = copy_to_mode_reg (mode1, op1);
12700       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12701         {
12702           /* @@@ better error message */
12703           error ("selector must be an immediate");
12704           return const0_rtx;
12705         }
12706       if (target == 0
12707           || GET_MODE (target) != tmode
12708           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12709         target = gen_reg_rtx (tmode);
12710       pat = GEN_FCN (icode) (target, op0, op1, op2);
12711       if (! pat)
12712         return 0;
12713       emit_insn (pat);
12714       return target;
12715
12716     case ARM_BUILTIN_SETWCX:
12717       arg0 = TREE_VALUE (arglist);
12718       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12719       op0 = force_reg (SImode, expand_normal (arg0));
12720       op1 = expand_normal (arg1);
12721       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12722       return 0;
12723
12724     case ARM_BUILTIN_GETWCX:
12725       arg0 = TREE_VALUE (arglist);
12726       op0 = expand_normal (arg0);
12727       target = gen_reg_rtx (SImode);
12728       emit_insn (gen_iwmmxt_tmrc (target, op0));
12729       return target;
12730
12731     case ARM_BUILTIN_WSHUFH:
12732       icode = CODE_FOR_iwmmxt_wshufh;
12733       arg0 = TREE_VALUE (arglist);
12734       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12735       op0 = expand_normal (arg0);
12736       op1 = expand_normal (arg1);
12737       tmode = insn_data[icode].operand[0].mode;
12738       mode1 = insn_data[icode].operand[1].mode;
12739       mode2 = insn_data[icode].operand[2].mode;
12740
12741       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12742         op0 = copy_to_mode_reg (mode1, op0);
12743       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12744         {
12745           /* @@@ better error message */
12746           error ("mask must be an immediate");
12747           return const0_rtx;
12748         }
12749       if (target == 0
12750           || GET_MODE (target) != tmode
12751           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12752         target = gen_reg_rtx (tmode);
12753       pat = GEN_FCN (icode) (target, op0, op1);
12754       if (! pat)
12755         return 0;
12756       emit_insn (pat);
12757       return target;
12758
12759     case ARM_BUILTIN_WSADB:
12760       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12761     case ARM_BUILTIN_WSADH:
12762       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12763     case ARM_BUILTIN_WSADBZ:
12764       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12765     case ARM_BUILTIN_WSADHZ:
12766       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12767
12768       /* Several three-argument builtins.  */
12769     case ARM_BUILTIN_WMACS:
12770     case ARM_BUILTIN_WMACU:
12771     case ARM_BUILTIN_WALIGN:
12772     case ARM_BUILTIN_TMIA:
12773     case ARM_BUILTIN_TMIAPH:
12774     case ARM_BUILTIN_TMIATT:
12775     case ARM_BUILTIN_TMIATB:
12776     case ARM_BUILTIN_TMIABT:
12777     case ARM_BUILTIN_TMIABB:
12778       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12779                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12780                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12781                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12782                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12783                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12784                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12785                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12786                : CODE_FOR_iwmmxt_walign);
12787       arg0 = TREE_VALUE (arglist);
12788       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12789       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12790       op0 = expand_normal (arg0);
12791       op1 = expand_normal (arg1);
12792       op2 = expand_normal (arg2);
12793       tmode = insn_data[icode].operand[0].mode;
12794       mode0 = insn_data[icode].operand[1].mode;
12795       mode1 = insn_data[icode].operand[2].mode;
12796       mode2 = insn_data[icode].operand[3].mode;
12797
12798       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12799         op0 = copy_to_mode_reg (mode0, op0);
12800       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12801         op1 = copy_to_mode_reg (mode1, op1);
12802       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12803         op2 = copy_to_mode_reg (mode2, op2);
12804       if (target == 0
12805           || GET_MODE (target) != tmode
12806           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12807         target = gen_reg_rtx (tmode);
12808       pat = GEN_FCN (icode) (target, op0, op1, op2);
12809       if (! pat)
12810         return 0;
12811       emit_insn (pat);
12812       return target;
12813
12814     case ARM_BUILTIN_WZERO:
12815       target = gen_reg_rtx (DImode);
12816       emit_insn (gen_iwmmxt_clrdi (target));
12817       return target;
12818
12819     case ARM_BUILTIN_THREAD_POINTER:
12820       return arm_load_tp (target);
12821
12822     default:
12823       break;
12824     }
12825
12826   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12827     if (d->code == (const enum arm_builtins) fcode)
12828       return arm_expand_binop_builtin (d->icode, arglist, target);
12829
12830   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12831     if (d->code == (const enum arm_builtins) fcode)
12832       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12833
12834   /* @@@ Should really do something sensible here.  */
12835   return NULL_RTX;
12836 }
12837 \f
12838 /* Return the number (counting from 0) of
12839    the least significant set bit in MASK.  */
12840
12841 inline static int
12842 number_of_first_bit_set (unsigned mask)
12843 {
12844   int bit;
12845
12846   for (bit = 0;
12847        (mask & (1 << bit)) == 0;
12848        ++bit)
12849     continue;
12850
12851   return bit;
12852 }
12853
12854 /* Emit code to push or pop registers to or from the stack.  F is the
12855    assembly file.  MASK is the registers to push or pop.  PUSH is
12856    nonzero if we should push, and zero if we should pop.  For debugging
12857    output, if pushing, adjust CFA_OFFSET by the amount of space added
12858    to the stack.  REAL_REGS should have the same number of bits set as
12859    MASK, and will be used instead (in the same order) to describe which
12860    registers were saved - this is used to mark the save slots when we
12861    push high registers after moving them to low registers.  */
12862 static void
12863 thumb_pushpop (FILE *f, unsigned long mask, int push, int *cfa_offset,
12864                unsigned long real_regs)
12865 {
12866   int regno;
12867   int lo_mask = mask & 0xFF;
12868   int pushed_words = 0;
12869
12870   gcc_assert (mask);
12871
12872   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12873     {
12874       /* Special case.  Do not generate a POP PC statement here, do it in
12875          thumb_exit() */
12876       thumb_exit (f, -1);
12877       return;
12878     }
12879
12880   if (ARM_EABI_UNWIND_TABLES && push)
12881     {
12882       fprintf (f, "\t.save\t{");
12883       for (regno = 0; regno < 15; regno++)
12884         {
12885           if (real_regs & (1 << regno))
12886             {
12887               if (real_regs & ((1 << regno) -1))
12888                 fprintf (f, ", ");
12889               asm_fprintf (f, "%r", regno);
12890             }
12891         }
12892       fprintf (f, "}\n");
12893     }
12894
12895   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12896
12897   /* Look at the low registers first.  */
12898   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12899     {
12900       if (lo_mask & 1)
12901         {
12902           asm_fprintf (f, "%r", regno);
12903
12904           if ((lo_mask & ~1) != 0)
12905             fprintf (f, ", ");
12906
12907           pushed_words++;
12908         }
12909     }
12910
12911   if (push && (mask & (1 << LR_REGNUM)))
12912     {
12913       /* Catch pushing the LR.  */
12914       if (mask & 0xFF)
12915         fprintf (f, ", ");
12916
12917       asm_fprintf (f, "%r", LR_REGNUM);
12918
12919       pushed_words++;
12920     }
12921   else if (!push && (mask & (1 << PC_REGNUM)))
12922     {
12923       /* Catch popping the PC.  */
12924       if (TARGET_INTERWORK || TARGET_BACKTRACE
12925           || current_function_calls_eh_return)
12926         {
12927           /* The PC is never poped directly, instead
12928              it is popped into r3 and then BX is used.  */
12929           fprintf (f, "}\n");
12930
12931           thumb_exit (f, -1);
12932
12933           return;
12934         }
12935       else
12936         {
12937           if (mask & 0xFF)
12938             fprintf (f, ", ");
12939
12940           asm_fprintf (f, "%r", PC_REGNUM);
12941         }
12942     }
12943
12944   fprintf (f, "}\n");
12945
12946   if (push && pushed_words && dwarf2out_do_frame ())
12947     {
12948       char *l = dwarf2out_cfi_label ();
12949       int pushed_mask = real_regs;
12950
12951       *cfa_offset += pushed_words * 4;
12952       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12953
12954       pushed_words = 0;
12955       pushed_mask = real_regs;
12956       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12957         {
12958           if (pushed_mask & 1)
12959             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12960         }
12961     }
12962 }
12963
12964 /* Generate code to return from a thumb function.
12965    If 'reg_containing_return_addr' is -1, then the return address is
12966    actually on the stack, at the stack pointer.  */
12967 static void
12968 thumb_exit (FILE *f, int reg_containing_return_addr)
12969 {
12970   unsigned regs_available_for_popping;
12971   unsigned regs_to_pop;
12972   int pops_needed;
12973   unsigned available;
12974   unsigned required;
12975   int mode;
12976   int size;
12977   int restore_a4 = FALSE;
12978
12979   /* Compute the registers we need to pop.  */
12980   regs_to_pop = 0;
12981   pops_needed = 0;
12982
12983   if (reg_containing_return_addr == -1)
12984     {
12985       regs_to_pop |= 1 << LR_REGNUM;
12986       ++pops_needed;
12987     }
12988
12989   if (TARGET_BACKTRACE)
12990     {
12991       /* Restore the (ARM) frame pointer and stack pointer.  */
12992       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12993       pops_needed += 2;
12994     }
12995
12996   /* If there is nothing to pop then just emit the BX instruction and
12997      return.  */
12998   if (pops_needed == 0)
12999     {
13000       if (current_function_calls_eh_return)
13001         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13002
13003       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13004       return;
13005     }
13006   /* Otherwise if we are not supporting interworking and we have not created
13007      a backtrace structure and the function was not entered in ARM mode then
13008      just pop the return address straight into the PC.  */
13009   else if (!TARGET_INTERWORK
13010            && !TARGET_BACKTRACE
13011            && !is_called_in_ARM_mode (current_function_decl)
13012            && !current_function_calls_eh_return)
13013     {
13014       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
13015       return;
13016     }
13017
13018   /* Find out how many of the (return) argument registers we can corrupt.  */
13019   regs_available_for_popping = 0;
13020
13021   /* If returning via __builtin_eh_return, the bottom three registers
13022      all contain information needed for the return.  */
13023   if (current_function_calls_eh_return)
13024     size = 12;
13025   else
13026     {
13027       /* If we can deduce the registers used from the function's
13028          return value.  This is more reliable that examining
13029          regs_ever_live[] because that will be set if the register is
13030          ever used in the function, not just if the register is used
13031          to hold a return value.  */
13032
13033       if (current_function_return_rtx != 0)
13034         mode = GET_MODE (current_function_return_rtx);
13035       else
13036         mode = DECL_MODE (DECL_RESULT (current_function_decl));
13037
13038       size = GET_MODE_SIZE (mode);
13039
13040       if (size == 0)
13041         {
13042           /* In a void function we can use any argument register.
13043              In a function that returns a structure on the stack
13044              we can use the second and third argument registers.  */
13045           if (mode == VOIDmode)
13046             regs_available_for_popping =
13047               (1 << ARG_REGISTER (1))
13048               | (1 << ARG_REGISTER (2))
13049               | (1 << ARG_REGISTER (3));
13050           else
13051             regs_available_for_popping =
13052               (1 << ARG_REGISTER (2))
13053               | (1 << ARG_REGISTER (3));
13054         }
13055       else if (size <= 4)
13056         regs_available_for_popping =
13057           (1 << ARG_REGISTER (2))
13058           | (1 << ARG_REGISTER (3));
13059       else if (size <= 8)
13060         regs_available_for_popping =
13061           (1 << ARG_REGISTER (3));
13062     }
13063
13064   /* Match registers to be popped with registers into which we pop them.  */
13065   for (available = regs_available_for_popping,
13066        required  = regs_to_pop;
13067        required != 0 && available != 0;
13068        available &= ~(available & - available),
13069        required  &= ~(required  & - required))
13070     -- pops_needed;
13071
13072   /* If we have any popping registers left over, remove them.  */
13073   if (available > 0)
13074     regs_available_for_popping &= ~available;
13075
13076   /* Otherwise if we need another popping register we can use
13077      the fourth argument register.  */
13078   else if (pops_needed)
13079     {
13080       /* If we have not found any free argument registers and
13081          reg a4 contains the return address, we must move it.  */
13082       if (regs_available_for_popping == 0
13083           && reg_containing_return_addr == LAST_ARG_REGNUM)
13084         {
13085           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13086           reg_containing_return_addr = LR_REGNUM;
13087         }
13088       else if (size > 12)
13089         {
13090           /* Register a4 is being used to hold part of the return value,
13091              but we have dire need of a free, low register.  */
13092           restore_a4 = TRUE;
13093
13094           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
13095         }
13096
13097       if (reg_containing_return_addr != LAST_ARG_REGNUM)
13098         {
13099           /* The fourth argument register is available.  */
13100           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
13101
13102           --pops_needed;
13103         }
13104     }
13105
13106   /* Pop as many registers as we can.  */
13107   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13108                  regs_available_for_popping);
13109
13110   /* Process the registers we popped.  */
13111   if (reg_containing_return_addr == -1)
13112     {
13113       /* The return address was popped into the lowest numbered register.  */
13114       regs_to_pop &= ~(1 << LR_REGNUM);
13115
13116       reg_containing_return_addr =
13117         number_of_first_bit_set (regs_available_for_popping);
13118
13119       /* Remove this register for the mask of available registers, so that
13120          the return address will not be corrupted by further pops.  */
13121       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
13122     }
13123
13124   /* If we popped other registers then handle them here.  */
13125   if (regs_available_for_popping)
13126     {
13127       int frame_pointer;
13128
13129       /* Work out which register currently contains the frame pointer.  */
13130       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
13131
13132       /* Move it into the correct place.  */
13133       asm_fprintf (f, "\tmov\t%r, %r\n",
13134                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
13135
13136       /* (Temporarily) remove it from the mask of popped registers.  */
13137       regs_available_for_popping &= ~(1 << frame_pointer);
13138       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
13139
13140       if (regs_available_for_popping)
13141         {
13142           int stack_pointer;
13143
13144           /* We popped the stack pointer as well,
13145              find the register that contains it.  */
13146           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
13147
13148           /* Move it into the stack register.  */
13149           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
13150
13151           /* At this point we have popped all necessary registers, so
13152              do not worry about restoring regs_available_for_popping
13153              to its correct value:
13154
13155              assert (pops_needed == 0)
13156              assert (regs_available_for_popping == (1 << frame_pointer))
13157              assert (regs_to_pop == (1 << STACK_POINTER))  */
13158         }
13159       else
13160         {
13161           /* Since we have just move the popped value into the frame
13162              pointer, the popping register is available for reuse, and
13163              we know that we still have the stack pointer left to pop.  */
13164           regs_available_for_popping |= (1 << frame_pointer);
13165         }
13166     }
13167
13168   /* If we still have registers left on the stack, but we no longer have
13169      any registers into which we can pop them, then we must move the return
13170      address into the link register and make available the register that
13171      contained it.  */
13172   if (regs_available_for_popping == 0 && pops_needed > 0)
13173     {
13174       regs_available_for_popping |= 1 << reg_containing_return_addr;
13175
13176       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
13177                    reg_containing_return_addr);
13178
13179       reg_containing_return_addr = LR_REGNUM;
13180     }
13181
13182   /* If we have registers left on the stack then pop some more.
13183      We know that at most we will want to pop FP and SP.  */
13184   if (pops_needed > 0)
13185     {
13186       int  popped_into;
13187       int  move_to;
13188
13189       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13190                      regs_available_for_popping);
13191
13192       /* We have popped either FP or SP.
13193          Move whichever one it is into the correct register.  */
13194       popped_into = number_of_first_bit_set (regs_available_for_popping);
13195       move_to     = number_of_first_bit_set (regs_to_pop);
13196
13197       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
13198
13199       regs_to_pop &= ~(1 << move_to);
13200
13201       --pops_needed;
13202     }
13203
13204   /* If we still have not popped everything then we must have only
13205      had one register available to us and we are now popping the SP.  */
13206   if (pops_needed > 0)
13207     {
13208       int  popped_into;
13209
13210       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
13211                      regs_available_for_popping);
13212
13213       popped_into = number_of_first_bit_set (regs_available_for_popping);
13214
13215       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
13216       /*
13217         assert (regs_to_pop == (1 << STACK_POINTER))
13218         assert (pops_needed == 1)
13219       */
13220     }
13221
13222   /* If necessary restore the a4 register.  */
13223   if (restore_a4)
13224     {
13225       if (reg_containing_return_addr != LR_REGNUM)
13226         {
13227           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
13228           reg_containing_return_addr = LR_REGNUM;
13229         }
13230
13231       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
13232     }
13233
13234   if (current_function_calls_eh_return)
13235     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
13236
13237   /* Return to caller.  */
13238   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
13239 }
13240
13241 \f
13242 void
13243 thumb_final_prescan_insn (rtx insn)
13244 {
13245   if (flag_print_asm_name)
13246     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
13247                  INSN_ADDRESSES (INSN_UID (insn)));
13248 }
13249
13250 int
13251 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
13252 {
13253   unsigned HOST_WIDE_INT mask = 0xff;
13254   int i;
13255
13256   if (val == 0) /* XXX */
13257     return 0;
13258
13259   for (i = 0; i < 25; i++)
13260     if ((val & (mask << i)) == val)
13261       return 1;
13262
13263   return 0;
13264 }
13265
13266 /* Returns nonzero if the current function contains,
13267    or might contain a far jump.  */
13268 static int
13269 thumb_far_jump_used_p (void)
13270 {
13271   rtx insn;
13272
13273   /* This test is only important for leaf functions.  */
13274   /* assert (!leaf_function_p ()); */
13275
13276   /* If we have already decided that far jumps may be used,
13277      do not bother checking again, and always return true even if
13278      it turns out that they are not being used.  Once we have made
13279      the decision that far jumps are present (and that hence the link
13280      register will be pushed onto the stack) we cannot go back on it.  */
13281   if (cfun->machine->far_jump_used)
13282     return 1;
13283
13284   /* If this function is not being called from the prologue/epilogue
13285      generation code then it must be being called from the
13286      INITIAL_ELIMINATION_OFFSET macro.  */
13287   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
13288     {
13289       /* In this case we know that we are being asked about the elimination
13290          of the arg pointer register.  If that register is not being used,
13291          then there are no arguments on the stack, and we do not have to
13292          worry that a far jump might force the prologue to push the link
13293          register, changing the stack offsets.  In this case we can just
13294          return false, since the presence of far jumps in the function will
13295          not affect stack offsets.
13296
13297          If the arg pointer is live (or if it was live, but has now been
13298          eliminated and so set to dead) then we do have to test to see if
13299          the function might contain a far jump.  This test can lead to some
13300          false negatives, since before reload is completed, then length of
13301          branch instructions is not known, so gcc defaults to returning their
13302          longest length, which in turn sets the far jump attribute to true.
13303
13304          A false negative will not result in bad code being generated, but it
13305          will result in a needless push and pop of the link register.  We
13306          hope that this does not occur too often.
13307
13308          If we need doubleword stack alignment this could affect the other
13309          elimination offsets so we can't risk getting it wrong.  */
13310       if (regs_ever_live [ARG_POINTER_REGNUM])
13311         cfun->machine->arg_pointer_live = 1;
13312       else if (!cfun->machine->arg_pointer_live)
13313         return 0;
13314     }
13315
13316   /* Check to see if the function contains a branch
13317      insn with the far jump attribute set.  */
13318   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
13319     {
13320       if (GET_CODE (insn) == JUMP_INSN
13321           /* Ignore tablejump patterns.  */
13322           && GET_CODE (PATTERN (insn)) != ADDR_VEC
13323           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
13324           && get_attr_far_jump (insn) == FAR_JUMP_YES
13325           )
13326         {
13327           /* Record the fact that we have decided that
13328              the function does use far jumps.  */
13329           cfun->machine->far_jump_used = 1;
13330           return 1;
13331         }
13332     }
13333
13334   return 0;
13335 }
13336
13337 /* Return nonzero if FUNC must be entered in ARM mode.  */
13338 int
13339 is_called_in_ARM_mode (tree func)
13340 {
13341   gcc_assert (TREE_CODE (func) == FUNCTION_DECL);
13342
13343   /* Ignore the problem about functions whose address is taken.  */
13344   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
13345     return TRUE;
13346
13347 #ifdef ARM_PE
13348   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
13349 #else
13350   return FALSE;
13351 #endif
13352 }
13353
13354 /* The bits which aren't usefully expanded as rtl.  */
13355 const char *
13356 thumb_unexpanded_epilogue (void)
13357 {
13358   int regno;
13359   unsigned long live_regs_mask = 0;
13360   int high_regs_pushed = 0;
13361   int had_to_push_lr;
13362   int size;
13363
13364   if (return_used_this_function)
13365     return "";
13366
13367   if (IS_NAKED (arm_current_func_type ()))
13368     return "";
13369
13370   live_regs_mask = thumb_compute_save_reg_mask ();
13371   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13372
13373   /* If we can deduce the registers used from the function's return value.
13374      This is more reliable that examining regs_ever_live[] because that
13375      will be set if the register is ever used in the function, not just if
13376      the register is used to hold a return value.  */
13377   size = arm_size_return_regs ();
13378
13379   /* The prolog may have pushed some high registers to use as
13380      work registers.  e.g. the testsuite file:
13381      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
13382      compiles to produce:
13383         push    {r4, r5, r6, r7, lr}
13384         mov     r7, r9
13385         mov     r6, r8
13386         push    {r6, r7}
13387      as part of the prolog.  We have to undo that pushing here.  */
13388
13389   if (high_regs_pushed)
13390     {
13391       unsigned long mask = live_regs_mask & 0xff;
13392       int next_hi_reg;
13393
13394       /* The available low registers depend on the size of the value we are
13395          returning.  */
13396       if (size <= 12)
13397         mask |=  1 << 3;
13398       if (size <= 8)
13399         mask |= 1 << 2;
13400
13401       if (mask == 0)
13402         /* Oh dear!  We have no low registers into which we can pop
13403            high registers!  */
13404         internal_error
13405           ("no low registers available for popping high registers");
13406
13407       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
13408         if (live_regs_mask & (1 << next_hi_reg))
13409           break;
13410
13411       while (high_regs_pushed)
13412         {
13413           /* Find lo register(s) into which the high register(s) can
13414              be popped.  */
13415           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13416             {
13417               if (mask & (1 << regno))
13418                 high_regs_pushed--;
13419               if (high_regs_pushed == 0)
13420                 break;
13421             }
13422
13423           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
13424
13425           /* Pop the values into the low register(s).  */
13426           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
13427
13428           /* Move the value(s) into the high registers.  */
13429           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
13430             {
13431               if (mask & (1 << regno))
13432                 {
13433                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
13434                                regno);
13435
13436                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
13437                     if (live_regs_mask & (1 << next_hi_reg))
13438                       break;
13439                 }
13440             }
13441         }
13442       live_regs_mask &= ~0x0f00;
13443     }
13444
13445   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
13446   live_regs_mask &= 0xff;
13447
13448   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
13449     {
13450       /* Pop the return address into the PC.  */
13451       if (had_to_push_lr)
13452         live_regs_mask |= 1 << PC_REGNUM;
13453
13454       /* Either no argument registers were pushed or a backtrace
13455          structure was created which includes an adjusted stack
13456          pointer, so just pop everything.  */
13457       if (live_regs_mask)
13458         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13459                        live_regs_mask);
13460
13461       /* We have either just popped the return address into the
13462          PC or it is was kept in LR for the entire function.  */
13463       if (!had_to_push_lr)
13464         thumb_exit (asm_out_file, LR_REGNUM);
13465     }
13466   else
13467     {
13468       /* Pop everything but the return address.  */
13469       if (live_regs_mask)
13470         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
13471                        live_regs_mask);
13472
13473       if (had_to_push_lr)
13474         {
13475           if (size > 12)
13476             {
13477               /* We have no free low regs, so save one.  */
13478               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
13479                            LAST_ARG_REGNUM);
13480             }
13481
13482           /* Get the return address into a temporary register.  */
13483           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
13484                          1 << LAST_ARG_REGNUM);
13485
13486           if (size > 12)
13487             {
13488               /* Move the return address to lr.  */
13489               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13490                            LAST_ARG_REGNUM);
13491               /* Restore the low register.  */
13492               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13493                            IP_REGNUM);
13494               regno = LR_REGNUM;
13495             }
13496           else
13497             regno = LAST_ARG_REGNUM;
13498         }
13499       else
13500         regno = LR_REGNUM;
13501
13502       /* Remove the argument registers that were pushed onto the stack.  */
13503       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13504                    SP_REGNUM, SP_REGNUM,
13505                    current_function_pretend_args_size);
13506
13507       thumb_exit (asm_out_file, regno);
13508     }
13509
13510   return "";
13511 }
13512
13513 /* Functions to save and restore machine-specific function data.  */
13514 static struct machine_function *
13515 arm_init_machine_status (void)
13516 {
13517   struct machine_function *machine;
13518   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13519
13520 #if ARM_FT_UNKNOWN != 0
13521   machine->func_type = ARM_FT_UNKNOWN;
13522 #endif
13523   return machine;
13524 }
13525
13526 /* Return an RTX indicating where the return address to the
13527    calling function can be found.  */
13528 rtx
13529 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13530 {
13531   if (count != 0)
13532     return NULL_RTX;
13533
13534   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13535 }
13536
13537 /* Do anything needed before RTL is emitted for each function.  */
13538 void
13539 arm_init_expanders (void)
13540 {
13541   /* Arrange to initialize and mark the machine per-function status.  */
13542   init_machine_status = arm_init_machine_status;
13543
13544   /* This is to stop the combine pass optimizing away the alignment
13545      adjustment of va_arg.  */
13546   /* ??? It is claimed that this should not be necessary.  */
13547   if (cfun)
13548     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13549 }
13550
13551
13552 /* Like arm_compute_initial_elimination offset.  Simpler because there
13553    isn't an ABI specified frame pointer for Thumb.  Instead, we set it
13554    to point at the base of the local variables after static stack
13555    space for a function has been allocated.  */
13556
13557 HOST_WIDE_INT
13558 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13559 {
13560   arm_stack_offsets *offsets;
13561
13562   offsets = arm_get_frame_offsets ();
13563
13564   switch (from)
13565     {
13566     case ARG_POINTER_REGNUM:
13567       switch (to)
13568         {
13569         case STACK_POINTER_REGNUM:
13570           return offsets->outgoing_args - offsets->saved_args;
13571
13572         case FRAME_POINTER_REGNUM:
13573           return offsets->soft_frame - offsets->saved_args;
13574
13575         case ARM_HARD_FRAME_POINTER_REGNUM:
13576           return offsets->saved_regs - offsets->saved_args;
13577
13578         case THUMB_HARD_FRAME_POINTER_REGNUM:
13579           return offsets->locals_base - offsets->saved_args;
13580
13581         default:
13582           gcc_unreachable ();
13583         }
13584       break;
13585
13586     case FRAME_POINTER_REGNUM:
13587       switch (to)
13588         {
13589         case STACK_POINTER_REGNUM:
13590           return offsets->outgoing_args - offsets->soft_frame;
13591
13592         case ARM_HARD_FRAME_POINTER_REGNUM:
13593           return offsets->saved_regs - offsets->soft_frame;
13594
13595         case THUMB_HARD_FRAME_POINTER_REGNUM:
13596           return offsets->locals_base - offsets->soft_frame;
13597
13598         default:
13599           gcc_unreachable ();
13600         }
13601       break;
13602
13603     default:
13604       gcc_unreachable ();
13605     }
13606 }
13607
13608
13609 /* Generate the rest of a function's prologue.  */
13610 void
13611 thumb_expand_prologue (void)
13612 {
13613   rtx insn, dwarf;
13614
13615   HOST_WIDE_INT amount;
13616   arm_stack_offsets *offsets;
13617   unsigned long func_type;
13618   int regno;
13619   unsigned long live_regs_mask;
13620
13621   func_type = arm_current_func_type ();
13622
13623   /* Naked functions don't have prologues.  */
13624   if (IS_NAKED (func_type))
13625     return;
13626
13627   if (IS_INTERRUPT (func_type))
13628     {
13629       error ("interrupt Service Routines cannot be coded in Thumb mode");
13630       return;
13631     }
13632
13633   live_regs_mask = thumb_compute_save_reg_mask ();
13634   /* Load the pic register before setting the frame pointer,
13635      so we can use r7 as a temporary work register.  */
13636   if (flag_pic && arm_pic_register != INVALID_REGNUM)
13637     arm_load_pic_register (live_regs_mask);
13638
13639   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
13640     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13641                     stack_pointer_rtx);
13642
13643   offsets = arm_get_frame_offsets ();
13644   amount = offsets->outgoing_args - offsets->saved_regs;
13645   if (amount)
13646     {
13647       if (amount < 512)
13648         {
13649           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13650                                         GEN_INT (- amount)));
13651           RTX_FRAME_RELATED_P (insn) = 1;
13652         }
13653       else
13654         {
13655           rtx reg;
13656
13657           /* The stack decrement is too big for an immediate value in a single
13658              insn.  In theory we could issue multiple subtracts, but after
13659              three of them it becomes more space efficient to place the full
13660              value in the constant pool and load into a register.  (Also the
13661              ARM debugger really likes to see only one stack decrement per
13662              function).  So instead we look for a scratch register into which
13663              we can load the decrement, and then we subtract this from the
13664              stack pointer.  Unfortunately on the thumb the only available
13665              scratch registers are the argument registers, and we cannot use
13666              these as they may hold arguments to the function.  Instead we
13667              attempt to locate a call preserved register which is used by this
13668              function.  If we can find one, then we know that it will have
13669              been pushed at the start of the prologue and so we can corrupt
13670              it now.  */
13671           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13672             if (live_regs_mask & (1 << regno)
13673                 && !(frame_pointer_needed
13674                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13675               break;
13676
13677           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13678             {
13679               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13680
13681               /* Choose an arbitrary, non-argument low register.  */
13682               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13683
13684               /* Save it by copying it into a high, scratch register.  */
13685               emit_insn (gen_movsi (spare, reg));
13686               /* Add a USE to stop propagate_one_insn() from barfing.  */
13687               emit_insn (gen_prologue_use (spare));
13688
13689               /* Decrement the stack.  */
13690               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
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 (VOIDmode, 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               /* Restore the low register's original value.  */
13703               emit_insn (gen_movsi (reg, spare));
13704
13705               /* Emit a USE of the restored scratch register, so that flow
13706                  analysis will not consider the restore redundant.  The
13707                  register won't be used again in this function and isn't
13708                  restored by the epilogue.  */
13709               emit_insn (gen_prologue_use (reg));
13710             }
13711           else
13712             {
13713               reg = gen_rtx_REG (SImode, regno);
13714
13715               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13716
13717               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13718                                             stack_pointer_rtx, reg));
13719               RTX_FRAME_RELATED_P (insn) = 1;
13720               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
13721                                    plus_constant (stack_pointer_rtx,
13722                                                   -amount));
13723               RTX_FRAME_RELATED_P (dwarf) = 1;
13724               REG_NOTES (insn)
13725                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13726                                      REG_NOTES (insn));
13727             }
13728         }
13729     }
13730
13731   if (frame_pointer_needed)
13732     {
13733       amount = offsets->outgoing_args - offsets->locals_base;
13734
13735       if (amount < 1024)
13736         insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13737                                       stack_pointer_rtx, GEN_INT (amount)));
13738       else
13739         {
13740           emit_insn (gen_movsi (hard_frame_pointer_rtx, GEN_INT (amount)));
13741           insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
13742                                         hard_frame_pointer_rtx,
13743                                         stack_pointer_rtx));
13744           dwarf = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
13745                                plus_constant (stack_pointer_rtx, amount));
13746           RTX_FRAME_RELATED_P (dwarf) = 1;
13747           REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13748                                                 REG_NOTES (insn));
13749         }
13750
13751       RTX_FRAME_RELATED_P (insn) = 1;
13752     }
13753
13754   /* If we are profiling, make sure no instructions are scheduled before
13755      the call to mcount.  Similarly if the user has requested no
13756      scheduling in the prolog.  Similarly if we want non-call exceptions
13757      using the EABI unwinder, to prevent faulting instructions from being
13758      swapped with a stack adjustment.  */
13759   if (current_function_profile || !TARGET_SCHED_PROLOG
13760       || (ARM_EABI_UNWIND_TABLES && flag_non_call_exceptions))
13761     emit_insn (gen_blockage ());
13762
13763   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13764   if (live_regs_mask & 0xff)
13765     cfun->machine->lr_save_eliminated = 0;
13766
13767   /* If the link register is being kept alive, with the return address in it,
13768      then make sure that it does not get reused by the ce2 pass.  */
13769   if (cfun->machine->lr_save_eliminated)
13770     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13771 }
13772
13773
13774 void
13775 thumb_expand_epilogue (void)
13776 {
13777   HOST_WIDE_INT amount;
13778   arm_stack_offsets *offsets;
13779   int regno;
13780
13781   /* Naked functions don't have prologues.  */
13782   if (IS_NAKED (arm_current_func_type ()))
13783     return;
13784
13785   offsets = arm_get_frame_offsets ();
13786   amount = offsets->outgoing_args - offsets->saved_regs;
13787
13788   if (frame_pointer_needed)
13789     {
13790       emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13791       amount = offsets->locals_base - offsets->saved_regs;
13792     }
13793
13794   if (amount)
13795     {
13796       if (amount < 512)
13797         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13798                                GEN_INT (amount)));
13799       else
13800         {
13801           /* r3 is always free in the epilogue.  */
13802           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13803
13804           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13805           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13806         }
13807     }
13808
13809   /* Emit a USE (stack_pointer_rtx), so that
13810      the stack adjustment will not be deleted.  */
13811   emit_insn (gen_prologue_use (stack_pointer_rtx));
13812
13813   if (current_function_profile || !TARGET_SCHED_PROLOG)
13814     emit_insn (gen_blockage ());
13815
13816   /* Emit a clobber for each insn that will be restored in the epilogue,
13817      so that flow2 will get register lifetimes correct.  */
13818   for (regno = 0; regno < 13; regno++)
13819     if (regs_ever_live[regno] && !call_used_regs[regno])
13820       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13821
13822   if (! regs_ever_live[LR_REGNUM])
13823     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13824 }
13825
13826 static void
13827 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13828 {
13829   unsigned long live_regs_mask = 0;
13830   unsigned long l_mask;
13831   unsigned high_regs_pushed = 0;
13832   int cfa_offset = 0;
13833   int regno;
13834
13835   if (IS_NAKED (arm_current_func_type ()))
13836     return;
13837
13838   if (is_called_in_ARM_mode (current_function_decl))
13839     {
13840       const char * name;
13841
13842       gcc_assert (GET_CODE (DECL_RTL (current_function_decl)) == MEM);
13843       gcc_assert (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0))
13844                   == SYMBOL_REF);
13845       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13846
13847       /* Generate code sequence to switch us into Thumb mode.  */
13848       /* The .code 32 directive has already been emitted by
13849          ASM_DECLARE_FUNCTION_NAME.  */
13850       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13851       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13852
13853       /* Generate a label, so that the debugger will notice the
13854          change in instruction sets.  This label is also used by
13855          the assembler to bypass the ARM code when this function
13856          is called from a Thumb encoded function elsewhere in the
13857          same file.  Hence the definition of STUB_NAME here must
13858          agree with the definition in gas/config/tc-arm.c.  */
13859
13860 #define STUB_NAME ".real_start_of"
13861
13862       fprintf (f, "\t.code\t16\n");
13863 #ifdef ARM_PE
13864       if (arm_dllexport_name_p (name))
13865         name = arm_strip_name_encoding (name);
13866 #endif
13867       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13868       fprintf (f, "\t.thumb_func\n");
13869       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13870     }
13871
13872   if (current_function_pretend_args_size)
13873     {
13874       /* Output unwind directive for the stack adjustment.  */
13875       if (ARM_EABI_UNWIND_TABLES)
13876         fprintf (f, "\t.pad #%d\n",
13877                  current_function_pretend_args_size);
13878
13879       if (cfun->machine->uses_anonymous_args)
13880         {
13881           int num_pushes;
13882
13883           fprintf (f, "\tpush\t{");
13884
13885           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13886
13887           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13888                regno <= LAST_ARG_REGNUM;
13889                regno++)
13890             asm_fprintf (f, "%r%s", regno,
13891                          regno == LAST_ARG_REGNUM ? "" : ", ");
13892
13893           fprintf (f, "}\n");
13894         }
13895       else
13896         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13897                      SP_REGNUM, SP_REGNUM,
13898                      current_function_pretend_args_size);
13899
13900       /* We don't need to record the stores for unwinding (would it
13901          help the debugger any if we did?), but record the change in
13902          the stack pointer.  */
13903       if (dwarf2out_do_frame ())
13904         {
13905           char *l = dwarf2out_cfi_label ();
13906
13907           cfa_offset = cfa_offset + current_function_pretend_args_size;
13908           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13909         }
13910     }
13911
13912   /* Get the registers we are going to push.  */
13913   live_regs_mask = thumb_compute_save_reg_mask ();
13914   /* Extract a mask of the ones we can give to the Thumb's push instruction.  */
13915   l_mask = live_regs_mask & 0x40ff;
13916   /* Then count how many other high registers will need to be pushed.  */
13917   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13918
13919   if (TARGET_BACKTRACE)
13920     {
13921       unsigned offset;
13922       unsigned work_register;
13923
13924       /* We have been asked to create a stack backtrace structure.
13925          The code looks like this:
13926
13927          0   .align 2
13928          0   func:
13929          0     sub   SP, #16         Reserve space for 4 registers.
13930          2     push  {R7}            Push low registers.
13931          4     add   R7, SP, #20     Get the stack pointer before the push.
13932          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13933          8     mov   R7, PC          Get hold of the start of this code plus 12.
13934         10     str   R7, [SP, #16]   Store it.
13935         12     mov   R7, FP          Get hold of the current frame pointer.
13936         14     str   R7, [SP, #4]    Store it.
13937         16     mov   R7, LR          Get hold of the current return address.
13938         18     str   R7, [SP, #12]   Store it.
13939         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13940         22     mov   FP, R7          Put this value into the frame pointer.  */
13941
13942       work_register = thumb_find_work_register (live_regs_mask);
13943
13944       if (ARM_EABI_UNWIND_TABLES)
13945         asm_fprintf (f, "\t.pad #16\n");
13946
13947       asm_fprintf
13948         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13949          SP_REGNUM, SP_REGNUM);
13950
13951       if (dwarf2out_do_frame ())
13952         {
13953           char *l = dwarf2out_cfi_label ();
13954
13955           cfa_offset = cfa_offset + 16;
13956           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13957         }
13958
13959       if (l_mask)
13960         {
13961           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13962           offset = bit_count (l_mask) * UNITS_PER_WORD;
13963         }
13964       else
13965         offset = 0;
13966
13967       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13968                    offset + 16 + current_function_pretend_args_size);
13969
13970       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13971                    offset + 4);
13972
13973       /* Make sure that the instruction fetching the PC is in the right place
13974          to calculate "start of backtrace creation code + 12".  */
13975       if (l_mask)
13976         {
13977           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13978           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13979                        offset + 12);
13980           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13981                        ARM_HARD_FRAME_POINTER_REGNUM);
13982           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13983                        offset);
13984         }
13985       else
13986         {
13987           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13988                        ARM_HARD_FRAME_POINTER_REGNUM);
13989           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13990                        offset);
13991           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13992           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13993                        offset + 12);
13994         }
13995
13996       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13997       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13998                    offset + 8);
13999       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
14000                    offset + 12);
14001       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
14002                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
14003     }
14004   /* Optimization:  If we are not pushing any low registers but we are going
14005      to push some high registers then delay our first push.  This will just
14006      be a push of LR and we can combine it with the push of the first high
14007      register.  */
14008   else if ((l_mask & 0xff) != 0
14009            || (high_regs_pushed == 0 && l_mask))
14010     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
14011
14012   if (high_regs_pushed)
14013     {
14014       unsigned pushable_regs;
14015       unsigned next_hi_reg;
14016
14017       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
14018         if (live_regs_mask & (1 << next_hi_reg))
14019           break;
14020
14021       pushable_regs = l_mask & 0xff;
14022
14023       if (pushable_regs == 0)
14024         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
14025
14026       while (high_regs_pushed > 0)
14027         {
14028           unsigned long real_regs_mask = 0;
14029
14030           for (regno = LAST_LO_REGNUM; regno >= 0; regno --)
14031             {
14032               if (pushable_regs & (1 << regno))
14033                 {
14034                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
14035
14036                   high_regs_pushed --;
14037                   real_regs_mask |= (1 << next_hi_reg);
14038
14039                   if (high_regs_pushed)
14040                     {
14041                       for (next_hi_reg --; next_hi_reg > LAST_LO_REGNUM;
14042                            next_hi_reg --)
14043                         if (live_regs_mask & (1 << next_hi_reg))
14044                           break;
14045                     }
14046                   else
14047                     {
14048                       pushable_regs &= ~((1 << regno) - 1);
14049                       break;
14050                     }
14051                 }
14052             }
14053
14054           /* If we had to find a work register and we have not yet
14055              saved the LR then add it to the list of regs to push.  */
14056           if (l_mask == (1 << LR_REGNUM))
14057             {
14058               thumb_pushpop (f, pushable_regs | (1 << LR_REGNUM),
14059                              1, &cfa_offset,
14060                              real_regs_mask | (1 << LR_REGNUM));
14061               l_mask = 0;
14062             }
14063           else
14064             thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
14065         }
14066     }
14067 }
14068
14069 /* Handle the case of a double word load into a low register from
14070    a computed memory address.  The computed address may involve a
14071    register which is overwritten by the load.  */
14072 const char *
14073 thumb_load_double_from_address (rtx *operands)
14074 {
14075   rtx addr;
14076   rtx base;
14077   rtx offset;
14078   rtx arg1;
14079   rtx arg2;
14080
14081   gcc_assert (GET_CODE (operands[0]) == REG);
14082   gcc_assert (GET_CODE (operands[1]) == MEM);
14083
14084   /* Get the memory address.  */
14085   addr = XEXP (operands[1], 0);
14086
14087   /* Work out how the memory address is computed.  */
14088   switch (GET_CODE (addr))
14089     {
14090     case REG:
14091       operands[2] = adjust_address (operands[1], SImode, 4);
14092
14093       if (REGNO (operands[0]) == REGNO (addr))
14094         {
14095           output_asm_insn ("ldr\t%H0, %2", operands);
14096           output_asm_insn ("ldr\t%0, %1", operands);
14097         }
14098       else
14099         {
14100           output_asm_insn ("ldr\t%0, %1", operands);
14101           output_asm_insn ("ldr\t%H0, %2", operands);
14102         }
14103       break;
14104
14105     case CONST:
14106       /* Compute <address> + 4 for the high order load.  */
14107       operands[2] = adjust_address (operands[1], SImode, 4);
14108
14109       output_asm_insn ("ldr\t%0, %1", operands);
14110       output_asm_insn ("ldr\t%H0, %2", operands);
14111       break;
14112
14113     case PLUS:
14114       arg1   = XEXP (addr, 0);
14115       arg2   = XEXP (addr, 1);
14116
14117       if (CONSTANT_P (arg1))
14118         base = arg2, offset = arg1;
14119       else
14120         base = arg1, offset = arg2;
14121
14122       gcc_assert (GET_CODE (base) == REG);
14123
14124       /* Catch the case of <address> = <reg> + <reg> */
14125       if (GET_CODE (offset) == REG)
14126         {
14127           int reg_offset = REGNO (offset);
14128           int reg_base   = REGNO (base);
14129           int reg_dest   = REGNO (operands[0]);
14130
14131           /* Add the base and offset registers together into the
14132              higher destination register.  */
14133           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
14134                        reg_dest + 1, reg_base, reg_offset);
14135
14136           /* Load the lower destination register from the address in
14137              the higher destination register.  */
14138           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
14139                        reg_dest, reg_dest + 1);
14140
14141           /* Load the higher destination register from its own address
14142              plus 4.  */
14143           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
14144                        reg_dest + 1, reg_dest + 1);
14145         }
14146       else
14147         {
14148           /* Compute <address> + 4 for the high order load.  */
14149           operands[2] = adjust_address (operands[1], SImode, 4);
14150
14151           /* If the computed address is held in the low order register
14152              then load the high order register first, otherwise always
14153              load the low order register first.  */
14154           if (REGNO (operands[0]) == REGNO (base))
14155             {
14156               output_asm_insn ("ldr\t%H0, %2", operands);
14157               output_asm_insn ("ldr\t%0, %1", operands);
14158             }
14159           else
14160             {
14161               output_asm_insn ("ldr\t%0, %1", operands);
14162               output_asm_insn ("ldr\t%H0, %2", operands);
14163             }
14164         }
14165       break;
14166
14167     case LABEL_REF:
14168       /* With no registers to worry about we can just load the value
14169          directly.  */
14170       operands[2] = adjust_address (operands[1], SImode, 4);
14171
14172       output_asm_insn ("ldr\t%H0, %2", operands);
14173       output_asm_insn ("ldr\t%0, %1", operands);
14174       break;
14175
14176     default:
14177       gcc_unreachable ();
14178     }
14179
14180   return "";
14181 }
14182
14183 const char *
14184 thumb_output_move_mem_multiple (int n, rtx *operands)
14185 {
14186   rtx tmp;
14187
14188   switch (n)
14189     {
14190     case 2:
14191       if (REGNO (operands[4]) > REGNO (operands[5]))
14192         {
14193           tmp = operands[4];
14194           operands[4] = operands[5];
14195           operands[5] = tmp;
14196         }
14197       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
14198       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
14199       break;
14200
14201     case 3:
14202       if (REGNO (operands[4]) > REGNO (operands[5]))
14203         {
14204           tmp = operands[4];
14205           operands[4] = operands[5];
14206           operands[5] = tmp;
14207         }
14208       if (REGNO (operands[5]) > REGNO (operands[6]))
14209         {
14210           tmp = operands[5];
14211           operands[5] = operands[6];
14212           operands[6] = tmp;
14213         }
14214       if (REGNO (operands[4]) > REGNO (operands[5]))
14215         {
14216           tmp = operands[4];
14217           operands[4] = operands[5];
14218           operands[5] = tmp;
14219         }
14220
14221       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
14222       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
14223       break;
14224
14225     default:
14226       gcc_unreachable ();
14227     }
14228
14229   return "";
14230 }
14231
14232 /* Output a call-via instruction for thumb state.  */
14233 const char *
14234 thumb_call_via_reg (rtx reg)
14235 {
14236   int regno = REGNO (reg);
14237   rtx *labelp;
14238
14239   gcc_assert (regno < LR_REGNUM);
14240
14241   /* If we are in the normal text section we can use a single instance
14242      per compilation unit.  If we are doing function sections, then we need
14243      an entry per section, since we can't rely on reachability.  */
14244   if (in_section == text_section)
14245     {
14246       thumb_call_reg_needed = 1;
14247
14248       if (thumb_call_via_label[regno] == NULL)
14249         thumb_call_via_label[regno] = gen_label_rtx ();
14250       labelp = thumb_call_via_label + regno;
14251     }
14252   else
14253     {
14254       if (cfun->machine->call_via[regno] == NULL)
14255         cfun->machine->call_via[regno] = gen_label_rtx ();
14256       labelp = cfun->machine->call_via + regno;
14257     }
14258
14259   output_asm_insn ("bl\t%a0", labelp);
14260   return "";
14261 }
14262
14263 /* Routines for generating rtl.  */
14264 void
14265 thumb_expand_movmemqi (rtx *operands)
14266 {
14267   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
14268   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
14269   HOST_WIDE_INT len = INTVAL (operands[2]);
14270   HOST_WIDE_INT offset = 0;
14271
14272   while (len >= 12)
14273     {
14274       emit_insn (gen_movmem12b (out, in, out, in));
14275       len -= 12;
14276     }
14277
14278   if (len >= 8)
14279     {
14280       emit_insn (gen_movmem8b (out, in, out, in));
14281       len -= 8;
14282     }
14283
14284   if (len >= 4)
14285     {
14286       rtx reg = gen_reg_rtx (SImode);
14287       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
14288       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
14289       len -= 4;
14290       offset += 4;
14291     }
14292
14293   if (len >= 2)
14294     {
14295       rtx reg = gen_reg_rtx (HImode);
14296       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
14297                                               plus_constant (in, offset))));
14298       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
14299                             reg));
14300       len -= 2;
14301       offset += 2;
14302     }
14303
14304   if (len)
14305     {
14306       rtx reg = gen_reg_rtx (QImode);
14307       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
14308                                               plus_constant (in, offset))));
14309       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
14310                             reg));
14311     }
14312 }
14313
14314 void
14315 thumb_reload_out_hi (rtx *operands)
14316 {
14317   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
14318 }
14319
14320 /* Handle reading a half-word from memory during reload.  */
14321 void
14322 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
14323 {
14324   gcc_unreachable ();
14325 }
14326
14327 /* Return the length of a function name prefix
14328     that starts with the character 'c'.  */
14329 static int
14330 arm_get_strip_length (int c)
14331 {
14332   switch (c)
14333     {
14334     ARM_NAME_ENCODING_LENGTHS
14335       default: return 0;
14336     }
14337 }
14338
14339 /* Return a pointer to a function's name with any
14340    and all prefix encodings stripped from it.  */
14341 const char *
14342 arm_strip_name_encoding (const char *name)
14343 {
14344   int skip;
14345
14346   while ((skip = arm_get_strip_length (* name)))
14347     name += skip;
14348
14349   return name;
14350 }
14351
14352 /* If there is a '*' anywhere in the name's prefix, then
14353    emit the stripped name verbatim, otherwise prepend an
14354    underscore if leading underscores are being used.  */
14355 void
14356 arm_asm_output_labelref (FILE *stream, const char *name)
14357 {
14358   int skip;
14359   int verbatim = 0;
14360
14361   while ((skip = arm_get_strip_length (* name)))
14362     {
14363       verbatim |= (*name == '*');
14364       name += skip;
14365     }
14366
14367   if (verbatim)
14368     fputs (name, stream);
14369   else
14370     asm_fprintf (stream, "%U%s", name);
14371 }
14372
14373 static void
14374 arm_file_end (void)
14375 {
14376   int regno;
14377
14378   if (! thumb_call_reg_needed)
14379     return;
14380
14381   switch_to_section (text_section);
14382   asm_fprintf (asm_out_file, "\t.code 16\n");
14383   ASM_OUTPUT_ALIGN (asm_out_file, 1);
14384
14385   for (regno = 0; regno < LR_REGNUM; regno++)
14386     {
14387       rtx label = thumb_call_via_label[regno];
14388
14389       if (label != 0)
14390         {
14391           targetm.asm_out.internal_label (asm_out_file, "L",
14392                                           CODE_LABEL_NUMBER (label));
14393           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
14394         }
14395     }
14396 }
14397
14398 rtx aof_pic_label;
14399
14400 #ifdef AOF_ASSEMBLER
14401 /* Special functions only needed when producing AOF syntax assembler.  */
14402
14403 struct pic_chain
14404 {
14405   struct pic_chain * next;
14406   const char * symname;
14407 };
14408
14409 static struct pic_chain * aof_pic_chain = NULL;
14410
14411 rtx
14412 aof_pic_entry (rtx x)
14413 {
14414   struct pic_chain ** chainp;
14415   int offset;
14416
14417   if (aof_pic_label == NULL_RTX)
14418     {
14419       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
14420     }
14421
14422   for (offset = 0, chainp = &aof_pic_chain; *chainp;
14423        offset += 4, chainp = &(*chainp)->next)
14424     if ((*chainp)->symname == XSTR (x, 0))
14425       return plus_constant (aof_pic_label, offset);
14426
14427   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
14428   (*chainp)->next = NULL;
14429   (*chainp)->symname = XSTR (x, 0);
14430   return plus_constant (aof_pic_label, offset);
14431 }
14432
14433 void
14434 aof_dump_pic_table (FILE *f)
14435 {
14436   struct pic_chain * chain;
14437
14438   if (aof_pic_chain == NULL)
14439     return;
14440
14441   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
14442                PIC_OFFSET_TABLE_REGNUM,
14443                PIC_OFFSET_TABLE_REGNUM);
14444   fputs ("|x$adcons|\n", f);
14445
14446   for (chain = aof_pic_chain; chain; chain = chain->next)
14447     {
14448       fputs ("\tDCD\t", f);
14449       assemble_name (f, chain->symname);
14450       fputs ("\n", f);
14451     }
14452 }
14453
14454 int arm_text_section_count = 1;
14455
14456 /* A get_unnamed_section callback for switching to the text section.  */
14457
14458 static void
14459 aof_output_text_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14460 {
14461   fprintf (asm_out_file, "\tAREA |C$$code%d|, CODE, READONLY",
14462            arm_text_section_count++);
14463   if (flag_pic)
14464     fprintf (asm_out_file, ", PIC, REENTRANT");
14465   fprintf (asm_out_file, "\n");
14466 }
14467
14468 static int arm_data_section_count = 1;
14469
14470 /* A get_unnamed_section callback for switching to the data section.  */
14471
14472 static void
14473 aof_output_data_section_asm_op (const void *data ATTRIBUTE_UNUSED)
14474 {
14475   fprintf (asm_out_file, "\tAREA |C$$data%d|, DATA\n",
14476            arm_data_section_count++);
14477 }
14478
14479 /* Implement TARGET_ASM_INIT_SECTIONS.
14480
14481    AOF Assembler syntax is a nightmare when it comes to areas, since once
14482    we change from one area to another, we can't go back again.  Instead,
14483    we must create a new area with the same attributes and add the new output
14484    to that.  Unfortunately, there is nothing we can do here to guarantee that
14485    two areas with the same attributes will be linked adjacently in the
14486    resulting executable, so we have to be careful not to do pc-relative
14487    addressing across such boundaries.  */
14488
14489 static void
14490 aof_asm_init_sections (void)
14491 {
14492   text_section = get_unnamed_section (SECTION_CODE,
14493                                       aof_output_text_section_asm_op, NULL);
14494   data_section = get_unnamed_section (SECTION_WRITE,
14495                                       aof_output_data_section_asm_op, NULL);
14496   readonly_data_section = text_section;
14497 }
14498
14499 void
14500 zero_init_section (void)
14501 {
14502   static int zero_init_count = 1;
14503
14504   fprintf (asm_out_file, "\tAREA |C$$zidata%d|,NOINIT\n", zero_init_count++);
14505   in_section = NULL;
14506 }
14507
14508 /* The AOF assembler is religiously strict about declarations of
14509    imported and exported symbols, so that it is impossible to declare
14510    a function as imported near the beginning of the file, and then to
14511    export it later on.  It is, however, possible to delay the decision
14512    until all the functions in the file have been compiled.  To get
14513    around this, we maintain a list of the imports and exports, and
14514    delete from it any that are subsequently defined.  At the end of
14515    compilation we spit the remainder of the list out before the END
14516    directive.  */
14517
14518 struct import
14519 {
14520   struct import * next;
14521   const char * name;
14522 };
14523
14524 static struct import * imports_list = NULL;
14525
14526 void
14527 aof_add_import (const char *name)
14528 {
14529   struct import * new;
14530
14531   for (new = imports_list; new; new = new->next)
14532     if (new->name == name)
14533       return;
14534
14535   new = (struct import *) xmalloc (sizeof (struct import));
14536   new->next = imports_list;
14537   imports_list = new;
14538   new->name = name;
14539 }
14540
14541 void
14542 aof_delete_import (const char *name)
14543 {
14544   struct import ** old;
14545
14546   for (old = &imports_list; *old; old = & (*old)->next)
14547     {
14548       if ((*old)->name == name)
14549         {
14550           *old = (*old)->next;
14551           return;
14552         }
14553     }
14554 }
14555
14556 int arm_main_function = 0;
14557
14558 static void
14559 aof_dump_imports (FILE *f)
14560 {
14561   /* The AOF assembler needs this to cause the startup code to be extracted
14562      from the library.  Brining in __main causes the whole thing to work
14563      automagically.  */
14564   if (arm_main_function)
14565     {
14566       switch_to_section (text_section);
14567       fputs ("\tIMPORT __main\n", f);
14568       fputs ("\tDCD __main\n", f);
14569     }
14570
14571   /* Now dump the remaining imports.  */
14572   while (imports_list)
14573     {
14574       fprintf (f, "\tIMPORT\t");
14575       assemble_name (f, imports_list->name);
14576       fputc ('\n', f);
14577       imports_list = imports_list->next;
14578     }
14579 }
14580
14581 static void
14582 aof_globalize_label (FILE *stream, const char *name)
14583 {
14584   default_globalize_label (stream, name);
14585   if (! strcmp (name, "main"))
14586     arm_main_function = 1;
14587 }
14588
14589 static void
14590 aof_file_start (void)
14591 {
14592   fputs ("__r0\tRN\t0\n", asm_out_file);
14593   fputs ("__a1\tRN\t0\n", asm_out_file);
14594   fputs ("__a2\tRN\t1\n", asm_out_file);
14595   fputs ("__a3\tRN\t2\n", asm_out_file);
14596   fputs ("__a4\tRN\t3\n", asm_out_file);
14597   fputs ("__v1\tRN\t4\n", asm_out_file);
14598   fputs ("__v2\tRN\t5\n", asm_out_file);
14599   fputs ("__v3\tRN\t6\n", asm_out_file);
14600   fputs ("__v4\tRN\t7\n", asm_out_file);
14601   fputs ("__v5\tRN\t8\n", asm_out_file);
14602   fputs ("__v6\tRN\t9\n", asm_out_file);
14603   fputs ("__sl\tRN\t10\n", asm_out_file);
14604   fputs ("__fp\tRN\t11\n", asm_out_file);
14605   fputs ("__ip\tRN\t12\n", asm_out_file);
14606   fputs ("__sp\tRN\t13\n", asm_out_file);
14607   fputs ("__lr\tRN\t14\n", asm_out_file);
14608   fputs ("__pc\tRN\t15\n", asm_out_file);
14609   fputs ("__f0\tFN\t0\n", asm_out_file);
14610   fputs ("__f1\tFN\t1\n", asm_out_file);
14611   fputs ("__f2\tFN\t2\n", asm_out_file);
14612   fputs ("__f3\tFN\t3\n", asm_out_file);
14613   fputs ("__f4\tFN\t4\n", asm_out_file);
14614   fputs ("__f5\tFN\t5\n", asm_out_file);
14615   fputs ("__f6\tFN\t6\n", asm_out_file);
14616   fputs ("__f7\tFN\t7\n", asm_out_file);
14617   switch_to_section (text_section);
14618 }
14619
14620 static void
14621 aof_file_end (void)
14622 {
14623   if (flag_pic)
14624     aof_dump_pic_table (asm_out_file);
14625   arm_file_end ();
14626   aof_dump_imports (asm_out_file);
14627   fputs ("\tEND\n", asm_out_file);
14628 }
14629 #endif /* AOF_ASSEMBLER */
14630
14631 #ifndef ARM_PE
14632 /* Symbols in the text segment can be accessed without indirecting via the
14633    constant pool; it may take an extra binary operation, but this is still
14634    faster than indirecting via memory.  Don't do this when not optimizing,
14635    since we won't be calculating al of the offsets necessary to do this
14636    simplification.  */
14637
14638 static void
14639 arm_encode_section_info (tree decl, rtx rtl, int first)
14640 {
14641   /* This doesn't work with AOF syntax, since the string table may be in
14642      a different AREA.  */
14643 #ifndef AOF_ASSEMBLER
14644   if (optimize > 0 && TREE_CONSTANT (decl))
14645     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14646 #endif
14647
14648   /* If we are referencing a function that is weak then encode a long call
14649      flag in the function name, otherwise if the function is static or
14650      or known to be defined in this file then encode a short call flag.  */
14651   if (first && DECL_P (decl))
14652     {
14653       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14654         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14655       else if (! TREE_PUBLIC (decl))
14656         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14657     }
14658
14659   default_encode_section_info (decl, rtl, first);
14660 }
14661 #endif /* !ARM_PE */
14662
14663 static void
14664 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14665 {
14666   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14667       && !strcmp (prefix, "L"))
14668     {
14669       arm_ccfsm_state = 0;
14670       arm_target_insn = NULL;
14671     }
14672   default_internal_label (stream, prefix, labelno);
14673 }
14674
14675 /* Output code to add DELTA to the first argument, and then jump
14676    to FUNCTION.  Used for C++ multiple inheritance.  */
14677 static void
14678 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14679                      HOST_WIDE_INT delta,
14680                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14681                      tree function)
14682 {
14683   static int thunk_label = 0;
14684   char label[256];
14685   int mi_delta = delta;
14686   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14687   int shift = 0;
14688   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14689                     ? 1 : 0);
14690   if (mi_delta < 0)
14691     mi_delta = - mi_delta;
14692   if (TARGET_THUMB)
14693     {
14694       int labelno = thunk_label++;
14695       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14696       fputs ("\tldr\tr12, ", file);
14697       assemble_name (file, label);
14698       fputc ('\n', file);
14699     }
14700   while (mi_delta != 0)
14701     {
14702       if ((mi_delta & (3 << shift)) == 0)
14703         shift += 2;
14704       else
14705         {
14706           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14707                        mi_op, this_regno, this_regno,
14708                        mi_delta & (0xff << shift));
14709           mi_delta &= ~(0xff << shift);
14710           shift += 8;
14711         }
14712     }
14713   if (TARGET_THUMB)
14714     {
14715       fprintf (file, "\tbx\tr12\n");
14716       ASM_OUTPUT_ALIGN (file, 2);
14717       assemble_name (file, label);
14718       fputs (":\n", file);
14719       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14720     }
14721   else
14722     {
14723       fputs ("\tb\t", file);
14724       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14725       if (NEED_PLT_RELOC)
14726         fputs ("(PLT)", file);
14727       fputc ('\n', file);
14728     }
14729 }
14730
14731 int
14732 arm_emit_vector_const (FILE *file, rtx x)
14733 {
14734   int i;
14735   const char * pattern;
14736
14737   gcc_assert (GET_CODE (x) == CONST_VECTOR);
14738
14739   switch (GET_MODE (x))
14740     {
14741     case V2SImode: pattern = "%08x"; break;
14742     case V4HImode: pattern = "%04x"; break;
14743     case V8QImode: pattern = "%02x"; break;
14744     default:       gcc_unreachable ();
14745     }
14746
14747   fprintf (file, "0x");
14748   for (i = CONST_VECTOR_NUNITS (x); i--;)
14749     {
14750       rtx element;
14751
14752       element = CONST_VECTOR_ELT (x, i);
14753       fprintf (file, pattern, INTVAL (element));
14754     }
14755
14756   return 1;
14757 }
14758
14759 const char *
14760 arm_output_load_gr (rtx *operands)
14761 {
14762   rtx reg;
14763   rtx offset;
14764   rtx wcgr;
14765   rtx sum;
14766
14767   if (GET_CODE (operands [1]) != MEM
14768       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14769       || GET_CODE (reg = XEXP (sum, 0)) != REG
14770       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14771       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14772     return "wldrw%?\t%0, %1";
14773
14774   /* Fix up an out-of-range load of a GR register.  */
14775   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14776   wcgr = operands[0];
14777   operands[0] = reg;
14778   output_asm_insn ("ldr%?\t%0, %1", operands);
14779
14780   operands[0] = wcgr;
14781   operands[1] = reg;
14782   output_asm_insn ("tmcr%?\t%0, %1", operands);
14783   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14784
14785   return "";
14786 }
14787
14788 static rtx
14789 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14790                       int incoming ATTRIBUTE_UNUSED)
14791 {
14792 #if 0
14793   /* FIXME: The ARM backend has special code to handle structure
14794          returns, and will reserve its own hidden first argument.  So
14795          if this macro is enabled a *second* hidden argument will be
14796          reserved, which will break binary compatibility with old
14797          toolchains and also thunk handling.  One day this should be
14798          fixed.  */
14799   return 0;
14800 #else
14801   /* Register in which address to store a structure value
14802      is passed to a function.  */
14803   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14804 #endif
14805 }
14806
14807 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14808
14809    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14810    named arg and all anonymous args onto the stack.
14811    XXX I know the prologue shouldn't be pushing registers, but it is faster
14812    that way.  */
14813
14814 static void
14815 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14816                             enum machine_mode mode ATTRIBUTE_UNUSED,
14817                             tree type ATTRIBUTE_UNUSED,
14818                             int *pretend_size,
14819                             int second_time ATTRIBUTE_UNUSED)
14820 {
14821   cfun->machine->uses_anonymous_args = 1;
14822   if (cum->nregs < NUM_ARG_REGS)
14823     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14824 }
14825
14826 /* Return nonzero if the CONSUMER instruction (a store) does not need
14827    PRODUCER's value to calculate the address.  */
14828
14829 int
14830 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14831 {
14832   rtx value = PATTERN (producer);
14833   rtx addr = PATTERN (consumer);
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 (addr) == COND_EXEC)
14841     addr = COND_EXEC_CODE (addr);
14842   if (GET_CODE (addr) == PARALLEL)
14843     addr = XVECEXP (addr, 0, 0);
14844   addr = XEXP (addr, 0);
14845
14846   return !reg_overlap_mentioned_p (value, addr);
14847 }
14848
14849 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14850    have an early register shift value or amount dependency on the
14851    result of PRODUCER.  */
14852
14853 int
14854 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14855 {
14856   rtx value = PATTERN (producer);
14857   rtx op = PATTERN (consumer);
14858   rtx early_op;
14859
14860   if (GET_CODE (value) == COND_EXEC)
14861     value = COND_EXEC_CODE (value);
14862   if (GET_CODE (value) == PARALLEL)
14863     value = XVECEXP (value, 0, 0);
14864   value = XEXP (value, 0);
14865   if (GET_CODE (op) == COND_EXEC)
14866     op = COND_EXEC_CODE (op);
14867   if (GET_CODE (op) == PARALLEL)
14868     op = XVECEXP (op, 0, 0);
14869   op = XEXP (op, 1);
14870
14871   early_op = XEXP (op, 0);
14872   /* This is either an actual independent shift, or a shift applied to
14873      the first operand of another operation.  We want the whole shift
14874      operation.  */
14875   if (GET_CODE (early_op) == REG)
14876     early_op = op;
14877
14878   return !reg_overlap_mentioned_p (value, early_op);
14879 }
14880
14881 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14882    have an early register shift value dependency on the result of
14883    PRODUCER.  */
14884
14885 int
14886 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14887 {
14888   rtx value = PATTERN (producer);
14889   rtx op = PATTERN (consumer);
14890   rtx early_op;
14891
14892   if (GET_CODE (value) == COND_EXEC)
14893     value = COND_EXEC_CODE (value);
14894   if (GET_CODE (value) == PARALLEL)
14895     value = XVECEXP (value, 0, 0);
14896   value = XEXP (value, 0);
14897   if (GET_CODE (op) == COND_EXEC)
14898     op = COND_EXEC_CODE (op);
14899   if (GET_CODE (op) == PARALLEL)
14900     op = XVECEXP (op, 0, 0);
14901   op = XEXP (op, 1);
14902
14903   early_op = XEXP (op, 0);
14904
14905   /* This is either an actual independent shift, or a shift applied to
14906      the first operand of another operation.  We want the value being
14907      shifted, in either case.  */
14908   if (GET_CODE (early_op) != REG)
14909     early_op = XEXP (early_op, 0);
14910
14911   return !reg_overlap_mentioned_p (value, early_op);
14912 }
14913
14914 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14915    have an early register mult dependency on the result of
14916    PRODUCER.  */
14917
14918 int
14919 arm_no_early_mul_dep (rtx producer, rtx consumer)
14920 {
14921   rtx value = PATTERN (producer);
14922   rtx op = PATTERN (consumer);
14923
14924   if (GET_CODE (value) == COND_EXEC)
14925     value = COND_EXEC_CODE (value);
14926   if (GET_CODE (value) == PARALLEL)
14927     value = XVECEXP (value, 0, 0);
14928   value = XEXP (value, 0);
14929   if (GET_CODE (op) == COND_EXEC)
14930     op = COND_EXEC_CODE (op);
14931   if (GET_CODE (op) == PARALLEL)
14932     op = XVECEXP (op, 0, 0);
14933   op = XEXP (op, 1);
14934
14935   return (GET_CODE (op) == PLUS
14936           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14937 }
14938
14939
14940 /* We can't rely on the caller doing the proper promotion when
14941    using APCS or ATPCS.  */
14942
14943 static bool
14944 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14945 {
14946     return !TARGET_AAPCS_BASED;
14947 }
14948
14949
14950 /* AAPCS based ABIs use short enums by default.  */
14951
14952 static bool
14953 arm_default_short_enums (void)
14954 {
14955   return TARGET_AAPCS_BASED && arm_abi != ARM_ABI_AAPCS_LINUX;
14956 }
14957
14958
14959 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14960
14961 static bool
14962 arm_align_anon_bitfield (void)
14963 {
14964   return TARGET_AAPCS_BASED;
14965 }
14966
14967
14968 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14969
14970 static tree
14971 arm_cxx_guard_type (void)
14972 {
14973   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14974 }
14975
14976
14977 /* The EABI says test the least significant bit of a guard variable.  */
14978
14979 static bool
14980 arm_cxx_guard_mask_bit (void)
14981 {
14982   return TARGET_AAPCS_BASED;
14983 }
14984
14985
14986 /* The EABI specifies that all array cookies are 8 bytes long.  */
14987
14988 static tree
14989 arm_get_cookie_size (tree type)
14990 {
14991   tree size;
14992
14993   if (!TARGET_AAPCS_BASED)
14994     return default_cxx_get_cookie_size (type);
14995
14996   size = build_int_cst (sizetype, 8);
14997   return size;
14998 }
14999
15000
15001 /* The EABI says that array cookies should also contain the element size.  */
15002
15003 static bool
15004 arm_cookie_has_size (void)
15005 {
15006   return TARGET_AAPCS_BASED;
15007 }
15008
15009
15010 /* The EABI says constructors and destructors should return a pointer to
15011    the object constructed/destroyed.  */
15012
15013 static bool
15014 arm_cxx_cdtor_returns_this (void)
15015 {
15016   return TARGET_AAPCS_BASED;
15017 }
15018
15019 /* The EABI says that an inline function may never be the key
15020    method.  */
15021
15022 static bool
15023 arm_cxx_key_method_may_be_inline (void)
15024 {
15025   return !TARGET_AAPCS_BASED;
15026 }
15027
15028 static void
15029 arm_cxx_determine_class_data_visibility (tree decl)
15030 {
15031   if (!TARGET_AAPCS_BASED)
15032     return;
15033
15034   /* In general, \S 3.2.5.5 of the ARM EABI requires that class data
15035      is exported.  However, on systems without dynamic vague linkage,
15036      \S 3.2.5.6 says that COMDAT class data has hidden linkage.  */
15037   if (!TARGET_ARM_DYNAMIC_VAGUE_LINKAGE_P && DECL_COMDAT (decl))
15038     DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
15039   else
15040     DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
15041   DECL_VISIBILITY_SPECIFIED (decl) = 1;
15042 }
15043
15044 static bool
15045 arm_cxx_class_data_always_comdat (void)
15046 {
15047   /* \S 3.2.5.4 of the ARM C++ ABI says that class data only have
15048      vague linkage if the class has no key function.  */
15049   return !TARGET_AAPCS_BASED;
15050 }
15051
15052
15053 /* The EABI says __aeabi_atexit should be used to register static
15054    destructors.  */
15055
15056 static bool
15057 arm_cxx_use_aeabi_atexit (void)
15058 {
15059   return TARGET_AAPCS_BASED;
15060 }
15061
15062
15063 void
15064 arm_set_return_address (rtx source, rtx scratch)
15065 {
15066   arm_stack_offsets *offsets;
15067   HOST_WIDE_INT delta;
15068   rtx addr;
15069   unsigned long saved_regs;
15070
15071   saved_regs = arm_compute_save_reg_mask ();
15072
15073   if ((saved_regs & (1 << LR_REGNUM)) == 0)
15074     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15075   else
15076     {
15077       if (frame_pointer_needed)
15078         addr = plus_constant(hard_frame_pointer_rtx, -4);
15079       else
15080         {
15081           /* LR will be the first saved register.  */
15082           offsets = arm_get_frame_offsets ();
15083           delta = offsets->outgoing_args - (offsets->frame + 4);
15084
15085
15086           if (delta >= 4096)
15087             {
15088               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
15089                                      GEN_INT (delta & ~4095)));
15090               addr = scratch;
15091               delta &= 4095;
15092             }
15093           else
15094             addr = stack_pointer_rtx;
15095
15096           addr = plus_constant (addr, delta);
15097         }
15098       emit_move_insn (gen_frame_mem (Pmode, addr), source);
15099     }
15100 }
15101
15102
15103 void
15104 thumb_set_return_address (rtx source, rtx scratch)
15105 {
15106   arm_stack_offsets *offsets;
15107   HOST_WIDE_INT delta;
15108   int reg;
15109   rtx addr;
15110   unsigned long mask;
15111
15112   emit_insn (gen_rtx_USE (VOIDmode, source));
15113
15114   mask = thumb_compute_save_reg_mask ();
15115   if (mask & (1 << LR_REGNUM))
15116     {
15117       offsets = arm_get_frame_offsets ();
15118
15119       /* Find the saved regs.  */
15120       if (frame_pointer_needed)
15121         {
15122           delta = offsets->soft_frame - offsets->saved_args;
15123           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
15124         }
15125       else
15126         {
15127           delta = offsets->outgoing_args - offsets->saved_args;
15128           reg = SP_REGNUM;
15129         }
15130       /* Allow for the stack frame.  */
15131       if (TARGET_BACKTRACE)
15132         delta -= 16;
15133       /* The link register is always the first saved register.  */
15134       delta -= 4;
15135
15136       /* Construct the address.  */
15137       addr = gen_rtx_REG (SImode, reg);
15138       if ((reg != SP_REGNUM && delta >= 128)
15139           || delta >= 1024)
15140         {
15141           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
15142           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
15143           addr = scratch;
15144         }
15145       else
15146         addr = plus_constant (addr, delta);
15147
15148       emit_move_insn (gen_frame_mem (Pmode, addr), source);
15149     }
15150   else
15151     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
15152 }
15153
15154 /* Implements target hook vector_mode_supported_p.  */
15155 bool
15156 arm_vector_mode_supported_p (enum machine_mode mode)
15157 {
15158   if ((mode == V2SImode)
15159       || (mode == V4HImode)
15160       || (mode == V8QImode))
15161     return true;
15162
15163   return false;
15164 }
15165
15166 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
15167    ARM insns and therefore guarantee that the shift count is modulo 256.
15168    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
15169    guarantee no particular behavior for out-of-range counts.  */
15170
15171 static unsigned HOST_WIDE_INT
15172 arm_shift_truncation_mask (enum machine_mode mode)
15173 {
15174   return mode == SImode ? 255 : 0;
15175 }
15176
15177
15178 /* Map internal gcc register numbers to DWARF2 register numbers.  */
15179
15180 unsigned int
15181 arm_dbx_register_number (unsigned int regno)
15182 {
15183   if (regno < 16)
15184     return regno;
15185
15186   /* TODO: Legacy targets output FPA regs as registers 16-23 for backwards
15187      compatibility.  The EABI defines them as registers 96-103.  */
15188   if (IS_FPA_REGNUM (regno))
15189     return (TARGET_AAPCS_BASED ? 96 : 16) + regno - FIRST_FPA_REGNUM;
15190
15191   if (IS_VFP_REGNUM (regno))
15192     return 64 + regno - FIRST_VFP_REGNUM;
15193
15194   if (IS_IWMMXT_GR_REGNUM (regno))
15195     return 104 + regno - FIRST_IWMMXT_GR_REGNUM;
15196
15197   if (IS_IWMMXT_REGNUM (regno))
15198     return 112 + regno - FIRST_IWMMXT_REGNUM;
15199
15200   gcc_unreachable ();
15201 }
15202
15203
15204 #ifdef TARGET_UNWIND_INFO
15205 /* Emit unwind directives for a store-multiple instruction.  This should
15206    only ever be generated by the function prologue code, so we expect it
15207    to have a particular form.  */
15208
15209 static void
15210 arm_unwind_emit_stm (FILE * asm_out_file, rtx p)
15211 {
15212   int i;
15213   HOST_WIDE_INT offset;
15214   HOST_WIDE_INT nregs;
15215   int reg_size;
15216   unsigned reg;
15217   unsigned lastreg;
15218   rtx e;
15219
15220   /* First insn will adjust the stack pointer.  */
15221   e = XVECEXP (p, 0, 0);
15222   if (GET_CODE (e) != SET
15223       || GET_CODE (XEXP (e, 0)) != REG
15224       || REGNO (XEXP (e, 0)) != SP_REGNUM
15225       || GET_CODE (XEXP (e, 1)) != PLUS)
15226     abort ();
15227
15228   offset = -INTVAL (XEXP (XEXP (e, 1), 1));
15229   nregs = XVECLEN (p, 0) - 1;
15230
15231   reg = REGNO (XEXP (XVECEXP (p, 0, 1), 1));
15232   if (reg < 16)
15233     {
15234       /* The function prologue may also push pc, but not annotate it as it is
15235          never restored.  We turn this into a stack pointer adjustment.  */
15236       if (nregs * 4 == offset - 4)
15237         {
15238           fprintf (asm_out_file, "\t.pad #4\n");
15239           offset -= 4;
15240         }
15241       reg_size = 4;
15242     }
15243   else if (IS_VFP_REGNUM (reg))
15244     {
15245       /* FPA register saves use an additional word.  */
15246       offset -= 4;
15247       reg_size = 8;
15248     }
15249   else if (reg >= FIRST_FPA_REGNUM && reg <= LAST_FPA_REGNUM)
15250     {
15251       /* FPA registers are done differently.  */
15252       asm_fprintf (asm_out_file, "\t.save %r, %wd\n", reg, nregs);
15253       return;
15254     }
15255   else
15256     /* Unknown register type.  */
15257     abort ();
15258
15259   /* If the stack increment doesn't match the size of the saved registers,
15260      something has gone horribly wrong.  */
15261   if (offset != nregs * reg_size)
15262     abort ();
15263
15264   fprintf (asm_out_file, "\t.save {");
15265
15266   offset = 0;
15267   lastreg = 0;
15268   /* The remaining insns will describe the stores.  */
15269   for (i = 1; i <= nregs; i++)
15270     {
15271       /* Expect (set (mem <addr>) (reg)).
15272          Where <addr> is (reg:SP) or (plus (reg:SP) (const_int)).  */
15273       e = XVECEXP (p, 0, i);
15274       if (GET_CODE (e) != SET
15275           || GET_CODE (XEXP (e, 0)) != MEM
15276           || GET_CODE (XEXP (e, 1)) != REG)
15277         abort ();
15278
15279       reg = REGNO (XEXP (e, 1));
15280       if (reg < lastreg)
15281         abort ();
15282
15283       if (i != 1)
15284         fprintf (asm_out_file, ", ");
15285       /* We can't use %r for vfp because we need to use the
15286          double precision register names.  */
15287       if (IS_VFP_REGNUM (reg))
15288         asm_fprintf (asm_out_file, "d%d", (reg - FIRST_VFP_REGNUM) / 2);
15289       else
15290         asm_fprintf (asm_out_file, "%r", reg);
15291
15292 #ifdef ENABLE_CHECKING
15293       /* Check that the addresses are consecutive.  */
15294       e = XEXP (XEXP (e, 0), 0);
15295       if (GET_CODE (e) == PLUS)
15296         {
15297           offset += reg_size;
15298           if (GET_CODE (XEXP (e, 0)) != REG
15299               || REGNO (XEXP (e, 0)) != SP_REGNUM
15300               || GET_CODE (XEXP (e, 1)) != CONST_INT
15301               || offset != INTVAL (XEXP (e, 1)))
15302             abort ();
15303         }
15304       else if (i != 1
15305                || GET_CODE (e) != REG
15306                || REGNO (e) != SP_REGNUM)
15307         abort ();
15308 #endif
15309     }
15310   fprintf (asm_out_file, "}\n");
15311 }
15312
15313 /*  Emit unwind directives for a SET.  */
15314
15315 static void
15316 arm_unwind_emit_set (FILE * asm_out_file, rtx p)
15317 {
15318   rtx e0;
15319   rtx e1;
15320
15321   e0 = XEXP (p, 0);
15322   e1 = XEXP (p, 1);
15323   switch (GET_CODE (e0))
15324     {
15325     case MEM:
15326       /* Pushing a single register.  */
15327       if (GET_CODE (XEXP (e0, 0)) != PRE_DEC
15328           || GET_CODE (XEXP (XEXP (e0, 0), 0)) != REG
15329           || REGNO (XEXP (XEXP (e0, 0), 0)) != SP_REGNUM)
15330         abort ();
15331
15332       asm_fprintf (asm_out_file, "\t.save ");
15333       if (IS_VFP_REGNUM (REGNO (e1)))
15334         asm_fprintf(asm_out_file, "{d%d}\n",
15335                     (REGNO (e1) - FIRST_VFP_REGNUM) / 2);
15336       else
15337         asm_fprintf(asm_out_file, "{%r}\n", REGNO (e1));
15338       break;
15339
15340     case REG:
15341       if (REGNO (e0) == SP_REGNUM)
15342         {
15343           /* A stack increment.  */
15344           if (GET_CODE (e1) != PLUS
15345               || GET_CODE (XEXP (e1, 0)) != REG
15346               || REGNO (XEXP (e1, 0)) != SP_REGNUM
15347               || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15348             abort ();
15349
15350           asm_fprintf (asm_out_file, "\t.pad #%wd\n",
15351                        -INTVAL (XEXP (e1, 1)));
15352         }
15353       else if (REGNO (e0) == HARD_FRAME_POINTER_REGNUM)
15354         {
15355           HOST_WIDE_INT offset;
15356           unsigned reg;
15357
15358           if (GET_CODE (e1) == PLUS)
15359             {
15360               if (GET_CODE (XEXP (e1, 0)) != REG
15361                   || GET_CODE (XEXP (e1, 1)) != CONST_INT)
15362                 abort ();
15363               reg = REGNO (XEXP (e1, 0));
15364               offset = INTVAL (XEXP (e1, 1));
15365               asm_fprintf (asm_out_file, "\t.setfp %r, %r, #%wd\n",
15366                            HARD_FRAME_POINTER_REGNUM, reg,
15367                            INTVAL (XEXP (e1, 1)));
15368             }
15369           else if (GET_CODE (e1) == REG)
15370             {
15371               reg = REGNO (e1);
15372               asm_fprintf (asm_out_file, "\t.setfp %r, %r\n",
15373                            HARD_FRAME_POINTER_REGNUM, reg);
15374             }
15375           else
15376             abort ();
15377         }
15378       else if (GET_CODE (e1) == REG && REGNO (e1) == SP_REGNUM)
15379         {
15380           /* Move from sp to reg.  */
15381           asm_fprintf (asm_out_file, "\t.movsp %r\n", REGNO (e0));
15382         }
15383       else
15384         abort ();
15385       break;
15386
15387     default:
15388       abort ();
15389     }
15390 }
15391
15392
15393 /* Emit unwind directives for the given insn.  */
15394
15395 static void
15396 arm_unwind_emit (FILE * asm_out_file, rtx insn)
15397 {
15398   rtx pat;
15399
15400   if (!ARM_EABI_UNWIND_TABLES)
15401     return;
15402
15403   if (GET_CODE (insn) == NOTE || !RTX_FRAME_RELATED_P (insn))
15404     return;
15405
15406   pat = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
15407   if (pat)
15408     pat = XEXP (pat, 0);
15409   else
15410     pat = PATTERN (insn);
15411
15412   switch (GET_CODE (pat))
15413     {
15414     case SET:
15415       arm_unwind_emit_set (asm_out_file, pat);
15416       break;
15417
15418     case SEQUENCE:
15419       /* Store multiple.  */
15420       arm_unwind_emit_stm (asm_out_file, pat);
15421       break;
15422
15423     default:
15424       abort();
15425     }
15426 }
15427
15428
15429 /* Output a reference from a function exception table to the type_info
15430    object X.  The EABI specifies that the symbol should be relocated by
15431    an R_ARM_TARGET2 relocation.  */
15432
15433 static bool
15434 arm_output_ttype (rtx x)
15435 {
15436   fputs ("\t.word\t", asm_out_file);
15437   output_addr_const (asm_out_file, x);
15438   /* Use special relocations for symbol references.  */
15439   if (GET_CODE (x) != CONST_INT)
15440     fputs ("(TARGET2)", asm_out_file);
15441   fputc ('\n', asm_out_file);
15442
15443   return TRUE;
15444 }
15445 #endif /* TARGET_UNWIND_INFO */
15446
15447
15448 /* Output unwind directives for the start/end of a function.  */
15449
15450 void
15451 arm_output_fn_unwind (FILE * f, bool prologue)
15452 {
15453   if (!ARM_EABI_UNWIND_TABLES)
15454     return;
15455
15456   if (prologue)
15457     fputs ("\t.fnstart\n", f);
15458   else
15459     fputs ("\t.fnend\n", f);
15460 }
15461
15462 static bool
15463 arm_emit_tls_decoration (FILE *fp, rtx x)
15464 {
15465   enum tls_reloc reloc;
15466   rtx val;
15467
15468   val = XVECEXP (x, 0, 0);
15469   reloc = INTVAL (XVECEXP (x, 0, 1));
15470
15471   output_addr_const (fp, val);
15472
15473   switch (reloc)
15474     {
15475     case TLS_GD32:
15476       fputs ("(tlsgd)", fp);
15477       break;
15478     case TLS_LDM32:
15479       fputs ("(tlsldm)", fp);
15480       break;
15481     case TLS_LDO32:
15482       fputs ("(tlsldo)", fp);
15483       break;
15484     case TLS_IE32:
15485       fputs ("(gottpoff)", fp);
15486       break;
15487     case TLS_LE32:
15488       fputs ("(tpoff)", fp);
15489       break;
15490     default:
15491       gcc_unreachable ();
15492     }
15493
15494   switch (reloc)
15495     {
15496     case TLS_GD32:
15497     case TLS_LDM32:
15498     case TLS_IE32:
15499       fputs (" + (. - ", fp);
15500       output_addr_const (fp, XVECEXP (x, 0, 2));
15501       fputs (" - ", fp);
15502       output_addr_const (fp, XVECEXP (x, 0, 3));
15503       fputc (')', fp);
15504       break;
15505     default:
15506       break;
15507     }
15508
15509   return TRUE;
15510 }
15511
15512 bool
15513 arm_output_addr_const_extra (FILE *fp, rtx x)
15514 {
15515   if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_TLS)
15516     return arm_emit_tls_decoration (fp, x);
15517   else if (GET_CODE (x) == UNSPEC && XINT (x, 1) == UNSPEC_PIC_LABEL)
15518     {
15519       char label[256];
15520       int labelno = INTVAL (XVECEXP (x, 0, 0));
15521
15522       ASM_GENERATE_INTERNAL_LABEL (label, "LPIC", labelno);
15523       assemble_name_raw (fp, label);
15524
15525       return TRUE;
15526     }
15527   else if (GET_CODE (x) == CONST_VECTOR)
15528     return arm_emit_vector_const (fp, x);
15529
15530   return FALSE;
15531 }
15532
15533 #include "gt-arm.h"