OSDN Git Service

PR target/16201
[pf3gnuchains/gcc-fork.git] / gcc / config / arm / arm.c
1 /* Output routines for GCC for ARM.
2    Copyright (C) 1991, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005  Free Software Foundation, Inc.
4    Contributed by Pieter `Tiggr' Schoenmakers (rcpieter@win.tue.nl)
5    and Martin Simmons (@harleqn.co.uk).
6    More major hacks by Richard Earnshaw (rearnsha@arm.com).
7
8    This file is part of GCC.
9
10    GCC is free software; you can redistribute it and/or modify it
11    under the terms of the GNU General Public License as published
12    by the Free Software Foundation; either version 2, or (at your
13    option) any later version.
14
15    GCC is distributed in the hope that it will be useful, but WITHOUT
16    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18    License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with GCC; see the file COPYING.  If not, write to
22    the Free Software Foundation, 59 Temple Place - Suite 330,
23    Boston, MA 02111-1307, 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 unsigned long thumb_compute_save_reg_mask (void);
75 static int const_ok_for_op (HOST_WIDE_INT, enum rtx_code);
76 static rtx emit_multi_reg_push (int);
77 static rtx emit_sfm (int, int);
78 #ifndef AOF_ASSEMBLER
79 static bool arm_assemble_integer (rtx, unsigned int, int);
80 #endif
81 static const char *fp_const_from_val (REAL_VALUE_TYPE *);
82 static arm_cc get_arm_condition_code (rtx);
83 static HOST_WIDE_INT int_log2 (HOST_WIDE_INT);
84 static rtx is_jump_table (rtx);
85 static const char *output_multi_immediate (rtx *, const char *, const char *,
86                                            int, HOST_WIDE_INT);
87 static void print_multi_reg (FILE *, const char *, int, int);
88 static const char *shift_op (rtx, HOST_WIDE_INT *);
89 static struct machine_function *arm_init_machine_status (void);
90 static int number_of_first_bit_set (int);
91 static void replace_symbols_in_block (tree, rtx, rtx);
92 static void thumb_exit (FILE *, int);
93 static void thumb_pushpop (FILE *, int, int, int *, int);
94 static rtx is_jump_table (rtx);
95 static HOST_WIDE_INT get_jump_table_size (rtx);
96 static Mnode *move_minipool_fix_forward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
97 static Mnode *add_minipool_forward_ref (Mfix *);
98 static Mnode *move_minipool_fix_backward_ref (Mnode *, Mnode *, HOST_WIDE_INT);
99 static Mnode *add_minipool_backward_ref (Mfix *);
100 static void assign_minipool_offsets (Mfix *);
101 static void arm_print_value (FILE *, rtx);
102 static void dump_minipool (rtx);
103 static int arm_barrier_cost (rtx);
104 static Mfix *create_fix_barrier (Mfix *, HOST_WIDE_INT);
105 static void push_minipool_barrier (rtx, HOST_WIDE_INT);
106 static void push_minipool_fix (rtx, HOST_WIDE_INT, rtx *, enum machine_mode,
107                                rtx);
108 static void arm_reorg (void);
109 static bool note_invalid_constants (rtx, HOST_WIDE_INT, int);
110 static int current_file_function_operand (rtx);
111 static unsigned long arm_compute_save_reg0_reg12_mask (void);
112 static unsigned long arm_compute_save_reg_mask (void);
113 static unsigned long arm_isr_value (tree);
114 static unsigned long arm_compute_func_type (void);
115 static tree arm_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
116 static tree arm_handle_isr_attribute (tree *, tree, tree, int, bool *);
117 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
118 static tree arm_handle_notshared_attribute (tree *, tree, tree, int, bool *);
119 #endif
120 static void arm_output_function_epilogue (FILE *, HOST_WIDE_INT);
121 static void arm_output_function_prologue (FILE *, HOST_WIDE_INT);
122 static void thumb_output_function_prologue (FILE *, HOST_WIDE_INT);
123 static int arm_comp_type_attributes (tree, tree);
124 static void arm_set_default_type_attributes (tree);
125 static int arm_adjust_cost (rtx, rtx, rtx, int);
126 static int count_insns_for_constant (HOST_WIDE_INT, int);
127 static int arm_get_strip_length (int);
128 static bool arm_function_ok_for_sibcall (tree, tree);
129 static void arm_internal_label (FILE *, const char *, unsigned long);
130 static void arm_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
131                                  tree);
132 static int arm_rtx_costs_1 (rtx, enum rtx_code, enum rtx_code);
133 static bool arm_size_rtx_costs (rtx, int, int, int *);
134 static bool arm_slowmul_rtx_costs (rtx, int, int, int *);
135 static bool arm_fastmul_rtx_costs (rtx, int, int, int *);
136 static bool arm_xscale_rtx_costs (rtx, int, int, int *);
137 static bool arm_9e_rtx_costs (rtx, int, int, int *);
138 static int arm_address_cost (rtx);
139 static bool arm_memory_load_p (rtx);
140 static bool arm_cirrus_insn_p (rtx);
141 static void cirrus_reorg (rtx);
142 static void arm_init_builtins (void);
143 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
144 static void arm_init_iwmmxt_builtins (void);
145 static rtx safe_vector_operand (rtx, enum machine_mode);
146 static rtx arm_expand_binop_builtin (enum insn_code, tree, rtx);
147 static rtx arm_expand_unop_builtin (enum insn_code, tree, rtx, int);
148 static rtx arm_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
149 static void emit_constant_insn (rtx cond, rtx pattern);
150 static int arm_arg_partial_bytes (CUMULATIVE_ARGS *, enum machine_mode,
151                                   tree, bool);
152
153 #ifndef ARM_PE
154 static void arm_encode_section_info (tree, rtx, int);
155 #endif
156
157 static void arm_file_end (void);
158
159 #ifdef AOF_ASSEMBLER
160 static void aof_globalize_label (FILE *, const char *);
161 static void aof_dump_imports (FILE *);
162 static void aof_dump_pic_table (FILE *);
163 static void aof_file_start (void);
164 static void aof_file_end (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
175 static tree arm_cxx_guard_type (void);
176 static bool arm_cxx_guard_mask_bit (void);
177 static tree arm_get_cookie_size (tree);
178 static bool arm_cookie_has_size (void);
179 static bool arm_cxx_cdtor_returns_this (void);
180 static bool arm_cxx_key_method_may_be_inline (void);
181 static bool arm_cxx_export_class_data (void);
182 static void arm_init_libfuncs (void);
183 static unsigned HOST_WIDE_INT arm_shift_truncation_mask (enum machine_mode);
184 \f
185 /* Initialize the GCC target structure.  */
186 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
187 #undef  TARGET_MERGE_DECL_ATTRIBUTES
188 #define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
189 #endif
190
191 #undef  TARGET_ATTRIBUTE_TABLE
192 #define TARGET_ATTRIBUTE_TABLE arm_attribute_table
193
194 #undef TARGET_ASM_FILE_END
195 #define TARGET_ASM_FILE_END arm_file_end
196
197 #ifdef AOF_ASSEMBLER
198 #undef  TARGET_ASM_BYTE_OP
199 #define TARGET_ASM_BYTE_OP "\tDCB\t"
200 #undef  TARGET_ASM_ALIGNED_HI_OP
201 #define TARGET_ASM_ALIGNED_HI_OP "\tDCW\t"
202 #undef  TARGET_ASM_ALIGNED_SI_OP
203 #define TARGET_ASM_ALIGNED_SI_OP "\tDCD\t"
204 #undef TARGET_ASM_GLOBALIZE_LABEL
205 #define TARGET_ASM_GLOBALIZE_LABEL aof_globalize_label
206 #undef TARGET_ASM_FILE_START
207 #define TARGET_ASM_FILE_START aof_file_start
208 #undef TARGET_ASM_FILE_END
209 #define TARGET_ASM_FILE_END aof_file_end
210 #else
211 #undef  TARGET_ASM_ALIGNED_SI_OP
212 #define TARGET_ASM_ALIGNED_SI_OP NULL
213 #undef  TARGET_ASM_INTEGER
214 #define TARGET_ASM_INTEGER arm_assemble_integer
215 #endif
216
217 #undef  TARGET_ASM_FUNCTION_PROLOGUE
218 #define TARGET_ASM_FUNCTION_PROLOGUE arm_output_function_prologue
219
220 #undef  TARGET_ASM_FUNCTION_EPILOGUE
221 #define TARGET_ASM_FUNCTION_EPILOGUE arm_output_function_epilogue
222
223 #undef  TARGET_COMP_TYPE_ATTRIBUTES
224 #define TARGET_COMP_TYPE_ATTRIBUTES arm_comp_type_attributes
225
226 #undef  TARGET_SET_DEFAULT_TYPE_ATTRIBUTES
227 #define TARGET_SET_DEFAULT_TYPE_ATTRIBUTES arm_set_default_type_attributes
228
229 #undef  TARGET_SCHED_ADJUST_COST
230 #define TARGET_SCHED_ADJUST_COST arm_adjust_cost
231
232 #undef TARGET_ENCODE_SECTION_INFO
233 #ifdef ARM_PE
234 #define TARGET_ENCODE_SECTION_INFO  arm_pe_encode_section_info
235 #else
236 #define TARGET_ENCODE_SECTION_INFO  arm_encode_section_info
237 #endif
238
239 #undef  TARGET_STRIP_NAME_ENCODING
240 #define TARGET_STRIP_NAME_ENCODING arm_strip_name_encoding
241
242 #undef  TARGET_ASM_INTERNAL_LABEL
243 #define TARGET_ASM_INTERNAL_LABEL arm_internal_label
244
245 #undef  TARGET_FUNCTION_OK_FOR_SIBCALL
246 #define TARGET_FUNCTION_OK_FOR_SIBCALL arm_function_ok_for_sibcall
247
248 #undef  TARGET_ASM_OUTPUT_MI_THUNK
249 #define TARGET_ASM_OUTPUT_MI_THUNK arm_output_mi_thunk
250 #undef  TARGET_ASM_CAN_OUTPUT_MI_THUNK
251 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK default_can_output_mi_thunk_no_vcall
252
253 /* This will be overridden in arm_override_options.  */
254 #undef  TARGET_RTX_COSTS
255 #define TARGET_RTX_COSTS arm_slowmul_rtx_costs
256 #undef  TARGET_ADDRESS_COST
257 #define TARGET_ADDRESS_COST arm_address_cost
258
259 #undef TARGET_SHIFT_TRUNCATION_MASK
260 #define TARGET_SHIFT_TRUNCATION_MASK arm_shift_truncation_mask
261 #undef TARGET_VECTOR_MODE_SUPPORTED_P
262 #define TARGET_VECTOR_MODE_SUPPORTED_P arm_vector_mode_supported_p
263
264 #undef  TARGET_MACHINE_DEPENDENT_REORG
265 #define TARGET_MACHINE_DEPENDENT_REORG arm_reorg
266
267 #undef  TARGET_INIT_BUILTINS
268 #define TARGET_INIT_BUILTINS  arm_init_builtins
269 #undef  TARGET_EXPAND_BUILTIN
270 #define TARGET_EXPAND_BUILTIN arm_expand_builtin
271
272 #undef TARGET_INIT_LIBFUNCS
273 #define TARGET_INIT_LIBFUNCS arm_init_libfuncs
274
275 #undef TARGET_PROMOTE_FUNCTION_ARGS
276 #define TARGET_PROMOTE_FUNCTION_ARGS hook_bool_tree_true
277 #undef TARGET_PROMOTE_FUNCTION_RETURN
278 #define TARGET_PROMOTE_FUNCTION_RETURN hook_bool_tree_true
279 #undef TARGET_PROMOTE_PROTOTYPES
280 #define TARGET_PROMOTE_PROTOTYPES arm_promote_prototypes
281 #undef TARGET_PASS_BY_REFERENCE
282 #define TARGET_PASS_BY_REFERENCE arm_pass_by_reference
283 #undef TARGET_ARG_PARTIAL_BYTES
284 #define TARGET_ARG_PARTIAL_BYTES arm_arg_partial_bytes
285
286 #undef TARGET_STRUCT_VALUE_RTX
287 #define TARGET_STRUCT_VALUE_RTX arm_struct_value_rtx
288
289 #undef  TARGET_SETUP_INCOMING_VARARGS
290 #define TARGET_SETUP_INCOMING_VARARGS arm_setup_incoming_varargs
291
292 #undef TARGET_DEFAULT_SHORT_ENUMS
293 #define TARGET_DEFAULT_SHORT_ENUMS arm_default_short_enums
294
295 #undef TARGET_ALIGN_ANON_BITFIELD
296 #define TARGET_ALIGN_ANON_BITFIELD arm_align_anon_bitfield
297
298 #undef TARGET_CXX_GUARD_TYPE
299 #define TARGET_CXX_GUARD_TYPE arm_cxx_guard_type
300
301 #undef TARGET_CXX_GUARD_MASK_BIT
302 #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit
303
304 #undef TARGET_CXX_GET_COOKIE_SIZE
305 #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size
306
307 #undef TARGET_CXX_COOKIE_HAS_SIZE
308 #define TARGET_CXX_COOKIE_HAS_SIZE arm_cookie_has_size
309
310 #undef TARGET_CXX_CDTOR_RETURNS_THIS
311 #define TARGET_CXX_CDTOR_RETURNS_THIS arm_cxx_cdtor_returns_this
312
313 #undef TARGET_CXX_KEY_METHOD_MAY_BE_INLINE
314 #define TARGET_CXX_KEY_METHOD_MAY_BE_INLINE arm_cxx_key_method_may_be_inline
315
316 #undef TARGET_CXX_EXPORT_CLASS_DATA
317 #define TARGET_CXX_EXPORT_CLASS_DATA arm_cxx_export_class_data
318
319 struct gcc_target targetm = TARGET_INITIALIZER;
320 \f
321 /* Obstack for minipool constant handling.  */
322 static struct obstack minipool_obstack;
323 static char *         minipool_startobj;
324
325 /* The maximum number of insns skipped which
326    will be conditionalised if possible.  */
327 static int max_insns_skipped = 5;
328
329 extern FILE * asm_out_file;
330
331 /* True if we are currently building a constant table.  */
332 int making_const_table;
333
334 /* Define the information needed to generate branch insns.  This is
335    stored from the compare operation.  */
336 rtx arm_compare_op0, arm_compare_op1;
337
338 /* The processor for which instructions should be scheduled.  */
339 enum processor_type arm_tune = arm_none;
340
341 /* Which floating point model to use.  */
342 enum arm_fp_model arm_fp_model;
343
344 /* Which floating point hardware is available.  */
345 enum fputype arm_fpu_arch;
346
347 /* Which floating point hardware to schedule for.  */
348 enum fputype arm_fpu_tune;
349
350 /* Whether to use floating point hardware.  */
351 enum float_abi_type arm_float_abi;
352
353 /* Which ABI to use.  */
354 enum arm_abi_type arm_abi;
355
356 /* Set by the -mfpu=... option.  */
357 const char * target_fpu_name = NULL;
358
359 /* Set by the -mfpe=... option.  */
360 const char * target_fpe_name = NULL;
361
362 /* Set by the -mfloat-abi=... option.  */
363 const char * target_float_abi_name = NULL;
364
365 /* Set by the legacy -mhard-float and -msoft-float options.  */
366 const char * target_float_switch = NULL;
367
368 /* Set by the -mabi=... option.  */
369 const char * target_abi_name = NULL;
370
371 /* Used to parse -mstructure_size_boundary command line option.  */
372 const char * structure_size_string = NULL;
373 int    arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
374
375 /* Used for Thumb call_via trampolines.  */
376 rtx thumb_call_via_label[13];
377 static int thumb_call_reg_needed;
378
379 /* Bit values used to identify processor capabilities.  */
380 #define FL_CO_PROC    (1 << 0)        /* Has external co-processor bus */
381 #define FL_ARCH3M     (1 << 1)        /* Extended multiply */
382 #define FL_MODE26     (1 << 2)        /* 26-bit mode support */
383 #define FL_MODE32     (1 << 3)        /* 32-bit mode support */
384 #define FL_ARCH4      (1 << 4)        /* Architecture rel 4 */
385 #define FL_ARCH5      (1 << 5)        /* Architecture rel 5 */
386 #define FL_THUMB      (1 << 6)        /* Thumb aware */
387 #define FL_LDSCHED    (1 << 7)        /* Load scheduling necessary */
388 #define FL_STRONG     (1 << 8)        /* StrongARM */
389 #define FL_ARCH5E     (1 << 9)        /* DSP extensions to v5 */
390 #define FL_XSCALE     (1 << 10)       /* XScale */
391 #define FL_CIRRUS     (1 << 11)       /* Cirrus/DSP.  */
392 #define FL_ARCH6      (1 << 12)       /* Architecture rel 6.  Adds
393                                          media instructions.  */
394 #define FL_VFPV2      (1 << 13)       /* Vector Floating Point V2.  */
395
396 #define FL_IWMMXT     (1 << 29)       /* XScale v2 or "Intel Wireless MMX technology".  */
397
398 #define FL_FOR_ARCH2    0
399 #define FL_FOR_ARCH3    FL_MODE32
400 #define FL_FOR_ARCH3M   (FL_FOR_ARCH3 | FL_ARCH3M)
401 #define FL_FOR_ARCH4    (FL_FOR_ARCH3M | FL_ARCH4)
402 #define FL_FOR_ARCH4T   (FL_FOR_ARCH4 | FL_THUMB)
403 #define FL_FOR_ARCH5    (FL_FOR_ARCH4 | FL_ARCH5)
404 #define FL_FOR_ARCH5T   (FL_FOR_ARCH5 | FL_THUMB)
405 #define FL_FOR_ARCH5E   (FL_FOR_ARCH5 | FL_ARCH5E)
406 #define FL_FOR_ARCH5TE  (FL_FOR_ARCH5E | FL_THUMB)
407 #define FL_FOR_ARCH5TEJ FL_FOR_ARCH5TE
408 #define FL_FOR_ARCH6    (FL_FOR_ARCH5TE | FL_ARCH6)
409 #define FL_FOR_ARCH6J   FL_FOR_ARCH6
410 #define FL_FOR_ARCH6K   FL_FOR_ARCH6
411 #define FL_FOR_ARCH6Z   FL_FOR_ARCH6
412 #define FL_FOR_ARCH6ZK  FL_FOR_ARCH6
413
414 /* The bits in this mask specify which
415    instructions we are allowed to generate.  */
416 static unsigned long insn_flags = 0;
417
418 /* The bits in this mask specify which instruction scheduling options should
419    be used.  */
420 static unsigned long tune_flags = 0;
421
422 /* The following are used in the arm.md file as equivalents to bits
423    in the above two flag variables.  */
424
425 /* Nonzero if this chip supports the ARM Architecture 3M extensions.  */
426 int arm_arch3m = 0;
427
428 /* Nonzero if this chip supports the ARM Architecture 4 extensions.  */
429 int arm_arch4 = 0;
430
431 /* Nonzero if this chip supports the ARM Architecture 4t extensions.  */
432 int arm_arch4t = 0;
433
434 /* Nonzero if this chip supports the ARM Architecture 5 extensions.  */
435 int arm_arch5 = 0;
436
437 /* Nonzero if this chip supports the ARM Architecture 5E extensions.  */
438 int arm_arch5e = 0;
439
440 /* Nonzero if this chip supports the ARM Architecture 6 extensions.  */
441 int arm_arch6 = 0;
442
443 /* Nonzero if this chip can benefit from load scheduling.  */
444 int arm_ld_sched = 0;
445
446 /* Nonzero if this chip is a StrongARM.  */
447 int arm_is_strong = 0;
448
449 /* Nonzero if this chip is a Cirrus variant.  */
450 int arm_arch_cirrus = 0;
451
452 /* Nonzero if this chip supports Intel Wireless MMX technology.  */
453 int arm_arch_iwmmxt = 0;
454
455 /* Nonzero if this chip is an XScale.  */
456 int arm_arch_xscale = 0;
457
458 /* Nonzero if tuning for XScale  */
459 int arm_tune_xscale = 0;
460
461 /* Nonzero if this chip is an ARM6 or an ARM7.  */
462 int arm_is_6_or_7 = 0;
463
464 /* Nonzero if generating Thumb instructions.  */
465 int thumb_code = 0;
466
467 /* Nonzero if we should define __THUMB_INTERWORK__ in the
468    preprocessor.
469    XXX This is a bit of a hack, it's intended to help work around
470    problems in GLD which doesn't understand that armv5t code is
471    interworking clean.  */
472 int arm_cpp_interwork = 0;
473
474 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
475    must report the mode of the memory reference from PRINT_OPERAND to
476    PRINT_OPERAND_ADDRESS.  */
477 enum machine_mode output_memory_reference_mode;
478
479 /* The register number to be used for the PIC offset register.  */
480 const char * arm_pic_register_string = NULL;
481 int arm_pic_register = INVALID_REGNUM;
482
483 /* Set to 1 when a return insn is output, this means that the epilogue
484    is not needed.  */
485 int return_used_this_function;
486
487 /* Set to 1 after arm_reorg has started.  Reset to start at the start of
488    the next function.  */
489 static int after_arm_reorg = 0;
490
491 /* The maximum number of insns to be used when loading a constant.  */
492 static int arm_constant_limit = 3;
493
494 /* For an explanation of these variables, see final_prescan_insn below.  */
495 int arm_ccfsm_state;
496 enum arm_cond_code arm_current_cc;
497 rtx arm_target_insn;
498 int arm_target_label;
499
500 /* The condition codes of the ARM, and the inverse function.  */
501 static const char * const arm_condition_codes[] =
502 {
503   "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
504   "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
505 };
506
507 #define streq(string1, string2) (strcmp (string1, string2) == 0)
508 \f
509 /* Initialization code.  */
510
511 struct processors
512 {
513   const char *const name;
514   enum processor_type core;
515   const char *arch;
516   const unsigned long flags;
517   bool (* rtx_costs) (rtx, int, int, int *);
518 };
519
520 /* Not all of these give usefully different compilation alternatives,
521    but there is no simple way of generalizing them.  */
522 static const struct processors all_cores[] =
523 {
524   /* ARM Cores */
525 #define ARM_CORE(NAME, IDENT, ARCH, FLAGS, COSTS) \
526   {NAME, arm_none, #ARCH, FLAGS | FL_FOR_ARCH##ARCH, arm_##COSTS##_rtx_costs},
527 #include "arm-cores.def"
528 #undef ARM_CORE
529   {NULL, arm_none, NULL, 0, NULL}
530 };
531
532 static const struct processors all_architectures[] =
533 {
534   /* ARM Architectures */
535   /* We don't specify rtx_costs here as it will be figured out
536      from the core.  */
537
538   {"armv2",   arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
539   {"armv2a",  arm2,       "2",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH2, NULL},
540   {"armv3",   arm6,       "3",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3, NULL},
541   {"armv3m",  arm7m,      "3M",  FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH3M, NULL},
542   {"armv4",   arm7tdmi,   "4",   FL_CO_PROC | FL_MODE26 | FL_FOR_ARCH4, NULL},
543   /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
544      implementations that support it, so we will leave it out for now.  */
545   {"armv4t",  arm7tdmi,   "4T",  FL_CO_PROC |             FL_FOR_ARCH4T, NULL},
546   {"armv5",   arm10tdmi,  "5",   FL_CO_PROC |             FL_FOR_ARCH5, NULL},
547   {"armv5t",  arm10tdmi,  "5T",  FL_CO_PROC |             FL_FOR_ARCH5T, NULL},
548   {"armv5e",  arm1026ejs, "5E",  FL_CO_PROC |             FL_FOR_ARCH5E, NULL},
549   {"armv5te", arm1026ejs, "5TE", FL_CO_PROC |             FL_FOR_ARCH5TE, NULL},
550   {"armv6",   arm1136js,  "6",   FL_CO_PROC |             FL_FOR_ARCH6, NULL},
551   {"armv6j",  arm1136js,  "6J",  FL_CO_PROC |             FL_FOR_ARCH6J, NULL},
552   {"armv6k",  mpcore,     "6K",  FL_CO_PROC |             FL_FOR_ARCH6K, NULL},
553   {"armv6z",  arm1176jzs, "6Z",  FL_CO_PROC |             FL_FOR_ARCH6Z, NULL},
554   {"armv6zk", arm1176jzs, "6ZK", FL_CO_PROC |             FL_FOR_ARCH6ZK, NULL},
555   {"ep9312",  ep9312,     "4T",  FL_LDSCHED | FL_CIRRUS | FL_FOR_ARCH4, NULL},
556   {"iwmmxt",  iwmmxt,     "5TE", FL_LDSCHED | FL_STRONG | FL_FOR_ARCH5TE | FL_XSCALE | FL_IWMMXT , NULL},
557   {NULL, arm_none, NULL, 0 , NULL}
558 };
559
560 /* This is a magic structure.  The 'string' field is magically filled in
561    with a pointer to the value specified by the user on the command line
562    assuming that the user has specified such a value.  */
563
564 struct arm_cpu_select arm_select[] =
565 {
566   /* string       name            processors  */
567   { NULL,       "-mcpu=",       all_cores  },
568   { NULL,       "-march=",      all_architectures },
569   { NULL,       "-mtune=",      all_cores }
570 };
571
572
573 /* The name of the proprocessor macro to define for this architecture.  */
574
575 char arm_arch_name[] = "__ARM_ARCH_0UNK__";
576
577 struct fpu_desc
578 {
579   const char * name;
580   enum fputype fpu;
581 };
582
583
584 /* Available values for for -mfpu=.  */
585
586 static const struct fpu_desc all_fpus[] =
587 {
588   {"fpa",       FPUTYPE_FPA},
589   {"fpe2",      FPUTYPE_FPA_EMU2},
590   {"fpe3",      FPUTYPE_FPA_EMU2},
591   {"maverick",  FPUTYPE_MAVERICK},
592   {"vfp",       FPUTYPE_VFP}
593 };
594
595
596 /* Floating point models used by the different hardware.
597    See fputype in arm.h.  */
598
599 static const enum fputype fp_model_for_fpu[] =
600 {
601   /* No FP hardware.  */
602   ARM_FP_MODEL_UNKNOWN,         /* FPUTYPE_NONE  */
603   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA  */
604   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU2  */
605   ARM_FP_MODEL_FPA,             /* FPUTYPE_FPA_EMU3  */
606   ARM_FP_MODEL_MAVERICK,        /* FPUTYPE_MAVERICK  */
607   ARM_FP_MODEL_VFP              /* FPUTYPE_VFP  */
608 };
609
610
611 struct float_abi
612 {
613   const char * name;
614   enum float_abi_type abi_type;
615 };
616
617
618 /* Available values for -mfloat-abi=.  */
619
620 static const struct float_abi all_float_abis[] =
621 {
622   {"soft",      ARM_FLOAT_ABI_SOFT},
623   {"softfp",    ARM_FLOAT_ABI_SOFTFP},
624   {"hard",      ARM_FLOAT_ABI_HARD}
625 };
626
627
628 struct abi_name
629 {
630   const char *name;
631   enum arm_abi_type abi_type;
632 };
633
634
635 /* Available values for -mabi=.  */
636
637 static const struct abi_name arm_all_abis[] =
638 {
639   {"apcs-gnu",    ARM_ABI_APCS},
640   {"atpcs",   ARM_ABI_ATPCS},
641   {"aapcs",   ARM_ABI_AAPCS},
642   {"iwmmxt",  ARM_ABI_IWMMXT}
643 };
644
645 /* Return the number of bits set in VALUE.  */
646 static unsigned
647 bit_count (unsigned long value)
648 {
649   unsigned long count = 0;
650
651   while (value)
652     {
653       count++;
654       value &= value - 1;  /* Clear the least-significant set bit.  */
655     }
656
657   return count;
658 }
659
660 /* Set up library functions unique to ARM.  */
661
662 static void
663 arm_init_libfuncs (void)
664 {
665   /* There are no special library functions unless we are using the
666      ARM BPABI.  */
667   if (!TARGET_BPABI)
668     return;
669
670   /* The functions below are described in Section 4 of the "Run-Time
671      ABI for the ARM architecture", Version 1.0.  */
672
673   /* Double-precision floating-point arithmetic.  Table 2.  */
674   set_optab_libfunc (add_optab, DFmode, "__aeabi_dadd");
675   set_optab_libfunc (sdiv_optab, DFmode, "__aeabi_ddiv");
676   set_optab_libfunc (smul_optab, DFmode, "__aeabi_dmul");
677   set_optab_libfunc (neg_optab, DFmode, "__aeabi_dneg");
678   set_optab_libfunc (sub_optab, DFmode, "__aeabi_dsub");
679
680   /* Double-precision comparisons.  Table 3.  */
681   set_optab_libfunc (eq_optab, DFmode, "__aeabi_dcmpeq");
682   set_optab_libfunc (ne_optab, DFmode, NULL);
683   set_optab_libfunc (lt_optab, DFmode, "__aeabi_dcmplt");
684   set_optab_libfunc (le_optab, DFmode, "__aeabi_dcmple");
685   set_optab_libfunc (ge_optab, DFmode, "__aeabi_dcmpge");
686   set_optab_libfunc (gt_optab, DFmode, "__aeabi_dcmpgt");
687   set_optab_libfunc (unord_optab, DFmode, "__aeabi_dcmpun");
688
689   /* Single-precision floating-point arithmetic.  Table 4.  */
690   set_optab_libfunc (add_optab, SFmode, "__aeabi_fadd");
691   set_optab_libfunc (sdiv_optab, SFmode, "__aeabi_fdiv");
692   set_optab_libfunc (smul_optab, SFmode, "__aeabi_fmul");
693   set_optab_libfunc (neg_optab, SFmode, "__aeabi_fneg");
694   set_optab_libfunc (sub_optab, SFmode, "__aeabi_fsub");
695
696   /* Single-precision comparisons.  Table 5.  */
697   set_optab_libfunc (eq_optab, SFmode, "__aeabi_fcmpeq");
698   set_optab_libfunc (ne_optab, SFmode, NULL);
699   set_optab_libfunc (lt_optab, SFmode, "__aeabi_fcmplt");
700   set_optab_libfunc (le_optab, SFmode, "__aeabi_fcmple");
701   set_optab_libfunc (ge_optab, SFmode, "__aeabi_fcmpge");
702   set_optab_libfunc (gt_optab, SFmode, "__aeabi_fcmpgt");
703   set_optab_libfunc (unord_optab, SFmode, "__aeabi_fcmpun");
704
705   /* Floating-point to integer conversions.  Table 6.  */
706   set_conv_libfunc (sfix_optab, SImode, DFmode, "__aeabi_d2iz");
707   set_conv_libfunc (ufix_optab, SImode, DFmode, "__aeabi_d2uiz");
708   set_conv_libfunc (sfix_optab, DImode, DFmode, "__aeabi_d2lz");
709   set_conv_libfunc (ufix_optab, DImode, DFmode, "__aeabi_d2ulz");
710   set_conv_libfunc (sfix_optab, SImode, SFmode, "__aeabi_f2iz");
711   set_conv_libfunc (ufix_optab, SImode, SFmode, "__aeabi_f2uiz");
712   set_conv_libfunc (sfix_optab, DImode, SFmode, "__aeabi_f2lz");
713   set_conv_libfunc (ufix_optab, DImode, SFmode, "__aeabi_f2ulz");
714
715   /* Conversions between floating types.  Table 7.  */
716   set_conv_libfunc (trunc_optab, SFmode, DFmode, "__aeabi_d2f");
717   set_conv_libfunc (sext_optab, DFmode, SFmode, "__aeabi_f2d");
718
719   /* Integer to floating-point conversions.  Table 8.  */
720   set_conv_libfunc (sfloat_optab, DFmode, SImode, "__aeabi_i2d");
721   set_conv_libfunc (ufloat_optab, DFmode, SImode, "__aeabi_ui2d");
722   set_conv_libfunc (sfloat_optab, DFmode, DImode, "__aeabi_l2d");
723   set_conv_libfunc (ufloat_optab, DFmode, DImode, "__aeabi_ul2d");
724   set_conv_libfunc (sfloat_optab, SFmode, SImode, "__aeabi_i2f");
725   set_conv_libfunc (ufloat_optab, SFmode, SImode, "__aeabi_ui2f");
726   set_conv_libfunc (sfloat_optab, SFmode, DImode, "__aeabi_l2f");
727   set_conv_libfunc (ufloat_optab, SFmode, DImode, "__aeabi_ul2f");
728
729   /* Long long.  Table 9.  */
730   set_optab_libfunc (smul_optab, DImode, "__aeabi_lmul");
731   set_optab_libfunc (sdivmod_optab, DImode, "__aeabi_ldivmod");
732   set_optab_libfunc (udivmod_optab, DImode, "__aeabi_uldivmod");
733   set_optab_libfunc (ashl_optab, DImode, "__aeabi_llsl");
734   set_optab_libfunc (lshr_optab, DImode, "__aeabi_llsr");
735   set_optab_libfunc (ashr_optab, DImode, "__aeabi_lasr");
736   set_optab_libfunc (cmp_optab, DImode, "__aeabi_lcmp");
737   set_optab_libfunc (ucmp_optab, DImode, "__aeabi_ulcmp");
738
739   /* Integer (32/32->32) division.  \S 4.3.1.  */
740   set_optab_libfunc (sdivmod_optab, SImode, "__aeabi_idivmod");
741   set_optab_libfunc (udivmod_optab, SImode, "__aeabi_uidivmod");
742
743   /* The divmod functions are designed so that they can be used for
744      plain division, even though they return both the quotient and the
745      remainder.  The quotient is returned in the usual location (i.e.,
746      r0 for SImode, {r0, r1} for DImode), just as would be expected
747      for an ordinary division routine.  Because the AAPCS calling
748      conventions specify that all of { r0, r1, r2, r3 } are
749      callee-saved registers, there is no need to tell the compiler
750      explicitly that those registers are clobbered by these
751      routines.  */
752   set_optab_libfunc (sdiv_optab, DImode, "__aeabi_ldivmod");
753   set_optab_libfunc (udiv_optab, DImode, "__aeabi_uldivmod");
754   set_optab_libfunc (sdiv_optab, SImode, "__aeabi_idivmod");
755   set_optab_libfunc (udiv_optab, SImode, "__aeabi_uidivmod");
756 }
757
758 /* Fix up any incompatible options that the user has specified.
759    This has now turned into a maze.  */
760 void
761 arm_override_options (void)
762 {
763   unsigned i;
764
765   /* Set up the flags based on the cpu/architecture selected by the user.  */
766   for (i = ARRAY_SIZE (arm_select); i--;)
767     {
768       struct arm_cpu_select * ptr = arm_select + i;
769
770       if (ptr->string != NULL && ptr->string[0] != '\0')
771         {
772           const struct processors * sel;
773
774           for (sel = ptr->processors; sel->name != NULL; sel++)
775             if (streq (ptr->string, sel->name))
776               {
777                 /* Set the architecture define.  */
778                 if (i != 2)
779                   sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
780
781                 /* Determine the processor core for which we should
782                    tune code-generation.  */
783                 if (/* -mcpu= is a sensible default.  */
784                     i == 0
785                     /* If -march= is used, and -mcpu= has not been used,
786                        assume that we should tune for a representative
787                        CPU from that architecture.  */
788                     || i == 1
789                     /* -mtune= overrides -mcpu= and -march=.  */
790                     || i == 2)
791                   arm_tune = (enum processor_type) (sel - ptr->processors);
792
793                 if (i != 2)
794                   {
795                     /* If we have been given an architecture and a processor
796                        make sure that they are compatible.  We only generate
797                        a warning though, and we prefer the CPU over the
798                        architecture.  */
799                     if (insn_flags != 0 && (insn_flags ^ sel->flags))
800                       warning ("switch -mcpu=%s conflicts with -march= switch",
801                                ptr->string);
802
803                     insn_flags = sel->flags;
804                   }
805
806                 break;
807               }
808
809           if (sel->name == NULL)
810             error ("bad value (%s) for %s switch", ptr->string, ptr->name);
811         }
812     }
813
814   /* If the user did not specify a processor, choose one for them.  */
815   if (insn_flags == 0)
816     {
817       const struct processors * sel;
818       unsigned int        sought;
819       enum processor_type cpu;
820
821       cpu = TARGET_CPU_DEFAULT;
822       if (cpu == arm_none)
823         {
824 #ifdef SUBTARGET_CPU_DEFAULT
825           /* Use the subtarget default CPU if none was specified by
826              configure.  */
827           cpu = SUBTARGET_CPU_DEFAULT;
828 #endif
829           /* Default to ARM6.  */
830           if (cpu == arm_none)
831             cpu = arm6;
832         }
833       sel = &all_cores[cpu];
834
835       insn_flags = sel->flags;
836
837       /* Now check to see if the user has specified some command line
838          switch that require certain abilities from the cpu.  */
839       sought = 0;
840
841       if (TARGET_INTERWORK || TARGET_THUMB)
842         {
843           sought |= (FL_THUMB | FL_MODE32);
844
845           /* There are no ARM processors that support both APCS-26 and
846              interworking.  Therefore we force FL_MODE26 to be removed
847              from insn_flags here (if it was set), so that the search
848              below will always be able to find a compatible processor.  */
849           insn_flags &= ~FL_MODE26;
850         }
851
852       if (sought != 0 && ((sought & insn_flags) != sought))
853         {
854           /* Try to locate a CPU type that supports all of the abilities
855              of the default CPU, plus the extra abilities requested by
856              the user.  */
857           for (sel = all_cores; sel->name != NULL; sel++)
858             if ((sel->flags & sought) == (sought | insn_flags))
859               break;
860
861           if (sel->name == NULL)
862             {
863               unsigned current_bit_count = 0;
864               const struct processors * best_fit = NULL;
865
866               /* Ideally we would like to issue an error message here
867                  saying that it was not possible to find a CPU compatible
868                  with the default CPU, but which also supports the command
869                  line options specified by the programmer, and so they
870                  ought to use the -mcpu=<name> command line option to
871                  override the default CPU type.
872
873                  If we cannot find a cpu that has both the
874                  characteristics of the default cpu and the given
875                  command line options we scan the array again looking
876                  for a best match.  */
877               for (sel = all_cores; sel->name != NULL; sel++)
878                 if ((sel->flags & sought) == sought)
879                   {
880                     unsigned count;
881
882                     count = bit_count (sel->flags & insn_flags);
883
884                     if (count >= current_bit_count)
885                       {
886                         best_fit = sel;
887                         current_bit_count = count;
888                       }
889                   }
890
891               if (best_fit == NULL)
892                 abort ();
893               else
894                 sel = best_fit;
895             }
896
897           insn_flags = sel->flags;
898         }
899       sprintf (arm_arch_name, "__ARM_ARCH_%s__", sel->arch);
900       if (arm_tune == arm_none)
901         arm_tune = (enum processor_type) (sel - all_cores);
902     }
903
904   /* The processor for which we should tune should now have been
905      chosen.  */
906   if (arm_tune == arm_none)
907     abort ();
908
909   tune_flags = all_cores[(int)arm_tune].flags;
910   if (optimize_size)
911     targetm.rtx_costs = arm_size_rtx_costs;
912   else
913     targetm.rtx_costs = all_cores[(int)arm_tune].rtx_costs;
914
915   /* Make sure that the processor choice does not conflict with any of the
916      other command line choices.  */
917   if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
918     {
919       warning ("target CPU does not support interworking" );
920       target_flags &= ~ARM_FLAG_INTERWORK;
921     }
922
923   if (TARGET_THUMB && !(insn_flags & FL_THUMB))
924     {
925       warning ("target CPU does not support THUMB instructions");
926       target_flags &= ~ARM_FLAG_THUMB;
927     }
928
929   if (TARGET_APCS_FRAME && TARGET_THUMB)
930     {
931       /* warning ("ignoring -mapcs-frame because -mthumb was used"); */
932       target_flags &= ~ARM_FLAG_APCS_FRAME;
933     }
934
935   /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
936      from here where no function is being compiled currently.  */
937   if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
938       && TARGET_ARM)
939     warning ("enabling backtrace support is only meaningful when compiling for the Thumb");
940
941   if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
942     warning ("enabling callee interworking support is only meaningful when compiling for the Thumb");
943
944   if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
945     warning ("enabling caller interworking support is only meaningful when compiling for the Thumb");
946
947   if (TARGET_APCS_STACK && !TARGET_APCS_FRAME)
948     {
949       warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
950       target_flags |= ARM_FLAG_APCS_FRAME;
951     }
952
953   if (TARGET_POKE_FUNCTION_NAME)
954     target_flags |= ARM_FLAG_APCS_FRAME;
955
956   if (TARGET_APCS_REENT && flag_pic)
957     error ("-fpic and -mapcs-reent are incompatible");
958
959   if (TARGET_APCS_REENT)
960     warning ("APCS reentrant code not supported.  Ignored");
961
962   /* If this target is normally configured to use APCS frames, warn if they
963      are turned off and debugging is turned on.  */
964   if (TARGET_ARM
965       && write_symbols != NO_DEBUG
966       && !TARGET_APCS_FRAME
967       && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
968     warning ("-g with -mno-apcs-frame may not give sensible debugging");
969
970   /* If stack checking is disabled, we can use r10 as the PIC register,
971      which keeps r9 available.  */
972   if (flag_pic)
973     arm_pic_register = TARGET_APCS_STACK ? 9 : 10;
974
975   if (TARGET_APCS_FLOAT)
976     warning ("passing floating point arguments in fp regs not yet supported");
977
978   /* Initialize boolean versions of the flags, for use in the arm.md file.  */
979   arm_arch3m = (insn_flags & FL_ARCH3M) != 0;
980   arm_arch4 = (insn_flags & FL_ARCH4) != 0;
981   arm_arch4t = arm_arch4 & ((insn_flags & FL_THUMB) != 0);
982   arm_arch5 = (insn_flags & FL_ARCH5) != 0;
983   arm_arch5e = (insn_flags & FL_ARCH5E) != 0;
984   arm_arch6 = (insn_flags & FL_ARCH6) != 0;
985   arm_arch_xscale = (insn_flags & FL_XSCALE) != 0;
986   arm_arch_cirrus = (insn_flags & FL_CIRRUS) != 0;
987
988   arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
989   arm_is_strong = (tune_flags & FL_STRONG) != 0;
990   thumb_code = (TARGET_ARM == 0);
991   arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
992                     && !(tune_flags & FL_ARCH4))) != 0;
993   arm_tune_xscale = (tune_flags & FL_XSCALE) != 0;
994   arm_arch_iwmmxt = (insn_flags & FL_IWMMXT) != 0;
995
996   /* V5 code we generate is completely interworking capable, so we turn off
997      TARGET_INTERWORK here to avoid many tests later on.  */
998
999   /* XXX However, we must pass the right pre-processor defines to CPP
1000      or GLD can get confused.  This is a hack.  */
1001   if (TARGET_INTERWORK)
1002     arm_cpp_interwork = 1;
1003
1004   if (arm_arch5)
1005     target_flags &= ~ARM_FLAG_INTERWORK;
1006
1007   if (target_abi_name)
1008     {
1009       for (i = 0; i < ARRAY_SIZE (arm_all_abis); i++)
1010         {
1011           if (streq (arm_all_abis[i].name, target_abi_name))
1012             {
1013               arm_abi = arm_all_abis[i].abi_type;
1014               break;
1015             }
1016         }
1017       if (i == ARRAY_SIZE (arm_all_abis))
1018         error ("invalid ABI option: -mabi=%s", target_abi_name);
1019     }
1020   else
1021     arm_abi = ARM_DEFAULT_ABI;
1022
1023   if (TARGET_IWMMXT && !ARM_DOUBLEWORD_ALIGN)
1024     error ("iwmmxt requires an AAPCS compatible ABI for proper operation");
1025
1026   if (TARGET_IWMMXT_ABI && !TARGET_IWMMXT)
1027     error ("iwmmxt abi requires an iwmmxt capable cpu");
1028
1029   arm_fp_model = ARM_FP_MODEL_UNKNOWN;
1030   if (target_fpu_name == NULL && target_fpe_name != NULL)
1031     {
1032       if (streq (target_fpe_name, "2"))
1033         target_fpu_name = "fpe2";
1034       else if (streq (target_fpe_name, "3"))
1035         target_fpu_name = "fpe3";
1036       else
1037         error ("invalid floating point emulation option: -mfpe=%s",
1038                target_fpe_name);
1039     }
1040   if (target_fpu_name != NULL)
1041     {
1042       /* The user specified a FPU.  */
1043       for (i = 0; i < ARRAY_SIZE (all_fpus); i++)
1044         {
1045           if (streq (all_fpus[i].name, target_fpu_name))
1046             {
1047               arm_fpu_arch = all_fpus[i].fpu;
1048               arm_fpu_tune = arm_fpu_arch;
1049               arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1050               break;
1051             }
1052         }
1053       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1054         error ("invalid floating point option: -mfpu=%s", target_fpu_name);
1055     }
1056   else
1057     {
1058 #ifdef FPUTYPE_DEFAULT
1059       /* Use the default if it is specified for this platform.  */
1060       arm_fpu_arch = FPUTYPE_DEFAULT;
1061       arm_fpu_tune = FPUTYPE_DEFAULT;
1062 #else
1063       /* Pick one based on CPU type.  */
1064       /* ??? Some targets assume FPA is the default.
1065       if ((insn_flags & FL_VFP) != 0)
1066         arm_fpu_arch = FPUTYPE_VFP;
1067       else
1068       */
1069       if (arm_arch_cirrus)
1070         arm_fpu_arch = FPUTYPE_MAVERICK;
1071       else
1072         arm_fpu_arch = FPUTYPE_FPA_EMU2;
1073 #endif
1074       if (tune_flags & FL_CO_PROC && arm_fpu_arch == FPUTYPE_FPA_EMU2)
1075         arm_fpu_tune = FPUTYPE_FPA;
1076       else
1077         arm_fpu_tune = arm_fpu_arch;
1078       arm_fp_model = fp_model_for_fpu[arm_fpu_arch];
1079       if (arm_fp_model == ARM_FP_MODEL_UNKNOWN)
1080         abort ();
1081     }
1082
1083   if (target_float_abi_name != NULL)
1084     {
1085       /* The user specified a FP ABI.  */
1086       for (i = 0; i < ARRAY_SIZE (all_float_abis); i++)
1087         {
1088           if (streq (all_float_abis[i].name, target_float_abi_name))
1089             {
1090               arm_float_abi = all_float_abis[i].abi_type;
1091               break;
1092             }
1093         }
1094       if (i == ARRAY_SIZE (all_float_abis))
1095         error ("invalid floating point abi: -mfloat-abi=%s",
1096                target_float_abi_name);
1097     }
1098   else if (target_float_switch)
1099     {
1100       /* This is a bit of a hack to avoid needing target flags for these.  */
1101       if (target_float_switch[0] == 'h')
1102         arm_float_abi = ARM_FLOAT_ABI_HARD;
1103       else
1104         arm_float_abi = ARM_FLOAT_ABI_SOFT;
1105     }
1106   else
1107     arm_float_abi = TARGET_DEFAULT_FLOAT_ABI;
1108
1109   if (arm_float_abi == ARM_FLOAT_ABI_HARD && TARGET_VFP)
1110     sorry ("-mfloat-abi=hard and VFP");
1111
1112   /* If soft-float is specified then don't use FPU.  */
1113   if (TARGET_SOFT_FLOAT)
1114     arm_fpu_arch = FPUTYPE_NONE;
1115
1116   /* For arm2/3 there is no need to do any scheduling if there is only
1117      a floating point emulator, or we are doing software floating-point.  */
1118   if ((TARGET_SOFT_FLOAT
1119        || arm_fpu_tune == FPUTYPE_FPA_EMU2
1120        || arm_fpu_tune == FPUTYPE_FPA_EMU3)
1121       && (tune_flags & FL_MODE32) == 0)
1122     flag_schedule_insns = flag_schedule_insns_after_reload = 0;
1123
1124   /* Override the default structure alignment for AAPCS ABI.  */
1125   if (arm_abi == ARM_ABI_AAPCS)
1126     arm_structure_size_boundary = 8;
1127
1128   if (structure_size_string != NULL)
1129     {
1130       int size = strtol (structure_size_string, NULL, 0);
1131
1132       if (size == 8 || size == 32
1133           || (ARM_DOUBLEWORD_ALIGN && size == 64))
1134         arm_structure_size_boundary = size;
1135       else
1136         warning ("structure size boundary can only be set to %s",
1137                  ARM_DOUBLEWORD_ALIGN ? "8, 32 or 64": "8 or 32");
1138     }
1139
1140   if (arm_pic_register_string != NULL)
1141     {
1142       int pic_register = decode_reg_name (arm_pic_register_string);
1143
1144       if (!flag_pic)
1145         warning ("-mpic-register= is useless without -fpic");
1146
1147       /* Prevent the user from choosing an obviously stupid PIC register.  */
1148       else if (pic_register < 0 || call_used_regs[pic_register]
1149                || pic_register == HARD_FRAME_POINTER_REGNUM
1150                || pic_register == STACK_POINTER_REGNUM
1151                || pic_register >= PC_REGNUM)
1152         error ("unable to use '%s' for PIC register", arm_pic_register_string);
1153       else
1154         arm_pic_register = pic_register;
1155     }
1156
1157   if (TARGET_THUMB && flag_schedule_insns)
1158     {
1159       /* Don't warn since it's on by default in -O2.  */
1160       flag_schedule_insns = 0;
1161     }
1162
1163   if (optimize_size)
1164     {
1165       /* There's some dispute as to whether this should be 1 or 2.  However,
1166          experiments seem to show that in pathological cases a setting of
1167          1 degrades less severely than a setting of 2.  This could change if
1168          other parts of the compiler change their behavior.  */
1169       arm_constant_limit = 1;
1170
1171       /* If optimizing for size, bump the number of instructions that we
1172          are prepared to conditionally execute (even on a StrongARM).  */
1173       max_insns_skipped = 6;
1174     }
1175   else
1176     {
1177       /* For processors with load scheduling, it never costs more than
1178          2 cycles to load a constant, and the load scheduler may well
1179          reduce that to 1.  */
1180       if (arm_ld_sched)
1181         arm_constant_limit = 1;
1182
1183       /* On XScale the longer latency of a load makes it more difficult
1184          to achieve a good schedule, so it's faster to synthesize
1185          constants that can be done in two insns.  */
1186       if (arm_tune_xscale)
1187         arm_constant_limit = 2;
1188
1189       /* StrongARM has early execution of branches, so a sequence
1190          that is worth skipping is shorter.  */
1191       if (arm_is_strong)
1192         max_insns_skipped = 3;
1193     }
1194
1195   /* Register global variables with the garbage collector.  */
1196   arm_add_gc_roots ();
1197 }
1198
1199 static void
1200 arm_add_gc_roots (void)
1201 {
1202   gcc_obstack_init(&minipool_obstack);
1203   minipool_startobj = (char *) obstack_alloc (&minipool_obstack, 0);
1204 }
1205 \f
1206 /* A table of known ARM exception types.
1207    For use with the interrupt function attribute.  */
1208
1209 typedef struct
1210 {
1211   const char *const arg;
1212   const unsigned long return_value;
1213 }
1214 isr_attribute_arg;
1215
1216 static const isr_attribute_arg isr_attribute_args [] =
1217 {
1218   { "IRQ",   ARM_FT_ISR },
1219   { "irq",   ARM_FT_ISR },
1220   { "FIQ",   ARM_FT_FIQ },
1221   { "fiq",   ARM_FT_FIQ },
1222   { "ABORT", ARM_FT_ISR },
1223   { "abort", ARM_FT_ISR },
1224   { "ABORT", ARM_FT_ISR },
1225   { "abort", ARM_FT_ISR },
1226   { "UNDEF", ARM_FT_EXCEPTION },
1227   { "undef", ARM_FT_EXCEPTION },
1228   { "SWI",   ARM_FT_EXCEPTION },
1229   { "swi",   ARM_FT_EXCEPTION },
1230   { NULL,    ARM_FT_NORMAL }
1231 };
1232
1233 /* Returns the (interrupt) function type of the current
1234    function, or ARM_FT_UNKNOWN if the type cannot be determined.  */
1235
1236 static unsigned long
1237 arm_isr_value (tree argument)
1238 {
1239   const isr_attribute_arg * ptr;
1240   const char *              arg;
1241
1242   /* No argument - default to IRQ.  */
1243   if (argument == NULL_TREE)
1244     return ARM_FT_ISR;
1245
1246   /* Get the value of the argument.  */
1247   if (TREE_VALUE (argument) == NULL_TREE
1248       || TREE_CODE (TREE_VALUE (argument)) != STRING_CST)
1249     return ARM_FT_UNKNOWN;
1250
1251   arg = TREE_STRING_POINTER (TREE_VALUE (argument));
1252
1253   /* Check it against the list of known arguments.  */
1254   for (ptr = isr_attribute_args; ptr->arg != NULL; ptr++)
1255     if (streq (arg, ptr->arg))
1256       return ptr->return_value;
1257
1258   /* An unrecognized interrupt type.  */
1259   return ARM_FT_UNKNOWN;
1260 }
1261
1262 /* Computes the type of the current function.  */
1263
1264 static unsigned long
1265 arm_compute_func_type (void)
1266 {
1267   unsigned long type = ARM_FT_UNKNOWN;
1268   tree a;
1269   tree attr;
1270
1271   if (TREE_CODE (current_function_decl) != FUNCTION_DECL)
1272     abort ();
1273
1274   /* Decide if the current function is volatile.  Such functions
1275      never return, and many memory cycles can be saved by not storing
1276      register values that will never be needed again.  This optimization
1277      was added to speed up context switching in a kernel application.  */
1278   if (optimize > 0
1279       && TREE_NOTHROW (current_function_decl)
1280       && TREE_THIS_VOLATILE (current_function_decl))
1281     type |= ARM_FT_VOLATILE;
1282
1283   if (cfun->static_chain_decl != NULL)
1284     type |= ARM_FT_NESTED;
1285
1286   attr = DECL_ATTRIBUTES (current_function_decl);
1287
1288   a = lookup_attribute ("naked", attr);
1289   if (a != NULL_TREE)
1290     type |= ARM_FT_NAKED;
1291
1292   a = lookup_attribute ("isr", attr);
1293   if (a == NULL_TREE)
1294     a = lookup_attribute ("interrupt", attr);
1295
1296   if (a == NULL_TREE)
1297     type |= TARGET_INTERWORK ? ARM_FT_INTERWORKED : ARM_FT_NORMAL;
1298   else
1299     type |= arm_isr_value (TREE_VALUE (a));
1300
1301   return type;
1302 }
1303
1304 /* Returns the type of the current function.  */
1305
1306 unsigned long
1307 arm_current_func_type (void)
1308 {
1309   if (ARM_FUNC_TYPE (cfun->machine->func_type) == ARM_FT_UNKNOWN)
1310     cfun->machine->func_type = arm_compute_func_type ();
1311
1312   return cfun->machine->func_type;
1313 }
1314 \f
1315 /* Return 1 if it is possible to return using a single instruction.
1316    If SIBLING is non-null, this is a test for a return before a sibling
1317    call.  SIBLING is the call insn, so we can examine its register usage.  */
1318
1319 int
1320 use_return_insn (int iscond, rtx sibling)
1321 {
1322   int regno;
1323   unsigned int func_type;
1324   unsigned long saved_int_regs;
1325   unsigned HOST_WIDE_INT stack_adjust;
1326   arm_stack_offsets *offsets;
1327
1328   /* Never use a return instruction before reload has run.  */
1329   if (!reload_completed)
1330     return 0;
1331
1332   func_type = arm_current_func_type ();
1333
1334   /* Naked functions and volatile functions need special
1335      consideration.  */
1336   if (func_type & (ARM_FT_VOLATILE | ARM_FT_NAKED))
1337     return 0;
1338
1339   /* So do interrupt functions that use the frame pointer.  */
1340   if (IS_INTERRUPT (func_type) && frame_pointer_needed)
1341     return 0;
1342
1343   offsets = arm_get_frame_offsets ();
1344   stack_adjust = offsets->outgoing_args - offsets->saved_regs;
1345
1346   /* As do variadic functions.  */
1347   if (current_function_pretend_args_size
1348       || cfun->machine->uses_anonymous_args
1349       /* Or if the function calls __builtin_eh_return () */
1350       || current_function_calls_eh_return
1351       /* Or if the function calls alloca */
1352       || current_function_calls_alloca
1353       /* Or if there is a stack adjustment.  However, if the stack pointer
1354          is saved on the stack, we can use a pre-incrementing stack load.  */
1355       || !(stack_adjust == 0 || (frame_pointer_needed && stack_adjust == 4)))
1356     return 0;
1357
1358   saved_int_regs = arm_compute_save_reg_mask ();
1359
1360   /* Unfortunately, the insn
1361
1362        ldmib sp, {..., sp, ...}
1363
1364      triggers a bug on most SA-110 based devices, such that the stack
1365      pointer won't be correctly restored if the instruction takes a
1366      page fault.  We work around this problem by popping r3 along with
1367      the other registers, since that is never slower than executing
1368      another instruction.
1369
1370      We test for !arm_arch5 here, because code for any architecture
1371      less than this could potentially be run on one of the buggy
1372      chips.  */
1373   if (stack_adjust == 4 && !arm_arch5)
1374     {
1375       /* Validate that r3 is a call-clobbered register (always true in
1376          the default abi) ...  */
1377       if (!call_used_regs[3])
1378         return 0;
1379
1380       /* ... that it isn't being used for a return value (always true
1381          until we implement return-in-regs), or for a tail-call
1382          argument ...  */
1383       if (sibling)
1384         {
1385           if (GET_CODE (sibling) != CALL_INSN)
1386             abort ();
1387
1388           if (find_regno_fusage (sibling, USE, 3))
1389             return 0;
1390         }
1391
1392       /* ... and that there are no call-saved registers in r0-r2
1393          (always true in the default ABI).  */
1394       if (saved_int_regs & 0x7)
1395         return 0;
1396     }
1397
1398   /* Can't be done if interworking with Thumb, and any registers have been
1399      stacked.  */
1400   if (TARGET_INTERWORK && saved_int_regs != 0)
1401     return 0;
1402
1403   /* On StrongARM, conditional returns are expensive if they aren't
1404      taken and multiple registers have been stacked.  */
1405   if (iscond && arm_is_strong)
1406     {
1407       /* Conditional return when just the LR is stored is a simple
1408          conditional-load instruction, that's not expensive.  */
1409       if (saved_int_regs != 0 && saved_int_regs != (1 << LR_REGNUM))
1410         return 0;
1411
1412       if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
1413         return 0;
1414     }
1415
1416   /* If there are saved registers but the LR isn't saved, then we need
1417      two instructions for the return.  */
1418   if (saved_int_regs && !(saved_int_regs & (1 << LR_REGNUM)))
1419     return 0;
1420
1421   /* Can't be done if any of the FPA regs are pushed,
1422      since this also requires an insn.  */
1423   if (TARGET_HARD_FLOAT && TARGET_FPA)
1424     for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
1425       if (regs_ever_live[regno] && !call_used_regs[regno])
1426         return 0;
1427
1428   /* Likewise VFP regs.  */
1429   if (TARGET_HARD_FLOAT && TARGET_VFP)
1430     for (regno = FIRST_VFP_REGNUM; regno <= LAST_VFP_REGNUM; regno++)
1431       if (regs_ever_live[regno] && !call_used_regs[regno])
1432         return 0;
1433
1434   if (TARGET_REALLY_IWMMXT)
1435     for (regno = FIRST_IWMMXT_REGNUM; regno <= LAST_IWMMXT_REGNUM; regno++)
1436       if (regs_ever_live[regno] && ! call_used_regs [regno])
1437         return 0;
1438
1439   return 1;
1440 }
1441
1442 /* Return TRUE if int I is a valid immediate ARM constant.  */
1443
1444 int
1445 const_ok_for_arm (HOST_WIDE_INT i)
1446 {
1447   unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
1448
1449   /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
1450      be all zero, or all one.  */
1451   if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
1452       && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
1453           != ((~(unsigned HOST_WIDE_INT) 0)
1454               & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
1455     return FALSE;
1456
1457   /* Fast return for 0 and powers of 2 */
1458   if ((i & (i - 1)) == 0)
1459     return TRUE;
1460
1461   do
1462     {
1463       if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
1464         return TRUE;
1465       mask =
1466           (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
1467                           >> (32 - 2)) | ~(unsigned HOST_WIDE_INT) 0xffffffff;
1468     }
1469   while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
1470
1471   return FALSE;
1472 }
1473
1474 /* Return true if I is a valid constant for the operation CODE.  */
1475 static int
1476 const_ok_for_op (HOST_WIDE_INT i, enum rtx_code code)
1477 {
1478   if (const_ok_for_arm (i))
1479     return 1;
1480
1481   switch (code)
1482     {
1483     case PLUS:
1484       return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
1485
1486     case MINUS:         /* Should only occur with (MINUS I reg) => rsb */
1487     case XOR:
1488     case IOR:
1489       return 0;
1490
1491     case AND:
1492       return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
1493
1494     default:
1495       abort ();
1496     }
1497 }
1498
1499 /* Emit a sequence of insns to handle a large constant.
1500    CODE is the code of the operation required, it can be any of SET, PLUS,
1501    IOR, AND, XOR, MINUS;
1502    MODE is the mode in which the operation is being performed;
1503    VAL is the integer to operate on;
1504    SOURCE is the other operand (a register, or a null-pointer for SET);
1505    SUBTARGETS means it is safe to create scratch registers if that will
1506    either produce a simpler sequence, or we will want to cse the values.
1507    Return value is the number of insns emitted.  */
1508
1509 int
1510 arm_split_constant (enum rtx_code code, enum machine_mode mode, rtx insn,
1511                     HOST_WIDE_INT val, rtx target, rtx source, int subtargets)
1512 {
1513   rtx cond;
1514
1515   if (insn && GET_CODE (PATTERN (insn)) == COND_EXEC)
1516     cond = COND_EXEC_TEST (PATTERN (insn));
1517   else
1518     cond = NULL_RTX;
1519
1520   if (subtargets || code == SET
1521       || (GET_CODE (target) == REG && GET_CODE (source) == REG
1522           && REGNO (target) != REGNO (source)))
1523     {
1524       /* After arm_reorg has been called, we can't fix up expensive
1525          constants by pushing them into memory so we must synthesize
1526          them in-line, regardless of the cost.  This is only likely to
1527          be more costly on chips that have load delay slots and we are
1528          compiling without running the scheduler (so no splitting
1529          occurred before the final instruction emission).
1530
1531          Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
1532       */
1533       if (!after_arm_reorg
1534           && !cond
1535           && (arm_gen_constant (code, mode, NULL_RTX, val, target, source,
1536                                 1, 0)
1537               > arm_constant_limit + (code != SET)))
1538         {
1539           if (code == SET)
1540             {
1541               /* Currently SET is the only monadic value for CODE, all
1542                  the rest are diadic.  */
1543               emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
1544               return 1;
1545             }
1546           else
1547             {
1548               rtx temp = subtargets ? gen_reg_rtx (mode) : target;
1549
1550               emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
1551               /* For MINUS, the value is subtracted from, since we never
1552                  have subtraction of a constant.  */
1553               if (code == MINUS)
1554                 emit_insn (gen_rtx_SET (VOIDmode, target,
1555                                         gen_rtx_MINUS (mode, temp, source)));
1556               else
1557                 emit_insn (gen_rtx_SET (VOIDmode, target,
1558                                         gen_rtx_fmt_ee (code, mode, source, temp)));
1559               return 2;
1560             }
1561         }
1562     }
1563
1564   return arm_gen_constant (code, mode, cond, val, target, source, subtargets,
1565                            1);
1566 }
1567
1568 static int
1569 count_insns_for_constant (HOST_WIDE_INT remainder, int i)
1570 {
1571   HOST_WIDE_INT temp1;
1572   int num_insns = 0;
1573   do
1574     {
1575       int end;
1576
1577       if (i <= 0)
1578         i += 32;
1579       if (remainder & (3 << (i - 2)))
1580         {
1581           end = i - 8;
1582           if (end < 0)
1583             end += 32;
1584           temp1 = remainder & ((0x0ff << end)
1585                                     | ((i < end) ? (0xff >> (32 - end)) : 0));
1586           remainder &= ~temp1;
1587           num_insns++;
1588           i -= 6;
1589         }
1590       i -= 2;
1591     } while (remainder);
1592   return num_insns;
1593 }
1594
1595 /* Emit an instruction with the indicated PATTERN.  If COND is
1596    non-NULL, conditionalize the execution of the instruction on COND
1597    being true.  */
1598
1599 static void
1600 emit_constant_insn (rtx cond, rtx pattern)
1601 {
1602   if (cond)
1603     pattern = gen_rtx_COND_EXEC (VOIDmode, copy_rtx (cond), pattern);
1604   emit_insn (pattern);
1605 }
1606
1607 /* As above, but extra parameter GENERATE which, if clear, suppresses
1608    RTL generation.  */
1609
1610 static int
1611 arm_gen_constant (enum rtx_code code, enum machine_mode mode, rtx cond,
1612                   HOST_WIDE_INT val, rtx target, rtx source, int subtargets,
1613                   int generate)
1614 {
1615   int can_invert = 0;
1616   int can_negate = 0;
1617   int can_negate_initial = 0;
1618   int can_shift = 0;
1619   int i;
1620   int num_bits_set = 0;
1621   int set_sign_bit_copies = 0;
1622   int clear_sign_bit_copies = 0;
1623   int clear_zero_bit_copies = 0;
1624   int set_zero_bit_copies = 0;
1625   int insns = 0;
1626   unsigned HOST_WIDE_INT temp1, temp2;
1627   unsigned HOST_WIDE_INT remainder = val & 0xffffffff;
1628
1629   /* Find out which operations are safe for a given CODE.  Also do a quick
1630      check for degenerate cases; these can occur when DImode operations
1631      are split.  */
1632   switch (code)
1633     {
1634     case SET:
1635       can_invert = 1;
1636       can_shift = 1;
1637       can_negate = 1;
1638       break;
1639
1640     case PLUS:
1641       can_negate = 1;
1642       can_negate_initial = 1;
1643       break;
1644
1645     case IOR:
1646       if (remainder == 0xffffffff)
1647         {
1648           if (generate)
1649             emit_constant_insn (cond,
1650                                 gen_rtx_SET (VOIDmode, target,
1651                                              GEN_INT (ARM_SIGN_EXTEND (val))));
1652           return 1;
1653         }
1654       if (remainder == 0)
1655         {
1656           if (reload_completed && rtx_equal_p (target, source))
1657             return 0;
1658           if (generate)
1659             emit_constant_insn (cond,
1660                                 gen_rtx_SET (VOIDmode, target, source));
1661           return 1;
1662         }
1663       break;
1664
1665     case AND:
1666       if (remainder == 0)
1667         {
1668           if (generate)
1669             emit_constant_insn (cond,
1670                                 gen_rtx_SET (VOIDmode, target, const0_rtx));
1671           return 1;
1672         }
1673       if (remainder == 0xffffffff)
1674         {
1675           if (reload_completed && rtx_equal_p (target, source))
1676             return 0;
1677           if (generate)
1678             emit_constant_insn (cond,
1679                                 gen_rtx_SET (VOIDmode, target, source));
1680           return 1;
1681         }
1682       can_invert = 1;
1683       break;
1684
1685     case XOR:
1686       if (remainder == 0)
1687         {
1688           if (reload_completed && rtx_equal_p (target, source))
1689             return 0;
1690           if (generate)
1691             emit_constant_insn (cond,
1692                                 gen_rtx_SET (VOIDmode, target, source));
1693           return 1;
1694         }
1695       if (remainder == 0xffffffff)
1696         {
1697           if (generate)
1698             emit_constant_insn (cond,
1699                                 gen_rtx_SET (VOIDmode, target,
1700                                              gen_rtx_NOT (mode, source)));
1701           return 1;
1702         }
1703
1704       /* We don't know how to handle this yet below.  */
1705       abort ();
1706
1707     case MINUS:
1708       /* We treat MINUS as (val - source), since (source - val) is always
1709          passed as (source + (-val)).  */
1710       if (remainder == 0)
1711         {
1712           if (generate)
1713             emit_constant_insn (cond,
1714                                 gen_rtx_SET (VOIDmode, target,
1715                                              gen_rtx_NEG (mode, source)));
1716           return 1;
1717         }
1718       if (const_ok_for_arm (val))
1719         {
1720           if (generate)
1721             emit_constant_insn (cond,
1722                                 gen_rtx_SET (VOIDmode, target,
1723                                              gen_rtx_MINUS (mode, GEN_INT (val),
1724                                                             source)));
1725           return 1;
1726         }
1727       can_negate = 1;
1728
1729       break;
1730
1731     default:
1732       abort ();
1733     }
1734
1735   /* If we can do it in one insn get out quickly.  */
1736   if (const_ok_for_arm (val)
1737       || (can_negate_initial && const_ok_for_arm (-val))
1738       || (can_invert && const_ok_for_arm (~val)))
1739     {
1740       if (generate)
1741         emit_constant_insn (cond,
1742                             gen_rtx_SET (VOIDmode, target,
1743                                          (source
1744                                           ? gen_rtx_fmt_ee (code, mode, source,
1745                                                             GEN_INT (val))
1746                                           : GEN_INT (val))));
1747       return 1;
1748     }
1749
1750   /* Calculate a few attributes that may be useful for specific
1751      optimizations.  */
1752   for (i = 31; i >= 0; i--)
1753     {
1754       if ((remainder & (1 << i)) == 0)
1755         clear_sign_bit_copies++;
1756       else
1757         break;
1758     }
1759
1760   for (i = 31; i >= 0; i--)
1761     {
1762       if ((remainder & (1 << i)) != 0)
1763         set_sign_bit_copies++;
1764       else
1765         break;
1766     }
1767
1768   for (i = 0; i <= 31; i++)
1769     {
1770       if ((remainder & (1 << i)) == 0)
1771         clear_zero_bit_copies++;
1772       else
1773         break;
1774     }
1775
1776   for (i = 0; i <= 31; i++)
1777     {
1778       if ((remainder & (1 << i)) != 0)
1779         set_zero_bit_copies++;
1780       else
1781         break;
1782     }
1783
1784   switch (code)
1785     {
1786     case SET:
1787       /* See if we can do this by sign_extending a constant that is known
1788          to be negative.  This is a good, way of doing it, since the shift
1789          may well merge into a subsequent insn.  */
1790       if (set_sign_bit_copies > 1)
1791         {
1792           if (const_ok_for_arm
1793               (temp1 = ARM_SIGN_EXTEND (remainder
1794                                         << (set_sign_bit_copies - 1))))
1795             {
1796               if (generate)
1797                 {
1798                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1799                   emit_constant_insn (cond,
1800                                       gen_rtx_SET (VOIDmode, new_src,
1801                                                    GEN_INT (temp1)));
1802                   emit_constant_insn (cond,
1803                                       gen_ashrsi3 (target, new_src,
1804                                                    GEN_INT (set_sign_bit_copies - 1)));
1805                 }
1806               return 2;
1807             }
1808           /* For an inverted constant, we will need to set the low bits,
1809              these will be shifted out of harm's way.  */
1810           temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1811           if (const_ok_for_arm (~temp1))
1812             {
1813               if (generate)
1814                 {
1815                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1816                   emit_constant_insn (cond,
1817                                       gen_rtx_SET (VOIDmode, new_src,
1818                                                    GEN_INT (temp1)));
1819                   emit_constant_insn (cond,
1820                                       gen_ashrsi3 (target, new_src,
1821                                                    GEN_INT (set_sign_bit_copies - 1)));
1822                 }
1823               return 2;
1824             }
1825         }
1826
1827       /* See if we can generate this by setting the bottom (or the top)
1828          16 bits, and then shifting these into the other half of the
1829          word.  We only look for the simplest cases, to do more would cost
1830          too much.  Be careful, however, not to generate this when the
1831          alternative would take fewer insns.  */
1832       if (val & 0xffff0000)
1833         {
1834           temp1 = remainder & 0xffff0000;
1835           temp2 = remainder & 0x0000ffff;
1836
1837           /* Overlaps outside this range are best done using other methods.  */
1838           for (i = 9; i < 24; i++)
1839             {
1840               if ((((temp2 | (temp2 << i)) & 0xffffffff) == remainder)
1841                   && !const_ok_for_arm (temp2))
1842                 {
1843                   rtx new_src = (subtargets
1844                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1845                                  : target);
1846                   insns = arm_gen_constant (code, mode, cond, temp2, new_src,
1847                                             source, subtargets, generate);
1848                   source = new_src;
1849                   if (generate)
1850                     emit_constant_insn
1851                       (cond,
1852                        gen_rtx_SET
1853                        (VOIDmode, target,
1854                         gen_rtx_IOR (mode,
1855                                      gen_rtx_ASHIFT (mode, source,
1856                                                      GEN_INT (i)),
1857                                      source)));
1858                   return insns + 1;
1859                 }
1860             }
1861
1862           /* Don't duplicate cases already considered.  */
1863           for (i = 17; i < 24; i++)
1864             {
1865               if (((temp1 | (temp1 >> i)) == remainder)
1866                   && !const_ok_for_arm (temp1))
1867                 {
1868                   rtx new_src = (subtargets
1869                                  ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1870                                  : target);
1871                   insns = arm_gen_constant (code, mode, cond, temp1, new_src,
1872                                             source, subtargets, generate);
1873                   source = new_src;
1874                   if (generate)
1875                     emit_constant_insn
1876                       (cond,
1877                        gen_rtx_SET (VOIDmode, target,
1878                                     gen_rtx_IOR
1879                                     (mode,
1880                                      gen_rtx_LSHIFTRT (mode, source,
1881                                                        GEN_INT (i)),
1882                                      source)));
1883                   return insns + 1;
1884                 }
1885             }
1886         }
1887       break;
1888
1889     case IOR:
1890     case XOR:
1891       /* If we have IOR or XOR, and the constant can be loaded in a
1892          single instruction, and we can find a temporary to put it in,
1893          then this can be done in two instructions instead of 3-4.  */
1894       if (subtargets
1895           /* TARGET can't be NULL if SUBTARGETS is 0 */
1896           || (reload_completed && !reg_mentioned_p (target, source)))
1897         {
1898           if (const_ok_for_arm (ARM_SIGN_EXTEND (~val)))
1899             {
1900               if (generate)
1901                 {
1902                   rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1903
1904                   emit_constant_insn (cond,
1905                                       gen_rtx_SET (VOIDmode, sub,
1906                                                    GEN_INT (val)));
1907                   emit_constant_insn (cond,
1908                                       gen_rtx_SET (VOIDmode, target,
1909                                                    gen_rtx_fmt_ee (code, mode,
1910                                                                    source, sub)));
1911                 }
1912               return 2;
1913             }
1914         }
1915
1916       if (code == XOR)
1917         break;
1918
1919       if (set_sign_bit_copies > 8
1920           && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1921         {
1922           if (generate)
1923             {
1924               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1925               rtx shift = GEN_INT (set_sign_bit_copies);
1926
1927               emit_constant_insn
1928                 (cond,
1929                  gen_rtx_SET (VOIDmode, sub,
1930                               gen_rtx_NOT (mode,
1931                                            gen_rtx_ASHIFT (mode,
1932                                                            source,
1933                                                            shift))));
1934               emit_constant_insn
1935                 (cond,
1936                  gen_rtx_SET (VOIDmode, target,
1937                               gen_rtx_NOT (mode,
1938                                            gen_rtx_LSHIFTRT (mode, sub,
1939                                                              shift))));
1940             }
1941           return 2;
1942         }
1943
1944       if (set_zero_bit_copies > 8
1945           && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1946         {
1947           if (generate)
1948             {
1949               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1950               rtx shift = GEN_INT (set_zero_bit_copies);
1951
1952               emit_constant_insn
1953                 (cond,
1954                  gen_rtx_SET (VOIDmode, sub,
1955                               gen_rtx_NOT (mode,
1956                                            gen_rtx_LSHIFTRT (mode,
1957                                                              source,
1958                                                              shift))));
1959               emit_constant_insn
1960                 (cond,
1961                  gen_rtx_SET (VOIDmode, target,
1962                               gen_rtx_NOT (mode,
1963                                            gen_rtx_ASHIFT (mode, sub,
1964                                                            shift))));
1965             }
1966           return 2;
1967         }
1968
1969       if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~val)))
1970         {
1971           if (generate)
1972             {
1973               rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1974               emit_constant_insn (cond,
1975                                   gen_rtx_SET (VOIDmode, sub,
1976                                                gen_rtx_NOT (mode, source)));
1977               source = sub;
1978               if (subtargets)
1979                 sub = gen_reg_rtx (mode);
1980               emit_constant_insn (cond,
1981                                   gen_rtx_SET (VOIDmode, sub,
1982                                                gen_rtx_AND (mode, source,
1983                                                             GEN_INT (temp1))));
1984               emit_constant_insn (cond,
1985                                   gen_rtx_SET (VOIDmode, target,
1986                                                gen_rtx_NOT (mode, sub)));
1987             }
1988           return 3;
1989         }
1990       break;
1991
1992     case AND:
1993       /* See if two shifts will do 2 or more insn's worth of work.  */
1994       if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1995         {
1996           HOST_WIDE_INT shift_mask = ((0xffffffff
1997                                        << (32 - clear_sign_bit_copies))
1998                                       & 0xffffffff);
1999
2000           if ((remainder | shift_mask) != 0xffffffff)
2001             {
2002               if (generate)
2003                 {
2004                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2005                   insns = arm_gen_constant (AND, mode, cond,
2006                                             remainder | shift_mask,
2007                                             new_src, source, subtargets, 1);
2008                   source = new_src;
2009                 }
2010               else
2011                 {
2012                   rtx targ = subtargets ? NULL_RTX : target;
2013                   insns = arm_gen_constant (AND, mode, cond,
2014                                             remainder | shift_mask,
2015                                             targ, source, subtargets, 0);
2016                 }
2017             }
2018
2019           if (generate)
2020             {
2021               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2022               rtx shift = GEN_INT (clear_sign_bit_copies);
2023
2024               emit_insn (gen_ashlsi3 (new_src, source, shift));
2025               emit_insn (gen_lshrsi3 (target, new_src, shift));
2026             }
2027
2028           return insns + 2;
2029         }
2030
2031       if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
2032         {
2033           HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
2034
2035           if ((remainder | shift_mask) != 0xffffffff)
2036             {
2037               if (generate)
2038                 {
2039                   rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2040
2041                   insns = arm_gen_constant (AND, mode, cond,
2042                                             remainder | shift_mask,
2043                                             new_src, source, subtargets, 1);
2044                   source = new_src;
2045                 }
2046               else
2047                 {
2048                   rtx targ = subtargets ? NULL_RTX : target;
2049
2050                   insns = arm_gen_constant (AND, mode, cond,
2051                                             remainder | shift_mask,
2052                                             targ, source, subtargets, 0);
2053                 }
2054             }
2055
2056           if (generate)
2057             {
2058               rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
2059               rtx shift = GEN_INT (clear_zero_bit_copies);
2060
2061               emit_insn (gen_lshrsi3 (new_src, source, shift));
2062               emit_insn (gen_ashlsi3 (target, new_src, shift));
2063             }
2064
2065           return insns + 2;
2066         }
2067
2068       break;
2069
2070     default:
2071       break;
2072     }
2073
2074   for (i = 0; i < 32; i++)
2075     if (remainder & (1 << i))
2076       num_bits_set++;
2077
2078   if (code == AND || (can_invert && num_bits_set > 16))
2079     remainder = (~remainder) & 0xffffffff;
2080   else if (code == PLUS && num_bits_set > 16)
2081     remainder = (-remainder) & 0xffffffff;
2082   else
2083     {
2084       can_invert = 0;
2085       can_negate = 0;
2086     }
2087
2088   /* Now try and find a way of doing the job in either two or three
2089      instructions.
2090      We start by looking for the largest block of zeros that are aligned on
2091      a 2-bit boundary, we then fill up the temps, wrapping around to the
2092      top of the word when we drop off the bottom.
2093      In the worst case this code should produce no more than four insns.  */
2094   {
2095     int best_start = 0;
2096     int best_consecutive_zeros = 0;
2097
2098     for (i = 0; i < 32; i += 2)
2099       {
2100         int consecutive_zeros = 0;
2101
2102         if (!(remainder & (3 << i)))
2103           {
2104             while ((i < 32) && !(remainder & (3 << i)))
2105               {
2106                 consecutive_zeros += 2;
2107                 i += 2;
2108               }
2109             if (consecutive_zeros > best_consecutive_zeros)
2110               {
2111                 best_consecutive_zeros = consecutive_zeros;
2112                 best_start = i - consecutive_zeros;
2113               }
2114             i -= 2;
2115           }
2116       }
2117
2118     /* So long as it won't require any more insns to do so, it's
2119        desirable to emit a small constant (in bits 0...9) in the last
2120        insn.  This way there is more chance that it can be combined with
2121        a later addressing insn to form a pre-indexed load or store
2122        operation.  Consider:
2123
2124                *((volatile int *)0xe0000100) = 1;
2125                *((volatile int *)0xe0000110) = 2;
2126
2127        We want this to wind up as:
2128
2129                 mov rA, #0xe0000000
2130                 mov rB, #1
2131                 str rB, [rA, #0x100]
2132                 mov rB, #2
2133                 str rB, [rA, #0x110]
2134
2135        rather than having to synthesize both large constants from scratch.
2136
2137        Therefore, we calculate how many insns would be required to emit
2138        the constant starting from `best_start', and also starting from
2139        zero (i.e. with bit 31 first to be output).  If `best_start' doesn't
2140        yield a shorter sequence, we may as well use zero.  */
2141     if (best_start != 0
2142         && ((((unsigned HOST_WIDE_INT) 1) << best_start) < remainder)
2143         && (count_insns_for_constant (remainder, 0) <=
2144             count_insns_for_constant (remainder, best_start)))
2145       best_start = 0;
2146
2147     /* Now start emitting the insns.  */
2148     i = best_start;
2149     do
2150       {
2151         int end;
2152
2153         if (i <= 0)
2154           i += 32;
2155         if (remainder & (3 << (i - 2)))
2156           {
2157             end = i - 8;
2158             if (end < 0)
2159               end += 32;
2160             temp1 = remainder & ((0x0ff << end)
2161                                  | ((i < end) ? (0xff >> (32 - end)) : 0));
2162             remainder &= ~temp1;
2163
2164             if (generate)
2165               {
2166                 rtx new_src, temp1_rtx;
2167
2168                 if (code == SET || code == MINUS)
2169                   {
2170                     new_src = (subtargets ? gen_reg_rtx (mode) : target);
2171                     if (can_invert && code != MINUS)
2172                       temp1 = ~temp1;
2173                   }
2174                 else
2175                   {
2176                     if (remainder && subtargets)
2177                       new_src = gen_reg_rtx (mode);
2178                     else
2179                       new_src = target;
2180                     if (can_invert)
2181                       temp1 = ~temp1;
2182                     else if (can_negate)
2183                       temp1 = -temp1;
2184                   }
2185
2186                 temp1 = trunc_int_for_mode (temp1, mode);
2187                 temp1_rtx = GEN_INT (temp1);
2188
2189                 if (code == SET)
2190                   ;
2191                 else if (code == MINUS)
2192                   temp1_rtx = gen_rtx_MINUS (mode, temp1_rtx, source);
2193                 else
2194                   temp1_rtx = gen_rtx_fmt_ee (code, mode, source, temp1_rtx);
2195
2196                 emit_constant_insn (cond,
2197                                     gen_rtx_SET (VOIDmode, new_src,
2198                                                  temp1_rtx));
2199                 source = new_src;
2200               }
2201
2202             if (code == SET)
2203               {
2204                 can_invert = 0;
2205                 code = PLUS;
2206               }
2207             else if (code == MINUS)
2208               code = PLUS;
2209
2210             insns++;
2211             i -= 6;
2212           }
2213         i -= 2;
2214       }
2215     while (remainder);
2216   }
2217
2218   return insns;
2219 }
2220
2221 /* Canonicalize a comparison so that we are more likely to recognize it.
2222    This can be done for a few constant compares, where we can make the
2223    immediate value easier to load.  */
2224
2225 enum rtx_code
2226 arm_canonicalize_comparison (enum rtx_code code, rtx * op1)
2227 {
2228   unsigned HOST_WIDE_INT i = INTVAL (*op1);
2229
2230   switch (code)
2231     {
2232     case EQ:
2233     case NE:
2234       return code;
2235
2236     case GT:
2237     case LE:
2238       if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1)) - 1)
2239           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2240         {
2241           *op1 = GEN_INT (i + 1);
2242           return code == GT ? GE : LT;
2243         }
2244       break;
2245
2246     case GE:
2247     case LT:
2248       if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
2249           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2250         {
2251           *op1 = GEN_INT (i - 1);
2252           return code == GE ? GT : LE;
2253         }
2254       break;
2255
2256     case GTU:
2257     case LEU:
2258       if (i != ~((unsigned HOST_WIDE_INT) 0)
2259           && (const_ok_for_arm (i + 1) || const_ok_for_arm (-(i + 1))))
2260         {
2261           *op1 = GEN_INT (i + 1);
2262           return code == GTU ? GEU : LTU;
2263         }
2264       break;
2265
2266     case GEU:
2267     case LTU:
2268       if (i != 0
2269           && (const_ok_for_arm (i - 1) || const_ok_for_arm (-(i - 1))))
2270         {
2271           *op1 = GEN_INT (i - 1);
2272           return code == GEU ? GTU : LEU;
2273         }
2274       break;
2275
2276     default:
2277       abort ();
2278     }
2279
2280   return code;
2281 }
2282
2283
2284 /* Define how to find the value returned by a function.  */
2285
2286 rtx arm_function_value(tree type, tree func ATTRIBUTE_UNUSED)
2287 {
2288   enum machine_mode mode;
2289   int unsignedp ATTRIBUTE_UNUSED;
2290   rtx r ATTRIBUTE_UNUSED;
2291
2292
2293   mode = TYPE_MODE (type);
2294   /* Promote integer types.  */
2295   if (INTEGRAL_TYPE_P (type))
2296     PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
2297   return LIBCALL_VALUE(mode);
2298 }
2299
2300
2301 /* Decide whether a type should be returned in memory (true)
2302    or in a register (false).  This is called by the macro
2303    RETURN_IN_MEMORY.  */
2304 int
2305 arm_return_in_memory (tree type)
2306 {
2307   HOST_WIDE_INT size;
2308
2309   if (!AGGREGATE_TYPE_P (type) &&
2310       !(TARGET_AAPCS_BASED && TREE_CODE (type) == COMPLEX_TYPE))
2311     /* All simple types are returned in registers.
2312        For AAPCS, complex types are treated the same as aggregates.  */
2313     return 0;
2314
2315   size = int_size_in_bytes (type);
2316
2317   if (arm_abi != ARM_ABI_APCS)
2318     {
2319       /* ATPCS and later return aggregate types in memory only if they are
2320          larger than a word (or are variable size).  */
2321       return (size < 0 || size > UNITS_PER_WORD);
2322     }
2323
2324   /* For the arm-wince targets we choose to be compatible with Microsoft's
2325      ARM and Thumb compilers, which always return aggregates in memory.  */
2326 #ifndef ARM_WINCE
2327   /* All structures/unions bigger than one word are returned in memory.
2328      Also catch the case where int_size_in_bytes returns -1.  In this case
2329      the aggregate is either huge or of variable size, and in either case
2330      we will want to return it via memory and not in a register.  */
2331   if (size < 0 || size > UNITS_PER_WORD)
2332     return 1;
2333
2334   if (TREE_CODE (type) == RECORD_TYPE)
2335     {
2336       tree field;
2337
2338       /* For a struct the APCS says that we only return in a register
2339          if the type is 'integer like' and every addressable element
2340          has an offset of zero.  For practical purposes this means
2341          that the structure can have at most one non bit-field element
2342          and that this element must be the first one in the structure.  */
2343
2344       /* Find the first field, ignoring non FIELD_DECL things which will
2345          have been created by C++.  */
2346       for (field = TYPE_FIELDS (type);
2347            field && TREE_CODE (field) != FIELD_DECL;
2348            field = TREE_CHAIN (field))
2349         continue;
2350
2351       if (field == NULL)
2352         return 0; /* An empty structure.  Allowed by an extension to ANSI C.  */
2353
2354       /* Check that the first field is valid for returning in a register.  */
2355
2356       /* ... Floats are not allowed */
2357       if (FLOAT_TYPE_P (TREE_TYPE (field)))
2358         return 1;
2359
2360       /* ... Aggregates that are not themselves valid for returning in
2361          a register are not allowed.  */
2362       if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2363         return 1;
2364
2365       /* Now check the remaining fields, if any.  Only bitfields are allowed,
2366          since they are not addressable.  */
2367       for (field = TREE_CHAIN (field);
2368            field;
2369            field = TREE_CHAIN (field))
2370         {
2371           if (TREE_CODE (field) != FIELD_DECL)
2372             continue;
2373
2374           if (!DECL_BIT_FIELD_TYPE (field))
2375             return 1;
2376         }
2377
2378       return 0;
2379     }
2380
2381   if (TREE_CODE (type) == UNION_TYPE)
2382     {
2383       tree field;
2384
2385       /* Unions can be returned in registers if every element is
2386          integral, or can be returned in an integer register.  */
2387       for (field = TYPE_FIELDS (type);
2388            field;
2389            field = TREE_CHAIN (field))
2390         {
2391           if (TREE_CODE (field) != FIELD_DECL)
2392             continue;
2393
2394           if (FLOAT_TYPE_P (TREE_TYPE (field)))
2395             return 1;
2396
2397           if (RETURN_IN_MEMORY (TREE_TYPE (field)))
2398             return 1;
2399         }
2400
2401       return 0;
2402     }
2403 #endif /* not ARM_WINCE */
2404
2405   /* Return all other types in memory.  */
2406   return 1;
2407 }
2408
2409 /* Indicate whether or not words of a double are in big-endian order.  */
2410
2411 int
2412 arm_float_words_big_endian (void)
2413 {
2414   if (TARGET_MAVERICK)
2415     return 0;
2416
2417   /* For FPA, float words are always big-endian.  For VFP, floats words
2418      follow the memory system mode.  */
2419
2420   if (TARGET_FPA)
2421     {
2422       return 1;
2423     }
2424
2425   if (TARGET_VFP)
2426     return (TARGET_BIG_END ? 1 : 0);
2427
2428   return 1;
2429 }
2430
2431 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2432    for a call to a function whose data type is FNTYPE.
2433    For a library call, FNTYPE is NULL.  */
2434 void
2435 arm_init_cumulative_args (CUMULATIVE_ARGS *pcum, tree fntype,
2436                           rtx libname  ATTRIBUTE_UNUSED,
2437                           tree fndecl ATTRIBUTE_UNUSED)
2438 {
2439   /* On the ARM, the offset starts at 0.  */
2440   pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype), fntype)) ? 1 : 0);
2441   pcum->iwmmxt_nregs = 0;
2442   pcum->can_split = true;
2443
2444   pcum->call_cookie = CALL_NORMAL;
2445
2446   if (TARGET_LONG_CALLS)
2447     pcum->call_cookie = CALL_LONG;
2448
2449   /* Check for long call/short call attributes.  The attributes
2450      override any command line option.  */
2451   if (fntype)
2452     {
2453       if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
2454         pcum->call_cookie = CALL_SHORT;
2455       else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
2456         pcum->call_cookie = CALL_LONG;
2457     }
2458
2459   /* Varargs vectors are treated the same as long long.
2460      named_count avoids having to change the way arm handles 'named' */
2461   pcum->named_count = 0;
2462   pcum->nargs = 0;
2463
2464   if (TARGET_REALLY_IWMMXT && fntype)
2465     {
2466       tree fn_arg;
2467
2468       for (fn_arg = TYPE_ARG_TYPES (fntype);
2469            fn_arg;
2470            fn_arg = TREE_CHAIN (fn_arg))
2471         pcum->named_count += 1;
2472
2473       if (! pcum->named_count)
2474         pcum->named_count = INT_MAX;
2475     }
2476 }
2477
2478
2479 /* Return true if mode/type need doubleword alignment.  */
2480 bool
2481 arm_needs_doubleword_align (enum machine_mode mode, tree type)
2482 {
2483   return (GET_MODE_ALIGNMENT (mode) > PARM_BOUNDARY
2484           || (type && TYPE_ALIGN (type) > PARM_BOUNDARY));
2485 }
2486
2487
2488 /* Determine where to put an argument to a function.
2489    Value is zero to push the argument on the stack,
2490    or a hard register in which to store the argument.
2491
2492    MODE is the argument's machine mode.
2493    TYPE is the data type of the argument (as a tree).
2494     This is null for libcalls where that information may
2495     not be available.
2496    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2497     the preceding args and about the function being called.
2498    NAMED is nonzero if this argument is a named parameter
2499     (otherwise it is an extra parameter matching an ellipsis).  */
2500
2501 rtx
2502 arm_function_arg (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2503                   tree type, int named)
2504 {
2505   int nregs;
2506
2507   /* Varargs vectors are treated the same as long long.
2508      named_count avoids having to change the way arm handles 'named' */
2509   if (TARGET_IWMMXT_ABI
2510       && arm_vector_mode_supported_p (mode)
2511       && pcum->named_count > pcum->nargs + 1)
2512     {
2513       if (pcum->iwmmxt_nregs <= 9)
2514         return gen_rtx_REG (mode, pcum->iwmmxt_nregs + FIRST_IWMMXT_REGNUM);
2515       else
2516         {
2517           pcum->can_split = false;
2518           return NULL_RTX;
2519         }
2520     }
2521
2522   /* Put doubleword aligned quantities in even register pairs.  */
2523   if (pcum->nregs & 1
2524       && ARM_DOUBLEWORD_ALIGN
2525       && arm_needs_doubleword_align (mode, type))
2526     pcum->nregs++;
2527
2528   if (mode == VOIDmode)
2529     /* Compute operand 2 of the call insn.  */
2530     return GEN_INT (pcum->call_cookie);
2531
2532   /* Only allow splitting an arg between regs and memory if all preceding
2533      args were allocated to regs.  For args passed by reference we only count
2534      the reference pointer.  */
2535   if (pcum->can_split)
2536     nregs = 1;
2537   else
2538     nregs = ARM_NUM_REGS2 (mode, type);
2539
2540   if (!named || pcum->nregs + nregs > NUM_ARG_REGS)
2541     return NULL_RTX;
2542
2543   return gen_rtx_REG (mode, pcum->nregs);
2544 }
2545
2546 static int
2547 arm_arg_partial_bytes (CUMULATIVE_ARGS *pcum, enum machine_mode mode,
2548                        tree type, bool named ATTRIBUTE_UNUSED)
2549 {
2550   int nregs = pcum->nregs;
2551
2552   if (arm_vector_mode_supported_p (mode))
2553     return 0;
2554
2555   if (NUM_ARG_REGS > nregs
2556       && (NUM_ARG_REGS < nregs + ARM_NUM_REGS2 (mode, type))
2557       && pcum->can_split)
2558     return (NUM_ARG_REGS - nregs) * UNITS_PER_WORD;
2559
2560   return 0;
2561 }
2562
2563 /* Variable sized types are passed by reference.  This is a GCC
2564    extension to the ARM ABI.  */
2565
2566 static bool
2567 arm_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
2568                        enum machine_mode mode ATTRIBUTE_UNUSED,
2569                        tree type, bool named ATTRIBUTE_UNUSED)
2570 {
2571   return type && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST;
2572 }
2573 \f
2574 /* Encode the current state of the #pragma [no_]long_calls.  */
2575 typedef enum
2576 {
2577   OFF,          /* No #pramgma [no_]long_calls is in effect.  */
2578   LONG,         /* #pragma long_calls is in effect.  */
2579   SHORT         /* #pragma no_long_calls is in effect.  */
2580 } arm_pragma_enum;
2581
2582 static arm_pragma_enum arm_pragma_long_calls = OFF;
2583
2584 void
2585 arm_pr_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2586 {
2587   arm_pragma_long_calls = LONG;
2588 }
2589
2590 void
2591 arm_pr_no_long_calls (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2592 {
2593   arm_pragma_long_calls = SHORT;
2594 }
2595
2596 void
2597 arm_pr_long_calls_off (struct cpp_reader * pfile ATTRIBUTE_UNUSED)
2598 {
2599   arm_pragma_long_calls = OFF;
2600 }
2601 \f
2602 /* Table of machine attributes.  */
2603 const struct attribute_spec arm_attribute_table[] =
2604 {
2605   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2606   /* Function calls made to this symbol must be done indirectly, because
2607      it may lie outside of the 26 bit addressing range of a normal function
2608      call.  */
2609   { "long_call",    0, 0, false, true,  true,  NULL },
2610   /* Whereas these functions are always known to reside within the 26 bit
2611      addressing range.  */
2612   { "short_call",   0, 0, false, true,  true,  NULL },
2613   /* Interrupt Service Routines have special prologue and epilogue requirements.  */
2614   { "isr",          0, 1, false, false, false, arm_handle_isr_attribute },
2615   { "interrupt",    0, 1, false, false, false, arm_handle_isr_attribute },
2616   { "naked",        0, 0, true,  false, false, arm_handle_fndecl_attribute },
2617 #ifdef ARM_PE
2618   /* ARM/PE has three new attributes:
2619      interfacearm - ?
2620      dllexport - for exporting a function/variable that will live in a dll
2621      dllimport - for importing a function/variable from a dll
2622
2623      Microsoft allows multiple declspecs in one __declspec, separating
2624      them with spaces.  We do NOT support this.  Instead, use __declspec
2625      multiple times.
2626   */
2627   { "dllimport",    0, 0, true,  false, false, NULL },
2628   { "dllexport",    0, 0, true,  false, false, NULL },
2629   { "interfacearm", 0, 0, true,  false, false, arm_handle_fndecl_attribute },
2630 #elif TARGET_DLLIMPORT_DECL_ATTRIBUTES
2631   { "dllimport",    0, 0, false, false, false, handle_dll_attribute },
2632   { "dllexport",    0, 0, false, false, false, handle_dll_attribute },
2633   { "notshared",    0, 0, false, true, false, arm_handle_notshared_attribute },
2634 #endif
2635   { NULL,           0, 0, false, false, false, NULL }
2636 };
2637
2638 /* Handle an attribute requiring a FUNCTION_DECL;
2639    arguments as in struct attribute_spec.handler.  */
2640 static tree
2641 arm_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
2642                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
2643 {
2644   if (TREE_CODE (*node) != FUNCTION_DECL)
2645     {
2646       warning ("%qs attribute only applies to functions",
2647                IDENTIFIER_POINTER (name));
2648       *no_add_attrs = true;
2649     }
2650
2651   return NULL_TREE;
2652 }
2653
2654 /* Handle an "interrupt" or "isr" attribute;
2655    arguments as in struct attribute_spec.handler.  */
2656 static tree
2657 arm_handle_isr_attribute (tree *node, tree name, tree args, int flags,
2658                           bool *no_add_attrs)
2659 {
2660   if (DECL_P (*node))
2661     {
2662       if (TREE_CODE (*node) != FUNCTION_DECL)
2663         {
2664           warning ("%qs attribute only applies to functions",
2665                    IDENTIFIER_POINTER (name));
2666           *no_add_attrs = true;
2667         }
2668       /* FIXME: the argument if any is checked for type attributes;
2669          should it be checked for decl ones?  */
2670     }
2671   else
2672     {
2673       if (TREE_CODE (*node) == FUNCTION_TYPE
2674           || TREE_CODE (*node) == METHOD_TYPE)
2675         {
2676           if (arm_isr_value (args) == ARM_FT_UNKNOWN)
2677             {
2678               warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2679               *no_add_attrs = true;
2680             }
2681         }
2682       else if (TREE_CODE (*node) == POINTER_TYPE
2683                && (TREE_CODE (TREE_TYPE (*node)) == FUNCTION_TYPE
2684                    || TREE_CODE (TREE_TYPE (*node)) == METHOD_TYPE)
2685                && arm_isr_value (args) != ARM_FT_UNKNOWN)
2686         {
2687           *node = build_variant_type_copy (*node);
2688           TREE_TYPE (*node) = build_type_attribute_variant
2689             (TREE_TYPE (*node),
2690              tree_cons (name, args, TYPE_ATTRIBUTES (TREE_TYPE (*node))));
2691           *no_add_attrs = true;
2692         }
2693       else
2694         {
2695           /* Possibly pass this attribute on from the type to a decl.  */
2696           if (flags & ((int) ATTR_FLAG_DECL_NEXT
2697                        | (int) ATTR_FLAG_FUNCTION_NEXT
2698                        | (int) ATTR_FLAG_ARRAY_NEXT))
2699             {
2700               *no_add_attrs = true;
2701               return tree_cons (name, args, NULL_TREE);
2702             }
2703           else
2704             {
2705               warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
2706             }
2707         }
2708     }
2709
2710   return NULL_TREE;
2711 }
2712
2713 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2714 /* Handle the "notshared" attribute.  This attribute is another way of
2715    requesting hidden visibility.  ARM's compiler supports
2716    "__declspec(notshared)"; we support the same thing via an
2717    attribute.  */
2718
2719 static tree
2720 arm_handle_notshared_attribute (tree *node, 
2721                                 tree name ATTRIBUTE_UNUSED, 
2722                                 tree args ATTRIBUTE_UNUSED, 
2723                                 int flags ATTRIBUTE_UNUSED, 
2724                                 bool *no_add_attrs)
2725 {
2726   tree decl = TYPE_NAME (*node);
2727
2728   if (decl)
2729     {
2730       DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2731       DECL_VISIBILITY_SPECIFIED (decl) = 1;
2732       *no_add_attrs = false;
2733     }
2734   return NULL_TREE;
2735 }
2736 #endif
2737
2738 /* Return 0 if the attributes for two types are incompatible, 1 if they
2739    are compatible, and 2 if they are nearly compatible (which causes a
2740    warning to be generated).  */
2741 static int
2742 arm_comp_type_attributes (tree type1, tree type2)
2743 {
2744   int l1, l2, s1, s2;
2745
2746   /* Check for mismatch of non-default calling convention.  */
2747   if (TREE_CODE (type1) != FUNCTION_TYPE)
2748     return 1;
2749
2750   /* Check for mismatched call attributes.  */
2751   l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
2752   l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
2753   s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
2754   s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
2755
2756   /* Only bother to check if an attribute is defined.  */
2757   if (l1 | l2 | s1 | s2)
2758     {
2759       /* If one type has an attribute, the other must have the same attribute.  */
2760       if ((l1 != l2) || (s1 != s2))
2761         return 0;
2762
2763       /* Disallow mixed attributes.  */
2764       if ((l1 & s2) || (l2 & s1))
2765         return 0;
2766     }
2767
2768   /* Check for mismatched ISR attribute.  */
2769   l1 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type1)) != NULL;
2770   if (! l1)
2771     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type1)) != NULL;
2772   l2 = lookup_attribute ("isr", TYPE_ATTRIBUTES (type2)) != NULL;
2773   if (! l2)
2774     l1 = lookup_attribute ("interrupt", TYPE_ATTRIBUTES (type2)) != NULL;
2775   if (l1 != l2)
2776     return 0;
2777
2778   return 1;
2779 }
2780
2781 /*  Encode long_call or short_call attribute by prefixing
2782     symbol name in DECL with a special character FLAG.  */
2783 void
2784 arm_encode_call_attribute (tree decl, int flag)
2785 {
2786   const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2787   int          len = strlen (str);
2788   char *       newstr;
2789
2790   /* Do not allow weak functions to be treated as short call.  */
2791   if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
2792     return;
2793
2794   newstr = alloca (len + 2);
2795   newstr[0] = flag;
2796   strcpy (newstr + 1, str);
2797
2798   newstr = (char *) ggc_alloc_string (newstr, len + 1);
2799   XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
2800 }
2801
2802 /*  Assigns default attributes to newly defined type.  This is used to
2803     set short_call/long_call attributes for function types of
2804     functions defined inside corresponding #pragma scopes.  */
2805 static void
2806 arm_set_default_type_attributes (tree type)
2807 {
2808   /* Add __attribute__ ((long_call)) to all functions, when
2809      inside #pragma long_calls or __attribute__ ((short_call)),
2810      when inside #pragma no_long_calls.  */
2811   if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
2812     {
2813       tree type_attr_list, attr_name;
2814       type_attr_list = TYPE_ATTRIBUTES (type);
2815
2816       if (arm_pragma_long_calls == LONG)
2817         attr_name = get_identifier ("long_call");
2818       else if (arm_pragma_long_calls == SHORT)
2819         attr_name = get_identifier ("short_call");
2820       else
2821         return;
2822
2823       type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
2824       TYPE_ATTRIBUTES (type) = type_attr_list;
2825     }
2826 }
2827 \f
2828 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
2829    defined within the current compilation unit.  If this cannot be
2830    determined, then 0 is returned.  */
2831 static int
2832 current_file_function_operand (rtx sym_ref)
2833 {
2834   /* This is a bit of a fib.  A function will have a short call flag
2835      applied to its name if it has the short call attribute, or it has
2836      already been defined within the current compilation unit.  */
2837   if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
2838     return 1;
2839
2840   /* The current function is always defined within the current compilation
2841      unit.  If it s a weak definition however, then this may not be the real
2842      definition of the function, and so we have to say no.  */
2843   if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
2844       && !DECL_WEAK (current_function_decl))
2845     return 1;
2846
2847   /* We cannot make the determination - default to returning 0.  */
2848   return 0;
2849 }
2850
2851 /* Return nonzero if a 32 bit "long_call" should be generated for
2852    this call.  We generate a long_call if the function:
2853
2854         a.  has an __attribute__((long call))
2855      or b.  is within the scope of a #pragma long_calls
2856      or c.  the -mlong-calls command line switch has been specified
2857          .  and either:
2858                 1. -ffunction-sections is in effect
2859              or 2. the current function has __attribute__ ((section))
2860              or 3. the target function has __attribute__ ((section))
2861
2862    However we do not generate a long call if the function:
2863
2864         d.  has an __attribute__ ((short_call))
2865      or e.  is inside the scope of a #pragma no_long_calls
2866      or f.  is defined within the current compilation unit.
2867
2868    This function will be called by C fragments contained in the machine
2869    description file.  SYM_REF and CALL_COOKIE correspond to the matched
2870    rtl operands.  CALL_SYMBOL is used to distinguish between
2871    two different callers of the function.  It is set to 1 in the
2872    "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
2873    and "call_value" patterns.  This is because of the difference in the
2874    SYM_REFs passed by these patterns.  */
2875 int
2876 arm_is_longcall_p (rtx sym_ref, int call_cookie, int call_symbol)
2877 {
2878   if (!call_symbol)
2879     {
2880       if (GET_CODE (sym_ref) != MEM)
2881         return 0;
2882
2883       sym_ref = XEXP (sym_ref, 0);
2884     }
2885
2886   if (GET_CODE (sym_ref) != SYMBOL_REF)
2887     return 0;
2888
2889   if (call_cookie & CALL_SHORT)
2890     return 0;
2891
2892   if (TARGET_LONG_CALLS)
2893     {
2894       if (flag_function_sections
2895           || DECL_SECTION_NAME (current_function_decl))
2896         /* c.3 is handled by the definition of the
2897            ARM_DECLARE_FUNCTION_SIZE macro.  */
2898         return 1;
2899     }
2900
2901   if (current_file_function_operand (sym_ref))
2902     return 0;
2903
2904   return (call_cookie & CALL_LONG)
2905     || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
2906     || TARGET_LONG_CALLS;
2907 }
2908
2909 /* Return nonzero if it is ok to make a tail-call to DECL.  */
2910 static bool
2911 arm_function_ok_for_sibcall (tree decl, tree exp ATTRIBUTE_UNUSED)
2912 {
2913   int call_type = TARGET_LONG_CALLS ? CALL_LONG : CALL_NORMAL;
2914
2915   if (cfun->machine->sibcall_blocked)
2916     return false;
2917
2918   /* Never tailcall something for which we have no decl, or if we
2919      are in Thumb mode.  */
2920   if (decl == NULL || TARGET_THUMB)
2921     return false;
2922
2923   /* Get the calling method.  */
2924   if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2925     call_type = CALL_SHORT;
2926   else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (TREE_TYPE (decl))))
2927     call_type = CALL_LONG;
2928
2929   /* Cannot tail-call to long calls, since these are out of range of
2930      a branch instruction.  However, if not compiling PIC, we know
2931      we can reach the symbol if it is in this compilation unit.  */
2932   if (call_type == CALL_LONG && (flag_pic || !TREE_ASM_WRITTEN (decl)))
2933     return false;
2934
2935   /* If we are interworking and the function is not declared static
2936      then we can't tail-call it unless we know that it exists in this
2937      compilation unit (since it might be a Thumb routine).  */
2938   if (TARGET_INTERWORK && TREE_PUBLIC (decl) && !TREE_ASM_WRITTEN (decl))
2939     return false;
2940
2941   /* Never tailcall from an ISR routine - it needs a special exit sequence.  */
2942   if (IS_INTERRUPT (arm_current_func_type ()))
2943     return false;
2944
2945   /* Everything else is ok.  */
2946   return true;
2947 }
2948
2949 \f
2950 /* Addressing mode support functions.  */
2951
2952 /* Return nonzero if X is a legitimate immediate operand when compiling
2953    for PIC.  */
2954 int
2955 legitimate_pic_operand_p (rtx x)
2956 {
2957   if (CONSTANT_P (x)
2958       && flag_pic
2959       && (GET_CODE (x) == SYMBOL_REF
2960           || (GET_CODE (x) == CONST
2961               && GET_CODE (XEXP (x, 0)) == PLUS
2962               && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
2963     return 0;
2964
2965   return 1;
2966 }
2967
2968 rtx
2969 legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
2970 {
2971   if (GET_CODE (orig) == SYMBOL_REF
2972       || GET_CODE (orig) == LABEL_REF)
2973     {
2974 #ifndef AOF_ASSEMBLER
2975       rtx pic_ref, address;
2976 #endif
2977       rtx insn;
2978       int subregs = 0;
2979
2980       if (reg == 0)
2981         {
2982           if (no_new_pseudos)
2983             abort ();
2984           else
2985             reg = gen_reg_rtx (Pmode);
2986
2987           subregs = 1;
2988         }
2989
2990 #ifdef AOF_ASSEMBLER
2991       /* The AOF assembler can generate relocations for these directly, and
2992          understands that the PIC register has to be added into the offset.  */
2993       insn = emit_insn (gen_pic_load_addr_based (reg, orig));
2994 #else
2995       if (subregs)
2996         address = gen_reg_rtx (Pmode);
2997       else
2998         address = reg;
2999
3000       if (TARGET_ARM)
3001         emit_insn (gen_pic_load_addr_arm (address, orig));
3002       else
3003         emit_insn (gen_pic_load_addr_thumb (address, orig));
3004
3005       if ((GET_CODE (orig) == LABEL_REF
3006            || (GET_CODE (orig) == SYMBOL_REF &&
3007                SYMBOL_REF_LOCAL_P (orig)))
3008           && NEED_GOT_RELOC)
3009         pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
3010       else
3011         {
3012           pic_ref = gen_const_mem (Pmode,
3013                                    gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
3014                                                  address));
3015         }
3016
3017       insn = emit_move_insn (reg, pic_ref);
3018 #endif
3019       current_function_uses_pic_offset_table = 1;
3020       /* Put a REG_EQUAL note on this insn, so that it can be optimized
3021          by loop.  */
3022       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
3023                                             REG_NOTES (insn));
3024       return reg;
3025     }
3026   else if (GET_CODE (orig) == CONST)
3027     {
3028       rtx base, offset;
3029
3030       if (GET_CODE (XEXP (orig, 0)) == PLUS
3031           && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
3032         return orig;
3033
3034       if (reg == 0)
3035         {
3036           if (no_new_pseudos)
3037             abort ();
3038           else
3039             reg = gen_reg_rtx (Pmode);
3040         }
3041
3042       if (GET_CODE (XEXP (orig, 0)) == PLUS)
3043         {
3044           base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
3045           offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
3046                                            base == reg ? 0 : reg);
3047         }
3048       else
3049         abort ();
3050
3051       if (GET_CODE (offset) == CONST_INT)
3052         {
3053           /* The base register doesn't really matter, we only want to
3054              test the index for the appropriate mode.  */
3055           if (!arm_legitimate_index_p (mode, offset, SET, 0))
3056             {
3057               if (!no_new_pseudos)
3058                 offset = force_reg (Pmode, offset);
3059               else
3060                 abort ();
3061             }
3062
3063           if (GET_CODE (offset) == CONST_INT)
3064             return plus_constant (base, INTVAL (offset));
3065         }
3066
3067       if (GET_MODE_SIZE (mode) > 4
3068           && (GET_MODE_CLASS (mode) == MODE_INT
3069               || TARGET_SOFT_FLOAT))
3070         {
3071           emit_insn (gen_addsi3 (reg, base, offset));
3072           return reg;
3073         }
3074
3075       return gen_rtx_PLUS (Pmode, base, offset);
3076     }
3077
3078   return orig;
3079 }
3080
3081
3082 /* Find a spare low register.  */
3083
3084 static int
3085 thumb_find_work_register (int live_regs_mask)
3086 {
3087   int reg;
3088
3089   /* Use a spare arg register.  */
3090   if (!regs_ever_live[LAST_ARG_REGNUM])
3091     return LAST_ARG_REGNUM;
3092
3093   /* Look for a pushed register.  This is used before the frame pointer is
3094      setup, so r7 is a candidate.  */
3095   for (reg = LAST_LO_REGNUM; reg >=0; reg--)
3096     if (live_regs_mask & (1 << reg))
3097       return reg;
3098
3099   /* Something went wrong.  */
3100   abort ();
3101 }
3102
3103
3104 /* Generate code to load the PIC register.  In thumb mode SCRATCH is a
3105    low register.  */
3106
3107 void
3108 arm_load_pic_register (unsigned int scratch)
3109 {
3110 #ifndef AOF_ASSEMBLER
3111   rtx l1, pic_tmp, pic_tmp2, pic_rtx;
3112   rtx global_offset_table;
3113
3114   if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
3115     return;
3116
3117   if (!flag_pic)
3118     abort ();
3119
3120   l1 = gen_label_rtx ();
3121
3122   global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
3123   /* On the ARM the PC register contains 'dot + 8' at the time of the
3124      addition, on the Thumb it is 'dot + 4'.  */
3125   pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
3126   if (GOT_PCREL)
3127     pic_tmp2 = gen_rtx_CONST (VOIDmode,
3128                             gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
3129   else
3130     pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
3131
3132   pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
3133
3134   if (TARGET_ARM)
3135     {
3136       emit_insn (gen_pic_load_addr_arm (pic_offset_table_rtx, pic_rtx));
3137       emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
3138     }
3139   else
3140     {
3141       if (REGNO (pic_offset_table_rtx) > LAST_LO_REGNUM)
3142         {
3143           /* We will have pushed the pic register, so should always be
3144              able to find a work register.  */
3145           pic_tmp = gen_rtx_REG (SImode, scratch);
3146           emit_insn (gen_pic_load_addr_thumb (pic_tmp, pic_rtx));
3147           emit_insn (gen_movsi (pic_offset_table_rtx, pic_tmp));
3148         }
3149       else
3150         emit_insn (gen_pic_load_addr_thumb (pic_offset_table_rtx, pic_rtx));
3151       emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
3152     }
3153
3154   /* Need to emit this whether or not we obey regdecls,
3155      since setjmp/longjmp can cause life info to screw up.  */
3156   emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
3157 #endif /* AOF_ASSEMBLER */
3158 }
3159
3160
3161 /* Return nonzero if X is valid as an ARM state addressing register.  */
3162 static int
3163 arm_address_register_rtx_p (rtx x, int strict_p)
3164 {
3165   int regno;
3166
3167   if (GET_CODE (x) != REG)
3168     return 0;
3169
3170   regno = REGNO (x);
3171
3172   if (strict_p)
3173     return ARM_REGNO_OK_FOR_BASE_P (regno);
3174
3175   return (regno <= LAST_ARM_REGNUM
3176           || regno >= FIRST_PSEUDO_REGISTER
3177           || regno == FRAME_POINTER_REGNUM
3178           || regno == ARG_POINTER_REGNUM);
3179 }
3180
3181 /* Return nonzero if X is a valid ARM state address operand.  */
3182 int
3183 arm_legitimate_address_p (enum machine_mode mode, rtx x, RTX_CODE outer,
3184                           int strict_p)
3185 {
3186   bool use_ldrd;
3187   enum rtx_code code = GET_CODE (x);
3188
3189   if (arm_address_register_rtx_p (x, strict_p))
3190     return 1;
3191
3192   use_ldrd = (TARGET_LDRD
3193               && (mode == DImode
3194                   || (mode == DFmode && (TARGET_SOFT_FLOAT || TARGET_VFP))));
3195
3196   if (code == POST_INC || code == PRE_DEC
3197       || ((code == PRE_INC || code == POST_DEC)
3198           && (use_ldrd || GET_MODE_SIZE (mode) <= 4)))
3199     return arm_address_register_rtx_p (XEXP (x, 0), strict_p);
3200
3201   else if ((code == POST_MODIFY || code == PRE_MODIFY)
3202            && arm_address_register_rtx_p (XEXP (x, 0), strict_p)
3203            && GET_CODE (XEXP (x, 1)) == PLUS
3204            && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3205     {
3206       rtx addend = XEXP (XEXP (x, 1), 1);
3207
3208       /* Don't allow ldrd post increment by register because it's hard
3209          to fixup invalid register choices.  */
3210       if (use_ldrd
3211           && GET_CODE (x) == POST_MODIFY
3212           && GET_CODE (addend) == REG)
3213         return 0;
3214
3215       return ((use_ldrd || GET_MODE_SIZE (mode) <= 4)
3216               && arm_legitimate_index_p (mode, addend, outer, strict_p));
3217     }
3218
3219   /* After reload constants split into minipools will have addresses
3220      from a LABEL_REF.  */
3221   else if (reload_completed
3222            && (code == LABEL_REF
3223                || (code == CONST
3224                    && GET_CODE (XEXP (x, 0)) == PLUS
3225                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3226                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3227     return 1;
3228
3229   else if (mode == TImode)
3230     return 0;
3231
3232   else if (code == PLUS)
3233     {
3234       rtx xop0 = XEXP (x, 0);
3235       rtx xop1 = XEXP (x, 1);
3236
3237       return ((arm_address_register_rtx_p (xop0, strict_p)
3238                && arm_legitimate_index_p (mode, xop1, outer, strict_p))
3239               || (arm_address_register_rtx_p (xop1, strict_p)
3240                   && arm_legitimate_index_p (mode, xop0, outer, strict_p)));
3241     }
3242
3243 #if 0
3244   /* Reload currently can't handle MINUS, so disable this for now */
3245   else if (GET_CODE (x) == MINUS)
3246     {
3247       rtx xop0 = XEXP (x, 0);
3248       rtx xop1 = XEXP (x, 1);
3249
3250       return (arm_address_register_rtx_p (xop0, strict_p)
3251               && arm_legitimate_index_p (mode, xop1, outer, strict_p));
3252     }
3253 #endif
3254
3255   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3256            && code == SYMBOL_REF
3257            && CONSTANT_POOL_ADDRESS_P (x)
3258            && ! (flag_pic
3259                  && symbol_mentioned_p (get_pool_constant (x))))
3260     return 1;
3261
3262   return 0;
3263 }
3264
3265 /* Return nonzero if INDEX is valid for an address index operand in
3266    ARM state.  */
3267 static int
3268 arm_legitimate_index_p (enum machine_mode mode, rtx index, RTX_CODE outer,
3269                         int strict_p)
3270 {
3271   HOST_WIDE_INT range;
3272   enum rtx_code code = GET_CODE (index);
3273
3274   /* Standard coprocessor addressing modes.  */
3275   if (TARGET_HARD_FLOAT
3276       && (TARGET_FPA || TARGET_MAVERICK)
3277       && (GET_MODE_CLASS (mode) == MODE_FLOAT
3278           || (TARGET_MAVERICK && mode == DImode)))
3279     return (code == CONST_INT && INTVAL (index) < 1024
3280             && INTVAL (index) > -1024
3281             && (INTVAL (index) & 3) == 0);
3282
3283   if (TARGET_REALLY_IWMMXT && VALID_IWMMXT_REG_MODE (mode))
3284     return (code == CONST_INT
3285             && INTVAL (index) < 1024
3286             && INTVAL (index) > -1024
3287             && (INTVAL (index) & 3) == 0);
3288
3289   if (arm_address_register_rtx_p (index, strict_p)
3290       && (GET_MODE_SIZE (mode) <= 4))
3291     return 1;
3292
3293   if (mode == DImode || mode == DFmode)
3294     {
3295       if (code == CONST_INT)
3296         {
3297           HOST_WIDE_INT val = INTVAL (index);
3298
3299           if (TARGET_LDRD)
3300             return val > -256 && val < 256;
3301           else
3302             return val > -4096 && val < 4092;
3303         }
3304
3305       return TARGET_LDRD && arm_address_register_rtx_p (index, strict_p);
3306     }
3307
3308   if (GET_MODE_SIZE (mode) <= 4
3309       && ! (arm_arch4
3310             && (mode == HImode
3311                 || (mode == QImode && outer == SIGN_EXTEND))))
3312     {
3313       if (code == MULT)
3314         {
3315           rtx xiop0 = XEXP (index, 0);
3316           rtx xiop1 = XEXP (index, 1);
3317
3318           return ((arm_address_register_rtx_p (xiop0, strict_p)
3319                    && power_of_two_operand (xiop1, SImode))
3320                   || (arm_address_register_rtx_p (xiop1, strict_p)
3321                       && power_of_two_operand (xiop0, SImode)));
3322         }
3323       else if (code == LSHIFTRT || code == ASHIFTRT
3324                || code == ASHIFT || code == ROTATERT)
3325         {
3326           rtx op = XEXP (index, 1);
3327
3328           return (arm_address_register_rtx_p (XEXP (index, 0), strict_p)
3329                   && GET_CODE (op) == CONST_INT
3330                   && INTVAL (op) > 0
3331                   && INTVAL (op) <= 31);
3332         }
3333     }
3334
3335   /* For ARM v4 we may be doing a sign-extend operation during the
3336      load.  */
3337   if (arm_arch4)
3338     {
3339       if (mode == HImode || (outer == SIGN_EXTEND && mode == QImode))
3340         range = 256;
3341       else
3342         range = 4096;
3343     }
3344   else
3345     range = (mode == HImode) ? 4095 : 4096;
3346
3347   return (code == CONST_INT
3348           && INTVAL (index) < range
3349           && INTVAL (index) > -range);
3350 }
3351
3352 /* Return nonzero if X is valid as a Thumb state base register.  */
3353 static int
3354 thumb_base_register_rtx_p (rtx x, enum machine_mode mode, int strict_p)
3355 {
3356   int regno;
3357
3358   if (GET_CODE (x) != REG)
3359     return 0;
3360
3361   regno = REGNO (x);
3362
3363   if (strict_p)
3364     return THUMB_REGNO_MODE_OK_FOR_BASE_P (regno, mode);
3365
3366   return (regno <= LAST_LO_REGNUM
3367           || regno > LAST_VIRTUAL_REGISTER
3368           || regno == FRAME_POINTER_REGNUM
3369           || (GET_MODE_SIZE (mode) >= 4
3370               && (regno == STACK_POINTER_REGNUM
3371                   || regno >= FIRST_PSEUDO_REGISTER
3372                   || x == hard_frame_pointer_rtx
3373                   || x == arg_pointer_rtx)));
3374 }
3375
3376 /* Return nonzero if x is a legitimate index register.  This is the case
3377    for any base register that can access a QImode object.  */
3378 inline static int
3379 thumb_index_register_rtx_p (rtx x, int strict_p)
3380 {
3381   return thumb_base_register_rtx_p (x, QImode, strict_p);
3382 }
3383
3384 /* Return nonzero if x is a legitimate Thumb-state address.
3385
3386    The AP may be eliminated to either the SP or the FP, so we use the
3387    least common denominator, e.g. SImode, and offsets from 0 to 64.
3388
3389    ??? Verify whether the above is the right approach.
3390
3391    ??? Also, the FP may be eliminated to the SP, so perhaps that
3392    needs special handling also.
3393
3394    ??? Look at how the mips16 port solves this problem.  It probably uses
3395    better ways to solve some of these problems.
3396
3397    Although it is not incorrect, we don't accept QImode and HImode
3398    addresses based on the frame pointer or arg pointer until the
3399    reload pass starts.  This is so that eliminating such addresses
3400    into stack based ones won't produce impossible code.  */
3401 int
3402 thumb_legitimate_address_p (enum machine_mode mode, rtx x, int strict_p)
3403 {
3404   /* ??? Not clear if this is right.  Experiment.  */
3405   if (GET_MODE_SIZE (mode) < 4
3406       && !(reload_in_progress || reload_completed)
3407       && (reg_mentioned_p (frame_pointer_rtx, x)
3408           || reg_mentioned_p (arg_pointer_rtx, x)
3409           || reg_mentioned_p (virtual_incoming_args_rtx, x)
3410           || reg_mentioned_p (virtual_outgoing_args_rtx, x)
3411           || reg_mentioned_p (virtual_stack_dynamic_rtx, x)
3412           || reg_mentioned_p (virtual_stack_vars_rtx, x)))
3413     return 0;
3414
3415   /* Accept any base register.  SP only in SImode or larger.  */
3416   else if (thumb_base_register_rtx_p (x, mode, strict_p))
3417     return 1;
3418
3419   /* This is PC relative data before arm_reorg runs.  */
3420   else if (GET_MODE_SIZE (mode) >= 4 && CONSTANT_P (x)
3421            && GET_CODE (x) == SYMBOL_REF
3422            && CONSTANT_POOL_ADDRESS_P (x) && ! flag_pic)
3423     return 1;
3424
3425   /* This is PC relative data after arm_reorg runs.  */
3426   else if (GET_MODE_SIZE (mode) >= 4 && reload_completed
3427            && (GET_CODE (x) == LABEL_REF
3428                || (GET_CODE (x) == CONST
3429                    && GET_CODE (XEXP (x, 0)) == PLUS
3430                    && GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF
3431                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
3432     return 1;
3433
3434   /* Post-inc indexing only supported for SImode and larger.  */
3435   else if (GET_CODE (x) == POST_INC && GET_MODE_SIZE (mode) >= 4
3436            && thumb_index_register_rtx_p (XEXP (x, 0), strict_p))
3437     return 1;
3438
3439   else if (GET_CODE (x) == PLUS)
3440     {
3441       /* REG+REG address can be any two index registers.  */
3442       /* We disallow FRAME+REG addressing since we know that FRAME
3443          will be replaced with STACK, and SP relative addressing only
3444          permits SP+OFFSET.  */
3445       if (GET_MODE_SIZE (mode) <= 4
3446           && XEXP (x, 0) != frame_pointer_rtx
3447           && XEXP (x, 1) != frame_pointer_rtx
3448           && thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3449           && thumb_index_register_rtx_p (XEXP (x, 1), strict_p))
3450         return 1;
3451
3452       /* REG+const has 5-7 bit offset for non-SP registers.  */
3453       else if ((thumb_index_register_rtx_p (XEXP (x, 0), strict_p)
3454                 || XEXP (x, 0) == arg_pointer_rtx)
3455                && GET_CODE (XEXP (x, 1)) == CONST_INT
3456                && thumb_legitimate_offset_p (mode, INTVAL (XEXP (x, 1))))
3457         return 1;
3458
3459       /* REG+const has 10 bit offset for SP, but only SImode and
3460          larger is supported.  */
3461       /* ??? Should probably check for DI/DFmode overflow here
3462          just like GO_IF_LEGITIMATE_OFFSET does.  */
3463       else if (GET_CODE (XEXP (x, 0)) == REG
3464                && REGNO (XEXP (x, 0)) == STACK_POINTER_REGNUM
3465                && GET_MODE_SIZE (mode) >= 4
3466                && GET_CODE (XEXP (x, 1)) == CONST_INT
3467                && INTVAL (XEXP (x, 1)) >= 0
3468                && INTVAL (XEXP (x, 1)) + GET_MODE_SIZE (mode) <= 1024
3469                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3470         return 1;
3471
3472       else if (GET_CODE (XEXP (x, 0)) == REG
3473                && REGNO (XEXP (x, 0)) == FRAME_POINTER_REGNUM
3474                && GET_MODE_SIZE (mode) >= 4
3475                && GET_CODE (XEXP (x, 1)) == CONST_INT
3476                && (INTVAL (XEXP (x, 1)) & 3) == 0)
3477         return 1;
3478     }
3479
3480   else if (GET_MODE_CLASS (mode) != MODE_FLOAT
3481            && GET_MODE_SIZE (mode) == 4
3482            && GET_CODE (x) == SYMBOL_REF
3483            && CONSTANT_POOL_ADDRESS_P (x)
3484            && !(flag_pic
3485                 && symbol_mentioned_p (get_pool_constant (x))))
3486     return 1;
3487
3488   return 0;
3489 }
3490
3491 /* Return nonzero if VAL can be used as an offset in a Thumb-state address
3492    instruction of mode MODE.  */
3493 int
3494 thumb_legitimate_offset_p (enum machine_mode mode, HOST_WIDE_INT val)
3495 {
3496   switch (GET_MODE_SIZE (mode))
3497     {
3498     case 1:
3499       return val >= 0 && val < 32;
3500
3501     case 2:
3502       return val >= 0 && val < 64 && (val & 1) == 0;
3503
3504     default:
3505       return (val >= 0
3506               && (val + GET_MODE_SIZE (mode)) <= 128
3507               && (val & 3) == 0);
3508     }
3509 }
3510
3511 /* Try machine-dependent ways of modifying an illegitimate address
3512    to be legitimate.  If we find one, return the new, valid address.  */
3513 rtx
3514 arm_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3515 {
3516   if (GET_CODE (x) == PLUS)
3517     {
3518       rtx xop0 = XEXP (x, 0);
3519       rtx xop1 = XEXP (x, 1);
3520
3521       if (CONSTANT_P (xop0) && !symbol_mentioned_p (xop0))
3522         xop0 = force_reg (SImode, xop0);
3523
3524       if (CONSTANT_P (xop1) && !symbol_mentioned_p (xop1))
3525         xop1 = force_reg (SImode, xop1);
3526
3527       if (ARM_BASE_REGISTER_RTX_P (xop0)
3528           && GET_CODE (xop1) == CONST_INT)
3529         {
3530           HOST_WIDE_INT n, low_n;
3531           rtx base_reg, val;
3532           n = INTVAL (xop1);
3533
3534           /* VFP addressing modes actually allow greater offsets, but for
3535              now we just stick with the lowest common denominator.  */
3536           if (mode == DImode
3537               || ((TARGET_SOFT_FLOAT || TARGET_VFP) && mode == DFmode))
3538             {
3539               low_n = n & 0x0f;
3540               n &= ~0x0f;
3541               if (low_n > 4)
3542                 {
3543                   n += 16;
3544                   low_n -= 16;
3545                 }
3546             }
3547           else
3548             {
3549               low_n = ((mode) == TImode ? 0
3550                        : n >= 0 ? (n & 0xfff) : -((-n) & 0xfff));
3551               n -= low_n;
3552             }
3553
3554           base_reg = gen_reg_rtx (SImode);
3555           val = force_operand (gen_rtx_PLUS (SImode, xop0,
3556                                              GEN_INT (n)), NULL_RTX);
3557           emit_move_insn (base_reg, val);
3558           x = (low_n == 0 ? base_reg
3559                : gen_rtx_PLUS (SImode, base_reg, GEN_INT (low_n)));
3560         }
3561       else if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3562         x = gen_rtx_PLUS (SImode, xop0, xop1);
3563     }
3564
3565   /* XXX We don't allow MINUS any more -- see comment in
3566      arm_legitimate_address_p ().  */
3567   else if (GET_CODE (x) == MINUS)
3568     {
3569       rtx xop0 = XEXP (x, 0);
3570       rtx xop1 = XEXP (x, 1);
3571
3572       if (CONSTANT_P (xop0))
3573         xop0 = force_reg (SImode, xop0);
3574
3575       if (CONSTANT_P (xop1) && ! symbol_mentioned_p (xop1))
3576         xop1 = force_reg (SImode, xop1);
3577
3578       if (xop0 != XEXP (x, 0) || xop1 != XEXP (x, 1))
3579         x = gen_rtx_MINUS (SImode, xop0, xop1);
3580     }
3581
3582   if (flag_pic)
3583     {
3584       /* We need to find and carefully transform any SYMBOL and LABEL
3585          references; so go back to the original address expression.  */
3586       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3587
3588       if (new_x != orig_x)
3589         x = new_x;
3590     }
3591
3592   return x;
3593 }
3594
3595
3596 /* Try machine-dependent ways of modifying an illegitimate Thumb address
3597    to be legitimate.  If we find one, return the new, valid address.  */
3598 rtx
3599 thumb_legitimize_address (rtx x, rtx orig_x, enum machine_mode mode)
3600 {
3601   if (GET_CODE (x) == PLUS
3602       && GET_CODE (XEXP (x, 1)) == CONST_INT
3603       && (INTVAL (XEXP (x, 1)) >= 32 * GET_MODE_SIZE (mode)
3604           || INTVAL (XEXP (x, 1)) < 0))
3605     {
3606       rtx xop0 = XEXP (x, 0);
3607       rtx xop1 = XEXP (x, 1);
3608       HOST_WIDE_INT offset = INTVAL (xop1);
3609
3610       /* Try and fold the offset into a biasing of the base register and
3611          then offsetting that.  Don't do this when optimizing for space
3612          since it can cause too many CSEs.  */
3613       if (optimize_size && offset >= 0
3614           && offset < 256 + 31 * GET_MODE_SIZE (mode))
3615         {
3616           HOST_WIDE_INT delta;
3617
3618           if (offset >= 256)
3619             delta = offset - (256 - GET_MODE_SIZE (mode));
3620           else if (offset < 32 * GET_MODE_SIZE (mode) + 8)
3621             delta = 31 * GET_MODE_SIZE (mode);
3622           else
3623             delta = offset & (~31 * GET_MODE_SIZE (mode));
3624
3625           xop0 = force_operand (plus_constant (xop0, offset - delta),
3626                                 NULL_RTX);
3627           x = plus_constant (xop0, delta);
3628         }
3629       else if (offset < 0 && offset > -256)
3630         /* Small negative offsets are best done with a subtract before the
3631            dereference, forcing these into a register normally takes two
3632            instructions.  */
3633         x = force_operand (x, NULL_RTX);
3634       else
3635         {
3636           /* For the remaining cases, force the constant into a register.  */
3637           xop1 = force_reg (SImode, xop1);
3638           x = gen_rtx_PLUS (SImode, xop0, xop1);
3639         }
3640     }
3641   else if (GET_CODE (x) == PLUS
3642            && s_register_operand (XEXP (x, 1), SImode)
3643            && !s_register_operand (XEXP (x, 0), SImode))
3644     {
3645       rtx xop0 = force_operand (XEXP (x, 0), NULL_RTX);
3646
3647       x = gen_rtx_PLUS (SImode, xop0, XEXP (x, 1));
3648     }
3649
3650   if (flag_pic)
3651     {
3652       /* We need to find and carefully transform any SYMBOL and LABEL
3653          references; so go back to the original address expression.  */
3654       rtx new_x = legitimize_pic_address (orig_x, mode, NULL_RTX);
3655
3656       if (new_x != orig_x)
3657         x = new_x;
3658     }
3659
3660   return x;
3661 }
3662
3663 \f
3664
3665 #define REG_OR_SUBREG_REG(X)                                            \
3666   (GET_CODE (X) == REG                                                  \
3667    || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
3668
3669 #define REG_OR_SUBREG_RTX(X)                    \
3670    (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
3671
3672 #ifndef COSTS_N_INSNS
3673 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
3674 #endif
3675 static inline int
3676 thumb_rtx_costs (rtx x, enum rtx_code code, enum rtx_code outer)
3677 {
3678   enum machine_mode mode = GET_MODE (x);
3679
3680   switch (code)
3681     {
3682     case ASHIFT:
3683     case ASHIFTRT:
3684     case LSHIFTRT:
3685     case ROTATERT:
3686     case PLUS:
3687     case MINUS:
3688     case COMPARE:
3689     case NEG:
3690     case NOT:
3691       return COSTS_N_INSNS (1);
3692
3693     case MULT:
3694       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
3695         {
3696           int cycles = 0;
3697           unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
3698
3699           while (i)
3700             {
3701               i >>= 2;
3702               cycles++;
3703             }
3704           return COSTS_N_INSNS (2) + cycles;
3705         }
3706       return COSTS_N_INSNS (1) + 16;
3707
3708     case SET:
3709       return (COSTS_N_INSNS (1)
3710               + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
3711                      + GET_CODE (SET_DEST (x)) == MEM));
3712
3713     case CONST_INT:
3714       if (outer == SET)
3715         {
3716           if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
3717             return 0;
3718           if (thumb_shiftable_const (INTVAL (x)))
3719             return COSTS_N_INSNS (2);
3720           return COSTS_N_INSNS (3);
3721         }
3722       else if ((outer == PLUS || outer == COMPARE)
3723                && INTVAL (x) < 256 && INTVAL (x) > -256)
3724         return 0;
3725       else if (outer == AND
3726                && INTVAL (x) < 256 && INTVAL (x) >= -256)
3727         return COSTS_N_INSNS (1);
3728       else if (outer == ASHIFT || outer == ASHIFTRT
3729                || outer == LSHIFTRT)
3730         return 0;
3731       return COSTS_N_INSNS (2);
3732
3733     case CONST:
3734     case CONST_DOUBLE:
3735     case LABEL_REF:
3736     case SYMBOL_REF:
3737       return COSTS_N_INSNS (3);
3738
3739     case UDIV:
3740     case UMOD:
3741     case DIV:
3742     case MOD:
3743       return 100;
3744
3745     case TRUNCATE:
3746       return 99;
3747
3748     case AND:
3749     case XOR:
3750     case IOR:
3751       /* XXX guess.  */
3752       return 8;
3753
3754     case MEM:
3755       /* XXX another guess.  */
3756       /* Memory costs quite a lot for the first word, but subsequent words
3757          load at the equivalent of a single insn each.  */
3758       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3759               + ((GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
3760                  ? 4 : 0));
3761
3762     case IF_THEN_ELSE:
3763       /* XXX a guess.  */
3764       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3765         return 14;
3766       return 2;
3767
3768     case ZERO_EXTEND:
3769       /* XXX still guessing.  */
3770       switch (GET_MODE (XEXP (x, 0)))
3771         {
3772         case QImode:
3773           return (1 + (mode == DImode ? 4 : 0)
3774                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3775
3776         case HImode:
3777           return (4 + (mode == DImode ? 4 : 0)
3778                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3779
3780         case SImode:
3781           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3782
3783         default:
3784           return 99;
3785         }
3786
3787     default:
3788       return 99;
3789     }
3790 }
3791
3792
3793 /* Worker routine for arm_rtx_costs.  */
3794 static inline int
3795 arm_rtx_costs_1 (rtx x, enum rtx_code code, enum rtx_code outer)
3796 {
3797   enum machine_mode mode = GET_MODE (x);
3798   enum rtx_code subcode;
3799   int extra_cost;
3800
3801   switch (code)
3802     {
3803     case MEM:
3804       /* Memory costs quite a lot for the first word, but subsequent words
3805          load at the equivalent of a single insn each.  */
3806       return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
3807               + (GET_CODE (x) == SYMBOL_REF
3808                  && CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
3809
3810     case DIV:
3811     case MOD:
3812     case UDIV:
3813     case UMOD:
3814       return optimize_size ? COSTS_N_INSNS (2) : 100;
3815
3816     case ROTATE:
3817       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
3818         return 4;
3819       /* Fall through */
3820     case ROTATERT:
3821       if (mode != SImode)
3822         return 8;
3823       /* Fall through */
3824     case ASHIFT: case LSHIFTRT: case ASHIFTRT:
3825       if (mode == DImode)
3826         return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
3827                 + ((GET_CODE (XEXP (x, 0)) == REG
3828                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3829                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3830                    ? 0 : 8));
3831       return (1 + ((GET_CODE (XEXP (x, 0)) == REG
3832                     || (GET_CODE (XEXP (x, 0)) == SUBREG
3833                         && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
3834                    ? 0 : 4)
3835               + ((GET_CODE (XEXP (x, 1)) == REG
3836                   || (GET_CODE (XEXP (x, 1)) == SUBREG
3837                       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
3838                   || (GET_CODE (XEXP (x, 1)) == CONST_INT))
3839                  ? 0 : 4));
3840
3841     case MINUS:
3842       if (mode == DImode)
3843         return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
3844                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3845                     || (GET_CODE (XEXP (x, 0)) == CONST_INT
3846                        && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
3847                    ? 0 : 8));
3848
3849       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3850         return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3851                       || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3852                           && arm_const_double_rtx (XEXP (x, 1))))
3853                      ? 0 : 8)
3854                 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
3855                     || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
3856                         && arm_const_double_rtx (XEXP (x, 0))))
3857                    ? 0 : 8));
3858
3859       if (((GET_CODE (XEXP (x, 0)) == CONST_INT
3860             && const_ok_for_arm (INTVAL (XEXP (x, 0)))
3861             && REG_OR_SUBREG_REG (XEXP (x, 1))))
3862           || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
3863                || subcode == ASHIFTRT || subcode == LSHIFTRT
3864                || subcode == ROTATE || subcode == ROTATERT
3865                || (subcode == MULT
3866                    && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3867                    && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
3868                         (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
3869               && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
3870               && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
3871                   || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
3872               && REG_OR_SUBREG_REG (XEXP (x, 0))))
3873         return 1;
3874       /* Fall through */
3875
3876     case PLUS:
3877       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3878         return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3879                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3880                     || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
3881                         && arm_const_double_rtx (XEXP (x, 1))))
3882                    ? 0 : 8));
3883
3884       /* Fall through */
3885     case AND: case XOR: case IOR:
3886       extra_cost = 0;
3887
3888       /* Normally the frame registers will be spilt into reg+const during
3889          reload, so it is a bad idea to combine them with other instructions,
3890          since then they might not be moved outside of loops.  As a compromise
3891          we allow integration with ops that have a constant as their second
3892          operand.  */
3893       if ((REG_OR_SUBREG_REG (XEXP (x, 0))
3894            && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
3895            && GET_CODE (XEXP (x, 1)) != CONST_INT)
3896           || (REG_OR_SUBREG_REG (XEXP (x, 0))
3897               && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
3898         extra_cost = 4;
3899
3900       if (mode == DImode)
3901         return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
3902                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3903                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3904                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3905                    ? 0 : 8));
3906
3907       if (REG_OR_SUBREG_REG (XEXP (x, 0)))
3908         return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
3909                 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
3910                     || (GET_CODE (XEXP (x, 1)) == CONST_INT
3911                         && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
3912                    ? 0 : 4));
3913
3914       else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
3915         return (1 + extra_cost
3916                 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
3917                      || subcode == LSHIFTRT || subcode == ASHIFTRT
3918                      || subcode == ROTATE || subcode == ROTATERT
3919                      || (subcode == MULT
3920                          && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3921                          && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
3922                               (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
3923                     && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
3924                     && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
3925                         || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
3926                    ? 0 : 4));
3927
3928       return 8;
3929
3930     case MULT:
3931       /* This should have been handled by the CPU specific routines.  */
3932       abort ();
3933
3934     case TRUNCATE:
3935       if (arm_arch3m && mode == SImode
3936           && GET_CODE (XEXP (x, 0)) == LSHIFTRT
3937           && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
3938           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
3939               == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
3940           && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
3941               || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
3942         return 8;
3943       return 99;
3944
3945     case NEG:
3946       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
3947         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
3948       /* Fall through */
3949     case NOT:
3950       if (mode == DImode)
3951         return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3952
3953       return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
3954
3955     case IF_THEN_ELSE:
3956       if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
3957         return 14;
3958       return 2;
3959
3960     case COMPARE:
3961       return 1;
3962
3963     case ABS:
3964       return 4 + (mode == DImode ? 4 : 0);
3965
3966     case SIGN_EXTEND:
3967       if (GET_MODE (XEXP (x, 0)) == QImode)
3968         return (4 + (mode == DImode ? 4 : 0)
3969                 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3970       /* Fall through */
3971     case ZERO_EXTEND:
3972       switch (GET_MODE (XEXP (x, 0)))
3973         {
3974         case QImode:
3975           return (1 + (mode == DImode ? 4 : 0)
3976                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3977
3978         case HImode:
3979           return (4 + (mode == DImode ? 4 : 0)
3980                   + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3981
3982         case SImode:
3983           return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
3984
3985         case V8QImode:
3986         case V4HImode:
3987         case V2SImode:
3988         case V4QImode:
3989         case V2HImode:
3990             return 1;
3991
3992         default:
3993           break;
3994         }
3995       abort ();
3996
3997     case CONST_INT:
3998       if (const_ok_for_arm (INTVAL (x)))
3999         return outer == SET ? 2 : -1;
4000       else if (outer == AND
4001                && const_ok_for_arm (~INTVAL (x)))
4002         return -1;
4003       else if ((outer == COMPARE
4004                 || outer == PLUS || outer == MINUS)
4005                && const_ok_for_arm (-INTVAL (x)))
4006         return -1;
4007       else
4008         return 5;
4009
4010     case CONST:
4011     case LABEL_REF:
4012     case SYMBOL_REF:
4013       return 6;
4014
4015     case CONST_DOUBLE:
4016       if (arm_const_double_rtx (x))
4017         return outer == SET ? 2 : -1;
4018       else if ((outer == COMPARE || outer == PLUS)
4019                && neg_const_double_rtx_ok_for_fpa (x))
4020         return -1;
4021       return 7;
4022
4023     default:
4024       return 99;
4025     }
4026 }
4027
4028 /* RTX costs when optimizing for size.  */
4029 static bool
4030 arm_size_rtx_costs (rtx x, int code, int outer_code, int *total)
4031 {
4032   enum machine_mode mode = GET_MODE (x);
4033
4034   if (TARGET_THUMB)
4035     {
4036       /* XXX TBD.  For now, use the standard costs.  */
4037       *total = thumb_rtx_costs (x, code, outer_code);
4038       return true;
4039     }
4040
4041   switch (code)
4042     {
4043     case MEM:
4044       /* A memory access costs 1 insn if the mode is small, or the address is
4045          a single register, otherwise it costs one insn per word.  */
4046       if (REG_P (XEXP (x, 0)))
4047         *total = COSTS_N_INSNS (1);
4048       else
4049         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4050       return true;
4051
4052     case DIV:
4053     case MOD:
4054     case UDIV:
4055     case UMOD:
4056       /* Needs a libcall, so it costs about this.  */
4057       *total = COSTS_N_INSNS (2);
4058       return false;
4059
4060     case ROTATE:
4061       if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
4062         {
4063           *total = COSTS_N_INSNS (2) + rtx_cost (XEXP (x, 0), code);
4064           return true;
4065         }
4066       /* Fall through */
4067     case ROTATERT:
4068     case ASHIFT:
4069     case LSHIFTRT:
4070     case ASHIFTRT:
4071       if (mode == DImode && GET_CODE (XEXP (x, 1)) == CONST_INT)
4072         {
4073           *total = COSTS_N_INSNS (3) + rtx_cost (XEXP (x, 0), code);
4074           return true;
4075         }
4076       else if (mode == SImode)
4077         {
4078           *total = COSTS_N_INSNS (1) + rtx_cost (XEXP (x, 0), code);
4079           /* Slightly disparage register shifts, but not by much.  */
4080           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
4081             *total += 1 + rtx_cost (XEXP (x, 1), code);
4082           return true;
4083         }
4084
4085       /* Needs a libcall.  */
4086       *total = COSTS_N_INSNS (2);
4087       return false;
4088
4089     case MINUS:
4090       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4091         {
4092           *total = COSTS_N_INSNS (1);
4093           return false;
4094         }
4095
4096       if (mode == SImode)
4097         {
4098           enum rtx_code subcode0 = GET_CODE (XEXP (x, 0));
4099           enum rtx_code subcode1 = GET_CODE (XEXP (x, 1));
4100
4101           if (subcode0 == ROTATE || subcode0 == ROTATERT || subcode0 == ASHIFT
4102               || subcode0 == LSHIFTRT || subcode0 == ASHIFTRT
4103               || subcode1 == ROTATE || subcode1 == ROTATERT
4104               || subcode1 == ASHIFT || subcode1 == LSHIFTRT
4105               || subcode1 == ASHIFTRT)
4106             {
4107               /* It's just the cost of the two operands.  */
4108               *total = 0;
4109               return false;
4110             }
4111
4112           *total = COSTS_N_INSNS (1);
4113           return false;
4114         }
4115
4116       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4117       return false;
4118
4119     case PLUS:
4120       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4121         {
4122           *total = COSTS_N_INSNS (1);
4123           return false;
4124         }
4125
4126       /* Fall through */
4127     case AND: case XOR: case IOR:
4128       if (mode == SImode)
4129         {
4130           enum rtx_code subcode = GET_CODE (XEXP (x, 0));
4131
4132           if (subcode == ROTATE || subcode == ROTATERT || subcode == ASHIFT
4133               || subcode == LSHIFTRT || subcode == ASHIFTRT
4134               || (code == AND && subcode == NOT))
4135             {
4136               /* It's just the cost of the two operands.  */
4137               *total = 0;
4138               return false;
4139             }
4140         }
4141
4142       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4143       return false;
4144
4145     case MULT:
4146       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4147       return false;
4148
4149     case NEG:
4150       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4151         *total = COSTS_N_INSNS (1);
4152       /* Fall through */
4153     case NOT:
4154       *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4155
4156       return false;
4157
4158     case IF_THEN_ELSE:
4159       *total = 0;
4160       return false;
4161
4162     case COMPARE:
4163       if (cc_register (XEXP (x, 0), VOIDmode))
4164         * total = 0;
4165       else
4166         *total = COSTS_N_INSNS (1);
4167       return false;
4168
4169     case ABS:
4170       if (TARGET_HARD_FLOAT && GET_MODE_CLASS (mode) == MODE_FLOAT)
4171         *total = COSTS_N_INSNS (1);
4172       else
4173         *total = COSTS_N_INSNS (1 + ARM_NUM_REGS (mode));
4174       return false;
4175
4176     case SIGN_EXTEND:
4177       *total = 0;
4178       if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) < 4)
4179         {
4180           if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4181             *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4182         }
4183       if (mode == DImode)
4184         *total += COSTS_N_INSNS (1);
4185       return false;
4186
4187     case ZERO_EXTEND:
4188       *total = 0;
4189       if (!(arm_arch4 && MEM_P (XEXP (x, 0))))
4190         {
4191           switch (GET_MODE (XEXP (x, 0)))
4192             {
4193             case QImode:
4194               *total += COSTS_N_INSNS (1);
4195               break;
4196
4197             case HImode:
4198               *total += COSTS_N_INSNS (arm_arch6 ? 1 : 2);
4199
4200             case SImode:
4201               break;
4202
4203             default:
4204               *total += COSTS_N_INSNS (2);
4205             }
4206         }
4207
4208       if (mode == DImode)
4209         *total += COSTS_N_INSNS (1);
4210
4211       return false;
4212
4213     case CONST_INT:
4214       if (const_ok_for_arm (INTVAL (x)))
4215         *total = COSTS_N_INSNS (outer_code == SET ? 1 : 0);
4216       else if (const_ok_for_arm (~INTVAL (x)))
4217         *total = COSTS_N_INSNS (outer_code == AND ? 0 : 1);
4218       else if (const_ok_for_arm (-INTVAL (x)))
4219         {
4220           if (outer_code == COMPARE || outer_code == PLUS
4221               || outer_code == MINUS)
4222             *total = 0;
4223           else
4224             *total = COSTS_N_INSNS (1);
4225         }
4226       else
4227         *total = COSTS_N_INSNS (2);
4228       return true;
4229
4230     case CONST:
4231     case LABEL_REF:
4232     case SYMBOL_REF:
4233       *total = COSTS_N_INSNS (2);
4234       return true;
4235
4236     case CONST_DOUBLE:
4237       *total = COSTS_N_INSNS (4);
4238       return true;
4239
4240     default:
4241       if (mode != VOIDmode)
4242         *total = COSTS_N_INSNS (ARM_NUM_REGS (mode));
4243       else
4244         *total = COSTS_N_INSNS (4); /* How knows?  */
4245       return false;
4246     }
4247 }
4248
4249 /* RTX costs for cores with a slow MUL implementation.  */
4250
4251 static bool
4252 arm_slowmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4253 {
4254   enum machine_mode mode = GET_MODE (x);
4255
4256   if (TARGET_THUMB)
4257     {
4258       *total = thumb_rtx_costs (x, code, outer_code);
4259       return true;
4260     }
4261
4262   switch (code)
4263     {
4264     case MULT:
4265       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4266           || mode == DImode)
4267         {
4268           *total = 30;
4269           return true;
4270         }
4271
4272       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4273         {
4274           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4275                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4276           int cost, const_ok = const_ok_for_arm (i);
4277           int j, booth_unit_size;
4278
4279           /* Tune as appropriate.  */
4280           cost = const_ok ? 4 : 8;
4281           booth_unit_size = 2;
4282           for (j = 0; i && j < 32; j += booth_unit_size)
4283             {
4284               i >>= booth_unit_size;
4285               cost += 2;
4286             }
4287
4288           *total = cost;
4289           return true;
4290         }
4291
4292       *total = 30 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4293                   + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4294       return true;
4295
4296     default:
4297       *total = arm_rtx_costs_1 (x, code, outer_code);
4298       return true;
4299     }
4300 }
4301
4302
4303 /* RTX cost for cores with a fast multiply unit (M variants).  */
4304
4305 static bool
4306 arm_fastmul_rtx_costs (rtx x, int code, int outer_code, int *total)
4307 {
4308   enum machine_mode mode = GET_MODE (x);
4309
4310   if (TARGET_THUMB)
4311     {
4312       *total = thumb_rtx_costs (x, code, outer_code);
4313       return true;
4314     }
4315
4316   switch (code)
4317     {
4318     case MULT:
4319       /* There is no point basing this on the tuning, since it is always the
4320          fast variant if it exists at all.  */
4321       if (mode == DImode
4322           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4323           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4324               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4325         {
4326           *total = 8;
4327           return true;
4328         }
4329
4330
4331       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4332           || mode == DImode)
4333         {
4334           *total = 30;
4335           return true;
4336         }
4337
4338       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4339         {
4340           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4341                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4342           int cost, const_ok = const_ok_for_arm (i);
4343           int j, booth_unit_size;
4344
4345           /* Tune as appropriate.  */
4346           cost = const_ok ? 4 : 8;
4347           booth_unit_size = 8;
4348           for (j = 0; i && j < 32; j += booth_unit_size)
4349             {
4350               i >>= booth_unit_size;
4351               cost += 2;
4352             }
4353
4354           *total = cost;
4355           return true;
4356         }
4357
4358       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4359                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4360       return true;
4361
4362     default:
4363       *total = arm_rtx_costs_1 (x, code, outer_code);
4364       return true;
4365     }
4366 }
4367
4368
4369 /* RTX cost for XScale CPUs.  */
4370
4371 static bool
4372 arm_xscale_rtx_costs (rtx x, int code, int outer_code, int *total)
4373 {
4374   enum machine_mode mode = GET_MODE (x);
4375
4376   if (TARGET_THUMB)
4377     {
4378       *total = thumb_rtx_costs (x, code, outer_code);
4379       return true;
4380     }
4381
4382   switch (code)
4383     {
4384     case MULT:
4385       /* There is no point basing this on the tuning, since it is always the
4386          fast variant if it exists at all.  */
4387       if (mode == DImode
4388           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4389           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4390               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4391         {
4392           *total = 8;
4393           return true;
4394         }
4395
4396
4397       if (GET_MODE_CLASS (mode) == MODE_FLOAT
4398           || mode == DImode)
4399         {
4400           *total = 30;
4401           return true;
4402         }
4403
4404       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4405         {
4406           unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
4407                                       & (unsigned HOST_WIDE_INT) 0xffffffff);
4408           int cost, const_ok = const_ok_for_arm (i);
4409           unsigned HOST_WIDE_INT masked_const;
4410
4411           /* The cost will be related to two insns.
4412              First a load of the constant (MOV or LDR), then a multiply.  */
4413           cost = 2;
4414           if (! const_ok)
4415             cost += 1;      /* LDR is probably more expensive because
4416                                of longer result latency.  */
4417           masked_const = i & 0xffff8000;
4418           if (masked_const != 0 && masked_const != 0xffff8000)
4419             {
4420               masked_const = i & 0xf8000000;
4421               if (masked_const == 0 || masked_const == 0xf8000000)
4422                 cost += 1;
4423               else
4424                 cost += 2;
4425             }
4426           *total = cost;
4427           return true;
4428         }
4429
4430       *total = 8 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
4431                  + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4);
4432       return true;
4433
4434     case COMPARE:
4435       /* A COMPARE of a MULT is slow on XScale; the muls instruction
4436          will stall until the multiplication is complete.  */
4437       if (GET_CODE (XEXP (x, 0)) == MULT)
4438         *total = 4 + rtx_cost (XEXP (x, 0), code);
4439       else
4440         *total = arm_rtx_costs_1 (x, code, outer_code);
4441       return true;
4442
4443     default:
4444       *total = arm_rtx_costs_1 (x, code, outer_code);
4445       return true;
4446     }
4447 }
4448
4449
4450 /* RTX costs for 9e (and later) cores.  */
4451
4452 static bool
4453 arm_9e_rtx_costs (rtx x, int code, int outer_code, int *total)
4454 {
4455   enum machine_mode mode = GET_MODE (x);
4456   int nonreg_cost;
4457   int cost;
4458
4459   if (TARGET_THUMB)
4460     {
4461       switch (code)
4462         {
4463         case MULT:
4464           *total = COSTS_N_INSNS (3);
4465           return true;
4466
4467         default:
4468           *total = thumb_rtx_costs (x, code, outer_code);
4469           return true;
4470         }
4471     }
4472
4473   switch (code)
4474     {
4475     case MULT:
4476       /* There is no point basing this on the tuning, since it is always the
4477          fast variant if it exists at all.  */
4478       if (mode == DImode
4479           && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
4480           && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
4481               || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
4482         {
4483           *total = 3;
4484           return true;
4485         }
4486
4487
4488       if (GET_MODE_CLASS (mode) == MODE_FLOAT)
4489         {
4490           *total = 30;
4491           return true;
4492         }
4493       if (mode == DImode)
4494         {
4495           cost = 7;
4496           nonreg_cost = 8;
4497         }
4498       else
4499         {
4500           cost = 2;
4501           nonreg_cost = 4;
4502         }
4503
4504
4505       *total = cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : nonreg_cost)
4506                     + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : nonreg_cost);
4507       return true;
4508
4509     default:
4510       *total = arm_rtx_costs_1 (x, code, outer_code);
4511       return true;
4512     }
4513 }
4514 /* All address computations that can be done are free, but rtx cost returns
4515    the same for practically all of them.  So we weight the different types
4516    of address here in the order (most pref first):
4517    PRE/POST_INC/DEC, SHIFT or NON-INT sum, INT sum, REG, MEM or LABEL.  */
4518 static inline int
4519 arm_arm_address_cost (rtx x)
4520 {
4521   enum rtx_code c  = GET_CODE (x);
4522
4523   if (c == PRE_INC || c == PRE_DEC || c == POST_INC || c == POST_DEC)
4524     return 0;
4525   if (c == MEM || c == LABEL_REF || c == SYMBOL_REF)
4526     return 10;
4527
4528   if (c == PLUS || c == MINUS)
4529     {
4530       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4531         return 2;
4532
4533       if (ARITHMETIC_P (XEXP (x, 0)) || ARITHMETIC_P (XEXP (x, 1)))
4534         return 3;
4535
4536       return 4;
4537     }
4538
4539   return 6;
4540 }
4541
4542 static inline int
4543 arm_thumb_address_cost (rtx x)
4544 {
4545   enum rtx_code c  = GET_CODE (x);
4546
4547   if (c == REG)
4548     return 1;
4549   if (c == PLUS
4550       && GET_CODE (XEXP (x, 0)) == REG
4551       && GET_CODE (XEXP (x, 1)) == CONST_INT)
4552     return 1;
4553
4554   return 2;
4555 }
4556
4557 static int
4558 arm_address_cost (rtx x)
4559 {
4560   return TARGET_ARM ? arm_arm_address_cost (x) : arm_thumb_address_cost (x);
4561 }
4562
4563 static int
4564 arm_adjust_cost (rtx insn, rtx link, rtx dep, int cost)
4565 {
4566   rtx i_pat, d_pat;
4567
4568   /* Some true dependencies can have a higher cost depending
4569      on precisely how certain input operands are used.  */
4570   if (arm_tune_xscale
4571       && REG_NOTE_KIND (link) == 0
4572       && recog_memoized (insn) >= 0
4573       && recog_memoized (dep) >= 0)
4574     {
4575       int shift_opnum = get_attr_shift (insn);
4576       enum attr_type attr_type = get_attr_type (dep);
4577
4578       /* If nonzero, SHIFT_OPNUM contains the operand number of a shifted
4579          operand for INSN.  If we have a shifted input operand and the
4580          instruction we depend on is another ALU instruction, then we may
4581          have to account for an additional stall.  */
4582       if (shift_opnum != 0
4583           && (attr_type == TYPE_ALU_SHIFT || attr_type == TYPE_ALU_SHIFT_REG))
4584         {
4585           rtx shifted_operand;
4586           int opno;
4587
4588           /* Get the shifted operand.  */
4589           extract_insn (insn);
4590           shifted_operand = recog_data.operand[shift_opnum];
4591
4592           /* Iterate over all the operands in DEP.  If we write an operand
4593              that overlaps with SHIFTED_OPERAND, then we have increase the
4594              cost of this dependency.  */
4595           extract_insn (dep);
4596           preprocess_constraints ();
4597           for (opno = 0; opno < recog_data.n_operands; opno++)
4598             {
4599               /* We can ignore strict inputs.  */
4600               if (recog_data.operand_type[opno] == OP_IN)
4601                 continue;
4602
4603               if (reg_overlap_mentioned_p (recog_data.operand[opno],
4604                                            shifted_operand))
4605                 return 2;
4606             }
4607         }
4608     }
4609
4610   /* XXX This is not strictly true for the FPA.  */
4611   if (REG_NOTE_KIND (link) == REG_DEP_ANTI
4612       || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
4613     return 0;
4614
4615   /* Call insns don't incur a stall, even if they follow a load.  */
4616   if (REG_NOTE_KIND (link) == 0
4617       && GET_CODE (insn) == CALL_INSN)
4618     return 1;
4619
4620   if ((i_pat = single_set (insn)) != NULL
4621       && GET_CODE (SET_SRC (i_pat)) == MEM
4622       && (d_pat = single_set (dep)) != NULL
4623       && GET_CODE (SET_DEST (d_pat)) == MEM)
4624     {
4625       rtx src_mem = XEXP (SET_SRC (i_pat), 0);
4626       /* This is a load after a store, there is no conflict if the load reads
4627          from a cached area.  Assume that loads from the stack, and from the
4628          constant pool are cached, and that others will miss.  This is a
4629          hack.  */
4630
4631       if ((GET_CODE (src_mem) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (src_mem))
4632           || reg_mentioned_p (stack_pointer_rtx, src_mem)
4633           || reg_mentioned_p (frame_pointer_rtx, src_mem)
4634           || reg_mentioned_p (hard_frame_pointer_rtx, src_mem))
4635         return 1;
4636     }
4637
4638   return cost;
4639 }
4640
4641 static int fp_consts_inited = 0;
4642
4643 /* Only zero is valid for VFP.  Other values are also valid for FPA.  */
4644 static const char * const strings_fp[8] =
4645 {
4646   "0",   "1",   "2",   "3",
4647   "4",   "5",   "0.5", "10"
4648 };
4649
4650 static REAL_VALUE_TYPE values_fp[8];
4651
4652 static void
4653 init_fp_table (void)
4654 {
4655   int i;
4656   REAL_VALUE_TYPE r;
4657
4658   if (TARGET_VFP)
4659     fp_consts_inited = 1;
4660   else
4661     fp_consts_inited = 8;
4662
4663   for (i = 0; i < fp_consts_inited; i++)
4664     {
4665       r = REAL_VALUE_ATOF (strings_fp[i], DFmode);
4666       values_fp[i] = r;
4667     }
4668 }
4669
4670 /* Return TRUE if rtx X is a valid immediate FP constant.  */
4671 int
4672 arm_const_double_rtx (rtx x)
4673 {
4674   REAL_VALUE_TYPE r;
4675   int i;
4676
4677   if (!fp_consts_inited)
4678     init_fp_table ();
4679
4680   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4681   if (REAL_VALUE_MINUS_ZERO (r))
4682     return 0;
4683
4684   for (i = 0; i < fp_consts_inited; i++)
4685     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4686       return 1;
4687
4688   return 0;
4689 }
4690
4691 /* Return TRUE if rtx X is a valid immediate FPA constant.  */
4692 int
4693 neg_const_double_rtx_ok_for_fpa (rtx x)
4694 {
4695   REAL_VALUE_TYPE r;
4696   int i;
4697
4698   if (!fp_consts_inited)
4699     init_fp_table ();
4700
4701   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4702   r = REAL_VALUE_NEGATE (r);
4703   if (REAL_VALUE_MINUS_ZERO (r))
4704     return 0;
4705
4706   for (i = 0; i < 8; i++)
4707     if (REAL_VALUES_EQUAL (r, values_fp[i]))
4708       return 1;
4709
4710   return 0;
4711 }
4712 \f
4713 /* Predicates for `match_operand' and `match_operator'.  */
4714
4715 /* Return nonzero if OP is a valid Cirrus memory address pattern.  */
4716 int
4717 cirrus_memory_offset (rtx op)
4718 {
4719   /* Reject eliminable registers.  */
4720   if (! (reload_in_progress || reload_completed)
4721       && (   reg_mentioned_p (frame_pointer_rtx, op)
4722           || reg_mentioned_p (arg_pointer_rtx, op)
4723           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4724           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4725           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4726           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4727     return 0;
4728
4729   if (GET_CODE (op) == MEM)
4730     {
4731       rtx ind;
4732
4733       ind = XEXP (op, 0);
4734
4735       /* Match: (mem (reg)).  */
4736       if (GET_CODE (ind) == REG)
4737         return 1;
4738
4739       /* Match:
4740          (mem (plus (reg)
4741                     (const))).  */
4742       if (GET_CODE (ind) == PLUS
4743           && GET_CODE (XEXP (ind, 0)) == REG
4744           && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4745           && GET_CODE (XEXP (ind, 1)) == CONST_INT)
4746         return 1;
4747     }
4748
4749   return 0;
4750 }
4751
4752 /* Return TRUE if OP is a valid VFP memory address pattern.
4753    WB if true if writeback address modes are allowed.  */
4754
4755 int
4756 arm_coproc_mem_operand (rtx op, bool wb)
4757 {
4758   rtx ind;
4759
4760   /* Reject eliminable registers.  */
4761   if (! (reload_in_progress || reload_completed)
4762       && (   reg_mentioned_p (frame_pointer_rtx, op)
4763           || reg_mentioned_p (arg_pointer_rtx, op)
4764           || reg_mentioned_p (virtual_incoming_args_rtx, op)
4765           || reg_mentioned_p (virtual_outgoing_args_rtx, op)
4766           || reg_mentioned_p (virtual_stack_dynamic_rtx, op)
4767           || reg_mentioned_p (virtual_stack_vars_rtx, op)))
4768     return FALSE;
4769
4770   /* Constants are converted into offsets from labels.  */
4771   if (GET_CODE (op) != MEM)
4772     return FALSE;
4773
4774   ind = XEXP (op, 0);
4775
4776   if (reload_completed
4777       && (GET_CODE (ind) == LABEL_REF
4778           || (GET_CODE (ind) == CONST
4779               && GET_CODE (XEXP (ind, 0)) == PLUS
4780               && GET_CODE (XEXP (XEXP (ind, 0), 0)) == LABEL_REF
4781               && GET_CODE (XEXP (XEXP (ind, 0), 1)) == CONST_INT)))
4782     return TRUE;
4783
4784   /* Match: (mem (reg)).  */
4785   if (GET_CODE (ind) == REG)
4786     return arm_address_register_rtx_p (ind, 0);
4787
4788   /* Autoincremment addressing modes.  */
4789   if (wb
4790       && (GET_CODE (ind) == PRE_INC
4791           || GET_CODE (ind) == POST_INC
4792           || GET_CODE (ind) == PRE_DEC
4793           || GET_CODE (ind) == POST_DEC))
4794     return arm_address_register_rtx_p (XEXP (ind, 0), 0);
4795
4796   if (wb
4797       && (GET_CODE (ind) == POST_MODIFY || GET_CODE (ind) == PRE_MODIFY)
4798       && arm_address_register_rtx_p (XEXP (ind, 0), 0)
4799       && GET_CODE (XEXP (ind, 1)) == PLUS
4800       && rtx_equal_p (XEXP (XEXP (ind, 1), 0), XEXP (ind, 0)))
4801     ind = XEXP (ind, 1);
4802
4803   /* Match:
4804      (plus (reg)
4805            (const)).  */
4806   if (GET_CODE (ind) == PLUS
4807       && GET_CODE (XEXP (ind, 0)) == REG
4808       && REG_MODE_OK_FOR_BASE_P (XEXP (ind, 0), VOIDmode)
4809       && GET_CODE (XEXP (ind, 1)) == CONST_INT
4810       && INTVAL (XEXP (ind, 1)) > -1024
4811       && INTVAL (XEXP (ind, 1)) <  1024
4812       && (INTVAL (XEXP (ind, 1)) & 3) == 0)
4813     return TRUE;
4814
4815   return FALSE;
4816 }
4817
4818 /* Return true if X is a register that will be eliminated later on.  */
4819 int
4820 arm_eliminable_register (rtx x)
4821 {
4822   return REG_P (x) && (REGNO (x) == FRAME_POINTER_REGNUM
4823                        || REGNO (x) == ARG_POINTER_REGNUM
4824                        || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
4825                            && REGNO (x) <= LAST_VIRTUAL_REGISTER));
4826 }
4827
4828 /* Return GENERAL_REGS if a scratch register required to reload x to/from
4829    VFP registers.  Otherwise return NO_REGS.  */
4830
4831 enum reg_class
4832 vfp_secondary_reload_class (enum machine_mode mode, rtx x)
4833 {
4834   if (arm_coproc_mem_operand (x, FALSE) || s_register_operand (x, mode))
4835     return NO_REGS;
4836
4837   return GENERAL_REGS;
4838 }
4839
4840
4841 /* Returns TRUE if INSN is an "LDR REG, ADDR" instruction.
4842    Use by the Cirrus Maverick code which has to workaround
4843    a hardware bug triggered by such instructions.  */
4844 static bool
4845 arm_memory_load_p (rtx insn)
4846 {
4847   rtx body, lhs, rhs;;
4848
4849   if (insn == NULL_RTX || GET_CODE (insn) != INSN)
4850     return false;
4851
4852   body = PATTERN (insn);
4853
4854   if (GET_CODE (body) != SET)
4855     return false;
4856
4857   lhs = XEXP (body, 0);
4858   rhs = XEXP (body, 1);
4859
4860   lhs = REG_OR_SUBREG_RTX (lhs);
4861
4862   /* If the destination is not a general purpose
4863      register we do not have to worry.  */
4864   if (GET_CODE (lhs) != REG
4865       || REGNO_REG_CLASS (REGNO (lhs)) != GENERAL_REGS)
4866     return false;
4867
4868   /* As well as loads from memory we also have to react
4869      to loads of invalid constants which will be turned
4870      into loads from the minipool.  */
4871   return (GET_CODE (rhs) == MEM
4872           || GET_CODE (rhs) == SYMBOL_REF
4873           || note_invalid_constants (insn, -1, false));
4874 }
4875
4876 /* Return TRUE if INSN is a Cirrus instruction.  */
4877 static bool
4878 arm_cirrus_insn_p (rtx insn)
4879 {
4880   enum attr_cirrus attr;
4881
4882   /* get_attr aborts on USE and CLOBBER.  */
4883   if (!insn
4884       || GET_CODE (insn) != INSN
4885       || GET_CODE (PATTERN (insn)) == USE
4886       || GET_CODE (PATTERN (insn)) == CLOBBER)
4887     return 0;
4888
4889   attr = get_attr_cirrus (insn);
4890
4891   return attr != CIRRUS_NOT;
4892 }
4893
4894 /* Cirrus reorg for invalid instruction combinations.  */
4895 static void
4896 cirrus_reorg (rtx first)
4897 {
4898   enum attr_cirrus attr;
4899   rtx body = PATTERN (first);
4900   rtx t;
4901   int nops;
4902
4903   /* Any branch must be followed by 2 non Cirrus instructions.  */
4904   if (GET_CODE (first) == JUMP_INSN && GET_CODE (body) != RETURN)
4905     {
4906       nops = 0;
4907       t = next_nonnote_insn (first);
4908
4909       if (arm_cirrus_insn_p (t))
4910         ++ nops;
4911
4912       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
4913         ++ nops;
4914
4915       while (nops --)
4916         emit_insn_after (gen_nop (), first);
4917
4918       return;
4919     }
4920
4921   /* (float (blah)) is in parallel with a clobber.  */
4922   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4923     body = XVECEXP (body, 0, 0);
4924
4925   if (GET_CODE (body) == SET)
4926     {
4927       rtx lhs = XEXP (body, 0), rhs = XEXP (body, 1);
4928
4929       /* cfldrd, cfldr64, cfstrd, cfstr64 must
4930          be followed by a non Cirrus insn.  */
4931       if (get_attr_cirrus (first) == CIRRUS_DOUBLE)
4932         {
4933           if (arm_cirrus_insn_p (next_nonnote_insn (first)))
4934             emit_insn_after (gen_nop (), first);
4935
4936           return;
4937         }
4938       else if (arm_memory_load_p (first))
4939         {
4940           unsigned int arm_regno;
4941
4942           /* Any ldr/cfmvdlr, ldr/cfmvdhr, ldr/cfmvsr, ldr/cfmv64lr,
4943              ldr/cfmv64hr combination where the Rd field is the same
4944              in both instructions must be split with a non Cirrus
4945              insn.  Example:
4946
4947              ldr r0, blah
4948              nop
4949              cfmvsr mvf0, r0.  */
4950
4951           /* Get Arm register number for ldr insn.  */
4952           if (GET_CODE (lhs) == REG)
4953             arm_regno = REGNO (lhs);
4954           else if (GET_CODE (rhs) == REG)
4955             arm_regno = REGNO (rhs);
4956           else
4957             abort ();
4958
4959           /* Next insn.  */
4960           first = next_nonnote_insn (first);
4961
4962           if (! arm_cirrus_insn_p (first))
4963             return;
4964
4965           body = PATTERN (first);
4966
4967           /* (float (blah)) is in parallel with a clobber.  */
4968           if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0))
4969             body = XVECEXP (body, 0, 0);
4970
4971           if (GET_CODE (body) == FLOAT)
4972             body = XEXP (body, 0);
4973
4974           if (get_attr_cirrus (first) == CIRRUS_MOVE
4975               && GET_CODE (XEXP (body, 1)) == REG
4976               && arm_regno == REGNO (XEXP (body, 1)))
4977             emit_insn_after (gen_nop (), first);
4978
4979           return;
4980         }
4981     }
4982
4983   /* get_attr aborts on USE and CLOBBER.  */
4984   if (!first
4985       || GET_CODE (first) != INSN
4986       || GET_CODE (PATTERN (first)) == USE
4987       || GET_CODE (PATTERN (first)) == CLOBBER)
4988     return;
4989
4990   attr = get_attr_cirrus (first);
4991
4992   /* Any coprocessor compare instruction (cfcmps, cfcmpd, ...)
4993      must be followed by a non-coprocessor instruction.  */
4994   if (attr == CIRRUS_COMPARE)
4995     {
4996       nops = 0;
4997
4998       t = next_nonnote_insn (first);
4999
5000       if (arm_cirrus_insn_p (t))
5001         ++ nops;
5002
5003       if (arm_cirrus_insn_p (next_nonnote_insn (t)))
5004         ++ nops;
5005
5006       while (nops --)
5007         emit_insn_after (gen_nop (), first);
5008
5009       return;
5010     }
5011 }
5012
5013 /* Return TRUE if X references a SYMBOL_REF.  */
5014 int
5015 symbol_mentioned_p (rtx x)
5016 {
5017   const char * fmt;
5018   int i;
5019
5020   if (GET_CODE (x) == SYMBOL_REF)
5021     return 1;
5022
5023   fmt = GET_RTX_FORMAT (GET_CODE (x));
5024
5025   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5026     {
5027       if (fmt[i] == 'E')
5028         {
5029           int j;
5030
5031           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5032             if (symbol_mentioned_p (XVECEXP (x, i, j)))
5033               return 1;
5034         }
5035       else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
5036         return 1;
5037     }
5038
5039   return 0;
5040 }
5041
5042 /* Return TRUE if X references a LABEL_REF.  */
5043 int
5044 label_mentioned_p (rtx x)
5045 {
5046   const char * fmt;
5047   int i;
5048
5049   if (GET_CODE (x) == LABEL_REF)
5050     return 1;
5051
5052   fmt = GET_RTX_FORMAT (GET_CODE (x));
5053   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
5054     {
5055       if (fmt[i] == 'E')
5056         {
5057           int j;
5058
5059           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5060             if (label_mentioned_p (XVECEXP (x, i, j)))
5061               return 1;
5062         }
5063       else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
5064         return 1;
5065     }
5066
5067   return 0;
5068 }
5069
5070 enum rtx_code
5071 minmax_code (rtx x)
5072 {
5073   enum rtx_code code = GET_CODE (x);
5074
5075   if (code == SMAX)
5076     return GE;
5077   else if (code == SMIN)
5078     return LE;
5079   else if (code == UMIN)
5080     return LEU;
5081   else if (code == UMAX)
5082     return GEU;
5083
5084   abort ();
5085 }
5086
5087 /* Return 1 if memory locations are adjacent.  */
5088 int
5089 adjacent_mem_locations (rtx a, rtx b)
5090 {
5091   if ((GET_CODE (XEXP (a, 0)) == REG
5092        || (GET_CODE (XEXP (a, 0)) == PLUS
5093            && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
5094       && (GET_CODE (XEXP (b, 0)) == REG
5095           || (GET_CODE (XEXP (b, 0)) == PLUS
5096               && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
5097     {
5098       HOST_WIDE_INT val0 = 0, val1 = 0;
5099       rtx reg0, reg1;
5100       int val_diff;
5101
5102       if (GET_CODE (XEXP (a, 0)) == PLUS)
5103         {
5104           reg0 = XEXP (XEXP (a, 0), 0);
5105           val0 = INTVAL (XEXP (XEXP (a, 0), 1));
5106         }
5107       else
5108         reg0 = XEXP (a, 0);
5109
5110       if (GET_CODE (XEXP (b, 0)) == PLUS)
5111         {
5112           reg1 = XEXP (XEXP (b, 0), 0);
5113           val1 = INTVAL (XEXP (XEXP (b, 0), 1));
5114         }
5115       else
5116         reg1 = XEXP (b, 0);
5117
5118       /* Don't accept any offset that will require multiple
5119          instructions to handle, since this would cause the
5120          arith_adjacentmem pattern to output an overlong sequence.  */
5121       if (!const_ok_for_op (PLUS, val0) || !const_ok_for_op (PLUS, val1))
5122         return 0;
5123
5124       /* Don't allow an eliminable register: register elimination can make
5125          the offset too large.  */
5126       if (arm_eliminable_register (reg0))
5127         return 0;
5128
5129       val_diff = val1 - val0;
5130       return ((REGNO (reg0) == REGNO (reg1))
5131               && (val_diff == 4 || val_diff == -4));
5132     }
5133
5134   return 0;
5135 }
5136
5137 int
5138 load_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5139                         HOST_WIDE_INT *load_offset)
5140 {
5141   int unsorted_regs[4];
5142   HOST_WIDE_INT unsorted_offsets[4];
5143   int order[4];
5144   int base_reg = -1;
5145   int i;
5146
5147   /* Can only handle 2, 3, or 4 insns at present,
5148      though could be easily extended if required.  */
5149   if (nops < 2 || nops > 4)
5150     abort ();
5151
5152   /* Loop over the operands and check that the memory references are
5153      suitable (i.e. immediate offsets from the same base register).  At
5154      the same time, extract the target register, and the memory
5155      offsets.  */
5156   for (i = 0; i < nops; i++)
5157     {
5158       rtx reg;
5159       rtx offset;
5160
5161       /* Convert a subreg of a mem into the mem itself.  */
5162       if (GET_CODE (operands[nops + i]) == SUBREG)
5163         operands[nops + i] = alter_subreg (operands + (nops + i));
5164
5165       if (GET_CODE (operands[nops + i]) != MEM)
5166         abort ();
5167
5168       /* Don't reorder volatile memory references; it doesn't seem worth
5169          looking for the case where the order is ok anyway.  */
5170       if (MEM_VOLATILE_P (operands[nops + i]))
5171         return 0;
5172
5173       offset = const0_rtx;
5174
5175       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5176            || (GET_CODE (reg) == SUBREG
5177                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5178           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5179               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5180                    == REG)
5181                   || (GET_CODE (reg) == SUBREG
5182                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5183               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5184                   == CONST_INT)))
5185         {
5186           if (i == 0)
5187             {
5188               base_reg = REGNO (reg);
5189               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5190                                   ? REGNO (operands[i])
5191                                   : REGNO (SUBREG_REG (operands[i])));
5192               order[0] = 0;
5193             }
5194           else
5195             {
5196               if (base_reg != (int) REGNO (reg))
5197                 /* Not addressed from the same base register.  */
5198                 return 0;
5199
5200               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5201                                   ? REGNO (operands[i])
5202                                   : REGNO (SUBREG_REG (operands[i])));
5203               if (unsorted_regs[i] < unsorted_regs[order[0]])
5204                 order[0] = i;
5205             }
5206
5207           /* If it isn't an integer register, or if it overwrites the
5208              base register but isn't the last insn in the list, then
5209              we can't do this.  */
5210           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
5211               || (i != nops - 1 && unsorted_regs[i] == base_reg))
5212             return 0;
5213
5214           unsorted_offsets[i] = INTVAL (offset);
5215         }
5216       else
5217         /* Not a suitable memory address.  */
5218         return 0;
5219     }
5220
5221   /* All the useful information has now been extracted from the
5222      operands into unsorted_regs and unsorted_offsets; additionally,
5223      order[0] has been set to the lowest numbered register in the
5224      list.  Sort the registers into order, and check that the memory
5225      offsets are ascending and adjacent.  */
5226
5227   for (i = 1; i < nops; i++)
5228     {
5229       int j;
5230
5231       order[i] = order[i - 1];
5232       for (j = 0; j < nops; j++)
5233         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5234             && (order[i] == order[i - 1]
5235                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5236           order[i] = j;
5237
5238       /* Have we found a suitable register? if not, one must be used more
5239          than once.  */
5240       if (order[i] == order[i - 1])
5241         return 0;
5242
5243       /* Is the memory address adjacent and ascending? */
5244       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5245         return 0;
5246     }
5247
5248   if (base)
5249     {
5250       *base = base_reg;
5251
5252       for (i = 0; i < nops; i++)
5253         regs[i] = unsorted_regs[order[i]];
5254
5255       *load_offset = unsorted_offsets[order[0]];
5256     }
5257
5258   if (unsorted_offsets[order[0]] == 0)
5259     return 1; /* ldmia */
5260
5261   if (unsorted_offsets[order[0]] == 4)
5262     return 2; /* ldmib */
5263
5264   if (unsorted_offsets[order[nops - 1]] == 0)
5265     return 3; /* ldmda */
5266
5267   if (unsorted_offsets[order[nops - 1]] == -4)
5268     return 4; /* ldmdb */
5269
5270   /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
5271      if the offset isn't small enough.  The reason 2 ldrs are faster
5272      is because these ARMs are able to do more than one cache access
5273      in a single cycle.  The ARM9 and StrongARM have Harvard caches,
5274      whilst the ARM8 has a double bandwidth cache.  This means that
5275      these cores can do both an instruction fetch and a data fetch in
5276      a single cycle, so the trick of calculating the address into a
5277      scratch register (one of the result regs) and then doing a load
5278      multiple actually becomes slower (and no smaller in code size).
5279      That is the transformation
5280
5281         ldr     rd1, [rbase + offset]
5282         ldr     rd2, [rbase + offset + 4]
5283
5284      to
5285
5286         add     rd1, rbase, offset
5287         ldmia   rd1, {rd1, rd2}
5288
5289      produces worse code -- '3 cycles + any stalls on rd2' instead of
5290      '2 cycles + any stalls on rd2'.  On ARMs with only one cache
5291      access per cycle, the first sequence could never complete in less
5292      than 6 cycles, whereas the ldm sequence would only take 5 and
5293      would make better use of sequential accesses if not hitting the
5294      cache.
5295
5296      We cheat here and test 'arm_ld_sched' which we currently know to
5297      only be true for the ARM8, ARM9 and StrongARM.  If this ever
5298      changes, then the test below needs to be reworked.  */
5299   if (nops == 2 && arm_ld_sched)
5300     return 0;
5301
5302   /* Can't do it without setting up the offset, only do this if it takes
5303      no more than one insn.  */
5304   return (const_ok_for_arm (unsorted_offsets[order[0]])
5305           || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
5306 }
5307
5308 const char *
5309 emit_ldm_seq (rtx *operands, int nops)
5310 {
5311   int regs[4];
5312   int base_reg;
5313   HOST_WIDE_INT offset;
5314   char buf[100];
5315   int i;
5316
5317   switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5318     {
5319     case 1:
5320       strcpy (buf, "ldm%?ia\t");
5321       break;
5322
5323     case 2:
5324       strcpy (buf, "ldm%?ib\t");
5325       break;
5326
5327     case 3:
5328       strcpy (buf, "ldm%?da\t");
5329       break;
5330
5331     case 4:
5332       strcpy (buf, "ldm%?db\t");
5333       break;
5334
5335     case 5:
5336       if (offset >= 0)
5337         sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5338                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5339                  (long) offset);
5340       else
5341         sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
5342                  reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
5343                  (long) -offset);
5344       output_asm_insn (buf, operands);
5345       base_reg = regs[0];
5346       strcpy (buf, "ldm%?ia\t");
5347       break;
5348
5349     default:
5350       abort ();
5351     }
5352
5353   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5354            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5355
5356   for (i = 1; i < nops; i++)
5357     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5358              reg_names[regs[i]]);
5359
5360   strcat (buf, "}\t%@ phole ldm");
5361
5362   output_asm_insn (buf, operands);
5363   return "";
5364 }
5365
5366 int
5367 store_multiple_sequence (rtx *operands, int nops, int *regs, int *base,
5368                          HOST_WIDE_INT * load_offset)
5369 {
5370   int unsorted_regs[4];
5371   HOST_WIDE_INT unsorted_offsets[4];
5372   int order[4];
5373   int base_reg = -1;
5374   int i;
5375
5376   /* Can only handle 2, 3, or 4 insns at present, though could be easily
5377      extended if required.  */
5378   if (nops < 2 || nops > 4)
5379     abort ();
5380
5381   /* Loop over the operands and check that the memory references are
5382      suitable (i.e. immediate offsets from the same base register).  At
5383      the same time, extract the target register, and the memory
5384      offsets.  */
5385   for (i = 0; i < nops; i++)
5386     {
5387       rtx reg;
5388       rtx offset;
5389
5390       /* Convert a subreg of a mem into the mem itself.  */
5391       if (GET_CODE (operands[nops + i]) == SUBREG)
5392         operands[nops + i] = alter_subreg (operands + (nops + i));
5393
5394       if (GET_CODE (operands[nops + i]) != MEM)
5395         abort ();
5396
5397       /* Don't reorder volatile memory references; it doesn't seem worth
5398          looking for the case where the order is ok anyway.  */
5399       if (MEM_VOLATILE_P (operands[nops + i]))
5400         return 0;
5401
5402       offset = const0_rtx;
5403
5404       if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
5405            || (GET_CODE (reg) == SUBREG
5406                && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5407           || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
5408               && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
5409                    == REG)
5410                   || (GET_CODE (reg) == SUBREG
5411                       && GET_CODE (reg = SUBREG_REG (reg)) == REG))
5412               && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
5413                   == CONST_INT)))
5414         {
5415           if (i == 0)
5416             {
5417               base_reg = REGNO (reg);
5418               unsorted_regs[0] = (GET_CODE (operands[i]) == REG
5419                                   ? REGNO (operands[i])
5420                                   : REGNO (SUBREG_REG (operands[i])));
5421               order[0] = 0;
5422             }
5423           else
5424             {
5425               if (base_reg != (int) REGNO (reg))
5426                 /* Not addressed from the same base register.  */
5427                 return 0;
5428
5429               unsorted_regs[i] = (GET_CODE (operands[i]) == REG
5430                                   ? REGNO (operands[i])
5431                                   : REGNO (SUBREG_REG (operands[i])));
5432               if (unsorted_regs[i] < unsorted_regs[order[0]])
5433                 order[0] = i;
5434             }
5435
5436           /* If it isn't an integer register, then we can't do this.  */
5437           if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
5438             return 0;
5439
5440           unsorted_offsets[i] = INTVAL (offset);
5441         }
5442       else
5443         /* Not a suitable memory address.  */
5444         return 0;
5445     }
5446
5447   /* All the useful information has now been extracted from the
5448      operands into unsorted_regs and unsorted_offsets; additionally,
5449      order[0] has been set to the lowest numbered register in the
5450      list.  Sort the registers into order, and check that the memory
5451      offsets are ascending and adjacent.  */
5452
5453   for (i = 1; i < nops; i++)
5454     {
5455       int j;
5456
5457       order[i] = order[i - 1];
5458       for (j = 0; j < nops; j++)
5459         if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
5460             && (order[i] == order[i - 1]
5461                 || unsorted_regs[j] < unsorted_regs[order[i]]))
5462           order[i] = j;
5463
5464       /* Have we found a suitable register? if not, one must be used more
5465          than once.  */
5466       if (order[i] == order[i - 1])
5467         return 0;
5468
5469       /* Is the memory address adjacent and ascending? */
5470       if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
5471         return 0;
5472     }
5473
5474   if (base)
5475     {
5476       *base = base_reg;
5477
5478       for (i = 0; i < nops; i++)
5479         regs[i] = unsorted_regs[order[i]];
5480
5481       *load_offset = unsorted_offsets[order[0]];
5482     }
5483
5484   if (unsorted_offsets[order[0]] == 0)
5485     return 1; /* stmia */
5486
5487   if (unsorted_offsets[order[0]] == 4)
5488     return 2; /* stmib */
5489
5490   if (unsorted_offsets[order[nops - 1]] == 0)
5491     return 3; /* stmda */
5492
5493   if (unsorted_offsets[order[nops - 1]] == -4)
5494     return 4; /* stmdb */
5495
5496   return 0;
5497 }
5498
5499 const char *
5500 emit_stm_seq (rtx *operands, int nops)
5501 {
5502   int regs[4];
5503   int base_reg;
5504   HOST_WIDE_INT offset;
5505   char buf[100];
5506   int i;
5507
5508   switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
5509     {
5510     case 1:
5511       strcpy (buf, "stm%?ia\t");
5512       break;
5513
5514     case 2:
5515       strcpy (buf, "stm%?ib\t");
5516       break;
5517
5518     case 3:
5519       strcpy (buf, "stm%?da\t");
5520       break;
5521
5522     case 4:
5523       strcpy (buf, "stm%?db\t");
5524       break;
5525
5526     default:
5527       abort ();
5528     }
5529
5530   sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
5531            reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
5532
5533   for (i = 1; i < nops; i++)
5534     sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
5535              reg_names[regs[i]]);
5536
5537   strcat (buf, "}\t%@ phole stm");
5538
5539   output_asm_insn (buf, operands);
5540   return "";
5541 }
5542
5543 \f
5544 /* Routines for use in generating RTL.  */
5545
5546 rtx
5547 arm_gen_load_multiple (int base_regno, int count, rtx from, int up,
5548                        int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5549 {
5550   HOST_WIDE_INT offset = *offsetp;
5551   int i = 0, j;
5552   rtx result;
5553   int sign = up ? 1 : -1;
5554   rtx mem, addr;
5555
5556   /* XScale has load-store double instructions, but they have stricter
5557      alignment requirements than load-store multiple, so we cannot
5558      use them.
5559
5560      For XScale ldm requires 2 + NREGS cycles to complete and blocks
5561      the pipeline until completion.
5562
5563         NREGS           CYCLES
5564           1               3
5565           2               4
5566           3               5
5567           4               6
5568
5569      An ldr instruction takes 1-3 cycles, but does not block the
5570      pipeline.
5571
5572         NREGS           CYCLES
5573           1              1-3
5574           2              2-6
5575           3              3-9
5576           4              4-12
5577
5578      Best case ldr will always win.  However, the more ldr instructions
5579      we issue, the less likely we are to be able to schedule them well.
5580      Using ldr instructions also increases code size.
5581
5582      As a compromise, we use ldr for counts of 1 or 2 regs, and ldm
5583      for counts of 3 or 4 regs.  */
5584   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5585     {
5586       rtx seq;
5587
5588       start_sequence ();
5589
5590       for (i = 0; i < count; i++)
5591         {
5592           addr = plus_constant (from, i * 4 * sign);
5593           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5594           emit_move_insn (gen_rtx_REG (SImode, base_regno + i), mem);
5595           offset += 4 * sign;
5596         }
5597
5598       if (write_back)
5599         {
5600           emit_move_insn (from, plus_constant (from, count * 4 * sign));
5601           *offsetp = offset;
5602         }
5603
5604       seq = get_insns ();
5605       end_sequence ();
5606
5607       return seq;
5608     }
5609
5610   result = gen_rtx_PARALLEL (VOIDmode,
5611                              rtvec_alloc (count + (write_back ? 1 : 0)));
5612   if (write_back)
5613     {
5614       XVECEXP (result, 0, 0)
5615         = gen_rtx_SET (GET_MODE (from), from,
5616                        plus_constant (from, count * 4 * sign));
5617       i = 1;
5618       count++;
5619     }
5620
5621   for (j = 0; i < count; i++, j++)
5622     {
5623       addr = plus_constant (from, j * 4 * sign);
5624       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5625       XVECEXP (result, 0, i)
5626         = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
5627       offset += 4 * sign;
5628     }
5629
5630   if (write_back)
5631     *offsetp = offset;
5632
5633   return result;
5634 }
5635
5636 rtx
5637 arm_gen_store_multiple (int base_regno, int count, rtx to, int up,
5638                         int write_back, rtx basemem, HOST_WIDE_INT *offsetp)
5639 {
5640   HOST_WIDE_INT offset = *offsetp;
5641   int i = 0, j;
5642   rtx result;
5643   int sign = up ? 1 : -1;
5644   rtx mem, addr;
5645
5646   /* See arm_gen_load_multiple for discussion of
5647      the pros/cons of ldm/stm usage for XScale.  */
5648   if (arm_tune_xscale && count <= 2 && ! optimize_size)
5649     {
5650       rtx seq;
5651
5652       start_sequence ();
5653
5654       for (i = 0; i < count; i++)
5655         {
5656           addr = plus_constant (to, i * 4 * sign);
5657           mem = adjust_automodify_address (basemem, SImode, addr, offset);
5658           emit_move_insn (mem, gen_rtx_REG (SImode, base_regno + i));
5659           offset += 4 * sign;
5660         }
5661
5662       if (write_back)
5663         {
5664           emit_move_insn (to, plus_constant (to, count * 4 * sign));
5665           *offsetp = offset;
5666         }
5667
5668       seq = get_insns ();
5669       end_sequence ();
5670
5671       return seq;
5672     }
5673
5674   result = gen_rtx_PARALLEL (VOIDmode,
5675                              rtvec_alloc (count + (write_back ? 1 : 0)));
5676   if (write_back)
5677     {
5678       XVECEXP (result, 0, 0)
5679         = gen_rtx_SET (GET_MODE (to), to,
5680                        plus_constant (to, count * 4 * sign));
5681       i = 1;
5682       count++;
5683     }
5684
5685   for (j = 0; i < count; i++, j++)
5686     {
5687       addr = plus_constant (to, j * 4 * sign);
5688       mem = adjust_automodify_address_nv (basemem, SImode, addr, offset);
5689       XVECEXP (result, 0, i)
5690         = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
5691       offset += 4 * sign;
5692     }
5693
5694   if (write_back)
5695     *offsetp = offset;
5696
5697   return result;
5698 }
5699
5700 int
5701 arm_gen_movmemqi (rtx *operands)
5702 {
5703   HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
5704   HOST_WIDE_INT srcoffset, dstoffset;
5705   int i;
5706   rtx src, dst, srcbase, dstbase;
5707   rtx part_bytes_reg = NULL;
5708   rtx mem;
5709
5710   if (GET_CODE (operands[2]) != CONST_INT
5711       || GET_CODE (operands[3]) != CONST_INT
5712       || INTVAL (operands[2]) > 64
5713       || INTVAL (operands[3]) & 3)
5714     return 0;
5715
5716   dstbase = operands[0];
5717   srcbase = operands[1];
5718
5719   dst = copy_to_mode_reg (SImode, XEXP (dstbase, 0));
5720   src = copy_to_mode_reg (SImode, XEXP (srcbase, 0));
5721
5722   in_words_to_go = ARM_NUM_INTS (INTVAL (operands[2]));
5723   out_words_to_go = INTVAL (operands[2]) / 4;
5724   last_bytes = INTVAL (operands[2]) & 3;
5725   dstoffset = srcoffset = 0;
5726
5727   if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
5728     part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
5729
5730   for (i = 0; in_words_to_go >= 2; i+=4)
5731     {
5732       if (in_words_to_go > 4)
5733         emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
5734                                           srcbase, &srcoffset));
5735       else
5736         emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
5737                                           FALSE, srcbase, &srcoffset));
5738
5739       if (out_words_to_go)
5740         {
5741           if (out_words_to_go > 4)
5742             emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
5743                                                dstbase, &dstoffset));
5744           else if (out_words_to_go != 1)
5745             emit_insn (arm_gen_store_multiple (0, out_words_to_go,
5746                                                dst, TRUE,
5747                                                (last_bytes == 0
5748                                                 ? FALSE : TRUE),
5749                                                dstbase, &dstoffset));
5750           else
5751             {
5752               mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5753               emit_move_insn (mem, gen_rtx_REG (SImode, 0));
5754               if (last_bytes != 0)
5755                 {
5756                   emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
5757                   dstoffset += 4;
5758                 }
5759             }
5760         }
5761
5762       in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
5763       out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
5764     }
5765
5766   /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do.  */
5767   if (out_words_to_go)
5768     {
5769       rtx sreg;
5770
5771       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5772       sreg = copy_to_reg (mem);
5773
5774       mem = adjust_automodify_address (dstbase, SImode, dst, dstoffset);
5775       emit_move_insn (mem, sreg);
5776       in_words_to_go--;
5777
5778       if (in_words_to_go)       /* Sanity check */
5779         abort ();
5780     }
5781
5782   if (in_words_to_go)
5783     {
5784       if (in_words_to_go < 0)
5785         abort ();
5786
5787       mem = adjust_automodify_address (srcbase, SImode, src, srcoffset);
5788       part_bytes_reg = copy_to_mode_reg (SImode, mem);
5789     }
5790
5791   if (last_bytes && part_bytes_reg == NULL)
5792     abort ();
5793
5794   if (BYTES_BIG_ENDIAN && last_bytes)
5795     {
5796       rtx tmp = gen_reg_rtx (SImode);
5797
5798       /* The bytes we want are in the top end of the word.  */
5799       emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
5800                               GEN_INT (8 * (4 - last_bytes))));
5801       part_bytes_reg = tmp;
5802
5803       while (last_bytes)
5804         {
5805           mem = adjust_automodify_address (dstbase, QImode,
5806                                            plus_constant (dst, last_bytes - 1),
5807                                            dstoffset + last_bytes - 1);
5808           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5809
5810           if (--last_bytes)
5811             {
5812               tmp = gen_reg_rtx (SImode);
5813               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
5814               part_bytes_reg = tmp;
5815             }
5816         }
5817
5818     }
5819   else
5820     {
5821       if (last_bytes > 1)
5822         {
5823           mem = adjust_automodify_address (dstbase, HImode, dst, dstoffset);
5824           emit_move_insn (mem, gen_lowpart (HImode, part_bytes_reg));
5825           last_bytes -= 2;
5826           if (last_bytes)
5827             {
5828               rtx tmp = gen_reg_rtx (SImode);
5829               emit_insn (gen_addsi3 (dst, dst, const2_rtx));
5830               emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
5831               part_bytes_reg = tmp;
5832               dstoffset += 2;
5833             }
5834         }
5835
5836       if (last_bytes)
5837         {
5838           mem = adjust_automodify_address (dstbase, QImode, dst, dstoffset);
5839           emit_move_insn (mem, gen_lowpart (QImode, part_bytes_reg));
5840         }
5841     }
5842
5843   return 1;
5844 }
5845
5846 /* Generate a memory reference for a half word, such that it will be loaded
5847    into the top 16 bits of the word.  We can assume that the address is
5848    known to be alignable and of the form reg, or plus (reg, const).  */
5849
5850 rtx
5851 arm_gen_rotated_half_load (rtx memref)
5852 {
5853   HOST_WIDE_INT offset = 0;
5854   rtx base = XEXP (memref, 0);
5855
5856   if (GET_CODE (base) == PLUS)
5857     {
5858       offset = INTVAL (XEXP (base, 1));
5859       base = XEXP (base, 0);
5860     }
5861
5862   /* If we aren't allowed to generate unaligned addresses, then fail.  */
5863   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0))
5864     return NULL;
5865
5866   base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
5867
5868   if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
5869     return base;
5870
5871   return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
5872 }
5873
5874 /* Select a dominance comparison mode if possible for a test of the general
5875    form (OP (COND_OR (X) (Y)) (const_int 0)).  We support three forms.
5876    COND_OR == DOM_CC_X_AND_Y => (X && Y)
5877    COND_OR == DOM_CC_NX_OR_Y => ((! X) || Y)
5878    COND_OR == DOM_CC_X_OR_Y => (X || Y)
5879    In all cases OP will be either EQ or NE, but we don't need to know which
5880    here.  If we are unable to support a dominance comparison we return
5881    CC mode.  This will then fail to match for the RTL expressions that
5882    generate this call.  */
5883 enum machine_mode
5884 arm_select_dominance_cc_mode (rtx x, rtx y, HOST_WIDE_INT cond_or)
5885 {
5886   enum rtx_code cond1, cond2;
5887   int swapped = 0;
5888
5889   /* Currently we will probably get the wrong result if the individual
5890      comparisons are not simple.  This also ensures that it is safe to
5891      reverse a comparison if necessary.  */
5892   if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
5893        != CCmode)
5894       || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
5895           != CCmode))
5896     return CCmode;
5897
5898   /* The if_then_else variant of this tests the second condition if the
5899      first passes, but is true if the first fails.  Reverse the first
5900      condition to get a true "inclusive-or" expression.  */
5901   if (cond_or == DOM_CC_NX_OR_Y)
5902     cond1 = reverse_condition (cond1);
5903
5904   /* If the comparisons are not equal, and one doesn't dominate the other,
5905      then we can't do this.  */
5906   if (cond1 != cond2
5907       && !comparison_dominates_p (cond1, cond2)
5908       && (swapped = 1, !comparison_dominates_p (cond2, cond1)))
5909     return CCmode;
5910
5911   if (swapped)
5912     {
5913       enum rtx_code temp = cond1;
5914       cond1 = cond2;
5915       cond2 = temp;
5916     }
5917
5918   switch (cond1)
5919     {
5920     case EQ:
5921       if (cond2 == EQ || cond_or == DOM_CC_X_AND_Y)
5922         return CC_DEQmode;
5923
5924       switch (cond2)
5925         {
5926         case LE: return CC_DLEmode;
5927         case LEU: return CC_DLEUmode;
5928         case GE: return CC_DGEmode;
5929         case GEU: return CC_DGEUmode;
5930         default: break;
5931         }
5932
5933       break;
5934
5935     case LT:
5936       if (cond2 == LT || cond_or == DOM_CC_X_AND_Y)
5937         return CC_DLTmode;
5938       if (cond2 == LE)
5939         return CC_DLEmode;
5940       if (cond2 == NE)
5941         return CC_DNEmode;
5942       break;
5943
5944     case GT:
5945       if (cond2 == GT || cond_or == DOM_CC_X_AND_Y)
5946         return CC_DGTmode;
5947       if (cond2 == GE)
5948         return CC_DGEmode;
5949       if (cond2 == NE)
5950         return CC_DNEmode;
5951       break;
5952
5953     case LTU:
5954       if (cond2 == LTU || cond_or == DOM_CC_X_AND_Y)
5955         return CC_DLTUmode;
5956       if (cond2 == LEU)
5957         return CC_DLEUmode;
5958       if (cond2 == NE)
5959         return CC_DNEmode;
5960       break;
5961
5962     case GTU:
5963       if (cond2 == GTU || cond_or == DOM_CC_X_AND_Y)
5964         return CC_DGTUmode;
5965       if (cond2 == GEU)
5966         return CC_DGEUmode;
5967       if (cond2 == NE)
5968         return CC_DNEmode;
5969       break;
5970
5971     /* The remaining cases only occur when both comparisons are the
5972        same.  */
5973     case NE:
5974       return CC_DNEmode;
5975
5976     case LE:
5977       return CC_DLEmode;
5978
5979     case GE:
5980       return CC_DGEmode;
5981
5982     case LEU:
5983       return CC_DLEUmode;
5984
5985     case GEU:
5986       return CC_DGEUmode;
5987
5988     default:
5989       break;
5990     }
5991
5992   abort ();
5993 }
5994
5995 enum machine_mode
5996 arm_select_cc_mode (enum rtx_code op, rtx x, rtx y)
5997 {
5998   /* All floating point compares return CCFP if it is an equality
5999      comparison, and CCFPE otherwise.  */
6000   if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
6001     {
6002       switch (op)
6003         {
6004         case EQ:
6005         case NE:
6006         case UNORDERED:
6007         case ORDERED:
6008         case UNLT:
6009         case UNLE:
6010         case UNGT:
6011         case UNGE:
6012         case UNEQ:
6013         case LTGT:
6014           return CCFPmode;
6015
6016         case LT:
6017         case LE:
6018         case GT:
6019         case GE:
6020           if (TARGET_HARD_FLOAT && TARGET_MAVERICK)
6021             return CCFPmode;
6022           return CCFPEmode;
6023
6024         default:
6025           abort ();
6026         }
6027     }
6028
6029   /* A compare with a shifted operand.  Because of canonicalization, the
6030      comparison will have to be swapped when we emit the assembler.  */
6031   if (GET_MODE (y) == SImode && GET_CODE (y) == REG
6032       && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6033           || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
6034           || GET_CODE (x) == ROTATERT))
6035     return CC_SWPmode;
6036
6037   /* This is a special case that is used by combine to allow a
6038      comparison of a shifted byte load to be split into a zero-extend
6039      followed by a comparison of the shifted integer (only valid for
6040      equalities and unsigned inequalities).  */
6041   if (GET_MODE (x) == SImode
6042       && GET_CODE (x) == ASHIFT
6043       && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
6044       && GET_CODE (XEXP (x, 0)) == SUBREG
6045       && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
6046       && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
6047       && (op == EQ || op == NE
6048           || op == GEU || op == GTU || op == LTU || op == LEU)
6049       && GET_CODE (y) == CONST_INT)
6050     return CC_Zmode;
6051
6052   /* A construct for a conditional compare, if the false arm contains
6053      0, then both conditions must be true, otherwise either condition
6054      must be true.  Not all conditions are possible, so CCmode is
6055      returned if it can't be done.  */
6056   if (GET_CODE (x) == IF_THEN_ELSE
6057       && (XEXP (x, 2) == const0_rtx
6058           || XEXP (x, 2) == const1_rtx)
6059       && COMPARISON_P (XEXP (x, 0))
6060       && COMPARISON_P (XEXP (x, 1)))
6061     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6062                                          INTVAL (XEXP (x, 2)));
6063
6064   /* Alternate canonicalizations of the above.  These are somewhat cleaner.  */
6065   if (GET_CODE (x) == AND
6066       && COMPARISON_P (XEXP (x, 0))
6067       && COMPARISON_P (XEXP (x, 1)))
6068     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6069                                          DOM_CC_X_AND_Y);
6070
6071   if (GET_CODE (x) == IOR
6072       && COMPARISON_P (XEXP (x, 0))
6073       && COMPARISON_P (XEXP (x, 1)))
6074     return arm_select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
6075                                          DOM_CC_X_OR_Y);
6076
6077   /* An operation (on Thumb) where we want to test for a single bit.
6078      This is done by shifting that bit up into the top bit of a
6079      scratch register; we can then branch on the sign bit.  */
6080   if (TARGET_THUMB
6081       && GET_MODE (x) == SImode
6082       && (op == EQ || op == NE)
6083       && (GET_CODE (x) == ZERO_EXTRACT))
6084     return CC_Nmode;
6085
6086   /* An operation that sets the condition codes as a side-effect, the
6087      V flag is not set correctly, so we can only use comparisons where
6088      this doesn't matter.  (For LT and GE we can use "mi" and "pl"
6089      instead.)  */
6090   if (GET_MODE (x) == SImode
6091       && y == const0_rtx
6092       && (op == EQ || op == NE || op == LT || op == GE)
6093       && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
6094           || GET_CODE (x) == AND || GET_CODE (x) == IOR
6095           || GET_CODE (x) == XOR || GET_CODE (x) == MULT
6096           || GET_CODE (x) == NOT || GET_CODE (x) == NEG
6097           || GET_CODE (x) == LSHIFTRT
6098           || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
6099           || GET_CODE (x) == ROTATERT
6100           || (TARGET_ARM && GET_CODE (x) == ZERO_EXTRACT)))
6101     return CC_NOOVmode;
6102
6103   if (GET_MODE (x) == QImode && (op == EQ || op == NE))
6104     return CC_Zmode;
6105
6106   if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
6107       && GET_CODE (x) == PLUS
6108       && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
6109     return CC_Cmode;
6110
6111   return CCmode;
6112 }
6113
6114 /* X and Y are two things to compare using CODE.  Emit the compare insn and
6115    return the rtx for register 0 in the proper mode.  FP means this is a
6116    floating point compare: I don't think that it is needed on the arm.  */
6117 rtx
6118 arm_gen_compare_reg (enum rtx_code code, rtx x, rtx y)
6119 {
6120   enum machine_mode mode = SELECT_CC_MODE (code, x, y);
6121   rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
6122
6123   emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
6124                           gen_rtx_COMPARE (mode, x, y)));
6125
6126   return cc_reg;
6127 }
6128
6129 /* Generate a sequence of insns that will generate the correct return
6130    address mask depending on the physical architecture that the program
6131    is running on.  */
6132 rtx
6133 arm_gen_return_addr_mask (void)
6134 {
6135   rtx reg = gen_reg_rtx (Pmode);
6136
6137   emit_insn (gen_return_addr_mask (reg));
6138   return reg;
6139 }
6140
6141 void
6142 arm_reload_in_hi (rtx *operands)
6143 {
6144   rtx ref = operands[1];
6145   rtx base, scratch;
6146   HOST_WIDE_INT offset = 0;
6147
6148   if (GET_CODE (ref) == SUBREG)
6149     {
6150       offset = SUBREG_BYTE (ref);
6151       ref = SUBREG_REG (ref);
6152     }
6153
6154   if (GET_CODE (ref) == REG)
6155     {
6156       /* We have a pseudo which has been spilt onto the stack; there
6157          are two cases here: the first where there is a simple
6158          stack-slot replacement and a second where the stack-slot is
6159          out of range, or is used as a subreg.  */
6160       if (reg_equiv_mem[REGNO (ref)])
6161         {
6162           ref = reg_equiv_mem[REGNO (ref)];
6163           base = find_replacement (&XEXP (ref, 0));
6164         }
6165       else
6166         /* The slot is out of range, or was dressed up in a SUBREG.  */
6167         base = reg_equiv_address[REGNO (ref)];
6168     }
6169   else
6170     base = find_replacement (&XEXP (ref, 0));
6171
6172   /* Handle the case where the address is too complex to be offset by 1.  */
6173   if (GET_CODE (base) == MINUS
6174       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6175     {
6176       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6177
6178       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6179       base = base_plus;
6180     }
6181   else if (GET_CODE (base) == PLUS)
6182     {
6183       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6184       HOST_WIDE_INT hi, lo;
6185
6186       offset += INTVAL (XEXP (base, 1));
6187       base = XEXP (base, 0);
6188
6189       /* Rework the address into a legal sequence of insns.  */
6190       /* Valid range for lo is -4095 -> 4095 */
6191       lo = (offset >= 0
6192             ? (offset & 0xfff)
6193             : -((-offset) & 0xfff));
6194
6195       /* Corner case, if lo is the max offset then we would be out of range
6196          once we have added the additional 1 below, so bump the msb into the
6197          pre-loading insn(s).  */
6198       if (lo == 4095)
6199         lo &= 0x7ff;
6200
6201       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6202              ^ (HOST_WIDE_INT) 0x80000000)
6203             - (HOST_WIDE_INT) 0x80000000);
6204
6205       if (hi + lo != offset)
6206         abort ();
6207
6208       if (hi != 0)
6209         {
6210           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6211
6212           /* Get the base address; addsi3 knows how to handle constants
6213              that require more than one insn.  */
6214           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6215           base = base_plus;
6216           offset = lo;
6217         }
6218     }
6219
6220   /* Operands[2] may overlap operands[0] (though it won't overlap
6221      operands[1]), that's why we asked for a DImode reg -- so we can
6222      use the bit that does not overlap.  */
6223   if (REGNO (operands[2]) == REGNO (operands[0]))
6224     scratch = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6225   else
6226     scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6227
6228   emit_insn (gen_zero_extendqisi2 (scratch,
6229                                    gen_rtx_MEM (QImode,
6230                                                 plus_constant (base,
6231                                                                offset))));
6232   emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
6233                                    gen_rtx_MEM (QImode,
6234                                                 plus_constant (base,
6235                                                                offset + 1))));
6236   if (!BYTES_BIG_ENDIAN)
6237     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6238                         gen_rtx_IOR (SImode,
6239                                      gen_rtx_ASHIFT
6240                                      (SImode,
6241                                       gen_rtx_SUBREG (SImode, operands[0], 0),
6242                                       GEN_INT (8)),
6243                                      scratch)));
6244   else
6245     emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
6246                             gen_rtx_IOR (SImode,
6247                                          gen_rtx_ASHIFT (SImode, scratch,
6248                                                          GEN_INT (8)),
6249                                          gen_rtx_SUBREG (SImode, operands[0],
6250                                                          0))));
6251 }
6252
6253 /* Handle storing a half-word to memory during reload by synthesizing as two
6254    byte stores.  Take care not to clobber the input values until after we
6255    have moved them somewhere safe.  This code assumes that if the DImode
6256    scratch in operands[2] overlaps either the input value or output address
6257    in some way, then that value must die in this insn (we absolutely need
6258    two scratch registers for some corner cases).  */
6259 void
6260 arm_reload_out_hi (rtx *operands)
6261 {
6262   rtx ref = operands[0];
6263   rtx outval = operands[1];
6264   rtx base, scratch;
6265   HOST_WIDE_INT offset = 0;
6266
6267   if (GET_CODE (ref) == SUBREG)
6268     {
6269       offset = SUBREG_BYTE (ref);
6270       ref = SUBREG_REG (ref);
6271     }
6272
6273   if (GET_CODE (ref) == REG)
6274     {
6275       /* We have a pseudo which has been spilt onto the stack; there
6276          are two cases here: the first where there is a simple
6277          stack-slot replacement and a second where the stack-slot is
6278          out of range, or is used as a subreg.  */
6279       if (reg_equiv_mem[REGNO (ref)])
6280         {
6281           ref = reg_equiv_mem[REGNO (ref)];
6282           base = find_replacement (&XEXP (ref, 0));
6283         }
6284       else
6285         /* The slot is out of range, or was dressed up in a SUBREG.  */
6286         base = reg_equiv_address[REGNO (ref)];
6287     }
6288   else
6289     base = find_replacement (&XEXP (ref, 0));
6290
6291   scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
6292
6293   /* Handle the case where the address is too complex to be offset by 1.  */
6294   if (GET_CODE (base) == MINUS
6295       || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
6296     {
6297       rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6298
6299       /* Be careful not to destroy OUTVAL.  */
6300       if (reg_overlap_mentioned_p (base_plus, outval))
6301         {
6302           /* Updating base_plus might destroy outval, see if we can
6303              swap the scratch and base_plus.  */
6304           if (!reg_overlap_mentioned_p (scratch, outval))
6305             {
6306               rtx tmp = scratch;
6307               scratch = base_plus;
6308               base_plus = tmp;
6309             }
6310           else
6311             {
6312               rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6313
6314               /* Be conservative and copy OUTVAL into the scratch now,
6315                  this should only be necessary if outval is a subreg
6316                  of something larger than a word.  */
6317               /* XXX Might this clobber base?  I can't see how it can,
6318                  since scratch is known to overlap with OUTVAL, and
6319                  must be wider than a word.  */
6320               emit_insn (gen_movhi (scratch_hi, outval));
6321               outval = scratch_hi;
6322             }
6323         }
6324
6325       emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
6326       base = base_plus;
6327     }
6328   else if (GET_CODE (base) == PLUS)
6329     {
6330       /* The addend must be CONST_INT, or we would have dealt with it above.  */
6331       HOST_WIDE_INT hi, lo;
6332
6333       offset += INTVAL (XEXP (base, 1));
6334       base = XEXP (base, 0);
6335
6336       /* Rework the address into a legal sequence of insns.  */
6337       /* Valid range for lo is -4095 -> 4095 */
6338       lo = (offset >= 0
6339             ? (offset & 0xfff)
6340             : -((-offset) & 0xfff));
6341
6342       /* Corner case, if lo is the max offset then we would be out of range
6343          once we have added the additional 1 below, so bump the msb into the
6344          pre-loading insn(s).  */
6345       if (lo == 4095)
6346         lo &= 0x7ff;
6347
6348       hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
6349              ^ (HOST_WIDE_INT) 0x80000000)
6350             - (HOST_WIDE_INT) 0x80000000);
6351
6352       if (hi + lo != offset)
6353         abort ();
6354
6355       if (hi != 0)
6356         {
6357           rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
6358
6359           /* Be careful not to destroy OUTVAL.  */
6360           if (reg_overlap_mentioned_p (base_plus, outval))
6361             {
6362               /* Updating base_plus might destroy outval, see if we
6363                  can swap the scratch and base_plus.  */
6364               if (!reg_overlap_mentioned_p (scratch, outval))
6365                 {
6366                   rtx tmp = scratch;
6367                   scratch = base_plus;
6368                   base_plus = tmp;
6369                 }
6370               else
6371                 {
6372                   rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
6373
6374                   /* Be conservative and copy outval into scratch now,
6375                      this should only be necessary if outval is a
6376                      subreg of something larger than a word.  */
6377                   /* XXX Might this clobber base?  I can't see how it
6378                      can, since scratch is known to overlap with
6379                      outval.  */
6380                   emit_insn (gen_movhi (scratch_hi, outval));
6381                   outval = scratch_hi;
6382                 }
6383             }
6384
6385           /* Get the base address; addsi3 knows how to handle constants
6386              that require more than one insn.  */
6387           emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
6388           base = base_plus;
6389           offset = lo;
6390         }
6391     }
6392
6393   if (BYTES_BIG_ENDIAN)
6394     {
6395       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6396                                          plus_constant (base, offset + 1)),
6397                             gen_lowpart (QImode, outval)));
6398       emit_insn (gen_lshrsi3 (scratch,
6399                               gen_rtx_SUBREG (SImode, outval, 0),
6400                               GEN_INT (8)));
6401       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6402                             gen_lowpart (QImode, scratch)));
6403     }
6404   else
6405     {
6406       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
6407                             gen_lowpart (QImode, outval)));
6408       emit_insn (gen_lshrsi3 (scratch,
6409                               gen_rtx_SUBREG (SImode, outval, 0),
6410                               GEN_INT (8)));
6411       emit_insn (gen_movqi (gen_rtx_MEM (QImode,
6412                                          plus_constant (base, offset + 1)),
6413                             gen_lowpart (QImode, scratch)));
6414     }
6415 }
6416 \f
6417 /* Print a symbolic form of X to the debug file, F.  */
6418 static void
6419 arm_print_value (FILE *f, rtx x)
6420 {
6421   switch (GET_CODE (x))
6422     {
6423     case CONST_INT:
6424       fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
6425       return;
6426
6427     case CONST_DOUBLE:
6428       fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
6429       return;
6430
6431     case CONST_VECTOR:
6432       {
6433         int i;
6434
6435         fprintf (f, "<");
6436         for (i = 0; i < CONST_VECTOR_NUNITS (x); i++)
6437           {
6438             fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (CONST_VECTOR_ELT (x, i)));
6439             if (i < (CONST_VECTOR_NUNITS (x) - 1))
6440               fputc (',', f);
6441           }
6442         fprintf (f, ">");
6443       }
6444       return;
6445
6446     case CONST_STRING:
6447       fprintf (f, "\"%s\"", XSTR (x, 0));
6448       return;
6449
6450     case SYMBOL_REF:
6451       fprintf (f, "`%s'", XSTR (x, 0));
6452       return;
6453
6454     case LABEL_REF:
6455       fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
6456       return;
6457
6458     case CONST:
6459       arm_print_value (f, XEXP (x, 0));
6460       return;
6461
6462     case PLUS:
6463       arm_print_value (f, XEXP (x, 0));
6464       fprintf (f, "+");
6465       arm_print_value (f, XEXP (x, 1));
6466       return;
6467
6468     case PC:
6469       fprintf (f, "pc");
6470       return;
6471
6472     default:
6473       fprintf (f, "????");
6474       return;
6475     }
6476 }
6477 \f
6478 /* Routines for manipulation of the constant pool.  */
6479
6480 /* Arm instructions cannot load a large constant directly into a
6481    register; they have to come from a pc relative load.  The constant
6482    must therefore be placed in the addressable range of the pc
6483    relative load.  Depending on the precise pc relative load
6484    instruction the range is somewhere between 256 bytes and 4k.  This
6485    means that we often have to dump a constant inside a function, and
6486    generate code to branch around it.
6487
6488    It is important to minimize this, since the branches will slow
6489    things down and make the code larger.
6490
6491    Normally we can hide the table after an existing unconditional
6492    branch so that there is no interruption of the flow, but in the
6493    worst case the code looks like this:
6494
6495         ldr     rn, L1
6496         ...
6497         b       L2
6498         align
6499         L1:     .long value
6500         L2:
6501         ...
6502
6503         ldr     rn, L3
6504         ...
6505         b       L4
6506         align
6507         L3:     .long value
6508         L4:
6509         ...
6510
6511    We fix this by performing a scan after scheduling, which notices
6512    which instructions need to have their operands fetched from the
6513    constant table and builds the table.
6514
6515    The algorithm starts by building a table of all the constants that
6516    need fixing up and all the natural barriers in the function (places
6517    where a constant table can be dropped without breaking the flow).
6518    For each fixup we note how far the pc-relative replacement will be
6519    able to reach and the offset of the instruction into the function.
6520
6521    Having built the table we then group the fixes together to form
6522    tables that are as large as possible (subject to addressing
6523    constraints) and emit each table of constants after the last
6524    barrier that is within range of all the instructions in the group.
6525    If a group does not contain a barrier, then we forcibly create one
6526    by inserting a jump instruction into the flow.  Once the table has
6527    been inserted, the insns are then modified to reference the
6528    relevant entry in the pool.
6529
6530    Possible enhancements to the algorithm (not implemented) are:
6531
6532    1) For some processors and object formats, there may be benefit in
6533    aligning the pools to the start of cache lines; this alignment
6534    would need to be taken into account when calculating addressability
6535    of a pool.  */
6536
6537 /* These typedefs are located at the start of this file, so that
6538    they can be used in the prototypes there.  This comment is to
6539    remind readers of that fact so that the following structures
6540    can be understood more easily.
6541
6542      typedef struct minipool_node    Mnode;
6543      typedef struct minipool_fixup   Mfix;  */
6544
6545 struct minipool_node
6546 {
6547   /* Doubly linked chain of entries.  */
6548   Mnode * next;
6549   Mnode * prev;
6550   /* The maximum offset into the code that this entry can be placed.  While
6551      pushing fixes for forward references, all entries are sorted in order
6552      of increasing max_address.  */
6553   HOST_WIDE_INT max_address;
6554   /* Similarly for an entry inserted for a backwards ref.  */
6555   HOST_WIDE_INT min_address;
6556   /* The number of fixes referencing this entry.  This can become zero
6557      if we "unpush" an entry.  In this case we ignore the entry when we
6558      come to emit the code.  */
6559   int refcount;
6560   /* The offset from the start of the minipool.  */
6561   HOST_WIDE_INT offset;
6562   /* The value in table.  */
6563   rtx value;
6564   /* The mode of value.  */
6565   enum machine_mode mode;
6566   /* The size of the value.  With iWMMXt enabled
6567      sizes > 4 also imply an alignment of 8-bytes.  */
6568   int fix_size;
6569 };
6570
6571 struct minipool_fixup
6572 {
6573   Mfix *            next;
6574   rtx               insn;
6575   HOST_WIDE_INT     address;
6576   rtx *             loc;
6577   enum machine_mode mode;
6578   int               fix_size;
6579   rtx               value;
6580   Mnode *           minipool;
6581   HOST_WIDE_INT     forwards;
6582   HOST_WIDE_INT     backwards;
6583 };
6584
6585 /* Fixes less than a word need padding out to a word boundary.  */
6586 #define MINIPOOL_FIX_SIZE(mode) \
6587   (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
6588
6589 static Mnode *  minipool_vector_head;
6590 static Mnode *  minipool_vector_tail;
6591 static rtx      minipool_vector_label;
6592
6593 /* The linked list of all minipool fixes required for this function.  */
6594 Mfix *          minipool_fix_head;
6595 Mfix *          minipool_fix_tail;
6596 /* The fix entry for the current minipool, once it has been placed.  */
6597 Mfix *          minipool_barrier;
6598
6599 /* Determines if INSN is the start of a jump table.  Returns the end
6600    of the TABLE or NULL_RTX.  */
6601 static rtx
6602 is_jump_table (rtx insn)
6603 {
6604   rtx table;
6605
6606   if (GET_CODE (insn) == JUMP_INSN
6607       && JUMP_LABEL (insn) != NULL
6608       && ((table = next_real_insn (JUMP_LABEL (insn)))
6609           == next_real_insn (insn))
6610       && table != NULL
6611       && GET_CODE (table) == JUMP_INSN
6612       && (GET_CODE (PATTERN (table)) == ADDR_VEC
6613           || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
6614     return table;
6615
6616   return NULL_RTX;
6617 }
6618
6619 #ifndef JUMP_TABLES_IN_TEXT_SECTION
6620 #define JUMP_TABLES_IN_TEXT_SECTION 0
6621 #endif
6622
6623 static HOST_WIDE_INT
6624 get_jump_table_size (rtx insn)
6625 {
6626   /* ADDR_VECs only take room if read-only data does into the text
6627      section.  */
6628   if (JUMP_TABLES_IN_TEXT_SECTION
6629 #if !defined(READONLY_DATA_SECTION) && !defined(READONLY_DATA_SECTION_ASM_OP)
6630       || 1
6631 #endif
6632       )
6633     {
6634       rtx body = PATTERN (insn);
6635       int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
6636
6637       return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
6638     }
6639
6640   return 0;
6641 }
6642
6643 /* Move a minipool fix MP from its current location to before MAX_MP.
6644    If MAX_MP is NULL, then MP doesn't need moving, but the addressing
6645    constraints may need updating.  */
6646 static Mnode *
6647 move_minipool_fix_forward_ref (Mnode *mp, Mnode *max_mp,
6648                                HOST_WIDE_INT max_address)
6649 {
6650   /* This should never be true and the code below assumes these are
6651      different.  */
6652   if (mp == max_mp)
6653     abort ();
6654
6655   if (max_mp == NULL)
6656     {
6657       if (max_address < mp->max_address)
6658         mp->max_address = max_address;
6659     }
6660   else
6661     {
6662       if (max_address > max_mp->max_address - mp->fix_size)
6663         mp->max_address = max_mp->max_address - mp->fix_size;
6664       else
6665         mp->max_address = max_address;
6666
6667       /* Unlink MP from its current position.  Since max_mp is non-null,
6668        mp->prev must be non-null.  */
6669       mp->prev->next = mp->next;
6670       if (mp->next != NULL)
6671         mp->next->prev = mp->prev;
6672       else
6673         minipool_vector_tail = mp->prev;
6674
6675       /* Re-insert it before MAX_MP.  */
6676       mp->next = max_mp;
6677       mp->prev = max_mp->prev;
6678       max_mp->prev = mp;
6679
6680       if (mp->prev != NULL)
6681         mp->prev->next = mp;
6682       else
6683         minipool_vector_head = mp;
6684     }
6685
6686   /* Save the new entry.  */
6687   max_mp = mp;
6688
6689   /* Scan over the preceding entries and adjust their addresses as
6690      required.  */
6691   while (mp->prev != NULL
6692          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6693     {
6694       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6695       mp = mp->prev;
6696     }
6697
6698   return max_mp;
6699 }
6700
6701 /* Add a constant to the minipool for a forward reference.  Returns the
6702    node added or NULL if the constant will not fit in this pool.  */
6703 static Mnode *
6704 add_minipool_forward_ref (Mfix *fix)
6705 {
6706   /* If set, max_mp is the first pool_entry that has a lower
6707      constraint than the one we are trying to add.  */
6708   Mnode *       max_mp = NULL;
6709   HOST_WIDE_INT max_address = fix->address + fix->forwards;
6710   Mnode *       mp;
6711
6712   /* If this fix's address is greater than the address of the first
6713      entry, then we can't put the fix in this pool.  We subtract the
6714      size of the current fix to ensure that if the table is fully
6715      packed we still have enough room to insert this value by suffling
6716      the other fixes forwards.  */
6717   if (minipool_vector_head &&
6718       fix->address >= minipool_vector_head->max_address - fix->fix_size)
6719     return NULL;
6720
6721   /* Scan the pool to see if a constant with the same value has
6722      already been added.  While we are doing this, also note the
6723      location where we must insert the constant if it doesn't already
6724      exist.  */
6725   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6726     {
6727       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6728           && fix->mode == mp->mode
6729           && (GET_CODE (fix->value) != CODE_LABEL
6730               || (CODE_LABEL_NUMBER (fix->value)
6731                   == CODE_LABEL_NUMBER (mp->value)))
6732           && rtx_equal_p (fix->value, mp->value))
6733         {
6734           /* More than one fix references this entry.  */
6735           mp->refcount++;
6736           return move_minipool_fix_forward_ref (mp, max_mp, max_address);
6737         }
6738
6739       /* Note the insertion point if necessary.  */
6740       if (max_mp == NULL
6741           && mp->max_address > max_address)
6742         max_mp = mp;
6743
6744       /* If we are inserting an 8-bytes aligned quantity and
6745          we have not already found an insertion point, then
6746          make sure that all such 8-byte aligned quantities are
6747          placed at the start of the pool.  */
6748       if (ARM_DOUBLEWORD_ALIGN
6749           && max_mp == NULL
6750           && fix->fix_size == 8
6751           && mp->fix_size != 8)
6752         {
6753           max_mp = mp;
6754           max_address = mp->max_address;
6755         }
6756     }
6757
6758   /* The value is not currently in the minipool, so we need to create
6759      a new entry for it.  If MAX_MP is NULL, the entry will be put on
6760      the end of the list since the placement is less constrained than
6761      any existing entry.  Otherwise, we insert the new fix before
6762      MAX_MP and, if necessary, adjust the constraints on the other
6763      entries.  */
6764   mp = xmalloc (sizeof (* mp));
6765   mp->fix_size = fix->fix_size;
6766   mp->mode = fix->mode;
6767   mp->value = fix->value;
6768   mp->refcount = 1;
6769   /* Not yet required for a backwards ref.  */
6770   mp->min_address = -65536;
6771
6772   if (max_mp == NULL)
6773     {
6774       mp->max_address = max_address;
6775       mp->next = NULL;
6776       mp->prev = minipool_vector_tail;
6777
6778       if (mp->prev == NULL)
6779         {
6780           minipool_vector_head = mp;
6781           minipool_vector_label = gen_label_rtx ();
6782         }
6783       else
6784         mp->prev->next = mp;
6785
6786       minipool_vector_tail = mp;
6787     }
6788   else
6789     {
6790       if (max_address > max_mp->max_address - mp->fix_size)
6791         mp->max_address = max_mp->max_address - mp->fix_size;
6792       else
6793         mp->max_address = max_address;
6794
6795       mp->next = max_mp;
6796       mp->prev = max_mp->prev;
6797       max_mp->prev = mp;
6798       if (mp->prev != NULL)
6799         mp->prev->next = mp;
6800       else
6801         minipool_vector_head = mp;
6802     }
6803
6804   /* Save the new entry.  */
6805   max_mp = mp;
6806
6807   /* Scan over the preceding entries and adjust their addresses as
6808      required.  */
6809   while (mp->prev != NULL
6810          && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
6811     {
6812       mp->prev->max_address = mp->max_address - mp->prev->fix_size;
6813       mp = mp->prev;
6814     }
6815
6816   return max_mp;
6817 }
6818
6819 static Mnode *
6820 move_minipool_fix_backward_ref (Mnode *mp, Mnode *min_mp,
6821                                 HOST_WIDE_INT  min_address)
6822 {
6823   HOST_WIDE_INT offset;
6824
6825   /* This should never be true, and the code below assumes these are
6826      different.  */
6827   if (mp == min_mp)
6828     abort ();
6829
6830   if (min_mp == NULL)
6831     {
6832       if (min_address > mp->min_address)
6833         mp->min_address = min_address;
6834     }
6835   else
6836     {
6837       /* We will adjust this below if it is too loose.  */
6838       mp->min_address = min_address;
6839
6840       /* Unlink MP from its current position.  Since min_mp is non-null,
6841          mp->next must be non-null.  */
6842       mp->next->prev = mp->prev;
6843       if (mp->prev != NULL)
6844         mp->prev->next = mp->next;
6845       else
6846         minipool_vector_head = mp->next;
6847
6848       /* Reinsert it after MIN_MP.  */
6849       mp->prev = min_mp;
6850       mp->next = min_mp->next;
6851       min_mp->next = mp;
6852       if (mp->next != NULL)
6853         mp->next->prev = mp;
6854       else
6855         minipool_vector_tail = mp;
6856     }
6857
6858   min_mp = mp;
6859
6860   offset = 0;
6861   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
6862     {
6863       mp->offset = offset;
6864       if (mp->refcount > 0)
6865         offset += mp->fix_size;
6866
6867       if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
6868         mp->next->min_address = mp->min_address + mp->fix_size;
6869     }
6870
6871   return min_mp;
6872 }
6873
6874 /* Add a constant to the minipool for a backward reference.  Returns the
6875    node added or NULL if the constant will not fit in this pool.
6876
6877    Note that the code for insertion for a backwards reference can be
6878    somewhat confusing because the calculated offsets for each fix do
6879    not take into account the size of the pool (which is still under
6880    construction.  */
6881 static Mnode *
6882 add_minipool_backward_ref (Mfix *fix)
6883 {
6884   /* If set, min_mp is the last pool_entry that has a lower constraint
6885      than the one we are trying to add.  */
6886   Mnode *min_mp = NULL;
6887   /* This can be negative, since it is only a constraint.  */
6888   HOST_WIDE_INT  min_address = fix->address - fix->backwards;
6889   Mnode *mp;
6890
6891   /* If we can't reach the current pool from this insn, or if we can't
6892      insert this entry at the end of the pool without pushing other
6893      fixes out of range, then we don't try.  This ensures that we
6894      can't fail later on.  */
6895   if (min_address >= minipool_barrier->address
6896       || (minipool_vector_tail->min_address + fix->fix_size
6897           >= minipool_barrier->address))
6898     return NULL;
6899
6900   /* Scan the pool to see if a constant with the same value has
6901      already been added.  While we are doing this, also note the
6902      location where we must insert the constant if it doesn't already
6903      exist.  */
6904   for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
6905     {
6906       if (GET_CODE (fix->value) == GET_CODE (mp->value)
6907           && fix->mode == mp->mode
6908           && (GET_CODE (fix->value) != CODE_LABEL
6909               || (CODE_LABEL_NUMBER (fix->value)
6910                   == CODE_LABEL_NUMBER (mp->value)))
6911           && rtx_equal_p (fix->value, mp->value)
6912           /* Check that there is enough slack to move this entry to the
6913              end of the table (this is conservative).  */
6914           && (mp->max_address
6915               > (minipool_barrier->address
6916                  + minipool_vector_tail->offset
6917                  + minipool_vector_tail->fix_size)))
6918         {
6919           mp->refcount++;
6920           return move_minipool_fix_backward_ref (mp, min_mp, min_address);
6921         }
6922
6923       if (min_mp != NULL)
6924         mp->min_address += fix->fix_size;
6925       else
6926         {
6927           /* Note the insertion point if necessary.  */
6928           if (mp->min_address < min_address)
6929             {
6930               /* For now, we do not allow the insertion of 8-byte alignment
6931                  requiring nodes anywhere but at the start of the pool.  */
6932               if (ARM_DOUBLEWORD_ALIGN
6933                   && fix->fix_size == 8 && mp->fix_size != 8)
6934                 return NULL;
6935               else
6936                 min_mp = mp;
6937             }
6938           else if (mp->max_address
6939                    < minipool_barrier->address + mp->offset + fix->fix_size)
6940             {
6941               /* Inserting before this entry would push the fix beyond
6942                  its maximum address (which can happen if we have
6943                  re-located a forwards fix); force the new fix to come
6944                  after it.  */
6945               min_mp = mp;
6946               min_address = mp->min_address + fix->fix_size;
6947             }
6948           /* If we are inserting an 8-bytes aligned quantity and
6949              we have not already found an insertion point, then
6950              make sure that all such 8-byte aligned quantities are
6951              placed at the start of the pool.  */
6952           else if (ARM_DOUBLEWORD_ALIGN
6953                    && min_mp == NULL
6954                    && fix->fix_size == 8
6955                    && mp->fix_size < 8)
6956             {
6957               min_mp = mp;
6958               min_address = mp->min_address + fix->fix_size;
6959             }
6960         }
6961     }
6962
6963   /* We need to create a new entry.  */
6964   mp = xmalloc (sizeof (* mp));
6965   mp->fix_size = fix->fix_size;
6966   mp->mode = fix->mode;
6967   mp->value = fix->value;
6968   mp->refcount = 1;
6969   mp->max_address = minipool_barrier->address + 65536;
6970
6971   mp->min_address = min_address;
6972
6973   if (min_mp == NULL)
6974     {
6975       mp->prev = NULL;
6976       mp->next = minipool_vector_head;
6977
6978       if (mp->next == NULL)
6979         {
6980           minipool_vector_tail = mp;
6981           minipool_vector_label = gen_label_rtx ();
6982         }
6983       else
6984         mp->next->prev = mp;
6985
6986       minipool_vector_head = mp;
6987     }
6988   else
6989     {
6990       mp->next = min_mp->next;
6991       mp->prev = min_mp;
6992       min_mp->next = mp;
6993
6994       if (mp->next != NULL)
6995         mp->next->prev = mp;
6996       else
6997         minipool_vector_tail = mp;
6998     }
6999
7000   /* Save the new entry.  */
7001   min_mp = mp;
7002
7003   if (mp->prev)
7004     mp = mp->prev;
7005   else
7006     mp->offset = 0;
7007
7008   /* Scan over the following entries and adjust their offsets.  */
7009   while (mp->next != NULL)
7010     {
7011       if (mp->next->min_address < mp->min_address + mp->fix_size)
7012         mp->next->min_address = mp->min_address + mp->fix_size;
7013
7014       if (mp->refcount)
7015         mp->next->offset = mp->offset + mp->fix_size;
7016       else
7017         mp->next->offset = mp->offset;
7018
7019       mp = mp->next;
7020     }
7021
7022   return min_mp;
7023 }
7024
7025 static void
7026 assign_minipool_offsets (Mfix *barrier)
7027 {
7028   HOST_WIDE_INT offset = 0;
7029   Mnode *mp;
7030
7031   minipool_barrier = barrier;
7032
7033   for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7034     {
7035       mp->offset = offset;
7036
7037       if (mp->refcount > 0)
7038         offset += mp->fix_size;
7039     }
7040 }
7041
7042 /* Output the literal table */
7043 static void
7044 dump_minipool (rtx scan)
7045 {
7046   Mnode * mp;
7047   Mnode * nmp;
7048   int align64 = 0;
7049
7050   if (ARM_DOUBLEWORD_ALIGN)
7051     for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
7052       if (mp->refcount > 0 && mp->fix_size == 8)
7053         {
7054           align64 = 1;
7055           break;
7056         }
7057
7058   if (dump_file)
7059     fprintf (dump_file,
7060              ";; Emitting minipool after insn %u; address %ld; align %d (bytes)\n",
7061              INSN_UID (scan), (unsigned long) minipool_barrier->address, align64 ? 8 : 4);
7062
7063   scan = emit_label_after (gen_label_rtx (), scan);
7064   scan = emit_insn_after (align64 ? gen_align_8 () : gen_align_4 (), scan);
7065   scan = emit_label_after (minipool_vector_label, scan);
7066
7067   for (mp = minipool_vector_head; mp != NULL; mp = nmp)
7068     {
7069       if (mp->refcount > 0)
7070         {
7071           if (dump_file)
7072             {
7073               fprintf (dump_file,
7074                        ";;  Offset %u, min %ld, max %ld ",
7075                        (unsigned) mp->offset, (unsigned long) mp->min_address,
7076                        (unsigned long) mp->max_address);
7077               arm_print_value (dump_file, mp->value);
7078               fputc ('\n', dump_file);
7079             }
7080
7081           switch (mp->fix_size)
7082             {
7083 #ifdef HAVE_consttable_1
7084             case 1:
7085               scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
7086               break;
7087
7088 #endif
7089 #ifdef HAVE_consttable_2
7090             case 2:
7091               scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
7092               break;
7093
7094 #endif
7095 #ifdef HAVE_consttable_4
7096             case 4:
7097               scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
7098               break;
7099
7100 #endif
7101 #ifdef HAVE_consttable_8
7102             case 8:
7103               scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
7104               break;
7105
7106 #endif
7107             default:
7108               abort ();
7109               break;
7110             }
7111         }
7112
7113       nmp = mp->next;
7114       free (mp);
7115     }
7116
7117   minipool_vector_head = minipool_vector_tail = NULL;
7118   scan = emit_insn_after (gen_consttable_end (), scan);
7119   scan = emit_barrier_after (scan);
7120 }
7121
7122 /* Return the cost of forcibly inserting a barrier after INSN.  */
7123 static int
7124 arm_barrier_cost (rtx insn)
7125 {
7126   /* Basing the location of the pool on the loop depth is preferable,
7127      but at the moment, the basic block information seems to be
7128      corrupt by this stage of the compilation.  */
7129   int base_cost = 50;
7130   rtx next = next_nonnote_insn (insn);
7131
7132   if (next != NULL && GET_CODE (next) == CODE_LABEL)
7133     base_cost -= 20;
7134
7135   switch (GET_CODE (insn))
7136     {
7137     case CODE_LABEL:
7138       /* It will always be better to place the table before the label, rather
7139          than after it.  */
7140       return 50;
7141
7142     case INSN:
7143     case CALL_INSN:
7144       return base_cost;
7145
7146     case JUMP_INSN:
7147       return base_cost - 10;
7148
7149     default:
7150       return base_cost + 10;
7151     }
7152 }
7153
7154 /* Find the best place in the insn stream in the range
7155    (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
7156    Create the barrier by inserting a jump and add a new fix entry for
7157    it.  */
7158 static Mfix *
7159 create_fix_barrier (Mfix *fix, HOST_WIDE_INT max_address)
7160 {
7161   HOST_WIDE_INT count = 0;
7162   rtx barrier;
7163   rtx from = fix->insn;
7164   rtx selected = from;
7165   int selected_cost;
7166   HOST_WIDE_INT selected_address;
7167   Mfix * new_fix;
7168   HOST_WIDE_INT max_count = max_address - fix->address;
7169   rtx label = gen_label_rtx ();
7170
7171   selected_cost = arm_barrier_cost (from);
7172   selected_address = fix->address;
7173
7174   while (from && count < max_count)
7175     {
7176       rtx tmp;
7177       int new_cost;
7178
7179       /* This code shouldn't have been called if there was a natural barrier
7180          within range.  */
7181       if (GET_CODE (from) == BARRIER)
7182         abort ();
7183
7184       /* Count the length of this insn.  */
7185       count += get_attr_length (from);
7186
7187       /* If there is a jump table, add its length.  */
7188       tmp = is_jump_table (from);
7189       if (tmp != NULL)
7190         {
7191           count += get_jump_table_size (tmp);
7192
7193           /* Jump tables aren't in a basic block, so base the cost on
7194              the dispatch insn.  If we select this location, we will
7195              still put the pool after the table.  */
7196           new_cost = arm_barrier_cost (from);
7197
7198           if (count < max_count && new_cost <= selected_cost)
7199             {
7200               selected = tmp;
7201               selected_cost = new_cost;
7202               selected_address = fix->address + count;
7203             }
7204
7205           /* Continue after the dispatch table.  */
7206           from = NEXT_INSN (tmp);
7207           continue;
7208         }
7209
7210       new_cost = arm_barrier_cost (from);
7211
7212       if (count < max_count && new_cost <= selected_cost)
7213         {
7214           selected = from;
7215           selected_cost = new_cost;
7216           selected_address = fix->address + count;
7217         }
7218
7219       from = NEXT_INSN (from);
7220     }
7221
7222   /* Create a new JUMP_INSN that branches around a barrier.  */
7223   from = emit_jump_insn_after (gen_jump (label), selected);
7224   JUMP_LABEL (from) = label;
7225   barrier = emit_barrier_after (from);
7226   emit_label_after (label, barrier);
7227
7228   /* Create a minipool barrier entry for the new barrier.  */
7229   new_fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* new_fix));
7230   new_fix->insn = barrier;
7231   new_fix->address = selected_address;
7232   new_fix->next = fix->next;
7233   fix->next = new_fix;
7234
7235   return new_fix;
7236 }
7237
7238 /* Record that there is a natural barrier in the insn stream at
7239    ADDRESS.  */
7240 static void
7241 push_minipool_barrier (rtx insn, HOST_WIDE_INT address)
7242 {
7243   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7244
7245   fix->insn = insn;
7246   fix->address = address;
7247
7248   fix->next = NULL;
7249   if (minipool_fix_head != NULL)
7250     minipool_fix_tail->next = fix;
7251   else
7252     minipool_fix_head = fix;
7253
7254   minipool_fix_tail = fix;
7255 }
7256
7257 /* Record INSN, which will need fixing up to load a value from the
7258    minipool.  ADDRESS is the offset of the insn since the start of the
7259    function; LOC is a pointer to the part of the insn which requires
7260    fixing; VALUE is the constant that must be loaded, which is of type
7261    MODE.  */
7262 static void
7263 push_minipool_fix (rtx insn, HOST_WIDE_INT address, rtx *loc,
7264                    enum machine_mode mode, rtx value)
7265 {
7266   Mfix * fix = (Mfix *) obstack_alloc (&minipool_obstack, sizeof (* fix));
7267
7268 #ifdef AOF_ASSEMBLER
7269   /* PIC symbol references need to be converted into offsets into the
7270      based area.  */
7271   /* XXX This shouldn't be done here.  */
7272   if (flag_pic && GET_CODE (value) == SYMBOL_REF)
7273     value = aof_pic_entry (value);
7274 #endif /* AOF_ASSEMBLER */
7275
7276   fix->insn = insn;
7277   fix->address = address;
7278   fix->loc = loc;
7279   fix->mode = mode;
7280   fix->fix_size = MINIPOOL_FIX_SIZE (mode);
7281   fix->value = value;
7282   fix->forwards = get_attr_pool_range (insn);
7283   fix->backwards = get_attr_neg_pool_range (insn);
7284   fix->minipool = NULL;
7285
7286   /* If an insn doesn't have a range defined for it, then it isn't
7287      expecting to be reworked by this code.  Better to abort now than
7288      to generate duff assembly code.  */
7289   if (fix->forwards == 0 && fix->backwards == 0)
7290     abort ();
7291
7292   /* With AAPCS/iWMMXt enabled, the pool is aligned to an 8-byte boundary.
7293      So there might be an empty word before the start of the pool.
7294      Hence we reduce the forward range by 4 to allow for this
7295      possibility.  */
7296   if (ARM_DOUBLEWORD_ALIGN && fix->fix_size == 8)
7297     fix->forwards -= 4;
7298
7299   if (dump_file)
7300     {
7301       fprintf (dump_file,
7302                ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
7303                GET_MODE_NAME (mode),
7304                INSN_UID (insn), (unsigned long) address,
7305                -1 * (long)fix->backwards, (long)fix->forwards);
7306       arm_print_value (dump_file, fix->value);
7307       fprintf (dump_file, "\n");
7308     }
7309
7310   /* Add it to the chain of fixes.  */
7311   fix->next = NULL;
7312
7313   if (minipool_fix_head != NULL)
7314     minipool_fix_tail->next = fix;
7315   else
7316     minipool_fix_head = fix;
7317
7318   minipool_fix_tail = fix;
7319 }
7320
7321 /* Return the cost of synthesizing the const_double VAL inline.
7322    Returns the number of insns needed, or 99 if we don't know how to
7323    do it.  */
7324 int
7325 arm_const_double_inline_cost (rtx val)
7326 {
7327   long parts[2];
7328   
7329   if (GET_MODE (val) == DFmode)
7330     {
7331       REAL_VALUE_TYPE r;
7332       if (!TARGET_SOFT_FLOAT)
7333         return 99;
7334       REAL_VALUE_FROM_CONST_DOUBLE (r, val);
7335       REAL_VALUE_TO_TARGET_DOUBLE (r, parts);
7336     }
7337   else if (GET_MODE (val) != VOIDmode)
7338     return 99;
7339   else
7340     {
7341       parts[0] = CONST_DOUBLE_LOW (val);
7342       parts[1] = CONST_DOUBLE_HIGH (val);
7343     }
7344
7345   return (arm_gen_constant (SET, SImode, NULL_RTX, parts[0],
7346                             NULL_RTX, NULL_RTX, 0, 0)
7347           + arm_gen_constant (SET, SImode, NULL_RTX, parts[1],
7348                               NULL_RTX, NULL_RTX, 0, 0));
7349 }
7350
7351 /* Determine if a CONST_DOUBLE should be pushed to the minipool */
7352 static bool
7353 const_double_needs_minipool (rtx val)
7354 {
7355   /* thumb only knows to load a CONST_DOUBLE from memory at the moment */
7356   if (TARGET_THUMB)
7357     return true;
7358
7359   /* Don't push anything to the minipool if a CONST_DOUBLE can be built with
7360      a few ALU insns directly. On balance, the optimum is likely to be around
7361      3 insns, except when there are no load delay slots where it should be 4.
7362      When optimizing for size, a limit of 3 allows saving at least one word
7363      except for cases where a single minipool entry could be shared more than
7364      2 times which is rather unlikely to outweight the overall savings. */
7365   return (arm_const_double_inline_cost (val)
7366           > ((optimize_size || arm_ld_sched) ? 3 : 4));
7367 }
7368
7369 /* Scan INSN and note any of its operands that need fixing.
7370    If DO_PUSHES is false we do not actually push any of the fixups
7371    needed.  The function returns TRUE is any fixups were needed/pushed.
7372    This is used by arm_memory_load_p() which needs to know about loads
7373    of constants that will be converted into minipool loads.  */
7374 static bool
7375 note_invalid_constants (rtx insn, HOST_WIDE_INT address, int do_pushes)
7376 {
7377   bool result = false;
7378   int opno;
7379
7380   extract_insn (insn);
7381
7382   if (!constrain_operands (1))
7383     fatal_insn_not_found (insn);
7384
7385   if (recog_data.n_alternatives == 0)
7386     return false;
7387
7388   /* Fill in recog_op_alt with information about the constraints of this insn.  */
7389   preprocess_constraints ();
7390
7391   for (opno = 0; opno < recog_data.n_operands; opno++)
7392     {
7393       /* Things we need to fix can only occur in inputs.  */
7394       if (recog_data.operand_type[opno] != OP_IN)
7395         continue;
7396
7397       /* If this alternative is a memory reference, then any mention
7398          of constants in this alternative is really to fool reload
7399          into allowing us to accept one there.  We need to fix them up
7400          now so that we output the right code.  */
7401       if (recog_op_alt[opno][which_alternative].memory_ok)
7402         {
7403           rtx op = recog_data.operand[opno];
7404
7405           if (CONSTANT_P (op)
7406               && (GET_CODE (op) != CONST_DOUBLE
7407                   || const_double_needs_minipool (op)))
7408             {
7409               if (do_pushes)
7410                 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
7411                                    recog_data.operand_mode[opno], op);
7412               result = true;
7413             }
7414           else if (GET_CODE (op) == MEM
7415                    && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
7416                    && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
7417             {
7418               if (do_pushes)
7419                 {
7420                   rtx cop = avoid_constant_pool_reference (op);
7421
7422                   /* Casting the address of something to a mode narrower
7423                      than a word can cause avoid_constant_pool_reference()
7424                      to return the pool reference itself.  That's no good to
7425                      us here.  Lets just hope that we can use the
7426                      constant pool value directly.  */
7427                   if (op == cop)
7428                     cop = get_pool_constant (XEXP (op, 0));
7429
7430                   push_minipool_fix (insn, address,
7431                                      recog_data.operand_loc[opno],
7432                                      recog_data.operand_mode[opno], cop);
7433                 }
7434
7435               result = true;
7436             }
7437         }
7438     }
7439
7440   return result;
7441 }
7442
7443 /* Gcc puts the pool in the wrong place for ARM, since we can only
7444    load addresses a limited distance around the pc.  We do some
7445    special munging to move the constant pool values to the correct
7446    point in the code.  */
7447 static void
7448 arm_reorg (void)
7449 {
7450   rtx insn;
7451   HOST_WIDE_INT address = 0;
7452   Mfix * fix;
7453
7454   minipool_fix_head = minipool_fix_tail = NULL;
7455
7456   /* The first insn must always be a note, or the code below won't
7457      scan it properly.  */
7458   insn = get_insns ();
7459   if (GET_CODE (insn) != NOTE)
7460     abort ();
7461
7462   /* Scan all the insns and record the operands that will need fixing.  */
7463   for (insn = next_nonnote_insn (insn); insn; insn = next_nonnote_insn (insn))
7464     {
7465       if (TARGET_CIRRUS_FIX_INVALID_INSNS
7466           && (arm_cirrus_insn_p (insn)
7467               || GET_CODE (insn) == JUMP_INSN
7468               || arm_memory_load_p (insn)))
7469         cirrus_reorg (insn);
7470
7471       if (GET_CODE (insn) == BARRIER)
7472         push_minipool_barrier (insn, address);
7473       else if (INSN_P (insn))
7474         {
7475           rtx table;
7476
7477           note_invalid_constants (insn, address, true);
7478           address += get_attr_length (insn);
7479
7480           /* If the insn is a vector jump, add the size of the table
7481              and skip the table.  */
7482           if ((table = is_jump_table (insn)) != NULL)
7483             {
7484               address += get_jump_table_size (table);
7485               insn = table;
7486             }
7487         }
7488     }
7489
7490   fix = minipool_fix_head;
7491
7492   /* Now scan the fixups and perform the required changes.  */
7493   while (fix)
7494     {
7495       Mfix * ftmp;
7496       Mfix * fdel;
7497       Mfix *  last_added_fix;
7498       Mfix * last_barrier = NULL;
7499       Mfix * this_fix;
7500
7501       /* Skip any further barriers before the next fix.  */
7502       while (fix && GET_CODE (fix->insn) == BARRIER)
7503         fix = fix->next;
7504
7505       /* No more fixes.  */
7506       if (fix == NULL)
7507         break;
7508
7509       last_added_fix = NULL;
7510
7511       for (ftmp = fix; ftmp; ftmp = ftmp->next)
7512         {
7513           if (GET_CODE (ftmp->insn) == BARRIER)
7514             {
7515               if (ftmp->address >= minipool_vector_head->max_address)
7516                 break;
7517
7518               last_barrier = ftmp;
7519             }
7520           else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
7521             break;
7522
7523           last_added_fix = ftmp;  /* Keep track of the last fix added.  */
7524         }
7525
7526       /* If we found a barrier, drop back to that; any fixes that we
7527          could have reached but come after the barrier will now go in
7528          the next mini-pool.  */
7529       if (last_barrier != NULL)
7530         {
7531           /* Reduce the refcount for those fixes that won't go into this
7532              pool after all.  */
7533           for (fdel = last_barrier->next;
7534                fdel && fdel != ftmp;
7535                fdel = fdel->next)
7536             {
7537               fdel->minipool->refcount--;
7538               fdel->minipool = NULL;
7539             }
7540
7541           ftmp = last_barrier;
7542         }
7543       else
7544         {
7545           /* ftmp is first fix that we can't fit into this pool and
7546              there no natural barriers that we could use.  Insert a
7547              new barrier in the code somewhere between the previous
7548              fix and this one, and arrange to jump around it.  */
7549           HOST_WIDE_INT max_address;
7550
7551           /* The last item on the list of fixes must be a barrier, so
7552              we can never run off the end of the list of fixes without
7553              last_barrier being set.  */
7554           if (ftmp == NULL)
7555             abort ();
7556
7557           max_address = minipool_vector_head->max_address;
7558           /* Check that there isn't another fix that is in range that
7559              we couldn't fit into this pool because the pool was
7560              already too large: we need to put the pool before such an
7561              instruction.  */
7562           if (ftmp->address < max_address)
7563             max_address = ftmp->address;
7564
7565           last_barrier = create_fix_barrier (last_added_fix, max_address);
7566         }
7567
7568       assign_minipool_offsets (last_barrier);
7569
7570       while (ftmp)
7571         {
7572           if (GET_CODE (ftmp->insn) != BARRIER
7573               && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
7574                   == NULL))
7575             break;
7576
7577           ftmp = ftmp->next;
7578         }
7579
7580       /* Scan over the fixes we have identified for this pool, fixing them
7581          up and adding the constants to the pool itself.  */
7582       for (this_fix = fix; this_fix && ftmp != this_fix;
7583            this_fix = this_fix->next)
7584         if (GET_CODE (this_fix->insn) != BARRIER)
7585           {
7586             rtx addr
7587               = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
7588                                                   minipool_vector_label),
7589                                this_fix->minipool->offset);
7590             *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
7591           }
7592
7593       dump_minipool (last_barrier->insn);
7594       fix = ftmp;
7595     }
7596
7597   /* From now on we must synthesize any constants that we can't handle
7598      directly.  This can happen if the RTL gets split during final
7599      instruction generation.  */
7600   after_arm_reorg = 1;
7601
7602   /* Free the minipool memory.  */
7603   obstack_free (&minipool_obstack, minipool_startobj);
7604 }
7605 \f
7606 /* Routines to output assembly language.  */
7607
7608 /* If the rtx is the correct value then return the string of the number.
7609    In this way we can ensure that valid double constants are generated even
7610    when cross compiling.  */
7611 const char *
7612 fp_immediate_constant (rtx x)
7613 {
7614   REAL_VALUE_TYPE r;
7615   int i;
7616
7617   if (!fp_consts_inited)
7618     init_fp_table ();
7619
7620   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7621   for (i = 0; i < 8; i++)
7622     if (REAL_VALUES_EQUAL (r, values_fp[i]))
7623       return strings_fp[i];
7624
7625   abort ();
7626 }
7627
7628 /* As for fp_immediate_constant, but value is passed directly, not in rtx.  */
7629 static const char *
7630 fp_const_from_val (REAL_VALUE_TYPE *r)
7631 {
7632   int i;
7633
7634   if (!fp_consts_inited)
7635     init_fp_table ();
7636
7637   for (i = 0; i < 8; i++)
7638     if (REAL_VALUES_EQUAL (*r, values_fp[i]))
7639       return strings_fp[i];
7640
7641   abort ();
7642 }
7643
7644 /* Output the operands of a LDM/STM instruction to STREAM.
7645    MASK is the ARM register set mask of which only bits 0-15 are important.
7646    REG is the base register, either the frame pointer or the stack pointer,
7647    INSTR is the possibly suffixed load or store instruction.  */
7648 static void
7649 print_multi_reg (FILE *stream, const char *instr, int reg, int mask)
7650 {
7651   int i;
7652   int not_first = FALSE;
7653
7654   fputc ('\t', stream);
7655   asm_fprintf (stream, instr, reg);
7656   fputs (", {", stream);
7657
7658   for (i = 0; i <= LAST_ARM_REGNUM; i++)
7659     if (mask & (1 << i))
7660       {
7661         if (not_first)
7662           fprintf (stream, ", ");
7663
7664         asm_fprintf (stream, "%r", i);
7665         not_first = TRUE;
7666       }
7667
7668   fprintf (stream, "}\n");
7669 }
7670
7671
7672 /* Output a FLDMX instruction to STREAM.
7673    BASE if the register containing the address.
7674    REG and COUNT specify the register range.
7675    Extra registers may be added to avoid hardware bugs.  */
7676
7677 static void
7678 arm_output_fldmx (FILE * stream, unsigned int base, int reg, int count)
7679 {
7680   int i;
7681
7682   /* Workaround ARM10 VFPr1 bug.  */
7683   if (count == 2 && !arm_arch6)
7684     {
7685       if (reg == 15)
7686         reg--;
7687       count++;
7688     }
7689
7690   fputc ('\t', stream);
7691   asm_fprintf (stream, "fldmfdx\t%r!, {", base);
7692
7693   for (i = reg; i < reg + count; i++)
7694     {
7695       if (i > reg)
7696         fputs (", ", stream);
7697       asm_fprintf (stream, "d%d", i);
7698     }
7699   fputs ("}\n", stream);
7700
7701 }
7702
7703
7704 /* Output the assembly for a store multiple.  */
7705
7706 const char *
7707 vfp_output_fstmx (rtx * operands)
7708 {
7709   char pattern[100];
7710   int p;
7711   int base;
7712   int i;
7713
7714   strcpy (pattern, "fstmfdx\t%m0!, {%P1");
7715   p = strlen (pattern);
7716
7717   if (GET_CODE (operands[1]) != REG)
7718     abort ();
7719
7720   base = (REGNO (operands[1]) - FIRST_VFP_REGNUM) / 2;
7721   for (i = 1; i < XVECLEN (operands[2], 0); i++)
7722     {
7723       p += sprintf (&pattern[p], ", d%d", base + i);
7724     }
7725   strcpy (&pattern[p], "}");
7726
7727   output_asm_insn (pattern, operands);
7728   return "";
7729 }
7730
7731
7732 /* Emit RTL to save block of VFP register pairs to the stack.  Returns the
7733    number of bytes pushed.  */
7734
7735 static int
7736 vfp_emit_fstmx (int base_reg, int count)
7737 {
7738   rtx par;
7739   rtx dwarf;
7740   rtx tmp, reg;
7741   int i;
7742
7743   /* Workaround ARM10 VFPr1 bug.  Data corruption can occur when exactly two
7744      register pairs are stored by a store multiple insn.  We avoid this
7745      by pushing an extra pair.  */
7746   if (count == 2 && !arm_arch6)
7747     {
7748       if (base_reg == LAST_VFP_REGNUM - 3)
7749         base_reg -= 2;
7750       count++;
7751     }
7752
7753   /* ??? The frame layout is implementation defined.  We describe
7754      standard format 1 (equivalent to a FSTMD insn and unused pad word).
7755      We really need some way of representing the whole block so that the
7756      unwinder can figure it out at runtime.  */
7757   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7758   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
7759
7760   reg = gen_rtx_REG (DFmode, base_reg);
7761   base_reg += 2;
7762
7763   XVECEXP (par, 0, 0)
7764     = gen_rtx_SET (VOIDmode,
7765                    gen_rtx_MEM (BLKmode,
7766                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7767                    gen_rtx_UNSPEC (BLKmode,
7768                                    gen_rtvec (1, reg),
7769                                    UNSPEC_PUSH_MULT));
7770
7771   tmp = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
7772                      gen_rtx_PLUS (SImode, stack_pointer_rtx,
7773                                    GEN_INT (-(count * 8 + 4))));
7774   RTX_FRAME_RELATED_P (tmp) = 1;
7775   XVECEXP (dwarf, 0, 0) = tmp;
7776
7777   tmp = gen_rtx_SET (VOIDmode,
7778                      gen_rtx_MEM (DFmode, stack_pointer_rtx),
7779                      reg);
7780   RTX_FRAME_RELATED_P (tmp) = 1;
7781   XVECEXP (dwarf, 0, 1) = tmp;
7782
7783   for (i = 1; i < count; i++)
7784     {
7785       reg = gen_rtx_REG (DFmode, base_reg);
7786       base_reg += 2;
7787       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7788
7789       tmp = gen_rtx_SET (VOIDmode,
7790                          gen_rtx_MEM (DFmode,
7791                                       gen_rtx_PLUS (SImode,
7792                                                     stack_pointer_rtx,
7793                                                     GEN_INT (i * 8))),
7794                          reg);
7795       RTX_FRAME_RELATED_P (tmp) = 1;
7796       XVECEXP (dwarf, 0, i + 1) = tmp;
7797     }
7798
7799   par = emit_insn (par);
7800   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7801                                        REG_NOTES (par));
7802   RTX_FRAME_RELATED_P (par) = 1;
7803
7804   return count * 8 + 4;
7805 }
7806
7807
7808 /* Output a 'call' insn.  */
7809 const char *
7810 output_call (rtx *operands)
7811 {
7812   if (arm_arch5)
7813     abort ();           /* Patterns should call blx <reg> directly.  */
7814
7815   /* Handle calls to lr using ip (which may be clobbered in subr anyway).  */
7816   if (REGNO (operands[0]) == LR_REGNUM)
7817     {
7818       operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
7819       output_asm_insn ("mov%?\t%0, %|lr", operands);
7820     }
7821
7822   output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7823
7824   if (TARGET_INTERWORK || arm_arch4t)
7825     output_asm_insn ("bx%?\t%0", operands);
7826   else
7827     output_asm_insn ("mov%?\t%|pc, %0", operands);
7828
7829   return "";
7830 }
7831
7832 /* Output a 'call' insn that is a reference in memory.  */
7833 const char *
7834 output_call_mem (rtx *operands)
7835 {
7836   if (TARGET_INTERWORK && !arm_arch5)
7837     {
7838       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7839       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7840       output_asm_insn ("bx%?\t%|ip", operands);
7841     }
7842   else if (regno_use_in (LR_REGNUM, operands[0]))
7843     {
7844       /* LR is used in the memory address.  We load the address in the
7845          first instruction.  It's safe to use IP as the target of the
7846          load since the call will kill it anyway.  */
7847       output_asm_insn ("ldr%?\t%|ip, %0", operands);
7848       if (arm_arch5)
7849         output_asm_insn ("blx%?\t%|ip", operands);
7850       else
7851         {
7852           output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7853           if (arm_arch4t)
7854             output_asm_insn ("bx%?\t%|ip", operands);
7855           else
7856             output_asm_insn ("mov%?\t%|pc, %|ip", operands);
7857         }
7858     }
7859   else
7860     {
7861       output_asm_insn ("mov%?\t%|lr, %|pc", operands);
7862       output_asm_insn ("ldr%?\t%|pc, %0", operands);
7863     }
7864
7865   return "";
7866 }
7867
7868
7869 /* Output a move from arm registers to an fpa registers.
7870    OPERANDS[0] is an fpa register.
7871    OPERANDS[1] is the first registers of an arm register pair.  */
7872 const char *
7873 output_mov_long_double_fpa_from_arm (rtx *operands)
7874 {
7875   int arm_reg0 = REGNO (operands[1]);
7876   rtx ops[3];
7877
7878   if (arm_reg0 == IP_REGNUM)
7879     abort ();
7880
7881   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7882   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7883   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7884
7885   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
7886   output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
7887
7888   return "";
7889 }
7890
7891 /* Output a move from an fpa register to arm registers.
7892    OPERANDS[0] is the first registers of an arm register pair.
7893    OPERANDS[1] is an fpa register.  */
7894 const char *
7895 output_mov_long_double_arm_from_fpa (rtx *operands)
7896 {
7897   int arm_reg0 = REGNO (operands[0]);
7898   rtx ops[3];
7899
7900   if (arm_reg0 == IP_REGNUM)
7901     abort ();
7902
7903   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7904   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7905   ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
7906
7907   output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
7908   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
7909   return "";
7910 }
7911
7912 /* Output a move from arm registers to arm registers of a long double
7913    OPERANDS[0] is the destination.
7914    OPERANDS[1] is the source.  */
7915 const char *
7916 output_mov_long_double_arm_from_arm (rtx *operands)
7917 {
7918   /* We have to be careful here because the two might overlap.  */
7919   int dest_start = REGNO (operands[0]);
7920   int src_start = REGNO (operands[1]);
7921   rtx ops[2];
7922   int i;
7923
7924   if (dest_start < src_start)
7925     {
7926       for (i = 0; i < 3; i++)
7927         {
7928           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7929           ops[1] = gen_rtx_REG (SImode, src_start + i);
7930           output_asm_insn ("mov%?\t%0, %1", ops);
7931         }
7932     }
7933   else
7934     {
7935       for (i = 2; i >= 0; i--)
7936         {
7937           ops[0] = gen_rtx_REG (SImode, dest_start + i);
7938           ops[1] = gen_rtx_REG (SImode, src_start + i);
7939           output_asm_insn ("mov%?\t%0, %1", ops);
7940         }
7941     }
7942
7943   return "";
7944 }
7945
7946
7947 /* Output a move from arm registers to an fpa registers.
7948    OPERANDS[0] is an fpa register.
7949    OPERANDS[1] is the first registers of an arm register pair.  */
7950 const char *
7951 output_mov_double_fpa_from_arm (rtx *operands)
7952 {
7953   int arm_reg0 = REGNO (operands[1]);
7954   rtx ops[2];
7955
7956   if (arm_reg0 == IP_REGNUM)
7957     abort ();
7958
7959   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7960   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7961   output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
7962   output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
7963   return "";
7964 }
7965
7966 /* Output a move from an fpa register to arm registers.
7967    OPERANDS[0] is the first registers of an arm register pair.
7968    OPERANDS[1] is an fpa register.  */
7969 const char *
7970 output_mov_double_arm_from_fpa (rtx *operands)
7971 {
7972   int arm_reg0 = REGNO (operands[0]);
7973   rtx ops[2];
7974
7975   if (arm_reg0 == IP_REGNUM)
7976     abort ();
7977
7978   ops[0] = gen_rtx_REG (SImode, arm_reg0);
7979   ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
7980   output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
7981   output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
7982   return "";
7983 }
7984
7985 /* Output a move between double words.
7986    It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
7987    or MEM<-REG and all MEMs must be offsettable addresses.  */
7988 const char *
7989 output_move_double (rtx *operands)
7990 {
7991   enum rtx_code code0 = GET_CODE (operands[0]);
7992   enum rtx_code code1 = GET_CODE (operands[1]);
7993   rtx otherops[3];
7994
7995   if (code0 == REG)
7996     {
7997       int reg0 = REGNO (operands[0]);
7998
7999       otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
8000
8001       if (code1 == REG)
8002         {
8003           int reg1 = REGNO (operands[1]);
8004           if (reg1 == IP_REGNUM)
8005             abort ();
8006
8007           /* Ensure the second source is not overwritten.  */
8008           if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
8009             output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
8010           else
8011             output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
8012         }
8013       else if (code1 == CONST_VECTOR)
8014         {
8015           HOST_WIDE_INT hint = 0;
8016
8017           switch (GET_MODE (operands[1]))
8018             {
8019             case V2SImode:
8020               otherops[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 1)));
8021               operands[1] = GEN_INT (INTVAL (CONST_VECTOR_ELT (operands[1], 0)));
8022               break;
8023
8024             case V4HImode:
8025               if (BYTES_BIG_ENDIAN)
8026                 {
8027                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8028                   hint <<= 16;
8029                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8030                 }
8031               else
8032                 {
8033                   hint = INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8034                   hint <<= 16;
8035                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8036                 }
8037
8038               otherops[1] = GEN_INT (hint);
8039               hint = 0;
8040
8041               if (BYTES_BIG_ENDIAN)
8042                 {
8043                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8044                   hint <<= 16;
8045                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8046                 }
8047               else
8048                 {
8049                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8050                   hint <<= 16;
8051                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8052                 }
8053
8054               operands[1] = GEN_INT (hint);
8055               break;
8056
8057             case V8QImode:
8058               if (BYTES_BIG_ENDIAN)
8059                 {
8060                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8061                   hint <<= 8;
8062                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8063                   hint <<= 8;
8064                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8065                   hint <<= 8;
8066                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8067                 }
8068               else
8069                 {
8070                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 7));
8071                   hint <<= 8;
8072                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 6));
8073                   hint <<= 8;
8074                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 5));
8075                   hint <<= 8;
8076                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 4));
8077                 }
8078
8079               otherops[1] = GEN_INT (hint);
8080               hint = 0;
8081
8082               if (BYTES_BIG_ENDIAN)
8083                 {
8084                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8085                   hint <<= 8;
8086                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8087                   hint <<= 8;
8088                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8089                   hint <<= 8;
8090                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8091                 }
8092               else
8093                 {
8094                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 3));
8095                   hint <<= 8;
8096                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 2));
8097                   hint <<= 8;
8098                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 1));
8099                   hint <<= 8;
8100                   hint |= INTVAL (CONST_VECTOR_ELT (operands[1], 0));
8101                 }
8102
8103               operands[1] = GEN_INT (hint);
8104               break;
8105
8106             default:
8107               abort ();
8108             }
8109           output_mov_immediate (operands);
8110           output_mov_immediate (otherops);
8111         }
8112       else if (code1 == CONST_DOUBLE)
8113         {
8114           if (GET_MODE (operands[1]) == DFmode)
8115             {
8116               REAL_VALUE_TYPE r;
8117               long l[2];
8118
8119               REAL_VALUE_FROM_CONST_DOUBLE (r, operands[1]);
8120               REAL_VALUE_TO_TARGET_DOUBLE (r, l);
8121               otherops[1] = GEN_INT (l[1]);
8122               operands[1] = GEN_INT (l[0]);
8123             }
8124           else if (GET_MODE (operands[1]) != VOIDmode)
8125             abort ();
8126           else if (WORDS_BIG_ENDIAN)
8127             {
8128               otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8129               operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8130             }
8131           else
8132             {
8133               otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
8134               operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
8135             }
8136
8137           output_mov_immediate (operands);
8138           output_mov_immediate (otherops);
8139         }
8140       else if (code1 == CONST_INT)
8141         {
8142 #if HOST_BITS_PER_WIDE_INT > 32
8143           /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
8144              what the upper word is.  */
8145           if (WORDS_BIG_ENDIAN)
8146             {
8147               otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8148               operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8149             }
8150           else
8151             {
8152               otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
8153               operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
8154             }
8155 #else
8156           /* Sign extend the intval into the high-order word.  */
8157           if (WORDS_BIG_ENDIAN)
8158             {
8159               otherops[1] = operands[1];
8160               operands[1] = (INTVAL (operands[1]) < 0
8161                              ? constm1_rtx : const0_rtx);
8162             }
8163           else
8164             otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
8165 #endif
8166           output_mov_immediate (otherops);
8167           output_mov_immediate (operands);
8168         }
8169       else if (code1 == MEM)
8170         {
8171           switch (GET_CODE (XEXP (operands[1], 0)))
8172             {
8173             case REG:
8174               output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
8175               break;
8176
8177             case PRE_INC:
8178               if (!TARGET_LDRD)
8179                 abort (); /* Should never happen now.  */
8180               output_asm_insn ("ldr%?d\t%0, [%m1, #8]!", operands);
8181               break;
8182
8183             case PRE_DEC:
8184               output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
8185               break;
8186
8187             case POST_INC:
8188               output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
8189               break;
8190
8191             case POST_DEC:
8192               if (!TARGET_LDRD)
8193                 abort (); /* Should never happen now.  */
8194               output_asm_insn ("ldr%?d\t%0, [%m1], #-8", operands);
8195               break;
8196
8197             case PRE_MODIFY:
8198             case POST_MODIFY:
8199               otherops[0] = operands[0];
8200               otherops[1] = XEXP (XEXP (XEXP (operands[1], 0), 1), 0);
8201               otherops[2] = XEXP (XEXP (XEXP (operands[1], 0), 1), 1);
8202
8203               if (GET_CODE (XEXP (operands[1], 0)) == PRE_MODIFY)
8204                 {
8205                   if (reg_overlap_mentioned_p (otherops[0], otherops[2]))
8206                     {
8207                       /* Registers overlap so split out the increment.  */
8208                       output_asm_insn ("add%?\t%1, %1, %2", otherops);
8209                       output_asm_insn ("ldr%?d\t%0, [%1] @split", otherops);
8210                     }
8211                   else
8212                     output_asm_insn ("ldr%?d\t%0, [%1, %2]!", otherops);
8213                 }
8214               else
8215                 {
8216                   /* We only allow constant increments, so this is safe.  */
8217                   output_asm_insn ("ldr%?d\t%0, [%1], %2", otherops);
8218                 }
8219               break;
8220
8221             case LABEL_REF:
8222             case CONST:
8223               output_asm_insn ("adr%?\t%0, %1", operands);
8224               output_asm_insn ("ldm%?ia\t%0, %M0", operands);
8225               break;
8226
8227             default:
8228               if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
8229                                    GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
8230                 {
8231                   otherops[0] = operands[0];
8232                   otherops[1] = XEXP (XEXP (operands[1], 0), 0);
8233                   otherops[2] = XEXP (XEXP (operands[1], 0), 1);
8234
8235                   if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
8236                     {
8237                       if (GET_CODE (otherops[2]) == CONST_INT)
8238                         {
8239                           switch ((int) INTVAL (otherops[2]))
8240                             {
8241                             case -8:
8242                               output_asm_insn ("ldm%?db\t%1, %M0", otherops);
8243                               return "";
8244                             case -4:
8245                               output_asm_insn ("ldm%?da\t%1, %M0", otherops);
8246                               return "";
8247                             case 4:
8248                               output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
8249                               return "";
8250                             }
8251                         }
8252                       if (TARGET_LDRD
8253                           && (GET_CODE (otherops[2]) == REG
8254                               || (GET_CODE (otherops[2]) == CONST_INT
8255                                   && INTVAL (otherops[2]) > -256
8256                                   && INTVAL (otherops[2]) < 256)))
8257                         {
8258                           if (reg_overlap_mentioned_p (otherops[0],
8259                                                        otherops[2]))
8260                             {
8261                               /* Swap base and index registers over to
8262                                  avoid a conflict.  */
8263                               otherops[1] = XEXP (XEXP (operands[1], 0), 1);
8264                               otherops[2] = XEXP (XEXP (operands[1], 0), 0);
8265
8266                             }
8267                           /* If both registers conflict, it will usually
8268                              have been fixed by a splitter.  */
8269                           if (reg_overlap_mentioned_p (otherops[0],
8270                                                         otherops[2]))
8271                             {
8272                               output_asm_insn ("add%?\t%1, %1, %2", otherops);
8273                               output_asm_insn ("ldr%?d\t%0, [%1]",
8274                                                otherops);
8275                               return "";
8276                             }
8277                           else
8278                             {
8279                               output_asm_insn ("ldr%?d\t%0, [%1, %2]",
8280                                                otherops);
8281                               return "";
8282                             }
8283                         }
8284                       if (GET_CODE (otherops[2]) == CONST_INT)
8285                         {
8286                           if (!(const_ok_for_arm (INTVAL (otherops[2]))))
8287                             output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
8288                           else
8289                             output_asm_insn ("add%?\t%0, %1, %2", otherops);
8290                         }
8291                       else
8292                         output_asm_insn ("add%?\t%0, %1, %2", otherops);
8293                     }
8294                   else
8295                     output_asm_insn ("sub%?\t%0, %1, %2", otherops);
8296
8297                   return "ldm%?ia\t%0, %M0";
8298                 }
8299               else
8300                 {
8301                   otherops[1] = adjust_address (operands[1], SImode, 4);
8302                   /* Take care of overlapping base/data reg.  */
8303                   if (reg_mentioned_p (operands[0], operands[1]))
8304                     {
8305                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8306                       output_asm_insn ("ldr%?\t%0, %1", operands);
8307                     }
8308                   else
8309                     {
8310                       output_asm_insn ("ldr%?\t%0, %1", operands);
8311                       output_asm_insn ("ldr%?\t%0, %1", otherops);
8312                     }
8313                 }
8314             }
8315         }
8316       else
8317         abort ();  /* Constraints should prevent this.  */
8318     }
8319   else if (code0 == MEM && code1 == REG)
8320     {
8321       if (REGNO (operands[1]) == IP_REGNUM)
8322         abort ();
8323
8324       switch (GET_CODE (XEXP (operands[0], 0)))
8325         {
8326         case REG:
8327           output_asm_insn ("stm%?ia\t%m0, %M1", operands);
8328           break;
8329
8330         case PRE_INC:
8331           if (!TARGET_LDRD)
8332             abort (); /* Should never happen now.  */
8333           output_asm_insn ("str%?d\t%1, [%m0, #8]!", operands);
8334           break;
8335
8336         case PRE_DEC:
8337           output_asm_insn ("stm%?db\t%m0!, %M1", operands);
8338           break;
8339
8340         case POST_INC:
8341           output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
8342           break;
8343
8344         case POST_DEC:
8345           if (!TARGET_LDRD)
8346             abort (); /* Should never happen now.  */
8347           output_asm_insn ("str%?d\t%1, [%m0], #-8", operands);
8348           break;
8349
8350         case PRE_MODIFY:
8351         case POST_MODIFY:
8352           otherops[0] = operands[1];
8353           otherops[1] = XEXP (XEXP (XEXP (operands[0], 0), 1), 0);
8354           otherops[2] = XEXP (XEXP (XEXP (operands[0], 0), 1), 1);
8355
8356           if (GET_CODE (XEXP (operands[0], 0)) == PRE_MODIFY)
8357             output_asm_insn ("str%?d\t%0, [%1, %2]!", otherops);
8358           else
8359             output_asm_insn ("str%?d\t%0, [%1], %2", otherops);
8360           break;
8361
8362         case PLUS:
8363           otherops[2] = XEXP (XEXP (operands[0], 0), 1);
8364           if (GET_CODE (otherops[2]) == CONST_INT)
8365             {
8366               switch ((int) INTVAL (XEXP (XEXP (operands[0], 0), 1)))
8367                 {
8368                 case -8:
8369                   output_asm_insn ("stm%?db\t%m0, %M1", operands);
8370                   return "";
8371
8372                 case -4:
8373                   output_asm_insn ("stm%?da\t%m0, %M1", operands);
8374                   return "";
8375
8376                 case 4:
8377                   output_asm_insn ("stm%?ib\t%m0, %M1", operands);
8378                   return "";
8379                 }
8380             }
8381           if (TARGET_LDRD
8382               && (GET_CODE (otherops[2]) == REG
8383                   || (GET_CODE (otherops[2]) == CONST_INT
8384                       && INTVAL (otherops[2]) > -256
8385                       && INTVAL (otherops[2]) < 256)))
8386             {
8387               otherops[0] = operands[1];
8388               otherops[1] = XEXP (XEXP (operands[0], 0), 0);
8389               output_asm_insn ("str%?d\t%0, [%1, %2]", otherops);
8390               return "";
8391             }
8392           /* Fall through */
8393
8394         default:
8395           otherops[0] = adjust_address (operands[0], SImode, 4);
8396           otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
8397           output_asm_insn ("str%?\t%1, %0", operands);
8398           output_asm_insn ("str%?\t%1, %0", otherops);
8399         }
8400     }
8401   else
8402     /* Constraints should prevent this.  */
8403     abort ();
8404
8405   return "";
8406 }
8407
8408
8409 /* Output an arbitrary MOV reg, #n.
8410    OPERANDS[0] is a register.  OPERANDS[1] is a const_int.  */
8411 const char *
8412 output_mov_immediate (rtx *operands)
8413 {
8414   HOST_WIDE_INT n = INTVAL (operands[1]);
8415
8416   /* Try to use one MOV.  */
8417   if (const_ok_for_arm (n))
8418     output_asm_insn ("mov%?\t%0, %1", operands);
8419
8420   /* Try to use one MVN.  */
8421   else if (const_ok_for_arm (~n))
8422     {
8423       operands[1] = GEN_INT (~n);
8424       output_asm_insn ("mvn%?\t%0, %1", operands);
8425     }
8426   else
8427     {
8428       int n_ones = 0;
8429       int i;
8430
8431       /* If all else fails, make it out of ORRs or BICs as appropriate.  */
8432       for (i = 0; i < 32; i++)
8433         if (n & 1 << i)
8434           n_ones++;
8435
8436       if (n_ones > 16)  /* Shorter to use MVN with BIC in this case.  */
8437         output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1, ~ n);
8438       else
8439         output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
8440     }
8441
8442   return "";
8443 }
8444
8445 /* Output an ADD r, s, #n where n may be too big for one instruction.
8446    If adding zero to one register, output nothing.  */
8447 const char *
8448 output_add_immediate (rtx *operands)
8449 {
8450   HOST_WIDE_INT n = INTVAL (operands[2]);
8451
8452   if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
8453     {
8454       if (n < 0)
8455         output_multi_immediate (operands,
8456                                 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
8457                                 -n);
8458       else
8459         output_multi_immediate (operands,
8460                                 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
8461                                 n);
8462     }
8463
8464   return "";
8465 }
8466
8467 /* Output a multiple immediate operation.
8468    OPERANDS is the vector of operands referred to in the output patterns.
8469    INSTR1 is the output pattern to use for the first constant.
8470    INSTR2 is the output pattern to use for subsequent constants.
8471    IMMED_OP is the index of the constant slot in OPERANDS.
8472    N is the constant value.  */
8473 static const char *
8474 output_multi_immediate (rtx *operands, const char *instr1, const char *instr2,
8475                         int immed_op, HOST_WIDE_INT n)
8476 {
8477 #if HOST_BITS_PER_WIDE_INT > 32
8478   n &= 0xffffffff;
8479 #endif
8480
8481   if (n == 0)
8482     {
8483       /* Quick and easy output.  */
8484       operands[immed_op] = const0_rtx;
8485       output_asm_insn (instr1, operands);
8486     }
8487   else
8488     {
8489       int i;
8490       const char * instr = instr1;
8491
8492       /* Note that n is never zero here (which would give no output).  */
8493       for (i = 0; i < 32; i += 2)
8494         {
8495           if (n & (3 << i))
8496             {
8497               operands[immed_op] = GEN_INT (n & (255 << i));
8498               output_asm_insn (instr, operands);
8499               instr = instr2;
8500               i += 6;
8501             }
8502         }
8503     }
8504
8505   return "";
8506 }
8507
8508 /* Return the appropriate ARM instruction for the operation code.
8509    The returned result should not be overwritten.  OP is the rtx of the
8510    operation.  SHIFT_FIRST_ARG is TRUE if the first argument of the operator
8511    was shifted.  */
8512 const char *
8513 arithmetic_instr (rtx op, int shift_first_arg)
8514 {
8515   switch (GET_CODE (op))
8516     {
8517     case PLUS:
8518       return "add";
8519
8520     case MINUS:
8521       return shift_first_arg ? "rsb" : "sub";
8522
8523     case IOR:
8524       return "orr";
8525
8526     case XOR:
8527       return "eor";
8528
8529     case AND:
8530       return "and";
8531
8532     default:
8533       abort ();
8534     }
8535 }
8536
8537 /* Ensure valid constant shifts and return the appropriate shift mnemonic
8538    for the operation code.  The returned result should not be overwritten.
8539    OP is the rtx code of the shift.
8540    On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
8541    shift.  */
8542 static const char *
8543 shift_op (rtx op, HOST_WIDE_INT *amountp)
8544 {
8545   const char * mnem;
8546   enum rtx_code code = GET_CODE (op);
8547
8548   if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
8549     *amountp = -1;
8550   else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
8551     *amountp = INTVAL (XEXP (op, 1));
8552   else
8553     abort ();
8554
8555   switch (code)
8556     {
8557     case ASHIFT:
8558       mnem = "asl";
8559       break;
8560
8561     case ASHIFTRT:
8562       mnem = "asr";
8563       break;
8564
8565     case LSHIFTRT:
8566       mnem = "lsr";
8567       break;
8568
8569     case ROTATE:
8570       if (*amountp == -1)
8571         abort ();
8572       *amountp = 32 - *amountp;
8573
8574       /* Fall through.  */
8575
8576     case ROTATERT:
8577       mnem = "ror";
8578       break;
8579
8580     case MULT:
8581       /* We never have to worry about the amount being other than a
8582          power of 2, since this case can never be reloaded from a reg.  */
8583       if (*amountp != -1)
8584         *amountp = int_log2 (*amountp);
8585       else
8586         abort ();
8587       return "asl";
8588
8589     default:
8590       abort ();
8591     }
8592
8593   if (*amountp != -1)
8594     {
8595       /* This is not 100% correct, but follows from the desire to merge
8596          multiplication by a power of 2 with the recognizer for a
8597          shift.  >=32 is not a valid shift for "asl", so we must try and
8598          output a shift that produces the correct arithmetical result.
8599          Using lsr #32 is identical except for the fact that the carry bit
8600          is not set correctly if we set the flags; but we never use the
8601          carry bit from such an operation, so we can ignore that.  */
8602       if (code == ROTATERT)
8603         /* Rotate is just modulo 32.  */
8604         *amountp &= 31;
8605       else if (*amountp != (*amountp & 31))
8606         {
8607           if (code == ASHIFT)
8608             mnem = "lsr";
8609           *amountp = 32;
8610         }
8611
8612       /* Shifts of 0 are no-ops.  */
8613       if (*amountp == 0)
8614         return NULL;
8615     }
8616
8617   return mnem;
8618 }
8619
8620 /* Obtain the shift from the POWER of two.  */
8621
8622 static HOST_WIDE_INT
8623 int_log2 (HOST_WIDE_INT power)
8624 {
8625   HOST_WIDE_INT shift = 0;
8626
8627   while ((((HOST_WIDE_INT) 1 << shift) & power) == 0)
8628     {
8629       if (shift > 31)
8630         abort ();
8631       shift++;
8632     }
8633
8634   return shift;
8635 }
8636
8637 /* Output a .ascii pseudo-op, keeping track of lengths.  This is because
8638    /bin/as is horribly restrictive.  */
8639 #define MAX_ASCII_LEN 51
8640
8641 void
8642 output_ascii_pseudo_op (FILE *stream, const unsigned char *p, int len)
8643 {
8644   int i;
8645   int len_so_far = 0;
8646
8647   fputs ("\t.ascii\t\"", stream);
8648
8649   for (i = 0; i < len; i++)
8650     {
8651       int c = p[i];
8652
8653       if (len_so_far >= MAX_ASCII_LEN)
8654         {
8655           fputs ("\"\n\t.ascii\t\"", stream);
8656           len_so_far = 0;
8657         }
8658
8659       switch (c)
8660         {
8661         case TARGET_TAB:
8662           fputs ("\\t", stream);
8663           len_so_far += 2;
8664           break;
8665
8666         case TARGET_FF:
8667           fputs ("\\f", stream);
8668           len_so_far += 2;
8669           break;
8670
8671         case TARGET_BS:
8672           fputs ("\\b", stream);
8673           len_so_far += 2;
8674           break;
8675
8676         case TARGET_CR:
8677           fputs ("\\r", stream);
8678           len_so_far += 2;
8679           break;
8680
8681         case TARGET_NEWLINE:
8682           fputs ("\\n", stream);
8683           c = p [i + 1];
8684           if ((c >= ' ' && c <= '~')
8685               || c == TARGET_TAB)
8686             /* This is a good place for a line break.  */
8687             len_so_far = MAX_ASCII_LEN;
8688           else
8689             len_so_far += 2;
8690           break;
8691
8692         case '\"':
8693         case '\\':
8694           putc ('\\', stream);
8695           len_so_far++;
8696           /* Drop through.  */
8697
8698         default:
8699           if (c >= ' ' && c <= '~')
8700             {
8701               putc (c, stream);
8702               len_so_far++;
8703             }
8704           else
8705             {
8706               fprintf (stream, "\\%03o", c);
8707               len_so_far += 4;
8708             }
8709           break;
8710         }
8711     }
8712
8713   fputs ("\"\n", stream);
8714 }
8715 \f
8716 /* Compute the register save mask for registers 0 through 12
8717    inclusive.  This code is used by arm_compute_save_reg_mask.  */
8718 static unsigned long
8719 arm_compute_save_reg0_reg12_mask (void)
8720 {
8721   unsigned long func_type = arm_current_func_type ();
8722   unsigned int save_reg_mask = 0;
8723   unsigned int reg;
8724
8725   if (IS_INTERRUPT (func_type))
8726     {
8727       unsigned int max_reg;
8728       /* Interrupt functions must not corrupt any registers,
8729          even call clobbered ones.  If this is a leaf function
8730          we can just examine the registers used by the RTL, but
8731          otherwise we have to assume that whatever function is
8732          called might clobber anything, and so we have to save
8733          all the call-clobbered registers as well.  */
8734       if (ARM_FUNC_TYPE (func_type) == ARM_FT_FIQ)
8735         /* FIQ handlers have registers r8 - r12 banked, so
8736            we only need to check r0 - r7, Normal ISRs only
8737            bank r14 and r15, so we must check up to r12.
8738            r13 is the stack pointer which is always preserved,
8739            so we do not need to consider it here.  */
8740         max_reg = 7;
8741       else
8742         max_reg = 12;
8743
8744       for (reg = 0; reg <= max_reg; reg++)
8745         if (regs_ever_live[reg]
8746             || (! current_function_is_leaf && call_used_regs [reg]))
8747           save_reg_mask |= (1 << reg);
8748
8749       /* Also save the pic base register if necessary.  */
8750       if (flag_pic
8751           && !TARGET_SINGLE_PIC_BASE
8752           && current_function_uses_pic_offset_table)
8753         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8754     }
8755   else
8756     {
8757       /* In the normal case we only need to save those registers
8758          which are call saved and which are used by this function.  */
8759       for (reg = 0; reg <= 10; reg++)
8760         if (regs_ever_live[reg] && ! call_used_regs [reg])
8761           save_reg_mask |= (1 << reg);
8762
8763       /* Handle the frame pointer as a special case.  */
8764       if (! TARGET_APCS_FRAME
8765           && ! frame_pointer_needed
8766           && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
8767           && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
8768         save_reg_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
8769
8770       /* If we aren't loading the PIC register,
8771          don't stack it even though it may be live.  */
8772       if (flag_pic
8773           && !TARGET_SINGLE_PIC_BASE 
8774           && (regs_ever_live[PIC_OFFSET_TABLE_REGNUM]
8775               || current_function_uses_pic_offset_table))
8776         save_reg_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
8777     }
8778
8779   /* Save registers so the exception handler can modify them.  */
8780   if (current_function_calls_eh_return)
8781     {
8782       unsigned int i;
8783
8784       for (i = 0; ; i++)
8785         {
8786           reg = EH_RETURN_DATA_REGNO (i);
8787           if (reg == INVALID_REGNUM)
8788             break;
8789           save_reg_mask |= 1 << reg;
8790         }
8791     }
8792
8793   return save_reg_mask;
8794 }
8795
8796 /* Compute a bit mask of which registers need to be
8797    saved on the stack for the current function.  */
8798
8799 static unsigned long
8800 arm_compute_save_reg_mask (void)
8801 {
8802   unsigned int save_reg_mask = 0;
8803   unsigned long func_type = arm_current_func_type ();
8804
8805   if (IS_NAKED (func_type))
8806     /* This should never really happen.  */
8807     return 0;
8808
8809   /* If we are creating a stack frame, then we must save the frame pointer,
8810      IP (which will hold the old stack pointer), LR and the PC.  */
8811   if (frame_pointer_needed)
8812     save_reg_mask |=
8813       (1 << ARM_HARD_FRAME_POINTER_REGNUM)
8814       | (1 << IP_REGNUM)
8815       | (1 << LR_REGNUM)
8816       | (1 << PC_REGNUM);
8817
8818   /* Volatile functions do not return, so there
8819      is no need to save any other registers.  */
8820   if (IS_VOLATILE (func_type))
8821     return save_reg_mask;
8822
8823   save_reg_mask |= arm_compute_save_reg0_reg12_mask ();
8824
8825   /* Decide if we need to save the link register.
8826      Interrupt routines have their own banked link register,
8827      so they never need to save it.
8828      Otherwise if we do not use the link register we do not need to save
8829      it.  If we are pushing other registers onto the stack however, we
8830      can save an instruction in the epilogue by pushing the link register
8831      now and then popping it back into the PC.  This incurs extra memory
8832      accesses though, so we only do it when optimizing for size, and only
8833      if we know that we will not need a fancy return sequence.  */
8834   if (regs_ever_live [LR_REGNUM]
8835           || (save_reg_mask
8836               && optimize_size
8837               && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
8838               && !current_function_calls_eh_return))
8839     save_reg_mask |= 1 << LR_REGNUM;
8840
8841   if (cfun->machine->lr_save_eliminated)
8842     save_reg_mask &= ~ (1 << LR_REGNUM);
8843
8844   if (TARGET_REALLY_IWMMXT
8845       && ((bit_count (save_reg_mask)
8846            + ARM_NUM_INTS (current_function_pretend_args_size)) % 2) != 0)
8847     {
8848       unsigned int reg;
8849
8850       /* The total number of registers that are going to be pushed
8851          onto the stack is odd.  We need to ensure that the stack
8852          is 64-bit aligned before we start to save iWMMXt registers,
8853          and also before we start to create locals.  (A local variable
8854          might be a double or long long which we will load/store using
8855          an iWMMXt instruction).  Therefore we need to push another
8856          ARM register, so that the stack will be 64-bit aligned.  We
8857          try to avoid using the arg registers (r0 -r3) as they might be
8858          used to pass values in a tail call.  */
8859       for (reg = 4; reg <= 12; reg++)
8860         if ((save_reg_mask & (1 << reg)) == 0)
8861           break;
8862
8863       if (reg <= 12)
8864         save_reg_mask |= (1 << reg);
8865       else
8866         {
8867           cfun->machine->sibcall_blocked = 1;
8868           save_reg_mask |= (1 << 3);
8869         }
8870     }
8871
8872   return save_reg_mask;
8873 }
8874
8875
8876 /* Compute a bit mask of which registers need to be
8877    saved on the stack for the current function.  */
8878 static unsigned long
8879 thumb_compute_save_reg_mask (void)
8880 {
8881   unsigned long mask;
8882   int reg;
8883
8884   mask = 0;
8885   for (reg = 0; reg < 12; reg ++)
8886     {
8887       if (regs_ever_live[reg] && !call_used_regs[reg])
8888         mask |= 1 << reg;
8889     }
8890
8891   if (flag_pic && !TARGET_SINGLE_PIC_BASE)
8892     mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
8893   if (TARGET_SINGLE_PIC_BASE)
8894     mask &= ~(1 << arm_pic_register);
8895   /* See if we might need r11 for calls to _interwork_r11_call_via_rN().  */
8896   if (!frame_pointer_needed && CALLER_INTERWORKING_SLOT_SIZE > 0)
8897     mask |= 1 << ARM_HARD_FRAME_POINTER_REGNUM;
8898
8899   /* lr will also be pushed if any lo regs are pushed.  */
8900   if (mask & 0xff || thumb_force_lr_save ())
8901     mask |= (1 << LR_REGNUM);
8902
8903   /* Make sure we have a low work register if we need one.  */
8904   if (((mask & 0xff) == 0 && regs_ever_live[LAST_ARG_REGNUM])
8905       && ((mask & 0x0f00) || TARGET_BACKTRACE))
8906     mask |= 1 << LAST_LO_REGNUM;
8907
8908   return mask;
8909 }
8910
8911
8912 /* Return the number of bytes required to save VFP registers.  */
8913 static int
8914 arm_get_vfp_saved_size (void)
8915 {
8916   unsigned int regno;
8917   int count;
8918   int saved;
8919
8920   saved = 0;
8921   /* Space for saved VFP registers.  */
8922   if (TARGET_HARD_FLOAT && TARGET_VFP)
8923     {
8924       count = 0;
8925       for (regno = FIRST_VFP_REGNUM;
8926            regno < LAST_VFP_REGNUM;
8927            regno += 2)
8928         {
8929           if ((!regs_ever_live[regno] || call_used_regs[regno])
8930               && (!regs_ever_live[regno + 1] || call_used_regs[regno + 1]))
8931             {
8932               if (count > 0)
8933                 {
8934                   /* Workaround ARM10 VFPr1 bug.  */
8935                   if (count == 2 && !arm_arch6)
8936                     count++;
8937                   saved += count * 8 + 4;
8938                 }
8939               count = 0;
8940             }
8941           else
8942             count++;
8943         }
8944       if (count > 0)
8945         {
8946           if (count == 2 && !arm_arch6)
8947             count++;
8948           saved += count * 8 + 4;
8949         }
8950     }
8951   return saved;
8952 }
8953
8954
8955 /* Generate a function exit sequence.  If REALLY_RETURN is false, then do
8956    everything bar the final return instruction.  */
8957 const char *
8958 output_return_instruction (rtx operand, int really_return, int reverse)
8959 {
8960   char conditional[10];
8961   char instr[100];
8962   int reg;
8963   unsigned long live_regs_mask;
8964   unsigned long func_type;
8965   arm_stack_offsets *offsets;
8966
8967   func_type = arm_current_func_type ();
8968
8969   if (IS_NAKED (func_type))
8970     return "";
8971
8972   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
8973     {
8974       /* If this function was declared non-returning, and we have
8975          found a tail call, then we have to trust that the called
8976          function won't return.  */
8977       if (really_return)
8978         {
8979           rtx ops[2];
8980
8981           /* Otherwise, trap an attempted return by aborting.  */
8982           ops[0] = operand;
8983           ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
8984                                        : "abort");
8985           assemble_external_libcall (ops[1]);
8986           output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
8987         }
8988
8989       return "";
8990     }
8991
8992   if (current_function_calls_alloca && !really_return)
8993     abort ();
8994
8995   sprintf (conditional, "%%?%%%c0", reverse ? 'D' : 'd');
8996
8997   return_used_this_function = 1;
8998
8999   live_regs_mask = arm_compute_save_reg_mask ();
9000
9001   if (live_regs_mask)
9002     {
9003       const char * return_reg;
9004
9005       /* If we do not have any special requirements for function exit
9006          (e.g. interworking, or ISR) then we can load the return address
9007          directly into the PC.  Otherwise we must load it into LR.  */
9008       if (really_return
9009           && ! TARGET_INTERWORK)
9010         return_reg = reg_names[PC_REGNUM];
9011       else
9012         return_reg = reg_names[LR_REGNUM];
9013
9014       if ((live_regs_mask & (1 << IP_REGNUM)) == (1 << IP_REGNUM))
9015         {
9016           /* There are three possible reasons for the IP register
9017              being saved.  1) a stack frame was created, in which case
9018              IP contains the old stack pointer, or 2) an ISR routine
9019              corrupted it, or 3) it was saved to align the stack on
9020              iWMMXt.  In case 1, restore IP into SP, otherwise just
9021              restore IP.  */
9022           if (frame_pointer_needed)
9023             {
9024               live_regs_mask &= ~ (1 << IP_REGNUM);
9025               live_regs_mask |=   (1 << SP_REGNUM);
9026             }
9027           else
9028             {
9029               if (! IS_INTERRUPT (func_type)
9030                   && ! TARGET_REALLY_IWMMXT)
9031                 abort ();
9032             }
9033         }
9034
9035       /* On some ARM architectures it is faster to use LDR rather than
9036          LDM to load a single register.  On other architectures, the
9037          cost is the same.  In 26 bit mode, or for exception handlers,
9038          we have to use LDM to load the PC so that the CPSR is also
9039          restored.  */
9040       for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9041         {
9042           if (live_regs_mask == (unsigned int)(1 << reg))
9043             break;
9044         }
9045       if (reg <= LAST_ARM_REGNUM
9046           && (reg != LR_REGNUM
9047               || ! really_return
9048               || ! IS_INTERRUPT (func_type)))
9049         {
9050           sprintf (instr, "ldr%s\t%%|%s, [%%|sp], #4", conditional,
9051                    (reg == LR_REGNUM) ? return_reg : reg_names[reg]);
9052         }
9053       else
9054         {
9055           char *p;
9056           int first = 1;
9057
9058           /* Generate the load multiple instruction to restore the
9059              registers.  Note we can get here, even if
9060              frame_pointer_needed is true, but only if sp already
9061              points to the base of the saved core registers.  */
9062           if (live_regs_mask & (1 << SP_REGNUM))
9063             {
9064               unsigned HOST_WIDE_INT stack_adjust;
9065
9066               offsets = arm_get_frame_offsets ();
9067               stack_adjust = offsets->outgoing_args - offsets->saved_regs;
9068               if (stack_adjust != 0 && stack_adjust != 4)
9069                 abort ();
9070
9071               if (stack_adjust && arm_arch5)
9072                 sprintf (instr, "ldm%sib\t%%|sp, {", conditional);
9073               else
9074                 {
9075                   /* If we can't use ldmib (SA110 bug), then try to pop r3
9076                      instead.  */
9077                   if (stack_adjust)
9078                     live_regs_mask |= 1 << 3;
9079                   sprintf (instr, "ldm%sfd\t%%|sp, {", conditional);
9080                 }
9081             }
9082           else
9083             sprintf (instr, "ldm%sfd\t%%|sp!, {", conditional);
9084
9085           p = instr + strlen (instr);
9086
9087           for (reg = 0; reg <= SP_REGNUM; reg++)
9088             if (live_regs_mask & (1 << reg))
9089               {
9090                 int l = strlen (reg_names[reg]);
9091
9092                 if (first)
9093                   first = 0;
9094                 else
9095                   {
9096                     memcpy (p, ", ", 2);
9097                     p += 2;
9098                   }
9099
9100                 memcpy (p, "%|", 2);
9101                 memcpy (p + 2, reg_names[reg], l);
9102                 p += l + 2;
9103               }
9104
9105           if (live_regs_mask & (1 << LR_REGNUM))
9106             {
9107               sprintf (p, "%s%%|%s}", first ? "" : ", ", return_reg);
9108               /* If returning from an interrupt, restore the CPSR.  */
9109               if (IS_INTERRUPT (func_type))
9110                 strcat (p, "^");
9111             }
9112           else
9113             strcpy (p, "}");
9114         }
9115
9116       output_asm_insn (instr, & operand);
9117
9118       /* See if we need to generate an extra instruction to
9119          perform the actual function return.  */
9120       if (really_return
9121           && func_type != ARM_FT_INTERWORKED
9122           && (live_regs_mask & (1 << LR_REGNUM)) != 0)
9123         {
9124           /* The return has already been handled
9125              by loading the LR into the PC.  */
9126           really_return = 0;
9127         }
9128     }
9129
9130   if (really_return)
9131     {
9132       switch ((int) ARM_FUNC_TYPE (func_type))
9133         {
9134         case ARM_FT_ISR:
9135         case ARM_FT_FIQ:
9136           sprintf (instr, "sub%ss\t%%|pc, %%|lr, #4", conditional);
9137           break;
9138
9139         case ARM_FT_INTERWORKED:
9140           sprintf (instr, "bx%s\t%%|lr", conditional);
9141           break;
9142
9143         case ARM_FT_EXCEPTION:
9144           sprintf (instr, "mov%ss\t%%|pc, %%|lr", conditional);
9145           break;
9146
9147         default:
9148           /* Use bx if it's available.  */
9149           if (arm_arch5 || arm_arch4t)
9150             sprintf (instr, "bx%s\t%%|lr", conditional);
9151           else
9152             sprintf (instr, "mov%s\t%%|pc, %%|lr", conditional);
9153           break;
9154         }
9155
9156       output_asm_insn (instr, & operand);
9157     }
9158
9159   return "";
9160 }
9161
9162 /* Write the function name into the code section, directly preceding
9163    the function prologue.
9164
9165    Code will be output similar to this:
9166      t0
9167          .ascii "arm_poke_function_name", 0
9168          .align
9169      t1
9170          .word 0xff000000 + (t1 - t0)
9171      arm_poke_function_name
9172          mov     ip, sp
9173          stmfd   sp!, {fp, ip, lr, pc}
9174          sub     fp, ip, #4
9175
9176    When performing a stack backtrace, code can inspect the value
9177    of 'pc' stored at 'fp' + 0.  If the trace function then looks
9178    at location pc - 12 and the top 8 bits are set, then we know
9179    that there is a function name embedded immediately preceding this
9180    location and has length ((pc[-3]) & 0xff000000).
9181
9182    We assume that pc is declared as a pointer to an unsigned long.
9183
9184    It is of no benefit to output the function name if we are assembling
9185    a leaf function.  These function types will not contain a stack
9186    backtrace structure, therefore it is not possible to determine the
9187    function name.  */
9188 void
9189 arm_poke_function_name (FILE *stream, const char *name)
9190 {
9191   unsigned long alignlength;
9192   unsigned long length;
9193   rtx           x;
9194
9195   length      = strlen (name) + 1;
9196   alignlength = ROUND_UP_WORD (length);
9197
9198   ASM_OUTPUT_ASCII (stream, name, length);
9199   ASM_OUTPUT_ALIGN (stream, 2);
9200   x = GEN_INT ((unsigned HOST_WIDE_INT) 0xff000000 + alignlength);
9201   assemble_aligned_integer (UNITS_PER_WORD, x);
9202 }
9203
9204 /* Place some comments into the assembler stream
9205    describing the current function.  */
9206 static void
9207 arm_output_function_prologue (FILE *f, HOST_WIDE_INT frame_size)
9208 {
9209   unsigned long func_type;
9210
9211   if (!TARGET_ARM)
9212     {
9213       thumb_output_function_prologue (f, frame_size);
9214       return;
9215     }
9216
9217   /* Sanity check.  */
9218   if (arm_ccfsm_state || arm_target_insn)
9219     abort ();
9220
9221   func_type = arm_current_func_type ();
9222
9223   switch ((int) ARM_FUNC_TYPE (func_type))
9224     {
9225     default:
9226     case ARM_FT_NORMAL:
9227       break;
9228     case ARM_FT_INTERWORKED:
9229       asm_fprintf (f, "\t%@ Function supports interworking.\n");
9230       break;
9231     case ARM_FT_ISR:
9232       asm_fprintf (f, "\t%@ Interrupt Service Routine.\n");
9233       break;
9234     case ARM_FT_FIQ:
9235       asm_fprintf (f, "\t%@ Fast Interrupt Service Routine.\n");
9236       break;
9237     case ARM_FT_EXCEPTION:
9238       asm_fprintf (f, "\t%@ ARM Exception Handler.\n");
9239       break;
9240     }
9241
9242   if (IS_NAKED (func_type))
9243     asm_fprintf (f, "\t%@ Naked Function: prologue and epilogue provided by programmer.\n");
9244
9245   if (IS_VOLATILE (func_type))
9246     asm_fprintf (f, "\t%@ Volatile: function does not return.\n");
9247
9248   if (IS_NESTED (func_type))
9249     asm_fprintf (f, "\t%@ Nested: function declared inside another function.\n");
9250
9251   asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %wd\n",
9252                current_function_args_size,
9253                current_function_pretend_args_size, frame_size);
9254
9255   asm_fprintf (f, "\t%@ frame_needed = %d, uses_anonymous_args = %d\n",
9256                frame_pointer_needed,
9257                cfun->machine->uses_anonymous_args);
9258
9259   if (cfun->machine->lr_save_eliminated)
9260     asm_fprintf (f, "\t%@ link register save eliminated.\n");
9261
9262   if (current_function_calls_eh_return)
9263     asm_fprintf (f, "\t@ Calls __builtin_eh_return.\n");
9264
9265 #ifdef AOF_ASSEMBLER
9266   if (flag_pic)
9267     asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
9268 #endif
9269
9270   return_used_this_function = 0;
9271 }
9272
9273 const char *
9274 arm_output_epilogue (rtx sibling)
9275 {
9276   int reg;
9277   unsigned long saved_regs_mask;
9278   unsigned long func_type;
9279   /* Floats_offset is the offset from the "virtual" frame.  In an APCS
9280      frame that is $fp + 4 for a non-variadic function.  */
9281   int floats_offset = 0;
9282   rtx operands[3];
9283   FILE * f = asm_out_file;
9284   unsigned int lrm_count = 0;
9285   int really_return = (sibling == NULL);
9286   int start_reg;
9287   arm_stack_offsets *offsets;
9288
9289   /* If we have already generated the return instruction
9290      then it is futile to generate anything else.  */
9291   if (use_return_insn (FALSE, sibling) && return_used_this_function)
9292     return "";
9293
9294   func_type = arm_current_func_type ();
9295
9296   if (IS_NAKED (func_type))
9297     /* Naked functions don't have epilogues.  */
9298     return "";
9299
9300   if (IS_VOLATILE (func_type) && TARGET_ABORT_NORETURN)
9301     {
9302       rtx op;
9303
9304       /* A volatile function should never return.  Call abort.  */
9305       op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
9306       assemble_external_libcall (op);
9307       output_asm_insn ("bl\t%a0", &op);
9308
9309       return "";
9310     }
9311
9312   if (current_function_calls_eh_return
9313       && ! really_return)
9314     /* If we are throwing an exception, then we really must
9315        be doing a return,  so we can't tail-call.  */
9316     abort ();
9317
9318   offsets = arm_get_frame_offsets ();
9319   saved_regs_mask = arm_compute_save_reg_mask ();
9320
9321   if (TARGET_IWMMXT)
9322     lrm_count = bit_count (saved_regs_mask);
9323
9324   floats_offset = offsets->saved_args;
9325   /* Compute how far away the floats will be.  */
9326   for (reg = 0; reg <= LAST_ARM_REGNUM; reg++)
9327     if (saved_regs_mask & (1 << reg))
9328       floats_offset += 4;
9329
9330   if (frame_pointer_needed)
9331     {
9332       /* This variable is for the Virtual Frame Pointer, not VFP regs.  */
9333       int vfp_offset = offsets->frame;
9334
9335       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9336         {
9337           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9338             if (regs_ever_live[reg] && !call_used_regs[reg])
9339               {
9340                 floats_offset += 12;
9341                 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
9342                              reg, FP_REGNUM, floats_offset - vfp_offset);
9343               }
9344         }
9345       else
9346         {
9347           start_reg = LAST_FPA_REGNUM;
9348
9349           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
9350             {
9351               if (regs_ever_live[reg] && !call_used_regs[reg])
9352                 {
9353                   floats_offset += 12;
9354
9355                   /* We can't unstack more than four registers at once.  */
9356                   if (start_reg - reg == 3)
9357                     {
9358                       asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
9359                                    reg, FP_REGNUM, floats_offset - vfp_offset);
9360                       start_reg = reg - 1;
9361                     }
9362                 }
9363               else
9364                 {
9365                   if (reg != start_reg)
9366                     asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9367                                  reg + 1, start_reg - reg,
9368                                  FP_REGNUM, floats_offset - vfp_offset);
9369                   start_reg = reg - 1;
9370                 }
9371             }
9372
9373           /* Just in case the last register checked also needs unstacking.  */
9374           if (reg != start_reg)
9375             asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
9376                          reg + 1, start_reg - reg,
9377                          FP_REGNUM, floats_offset - vfp_offset);
9378         }
9379
9380       if (TARGET_HARD_FLOAT && TARGET_VFP)
9381         {
9382           int saved_size;
9383
9384           /* The fldmx insn does not have base+offset addressing modes,
9385              so we use IP to hold the address.  */
9386           saved_size = arm_get_vfp_saved_size ();
9387
9388           if (saved_size > 0)
9389             {
9390               floats_offset += saved_size;
9391               asm_fprintf (f, "\tsub\t%r, %r, #%d\n", IP_REGNUM,
9392                            FP_REGNUM, floats_offset - vfp_offset);
9393             }
9394           start_reg = FIRST_VFP_REGNUM;
9395           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9396             {
9397               if ((!regs_ever_live[reg] || call_used_regs[reg])
9398                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9399                 {
9400                   if (start_reg != reg)
9401                     arm_output_fldmx (f, IP_REGNUM,
9402                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9403                                       (reg - start_reg) / 2);
9404                   start_reg = reg + 2;
9405                 }
9406             }
9407           if (start_reg != reg)
9408             arm_output_fldmx (f, IP_REGNUM,
9409                               (start_reg - FIRST_VFP_REGNUM) / 2,
9410                               (reg - start_reg) / 2);
9411         }
9412
9413       if (TARGET_IWMMXT)
9414         {
9415           /* The frame pointer is guaranteed to be non-double-word aligned.
9416              This is because it is set to (old_stack_pointer - 4) and the
9417              old_stack_pointer was double word aligned.  Thus the offset to
9418              the iWMMXt registers to be loaded must also be non-double-word
9419              sized, so that the resultant address *is* double-word aligned.
9420              We can ignore floats_offset since that was already included in
9421              the live_regs_mask.  */
9422           lrm_count += (lrm_count % 2 ? 2 : 1);
9423
9424           for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
9425             if (regs_ever_live[reg] && !call_used_regs[reg])
9426               {
9427                 asm_fprintf (f, "\twldrd\t%r, [%r, #-%d]\n",
9428                              reg, FP_REGNUM, lrm_count * 4);
9429                 lrm_count += 2;
9430               }
9431         }
9432
9433       /* saved_regs_mask should contain the IP, which at the time of stack
9434          frame generation actually contains the old stack pointer.  So a
9435          quick way to unwind the stack is just pop the IP register directly
9436          into the stack pointer.  */
9437       if ((saved_regs_mask & (1 << IP_REGNUM)) == 0)
9438         abort ();
9439       saved_regs_mask &= ~ (1 << IP_REGNUM);
9440       saved_regs_mask |=   (1 << SP_REGNUM);
9441
9442       /* There are two registers left in saved_regs_mask - LR and PC.  We
9443          only need to restore the LR register (the return address), but to
9444          save time we can load it directly into the PC, unless we need a
9445          special function exit sequence, or we are not really returning.  */
9446       if (really_return
9447           && ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9448           && !current_function_calls_eh_return)
9449         /* Delete the LR from the register mask, so that the LR on
9450            the stack is loaded into the PC in the register mask.  */
9451         saved_regs_mask &= ~ (1 << LR_REGNUM);
9452       else
9453         saved_regs_mask &= ~ (1 << PC_REGNUM);
9454
9455       /* We must use SP as the base register, because SP is one of the
9456          registers being restored.  If an interrupt or page fault
9457          happens in the ldm instruction, the SP might or might not
9458          have been restored.  That would be bad, as then SP will no
9459          longer indicate the safe area of stack, and we can get stack
9460          corruption.  Using SP as the base register means that it will
9461          be reset correctly to the original value, should an interrupt
9462          occur.  If the stack pointer already points at the right
9463          place, then omit the subtraction.  */
9464       if (offsets->outgoing_args != (1 + (int) bit_count (saved_regs_mask))
9465           || current_function_calls_alloca)
9466         asm_fprintf (f, "\tsub\t%r, %r, #%d\n", SP_REGNUM, FP_REGNUM,
9467                      4 * bit_count (saved_regs_mask));
9468       print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9469
9470       if (IS_INTERRUPT (func_type))
9471         /* Interrupt handlers will have pushed the
9472            IP onto the stack, so restore it now.  */
9473         print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, 1 << IP_REGNUM);
9474     }
9475   else
9476     {
9477       /* Restore stack pointer if necessary.  */
9478       if (offsets->outgoing_args != offsets->saved_regs)
9479         {
9480           operands[0] = operands[1] = stack_pointer_rtx;
9481           operands[2] = GEN_INT (offsets->outgoing_args - offsets->saved_regs);
9482           output_add_immediate (operands);
9483         }
9484
9485       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
9486         {
9487           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9488             if (regs_ever_live[reg] && !call_used_regs[reg])
9489               asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
9490                            reg, SP_REGNUM);
9491         }
9492       else
9493         {
9494           start_reg = FIRST_FPA_REGNUM;
9495
9496           for (reg = FIRST_FPA_REGNUM; reg <= LAST_FPA_REGNUM; reg++)
9497             {
9498               if (regs_ever_live[reg] && !call_used_regs[reg])
9499                 {
9500                   if (reg - start_reg == 3)
9501                     {
9502                       asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
9503                                    start_reg, SP_REGNUM);
9504                       start_reg = reg + 1;
9505                     }
9506                 }
9507               else
9508                 {
9509                   if (reg != start_reg)
9510                     asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9511                                  start_reg, reg - start_reg,
9512                                  SP_REGNUM);
9513
9514                   start_reg = reg + 1;
9515                 }
9516             }
9517
9518           /* Just in case the last register checked also needs unstacking.  */
9519           if (reg != start_reg)
9520             asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
9521                          start_reg, reg - start_reg, SP_REGNUM);
9522         }
9523
9524       if (TARGET_HARD_FLOAT && TARGET_VFP)
9525         {
9526           start_reg = FIRST_VFP_REGNUM;
9527           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
9528             {
9529               if ((!regs_ever_live[reg] || call_used_regs[reg])
9530                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
9531                 {
9532                   if (start_reg != reg)
9533                     arm_output_fldmx (f, SP_REGNUM,
9534                                       (start_reg - FIRST_VFP_REGNUM) / 2,
9535                                       (reg - start_reg) / 2);
9536                   start_reg = reg + 2;
9537                 }
9538             }
9539           if (start_reg != reg)
9540             arm_output_fldmx (f, SP_REGNUM,
9541                               (start_reg - FIRST_VFP_REGNUM) / 2,
9542                               (reg - start_reg) / 2);
9543         }
9544       if (TARGET_IWMMXT)
9545         for (reg = FIRST_IWMMXT_REGNUM; reg <= LAST_IWMMXT_REGNUM; reg++)
9546           if (regs_ever_live[reg] && !call_used_regs[reg])
9547             asm_fprintf (f, "\twldrd\t%r, [%r], #8\n", reg, SP_REGNUM);
9548
9549       /* If we can, restore the LR into the PC.  */
9550       if (ARM_FUNC_TYPE (func_type) == ARM_FT_NORMAL
9551           && really_return
9552           && current_function_pretend_args_size == 0
9553           && saved_regs_mask & (1 << LR_REGNUM)
9554           && !current_function_calls_eh_return)
9555         {
9556           saved_regs_mask &= ~ (1 << LR_REGNUM);
9557           saved_regs_mask |=   (1 << PC_REGNUM);
9558         }
9559
9560       /* Load the registers off the stack.  If we only have one register
9561          to load use the LDR instruction - it is faster.  */
9562       if (saved_regs_mask == (1 << LR_REGNUM))
9563         {
9564           asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM, SP_REGNUM);
9565         }
9566       else if (saved_regs_mask)
9567         {
9568           if (saved_regs_mask & (1 << SP_REGNUM))
9569             /* Note - write back to the stack register is not enabled
9570                (i.e. "ldmfd sp!...").  We know that the stack pointer is
9571                in the list of registers and if we add writeback the
9572                instruction becomes UNPREDICTABLE.  */
9573             print_multi_reg (f, "ldmfd\t%r", SP_REGNUM, saved_regs_mask);
9574           else
9575             print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, saved_regs_mask);
9576         }
9577
9578       if (current_function_pretend_args_size)
9579         {
9580           /* Unwind the pre-pushed regs.  */
9581           operands[0] = operands[1] = stack_pointer_rtx;
9582           operands[2] = GEN_INT (current_function_pretend_args_size);
9583           output_add_immediate (operands);
9584         }
9585     }
9586
9587   /* We may have already restored PC directly from the stack.  */
9588   if (!really_return || saved_regs_mask & (1 << PC_REGNUM))
9589     return "";
9590
9591   /* Stack adjustment for exception handler.  */
9592   if (current_function_calls_eh_return)
9593     asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
9594                  ARM_EH_STACKADJ_REGNUM);
9595
9596   /* Generate the return instruction.  */
9597   switch ((int) ARM_FUNC_TYPE (func_type))
9598     {
9599     case ARM_FT_ISR:
9600     case ARM_FT_FIQ:
9601       asm_fprintf (f, "\tsubs\t%r, %r, #4\n", PC_REGNUM, LR_REGNUM);
9602       break;
9603
9604     case ARM_FT_EXCEPTION:
9605       asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9606       break;
9607
9608     case ARM_FT_INTERWORKED:
9609       asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9610       break;
9611
9612     default:
9613       if (arm_arch5 || arm_arch4t)
9614         asm_fprintf (f, "\tbx\t%r\n", LR_REGNUM);
9615       else
9616         asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, LR_REGNUM);
9617       break;
9618     }
9619
9620   return "";
9621 }
9622
9623 static void
9624 arm_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
9625                               HOST_WIDE_INT frame_size ATTRIBUTE_UNUSED)
9626 {
9627   arm_stack_offsets *offsets;
9628
9629   if (TARGET_THUMB)
9630     {
9631       int regno;
9632
9633       /* Emit any call-via-reg trampolines that are needed for v4t support
9634          of call_reg and call_value_reg type insns.  */
9635       for (regno = 0; regno < SP_REGNUM; regno++)
9636         {
9637           rtx label = cfun->machine->call_via[regno];
9638
9639           if (label != NULL)
9640             {
9641               function_section (current_function_decl);
9642               targetm.asm_out.internal_label (asm_out_file, "L",
9643                                               CODE_LABEL_NUMBER (label));
9644               asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
9645             }
9646         }
9647
9648       /* ??? Probably not safe to set this here, since it assumes that a
9649          function will be emitted as assembly immediately after we generate
9650          RTL for it.  This does not happen for inline functions.  */
9651       return_used_this_function = 0;
9652     }
9653   else
9654     {
9655       /* We need to take into account any stack-frame rounding.  */
9656       offsets = arm_get_frame_offsets ();
9657
9658       if (use_return_insn (FALSE, NULL)
9659           && return_used_this_function
9660           && offsets->saved_regs != offsets->outgoing_args
9661           && !frame_pointer_needed)
9662         abort ();
9663
9664       /* Reset the ARM-specific per-function variables.  */
9665       after_arm_reorg = 0;
9666     }
9667 }
9668
9669 /* Generate and emit an insn that we will recognize as a push_multi.
9670    Unfortunately, since this insn does not reflect very well the actual
9671    semantics of the operation, we need to annotate the insn for the benefit
9672    of DWARF2 frame unwind information.  */
9673 static rtx
9674 emit_multi_reg_push (int mask)
9675 {
9676   int num_regs = 0;
9677   int num_dwarf_regs;
9678   int i, j;
9679   rtx par;
9680   rtx dwarf;
9681   int dwarf_par_index;
9682   rtx tmp, reg;
9683
9684   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9685     if (mask & (1 << i))
9686       num_regs++;
9687
9688   if (num_regs == 0 || num_regs > 16)
9689     abort ();
9690
9691   /* We don't record the PC in the dwarf frame information.  */
9692   num_dwarf_regs = num_regs;
9693   if (mask & (1 << PC_REGNUM))
9694     num_dwarf_regs--;
9695
9696   /* For the body of the insn we are going to generate an UNSPEC in
9697      parallel with several USEs.  This allows the insn to be recognized
9698      by the push_multi pattern in the arm.md file.  The insn looks
9699      something like this:
9700
9701        (parallel [
9702            (set (mem:BLK (pre_dec:BLK (reg:SI sp)))
9703                 (unspec:BLK [(reg:SI r4)] UNSPEC_PUSH_MULT))
9704            (use (reg:SI 11 fp))
9705            (use (reg:SI 12 ip))
9706            (use (reg:SI 14 lr))
9707            (use (reg:SI 15 pc))
9708         ])
9709
9710      For the frame note however, we try to be more explicit and actually
9711      show each register being stored into the stack frame, plus a (single)
9712      decrement of the stack pointer.  We do it this way in order to be
9713      friendly to the stack unwinding code, which only wants to see a single
9714      stack decrement per instruction.  The RTL we generate for the note looks
9715      something like this:
9716
9717       (sequence [
9718            (set (reg:SI sp) (plus:SI (reg:SI sp) (const_int -20)))
9719            (set (mem:SI (reg:SI sp)) (reg:SI r4))
9720            (set (mem:SI (plus:SI (reg:SI sp) (const_int 4))) (reg:SI fp))
9721            (set (mem:SI (plus:SI (reg:SI sp) (const_int 8))) (reg:SI ip))
9722            (set (mem:SI (plus:SI (reg:SI sp) (const_int 12))) (reg:SI lr))
9723         ])
9724
9725       This sequence is used both by the code to support stack unwinding for
9726       exceptions handlers and the code to generate dwarf2 frame debugging.  */
9727
9728   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
9729   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (num_dwarf_regs + 1));
9730   dwarf_par_index = 1;
9731
9732   for (i = 0; i <= LAST_ARM_REGNUM; i++)
9733     {
9734       if (mask & (1 << i))
9735         {
9736           reg = gen_rtx_REG (SImode, i);
9737
9738           XVECEXP (par, 0, 0)
9739             = gen_rtx_SET (VOIDmode,
9740                            gen_rtx_MEM (BLKmode,
9741                                         gen_rtx_PRE_DEC (BLKmode,
9742                                                          stack_pointer_rtx)),
9743                            gen_rtx_UNSPEC (BLKmode,
9744                                            gen_rtvec (1, reg),
9745                                            UNSPEC_PUSH_MULT));
9746
9747           if (i != PC_REGNUM)
9748             {
9749               tmp = gen_rtx_SET (VOIDmode,
9750                                  gen_rtx_MEM (SImode, stack_pointer_rtx),
9751                                  reg);
9752               RTX_FRAME_RELATED_P (tmp) = 1;
9753               XVECEXP (dwarf, 0, dwarf_par_index) = tmp;
9754               dwarf_par_index++;
9755             }
9756
9757           break;
9758         }
9759     }
9760
9761   for (j = 1, i++; j < num_regs; i++)
9762     {
9763       if (mask & (1 << i))
9764         {
9765           reg = gen_rtx_REG (SImode, i);
9766
9767           XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
9768
9769           if (i != PC_REGNUM)
9770             {
9771               tmp = gen_rtx_SET (VOIDmode,
9772                                  gen_rtx_MEM (SImode,
9773                                               plus_constant (stack_pointer_rtx,
9774                                                              4 * j)),
9775                                  reg);
9776               RTX_FRAME_RELATED_P (tmp) = 1;
9777               XVECEXP (dwarf, 0, dwarf_par_index++) = tmp;
9778             }
9779
9780           j++;
9781         }
9782     }
9783
9784   par = emit_insn (par);
9785
9786   tmp = gen_rtx_SET (SImode,
9787                      stack_pointer_rtx,
9788                      gen_rtx_PLUS (SImode,
9789                                    stack_pointer_rtx,
9790                                    GEN_INT (-4 * num_regs)));
9791   RTX_FRAME_RELATED_P (tmp) = 1;
9792   XVECEXP (dwarf, 0, 0) = tmp;
9793
9794   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9795                                        REG_NOTES (par));
9796   return par;
9797 }
9798
9799 static rtx
9800 emit_sfm (int base_reg, int count)
9801 {
9802   rtx par;
9803   rtx dwarf;
9804   rtx tmp, reg;
9805   int i;
9806
9807   par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
9808   dwarf = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (count + 1));
9809
9810   reg = gen_rtx_REG (XFmode, base_reg++);
9811
9812   XVECEXP (par, 0, 0)
9813     = gen_rtx_SET (VOIDmode,
9814                    gen_rtx_MEM (BLKmode,
9815                                 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
9816                    gen_rtx_UNSPEC (BLKmode,
9817                                    gen_rtvec (1, reg),
9818                                    UNSPEC_PUSH_MULT));
9819   tmp = gen_rtx_SET (VOIDmode,
9820                      gen_rtx_MEM (XFmode, stack_pointer_rtx), reg);
9821   RTX_FRAME_RELATED_P (tmp) = 1;
9822   XVECEXP (dwarf, 0, 1) = tmp;
9823
9824   for (i = 1; i < count; i++)
9825     {
9826       reg = gen_rtx_REG (XFmode, base_reg++);
9827       XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
9828
9829       tmp = gen_rtx_SET (VOIDmode,
9830                          gen_rtx_MEM (XFmode,
9831                                       plus_constant (stack_pointer_rtx,
9832                                                      i * 12)),
9833                          reg);
9834       RTX_FRAME_RELATED_P (tmp) = 1;
9835       XVECEXP (dwarf, 0, i + 1) = tmp;
9836     }
9837
9838   tmp = gen_rtx_SET (VOIDmode,
9839                      stack_pointer_rtx,
9840                      gen_rtx_PLUS (SImode,
9841                                    stack_pointer_rtx,
9842                                    GEN_INT (-12 * count)));
9843   RTX_FRAME_RELATED_P (tmp) = 1;
9844   XVECEXP (dwarf, 0, 0) = tmp;
9845
9846   par = emit_insn (par);
9847   REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
9848                                        REG_NOTES (par));
9849   return par;
9850 }
9851
9852
9853 /* Return true if the current function needs to save/restore LR.  */
9854
9855 static bool
9856 thumb_force_lr_save (void)
9857 {
9858   return !cfun->machine->lr_save_eliminated
9859          && (!leaf_function_p ()
9860              || thumb_far_jump_used_p ()
9861              || regs_ever_live [LR_REGNUM]);
9862 }
9863
9864
9865 /* Compute the distance from register FROM to register TO.
9866    These can be the arg pointer (26), the soft frame pointer (25),
9867    the stack pointer (13) or the hard frame pointer (11).
9868    In thumb mode r7 is used as the soft frame pointer, if needed.
9869    Typical stack layout looks like this:
9870
9871        old stack pointer -> |    |
9872                              ----
9873                             |    | \
9874                             |    |   saved arguments for
9875                             |    |   vararg functions
9876                             |    | /
9877                               --
9878    hard FP & arg pointer -> |    | \
9879                             |    |   stack
9880                             |    |   frame
9881                             |    | /
9882                               --
9883                             |    | \
9884                             |    |   call saved
9885                             |    |   registers
9886       soft frame pointer -> |    | /
9887                               --
9888                             |    | \
9889                             |    |   local
9890                             |    |   variables
9891                             |    | /
9892                               --
9893                             |    | \
9894                             |    |   outgoing
9895                             |    |   arguments
9896    current stack pointer -> |    | /
9897                               --
9898
9899   For a given function some or all of these stack components
9900   may not be needed, giving rise to the possibility of
9901   eliminating some of the registers.
9902
9903   The values returned by this function must reflect the behavior
9904   of arm_expand_prologue() and arm_compute_save_reg_mask().
9905
9906   The sign of the number returned reflects the direction of stack
9907   growth, so the values are positive for all eliminations except
9908   from the soft frame pointer to the hard frame pointer.
9909
9910   SFP may point just inside the local variables block to ensure correct
9911   alignment.  */
9912
9913
9914 /* Calculate stack offsets.  These are used to calculate register elimination
9915    offsets and in prologue/epilogue code.  */
9916
9917 static arm_stack_offsets *
9918 arm_get_frame_offsets (void)
9919 {
9920   struct arm_stack_offsets *offsets;
9921   unsigned long func_type;
9922   int leaf;
9923   int saved;
9924   HOST_WIDE_INT frame_size;
9925
9926   offsets = &cfun->machine->stack_offsets;
9927
9928   /* We need to know if we are a leaf function.  Unfortunately, it
9929      is possible to be called after start_sequence has been called,
9930      which causes get_insns to return the insns for the sequence,
9931      not the function, which will cause leaf_function_p to return
9932      the incorrect result.
9933
9934      to know about leaf functions once reload has completed, and the
9935      frame size cannot be changed after that time, so we can safely
9936      use the cached value.  */
9937
9938   if (reload_completed)
9939     return offsets;
9940
9941   /* Initially this is the size of the local variables.  It will translated
9942      into an offset once we have determined the size of preceding data.  */
9943   frame_size = ROUND_UP_WORD (get_frame_size ());
9944
9945   leaf = leaf_function_p ();
9946
9947   /* Space for variadic functions.  */
9948   offsets->saved_args = current_function_pretend_args_size;
9949
9950   offsets->frame = offsets->saved_args + (frame_pointer_needed ? 4 : 0);
9951
9952   if (TARGET_ARM)
9953     {
9954       unsigned int regno;
9955
9956       saved = bit_count (arm_compute_save_reg_mask ()) * 4;
9957
9958       /* We know that SP will be doubleword aligned on entry, and we must
9959          preserve that condition at any subroutine call.  We also require the
9960          soft frame pointer to be doubleword aligned.  */
9961
9962       if (TARGET_REALLY_IWMMXT)
9963         {
9964           /* Check for the call-saved iWMMXt registers.  */
9965           for (regno = FIRST_IWMMXT_REGNUM;
9966                regno <= LAST_IWMMXT_REGNUM;
9967                regno++)
9968             if (regs_ever_live [regno] && ! call_used_regs [regno])
9969               saved += 8;
9970         }
9971
9972       func_type = arm_current_func_type ();
9973       if (! IS_VOLATILE (func_type))
9974         {
9975           /* Space for saved FPA registers.  */
9976           for (regno = FIRST_FPA_REGNUM; regno <= LAST_FPA_REGNUM; regno++)
9977           if (regs_ever_live[regno] && ! call_used_regs[regno])
9978             saved += 12;
9979
9980           /* Space for saved VFP registers.  */
9981           if (TARGET_HARD_FLOAT && TARGET_VFP)
9982             saved += arm_get_vfp_saved_size ();
9983         }
9984     }
9985   else /* TARGET_THUMB */
9986     {
9987       saved = bit_count (thumb_compute_save_reg_mask ()) * 4;
9988       if (TARGET_BACKTRACE)
9989         saved += 16;
9990     }
9991
9992   /* Saved registers include the stack frame.  */
9993   offsets->saved_regs = offsets->saved_args + saved;
9994   offsets->soft_frame = offsets->saved_regs + CALLER_INTERWORKING_SLOT_SIZE;
9995   /* A leaf function does not need any stack alignment if it has nothing
9996      on the stack.  */
9997   if (leaf && frame_size == 0)
9998     {
9999       offsets->outgoing_args = offsets->soft_frame;
10000       return offsets;
10001     }
10002
10003   /* Ensure SFP has the correct alignment.  */
10004   if (ARM_DOUBLEWORD_ALIGN
10005       && (offsets->soft_frame & 7))
10006     offsets->soft_frame += 4;
10007
10008   offsets->outgoing_args = offsets->soft_frame + frame_size
10009                            + current_function_outgoing_args_size;
10010
10011   if (ARM_DOUBLEWORD_ALIGN)
10012     {
10013       /* Ensure SP remains doubleword aligned.  */
10014       if (offsets->outgoing_args & 7)
10015         offsets->outgoing_args += 4;
10016       if (offsets->outgoing_args & 7)
10017         abort ();
10018     }
10019
10020   return offsets;
10021 }
10022
10023
10024 /* Calculate the relative offsets for the different stack pointers.  Positive
10025    offsets are in the direction of stack growth.  */
10026
10027 HOST_WIDE_INT
10028 arm_compute_initial_elimination_offset (unsigned int from, unsigned int to)
10029 {
10030   arm_stack_offsets *offsets;
10031
10032   offsets = arm_get_frame_offsets ();
10033
10034   /* OK, now we have enough information to compute the distances.
10035      There must be an entry in these switch tables for each pair
10036      of registers in ELIMINABLE_REGS, even if some of the entries
10037      seem to be redundant or useless.  */
10038   switch (from)
10039     {
10040     case ARG_POINTER_REGNUM:
10041       switch (to)
10042         {
10043         case THUMB_HARD_FRAME_POINTER_REGNUM:
10044           return 0;
10045
10046         case FRAME_POINTER_REGNUM:
10047           /* This is the reverse of the soft frame pointer
10048              to hard frame pointer elimination below.  */
10049           return offsets->soft_frame - offsets->saved_args;
10050
10051         case ARM_HARD_FRAME_POINTER_REGNUM:
10052           /* If there is no stack frame then the hard
10053              frame pointer and the arg pointer coincide.  */
10054           if (offsets->frame == offsets->saved_regs)
10055             return 0;
10056           /* FIXME:  Not sure about this.  Maybe we should always return 0 ?  */
10057           return (frame_pointer_needed
10058                   && cfun->static_chain_decl != NULL
10059                   && ! cfun->machine->uses_anonymous_args) ? 4 : 0;
10060
10061         case STACK_POINTER_REGNUM:
10062           /* If nothing has been pushed on the stack at all
10063              then this will return -4.  This *is* correct!  */
10064           return offsets->outgoing_args - (offsets->saved_args + 4);
10065
10066         default:
10067           abort ();
10068         }
10069       break;
10070
10071     case FRAME_POINTER_REGNUM:
10072       switch (to)
10073         {
10074         case THUMB_HARD_FRAME_POINTER_REGNUM:
10075           return 0;
10076
10077         case ARM_HARD_FRAME_POINTER_REGNUM:
10078           /* The hard frame pointer points to the top entry in the
10079              stack frame.  The soft frame pointer to the bottom entry
10080              in the stack frame.  If there is no stack frame at all,
10081              then they are identical.  */
10082
10083           return offsets->frame - offsets->soft_frame;
10084
10085         case STACK_POINTER_REGNUM:
10086           return offsets->outgoing_args - offsets->soft_frame;
10087
10088         default:
10089           abort ();
10090         }
10091       break;
10092
10093     default:
10094       /* You cannot eliminate from the stack pointer.
10095          In theory you could eliminate from the hard frame
10096          pointer to the stack pointer, but this will never
10097          happen, since if a stack frame is not needed the
10098          hard frame pointer will never be used.  */
10099       abort ();
10100     }
10101 }
10102
10103
10104 /* Generate the prologue instructions for entry into an ARM function.  */
10105 void
10106 arm_expand_prologue (void)
10107 {
10108   int reg;
10109   rtx amount;
10110   rtx insn;
10111   rtx ip_rtx;
10112   unsigned long live_regs_mask;
10113   unsigned long func_type;
10114   int fp_offset = 0;
10115   int saved_pretend_args = 0;
10116   int saved_regs = 0;
10117   unsigned HOST_WIDE_INT args_to_push;
10118   arm_stack_offsets *offsets;
10119
10120   func_type = arm_current_func_type ();
10121
10122   /* Naked functions don't have prologues.  */
10123   if (IS_NAKED (func_type))
10124     return;
10125
10126   /* Make a copy of c_f_p_a_s as we may need to modify it locally.  */
10127   args_to_push = current_function_pretend_args_size;
10128
10129   /* Compute which register we will have to save onto the stack.  */
10130   live_regs_mask = arm_compute_save_reg_mask ();
10131
10132   ip_rtx = gen_rtx_REG (SImode, IP_REGNUM);
10133
10134   if (frame_pointer_needed)
10135     {
10136       if (IS_INTERRUPT (func_type))
10137         {
10138           /* Interrupt functions must not corrupt any registers.
10139              Creating a frame pointer however, corrupts the IP
10140              register, so we must push it first.  */
10141           insn = emit_multi_reg_push (1 << IP_REGNUM);
10142
10143           /* Do not set RTX_FRAME_RELATED_P on this insn.
10144              The dwarf stack unwinding code only wants to see one
10145              stack decrement per function, and this is not it.  If
10146              this instruction is labeled as being part of the frame
10147              creation sequence then dwarf2out_frame_debug_expr will
10148              abort when it encounters the assignment of IP to FP
10149              later on, since the use of SP here establishes SP as
10150              the CFA register and not IP.
10151
10152              Anyway this instruction is not really part of the stack
10153              frame creation although it is part of the prologue.  */
10154         }
10155       else if (IS_NESTED (func_type))
10156         {
10157           /* The Static chain register is the same as the IP register
10158              used as a scratch register during stack frame creation.
10159              To get around this need to find somewhere to store IP
10160              whilst the frame is being created.  We try the following
10161              places in order:
10162
10163                1. The last argument register.
10164                2. A slot on the stack above the frame.  (This only
10165                   works if the function is not a varargs function).
10166                3. Register r3, after pushing the argument registers
10167                   onto the stack.
10168
10169              Note - we only need to tell the dwarf2 backend about the SP
10170              adjustment in the second variant; the static chain register
10171              doesn't need to be unwound, as it doesn't contain a value
10172              inherited from the caller.  */
10173
10174           if (regs_ever_live[3] == 0)
10175             {
10176               insn = gen_rtx_REG (SImode, 3);
10177               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10178               insn = emit_insn (insn);
10179             }
10180           else if (args_to_push == 0)
10181             {
10182               rtx dwarf;
10183               insn = gen_rtx_PRE_DEC (SImode, stack_pointer_rtx);
10184               insn = gen_rtx_MEM (SImode, insn);
10185               insn = gen_rtx_SET (VOIDmode, insn, ip_rtx);
10186               insn = emit_insn (insn);
10187
10188               fp_offset = 4;
10189
10190               /* Just tell the dwarf backend that we adjusted SP.  */
10191               dwarf = gen_rtx_SET (VOIDmode, stack_pointer_rtx,
10192                                    gen_rtx_PLUS (SImode, stack_pointer_rtx,
10193                                                  GEN_INT (-fp_offset)));
10194               RTX_FRAME_RELATED_P (insn) = 1;
10195               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
10196                                                     dwarf, REG_NOTES (insn));
10197             }
10198           else
10199             {
10200               /* Store the args on the stack.  */
10201               if (cfun->machine->uses_anonymous_args)
10202                 insn = emit_multi_reg_push
10203                   ((0xf0 >> (args_to_push / 4)) & 0xf);
10204               else
10205                 insn = emit_insn
10206                   (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10207                                GEN_INT (- args_to_push)));
10208
10209               RTX_FRAME_RELATED_P (insn) = 1;
10210
10211               saved_pretend_args = 1;
10212               fp_offset = args_to_push;
10213               args_to_push = 0;
10214
10215               /* Now reuse r3 to preserve IP.  */
10216               insn = gen_rtx_REG (SImode, 3);
10217               insn = gen_rtx_SET (SImode, insn, ip_rtx);
10218               (void) emit_insn (insn);
10219             }
10220         }
10221
10222       if (fp_offset)
10223         {
10224           insn = gen_rtx_PLUS (SImode, stack_pointer_rtx, GEN_INT (fp_offset));
10225           insn = gen_rtx_SET  (SImode, ip_rtx, insn);
10226         }
10227       else
10228         insn = gen_movsi (ip_rtx, stack_pointer_rtx);
10229
10230       insn = emit_insn (insn);
10231       RTX_FRAME_RELATED_P (insn) = 1;
10232     }
10233
10234   if (args_to_push)
10235     {
10236       /* Push the argument registers, or reserve space for them.  */
10237       if (cfun->machine->uses_anonymous_args)
10238         insn = emit_multi_reg_push
10239           ((0xf0 >> (args_to_push / 4)) & 0xf);
10240       else
10241         insn = emit_insn
10242           (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10243                        GEN_INT (- args_to_push)));
10244       RTX_FRAME_RELATED_P (insn) = 1;
10245     }
10246
10247   /* If this is an interrupt service routine, and the link register
10248      is going to be pushed, and we are not creating a stack frame,
10249      (which would involve an extra push of IP and a pop in the epilogue)
10250      subtracting four from LR now will mean that the function return
10251      can be done with a single instruction.  */
10252   if ((func_type == ARM_FT_ISR || func_type == ARM_FT_FIQ)
10253       && (live_regs_mask & (1 << LR_REGNUM)) != 0
10254       && ! frame_pointer_needed)
10255     emit_insn (gen_rtx_SET (SImode,
10256                             gen_rtx_REG (SImode, LR_REGNUM),
10257                             gen_rtx_PLUS (SImode,
10258                                           gen_rtx_REG (SImode, LR_REGNUM),
10259                                           GEN_INT (-4))));
10260
10261   if (live_regs_mask)
10262     {
10263       insn = emit_multi_reg_push (live_regs_mask);
10264       saved_regs += bit_count (live_regs_mask) * 4;
10265       RTX_FRAME_RELATED_P (insn) = 1;
10266     }
10267
10268   if (TARGET_IWMMXT)
10269     for (reg = LAST_IWMMXT_REGNUM; reg >= FIRST_IWMMXT_REGNUM; reg--)
10270       if (regs_ever_live[reg] && ! call_used_regs [reg])
10271         {
10272           insn = gen_rtx_PRE_DEC (V2SImode, stack_pointer_rtx);
10273           insn = gen_rtx_MEM (V2SImode, insn);
10274           insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10275                                          gen_rtx_REG (V2SImode, reg)));
10276           RTX_FRAME_RELATED_P (insn) = 1;
10277           saved_regs += 8;
10278         }
10279
10280   if (! IS_VOLATILE (func_type))
10281     {
10282       int start_reg;
10283
10284       /* Save any floating point call-saved registers used by this
10285          function.  */
10286       if (arm_fpu_arch == FPUTYPE_FPA_EMU2)
10287         {
10288           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10289             if (regs_ever_live[reg] && !call_used_regs[reg])
10290               {
10291                 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
10292                 insn = gen_rtx_MEM (XFmode, insn);
10293                 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
10294                                                gen_rtx_REG (XFmode, reg)));
10295                 RTX_FRAME_RELATED_P (insn) = 1;
10296                 saved_regs += 12;
10297               }
10298         }
10299       else
10300         {
10301           start_reg = LAST_FPA_REGNUM;
10302
10303           for (reg = LAST_FPA_REGNUM; reg >= FIRST_FPA_REGNUM; reg--)
10304             {
10305               if (regs_ever_live[reg] && !call_used_regs[reg])
10306                 {
10307                   if (start_reg - reg == 3)
10308                     {
10309                       insn = emit_sfm (reg, 4);
10310                       RTX_FRAME_RELATED_P (insn) = 1;
10311                       saved_regs += 48;
10312                       start_reg = reg - 1;
10313                     }
10314                 }
10315               else
10316                 {
10317                   if (start_reg != reg)
10318                     {
10319                       insn = emit_sfm (reg + 1, start_reg - reg);
10320                       RTX_FRAME_RELATED_P (insn) = 1;
10321                       saved_regs += (start_reg - reg) * 12;
10322                     }
10323                   start_reg = reg - 1;
10324                 }
10325             }
10326
10327           if (start_reg != reg)
10328             {
10329               insn = emit_sfm (reg + 1, start_reg - reg);
10330               saved_regs += (start_reg - reg) * 12;
10331               RTX_FRAME_RELATED_P (insn) = 1;
10332             }
10333         }
10334       if (TARGET_HARD_FLOAT && TARGET_VFP)
10335         {
10336           start_reg = FIRST_VFP_REGNUM;
10337
10338           for (reg = FIRST_VFP_REGNUM; reg < LAST_VFP_REGNUM; reg += 2)
10339             {
10340               if ((!regs_ever_live[reg] || call_used_regs[reg])
10341                   && (!regs_ever_live[reg + 1] || call_used_regs[reg + 1]))
10342                 {
10343                   if (start_reg != reg)
10344                     saved_regs += vfp_emit_fstmx (start_reg,
10345                                                   (reg - start_reg) / 2);
10346                   start_reg = reg + 2;
10347                 }
10348             }
10349           if (start_reg != reg)
10350             saved_regs += vfp_emit_fstmx (start_reg,
10351                                           (reg - start_reg) / 2);
10352         }
10353     }
10354
10355   if (frame_pointer_needed)
10356     {
10357       /* Create the new frame pointer.  */
10358       insn = GEN_INT (-(4 + args_to_push + fp_offset));
10359       insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx, ip_rtx, insn));
10360       RTX_FRAME_RELATED_P (insn) = 1;
10361
10362       if (IS_NESTED (func_type))
10363         {
10364           /* Recover the static chain register.  */
10365           if (regs_ever_live [3] == 0
10366               || saved_pretend_args)
10367             insn = gen_rtx_REG (SImode, 3);
10368           else /* if (current_function_pretend_args_size == 0) */
10369             {
10370               insn = gen_rtx_PLUS (SImode, hard_frame_pointer_rtx,
10371                                    GEN_INT (4));
10372               insn = gen_rtx_MEM (SImode, insn);
10373             }
10374
10375           emit_insn (gen_rtx_SET (SImode, ip_rtx, insn));
10376           /* Add a USE to stop propagate_one_insn() from barfing.  */
10377           emit_insn (gen_prologue_use (ip_rtx));
10378         }
10379     }
10380
10381   offsets = arm_get_frame_offsets ();
10382   if (offsets->outgoing_args != offsets->saved_args + saved_regs)
10383     {
10384       /* This add can produce multiple insns for a large constant, so we
10385          need to get tricky.  */
10386       rtx last = get_last_insn ();
10387
10388       amount = GEN_INT (offsets->saved_args + saved_regs
10389                         - offsets->outgoing_args);
10390
10391       insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
10392                                     amount));
10393       do
10394         {
10395           last = last ? NEXT_INSN (last) : get_insns ();
10396           RTX_FRAME_RELATED_P (last) = 1;
10397         }
10398       while (last != insn);
10399
10400       /* If the frame pointer is needed, emit a special barrier that
10401          will prevent the scheduler from moving stores to the frame
10402          before the stack adjustment.  */
10403       if (frame_pointer_needed)
10404         insn = emit_insn (gen_stack_tie (stack_pointer_rtx,
10405                                          hard_frame_pointer_rtx));
10406     }
10407
10408
10409   if (flag_pic)
10410     arm_load_pic_register (INVALID_REGNUM);
10411
10412   /* If we are profiling, make sure no instructions are scheduled before
10413      the call to mcount.  Similarly if the user has requested no
10414      scheduling in the prolog.  */
10415   if (current_function_profile || TARGET_NO_SCHED_PRO)
10416     emit_insn (gen_blockage ());
10417
10418   /* If the link register is being kept alive, with the return address in it,
10419      then make sure that it does not get reused by the ce2 pass.  */
10420   if ((live_regs_mask & (1 << LR_REGNUM)) == 0)
10421     {
10422       emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
10423       cfun->machine->lr_save_eliminated = 1;
10424     }
10425 }
10426 \f
10427 /* If CODE is 'd', then the X is a condition operand and the instruction
10428    should only be executed if the condition is true.
10429    if CODE is 'D', then the X is a condition operand and the instruction
10430    should only be executed if the condition is false: however, if the mode
10431    of the comparison is CCFPEmode, then always execute the instruction -- we
10432    do this because in these circumstances !GE does not necessarily imply LT;
10433    in these cases the instruction pattern will take care to make sure that
10434    an instruction containing %d will follow, thereby undoing the effects of
10435    doing this instruction unconditionally.
10436    If CODE is 'N' then X is a floating point operand that must be negated
10437    before output.
10438    If CODE is 'B' then output a bitwise inverted value of X (a const int).
10439    If X is a REG and CODE is `M', output a ldm/stm style multi-reg.  */
10440 void
10441 arm_print_operand (FILE *stream, rtx x, int code)
10442 {
10443   switch (code)
10444     {
10445     case '@':
10446       fputs (ASM_COMMENT_START, stream);
10447       return;
10448
10449     case '_':
10450       fputs (user_label_prefix, stream);
10451       return;
10452
10453     case '|':
10454       fputs (REGISTER_PREFIX, stream);
10455       return;
10456
10457     case '?':
10458       if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
10459         {
10460           if (TARGET_THUMB)
10461             {
10462               output_operand_lossage ("predicated Thumb instruction");
10463               break;
10464             }
10465           if (current_insn_predicate != NULL)
10466             {
10467               output_operand_lossage
10468                 ("predicated instruction in conditional sequence");
10469               break;
10470             }
10471
10472           fputs (arm_condition_codes[arm_current_cc], stream);
10473         }
10474       else if (current_insn_predicate)
10475         {
10476           enum arm_cond_code code;
10477
10478           if (TARGET_THUMB)
10479             {
10480               output_operand_lossage ("predicated Thumb instruction");
10481               break;
10482             }
10483
10484           code = get_arm_condition_code (current_insn_predicate);
10485           fputs (arm_condition_codes[code], stream);
10486         }
10487       return;
10488
10489     case 'N':
10490       {
10491         REAL_VALUE_TYPE r;
10492         REAL_VALUE_FROM_CONST_DOUBLE (r, x);
10493         r = REAL_VALUE_NEGATE (r);
10494         fprintf (stream, "%s", fp_const_from_val (&r));
10495       }
10496       return;
10497
10498     case 'B':
10499       if (GET_CODE (x) == CONST_INT)
10500         {
10501           HOST_WIDE_INT val;
10502           val = ARM_SIGN_EXTEND (~INTVAL (x));
10503           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
10504         }
10505       else
10506         {
10507           putc ('~', stream);
10508           output_addr_const (stream, x);
10509         }
10510       return;
10511
10512     case 'i':
10513       fprintf (stream, "%s", arithmetic_instr (x, 1));
10514       return;
10515
10516     /* Truncate Cirrus shift counts.  */
10517     case 's':
10518       if (GET_CODE (x) == CONST_INT)
10519         {
10520           fprintf (stream, HOST_WIDE_INT_PRINT_DEC, INTVAL (x) & 0x3f);
10521           return;
10522         }
10523       arm_print_operand (stream, x, 0);
10524       return;
10525
10526     case 'I':
10527       fprintf (stream, "%s", arithmetic_instr (x, 0));
10528       return;
10529
10530     case 'S':
10531       {
10532         HOST_WIDE_INT val;
10533         const char * shift = shift_op (x, &val);
10534
10535         if (shift)
10536           {
10537             fprintf (stream, ", %s ", shift_op (x, &val));
10538             if (val == -1)
10539               arm_print_operand (stream, XEXP (x, 1), 0);
10540             else
10541               fprintf (stream, "#" HOST_WIDE_INT_PRINT_DEC, val);
10542           }
10543       }
10544       return;
10545
10546       /* An explanation of the 'Q', 'R' and 'H' register operands:
10547
10548          In a pair of registers containing a DI or DF value the 'Q'
10549          operand returns the register number of the register containing
10550          the least significant part of the value.  The 'R' operand returns
10551          the register number of the register containing the most
10552          significant part of the value.
10553
10554          The 'H' operand returns the higher of the two register numbers.
10555          On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
10556          same as the 'Q' operand, since the most significant part of the
10557          value is held in the lower number register.  The reverse is true
10558          on systems where WORDS_BIG_ENDIAN is false.
10559
10560          The purpose of these operands is to distinguish between cases
10561          where the endian-ness of the values is important (for example
10562          when they are added together), and cases where the endian-ness
10563          is irrelevant, but the order of register operations is important.
10564          For example when loading a value from memory into a register
10565          pair, the endian-ness does not matter.  Provided that the value
10566          from the lower memory address is put into the lower numbered
10567          register, and the value from the higher address is put into the
10568          higher numbered register, the load will work regardless of whether
10569          the value being loaded is big-wordian or little-wordian.  The
10570          order of the two register loads can matter however, if the address
10571          of the memory location is actually held in one of the registers
10572          being overwritten by the load.  */
10573     case 'Q':
10574       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10575         {
10576           output_operand_lossage ("invalid operand for code '%c'", code);
10577           return;
10578         }
10579
10580       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
10581       return;
10582
10583     case 'R':
10584       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10585         {
10586           output_operand_lossage ("invalid operand for code '%c'", code);
10587           return;
10588         }
10589
10590       asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
10591       return;
10592
10593     case 'H':
10594       if (GET_CODE (x) != REG || REGNO (x) > LAST_ARM_REGNUM)
10595         {
10596           output_operand_lossage ("invalid operand for code '%c'", code);
10597           return;
10598         }
10599
10600       asm_fprintf (stream, "%r", REGNO (x) + 1);
10601       return;
10602
10603     case 'm':
10604       asm_fprintf (stream, "%r",
10605                    GET_CODE (XEXP (x, 0)) == REG
10606                    ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
10607       return;
10608
10609     case 'M':
10610       asm_fprintf (stream, "{%r-%r}",
10611                    REGNO (x),
10612                    REGNO (x) + ARM_NUM_REGS (GET_MODE (x)) - 1);
10613       return;
10614
10615     case 'd':
10616       /* CONST_TRUE_RTX means always -- that's the default.  */
10617       if (x == const_true_rtx)
10618         return;
10619
10620       if (!COMPARISON_P (x))
10621         {
10622           output_operand_lossage ("invalid operand for code '%c'", code);
10623           return;
10624         }
10625
10626       fputs (arm_condition_codes[get_arm_condition_code (x)],
10627              stream);
10628       return;
10629
10630     case 'D':
10631       /* CONST_TRUE_RTX means not always -- i.e. never.  We shouldn't ever
10632          want to do that.  */
10633       if (x == const_true_rtx)
10634         {
10635           output_operand_lossage ("instruction never exectued");
10636           return;
10637         }
10638       if (!COMPARISON_P (x))
10639         {
10640           output_operand_lossage ("invalid operand for code '%c'", code);
10641           return;
10642         }
10643
10644       fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
10645                                  (get_arm_condition_code (x))],
10646              stream);
10647       return;
10648
10649     /* Cirrus registers can be accessed in a variety of ways:
10650          single floating point (f)
10651          double floating point (d)
10652          32bit integer         (fx)
10653          64bit integer         (dx).  */
10654     case 'W':                   /* Cirrus register in F mode.  */
10655     case 'X':                   /* Cirrus register in D mode.  */
10656     case 'Y':                   /* Cirrus register in FX mode.  */
10657     case 'Z':                   /* Cirrus register in DX mode.  */
10658       if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10659         abort ();
10660
10661       fprintf (stream, "mv%s%s",
10662                code == 'W' ? "f"
10663                : code == 'X' ? "d"
10664                : code == 'Y' ? "fx" : "dx", reg_names[REGNO (x)] + 2);
10665
10666       return;
10667
10668     /* Print cirrus register in the mode specified by the register's mode.  */
10669     case 'V':
10670       {
10671         int mode = GET_MODE (x);
10672
10673         if (GET_CODE (x) != REG || REGNO_REG_CLASS (REGNO (x)) != CIRRUS_REGS)
10674           {
10675             output_operand_lossage ("invalid operand for code '%c'", code);
10676             return;
10677           }
10678
10679         fprintf (stream, "mv%s%s",
10680                  mode == DFmode ? "d"
10681                  : mode == SImode ? "fx"
10682                  : mode == DImode ? "dx"
10683                  : "f", reg_names[REGNO (x)] + 2);
10684
10685         return;
10686       }
10687
10688     case 'U':
10689       if (GET_CODE (x) != REG
10690           || REGNO (x) < FIRST_IWMMXT_GR_REGNUM
10691           || REGNO (x) > LAST_IWMMXT_GR_REGNUM)
10692         /* Bad value for wCG register number.  */
10693         {
10694           output_operand_lossage ("invalid operand for code '%c'", code);
10695           return;
10696         }
10697
10698       else
10699         fprintf (stream, "%d", REGNO (x) - FIRST_IWMMXT_GR_REGNUM);
10700       return;
10701
10702       /* Print an iWMMXt control register name.  */
10703     case 'w':
10704       if (GET_CODE (x) != CONST_INT
10705           || INTVAL (x) < 0
10706           || INTVAL (x) >= 16)
10707         /* Bad value for wC register number.  */
10708         {
10709           output_operand_lossage ("invalid operand for code '%c'", code);
10710           return;
10711         }
10712
10713       else
10714         {
10715           static const char * wc_reg_names [16] =
10716             {
10717               "wCID",  "wCon",  "wCSSF", "wCASF",
10718               "wC4",   "wC5",   "wC6",   "wC7",
10719               "wCGR0", "wCGR1", "wCGR2", "wCGR3",
10720               "wC12",  "wC13",  "wC14",  "wC15"
10721             };
10722
10723           fprintf (stream, wc_reg_names [INTVAL (x)]);
10724         }
10725       return;
10726
10727       /* Print a VFP double precision register name.  */
10728     case 'P':
10729       {
10730         int mode = GET_MODE (x);
10731         int num;
10732
10733         if (mode != DImode && mode != DFmode)
10734           {
10735             output_operand_lossage ("invalid operand for code '%c'", code);
10736             return;
10737           }
10738
10739         if (GET_CODE (x) != REG
10740             || !IS_VFP_REGNUM (REGNO (x)))
10741           {
10742             output_operand_lossage ("invalid operand for code '%c'", code);
10743             return;
10744           }
10745
10746         num = REGNO(x) - FIRST_VFP_REGNUM;
10747         if (num & 1)
10748           {
10749             output_operand_lossage ("invalid operand for code '%c'", code);
10750             return;
10751           }
10752
10753         fprintf (stream, "d%d", num >> 1);
10754       }
10755       return;
10756
10757     default:
10758       if (x == 0)
10759         {
10760           output_operand_lossage ("missing operand");
10761           return;
10762         }
10763
10764       if (GET_CODE (x) == REG)
10765         asm_fprintf (stream, "%r", REGNO (x));
10766       else if (GET_CODE (x) == MEM)
10767         {
10768           output_memory_reference_mode = GET_MODE (x);
10769           output_address (XEXP (x, 0));
10770         }
10771       else if (GET_CODE (x) == CONST_DOUBLE)
10772         fprintf (stream, "#%s", fp_immediate_constant (x));
10773       else if (GET_CODE (x) == NEG)
10774         abort (); /* This should never happen now.  */
10775       else
10776         {
10777           fputc ('#', stream);
10778           output_addr_const (stream, x);
10779         }
10780     }
10781 }
10782 \f
10783 #ifndef AOF_ASSEMBLER
10784 /* Target hook for assembling integer objects.  The ARM version needs to
10785    handle word-sized values specially.  */
10786 static bool
10787 arm_assemble_integer (rtx x, unsigned int size, int aligned_p)
10788 {
10789   if (size == UNITS_PER_WORD && aligned_p)
10790     {
10791       fputs ("\t.word\t", asm_out_file);
10792       output_addr_const (asm_out_file, x);
10793
10794       /* Mark symbols as position independent.  We only do this in the
10795          .text segment, not in the .data segment.  */
10796       if (NEED_GOT_RELOC && flag_pic && making_const_table &&
10797           (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF))
10798         {
10799           if (GET_CODE (x) == SYMBOL_REF
10800               && (CONSTANT_POOL_ADDRESS_P (x)
10801                   || SYMBOL_REF_LOCAL_P (x)))
10802             fputs ("(GOTOFF)", asm_out_file);
10803           else if (GET_CODE (x) == LABEL_REF)
10804             fputs ("(GOTOFF)", asm_out_file);
10805           else
10806             fputs ("(GOT)", asm_out_file);
10807         }
10808       fputc ('\n', asm_out_file);
10809       return true;
10810     }
10811
10812   if (arm_vector_mode_supported_p (GET_MODE (x)))
10813     {
10814       int i, units;
10815
10816       if (GET_CODE (x) != CONST_VECTOR)
10817         abort ();
10818
10819       units = CONST_VECTOR_NUNITS (x);
10820
10821       switch (GET_MODE (x))
10822         {
10823         case V2SImode: size = 4; break;
10824         case V4HImode: size = 2; break;
10825         case V8QImode: size = 1; break;
10826         default:
10827           abort ();
10828         }
10829
10830       for (i = 0; i < units; i++)
10831         {
10832           rtx elt;
10833
10834           elt = CONST_VECTOR_ELT (x, i);
10835           assemble_integer
10836             (elt, size, i == 0 ? BIGGEST_ALIGNMENT : size * BITS_PER_UNIT, 1);
10837         }
10838
10839       return true;
10840     }
10841
10842   return default_assemble_integer (x, size, aligned_p);
10843 }
10844 #endif
10845 \f
10846 /* A finite state machine takes care of noticing whether or not instructions
10847    can be conditionally executed, and thus decrease execution time and code
10848    size by deleting branch instructions.  The fsm is controlled by
10849    final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE.  */
10850
10851 /* The state of the fsm controlling condition codes are:
10852    0: normal, do nothing special
10853    1: make ASM_OUTPUT_OPCODE not output this instruction
10854    2: make ASM_OUTPUT_OPCODE not output this instruction
10855    3: make instructions conditional
10856    4: make instructions conditional
10857
10858    State transitions (state->state by whom under condition):
10859    0 -> 1 final_prescan_insn if the `target' is a label
10860    0 -> 2 final_prescan_insn if the `target' is an unconditional branch
10861    1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
10862    2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
10863    3 -> 0 (*targetm.asm_out.internal_label) if the `target' label is reached
10864           (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
10865    4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
10866           (the target insn is arm_target_insn).
10867
10868    If the jump clobbers the conditions then we use states 2 and 4.
10869
10870    A similar thing can be done with conditional return insns.
10871
10872    XXX In case the `target' is an unconditional branch, this conditionalising
10873    of the instructions always reduces code size, but not always execution
10874    time.  But then, I want to reduce the code size to somewhere near what
10875    /bin/cc produces.  */
10876
10877 /* Returns the index of the ARM condition code string in
10878    `arm_condition_codes'.  COMPARISON should be an rtx like
10879    `(eq (...) (...))'.  */
10880 static enum arm_cond_code
10881 get_arm_condition_code (rtx comparison)
10882 {
10883   enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
10884   int code;
10885   enum rtx_code comp_code = GET_CODE (comparison);
10886
10887   if (GET_MODE_CLASS (mode) != MODE_CC)
10888     mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
10889                            XEXP (comparison, 1));
10890
10891   switch (mode)
10892     {
10893     case CC_DNEmode: code = ARM_NE; goto dominance;
10894     case CC_DEQmode: code = ARM_EQ; goto dominance;
10895     case CC_DGEmode: code = ARM_GE; goto dominance;
10896     case CC_DGTmode: code = ARM_GT; goto dominance;
10897     case CC_DLEmode: code = ARM_LE; goto dominance;
10898     case CC_DLTmode: code = ARM_LT; goto dominance;
10899     case CC_DGEUmode: code = ARM_CS; goto dominance;
10900     case CC_DGTUmode: code = ARM_HI; goto dominance;
10901     case CC_DLEUmode: code = ARM_LS; goto dominance;
10902     case CC_DLTUmode: code = ARM_CC;
10903
10904     dominance:
10905       if (comp_code != EQ && comp_code != NE)
10906         abort ();
10907
10908       if (comp_code == EQ)
10909         return ARM_INVERSE_CONDITION_CODE (code);
10910       return code;
10911
10912     case CC_NOOVmode:
10913       switch (comp_code)
10914         {
10915         case NE: return ARM_NE;
10916         case EQ: return ARM_EQ;
10917         case GE: return ARM_PL;
10918         case LT: return ARM_MI;
10919         default: abort ();
10920         }
10921
10922     case CC_Zmode:
10923       switch (comp_code)
10924         {
10925         case NE: return ARM_NE;
10926         case EQ: return ARM_EQ;
10927         default: abort ();
10928         }
10929
10930     case CC_Nmode:
10931       switch (comp_code)
10932         {
10933         case NE: return ARM_MI;
10934         case EQ: return ARM_PL;
10935         default: abort ();
10936         }
10937
10938     case CCFPEmode:
10939     case CCFPmode:
10940       /* These encodings assume that AC=1 in the FPA system control
10941          byte.  This allows us to handle all cases except UNEQ and
10942          LTGT.  */
10943       switch (comp_code)
10944         {
10945         case GE: return ARM_GE;
10946         case GT: return ARM_GT;
10947         case LE: return ARM_LS;
10948         case LT: return ARM_MI;
10949         case NE: return ARM_NE;
10950         case EQ: return ARM_EQ;
10951         case ORDERED: return ARM_VC;
10952         case UNORDERED: return ARM_VS;
10953         case UNLT: return ARM_LT;
10954         case UNLE: return ARM_LE;
10955         case UNGT: return ARM_HI;
10956         case UNGE: return ARM_PL;
10957           /* UNEQ and LTGT do not have a representation.  */
10958         case UNEQ: /* Fall through.  */
10959         case LTGT: /* Fall through.  */
10960         default: abort ();
10961         }
10962
10963     case CC_SWPmode:
10964       switch (comp_code)
10965         {
10966         case NE: return ARM_NE;
10967         case EQ: return ARM_EQ;
10968         case GE: return ARM_LE;
10969         case GT: return ARM_LT;
10970         case LE: return ARM_GE;
10971         case LT: return ARM_GT;
10972         case GEU: return ARM_LS;
10973         case GTU: return ARM_CC;
10974         case LEU: return ARM_CS;
10975         case LTU: return ARM_HI;
10976         default: abort ();
10977         }
10978
10979     case CC_Cmode:
10980       switch (comp_code)
10981       {
10982       case LTU: return ARM_CS;
10983       case GEU: return ARM_CC;
10984       default: abort ();
10985       }
10986
10987     case CCmode:
10988       switch (comp_code)
10989         {
10990         case NE: return ARM_NE;
10991         case EQ: return ARM_EQ;
10992         case GE: return ARM_GE;
10993         case GT: return ARM_GT;
10994         case LE: return ARM_LE;
10995         case LT: return ARM_LT;
10996         case GEU: return ARM_CS;
10997         case GTU: return ARM_HI;
10998         case LEU: return ARM_LS;
10999         case LTU: return ARM_CC;
11000         default: abort ();
11001         }
11002
11003     default: abort ();
11004     }
11005
11006   abort ();
11007 }
11008
11009 void
11010 arm_final_prescan_insn (rtx insn)
11011 {
11012   /* BODY will hold the body of INSN.  */
11013   rtx body = PATTERN (insn);
11014
11015   /* This will be 1 if trying to repeat the trick, and things need to be
11016      reversed if it appears to fail.  */
11017   int reverse = 0;
11018
11019   /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
11020      taken are clobbered, even if the rtl suggests otherwise.  It also
11021      means that we have to grub around within the jump expression to find
11022      out what the conditions are when the jump isn't taken.  */
11023   int jump_clobbers = 0;
11024
11025   /* If we start with a return insn, we only succeed if we find another one.  */
11026   int seeking_return = 0;
11027
11028   /* START_INSN will hold the insn from where we start looking.  This is the
11029      first insn after the following code_label if REVERSE is true.  */
11030   rtx start_insn = insn;
11031
11032   /* If in state 4, check if the target branch is reached, in order to
11033      change back to state 0.  */
11034   if (arm_ccfsm_state == 4)
11035     {
11036       if (insn == arm_target_insn)
11037         {
11038           arm_target_insn = NULL;
11039           arm_ccfsm_state = 0;
11040         }
11041       return;
11042     }
11043
11044   /* If in state 3, it is possible to repeat the trick, if this insn is an
11045      unconditional branch to a label, and immediately following this branch
11046      is the previous target label which is only used once, and the label this
11047      branch jumps to is not too far off.  */
11048   if (arm_ccfsm_state == 3)
11049     {
11050       if (simplejump_p (insn))
11051         {
11052           start_insn = next_nonnote_insn (start_insn);
11053           if (GET_CODE (start_insn) == BARRIER)
11054             {
11055               /* XXX Isn't this always a barrier?  */
11056               start_insn = next_nonnote_insn (start_insn);
11057             }
11058           if (GET_CODE (start_insn) == CODE_LABEL
11059               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11060               && LABEL_NUSES (start_insn) == 1)
11061             reverse = TRUE;
11062           else
11063             return;
11064         }
11065       else if (GET_CODE (body) == RETURN)
11066         {
11067           start_insn = next_nonnote_insn (start_insn);
11068           if (GET_CODE (start_insn) == BARRIER)
11069             start_insn = next_nonnote_insn (start_insn);
11070           if (GET_CODE (start_insn) == CODE_LABEL
11071               && CODE_LABEL_NUMBER (start_insn) == arm_target_label
11072               && LABEL_NUSES (start_insn) == 1)
11073             {
11074               reverse = TRUE;
11075               seeking_return = 1;
11076             }
11077           else
11078             return;
11079         }
11080       else
11081         return;
11082     }
11083
11084   if (arm_ccfsm_state != 0 && !reverse)
11085     abort ();
11086   if (GET_CODE (insn) != JUMP_INSN)
11087     return;
11088
11089   /* This jump might be paralleled with a clobber of the condition codes
11090      the jump should always come first */
11091   if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
11092     body = XVECEXP (body, 0, 0);
11093
11094   if (reverse
11095       || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
11096           && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
11097     {
11098       int insns_skipped;
11099       int fail = FALSE, succeed = FALSE;
11100       /* Flag which part of the IF_THEN_ELSE is the LABEL_REF.  */
11101       int then_not_else = TRUE;
11102       rtx this_insn = start_insn, label = 0;
11103
11104       /* If the jump cannot be done with one instruction, we cannot
11105          conditionally execute the instruction in the inverse case.  */
11106       if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
11107         {
11108           jump_clobbers = 1;
11109           return;
11110         }
11111
11112       /* Register the insn jumped to.  */
11113       if (reverse)
11114         {
11115           if (!seeking_return)
11116             label = XEXP (SET_SRC (body), 0);
11117         }
11118       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
11119         label = XEXP (XEXP (SET_SRC (body), 1), 0);
11120       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
11121         {
11122           label = XEXP (XEXP (SET_SRC (body), 2), 0);
11123           then_not_else = FALSE;
11124         }
11125       else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
11126         seeking_return = 1;
11127       else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
11128         {
11129           seeking_return = 1;
11130           then_not_else = FALSE;
11131         }
11132       else
11133         abort ();
11134
11135       /* See how many insns this branch skips, and what kind of insns.  If all
11136          insns are okay, and the label or unconditional branch to the same
11137          label is not too far away, succeed.  */
11138       for (insns_skipped = 0;
11139            !fail && !succeed && insns_skipped++ < max_insns_skipped;)
11140         {
11141           rtx scanbody;
11142
11143           this_insn = next_nonnote_insn (this_insn);
11144           if (!this_insn)
11145             break;
11146
11147           switch (GET_CODE (this_insn))
11148             {
11149             case CODE_LABEL:
11150               /* Succeed if it is the target label, otherwise fail since
11151                  control falls in from somewhere else.  */
11152               if (this_insn == label)
11153                 {
11154                   if (jump_clobbers)
11155                     {
11156                       arm_ccfsm_state = 2;
11157                       this_insn = next_nonnote_insn (this_insn);
11158                     }
11159                   else
11160                     arm_ccfsm_state = 1;
11161                   succeed = TRUE;
11162                 }
11163               else
11164                 fail = TRUE;
11165               break;
11166
11167             case BARRIER:
11168               /* Succeed if the following insn is the target label.
11169                  Otherwise fail.
11170                  If return insns are used then the last insn in a function
11171                  will be a barrier.  */
11172               this_insn = next_nonnote_insn (this_insn);
11173               if (this_insn && this_insn == label)
11174                 {
11175                   if (jump_clobbers)
11176                     {
11177                       arm_ccfsm_state = 2;
11178                       this_insn = next_nonnote_insn (this_insn);
11179                     }
11180                   else
11181                     arm_ccfsm_state = 1;
11182                   succeed = TRUE;
11183                 }
11184               else
11185                 fail = TRUE;
11186               break;
11187
11188             case CALL_INSN:
11189               /* The AAPCS says that conditional calls should not be
11190                  used since they make interworking inefficient (the
11191                  linker can't transform BL<cond> into BLX).  That's
11192                  only a problem if the machine has BLX.  */
11193               if (arm_arch5)
11194                 {
11195                   fail = TRUE;
11196                   break;
11197                 }
11198
11199               /* Succeed if the following insn is the target label, or
11200                  if the following two insns are a barrier and the
11201                  target label.  */
11202               this_insn = next_nonnote_insn (this_insn);
11203               if (this_insn && GET_CODE (this_insn) == BARRIER)
11204                 this_insn = next_nonnote_insn (this_insn);
11205
11206               if (this_insn && this_insn == label
11207                   && insns_skipped < max_insns_skipped)
11208                 {
11209                   if (jump_clobbers)
11210                     {
11211                       arm_ccfsm_state = 2;
11212                       this_insn = next_nonnote_insn (this_insn);
11213                     }
11214                   else
11215                     arm_ccfsm_state = 1;
11216                   succeed = TRUE;
11217                 }
11218               else
11219                 fail = TRUE;
11220               break;
11221
11222             case JUMP_INSN:
11223               /* If this is an unconditional branch to the same label, succeed.
11224                  If it is to another label, do nothing.  If it is conditional,
11225                  fail.  */
11226               /* XXX Probably, the tests for SET and the PC are
11227                  unnecessary.  */
11228
11229               scanbody = PATTERN (this_insn);
11230               if (GET_CODE (scanbody) == SET
11231                   && GET_CODE (SET_DEST (scanbody)) == PC)
11232                 {
11233                   if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
11234                       && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
11235                     {
11236                       arm_ccfsm_state = 2;
11237                       succeed = TRUE;
11238                     }
11239                   else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
11240                     fail = TRUE;
11241                 }
11242               /* Fail if a conditional return is undesirable (e.g. on a
11243                  StrongARM), but still allow this if optimizing for size.  */
11244               else if (GET_CODE (scanbody) == RETURN
11245                        && !use_return_insn (TRUE, NULL)
11246                        && !optimize_size)
11247                 fail = TRUE;
11248               else if (GET_CODE (scanbody) == RETURN
11249                        && seeking_return)
11250                 {
11251                   arm_ccfsm_state = 2;
11252                   succeed = TRUE;
11253                 }
11254               else if (GET_CODE (scanbody) == PARALLEL)
11255                 {
11256                   switch (get_attr_conds (this_insn))
11257                     {
11258                     case CONDS_NOCOND:
11259                       break;
11260                     default:
11261                       fail = TRUE;
11262                       break;
11263                     }
11264                 }
11265               else
11266                 fail = TRUE;    /* Unrecognized jump (e.g. epilogue).  */
11267
11268               break;
11269
11270             case INSN:
11271               /* Instructions using or affecting the condition codes make it
11272                  fail.  */
11273               scanbody = PATTERN (this_insn);
11274               if (!(GET_CODE (scanbody) == SET
11275                     || GET_CODE (scanbody) == PARALLEL)
11276                   || get_attr_conds (this_insn) != CONDS_NOCOND)
11277                 fail = TRUE;
11278
11279               /* A conditional cirrus instruction must be followed by
11280                  a non Cirrus instruction.  However, since we
11281                  conditionalize instructions in this function and by
11282                  the time we get here we can't add instructions
11283                  (nops), because shorten_branches() has already been
11284                  called, we will disable conditionalizing Cirrus
11285                  instructions to be safe.  */
11286               if (GET_CODE (scanbody) != USE
11287                   && GET_CODE (scanbody) != CLOBBER
11288                   && get_attr_cirrus (this_insn) != CIRRUS_NOT)
11289                 fail = TRUE;
11290               break;
11291
11292             default:
11293               break;
11294             }
11295         }
11296       if (succeed)
11297         {
11298           if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
11299             arm_target_label = CODE_LABEL_NUMBER (label);
11300           else if (seeking_return || arm_ccfsm_state == 2)
11301             {
11302               while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
11303                 {
11304                   this_insn = next_nonnote_insn (this_insn);
11305                   if (this_insn && (GET_CODE (this_insn) == BARRIER
11306                                     || GET_CODE (this_insn) == CODE_LABEL))
11307                     abort ();
11308                 }
11309               if (!this_insn)
11310                 {
11311                   /* Oh, dear! we ran off the end.. give up.  */
11312                   recog (PATTERN (insn), insn, NULL);
11313                   arm_ccfsm_state = 0;
11314                   arm_target_insn = NULL;
11315                   return;
11316                 }
11317               arm_target_insn = this_insn;
11318             }
11319           else
11320             abort ();
11321           if (jump_clobbers)
11322             {
11323               if (reverse)
11324                 abort ();
11325               arm_current_cc =
11326                   get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
11327                                                             0), 0), 1));
11328               if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
11329                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11330               if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
11331                 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11332             }
11333           else
11334             {
11335               /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
11336                  what it was.  */
11337               if (!reverse)
11338                 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
11339                                                                0));
11340             }
11341
11342           if (reverse || then_not_else)
11343             arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
11344         }
11345
11346       /* Restore recog_data (getting the attributes of other insns can
11347          destroy this array, but final.c assumes that it remains intact
11348          across this call; since the insn has been recognized already we
11349          call recog direct).  */
11350       recog (PATTERN (insn), insn, NULL);
11351     }
11352 }
11353
11354 /* Returns true if REGNO is a valid register
11355    for holding a quantity of type MODE.  */
11356 int
11357 arm_hard_regno_mode_ok (unsigned int regno, enum machine_mode mode)
11358 {
11359   if (GET_MODE_CLASS (mode) == MODE_CC)
11360     return regno == CC_REGNUM || regno == VFPCC_REGNUM;
11361
11362   if (TARGET_THUMB)
11363     /* For the Thumb we only allow values bigger than SImode in
11364        registers 0 - 6, so that there is always a second low
11365        register available to hold the upper part of the value.
11366        We probably we ought to ensure that the register is the
11367        start of an even numbered register pair.  */
11368     return (ARM_NUM_REGS (mode) < 2) || (regno < LAST_LO_REGNUM);
11369
11370   if (IS_CIRRUS_REGNUM (regno))
11371     /* We have outlawed SI values in Cirrus registers because they
11372        reside in the lower 32 bits, but SF values reside in the
11373        upper 32 bits.  This causes gcc all sorts of grief.  We can't
11374        even split the registers into pairs because Cirrus SI values
11375        get sign extended to 64bits-- aldyh.  */
11376     return (GET_MODE_CLASS (mode) == MODE_FLOAT) || (mode == DImode);
11377
11378   if (IS_VFP_REGNUM (regno))
11379     {
11380       if (mode == SFmode || mode == SImode)
11381         return TRUE;
11382
11383       /* DFmode values are only valid in even register pairs.  */
11384       if (mode == DFmode)
11385         return ((regno - FIRST_VFP_REGNUM) & 1) == 0;
11386       return FALSE;
11387     }
11388
11389   if (IS_IWMMXT_GR_REGNUM (regno))
11390     return mode == SImode;
11391
11392   if (IS_IWMMXT_REGNUM (regno))
11393     return VALID_IWMMXT_REG_MODE (mode);
11394
11395   /* We allow any value to be stored in the general registers.
11396      Restrict doubleword quantities to even register pairs so that we can
11397      use ldrd.  */
11398   if (regno <= LAST_ARM_REGNUM)
11399     return !(TARGET_LDRD && GET_MODE_SIZE (mode) > 4 && (regno & 1) != 0);
11400
11401   if (   regno == FRAME_POINTER_REGNUM
11402       || regno == ARG_POINTER_REGNUM)
11403     /* We only allow integers in the fake hard registers.  */
11404     return GET_MODE_CLASS (mode) == MODE_INT;
11405
11406   /* The only registers left are the FPA registers
11407      which we only allow to hold FP values.  */
11408   return GET_MODE_CLASS (mode) == MODE_FLOAT
11409     && regno >= FIRST_FPA_REGNUM
11410     && regno <= LAST_FPA_REGNUM;
11411 }
11412
11413 int
11414 arm_regno_class (int regno)
11415 {
11416   if (TARGET_THUMB)
11417     {
11418       if (regno == STACK_POINTER_REGNUM)
11419         return STACK_REG;
11420       if (regno == CC_REGNUM)
11421         return CC_REG;
11422       if (regno < 8)
11423         return LO_REGS;
11424       return HI_REGS;
11425     }
11426
11427   if (   regno <= LAST_ARM_REGNUM
11428       || regno == FRAME_POINTER_REGNUM
11429       || regno == ARG_POINTER_REGNUM)
11430     return GENERAL_REGS;
11431
11432   if (regno == CC_REGNUM || regno == VFPCC_REGNUM)
11433     return NO_REGS;
11434
11435   if (IS_CIRRUS_REGNUM (regno))
11436     return CIRRUS_REGS;
11437
11438   if (IS_VFP_REGNUM (regno))
11439     return VFP_REGS;
11440
11441   if (IS_IWMMXT_REGNUM (regno))
11442     return IWMMXT_REGS;
11443
11444   if (IS_IWMMXT_GR_REGNUM (regno))
11445     return IWMMXT_GR_REGS;
11446
11447   return FPA_REGS;
11448 }
11449
11450 /* Handle a special case when computing the offset
11451    of an argument from the frame pointer.  */
11452 int
11453 arm_debugger_arg_offset (int value, rtx addr)
11454 {
11455   rtx insn;
11456
11457   /* We are only interested if dbxout_parms() failed to compute the offset.  */
11458   if (value != 0)
11459     return 0;
11460
11461   /* We can only cope with the case where the address is held in a register.  */
11462   if (GET_CODE (addr) != REG)
11463     return 0;
11464
11465   /* If we are using the frame pointer to point at the argument, then
11466      an offset of 0 is correct.  */
11467   if (REGNO (addr) == (unsigned) HARD_FRAME_POINTER_REGNUM)
11468     return 0;
11469
11470   /* If we are using the stack pointer to point at the
11471      argument, then an offset of 0 is correct.  */
11472   if ((TARGET_THUMB || !frame_pointer_needed)
11473       && REGNO (addr) == SP_REGNUM)
11474     return 0;
11475
11476   /* Oh dear.  The argument is pointed to by a register rather
11477      than being held in a register, or being stored at a known
11478      offset from the frame pointer.  Since GDB only understands
11479      those two kinds of argument we must translate the address
11480      held in the register into an offset from the frame pointer.
11481      We do this by searching through the insns for the function
11482      looking to see where this register gets its value.  If the
11483      register is initialized from the frame pointer plus an offset
11484      then we are in luck and we can continue, otherwise we give up.
11485
11486      This code is exercised by producing debugging information
11487      for a function with arguments like this:
11488
11489            double func (double a, double b, int c, double d) {return d;}
11490
11491      Without this code the stab for parameter 'd' will be set to
11492      an offset of 0 from the frame pointer, rather than 8.  */
11493
11494   /* The if() statement says:
11495
11496      If the insn is a normal instruction
11497      and if the insn is setting the value in a register
11498      and if the register being set is the register holding the address of the argument
11499      and if the address is computing by an addition
11500      that involves adding to a register
11501      which is the frame pointer
11502      a constant integer
11503
11504      then...  */
11505
11506   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
11507     {
11508       if (   GET_CODE (insn) == INSN
11509           && GET_CODE (PATTERN (insn)) == SET
11510           && REGNO    (XEXP (PATTERN (insn), 0)) == REGNO (addr)
11511           && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
11512           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
11513           && REGNO    (XEXP (XEXP (PATTERN (insn), 1), 0)) == (unsigned) HARD_FRAME_POINTER_REGNUM
11514           && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
11515              )
11516         {
11517           value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
11518
11519           break;
11520         }
11521     }
11522
11523   if (value == 0)
11524     {
11525       debug_rtx (addr);
11526       warning ("unable to compute real location of stacked parameter");
11527       value = 8; /* XXX magic hack */
11528     }
11529
11530   return value;
11531 }
11532 \f
11533 #define def_mbuiltin(MASK, NAME, TYPE, CODE)                            \
11534   do                                                                    \
11535     {                                                                   \
11536       if ((MASK) & insn_flags)                                          \
11537         lang_hooks.builtin_function ((NAME), (TYPE), (CODE),            \
11538                                      BUILT_IN_MD, NULL, NULL_TREE);     \
11539     }                                                                   \
11540   while (0)
11541
11542 struct builtin_description
11543 {
11544   const unsigned int       mask;
11545   const enum insn_code     icode;
11546   const char * const       name;
11547   const enum arm_builtins  code;
11548   const enum rtx_code      comparison;
11549   const unsigned int       flag;
11550 };
11551
11552 static const struct builtin_description bdesc_2arg[] =
11553 {
11554 #define IWMMXT_BUILTIN(code, string, builtin) \
11555   { FL_IWMMXT, CODE_FOR_##code, "__builtin_arm_" string, \
11556     ARM_BUILTIN_##builtin, 0, 0 },
11557
11558   IWMMXT_BUILTIN (addv8qi3, "waddb", WADDB)
11559   IWMMXT_BUILTIN (addv4hi3, "waddh", WADDH)
11560   IWMMXT_BUILTIN (addv2si3, "waddw", WADDW)
11561   IWMMXT_BUILTIN (subv8qi3, "wsubb", WSUBB)
11562   IWMMXT_BUILTIN (subv4hi3, "wsubh", WSUBH)
11563   IWMMXT_BUILTIN (subv2si3, "wsubw", WSUBW)
11564   IWMMXT_BUILTIN (ssaddv8qi3, "waddbss", WADDSSB)
11565   IWMMXT_BUILTIN (ssaddv4hi3, "waddhss", WADDSSH)
11566   IWMMXT_BUILTIN (ssaddv2si3, "waddwss", WADDSSW)
11567   IWMMXT_BUILTIN (sssubv8qi3, "wsubbss", WSUBSSB)
11568   IWMMXT_BUILTIN (sssubv4hi3, "wsubhss", WSUBSSH)
11569   IWMMXT_BUILTIN (sssubv2si3, "wsubwss", WSUBSSW)
11570   IWMMXT_BUILTIN (usaddv8qi3, "waddbus", WADDUSB)
11571   IWMMXT_BUILTIN (usaddv4hi3, "waddhus", WADDUSH)
11572   IWMMXT_BUILTIN (usaddv2si3, "waddwus", WADDUSW)
11573   IWMMXT_BUILTIN (ussubv8qi3, "wsubbus", WSUBUSB)
11574   IWMMXT_BUILTIN (ussubv4hi3, "wsubhus", WSUBUSH)
11575   IWMMXT_BUILTIN (ussubv2si3, "wsubwus", WSUBUSW)
11576   IWMMXT_BUILTIN (mulv4hi3, "wmulul", WMULUL)
11577   IWMMXT_BUILTIN (smulv4hi3_highpart, "wmulsm", WMULSM)
11578   IWMMXT_BUILTIN (umulv4hi3_highpart, "wmulum", WMULUM)
11579   IWMMXT_BUILTIN (eqv8qi3, "wcmpeqb", WCMPEQB)
11580   IWMMXT_BUILTIN (eqv4hi3, "wcmpeqh", WCMPEQH)
11581   IWMMXT_BUILTIN (eqv2si3, "wcmpeqw", WCMPEQW)
11582   IWMMXT_BUILTIN (gtuv8qi3, "wcmpgtub", WCMPGTUB)
11583   IWMMXT_BUILTIN (gtuv4hi3, "wcmpgtuh", WCMPGTUH)
11584   IWMMXT_BUILTIN (gtuv2si3, "wcmpgtuw", WCMPGTUW)
11585   IWMMXT_BUILTIN (gtv8qi3, "wcmpgtsb", WCMPGTSB)
11586   IWMMXT_BUILTIN (gtv4hi3, "wcmpgtsh", WCMPGTSH)
11587   IWMMXT_BUILTIN (gtv2si3, "wcmpgtsw", WCMPGTSW)
11588   IWMMXT_BUILTIN (umaxv8qi3, "wmaxub", WMAXUB)
11589   IWMMXT_BUILTIN (smaxv8qi3, "wmaxsb", WMAXSB)
11590   IWMMXT_BUILTIN (umaxv4hi3, "wmaxuh", WMAXUH)
11591   IWMMXT_BUILTIN (smaxv4hi3, "wmaxsh", WMAXSH)
11592   IWMMXT_BUILTIN (umaxv2si3, "wmaxuw", WMAXUW)
11593   IWMMXT_BUILTIN (smaxv2si3, "wmaxsw", WMAXSW)
11594   IWMMXT_BUILTIN (uminv8qi3, "wminub", WMINUB)
11595   IWMMXT_BUILTIN (sminv8qi3, "wminsb", WMINSB)
11596   IWMMXT_BUILTIN (uminv4hi3, "wminuh", WMINUH)
11597   IWMMXT_BUILTIN (sminv4hi3, "wminsh", WMINSH)
11598   IWMMXT_BUILTIN (uminv2si3, "wminuw", WMINUW)
11599   IWMMXT_BUILTIN (sminv2si3, "wminsw", WMINSW)
11600   IWMMXT_BUILTIN (iwmmxt_anddi3, "wand", WAND)
11601   IWMMXT_BUILTIN (iwmmxt_nanddi3, "wandn", WANDN)
11602   IWMMXT_BUILTIN (iwmmxt_iordi3, "wor", WOR)
11603   IWMMXT_BUILTIN (iwmmxt_xordi3, "wxor", WXOR)
11604   IWMMXT_BUILTIN (iwmmxt_uavgv8qi3, "wavg2b", WAVG2B)
11605   IWMMXT_BUILTIN (iwmmxt_uavgv4hi3, "wavg2h", WAVG2H)
11606   IWMMXT_BUILTIN (iwmmxt_uavgrndv8qi3, "wavg2br", WAVG2BR)
11607   IWMMXT_BUILTIN (iwmmxt_uavgrndv4hi3, "wavg2hr", WAVG2HR)
11608   IWMMXT_BUILTIN (iwmmxt_wunpckilb, "wunpckilb", WUNPCKILB)
11609   IWMMXT_BUILTIN (iwmmxt_wunpckilh, "wunpckilh", WUNPCKILH)
11610   IWMMXT_BUILTIN (iwmmxt_wunpckilw, "wunpckilw", WUNPCKILW)
11611   IWMMXT_BUILTIN (iwmmxt_wunpckihb, "wunpckihb", WUNPCKIHB)
11612   IWMMXT_BUILTIN (iwmmxt_wunpckihh, "wunpckihh", WUNPCKIHH)
11613   IWMMXT_BUILTIN (iwmmxt_wunpckihw, "wunpckihw", WUNPCKIHW)
11614   IWMMXT_BUILTIN (iwmmxt_wmadds, "wmadds", WMADDS)
11615   IWMMXT_BUILTIN (iwmmxt_wmaddu, "wmaddu", WMADDU)
11616
11617 #define IWMMXT_BUILTIN2(code, builtin) \
11618   { FL_IWMMXT, CODE_FOR_##code, NULL, ARM_BUILTIN_##builtin, 0, 0 },
11619
11620   IWMMXT_BUILTIN2 (iwmmxt_wpackhss, WPACKHSS)
11621   IWMMXT_BUILTIN2 (iwmmxt_wpackwss, WPACKWSS)
11622   IWMMXT_BUILTIN2 (iwmmxt_wpackdss, WPACKDSS)
11623   IWMMXT_BUILTIN2 (iwmmxt_wpackhus, WPACKHUS)
11624   IWMMXT_BUILTIN2 (iwmmxt_wpackwus, WPACKWUS)
11625   IWMMXT_BUILTIN2 (iwmmxt_wpackdus, WPACKDUS)
11626   IWMMXT_BUILTIN2 (ashlv4hi3_di,    WSLLH)
11627   IWMMXT_BUILTIN2 (ashlv4hi3,       WSLLHI)
11628   IWMMXT_BUILTIN2 (ashlv2si3_di,    WSLLW)
11629   IWMMXT_BUILTIN2 (ashlv2si3,       WSLLWI)
11630   IWMMXT_BUILTIN2 (ashldi3_di,      WSLLD)
11631   IWMMXT_BUILTIN2 (ashldi3_iwmmxt,  WSLLDI)
11632   IWMMXT_BUILTIN2 (lshrv4hi3_di,    WSRLH)
11633   IWMMXT_BUILTIN2 (lshrv4hi3,       WSRLHI)
11634   IWMMXT_BUILTIN2 (lshrv2si3_di,    WSRLW)
11635   IWMMXT_BUILTIN2 (lshrv2si3,       WSRLWI)
11636   IWMMXT_BUILTIN2 (lshrdi3_di,      WSRLD)
11637   IWMMXT_BUILTIN2 (lshrdi3_iwmmxt,  WSRLDI)
11638   IWMMXT_BUILTIN2 (ashrv4hi3_di,    WSRAH)
11639   IWMMXT_BUILTIN2 (ashrv4hi3,       WSRAHI)
11640   IWMMXT_BUILTIN2 (ashrv2si3_di,    WSRAW)
11641   IWMMXT_BUILTIN2 (ashrv2si3,       WSRAWI)
11642   IWMMXT_BUILTIN2 (ashrdi3_di,      WSRAD)
11643   IWMMXT_BUILTIN2 (ashrdi3_iwmmxt,  WSRADI)
11644   IWMMXT_BUILTIN2 (rorv4hi3_di,     WRORH)
11645   IWMMXT_BUILTIN2 (rorv4hi3,        WRORHI)
11646   IWMMXT_BUILTIN2 (rorv2si3_di,     WRORW)
11647   IWMMXT_BUILTIN2 (rorv2si3,        WRORWI)
11648   IWMMXT_BUILTIN2 (rordi3_di,       WRORD)
11649   IWMMXT_BUILTIN2 (rordi3,          WRORDI)
11650   IWMMXT_BUILTIN2 (iwmmxt_wmacuz,   WMACUZ)
11651   IWMMXT_BUILTIN2 (iwmmxt_wmacsz,   WMACSZ)
11652 };
11653
11654 static const struct builtin_description bdesc_1arg[] =
11655 {
11656   IWMMXT_BUILTIN (iwmmxt_tmovmskb, "tmovmskb", TMOVMSKB)
11657   IWMMXT_BUILTIN (iwmmxt_tmovmskh, "tmovmskh", TMOVMSKH)
11658   IWMMXT_BUILTIN (iwmmxt_tmovmskw, "tmovmskw", TMOVMSKW)
11659   IWMMXT_BUILTIN (iwmmxt_waccb, "waccb", WACCB)
11660   IWMMXT_BUILTIN (iwmmxt_wacch, "wacch", WACCH)
11661   IWMMXT_BUILTIN (iwmmxt_waccw, "waccw", WACCW)
11662   IWMMXT_BUILTIN (iwmmxt_wunpckehub, "wunpckehub", WUNPCKEHUB)
11663   IWMMXT_BUILTIN (iwmmxt_wunpckehuh, "wunpckehuh", WUNPCKEHUH)
11664   IWMMXT_BUILTIN (iwmmxt_wunpckehuw, "wunpckehuw", WUNPCKEHUW)
11665   IWMMXT_BUILTIN (iwmmxt_wunpckehsb, "wunpckehsb", WUNPCKEHSB)
11666   IWMMXT_BUILTIN (iwmmxt_wunpckehsh, "wunpckehsh", WUNPCKEHSH)
11667   IWMMXT_BUILTIN (iwmmxt_wunpckehsw, "wunpckehsw", WUNPCKEHSW)
11668   IWMMXT_BUILTIN (iwmmxt_wunpckelub, "wunpckelub", WUNPCKELUB)
11669   IWMMXT_BUILTIN (iwmmxt_wunpckeluh, "wunpckeluh", WUNPCKELUH)
11670   IWMMXT_BUILTIN (iwmmxt_wunpckeluw, "wunpckeluw", WUNPCKELUW)
11671   IWMMXT_BUILTIN (iwmmxt_wunpckelsb, "wunpckelsb", WUNPCKELSB)
11672   IWMMXT_BUILTIN (iwmmxt_wunpckelsh, "wunpckelsh", WUNPCKELSH)
11673   IWMMXT_BUILTIN (iwmmxt_wunpckelsw, "wunpckelsw", WUNPCKELSW)
11674 };
11675
11676 /* Set up all the iWMMXt builtins.  This is
11677    not called if TARGET_IWMMXT is zero.  */
11678
11679 static void
11680 arm_init_iwmmxt_builtins (void)
11681 {
11682   const struct builtin_description * d;
11683   size_t i;
11684   tree endlink = void_list_node;
11685
11686   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
11687   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
11688   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
11689
11690   tree int_ftype_int
11691     = build_function_type (integer_type_node,
11692                            tree_cons (NULL_TREE, integer_type_node, endlink));
11693   tree v8qi_ftype_v8qi_v8qi_int
11694     = build_function_type (V8QI_type_node,
11695                            tree_cons (NULL_TREE, V8QI_type_node,
11696                                       tree_cons (NULL_TREE, V8QI_type_node,
11697                                                  tree_cons (NULL_TREE,
11698                                                             integer_type_node,
11699                                                             endlink))));
11700   tree v4hi_ftype_v4hi_int
11701     = build_function_type (V4HI_type_node,
11702                            tree_cons (NULL_TREE, V4HI_type_node,
11703                                       tree_cons (NULL_TREE, integer_type_node,
11704                                                  endlink)));
11705   tree v2si_ftype_v2si_int
11706     = build_function_type (V2SI_type_node,
11707                            tree_cons (NULL_TREE, V2SI_type_node,
11708                                       tree_cons (NULL_TREE, integer_type_node,
11709                                                  endlink)));
11710   tree v2si_ftype_di_di
11711     = build_function_type (V2SI_type_node,
11712                            tree_cons (NULL_TREE, long_long_integer_type_node,
11713                                       tree_cons (NULL_TREE, long_long_integer_type_node,
11714                                                  endlink)));
11715   tree di_ftype_di_int
11716     = build_function_type (long_long_integer_type_node,
11717                            tree_cons (NULL_TREE, long_long_integer_type_node,
11718                                       tree_cons (NULL_TREE, integer_type_node,
11719                                                  endlink)));
11720   tree di_ftype_di_int_int
11721     = build_function_type (long_long_integer_type_node,
11722                            tree_cons (NULL_TREE, long_long_integer_type_node,
11723                                       tree_cons (NULL_TREE, integer_type_node,
11724                                                  tree_cons (NULL_TREE,
11725                                                             integer_type_node,
11726                                                             endlink))));
11727   tree int_ftype_v8qi
11728     = build_function_type (integer_type_node,
11729                            tree_cons (NULL_TREE, V8QI_type_node,
11730                                       endlink));
11731   tree int_ftype_v4hi
11732     = build_function_type (integer_type_node,
11733                            tree_cons (NULL_TREE, V4HI_type_node,
11734                                       endlink));
11735   tree int_ftype_v2si
11736     = build_function_type (integer_type_node,
11737                            tree_cons (NULL_TREE, V2SI_type_node,
11738                                       endlink));
11739   tree int_ftype_v8qi_int
11740     = build_function_type (integer_type_node,
11741                            tree_cons (NULL_TREE, V8QI_type_node,
11742                                       tree_cons (NULL_TREE, integer_type_node,
11743                                                  endlink)));
11744   tree int_ftype_v4hi_int
11745     = build_function_type (integer_type_node,
11746                            tree_cons (NULL_TREE, V4HI_type_node,
11747                                       tree_cons (NULL_TREE, integer_type_node,
11748                                                  endlink)));
11749   tree int_ftype_v2si_int
11750     = build_function_type (integer_type_node,
11751                            tree_cons (NULL_TREE, V2SI_type_node,
11752                                       tree_cons (NULL_TREE, integer_type_node,
11753                                                  endlink)));
11754   tree v8qi_ftype_v8qi_int_int
11755     = build_function_type (V8QI_type_node,
11756                            tree_cons (NULL_TREE, V8QI_type_node,
11757                                       tree_cons (NULL_TREE, integer_type_node,
11758                                                  tree_cons (NULL_TREE,
11759                                                             integer_type_node,
11760                                                             endlink))));
11761   tree v4hi_ftype_v4hi_int_int
11762     = build_function_type (V4HI_type_node,
11763                            tree_cons (NULL_TREE, V4HI_type_node,
11764                                       tree_cons (NULL_TREE, integer_type_node,
11765                                                  tree_cons (NULL_TREE,
11766                                                             integer_type_node,
11767                                                             endlink))));
11768   tree v2si_ftype_v2si_int_int
11769     = build_function_type (V2SI_type_node,
11770                            tree_cons (NULL_TREE, V2SI_type_node,
11771                                       tree_cons (NULL_TREE, integer_type_node,
11772                                                  tree_cons (NULL_TREE,
11773                                                             integer_type_node,
11774                                                             endlink))));
11775   /* Miscellaneous.  */
11776   tree v8qi_ftype_v4hi_v4hi
11777     = build_function_type (V8QI_type_node,
11778                            tree_cons (NULL_TREE, V4HI_type_node,
11779                                       tree_cons (NULL_TREE, V4HI_type_node,
11780                                                  endlink)));
11781   tree v4hi_ftype_v2si_v2si
11782     = build_function_type (V4HI_type_node,
11783                            tree_cons (NULL_TREE, V2SI_type_node,
11784                                       tree_cons (NULL_TREE, V2SI_type_node,
11785                                                  endlink)));
11786   tree v2si_ftype_v4hi_v4hi
11787     = build_function_type (V2SI_type_node,
11788                            tree_cons (NULL_TREE, V4HI_type_node,
11789                                       tree_cons (NULL_TREE, V4HI_type_node,
11790                                                  endlink)));
11791   tree v2si_ftype_v8qi_v8qi
11792     = build_function_type (V2SI_type_node,
11793                            tree_cons (NULL_TREE, V8QI_type_node,
11794                                       tree_cons (NULL_TREE, V8QI_type_node,
11795                                                  endlink)));
11796   tree v4hi_ftype_v4hi_di
11797     = build_function_type (V4HI_type_node,
11798                            tree_cons (NULL_TREE, V4HI_type_node,
11799                                       tree_cons (NULL_TREE,
11800                                                  long_long_integer_type_node,
11801                                                  endlink)));
11802   tree v2si_ftype_v2si_di
11803     = build_function_type (V2SI_type_node,
11804                            tree_cons (NULL_TREE, V2SI_type_node,
11805                                       tree_cons (NULL_TREE,
11806                                                  long_long_integer_type_node,
11807                                                  endlink)));
11808   tree void_ftype_int_int
11809     = build_function_type (void_type_node,
11810                            tree_cons (NULL_TREE, integer_type_node,
11811                                       tree_cons (NULL_TREE, integer_type_node,
11812                                                  endlink)));
11813   tree di_ftype_void
11814     = build_function_type (long_long_unsigned_type_node, endlink);
11815   tree di_ftype_v8qi
11816     = build_function_type (long_long_integer_type_node,
11817                            tree_cons (NULL_TREE, V8QI_type_node,
11818                                       endlink));
11819   tree di_ftype_v4hi
11820     = build_function_type (long_long_integer_type_node,
11821                            tree_cons (NULL_TREE, V4HI_type_node,
11822                                       endlink));
11823   tree di_ftype_v2si
11824     = build_function_type (long_long_integer_type_node,
11825                            tree_cons (NULL_TREE, V2SI_type_node,
11826                                       endlink));
11827   tree v2si_ftype_v4hi
11828     = build_function_type (V2SI_type_node,
11829                            tree_cons (NULL_TREE, V4HI_type_node,
11830                                       endlink));
11831   tree v4hi_ftype_v8qi
11832     = build_function_type (V4HI_type_node,
11833                            tree_cons (NULL_TREE, V8QI_type_node,
11834                                       endlink));
11835
11836   tree di_ftype_di_v4hi_v4hi
11837     = build_function_type (long_long_unsigned_type_node,
11838                            tree_cons (NULL_TREE,
11839                                       long_long_unsigned_type_node,
11840                                       tree_cons (NULL_TREE, V4HI_type_node,
11841                                                  tree_cons (NULL_TREE,
11842                                                             V4HI_type_node,
11843                                                             endlink))));
11844
11845   tree di_ftype_v4hi_v4hi
11846     = build_function_type (long_long_unsigned_type_node,
11847                            tree_cons (NULL_TREE, V4HI_type_node,
11848                                       tree_cons (NULL_TREE, V4HI_type_node,
11849                                                  endlink)));
11850
11851   /* Normal vector binops.  */
11852   tree v8qi_ftype_v8qi_v8qi
11853     = build_function_type (V8QI_type_node,
11854                            tree_cons (NULL_TREE, V8QI_type_node,
11855                                       tree_cons (NULL_TREE, V8QI_type_node,
11856                                                  endlink)));
11857   tree v4hi_ftype_v4hi_v4hi
11858     = build_function_type (V4HI_type_node,
11859                            tree_cons (NULL_TREE, V4HI_type_node,
11860                                       tree_cons (NULL_TREE, V4HI_type_node,
11861                                                  endlink)));
11862   tree v2si_ftype_v2si_v2si
11863     = build_function_type (V2SI_type_node,
11864                            tree_cons (NULL_TREE, V2SI_type_node,
11865                                       tree_cons (NULL_TREE, V2SI_type_node,
11866                                                  endlink)));
11867   tree di_ftype_di_di
11868     = build_function_type (long_long_unsigned_type_node,
11869                            tree_cons (NULL_TREE, long_long_unsigned_type_node,
11870                                       tree_cons (NULL_TREE,
11871                                                  long_long_unsigned_type_node,
11872                                                  endlink)));
11873
11874   /* Add all builtins that are more or less simple operations on two
11875      operands.  */
11876   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
11877     {
11878       /* Use one of the operands; the target can have a different mode for
11879          mask-generating compares.  */
11880       enum machine_mode mode;
11881       tree type;
11882
11883       if (d->name == 0)
11884         continue;
11885
11886       mode = insn_data[d->icode].operand[1].mode;
11887
11888       switch (mode)
11889         {
11890         case V8QImode:
11891           type = v8qi_ftype_v8qi_v8qi;
11892           break;
11893         case V4HImode:
11894           type = v4hi_ftype_v4hi_v4hi;
11895           break;
11896         case V2SImode:
11897           type = v2si_ftype_v2si_v2si;
11898           break;
11899         case DImode:
11900           type = di_ftype_di_di;
11901           break;
11902
11903         default:
11904           abort ();
11905         }
11906
11907       def_mbuiltin (d->mask, d->name, type, d->code);
11908     }
11909
11910   /* Add the remaining MMX insns with somewhat more complicated types.  */
11911   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wzero", di_ftype_void, ARM_BUILTIN_WZERO);
11912   def_mbuiltin (FL_IWMMXT, "__builtin_arm_setwcx", void_ftype_int_int, ARM_BUILTIN_SETWCX);
11913   def_mbuiltin (FL_IWMMXT, "__builtin_arm_getwcx", int_ftype_int, ARM_BUILTIN_GETWCX);
11914
11915   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSLLH);
11916   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllw", v2si_ftype_v2si_di, ARM_BUILTIN_WSLLW);
11917   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslld", di_ftype_di_di, ARM_BUILTIN_WSLLD);
11918   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSLLHI);
11919   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsllwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSLLWI);
11920   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wslldi", di_ftype_di_int, ARM_BUILTIN_WSLLDI);
11921
11922   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRLH);
11923   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRLW);
11924   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrld", di_ftype_di_di, ARM_BUILTIN_WSRLD);
11925   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRLHI);
11926   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrlwi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRLWI);
11927   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrldi", di_ftype_di_int, ARM_BUILTIN_WSRLDI);
11928
11929   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrah", v4hi_ftype_v4hi_di, ARM_BUILTIN_WSRAH);
11930   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsraw", v2si_ftype_v2si_di, ARM_BUILTIN_WSRAW);
11931   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrad", di_ftype_di_di, ARM_BUILTIN_WSRAD);
11932   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrahi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSRAHI);
11933   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsrawi", v2si_ftype_v2si_int, ARM_BUILTIN_WSRAWI);
11934   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsradi", di_ftype_di_int, ARM_BUILTIN_WSRADI);
11935
11936   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorh", v4hi_ftype_v4hi_di, ARM_BUILTIN_WRORH);
11937   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorw", v2si_ftype_v2si_di, ARM_BUILTIN_WRORW);
11938   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrord", di_ftype_di_di, ARM_BUILTIN_WRORD);
11939   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorhi", v4hi_ftype_v4hi_int, ARM_BUILTIN_WRORHI);
11940   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrorwi", v2si_ftype_v2si_int, ARM_BUILTIN_WRORWI);
11941   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wrordi", di_ftype_di_int, ARM_BUILTIN_WRORDI);
11942
11943   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wshufh", v4hi_ftype_v4hi_int, ARM_BUILTIN_WSHUFH);
11944
11945   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadb", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADB);
11946   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadh", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADH);
11947   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadbz", v2si_ftype_v8qi_v8qi, ARM_BUILTIN_WSADBZ);
11948   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wsadhz", v2si_ftype_v4hi_v4hi, ARM_BUILTIN_WSADHZ);
11949
11950   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsb", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMSB);
11951   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMSH);
11952   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmsw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMSW);
11953   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmub", int_ftype_v8qi_int, ARM_BUILTIN_TEXTRMUB);
11954   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuh", int_ftype_v4hi_int, ARM_BUILTIN_TEXTRMUH);
11955   def_mbuiltin (FL_IWMMXT, "__builtin_arm_textrmuw", int_ftype_v2si_int, ARM_BUILTIN_TEXTRMUW);
11956   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrb", v8qi_ftype_v8qi_int_int, ARM_BUILTIN_TINSRB);
11957   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrh", v4hi_ftype_v4hi_int_int, ARM_BUILTIN_TINSRH);
11958   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tinsrw", v2si_ftype_v2si_int_int, ARM_BUILTIN_TINSRW);
11959
11960   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccb", di_ftype_v8qi, ARM_BUILTIN_WACCB);
11961   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wacch", di_ftype_v4hi, ARM_BUILTIN_WACCH);
11962   def_mbuiltin (FL_IWMMXT, "__builtin_arm_waccw", di_ftype_v2si, ARM_BUILTIN_WACCW);
11963
11964   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskb", int_ftype_v8qi, ARM_BUILTIN_TMOVMSKB);
11965   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskh", int_ftype_v4hi, ARM_BUILTIN_TMOVMSKH);
11966   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmovmskw", int_ftype_v2si, ARM_BUILTIN_TMOVMSKW);
11967
11968   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhss", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHSS);
11969   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackhus", v8qi_ftype_v4hi_v4hi, ARM_BUILTIN_WPACKHUS);
11970   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwus", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWUS);
11971   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackwss", v4hi_ftype_v2si_v2si, ARM_BUILTIN_WPACKWSS);
11972   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdus", v2si_ftype_di_di, ARM_BUILTIN_WPACKDUS);
11973   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wpackdss", v2si_ftype_di_di, ARM_BUILTIN_WPACKDSS);
11974
11975   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHUB);
11976   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHUH);
11977   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehuw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHUW);
11978   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKEHSB);
11979   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKEHSH);
11980   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckehsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKEHSW);
11981   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelub", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELUB);
11982   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELUH);
11983   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckeluw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELUW);
11984   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsb", v4hi_ftype_v8qi, ARM_BUILTIN_WUNPCKELSB);
11985   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsh", v2si_ftype_v4hi, ARM_BUILTIN_WUNPCKELSH);
11986   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wunpckelsw", di_ftype_v2si, ARM_BUILTIN_WUNPCKELSW);
11987
11988   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacs", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACS);
11989   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacsz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACSZ);
11990   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacu", di_ftype_di_v4hi_v4hi, ARM_BUILTIN_WMACU);
11991   def_mbuiltin (FL_IWMMXT, "__builtin_arm_wmacuz", di_ftype_v4hi_v4hi, ARM_BUILTIN_WMACUZ);
11992
11993   def_mbuiltin (FL_IWMMXT, "__builtin_arm_walign", v8qi_ftype_v8qi_v8qi_int, ARM_BUILTIN_WALIGN);
11994   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmia", di_ftype_di_int_int, ARM_BUILTIN_TMIA);
11995   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiaph", di_ftype_di_int_int, ARM_BUILTIN_TMIAPH);
11996   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabb", di_ftype_di_int_int, ARM_BUILTIN_TMIABB);
11997   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiabt", di_ftype_di_int_int, ARM_BUILTIN_TMIABT);
11998   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatb", di_ftype_di_int_int, ARM_BUILTIN_TMIATB);
11999   def_mbuiltin (FL_IWMMXT, "__builtin_arm_tmiatt", di_ftype_di_int_int, ARM_BUILTIN_TMIATT);
12000 }
12001
12002 static void
12003 arm_init_builtins (void)
12004 {
12005   if (TARGET_REALLY_IWMMXT)
12006     arm_init_iwmmxt_builtins ();
12007 }
12008
12009 /* Errors in the source file can cause expand_expr to return const0_rtx
12010    where we expect a vector.  To avoid crashing, use one of the vector
12011    clear instructions.  */
12012
12013 static rtx
12014 safe_vector_operand (rtx x, enum machine_mode mode)
12015 {
12016   if (x != const0_rtx)
12017     return x;
12018   x = gen_reg_rtx (mode);
12019
12020   emit_insn (gen_iwmmxt_clrdi (mode == DImode ? x
12021                                : gen_rtx_SUBREG (DImode, x, 0)));
12022   return x;
12023 }
12024
12025 /* Subroutine of arm_expand_builtin to take care of binop insns.  */
12026
12027 static rtx
12028 arm_expand_binop_builtin (enum insn_code icode,
12029                           tree arglist, rtx target)
12030 {
12031   rtx pat;
12032   tree arg0 = TREE_VALUE (arglist);
12033   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12034   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12035   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12036   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12037   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12038   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
12039
12040   if (VECTOR_MODE_P (mode0))
12041     op0 = safe_vector_operand (op0, mode0);
12042   if (VECTOR_MODE_P (mode1))
12043     op1 = safe_vector_operand (op1, mode1);
12044
12045   if (! target
12046       || GET_MODE (target) != tmode
12047       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12048     target = gen_reg_rtx (tmode);
12049
12050   /* In case the insn wants input operands in modes different from
12051      the result, abort.  */
12052   if (GET_MODE (op0) != mode0 || GET_MODE (op1) != mode1)
12053     abort ();
12054
12055   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12056     op0 = copy_to_mode_reg (mode0, op0);
12057   if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12058     op1 = copy_to_mode_reg (mode1, op1);
12059
12060   pat = GEN_FCN (icode) (target, op0, op1);
12061   if (! pat)
12062     return 0;
12063   emit_insn (pat);
12064   return target;
12065 }
12066
12067 /* Subroutine of arm_expand_builtin to take care of unop insns.  */
12068
12069 static rtx
12070 arm_expand_unop_builtin (enum insn_code icode,
12071                          tree arglist, rtx target, int do_load)
12072 {
12073   rtx pat;
12074   tree arg0 = TREE_VALUE (arglist);
12075   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12076   enum machine_mode tmode = insn_data[icode].operand[0].mode;
12077   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
12078
12079   if (! target
12080       || GET_MODE (target) != tmode
12081       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12082     target = gen_reg_rtx (tmode);
12083   if (do_load)
12084     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
12085   else
12086     {
12087       if (VECTOR_MODE_P (mode0))
12088         op0 = safe_vector_operand (op0, mode0);
12089
12090       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12091         op0 = copy_to_mode_reg (mode0, op0);
12092     }
12093
12094   pat = GEN_FCN (icode) (target, op0);
12095   if (! pat)
12096     return 0;
12097   emit_insn (pat);
12098   return target;
12099 }
12100
12101 /* Expand an expression EXP that calls a built-in function,
12102    with result going to TARGET if that's convenient
12103    (and in mode MODE if that's convenient).
12104    SUBTARGET may be used as the target for computing one of EXP's operands.
12105    IGNORE is nonzero if the value is to be ignored.  */
12106
12107 static rtx
12108 arm_expand_builtin (tree exp,
12109                     rtx target,
12110                     rtx subtarget ATTRIBUTE_UNUSED,
12111                     enum machine_mode mode ATTRIBUTE_UNUSED,
12112                     int ignore ATTRIBUTE_UNUSED)
12113 {
12114   const struct builtin_description * d;
12115   enum insn_code    icode;
12116   tree              fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
12117   tree              arglist = TREE_OPERAND (exp, 1);
12118   tree              arg0;
12119   tree              arg1;
12120   tree              arg2;
12121   rtx               op0;
12122   rtx               op1;
12123   rtx               op2;
12124   rtx               pat;
12125   int               fcode = DECL_FUNCTION_CODE (fndecl);
12126   size_t            i;
12127   enum machine_mode tmode;
12128   enum machine_mode mode0;
12129   enum machine_mode mode1;
12130   enum machine_mode mode2;
12131
12132   switch (fcode)
12133     {
12134     case ARM_BUILTIN_TEXTRMSB:
12135     case ARM_BUILTIN_TEXTRMUB:
12136     case ARM_BUILTIN_TEXTRMSH:
12137     case ARM_BUILTIN_TEXTRMUH:
12138     case ARM_BUILTIN_TEXTRMSW:
12139     case ARM_BUILTIN_TEXTRMUW:
12140       icode = (fcode == ARM_BUILTIN_TEXTRMSB ? CODE_FOR_iwmmxt_textrmsb
12141                : fcode == ARM_BUILTIN_TEXTRMUB ? CODE_FOR_iwmmxt_textrmub
12142                : fcode == ARM_BUILTIN_TEXTRMSH ? CODE_FOR_iwmmxt_textrmsh
12143                : fcode == ARM_BUILTIN_TEXTRMUH ? CODE_FOR_iwmmxt_textrmuh
12144                : CODE_FOR_iwmmxt_textrmw);
12145
12146       arg0 = TREE_VALUE (arglist);
12147       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12148       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12149       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12150       tmode = insn_data[icode].operand[0].mode;
12151       mode0 = insn_data[icode].operand[1].mode;
12152       mode1 = insn_data[icode].operand[2].mode;
12153
12154       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12155         op0 = copy_to_mode_reg (mode0, op0);
12156       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12157         {
12158           /* @@@ better error message */
12159           error ("selector must be an immediate");
12160           return gen_reg_rtx (tmode);
12161         }
12162       if (target == 0
12163           || GET_MODE (target) != tmode
12164           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12165         target = gen_reg_rtx (tmode);
12166       pat = GEN_FCN (icode) (target, op0, op1);
12167       if (! pat)
12168         return 0;
12169       emit_insn (pat);
12170       return target;
12171
12172     case ARM_BUILTIN_TINSRB:
12173     case ARM_BUILTIN_TINSRH:
12174     case ARM_BUILTIN_TINSRW:
12175       icode = (fcode == ARM_BUILTIN_TINSRB ? CODE_FOR_iwmmxt_tinsrb
12176                : fcode == ARM_BUILTIN_TINSRH ? CODE_FOR_iwmmxt_tinsrh
12177                : CODE_FOR_iwmmxt_tinsrw);
12178       arg0 = TREE_VALUE (arglist);
12179       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12180       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12181       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12182       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12183       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12184       tmode = insn_data[icode].operand[0].mode;
12185       mode0 = insn_data[icode].operand[1].mode;
12186       mode1 = insn_data[icode].operand[2].mode;
12187       mode2 = insn_data[icode].operand[3].mode;
12188
12189       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12190         op0 = copy_to_mode_reg (mode0, op0);
12191       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12192         op1 = copy_to_mode_reg (mode1, op1);
12193       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12194         {
12195           /* @@@ better error message */
12196           error ("selector must be an immediate");
12197           return const0_rtx;
12198         }
12199       if (target == 0
12200           || GET_MODE (target) != tmode
12201           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12202         target = gen_reg_rtx (tmode);
12203       pat = GEN_FCN (icode) (target, op0, op1, op2);
12204       if (! pat)
12205         return 0;
12206       emit_insn (pat);
12207       return target;
12208
12209     case ARM_BUILTIN_SETWCX:
12210       arg0 = TREE_VALUE (arglist);
12211       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12212       op0 = force_reg (SImode, expand_expr (arg0, NULL_RTX, VOIDmode, 0));
12213       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12214       emit_insn (gen_iwmmxt_tmcr (op1, op0));
12215       return 0;
12216
12217     case ARM_BUILTIN_GETWCX:
12218       arg0 = TREE_VALUE (arglist);
12219       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12220       target = gen_reg_rtx (SImode);
12221       emit_insn (gen_iwmmxt_tmrc (target, op0));
12222       return target;
12223
12224     case ARM_BUILTIN_WSHUFH:
12225       icode = CODE_FOR_iwmmxt_wshufh;
12226       arg0 = TREE_VALUE (arglist);
12227       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12228       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12229       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12230       tmode = insn_data[icode].operand[0].mode;
12231       mode1 = insn_data[icode].operand[1].mode;
12232       mode2 = insn_data[icode].operand[2].mode;
12233
12234       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
12235         op0 = copy_to_mode_reg (mode1, op0);
12236       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
12237         {
12238           /* @@@ better error message */
12239           error ("mask must be an immediate");
12240           return const0_rtx;
12241         }
12242       if (target == 0
12243           || GET_MODE (target) != tmode
12244           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12245         target = gen_reg_rtx (tmode);
12246       pat = GEN_FCN (icode) (target, op0, op1);
12247       if (! pat)
12248         return 0;
12249       emit_insn (pat);
12250       return target;
12251
12252     case ARM_BUILTIN_WSADB:
12253       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadb, arglist, target);
12254     case ARM_BUILTIN_WSADH:
12255       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadh, arglist, target);
12256     case ARM_BUILTIN_WSADBZ:
12257       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadbz, arglist, target);
12258     case ARM_BUILTIN_WSADHZ:
12259       return arm_expand_binop_builtin (CODE_FOR_iwmmxt_wsadhz, arglist, target);
12260
12261       /* Several three-argument builtins.  */
12262     case ARM_BUILTIN_WMACS:
12263     case ARM_BUILTIN_WMACU:
12264     case ARM_BUILTIN_WALIGN:
12265     case ARM_BUILTIN_TMIA:
12266     case ARM_BUILTIN_TMIAPH:
12267     case ARM_BUILTIN_TMIATT:
12268     case ARM_BUILTIN_TMIATB:
12269     case ARM_BUILTIN_TMIABT:
12270     case ARM_BUILTIN_TMIABB:
12271       icode = (fcode == ARM_BUILTIN_WMACS ? CODE_FOR_iwmmxt_wmacs
12272                : fcode == ARM_BUILTIN_WMACU ? CODE_FOR_iwmmxt_wmacu
12273                : fcode == ARM_BUILTIN_TMIA ? CODE_FOR_iwmmxt_tmia
12274                : fcode == ARM_BUILTIN_TMIAPH ? CODE_FOR_iwmmxt_tmiaph
12275                : fcode == ARM_BUILTIN_TMIABB ? CODE_FOR_iwmmxt_tmiabb
12276                : fcode == ARM_BUILTIN_TMIABT ? CODE_FOR_iwmmxt_tmiabt
12277                : fcode == ARM_BUILTIN_TMIATB ? CODE_FOR_iwmmxt_tmiatb
12278                : fcode == ARM_BUILTIN_TMIATT ? CODE_FOR_iwmmxt_tmiatt
12279                : CODE_FOR_iwmmxt_walign);
12280       arg0 = TREE_VALUE (arglist);
12281       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
12282       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
12283       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
12284       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
12285       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
12286       tmode = insn_data[icode].operand[0].mode;
12287       mode0 = insn_data[icode].operand[1].mode;
12288       mode1 = insn_data[icode].operand[2].mode;
12289       mode2 = insn_data[icode].operand[3].mode;
12290
12291       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
12292         op0 = copy_to_mode_reg (mode0, op0);
12293       if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
12294         op1 = copy_to_mode_reg (mode1, op1);
12295       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
12296         op2 = copy_to_mode_reg (mode2, op2);
12297       if (target == 0
12298           || GET_MODE (target) != tmode
12299           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
12300         target = gen_reg_rtx (tmode);
12301       pat = GEN_FCN (icode) (target, op0, op1, op2);
12302       if (! pat)
12303         return 0;
12304       emit_insn (pat);
12305       return target;
12306
12307     case ARM_BUILTIN_WZERO:
12308       target = gen_reg_rtx (DImode);
12309       emit_insn (gen_iwmmxt_clrdi (target));
12310       return target;
12311
12312     default:
12313       break;
12314     }
12315
12316   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
12317     if (d->code == (const enum arm_builtins) fcode)
12318       return arm_expand_binop_builtin (d->icode, arglist, target);
12319
12320   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
12321     if (d->code == (const enum arm_builtins) fcode)
12322       return arm_expand_unop_builtin (d->icode, arglist, target, 0);
12323
12324   /* @@@ Should really do something sensible here.  */
12325   return NULL_RTX;
12326 }
12327 \f
12328 /* Recursively search through all of the blocks in a function
12329    checking to see if any of the variables created in that
12330    function match the RTX called 'orig'.  If they do then
12331    replace them with the RTX called 'new'.  */
12332 static void
12333 replace_symbols_in_block (tree block, rtx orig, rtx new)
12334 {
12335   for (; block; block = BLOCK_CHAIN (block))
12336     {
12337       tree sym;
12338
12339       if (!TREE_USED (block))
12340         continue;
12341
12342       for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
12343         {
12344           if (  (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
12345               || DECL_IGNORED_P (sym)
12346               || TREE_CODE (sym) != VAR_DECL
12347               || DECL_EXTERNAL (sym)
12348               || !rtx_equal_p (DECL_RTL (sym), orig)
12349               )
12350             continue;
12351
12352           SET_DECL_RTL (sym, new);
12353         }
12354
12355       replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
12356     }
12357 }
12358
12359 /* Return the number (counting from 0) of
12360    the least significant set bit in MASK.  */
12361
12362 inline static int
12363 number_of_first_bit_set (int mask)
12364 {
12365   int bit;
12366
12367   for (bit = 0;
12368        (mask & (1 << bit)) == 0;
12369        ++bit)
12370     continue;
12371
12372   return bit;
12373 }
12374
12375 /* Generate code to return from a thumb function.
12376    If 'reg_containing_return_addr' is -1, then the return address is
12377    actually on the stack, at the stack pointer.  */
12378 static void
12379 thumb_exit (FILE *f, int reg_containing_return_addr)
12380 {
12381   unsigned regs_available_for_popping;
12382   unsigned regs_to_pop;
12383   int pops_needed;
12384   unsigned available;
12385   unsigned required;
12386   int mode;
12387   int size;
12388   int restore_a4 = FALSE;
12389
12390   /* Compute the registers we need to pop.  */
12391   regs_to_pop = 0;
12392   pops_needed = 0;
12393
12394   if (reg_containing_return_addr == -1)
12395     {
12396       regs_to_pop |= 1 << LR_REGNUM;
12397       ++pops_needed;
12398     }
12399
12400   if (TARGET_BACKTRACE)
12401     {
12402       /* Restore the (ARM) frame pointer and stack pointer.  */
12403       regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
12404       pops_needed += 2;
12405     }
12406
12407   /* If there is nothing to pop then just emit the BX instruction and
12408      return.  */
12409   if (pops_needed == 0)
12410     {
12411       if (current_function_calls_eh_return)
12412         asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12413
12414       asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12415       return;
12416     }
12417   /* Otherwise if we are not supporting interworking and we have not created
12418      a backtrace structure and the function was not entered in ARM mode then
12419      just pop the return address straight into the PC.  */
12420   else if (!TARGET_INTERWORK
12421            && !TARGET_BACKTRACE
12422            && !is_called_in_ARM_mode (current_function_decl)
12423            && !current_function_calls_eh_return)
12424     {
12425       asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
12426       return;
12427     }
12428
12429   /* Find out how many of the (return) argument registers we can corrupt.  */
12430   regs_available_for_popping = 0;
12431
12432   /* If returning via __builtin_eh_return, the bottom three registers
12433      all contain information needed for the return.  */
12434   if (current_function_calls_eh_return)
12435     size = 12;
12436   else
12437     {
12438       /* If we can deduce the registers used from the function's
12439          return value.  This is more reliable that examining
12440          regs_ever_live[] because that will be set if the register is
12441          ever used in the function, not just if the register is used
12442          to hold a return value.  */
12443
12444       if (current_function_return_rtx != 0)
12445         mode = GET_MODE (current_function_return_rtx);
12446       else
12447         mode = DECL_MODE (DECL_RESULT (current_function_decl));
12448
12449       size = GET_MODE_SIZE (mode);
12450
12451       if (size == 0)
12452         {
12453           /* In a void function we can use any argument register.
12454              In a function that returns a structure on the stack
12455              we can use the second and third argument registers.  */
12456           if (mode == VOIDmode)
12457             regs_available_for_popping =
12458               (1 << ARG_REGISTER (1))
12459               | (1 << ARG_REGISTER (2))
12460               | (1 << ARG_REGISTER (3));
12461           else
12462             regs_available_for_popping =
12463               (1 << ARG_REGISTER (2))
12464               | (1 << ARG_REGISTER (3));
12465         }
12466       else if (size <= 4)
12467         regs_available_for_popping =
12468           (1 << ARG_REGISTER (2))
12469           | (1 << ARG_REGISTER (3));
12470       else if (size <= 8)
12471         regs_available_for_popping =
12472           (1 << ARG_REGISTER (3));
12473     }
12474
12475   /* Match registers to be popped with registers into which we pop them.  */
12476   for (available = regs_available_for_popping,
12477        required  = regs_to_pop;
12478        required != 0 && available != 0;
12479        available &= ~(available & - available),
12480        required  &= ~(required  & - required))
12481     -- pops_needed;
12482
12483   /* If we have any popping registers left over, remove them.  */
12484   if (available > 0)
12485     regs_available_for_popping &= ~available;
12486
12487   /* Otherwise if we need another popping register we can use
12488      the fourth argument register.  */
12489   else if (pops_needed)
12490     {
12491       /* If we have not found any free argument registers and
12492          reg a4 contains the return address, we must move it.  */
12493       if (regs_available_for_popping == 0
12494           && reg_containing_return_addr == LAST_ARG_REGNUM)
12495         {
12496           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12497           reg_containing_return_addr = LR_REGNUM;
12498         }
12499       else if (size > 12)
12500         {
12501           /* Register a4 is being used to hold part of the return value,
12502              but we have dire need of a free, low register.  */
12503           restore_a4 = TRUE;
12504
12505           asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
12506         }
12507
12508       if (reg_containing_return_addr != LAST_ARG_REGNUM)
12509         {
12510           /* The fourth argument register is available.  */
12511           regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
12512
12513           --pops_needed;
12514         }
12515     }
12516
12517   /* Pop as many registers as we can.  */
12518   thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12519                  regs_available_for_popping);
12520
12521   /* Process the registers we popped.  */
12522   if (reg_containing_return_addr == -1)
12523     {
12524       /* The return address was popped into the lowest numbered register.  */
12525       regs_to_pop &= ~(1 << LR_REGNUM);
12526
12527       reg_containing_return_addr =
12528         number_of_first_bit_set (regs_available_for_popping);
12529
12530       /* Remove this register for the mask of available registers, so that
12531          the return address will not be corrupted by further pops.  */
12532       regs_available_for_popping &= ~(1 << reg_containing_return_addr);
12533     }
12534
12535   /* If we popped other registers then handle them here.  */
12536   if (regs_available_for_popping)
12537     {
12538       int frame_pointer;
12539
12540       /* Work out which register currently contains the frame pointer.  */
12541       frame_pointer = number_of_first_bit_set (regs_available_for_popping);
12542
12543       /* Move it into the correct place.  */
12544       asm_fprintf (f, "\tmov\t%r, %r\n",
12545                    ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
12546
12547       /* (Temporarily) remove it from the mask of popped registers.  */
12548       regs_available_for_popping &= ~(1 << frame_pointer);
12549       regs_to_pop &= ~(1 << ARM_HARD_FRAME_POINTER_REGNUM);
12550
12551       if (regs_available_for_popping)
12552         {
12553           int stack_pointer;
12554
12555           /* We popped the stack pointer as well,
12556              find the register that contains it.  */
12557           stack_pointer = number_of_first_bit_set (regs_available_for_popping);
12558
12559           /* Move it into the stack register.  */
12560           asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
12561
12562           /* At this point we have popped all necessary registers, so
12563              do not worry about restoring regs_available_for_popping
12564              to its correct value:
12565
12566              assert (pops_needed == 0)
12567              assert (regs_available_for_popping == (1 << frame_pointer))
12568              assert (regs_to_pop == (1 << STACK_POINTER))  */
12569         }
12570       else
12571         {
12572           /* Since we have just move the popped value into the frame
12573              pointer, the popping register is available for reuse, and
12574              we know that we still have the stack pointer left to pop.  */
12575           regs_available_for_popping |= (1 << frame_pointer);
12576         }
12577     }
12578
12579   /* If we still have registers left on the stack, but we no longer have
12580      any registers into which we can pop them, then we must move the return
12581      address into the link register and make available the register that
12582      contained it.  */
12583   if (regs_available_for_popping == 0 && pops_needed > 0)
12584     {
12585       regs_available_for_popping |= 1 << reg_containing_return_addr;
12586
12587       asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
12588                    reg_containing_return_addr);
12589
12590       reg_containing_return_addr = LR_REGNUM;
12591     }
12592
12593   /* If we have registers left on the stack then pop some more.
12594      We know that at most we will want to pop FP and SP.  */
12595   if (pops_needed > 0)
12596     {
12597       int  popped_into;
12598       int  move_to;
12599
12600       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12601                      regs_available_for_popping);
12602
12603       /* We have popped either FP or SP.
12604          Move whichever one it is into the correct register.  */
12605       popped_into = number_of_first_bit_set (regs_available_for_popping);
12606       move_to     = number_of_first_bit_set (regs_to_pop);
12607
12608       asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
12609
12610       regs_to_pop &= ~(1 << move_to);
12611
12612       --pops_needed;
12613     }
12614
12615   /* If we still have not popped everything then we must have only
12616      had one register available to us and we are now popping the SP.  */
12617   if (pops_needed > 0)
12618     {
12619       int  popped_into;
12620
12621       thumb_pushpop (f, regs_available_for_popping, FALSE, NULL,
12622                      regs_available_for_popping);
12623
12624       popped_into = number_of_first_bit_set (regs_available_for_popping);
12625
12626       asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
12627       /*
12628         assert (regs_to_pop == (1 << STACK_POINTER))
12629         assert (pops_needed == 1)
12630       */
12631     }
12632
12633   /* If necessary restore the a4 register.  */
12634   if (restore_a4)
12635     {
12636       if (reg_containing_return_addr != LR_REGNUM)
12637         {
12638           asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
12639           reg_containing_return_addr = LR_REGNUM;
12640         }
12641
12642       asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
12643     }
12644
12645   if (current_function_calls_eh_return)
12646     asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, ARM_EH_STACKADJ_REGNUM);
12647
12648   /* Return to caller.  */
12649   asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
12650 }
12651
12652 /* Emit code to push or pop registers to or from the stack.  F is the
12653    assembly file.  MASK is the registers to push or pop.  PUSH is
12654    nonzero if we should push, and zero if we should pop.  For debugging
12655    output, if pushing, adjust CFA_OFFSET by the amount of space added
12656    to the stack.  REAL_REGS should have the same number of bits set as
12657    MASK, and will be used instead (in the same order) to describe which
12658    registers were saved - this is used to mark the save slots when we
12659    push high registers after moving them to low registers.  */
12660 static void
12661 thumb_pushpop (FILE *f, int mask, int push, int *cfa_offset, int real_regs)
12662 {
12663   int regno;
12664   int lo_mask = mask & 0xFF;
12665   int pushed_words = 0;
12666
12667   if (lo_mask == 0 && !push && (mask & (1 << PC_REGNUM)))
12668     {
12669       /* Special case.  Do not generate a POP PC statement here, do it in
12670          thumb_exit() */
12671       thumb_exit (f, -1);
12672       return;
12673     }
12674
12675   fprintf (f, "\t%s\t{", push ? "push" : "pop");
12676
12677   /* Look at the low registers first.  */
12678   for (regno = 0; regno <= LAST_LO_REGNUM; regno++, lo_mask >>= 1)
12679     {
12680       if (lo_mask & 1)
12681         {
12682           asm_fprintf (f, "%r", regno);
12683
12684           if ((lo_mask & ~1) != 0)
12685             fprintf (f, ", ");
12686
12687           pushed_words++;
12688         }
12689     }
12690
12691   if (push && (mask & (1 << LR_REGNUM)))
12692     {
12693       /* Catch pushing the LR.  */
12694       if (mask & 0xFF)
12695         fprintf (f, ", ");
12696
12697       asm_fprintf (f, "%r", LR_REGNUM);
12698
12699       pushed_words++;
12700     }
12701   else if (!push && (mask & (1 << PC_REGNUM)))
12702     {
12703       /* Catch popping the PC.  */
12704       if (TARGET_INTERWORK || TARGET_BACKTRACE
12705           || current_function_calls_eh_return)
12706         {
12707           /* The PC is never poped directly, instead
12708              it is popped into r3 and then BX is used.  */
12709           fprintf (f, "}\n");
12710
12711           thumb_exit (f, -1);
12712
12713           return;
12714         }
12715       else
12716         {
12717           if (mask & 0xFF)
12718             fprintf (f, ", ");
12719
12720           asm_fprintf (f, "%r", PC_REGNUM);
12721         }
12722     }
12723
12724   fprintf (f, "}\n");
12725
12726   if (push && pushed_words && dwarf2out_do_frame ())
12727     {
12728       char *l = dwarf2out_cfi_label ();
12729       int pushed_mask = real_regs;
12730
12731       *cfa_offset += pushed_words * 4;
12732       dwarf2out_def_cfa (l, SP_REGNUM, *cfa_offset);
12733
12734       pushed_words = 0;
12735       pushed_mask = real_regs;
12736       for (regno = 0; regno <= 14; regno++, pushed_mask >>= 1)
12737         {
12738           if (pushed_mask & 1)
12739             dwarf2out_reg_save (l, regno, 4 * pushed_words++ - *cfa_offset);
12740         }
12741     }
12742 }
12743 \f
12744 void
12745 thumb_final_prescan_insn (rtx insn)
12746 {
12747   if (flag_print_asm_name)
12748     asm_fprintf (asm_out_file, "%@ 0x%04x\n",
12749                  INSN_ADDRESSES (INSN_UID (insn)));
12750 }
12751
12752 int
12753 thumb_shiftable_const (unsigned HOST_WIDE_INT val)
12754 {
12755   unsigned HOST_WIDE_INT mask = 0xff;
12756   int i;
12757
12758   if (val == 0) /* XXX */
12759     return 0;
12760
12761   for (i = 0; i < 25; i++)
12762     if ((val & (mask << i)) == val)
12763       return 1;
12764
12765   return 0;
12766 }
12767
12768 /* Returns nonzero if the current function contains,
12769    or might contain a far jump.  */
12770 static int
12771 thumb_far_jump_used_p (void)
12772 {
12773   rtx insn;
12774
12775   /* This test is only important for leaf functions.  */
12776   /* assert (!leaf_function_p ()); */
12777
12778   /* If we have already decided that far jumps may be used,
12779      do not bother checking again, and always return true even if
12780      it turns out that they are not being used.  Once we have made
12781      the decision that far jumps are present (and that hence the link
12782      register will be pushed onto the stack) we cannot go back on it.  */
12783   if (cfun->machine->far_jump_used)
12784     return 1;
12785
12786   /* If this function is not being called from the prologue/epilogue
12787      generation code then it must be being called from the
12788      INITIAL_ELIMINATION_OFFSET macro.  */
12789   if (!(ARM_DOUBLEWORD_ALIGN || reload_completed))
12790     {
12791       /* In this case we know that we are being asked about the elimination
12792          of the arg pointer register.  If that register is not being used,
12793          then there are no arguments on the stack, and we do not have to
12794          worry that a far jump might force the prologue to push the link
12795          register, changing the stack offsets.  In this case we can just
12796          return false, since the presence of far jumps in the function will
12797          not affect stack offsets.
12798
12799          If the arg pointer is live (or if it was live, but has now been
12800          eliminated and so set to dead) then we do have to test to see if
12801          the function might contain a far jump.  This test can lead to some
12802          false negatives, since before reload is completed, then length of
12803          branch instructions is not known, so gcc defaults to returning their
12804          longest length, which in turn sets the far jump attribute to true.
12805
12806          A false negative will not result in bad code being generated, but it
12807          will result in a needless push and pop of the link register.  We
12808          hope that this does not occur too often.
12809
12810          If we need doubleword stack alignment this could affect the other
12811          elimination offsets so we can't risk getting it wrong.  */
12812       if (regs_ever_live [ARG_POINTER_REGNUM])
12813         cfun->machine->arg_pointer_live = 1;
12814       else if (!cfun->machine->arg_pointer_live)
12815         return 0;
12816     }
12817
12818   /* Check to see if the function contains a branch
12819      insn with the far jump attribute set.  */
12820   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
12821     {
12822       if (GET_CODE (insn) == JUMP_INSN
12823           /* Ignore tablejump patterns.  */
12824           && GET_CODE (PATTERN (insn)) != ADDR_VEC
12825           && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
12826           && get_attr_far_jump (insn) == FAR_JUMP_YES
12827           )
12828         {
12829           /* Record the fact that we have decided that
12830              the function does use far jumps.  */
12831           cfun->machine->far_jump_used = 1;
12832           return 1;
12833         }
12834     }
12835
12836   return 0;
12837 }
12838
12839 /* Return nonzero if FUNC must be entered in ARM mode.  */
12840 int
12841 is_called_in_ARM_mode (tree func)
12842 {
12843   if (TREE_CODE (func) != FUNCTION_DECL)
12844     abort ();
12845
12846   /* Ignore the problem about functions whoes address is taken.  */
12847   if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
12848     return TRUE;
12849
12850 #ifdef ARM_PE
12851   return lookup_attribute ("interfacearm", DECL_ATTRIBUTES (func)) != NULL_TREE;
12852 #else
12853   return FALSE;
12854 #endif
12855 }
12856
12857 /* The bits which aren't usefully expanded as rtl.  */
12858 const char *
12859 thumb_unexpanded_epilogue (void)
12860 {
12861   int regno;
12862   int live_regs_mask = 0;
12863   int high_regs_pushed = 0;
12864   int had_to_push_lr;
12865   int size;
12866   int mode;
12867
12868   if (return_used_this_function)
12869     return "";
12870
12871   if (IS_NAKED (arm_current_func_type ()))
12872     return "";
12873
12874   live_regs_mask = thumb_compute_save_reg_mask ();
12875   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
12876
12877   /* If we can deduce the registers used from the function's return value.
12878      This is more reliable that examining regs_ever_live[] because that
12879      will be set if the register is ever used in the function, not just if
12880      the register is used to hold a return value.  */
12881
12882   if (current_function_return_rtx != 0)
12883     mode = GET_MODE (current_function_return_rtx);
12884   else
12885     mode = DECL_MODE (DECL_RESULT (current_function_decl));
12886
12887   size = GET_MODE_SIZE (mode);
12888
12889   /* The prolog may have pushed some high registers to use as
12890      work registers.  e.g. the testsuite file:
12891      gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
12892      compiles to produce:
12893         push    {r4, r5, r6, r7, lr}
12894         mov     r7, r9
12895         mov     r6, r8
12896         push    {r6, r7}
12897      as part of the prolog.  We have to undo that pushing here.  */
12898
12899   if (high_regs_pushed)
12900     {
12901       int mask = live_regs_mask & 0xff;
12902       int next_hi_reg;
12903
12904       /* The available low registers depend on the size of the value we are
12905          returning.  */
12906       if (size <= 12)
12907         mask |=  1 << 3;
12908       if (size <= 8)
12909         mask |= 1 << 2;
12910
12911       if (mask == 0)
12912         /* Oh dear!  We have no low registers into which we can pop
12913            high registers!  */
12914         internal_error
12915           ("no low registers available for popping high registers");
12916
12917       for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
12918         if (live_regs_mask & (1 << next_hi_reg))
12919           break;
12920
12921       while (high_regs_pushed)
12922         {
12923           /* Find lo register(s) into which the high register(s) can
12924              be popped.  */
12925           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12926             {
12927               if (mask & (1 << regno))
12928                 high_regs_pushed--;
12929               if (high_regs_pushed == 0)
12930                 break;
12931             }
12932
12933           mask &= (2 << regno) - 1;     /* A noop if regno == 8 */
12934
12935           /* Pop the values into the low register(s).  */
12936           thumb_pushpop (asm_out_file, mask, 0, NULL, mask);
12937
12938           /* Move the value(s) into the high registers.  */
12939           for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
12940             {
12941               if (mask & (1 << regno))
12942                 {
12943                   asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
12944                                regno);
12945
12946                   for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
12947                     if (live_regs_mask & (1 << next_hi_reg))
12948                       break;
12949                 }
12950             }
12951         }
12952       live_regs_mask &= ~0x0f00;
12953     }
12954
12955   had_to_push_lr = (live_regs_mask & (1 << LR_REGNUM)) != 0;
12956   live_regs_mask &= 0xff;
12957
12958   if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
12959     {
12960       /* Pop the return address into the PC.  */
12961       if (had_to_push_lr)
12962         live_regs_mask |= 1 << PC_REGNUM;
12963
12964       /* Either no argument registers were pushed or a backtrace
12965          structure was created which includes an adjusted stack
12966          pointer, so just pop everything.  */
12967       if (live_regs_mask)
12968         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12969                        live_regs_mask);
12970
12971       /* We have either just popped the return address into the
12972          PC or it is was kept in LR for the entire function.  */
12973       if (!had_to_push_lr)
12974         thumb_exit (asm_out_file, LR_REGNUM);
12975     }
12976   else
12977     {
12978       /* Pop everything but the return address.  */
12979       if (live_regs_mask)
12980         thumb_pushpop (asm_out_file, live_regs_mask, FALSE, NULL,
12981                        live_regs_mask);
12982
12983       if (had_to_push_lr)
12984         {
12985           if (size > 12)
12986             {
12987               /* We have no free low regs, so save one.  */
12988               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", IP_REGNUM,
12989                            LAST_ARG_REGNUM);
12990             }
12991
12992           /* Get the return address into a temporary register.  */
12993           thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0, NULL,
12994                          1 << LAST_ARG_REGNUM);
12995
12996           if (size > 12)
12997             {
12998               /* Move the return address to lr.  */
12999               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LR_REGNUM,
13000                            LAST_ARG_REGNUM);
13001               /* Restore the low register.  */
13002               asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", LAST_ARG_REGNUM,
13003                            IP_REGNUM);
13004               regno = LR_REGNUM;
13005             }
13006           else
13007             regno = LAST_ARG_REGNUM;
13008         }
13009       else
13010         regno = LR_REGNUM;
13011
13012       /* Remove the argument registers that were pushed onto the stack.  */
13013       asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
13014                    SP_REGNUM, SP_REGNUM,
13015                    current_function_pretend_args_size);
13016
13017       thumb_exit (asm_out_file, regno);
13018     }
13019
13020   return "";
13021 }
13022
13023 /* Functions to save and restore machine-specific function data.  */
13024 static struct machine_function *
13025 arm_init_machine_status (void)
13026 {
13027   struct machine_function *machine;
13028   machine = (machine_function *) ggc_alloc_cleared (sizeof (machine_function));
13029
13030 #if ARM_FT_UNKNOWN != 0
13031   machine->func_type = ARM_FT_UNKNOWN;
13032 #endif
13033   return machine;
13034 }
13035
13036 /* Return an RTX indicating where the return address to the
13037    calling function can be found.  */
13038 rtx
13039 arm_return_addr (int count, rtx frame ATTRIBUTE_UNUSED)
13040 {
13041   if (count != 0)
13042     return NULL_RTX;
13043
13044   return get_hard_reg_initial_val (Pmode, LR_REGNUM);
13045 }
13046
13047 /* Do anything needed before RTL is emitted for each function.  */
13048 void
13049 arm_init_expanders (void)
13050 {
13051   /* Arrange to initialize and mark the machine per-function status.  */
13052   init_machine_status = arm_init_machine_status;
13053
13054   /* This is to stop the combine pass optimizing away the alignment
13055      adjustment of va_arg.  */
13056   /* ??? It is claimed that this should not be necessary.  */
13057   if (cfun)
13058     mark_reg_pointer (arg_pointer_rtx, PARM_BOUNDARY);
13059 }
13060
13061
13062 /* Like arm_compute_initial_elimination offset.  Simpler because
13063    THUMB_HARD_FRAME_POINTER isn't actually the ABI specified frame pointer.  */
13064
13065 HOST_WIDE_INT
13066 thumb_compute_initial_elimination_offset (unsigned int from, unsigned int to)
13067 {
13068   arm_stack_offsets *offsets;
13069
13070   offsets = arm_get_frame_offsets ();
13071
13072   switch (from)
13073     {
13074     case ARG_POINTER_REGNUM:
13075       switch (to)
13076         {
13077         case STACK_POINTER_REGNUM:
13078           return offsets->outgoing_args - offsets->saved_args;
13079
13080         case FRAME_POINTER_REGNUM:
13081           return offsets->soft_frame - offsets->saved_args;
13082
13083         case THUMB_HARD_FRAME_POINTER_REGNUM:
13084         case ARM_HARD_FRAME_POINTER_REGNUM:
13085           return offsets->saved_regs - offsets->saved_args;
13086
13087         default:
13088           abort();
13089         }
13090       break;
13091
13092     case FRAME_POINTER_REGNUM:
13093       switch (to)
13094         {
13095         case STACK_POINTER_REGNUM:
13096           return offsets->outgoing_args - offsets->soft_frame;
13097
13098         case THUMB_HARD_FRAME_POINTER_REGNUM:
13099         case ARM_HARD_FRAME_POINTER_REGNUM:
13100           return offsets->saved_regs - offsets->soft_frame;
13101
13102         default:
13103           abort();
13104         }
13105       break;
13106
13107     default:
13108       abort ();
13109     }
13110 }
13111
13112
13113 /* Generate the rest of a function's prologue.  */
13114 void
13115 thumb_expand_prologue (void)
13116 {
13117   rtx insn, dwarf;
13118
13119   HOST_WIDE_INT amount;
13120   arm_stack_offsets *offsets;
13121   unsigned long func_type;
13122   int regno;
13123   unsigned long live_regs_mask;
13124
13125   func_type = arm_current_func_type ();
13126
13127   /* Naked functions don't have prologues.  */
13128   if (IS_NAKED (func_type))
13129     return;
13130
13131   if (IS_INTERRUPT (func_type))
13132     {
13133       error ("interrupt Service Routines cannot be coded in Thumb mode");
13134       return;
13135     }
13136
13137   live_regs_mask = thumb_compute_save_reg_mask ();
13138   /* Load the pic register before setting the frame pointer, so we can use r7
13139      as a temporary work register.  */
13140   if (flag_pic)
13141     arm_load_pic_register (thumb_find_work_register (live_regs_mask));
13142
13143   offsets = arm_get_frame_offsets ();
13144
13145   if (frame_pointer_needed)
13146     {
13147       insn = emit_insn (gen_movsi (hard_frame_pointer_rtx,
13148                                    stack_pointer_rtx));
13149       RTX_FRAME_RELATED_P (insn) = 1;
13150     }
13151   else if (CALLER_INTERWORKING_SLOT_SIZE > 0)
13152     emit_move_insn (gen_rtx_REG (Pmode, ARM_HARD_FRAME_POINTER_REGNUM),
13153                     stack_pointer_rtx);
13154
13155   amount = offsets->outgoing_args - offsets->saved_regs;
13156   if (amount)
13157     {
13158       if (amount < 512)
13159         {
13160           insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13161                                         GEN_INT (- amount)));
13162           RTX_FRAME_RELATED_P (insn) = 1;
13163         }
13164       else
13165         {
13166           rtx reg;
13167
13168           /* The stack decrement is too big for an immediate value in a single
13169              insn.  In theory we could issue multiple subtracts, but after
13170              three of them it becomes more space efficient to place the full
13171              value in the constant pool and load into a register.  (Also the
13172              ARM debugger really likes to see only one stack decrement per
13173              function).  So instead we look for a scratch register into which
13174              we can load the decrement, and then we subtract this from the
13175              stack pointer.  Unfortunately on the thumb the only available
13176              scratch registers are the argument registers, and we cannot use
13177              these as they may hold arguments to the function.  Instead we
13178              attempt to locate a call preserved register which is used by this
13179              function.  If we can find one, then we know that it will have
13180              been pushed at the start of the prologue and so we can corrupt
13181              it now.  */
13182           for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
13183             if (live_regs_mask & (1 << regno)
13184                 && !(frame_pointer_needed
13185                      && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
13186               break;
13187
13188           if (regno > LAST_LO_REGNUM) /* Very unlikely.  */
13189             {
13190               rtx spare = gen_rtx_REG (SImode, IP_REGNUM);
13191
13192               /* Choose an arbitrary, non-argument low register.  */
13193               reg = gen_rtx_REG (SImode, LAST_LO_REGNUM);
13194
13195               /* Save it by copying it into a high, scratch register.  */
13196               emit_insn (gen_movsi (spare, reg));
13197               /* Add a USE to stop propagate_one_insn() from barfing.  */
13198               emit_insn (gen_prologue_use (spare));
13199
13200               /* Decrement the stack.  */
13201               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13202               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13203                                             stack_pointer_rtx, reg));
13204               RTX_FRAME_RELATED_P (insn) = 1;
13205               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13206                                    plus_constant (stack_pointer_rtx,
13207                                                   -amount));
13208               RTX_FRAME_RELATED_P (dwarf) = 1;
13209               REG_NOTES (insn)
13210                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13211                                      REG_NOTES (insn));
13212
13213               /* Restore the low register's original value.  */
13214               emit_insn (gen_movsi (reg, spare));
13215
13216               /* Emit a USE of the restored scratch register, so that flow
13217                  analysis will not consider the restore redundant.  The
13218                  register won't be used again in this function and isn't
13219                  restored by the epilogue.  */
13220               emit_insn (gen_prologue_use (reg));
13221             }
13222           else
13223             {
13224               reg = gen_rtx_REG (SImode, regno);
13225
13226               emit_insn (gen_movsi (reg, GEN_INT (- amount)));
13227
13228               insn = emit_insn (gen_addsi3 (stack_pointer_rtx,
13229                                             stack_pointer_rtx, reg));
13230               RTX_FRAME_RELATED_P (insn) = 1;
13231               dwarf = gen_rtx_SET (SImode, stack_pointer_rtx,
13232                                    plus_constant (stack_pointer_rtx,
13233                                                   -amount));
13234               RTX_FRAME_RELATED_P (dwarf) = 1;
13235               REG_NOTES (insn)
13236                 = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
13237                                      REG_NOTES (insn));
13238             }
13239         }
13240       /* If the frame pointer is needed, emit a special barrier that
13241          will prevent the scheduler from moving stores to the frame
13242          before the stack adjustment.  */
13243       if (frame_pointer_needed)
13244         emit_insn (gen_stack_tie (stack_pointer_rtx,
13245                                   hard_frame_pointer_rtx));
13246     }
13247
13248   if (current_function_profile || TARGET_NO_SCHED_PRO)
13249     emit_insn (gen_blockage ());
13250
13251   cfun->machine->lr_save_eliminated = !thumb_force_lr_save ();
13252   if (live_regs_mask & 0xff)
13253     cfun->machine->lr_save_eliminated = 0;
13254
13255   /* If the link register is being kept alive, with the return address in it,
13256      then make sure that it does not get reused by the ce2 pass.  */
13257   if (cfun->machine->lr_save_eliminated)
13258     emit_insn (gen_prologue_use (gen_rtx_REG (SImode, LR_REGNUM)));
13259 }
13260
13261
13262 void
13263 thumb_expand_epilogue (void)
13264 {
13265   HOST_WIDE_INT amount;
13266   arm_stack_offsets *offsets;
13267   int regno;
13268
13269   /* Naked functions don't have prologues.  */
13270   if (IS_NAKED (arm_current_func_type ()))
13271     return;
13272
13273   offsets = arm_get_frame_offsets ();
13274   amount = offsets->outgoing_args - offsets->saved_regs;
13275
13276   if (frame_pointer_needed)
13277     emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
13278   else if (amount)
13279     {
13280       if (amount < 512)
13281         emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
13282                                GEN_INT (amount)));
13283       else
13284         {
13285           /* r3 is always free in the epilogue.  */
13286           rtx reg = gen_rtx_REG (SImode, LAST_ARG_REGNUM);
13287
13288           emit_insn (gen_movsi (reg, GEN_INT (amount)));
13289           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
13290         }
13291     }
13292
13293   /* Emit a USE (stack_pointer_rtx), so that
13294      the stack adjustment will not be deleted.  */
13295   emit_insn (gen_prologue_use (stack_pointer_rtx));
13296
13297   if (current_function_profile || TARGET_NO_SCHED_PRO)
13298     emit_insn (gen_blockage ());
13299
13300   /* Emit a clobber for each insn that will be restored in the epilogue,
13301      so that flow2 will get register lifetimes correct.  */
13302   for (regno = 0; regno < 13; regno++)
13303     if (regs_ever_live[regno] && !call_used_regs[regno])
13304       emit_insn (gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (SImode, regno)));
13305
13306   if (! regs_ever_live[LR_REGNUM])
13307     emit_insn (gen_rtx_USE (VOIDmode, gen_rtx_REG (SImode, LR_REGNUM)));
13308 }
13309
13310 static void
13311 thumb_output_function_prologue (FILE *f, HOST_WIDE_INT size ATTRIBUTE_UNUSED)
13312 {
13313   int live_regs_mask = 0;
13314   int l_mask;
13315   int high_regs_pushed = 0;
13316   int cfa_offset = 0;
13317   int regno;
13318
13319   if (IS_NAKED (arm_current_func_type ()))
13320     return;
13321
13322   if (is_called_in_ARM_mode (current_function_decl))
13323     {
13324       const char * name;
13325
13326       if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
13327         abort ();
13328       if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
13329         abort ();
13330       name = XSTR  (XEXP (DECL_RTL (current_function_decl), 0), 0);
13331
13332       /* Generate code sequence to switch us into Thumb mode.  */
13333       /* The .code 32 directive has already been emitted by
13334          ASM_DECLARE_FUNCTION_NAME.  */
13335       asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
13336       asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
13337
13338       /* Generate a label, so that the debugger will notice the
13339          change in instruction sets.  This label is also used by
13340          the assembler to bypass the ARM code when this function
13341          is called from a Thumb encoded function elsewhere in the
13342          same file.  Hence the definition of STUB_NAME here must
13343          agree with the definition in gas/config/tc-arm.c.  */
13344
13345 #define STUB_NAME ".real_start_of"
13346
13347       fprintf (f, "\t.code\t16\n");
13348 #ifdef ARM_PE
13349       if (arm_dllexport_name_p (name))
13350         name = arm_strip_name_encoding (name);
13351 #endif
13352       asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
13353       fprintf (f, "\t.thumb_func\n");
13354       asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
13355     }
13356
13357   if (current_function_pretend_args_size)
13358     {
13359       if (cfun->machine->uses_anonymous_args)
13360         {
13361           int num_pushes;
13362
13363           fprintf (f, "\tpush\t{");
13364
13365           num_pushes = ARM_NUM_INTS (current_function_pretend_args_size);
13366
13367           for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
13368                regno <= LAST_ARG_REGNUM;
13369                regno++)
13370             asm_fprintf (f, "%r%s", regno,
13371                          regno == LAST_ARG_REGNUM ? "" : ", ");
13372
13373           fprintf (f, "}\n");
13374         }
13375       else
13376         asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
13377                      SP_REGNUM, SP_REGNUM,
13378                      current_function_pretend_args_size);
13379
13380       /* We don't need to record the stores for unwinding (would it
13381          help the debugger any if we did?), but record the change in
13382          the stack pointer.  */
13383       if (dwarf2out_do_frame ())
13384         {
13385           char *l = dwarf2out_cfi_label ();
13386           cfa_offset = cfa_offset + current_function_pretend_args_size;
13387           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13388         }
13389     }
13390
13391   live_regs_mask = thumb_compute_save_reg_mask ();
13392   /* Just low regs and lr. */
13393   l_mask = live_regs_mask & 0x40ff;
13394
13395   if (TARGET_BACKTRACE)
13396     {
13397       int    offset;
13398       int    work_register;
13399
13400       /* We have been asked to create a stack backtrace structure.
13401          The code looks like this:
13402
13403          0   .align 2
13404          0   func:
13405          0     sub   SP, #16         Reserve space for 4 registers.
13406          2     push  {R7}            Push low registers.
13407          4     add   R7, SP, #20     Get the stack pointer before the push.
13408          6     str   R7, [SP, #8]    Store the stack pointer (before reserving the space).
13409          8     mov   R7, PC          Get hold of the start of this code plus 12.
13410         10     str   R7, [SP, #16]   Store it.
13411         12     mov   R7, FP          Get hold of the current frame pointer.
13412         14     str   R7, [SP, #4]    Store it.
13413         16     mov   R7, LR          Get hold of the current return address.
13414         18     str   R7, [SP, #12]   Store it.
13415         20     add   R7, SP, #16     Point at the start of the backtrace structure.
13416         22     mov   FP, R7          Put this value into the frame pointer.  */
13417
13418       work_register = thumb_find_work_register (live_regs_mask);
13419
13420       asm_fprintf
13421         (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
13422          SP_REGNUM, SP_REGNUM);
13423
13424       if (dwarf2out_do_frame ())
13425         {
13426           char *l = dwarf2out_cfi_label ();
13427           cfa_offset = cfa_offset + 16;
13428           dwarf2out_def_cfa (l, SP_REGNUM, cfa_offset);
13429         }
13430
13431       if (l_mask)
13432         {
13433           thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13434           offset = bit_count (l_mask);
13435         }
13436       else
13437         offset = 0;
13438
13439       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13440                    offset + 16 + current_function_pretend_args_size);
13441
13442       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13443                    offset + 4);
13444
13445       /* Make sure that the instruction fetching the PC is in the right place
13446          to calculate "start of backtrace creation code + 12".  */
13447       if (l_mask)
13448         {
13449           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13450           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13451                        offset + 12);
13452           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13453                        ARM_HARD_FRAME_POINTER_REGNUM);
13454           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13455                        offset);
13456         }
13457       else
13458         {
13459           asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
13460                        ARM_HARD_FRAME_POINTER_REGNUM);
13461           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13462                        offset);
13463           asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
13464           asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13465                        offset + 12);
13466         }
13467
13468       asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
13469       asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
13470                    offset + 8);
13471       asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
13472                    offset + 12);
13473       asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
13474                    ARM_HARD_FRAME_POINTER_REGNUM, work_register);
13475     }
13476   else if (l_mask)
13477     thumb_pushpop (f, l_mask, 1, &cfa_offset, l_mask);
13478
13479   high_regs_pushed = bit_count (live_regs_mask & 0x0f00);
13480
13481   if (high_regs_pushed)
13482     {
13483       int pushable_regs = 0;
13484       int next_hi_reg;
13485
13486       for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
13487         if (live_regs_mask & (1 << next_hi_reg))
13488           break;
13489
13490       pushable_regs = l_mask & 0xff;
13491
13492       if (pushable_regs == 0)
13493         pushable_regs = 1 << thumb_find_work_register (live_regs_mask);
13494
13495       while (high_regs_pushed > 0)
13496         {
13497           int real_regs_mask = 0;
13498
13499           for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
13500             {
13501               if (pushable_regs & (1 << regno))
13502                 {
13503                   asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
13504
13505                   high_regs_pushed--;
13506                   real_regs_mask |= (1 << next_hi_reg);
13507
13508                   if (high_regs_pushed)
13509                     {
13510                       for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
13511                            next_hi_reg--)
13512                         if (live_regs_mask & (1 << next_hi_reg))
13513                           break;
13514                     }
13515                   else
13516                     {
13517                       pushable_regs &= ~((1 << regno) - 1);
13518                       break;
13519                     }
13520                 }
13521             }
13522
13523           thumb_pushpop (f, pushable_regs, 1, &cfa_offset, real_regs_mask);
13524         }
13525     }
13526 }
13527
13528 /* Handle the case of a double word load into a low register from
13529    a computed memory address.  The computed address may involve a
13530    register which is overwritten by the load.  */
13531 const char *
13532 thumb_load_double_from_address (rtx *operands)
13533 {
13534   rtx addr;
13535   rtx base;
13536   rtx offset;
13537   rtx arg1;
13538   rtx arg2;
13539
13540   if (GET_CODE (operands[0]) != REG)
13541     abort ();
13542
13543   if (GET_CODE (operands[1]) != MEM)
13544     abort ();
13545
13546   /* Get the memory address.  */
13547   addr = XEXP (operands[1], 0);
13548
13549   /* Work out how the memory address is computed.  */
13550   switch (GET_CODE (addr))
13551     {
13552     case REG:
13553       operands[2] = gen_rtx_MEM (SImode,
13554                                  plus_constant (XEXP (operands[1], 0), 4));
13555
13556       if (REGNO (operands[0]) == REGNO (addr))
13557         {
13558           output_asm_insn ("ldr\t%H0, %2", operands);
13559           output_asm_insn ("ldr\t%0, %1", operands);
13560         }
13561       else
13562         {
13563           output_asm_insn ("ldr\t%0, %1", operands);
13564           output_asm_insn ("ldr\t%H0, %2", operands);
13565         }
13566       break;
13567
13568     case CONST:
13569       /* Compute <address> + 4 for the high order load.  */
13570       operands[2] = gen_rtx_MEM (SImode,
13571                                  plus_constant (XEXP (operands[1], 0), 4));
13572
13573       output_asm_insn ("ldr\t%0, %1", operands);
13574       output_asm_insn ("ldr\t%H0, %2", operands);
13575       break;
13576
13577     case PLUS:
13578       arg1   = XEXP (addr, 0);
13579       arg2   = XEXP (addr, 1);
13580
13581       if (CONSTANT_P (arg1))
13582         base = arg2, offset = arg1;
13583       else
13584         base = arg1, offset = arg2;
13585
13586       if (GET_CODE (base) != REG)
13587         abort ();
13588
13589       /* Catch the case of <address> = <reg> + <reg> */
13590       if (GET_CODE (offset) == REG)
13591         {
13592           int reg_offset = REGNO (offset);
13593           int reg_base   = REGNO (base);
13594           int reg_dest   = REGNO (operands[0]);
13595
13596           /* Add the base and offset registers together into the
13597              higher destination register.  */
13598           asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
13599                        reg_dest + 1, reg_base, reg_offset);
13600
13601           /* Load the lower destination register from the address in
13602              the higher destination register.  */
13603           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
13604                        reg_dest, reg_dest + 1);
13605
13606           /* Load the higher destination register from its own address
13607              plus 4.  */
13608           asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
13609                        reg_dest + 1, reg_dest + 1);
13610         }
13611       else
13612         {
13613           /* Compute <address> + 4 for the high order load.  */
13614           operands[2] = gen_rtx_MEM (SImode,
13615                                      plus_constant (XEXP (operands[1], 0), 4));
13616
13617           /* If the computed address is held in the low order register
13618              then load the high order register first, otherwise always
13619              load the low order register first.  */
13620           if (REGNO (operands[0]) == REGNO (base))
13621             {
13622               output_asm_insn ("ldr\t%H0, %2", operands);
13623               output_asm_insn ("ldr\t%0, %1", operands);
13624             }
13625           else
13626             {
13627               output_asm_insn ("ldr\t%0, %1", operands);
13628               output_asm_insn ("ldr\t%H0, %2", operands);
13629             }
13630         }
13631       break;
13632
13633     case LABEL_REF:
13634       /* With no registers to worry about we can just load the value
13635          directly.  */
13636       operands[2] = gen_rtx_MEM (SImode,
13637                                  plus_constant (XEXP (operands[1], 0), 4));
13638
13639       output_asm_insn ("ldr\t%H0, %2", operands);
13640       output_asm_insn ("ldr\t%0, %1", operands);
13641       break;
13642
13643     default:
13644       abort ();
13645       break;
13646     }
13647
13648   return "";
13649 }
13650
13651 const char *
13652 thumb_output_move_mem_multiple (int n, rtx *operands)
13653 {
13654   rtx tmp;
13655
13656   switch (n)
13657     {
13658     case 2:
13659       if (REGNO (operands[4]) > REGNO (operands[5]))
13660         {
13661           tmp = operands[4];
13662           operands[4] = operands[5];
13663           operands[5] = tmp;
13664         }
13665       output_asm_insn ("ldmia\t%1!, {%4, %5}", operands);
13666       output_asm_insn ("stmia\t%0!, {%4, %5}", operands);
13667       break;
13668
13669     case 3:
13670       if (REGNO (operands[4]) > REGNO (operands[5]))
13671         {
13672           tmp = operands[4];
13673           operands[4] = operands[5];
13674           operands[5] = tmp;
13675         }
13676       if (REGNO (operands[5]) > REGNO (operands[6]))
13677         {
13678           tmp = operands[5];
13679           operands[5] = operands[6];
13680           operands[6] = tmp;
13681         }
13682       if (REGNO (operands[4]) > REGNO (operands[5]))
13683         {
13684           tmp = operands[4];
13685           operands[4] = operands[5];
13686           operands[5] = tmp;
13687         }
13688
13689       output_asm_insn ("ldmia\t%1!, {%4, %5, %6}", operands);
13690       output_asm_insn ("stmia\t%0!, {%4, %5, %6}", operands);
13691       break;
13692
13693     default:
13694       abort ();
13695     }
13696
13697   return "";
13698 }
13699
13700 /* Output a call-via instruction for thumb state.  */
13701 const char *
13702 thumb_call_via_reg (rtx reg)
13703 {
13704   int regno = REGNO (reg);
13705   rtx *labelp;
13706
13707   gcc_assert (regno < SP_REGNUM);
13708
13709   /* If we are in the normal text section we can use a single instance
13710      per compilation unit.  If we are doing function sections, then we need
13711      an entry per section, since we can't rely on reachability.  */
13712   if (in_text_section ())
13713     {
13714       thumb_call_reg_needed = 1;
13715
13716       if (thumb_call_via_label[regno] == NULL)
13717         thumb_call_via_label[regno] = gen_label_rtx ();
13718       labelp = thumb_call_via_label + regno;
13719     }
13720   else
13721     {
13722       if (cfun->machine->call_via[regno] == NULL)
13723         cfun->machine->call_via[regno] = gen_label_rtx ();
13724       labelp = cfun->machine->call_via + regno;
13725     }
13726
13727   output_asm_insn ("bl\t%a0", labelp);
13728   return "";
13729 }
13730
13731 /* Routines for generating rtl.  */
13732 void
13733 thumb_expand_movmemqi (rtx *operands)
13734 {
13735   rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
13736   rtx in  = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
13737   HOST_WIDE_INT len = INTVAL (operands[2]);
13738   HOST_WIDE_INT offset = 0;
13739
13740   while (len >= 12)
13741     {
13742       emit_insn (gen_movmem12b (out, in, out, in));
13743       len -= 12;
13744     }
13745
13746   if (len >= 8)
13747     {
13748       emit_insn (gen_movmem8b (out, in, out, in));
13749       len -= 8;
13750     }
13751
13752   if (len >= 4)
13753     {
13754       rtx reg = gen_reg_rtx (SImode);
13755       emit_insn (gen_movsi (reg, gen_rtx_MEM (SImode, in)));
13756       emit_insn (gen_movsi (gen_rtx_MEM (SImode, out), reg));
13757       len -= 4;
13758       offset += 4;
13759     }
13760
13761   if (len >= 2)
13762     {
13763       rtx reg = gen_reg_rtx (HImode);
13764       emit_insn (gen_movhi (reg, gen_rtx_MEM (HImode,
13765                                               plus_constant (in, offset))));
13766       emit_insn (gen_movhi (gen_rtx_MEM (HImode, plus_constant (out, offset)),
13767                             reg));
13768       len -= 2;
13769       offset += 2;
13770     }
13771
13772   if (len)
13773     {
13774       rtx reg = gen_reg_rtx (QImode);
13775       emit_insn (gen_movqi (reg, gen_rtx_MEM (QImode,
13776                                               plus_constant (in, offset))));
13777       emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (out, offset)),
13778                             reg));
13779     }
13780 }
13781
13782 void
13783 thumb_reload_out_hi (rtx *operands)
13784 {
13785   emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
13786 }
13787
13788 /* Handle reading a half-word from memory during reload.  */
13789 void
13790 thumb_reload_in_hi (rtx *operands ATTRIBUTE_UNUSED)
13791 {
13792   abort ();
13793 }
13794
13795 /* Return the length of a function name prefix
13796     that starts with the character 'c'.  */
13797 static int
13798 arm_get_strip_length (int c)
13799 {
13800   switch (c)
13801     {
13802     ARM_NAME_ENCODING_LENGTHS
13803       default: return 0;
13804     }
13805 }
13806
13807 /* Return a pointer to a function's name with any
13808    and all prefix encodings stripped from it.  */
13809 const char *
13810 arm_strip_name_encoding (const char *name)
13811 {
13812   int skip;
13813
13814   while ((skip = arm_get_strip_length (* name)))
13815     name += skip;
13816
13817   return name;
13818 }
13819
13820 /* If there is a '*' anywhere in the name's prefix, then
13821    emit the stripped name verbatim, otherwise prepend an
13822    underscore if leading underscores are being used.  */
13823 void
13824 arm_asm_output_labelref (FILE *stream, const char *name)
13825 {
13826   int skip;
13827   int verbatim = 0;
13828
13829   while ((skip = arm_get_strip_length (* name)))
13830     {
13831       verbatim |= (*name == '*');
13832       name += skip;
13833     }
13834
13835   if (verbatim)
13836     fputs (name, stream);
13837   else
13838     asm_fprintf (stream, "%U%s", name);
13839 }
13840
13841 static void
13842 arm_file_end (void)
13843 {
13844   int regno;
13845
13846   if (! thumb_call_reg_needed)
13847     return;
13848
13849   text_section ();
13850   asm_fprintf (asm_out_file, "\t.code 16\n");
13851   ASM_OUTPUT_ALIGN (asm_out_file, 1);
13852
13853   for (regno = 0; regno < SP_REGNUM; regno++)
13854     {
13855       rtx label = thumb_call_via_label[regno];
13856
13857       if (label != 0)
13858         {
13859           targetm.asm_out.internal_label (asm_out_file, "L",
13860                                           CODE_LABEL_NUMBER (label));
13861           asm_fprintf (asm_out_file, "\tbx\t%r\n", regno);
13862         }
13863     }
13864 }
13865
13866 rtx aof_pic_label;
13867
13868 #ifdef AOF_ASSEMBLER
13869 /* Special functions only needed when producing AOF syntax assembler.  */
13870
13871 struct pic_chain
13872 {
13873   struct pic_chain * next;
13874   const char * symname;
13875 };
13876
13877 static struct pic_chain * aof_pic_chain = NULL;
13878
13879 rtx
13880 aof_pic_entry (rtx x)
13881 {
13882   struct pic_chain ** chainp;
13883   int offset;
13884
13885   if (aof_pic_label == NULL_RTX)
13886     {
13887       aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
13888     }
13889
13890   for (offset = 0, chainp = &aof_pic_chain; *chainp;
13891        offset += 4, chainp = &(*chainp)->next)
13892     if ((*chainp)->symname == XSTR (x, 0))
13893       return plus_constant (aof_pic_label, offset);
13894
13895   *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
13896   (*chainp)->next = NULL;
13897   (*chainp)->symname = XSTR (x, 0);
13898   return plus_constant (aof_pic_label, offset);
13899 }
13900
13901 void
13902 aof_dump_pic_table (FILE *f)
13903 {
13904   struct pic_chain * chain;
13905
13906   if (aof_pic_chain == NULL)
13907     return;
13908
13909   asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
13910                PIC_OFFSET_TABLE_REGNUM,
13911                PIC_OFFSET_TABLE_REGNUM);
13912   fputs ("|x$adcons|\n", f);
13913
13914   for (chain = aof_pic_chain; chain; chain = chain->next)
13915     {
13916       fputs ("\tDCD\t", f);
13917       assemble_name (f, chain->symname);
13918       fputs ("\n", f);
13919     }
13920 }
13921
13922 int arm_text_section_count = 1;
13923
13924 char *
13925 aof_text_section (void )
13926 {
13927   static char buf[100];
13928   sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
13929            arm_text_section_count++);
13930   if (flag_pic)
13931     strcat (buf, ", PIC, REENTRANT");
13932   return buf;
13933 }
13934
13935 static int arm_data_section_count = 1;
13936
13937 char *
13938 aof_data_section (void)
13939 {
13940   static char buf[100];
13941   sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
13942   return buf;
13943 }
13944
13945 /* The AOF assembler is religiously strict about declarations of
13946    imported and exported symbols, so that it is impossible to declare
13947    a function as imported near the beginning of the file, and then to
13948    export it later on.  It is, however, possible to delay the decision
13949    until all the functions in the file have been compiled.  To get
13950    around this, we maintain a list of the imports and exports, and
13951    delete from it any that are subsequently defined.  At the end of
13952    compilation we spit the remainder of the list out before the END
13953    directive.  */
13954
13955 struct import
13956 {
13957   struct import * next;
13958   const char * name;
13959 };
13960
13961 static struct import * imports_list = NULL;
13962
13963 void
13964 aof_add_import (const char *name)
13965 {
13966   struct import * new;
13967
13968   for (new = imports_list; new; new = new->next)
13969     if (new->name == name)
13970       return;
13971
13972   new = (struct import *) xmalloc (sizeof (struct import));
13973   new->next = imports_list;
13974   imports_list = new;
13975   new->name = name;
13976 }
13977
13978 void
13979 aof_delete_import (const char *name)
13980 {
13981   struct import ** old;
13982
13983   for (old = &imports_list; *old; old = & (*old)->next)
13984     {
13985       if ((*old)->name == name)
13986         {
13987           *old = (*old)->next;
13988           return;
13989         }
13990     }
13991 }
13992
13993 int arm_main_function = 0;
13994
13995 static void
13996 aof_dump_imports (FILE *f)
13997 {
13998   /* The AOF assembler needs this to cause the startup code to be extracted
13999      from the library.  Brining in __main causes the whole thing to work
14000      automagically.  */
14001   if (arm_main_function)
14002     {
14003       text_section ();
14004       fputs ("\tIMPORT __main\n", f);
14005       fputs ("\tDCD __main\n", f);
14006     }
14007
14008   /* Now dump the remaining imports.  */
14009   while (imports_list)
14010     {
14011       fprintf (f, "\tIMPORT\t");
14012       assemble_name (f, imports_list->name);
14013       fputc ('\n', f);
14014       imports_list = imports_list->next;
14015     }
14016 }
14017
14018 static void
14019 aof_globalize_label (FILE *stream, const char *name)
14020 {
14021   default_globalize_label (stream, name);
14022   if (! strcmp (name, "main"))
14023     arm_main_function = 1;
14024 }
14025
14026 static void
14027 aof_file_start (void)
14028 {
14029   fputs ("__r0\tRN\t0\n", asm_out_file);
14030   fputs ("__a1\tRN\t0\n", asm_out_file);
14031   fputs ("__a2\tRN\t1\n", asm_out_file);
14032   fputs ("__a3\tRN\t2\n", asm_out_file);
14033   fputs ("__a4\tRN\t3\n", asm_out_file);
14034   fputs ("__v1\tRN\t4\n", asm_out_file);
14035   fputs ("__v2\tRN\t5\n", asm_out_file);
14036   fputs ("__v3\tRN\t6\n", asm_out_file);
14037   fputs ("__v4\tRN\t7\n", asm_out_file);
14038   fputs ("__v5\tRN\t8\n", asm_out_file);
14039   fputs ("__v6\tRN\t9\n", asm_out_file);
14040   fputs ("__sl\tRN\t10\n", asm_out_file);
14041   fputs ("__fp\tRN\t11\n", asm_out_file);
14042   fputs ("__ip\tRN\t12\n", asm_out_file);
14043   fputs ("__sp\tRN\t13\n", asm_out_file);
14044   fputs ("__lr\tRN\t14\n", asm_out_file);
14045   fputs ("__pc\tRN\t15\n", asm_out_file);
14046   fputs ("__f0\tFN\t0\n", asm_out_file);
14047   fputs ("__f1\tFN\t1\n", asm_out_file);
14048   fputs ("__f2\tFN\t2\n", asm_out_file);
14049   fputs ("__f3\tFN\t3\n", asm_out_file);
14050   fputs ("__f4\tFN\t4\n", asm_out_file);
14051   fputs ("__f5\tFN\t5\n", asm_out_file);
14052   fputs ("__f6\tFN\t6\n", asm_out_file);
14053   fputs ("__f7\tFN\t7\n", asm_out_file);
14054   text_section ();
14055 }
14056
14057 static void
14058 aof_file_end (void)
14059 {
14060   if (flag_pic)
14061     aof_dump_pic_table (asm_out_file);
14062   arm_file_end ();
14063   aof_dump_imports (asm_out_file);
14064   fputs ("\tEND\n", asm_out_file);
14065 }
14066 #endif /* AOF_ASSEMBLER */
14067
14068 #ifndef ARM_PE
14069 /* Symbols in the text segment can be accessed without indirecting via the
14070    constant pool; it may take an extra binary operation, but this is still
14071    faster than indirecting via memory.  Don't do this when not optimizing,
14072    since we won't be calculating al of the offsets necessary to do this
14073    simplification.  */
14074
14075 static void
14076 arm_encode_section_info (tree decl, rtx rtl, int first)
14077 {
14078   /* This doesn't work with AOF syntax, since the string table may be in
14079      a different AREA.  */
14080 #ifndef AOF_ASSEMBLER
14081   if (optimize > 0 && TREE_CONSTANT (decl))
14082     SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
14083 #endif
14084
14085   /* If we are referencing a function that is weak then encode a long call
14086      flag in the function name, otherwise if the function is static or
14087      or known to be defined in this file then encode a short call flag.  */
14088   if (first && DECL_P (decl))
14089     {
14090       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_WEAK (decl))
14091         arm_encode_call_attribute (decl, LONG_CALL_FLAG_CHAR);
14092       else if (! TREE_PUBLIC (decl))
14093         arm_encode_call_attribute (decl, SHORT_CALL_FLAG_CHAR);
14094     }
14095 }
14096 #endif /* !ARM_PE */
14097
14098 static void
14099 arm_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
14100 {
14101   if (arm_ccfsm_state == 3 && (unsigned) arm_target_label == labelno
14102       && !strcmp (prefix, "L"))
14103     {
14104       arm_ccfsm_state = 0;
14105       arm_target_insn = NULL;
14106     }
14107   default_internal_label (stream, prefix, labelno);
14108 }
14109
14110 /* Output code to add DELTA to the first argument, and then jump
14111    to FUNCTION.  Used for C++ multiple inheritance.  */
14112 static void
14113 arm_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
14114                      HOST_WIDE_INT delta,
14115                      HOST_WIDE_INT vcall_offset ATTRIBUTE_UNUSED,
14116                      tree function)
14117 {
14118   static int thunk_label = 0;
14119   char label[256];
14120   int mi_delta = delta;
14121   const char *const mi_op = mi_delta < 0 ? "sub" : "add";
14122   int shift = 0;
14123   int this_regno = (aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function)
14124                     ? 1 : 0);
14125   if (mi_delta < 0)
14126     mi_delta = - mi_delta;
14127   if (TARGET_THUMB)
14128     {
14129       int labelno = thunk_label++;
14130       ASM_GENERATE_INTERNAL_LABEL (label, "LTHUMBFUNC", labelno);
14131       fputs ("\tldr\tr12, ", file);
14132       assemble_name (file, label);
14133       fputc ('\n', file);
14134     }
14135   while (mi_delta != 0)
14136     {
14137       if ((mi_delta & (3 << shift)) == 0)
14138         shift += 2;
14139       else
14140         {
14141           asm_fprintf (file, "\t%s\t%r, %r, #%d\n",
14142                        mi_op, this_regno, this_regno,
14143                        mi_delta & (0xff << shift));
14144           mi_delta &= ~(0xff << shift);
14145           shift += 8;
14146         }
14147     }
14148   if (TARGET_THUMB)
14149     {
14150       fprintf (file, "\tbx\tr12\n");
14151       ASM_OUTPUT_ALIGN (file, 2);
14152       assemble_name (file, label);
14153       fputs (":\n", file);
14154       assemble_integer (XEXP (DECL_RTL (function), 0), 4, BITS_PER_WORD, 1);
14155     }
14156   else
14157     {
14158       fputs ("\tb\t", file);
14159       assemble_name (file, XSTR (XEXP (DECL_RTL (function), 0), 0));
14160       if (NEED_PLT_RELOC)
14161         fputs ("(PLT)", file);
14162       fputc ('\n', file);
14163     }
14164 }
14165
14166 int
14167 arm_emit_vector_const (FILE *file, rtx x)
14168 {
14169   int i;
14170   const char * pattern;
14171
14172   if (GET_CODE (x) != CONST_VECTOR)
14173     abort ();
14174
14175   switch (GET_MODE (x))
14176     {
14177     case V2SImode: pattern = "%08x"; break;
14178     case V4HImode: pattern = "%04x"; break;
14179     case V8QImode: pattern = "%02x"; break;
14180     default:       abort ();
14181     }
14182
14183   fprintf (file, "0x");
14184   for (i = CONST_VECTOR_NUNITS (x); i--;)
14185     {
14186       rtx element;
14187
14188       element = CONST_VECTOR_ELT (x, i);
14189       fprintf (file, pattern, INTVAL (element));
14190     }
14191
14192   return 1;
14193 }
14194
14195 const char *
14196 arm_output_load_gr (rtx *operands)
14197 {
14198   rtx reg;
14199   rtx offset;
14200   rtx wcgr;
14201   rtx sum;
14202
14203   if (GET_CODE (operands [1]) != MEM
14204       || GET_CODE (sum = XEXP (operands [1], 0)) != PLUS
14205       || GET_CODE (reg = XEXP (sum, 0)) != REG
14206       || GET_CODE (offset = XEXP (sum, 1)) != CONST_INT
14207       || ((INTVAL (offset) < 1024) && (INTVAL (offset) > -1024)))
14208     return "wldrw%?\t%0, %1";
14209
14210   /* Fix up an out-of-range load of a GR register.  */
14211   output_asm_insn ("str%?\t%0, [sp, #-4]!\t@ Start of GR load expansion", & reg);
14212   wcgr = operands[0];
14213   operands[0] = reg;
14214   output_asm_insn ("ldr%?\t%0, %1", operands);
14215
14216   operands[0] = wcgr;
14217   operands[1] = reg;
14218   output_asm_insn ("tmcr%?\t%0, %1", operands);
14219   output_asm_insn ("ldr%?\t%0, [sp], #4\t@ End of GR load expansion", & reg);
14220
14221   return "";
14222 }
14223
14224 static rtx
14225 arm_struct_value_rtx (tree fntype ATTRIBUTE_UNUSED,
14226                       int incoming ATTRIBUTE_UNUSED)
14227 {
14228 #if 0
14229   /* FIXME: The ARM backend has special code to handle structure
14230          returns, and will reserve its own hidden first argument.  So
14231          if this macro is enabled a *second* hidden argument will be
14232          reserved, which will break binary compatibility with old
14233          toolchains and also thunk handling.  One day this should be
14234          fixed.  */
14235   return 0;
14236 #else
14237   /* Register in which address to store a structure value
14238      is passed to a function.  */
14239   return gen_rtx_REG (Pmode, ARG_REGISTER (1));
14240 #endif
14241 }
14242
14243 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.
14244
14245    On the ARM, PRETEND_SIZE is set in order to have the prologue push the last
14246    named arg and all anonymous args onto the stack.
14247    XXX I know the prologue shouldn't be pushing registers, but it is faster
14248    that way.  */
14249
14250 static void
14251 arm_setup_incoming_varargs (CUMULATIVE_ARGS *cum,
14252                             enum machine_mode mode ATTRIBUTE_UNUSED,
14253                             tree type ATTRIBUTE_UNUSED,
14254                             int *pretend_size,
14255                             int second_time ATTRIBUTE_UNUSED)
14256 {
14257   cfun->machine->uses_anonymous_args = 1;
14258   if (cum->nregs < NUM_ARG_REGS)
14259     *pretend_size = (NUM_ARG_REGS - cum->nregs) * UNITS_PER_WORD;
14260 }
14261
14262 /* Return nonzero if the CONSUMER instruction (a store) does not need
14263    PRODUCER's value to calculate the address.  */
14264
14265 int
14266 arm_no_early_store_addr_dep (rtx producer, rtx consumer)
14267 {
14268   rtx value = PATTERN (producer);
14269   rtx addr = PATTERN (consumer);
14270
14271   if (GET_CODE (value) == COND_EXEC)
14272     value = COND_EXEC_CODE (value);
14273   if (GET_CODE (value) == PARALLEL)
14274     value = XVECEXP (value, 0, 0);
14275   value = XEXP (value, 0);
14276   if (GET_CODE (addr) == COND_EXEC)
14277     addr = COND_EXEC_CODE (addr);
14278   if (GET_CODE (addr) == PARALLEL)
14279     addr = XVECEXP (addr, 0, 0);
14280   addr = XEXP (addr, 0);
14281
14282   return !reg_overlap_mentioned_p (value, addr);
14283 }
14284
14285 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14286    have an early register shift value or amount dependency on the
14287    result of PRODUCER.  */
14288
14289 int
14290 arm_no_early_alu_shift_dep (rtx producer, rtx consumer)
14291 {
14292   rtx value = PATTERN (producer);
14293   rtx op = PATTERN (consumer);
14294   rtx early_op;
14295
14296   if (GET_CODE (value) == COND_EXEC)
14297     value = COND_EXEC_CODE (value);
14298   if (GET_CODE (value) == PARALLEL)
14299     value = XVECEXP (value, 0, 0);
14300   value = XEXP (value, 0);
14301   if (GET_CODE (op) == COND_EXEC)
14302     op = COND_EXEC_CODE (op);
14303   if (GET_CODE (op) == PARALLEL)
14304     op = XVECEXP (op, 0, 0);
14305   op = XEXP (op, 1);
14306
14307   early_op = XEXP (op, 0);
14308   /* This is either an actual independent shift, or a shift applied to
14309      the first operand of another operation.  We want the whole shift
14310      operation.  */
14311   if (GET_CODE (early_op) == REG)
14312     early_op = op;
14313
14314   return !reg_overlap_mentioned_p (value, early_op);
14315 }
14316
14317 /* Return nonzero if the CONSUMER instruction (an ALU op) does not
14318    have an early register shift value dependency on the result of
14319    PRODUCER.  */
14320
14321 int
14322 arm_no_early_alu_shift_value_dep (rtx producer, rtx consumer)
14323 {
14324   rtx value = PATTERN (producer);
14325   rtx op = PATTERN (consumer);
14326   rtx early_op;
14327
14328   if (GET_CODE (value) == COND_EXEC)
14329     value = COND_EXEC_CODE (value);
14330   if (GET_CODE (value) == PARALLEL)
14331     value = XVECEXP (value, 0, 0);
14332   value = XEXP (value, 0);
14333   if (GET_CODE (op) == COND_EXEC)
14334     op = COND_EXEC_CODE (op);
14335   if (GET_CODE (op) == PARALLEL)
14336     op = XVECEXP (op, 0, 0);
14337   op = XEXP (op, 1);
14338
14339   early_op = XEXP (op, 0);
14340
14341   /* This is either an actual independent shift, or a shift applied to
14342      the first operand of another operation.  We want the value being
14343      shifted, in either case.  */
14344   if (GET_CODE (early_op) != REG)
14345     early_op = XEXP (early_op, 0);
14346
14347   return !reg_overlap_mentioned_p (value, early_op);
14348 }
14349
14350 /* Return nonzero if the CONSUMER (a mul or mac op) does not
14351    have an early register mult dependency on the result of
14352    PRODUCER.  */
14353
14354 int
14355 arm_no_early_mul_dep (rtx producer, rtx consumer)
14356 {
14357   rtx value = PATTERN (producer);
14358   rtx op = PATTERN (consumer);
14359
14360   if (GET_CODE (value) == COND_EXEC)
14361     value = COND_EXEC_CODE (value);
14362   if (GET_CODE (value) == PARALLEL)
14363     value = XVECEXP (value, 0, 0);
14364   value = XEXP (value, 0);
14365   if (GET_CODE (op) == COND_EXEC)
14366     op = COND_EXEC_CODE (op);
14367   if (GET_CODE (op) == PARALLEL)
14368     op = XVECEXP (op, 0, 0);
14369   op = XEXP (op, 1);
14370
14371   return (GET_CODE (op) == PLUS
14372           && !reg_overlap_mentioned_p (value, XEXP (op, 0)));
14373 }
14374
14375
14376 /* We can't rely on the caller doing the proper promotion when
14377    using APCS or ATPCS.  */
14378
14379 static bool
14380 arm_promote_prototypes (tree t ATTRIBUTE_UNUSED)
14381 {
14382     return !TARGET_AAPCS_BASED;
14383 }
14384
14385
14386 /* AAPCS based ABIs use short enums by default.  */
14387
14388 static bool
14389 arm_default_short_enums (void)
14390 {
14391   return TARGET_AAPCS_BASED;
14392 }
14393
14394
14395 /* AAPCS requires that anonymous bitfields affect structure alignment.  */
14396
14397 static bool
14398 arm_align_anon_bitfield (void)
14399 {
14400   return TARGET_AAPCS_BASED;
14401 }
14402
14403
14404 /* The generic C++ ABI says 64-bit (long long).  The EABI says 32-bit.  */
14405
14406 static tree
14407 arm_cxx_guard_type (void)
14408 {
14409   return TARGET_AAPCS_BASED ? integer_type_node : long_long_integer_type_node;
14410 }
14411
14412
14413 /* The EABI says test the least significan bit of a guard variable.  */
14414
14415 static bool
14416 arm_cxx_guard_mask_bit (void)
14417 {
14418   return TARGET_AAPCS_BASED;
14419 }
14420
14421
14422 /* The EABI specifies that all array cookies are 8 bytes long.  */
14423
14424 static tree
14425 arm_get_cookie_size (tree type)
14426 {
14427   tree size;
14428
14429   if (!TARGET_AAPCS_BASED)
14430     return default_cxx_get_cookie_size (type);
14431
14432   size = build_int_cst (sizetype, 8);
14433   return size;
14434 }
14435
14436
14437 /* The EABI says that array cookies should also contain the element size.  */
14438
14439 static bool
14440 arm_cookie_has_size (void)
14441 {
14442   return TARGET_AAPCS_BASED;
14443 }
14444
14445
14446 /* The EABI says constructors and destructors should return a pointer to
14447    the object constructed/destroyed.  */
14448
14449 static bool
14450 arm_cxx_cdtor_returns_this (void)
14451 {
14452   return TARGET_AAPCS_BASED;
14453 }
14454
14455 /* The EABI says that an inline function may never be the key
14456    method.  */
14457
14458 static bool
14459 arm_cxx_key_method_may_be_inline (void)
14460 {
14461   return !TARGET_AAPCS_BASED;
14462 }
14463
14464 /* The EABI says that the virtual table, etc., for a class must be
14465    exported if it has a key method.  The EABI does not specific the
14466    behavior if there is no key method, but there is no harm in
14467    exporting the class data in that case too.  */
14468
14469 static bool
14470 arm_cxx_export_class_data (void)
14471 {
14472   return TARGET_AAPCS_BASED;
14473 }
14474
14475 void
14476 arm_set_return_address (rtx source, rtx scratch)
14477 {
14478   arm_stack_offsets *offsets;
14479   HOST_WIDE_INT delta;
14480   rtx addr;
14481   unsigned long saved_regs;
14482
14483   saved_regs = arm_compute_save_reg_mask ();
14484
14485   if ((saved_regs & (1 << LR_REGNUM)) == 0)
14486     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14487   else
14488     {
14489       if (frame_pointer_needed)
14490         addr = plus_constant(hard_frame_pointer_rtx, -4);
14491       else
14492         {
14493           /* LR will be the first saved register.  */
14494           offsets = arm_get_frame_offsets ();
14495           delta = offsets->outgoing_args - (offsets->frame + 4);
14496
14497
14498           if (delta >= 4096)
14499             {
14500               emit_insn (gen_addsi3 (scratch, stack_pointer_rtx,
14501                                      GEN_INT (delta & ~4095)));
14502               addr = scratch;
14503               delta &= 4095;
14504             }
14505           else
14506             addr = stack_pointer_rtx;
14507
14508           addr = plus_constant (addr, delta);
14509         }
14510       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14511     }
14512 }
14513
14514
14515 void
14516 thumb_set_return_address (rtx source, rtx scratch)
14517 {
14518   arm_stack_offsets *offsets;
14519   HOST_WIDE_INT delta;
14520   int reg;
14521   rtx addr;
14522   unsigned long mask;
14523
14524   emit_insn (gen_rtx_USE (VOIDmode, source));
14525
14526   mask = thumb_compute_save_reg_mask ();
14527   if (mask & (1 << LR_REGNUM))
14528     {
14529       offsets = arm_get_frame_offsets ();
14530
14531       /* Find the saved regs.  */
14532       if (frame_pointer_needed)
14533         {
14534           delta = offsets->soft_frame - offsets->saved_args;
14535           reg = THUMB_HARD_FRAME_POINTER_REGNUM;
14536         }
14537       else
14538         {
14539           delta = offsets->outgoing_args - offsets->saved_args;
14540           reg = SP_REGNUM;
14541         }
14542       /* Allow for the stack frame.  */
14543       if (TARGET_BACKTRACE)
14544         delta -= 16;
14545       /* The link register is always the first saved register.  */
14546       delta -= 4;
14547
14548       /* Construct the address.  */
14549       addr = gen_rtx_REG (SImode, reg);
14550       if ((reg != SP_REGNUM && delta >= 128)
14551           || delta >= 1024)
14552         {
14553           emit_insn (gen_movsi (scratch, GEN_INT (delta)));
14554           emit_insn (gen_addsi3 (scratch, scratch, stack_pointer_rtx));
14555           addr = scratch;
14556         }
14557       else
14558         addr = plus_constant (addr, delta);
14559
14560       emit_move_insn (gen_rtx_MEM (Pmode, addr), source);
14561     }
14562   else
14563     emit_move_insn (gen_rtx_REG (Pmode, LR_REGNUM), source);
14564 }
14565
14566 /* Implements target hook vector_mode_supported_p.  */
14567 bool
14568 arm_vector_mode_supported_p (enum machine_mode mode)
14569 {
14570   if ((mode == V2SImode)
14571       || (mode == V4HImode)
14572       || (mode == V8QImode))
14573     return true;
14574
14575   return false;
14576 }
14577
14578 /* Implement TARGET_SHIFT_TRUNCATION_MASK.  SImode shifts use normal
14579    ARM insns and therefore guarantee that the shift count is modulo 256.
14580    DImode shifts (those implemented by lib1funcs.asm or by optabs.c)
14581    guarantee no particular behavior for out-of-range counts.  */
14582
14583 static unsigned HOST_WIDE_INT
14584 arm_shift_truncation_mask (enum machine_mode mode)
14585 {
14586   return mode == SImode ? 255 : 0;
14587 }